hu_lib.c 6.3 KB

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