Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  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.  
  23. static const char
  24. rcsid[] = "$Id: hu_lib.c,v 1.3 1997/01/26 07:44:58 b1 Exp $";
  25.  
  26. #include <ctype.h>
  27.  
  28. #include "m_swap.h"
  29.  
  30. #include "doomdef.h"
  31.  
  32. #include "v_video.h"
  33.  
  34. #include "hu_lib.h"
  35. #include "r_local.h"
  36. #include "r_draw.h"
  37.  
  38. // boolean : whether the screen is always erased
  39. #define noterased viewwindowx
  40.  
  41. extern boolean  automapactive;  // in AM_map.c
  42.  
  43. void HUlib_init(void)
  44. {
  45. }
  46.  
  47. void HUlib_clearTextLine(hu_textline_t* t)
  48. {
  49.     t->len = 0;
  50.     t->l[0] = 0;
  51.     t->needsupdate = true;
  52. }
  53.  
  54. void
  55. HUlib_initTextLine
  56. ( hu_textline_t*        t,
  57.   int                   x,
  58.   int                   y,
  59.   patch_t**             f,
  60.   int                   sc )
  61. {
  62.     t->x = x;
  63.     t->y = y;
  64.     t->f = f;
  65.     t->sc = sc;
  66.     HUlib_clearTextLine(t);
  67. }
  68.  
  69. boolean
  70. HUlib_addCharToTextLine
  71. ( hu_textline_t*        t,
  72.   char                  ch )
  73. {
  74.  
  75.     if (t->len == HU_MAXLINELENGTH)
  76.         return false;
  77.     else
  78.     {
  79.         t->l[t->len++] = ch;
  80.         t->l[t->len] = 0;
  81.         t->needsupdate = 4;
  82.         return true;
  83.     }
  84.  
  85. }
  86.  
  87. boolean HUlib_delCharFromTextLine(hu_textline_t* t)
  88. {
  89.  
  90.     if (!t->len) return false;
  91.     else
  92.     {
  93.         t->l[--t->len] = 0;
  94.         t->needsupdate = 4;
  95.         return true;
  96.     }
  97.  
  98. }
  99.  
  100. void
  101. HUlib_drawTextLine
  102. ( hu_textline_t*        l,
  103.   boolean               drawcursor )
  104. {
  105.  
  106.     int                 i;
  107.     int                 w;
  108.     int                 x;
  109.     unsigned char       c;
  110.  
  111.     // draw the new stuff
  112.     x = l->x;
  113.     for (i=0;i<l->len;i++)
  114.     {
  115.         c = toupper(l->l[i]);
  116.         if (c != ' '
  117.             && c >= l->sc
  118.             && c <= '_')
  119.         {
  120.             w = SHORT(l->f[c - l->sc]->width);
  121.             if (x+w > SCREENWIDTH)
  122.                 break;
  123.             V_DrawPatchDirect(x, l->y, FG, l->f[c - l->sc]);
  124.             x += w;
  125.         }
  126.         else
  127.         {
  128.             x += 4;
  129.             if (x >= SCREENWIDTH)
  130.                 break;
  131.         }
  132.     }
  133.  
  134.     // draw the cursor if requested
  135.     if (drawcursor
  136.         && x + SHORT(l->f['_' - l->sc]->width) <= SCREENWIDTH)
  137.     {
  138.         V_DrawPatchDirect(x, l->y, FG, l->f['_' - l->sc]);
  139.     }
  140. }
  141.  
  142.  
  143. // sorta called by HU_Erase and just better darn get things straight
  144. void HUlib_eraseTextLine(hu_textline_t* l)
  145. {
  146.     int                 lh;
  147.     int                 y;
  148.     int                 yoffset;
  149.     static boolean      lastautomapactive = true;
  150.  
  151.     // Only erases when NOT in automap and the screen is reduced,
  152.     // and the text must either need updating or refreshing
  153.     // (because of a recent change back from the automap)
  154.  
  155.     if (!automapactive &&
  156.         viewwindowx && l->needsupdate)
  157.     {
  158.         lh = SHORT(l->f[0]->height) + 1;
  159.         for (y=l->y,yoffset=y*SCREENWIDTH ; y<l->y+lh ; y++,yoffset+=SCREENWIDTH)
  160.         {
  161.             if (y < viewwindowy || y >= viewwindowy + viewheight)
  162.                 R_VideoErase(yoffset, SCREENWIDTH); // erase entire line
  163.             else
  164.             {
  165.                 R_VideoErase(yoffset, viewwindowx); // erase left border
  166.                 R_VideoErase(yoffset + viewwindowx + viewwidth, viewwindowx);
  167.                 // erase right border
  168.             }
  169.         }
  170.     }
  171.  
  172.     lastautomapactive = automapactive;
  173.     if (l->needsupdate) l->needsupdate--;
  174.  
  175. }
  176.  
  177. void
  178. HUlib_initSText
  179. ( hu_stext_t*   s,
  180.   int           x,
  181.   int           y,
  182.   int           h,
  183.   patch_t**     font,
  184.   int           startchar,
  185.   boolean*      on )
  186. {
  187.  
  188.     int i;
  189.  
  190.     s->h = h;
  191.     s->on = on;
  192.     s->laston = true;
  193.     s->cl = 0;
  194.     for (i=0;i<h;i++)
  195.         HUlib_initTextLine(&s->l[i],
  196.                            x, y - i*(SHORT(font[0]->height)+1),
  197.                            font, startchar);
  198.  
  199. }
  200.  
  201. void HUlib_addLineToSText(hu_stext_t* s)
  202. {
  203.  
  204.     int i;
  205.  
  206.     // add a clear line
  207.     if (++s->cl == s->h)
  208.         s->cl = 0;
  209.     HUlib_clearTextLine(&s->l[s->cl]);
  210.  
  211.     // everything needs updating
  212.     for (i=0 ; i<s->h ; i++)
  213.         s->l[i].needsupdate = 4;
  214.  
  215. }
  216.  
  217. void
  218. HUlib_addMessageToSText
  219. ( hu_stext_t*   s,
  220.   char*         prefix,
  221.   char*         msg )
  222. {
  223.     HUlib_addLineToSText(s);
  224.     if (prefix)
  225.         while (*prefix)
  226.             HUlib_addCharToTextLine(&s->l[s->cl], *(prefix++));
  227.  
  228.     while (*msg)
  229.         HUlib_addCharToTextLine(&s->l[s->cl], *(msg++));
  230. }
  231.  
  232. void HUlib_drawSText(hu_stext_t* s)
  233. {
  234.     int i, idx;
  235.     hu_textline_t *l;
  236.  
  237.     if (!*s->on)
  238.         return; // if not on, don't draw
  239.  
  240.     // draw everything
  241.     for (i=0 ; i<s->h ; i++)
  242.     {
  243.         idx = s->cl - i;
  244.         if (idx < 0)
  245.             idx += s->h; // handle queue of lines
  246.        
  247.         l = &s->l[idx];
  248.  
  249.         // need a decision made here on whether to skip the draw
  250.         HUlib_drawTextLine(l, false); // no cursor, please
  251.     }
  252.  
  253. }
  254.  
  255. void HUlib_eraseSText(hu_stext_t* s)
  256. {
  257.  
  258.     int i;
  259.  
  260.     for (i=0 ; i<s->h ; i++)
  261.     {
  262.         if (s->laston && !*s->on)
  263.             s->l[i].needsupdate = 4;
  264.         HUlib_eraseTextLine(&s->l[i]);
  265.     }
  266.     s->laston = *s->on;
  267.  
  268. }
  269.  
  270. void
  271. HUlib_initIText
  272. ( hu_itext_t*   it,
  273.   int           x,
  274.   int           y,
  275.   patch_t**     font,
  276.   int           startchar,
  277.   boolean*      on )
  278. {
  279.     it->lm = 0; // default left margin is start of text
  280.     it->on = on;
  281.     it->laston = true;
  282.     HUlib_initTextLine(&it->l, x, y, font, startchar);
  283. }
  284.  
  285.  
  286. // The following deletion routines adhere to the left margin restriction
  287. void HUlib_delCharFromIText(hu_itext_t* it)
  288. {
  289.     if (it->l.len != it->lm)
  290.         HUlib_delCharFromTextLine(&it->l);
  291. }
  292.  
  293. void HUlib_eraseLineFromIText(hu_itext_t* it)
  294. {
  295.     while (it->lm != it->l.len)
  296.         HUlib_delCharFromTextLine(&it->l);
  297. }
  298.  
  299. // Resets left margin as well
  300. void HUlib_resetIText(hu_itext_t* it)
  301. {
  302.     it->lm = 0;
  303.     HUlib_clearTextLine(&it->l);
  304. }
  305.  
  306. void
  307. HUlib_addPrefixToIText
  308. ( hu_itext_t*   it,
  309.   char*         str )
  310. {
  311.     while (*str)
  312.         HUlib_addCharToTextLine(&it->l, *(str++));
  313.     it->lm = it->l.len;
  314. }
  315.  
  316. // wrapper function for handling general keyed input.
  317. // returns true if it ate the key
  318. boolean
  319. HUlib_keyInIText
  320. ( hu_itext_t*   it,
  321.   unsigned char ch )
  322. {
  323.  
  324.     if (ch >= ' ' && ch <= '_')
  325.         HUlib_addCharToTextLine(&it->l, (char) ch);
  326.     else
  327.         if (ch == KEY_BACKSPACE)
  328.             HUlib_delCharFromIText(it);
  329.         else
  330.             if (ch != KEY_ENTER)
  331.                 return false; // did not eat key
  332.  
  333.     return true; // ate the key
  334.  
  335. }
  336.  
  337. void HUlib_drawIText(hu_itext_t* it)
  338. {
  339.  
  340.     hu_textline_t *l = &it->l;
  341.  
  342.     if (!*it->on)
  343.         return;
  344.     HUlib_drawTextLine(l, true); // draw the line w/ cursor
  345.  
  346. }
  347.  
  348. void HUlib_eraseIText(hu_itext_t* it)
  349. {
  350.     if (it->laston && !*it->on)
  351.         it->l.needsupdate = 4;
  352.     HUlib_eraseTextLine(&it->l);
  353.     it->laston = *it->on;
  354. }
  355.  
  356.