hu_lib.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "Precompiled.h"
  21. #include "globaldata.h"
  22. #include <ctype.h>
  23. #include "doomdef.h"
  24. #include "v_video.h"
  25. #include "m_swap.h"
  26. #include "hu_lib.h"
  27. #include "r_local.h"
  28. #include "r_draw.h"
  29. // qboolean : whether the screen is always erased
  30. void HUlib_init(void)
  31. {
  32. }
  33. void HUlib_clearTextLine(hu_textline_t* t)
  34. {
  35. t->len = 0;
  36. t->l[0] = 0;
  37. t->needsupdate = true;
  38. }
  39. void
  40. HUlib_initTextLine
  41. ( hu_textline_t* t,
  42. int x,
  43. int y,
  44. patch_t** f,
  45. int sc )
  46. {
  47. t->x = x;
  48. t->y = y;
  49. t->f = f;
  50. t->sc = sc;
  51. HUlib_clearTextLine(t);
  52. }
  53. qboolean
  54. HUlib_addCharToTextLine
  55. ( hu_textline_t* t,
  56. char ch )
  57. {
  58. if (t->len == HU_MAXLINELENGTH)
  59. return false;
  60. else
  61. {
  62. t->l[t->len++] = ch;
  63. t->l[t->len] = 0;
  64. t->needsupdate = 4;
  65. return true;
  66. }
  67. }
  68. qboolean HUlib_delCharFromTextLine(hu_textline_t* t)
  69. {
  70. if (!t->len) return false;
  71. else
  72. {
  73. t->l[--t->len] = 0;
  74. t->needsupdate = 4;
  75. return true;
  76. }
  77. }
  78. void
  79. HUlib_drawTextLine
  80. ( hu_textline_t* l,
  81. qboolean drawcursor )
  82. {
  83. int i;
  84. int w;
  85. int x;
  86. unsigned char c;
  87. // draw the new stuff
  88. x = l->x;
  89. for (i=0;i<l->len;i++)
  90. {
  91. c = toupper(l->l[i]);
  92. if (c != ' '
  93. && c >= l->sc
  94. && c <= '_')
  95. {
  96. w = SHORT(l->f[c - l->sc]->width);
  97. if (x+w > SCREENWIDTH)
  98. break;
  99. V_DrawPatchDirect(x, l->y, FG, l->f[c - l->sc]);
  100. x += w;
  101. }
  102. else
  103. {
  104. x += 4;
  105. if (x >= SCREENWIDTH)
  106. break;
  107. }
  108. }
  109. // draw the cursor if requested
  110. if (drawcursor
  111. && x + SHORT(l->f['_' - l->sc]->width) <= SCREENWIDTH)
  112. {
  113. V_DrawPatchDirect(x, l->y, FG, l->f['_' - l->sc]);
  114. }
  115. }
  116. // sorta called by HU_Erase and just better darn get things straight
  117. void HUlib_eraseTextLine(hu_textline_t* l)
  118. {
  119. int lh;
  120. int y;
  121. int yoffset;
  122. // Only erases when NOT in automap and the screen is reduced,
  123. // and the text must either need updating or refreshing
  124. // (because of a recent change back from the automap)
  125. if (!::g->automapactive &&
  126. ::g->viewwindowx && l->needsupdate)
  127. {
  128. lh = SHORT(l->f[0]->height) + 1;
  129. for (y=l->y,yoffset=y*SCREENWIDTH ; y<l->y+lh ; y++,yoffset+=SCREENWIDTH)
  130. {
  131. if (y < ::g->viewwindowy || y >= ::g->viewwindowy + ::g->viewheight)
  132. R_VideoErase(yoffset, SCREENWIDTH); // erase entire line
  133. else
  134. {
  135. R_VideoErase(yoffset, ::g->viewwindowx); // erase left border
  136. R_VideoErase(yoffset + ::g->viewwindowx + ::g->viewwidth, ::g->viewwindowx);
  137. // erase right border
  138. }
  139. }
  140. }
  141. ::g->lastautomapactive = ::g->automapactive;
  142. if (l->needsupdate) l->needsupdate--;
  143. }
  144. void
  145. HUlib_initSText
  146. ( hu_stext_t* s,
  147. int x,
  148. int y,
  149. int h,
  150. patch_t** font,
  151. int startchar,
  152. qboolean* on )
  153. {
  154. int i;
  155. s->h = h;
  156. s->on = on;
  157. s->laston = true;
  158. s->cl = 0;
  159. for (i=0;i<h;i++)
  160. HUlib_initTextLine(&s->l[i],
  161. x, y - i*(SHORT(font[0]->height)+1),
  162. font, startchar);
  163. }
  164. void HUlib_addLineToSText(hu_stext_t* s)
  165. {
  166. int i;
  167. // add a clear line
  168. if (++s->cl == s->h)
  169. s->cl = 0;
  170. HUlib_clearTextLine(&s->l[s->cl]);
  171. // everything needs updating
  172. for (i=0 ; i<s->h ; i++)
  173. s->l[i].needsupdate = 4;
  174. }
  175. void
  176. HUlib_addMessageToSText
  177. ( hu_stext_t* s,
  178. const char* prefix,
  179. const char* msg )
  180. {
  181. HUlib_addLineToSText(s);
  182. if (prefix)
  183. while (*prefix)
  184. HUlib_addCharToTextLine(&s->l[s->cl], *(prefix++));
  185. while (*msg)
  186. HUlib_addCharToTextLine(&s->l[s->cl], *(msg++));
  187. }
  188. void HUlib_drawSText(hu_stext_t* s)
  189. {
  190. int i, idx;
  191. hu_textline_t *l;
  192. if (!*s->on)
  193. return; // if not on, don't draw
  194. // draw everything
  195. for (i=0 ; i<s->h ; i++)
  196. {
  197. idx = s->cl - i;
  198. if (idx < 0)
  199. idx += s->h; // handle queue of ::g->lines
  200. l = &s->l[idx];
  201. // need a decision made here on whether to skip the draw
  202. HUlib_drawTextLine(l, false); // no cursor, please
  203. }
  204. }
  205. void HUlib_eraseSText(hu_stext_t* s)
  206. {
  207. int i;
  208. for (i=0 ; i<s->h ; i++)
  209. {
  210. if (s->laston && !*s->on)
  211. s->l[i].needsupdate = 4;
  212. HUlib_eraseTextLine(&s->l[i]);
  213. }
  214. s->laston = *s->on;
  215. }
  216. void
  217. HUlib_initIText
  218. ( hu_itext_t* it,
  219. int x,
  220. int y,
  221. patch_t** font,
  222. int startchar,
  223. qboolean* on )
  224. {
  225. it->lm = 0; // default left margin is start of text
  226. it->on = on;
  227. it->laston = true;
  228. HUlib_initTextLine(&it->l, x, y, font, startchar);
  229. }
  230. // The following deletion routines adhere to the left margin restriction
  231. void HUlib_delCharFromIText(hu_itext_t* it)
  232. {
  233. if (it->l.len != it->lm)
  234. HUlib_delCharFromTextLine(&it->l);
  235. }
  236. void HUlib_eraseLineFromIText(hu_itext_t* it)
  237. {
  238. while (it->lm != it->l.len)
  239. HUlib_delCharFromTextLine(&it->l);
  240. }
  241. // Resets left margin as well
  242. void HUlib_resetIText(hu_itext_t* it)
  243. {
  244. it->lm = 0;
  245. HUlib_clearTextLine(&it->l);
  246. }
  247. void
  248. HUlib_addPrefixToIText
  249. ( hu_itext_t* it,
  250. char* str )
  251. {
  252. while (*str)
  253. HUlib_addCharToTextLine(&it->l, *(str++));
  254. it->lm = it->l.len;
  255. }
  256. // wrapper function for handling general keyed input.
  257. // returns true if it ate the key
  258. qboolean
  259. HUlib_keyInIText
  260. ( hu_itext_t* it,
  261. unsigned char ch )
  262. {
  263. if (ch >= ' ' && ch <= '_')
  264. HUlib_addCharToTextLine(&it->l, (char) ch);
  265. else
  266. if (ch == KEY_BACKSPACE)
  267. HUlib_delCharFromIText(it);
  268. else
  269. if (ch != KEY_ENTER)
  270. return false; // did not eat key
  271. return true; // ate the key
  272. }
  273. void HUlib_drawIText(hu_itext_t* it)
  274. {
  275. hu_textline_t *l = &it->l;
  276. if (!*it->on)
  277. return;
  278. HUlib_drawTextLine(l, true); // draw the line w/ cursor
  279. }
  280. void HUlib_eraseIText(hu_itext_t* it)
  281. {
  282. if (it->laston && !*it->on)
  283. it->l.needsupdate = 4;
  284. HUlib_eraseTextLine(&it->l);
  285. it->laston = *it->on;
  286. }