home *** CD-ROM | disk | FTP | other *** search
/ Quake++ for Quake / Quake++.iso / quake / edquake / code / input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-12  |  12.4 KB  |  494 lines

  1. /*EdQuake v0.50 source code
  2.   (c) Copyright 1996 Scott Mitting
  3.   email:smitting@netusa1.net
  4.   ----------------------------------
  5.   INPUT.C  -  Keyboard/mouse input routines /w windows
  6.  
  7.   __variables:
  8.   <no global variables>
  9.   __functions:
  10.   button_t button(int x0, int y0, int x1, int y1, char *text)
  11.                 -- creates a button and returns a pointer
  12.   button_t clearbutton(int x0, int y0, int x1, int y1)
  13.                 -- creates a button without drawing it
  14.   int getkey()            -- stable getkey function with extended
  15.   char *getstr(int x, int y)    -- gets string without button support
  16.   int hitbutton(int x, int y, button_t b)
  17.                 -- using a button pointer finds out if
  18.                    a button is currently being hit
  19.   button_t inputbox(int x0, int y0, int x1, int y1, char *title)
  20.                 -- creates a windows with a close button
  21.   void scrollbar(int x, int y0, int y1, int cur, int max)
  22.                 -- updates/creates a scrollbar
  23.   button_t scrollbutton(int x, int y, char *text)
  24.                 -- prefab buttons for up and down
  25.   int selectentry()        -- selects an entry from a list
  26.   char *wgetstr(char * def, int x, int y, button_t ok, button_t cancel, button_t xbutton)
  27.                 -- gets a string from the keyboard with
  28.                    button support
  29.   char *wgetstringbox(char *menubar, char *text, char *def)
  30.                 -- high-level function for getting
  31.                    a string.  draws an input box
  32.                    with buttons and checks on buttons
  33.   ----------------------------------
  34.   the entire source code is under renovation to make it easier
  35.   to understand.  happy coding.
  36. */
  37.  
  38. /* don't use too many low-level functions as they change rapidly
  39.    with absolutely no thought of other programmers.  High-level
  40.    functions are more stable */
  41.  
  42. #include <alloc.h>
  43. #include <string.h>
  44. #include "input.h"
  45. #include "svgacc.h"
  46. #include "pak.h"
  47. #include "grfx.h"
  48.  
  49. int list[200];
  50. int numlist;
  51.  
  52. button_t clearbutton(int x0, int y0, int x1, int y1)
  53. {
  54.       button_t ret;
  55.       ret.x0 = x0;
  56.       ret.x1 = x1;
  57.       ret.y0 = y0;
  58.       ret.y1 = y1;
  59.       return ret;
  60. }
  61.  
  62. button_t button(int x0, int y0, int x1, int y1, char *text)
  63. {
  64.       button_t ret;
  65.       mousehide();
  66.   drwfillbox(SET,LTGRAY, x0,y0,x1,y1);
  67.    drwstring(SET,BLACK,LTGRAY,text,x0 + 2,y0 + 1);
  68.       drwbox(SET,WHITE,x0    ,y0    ,x1 ,y1);
  69.       drwbox(SET,DKGRAY, x0 + 1,y0 + 1,x1 ,y1);
  70.       ret.x0 = x0;
  71.       ret.x1 = x1;
  72.       ret.y0 = y0;
  73.       ret.y1 = y1;
  74.       mouseshow();
  75.       return ret;
  76. }
  77.  
  78. button_t inputbox(int x0, int y0, int x1, int y1, char *title)
  79. {
  80.       mousehide();
  81.       drwbox(SET, DKGRAY,x0    ,y0    ,x1    ,y1    );
  82.       drwbox(SET, LTGRAY,x0 + 1,y0 + 1,x1 - 1,y1 - 1);
  83.       drwbox(SET,  WHITE,x0 + 2,y0 + 2,x1 - 2,y1 - 2);
  84.  
  85.       drwbox(SET, DKGRAY,x0 + 5,y0 + 19,x1 - 5,y1 - 5);
  86.       drwbox(SET, LTGRAY,x0 + 4,y0 + 18,x1 - 4,y1 - 4);
  87.       drwbox(SET,  WHITE,x0 + 3,y0 + 17,x1 - 3,y1 - 3);
  88.   drwfillbox(SET, LTGRAY,x0 + 6,y0 + 20,x1 - 6,y1 - 6);
  89.  
  90.   drwfillbox(SET, BLUE,x0 + 3,y0 +  3,x1 - 3, y0 + 16);
  91.    drwstring(SET, WHITE, BLUE,title,x0 + 27,y0 + 3);
  92.       return button(x1 - 13, y0 + 1, x1 -3 , y0+16, "x");
  93. }
  94.  
  95. int hitbutton(int x, int y, button_t b)
  96. {
  97.    if (b.x0 > x) return 0;
  98.    if (b.y0 > y) return 0;
  99.    if (b.x1 < x) return 0;
  100.    if (b.y1 < y) return 0;
  101.    return 1;
  102. }
  103.  
  104. int getkey()
  105. {
  106.    int k;
  107.    k = getch();
  108.    if (!k) k = getch() + 1000;
  109.    return k;
  110. }
  111.  
  112. char *getstr(int x, int y)
  113. {
  114.     char *data;
  115.     int length, ext;
  116.     char c;
  117.  
  118.     data = malloc(128);
  119.     sprintf(data,"_");
  120.     length = 0;
  121.     ext = 0;
  122.     c = '\0';
  123.  
  124.     while(!ext) {
  125.         drwstring(SET,WHITE,BLACK,data,x,y);
  126.         c = (char)getch();
  127.         if(c>=' ' && c<='~') {
  128.         data[length] = c;
  129.         data[length+1] = '_';
  130.         data[length+2] = '\0';
  131.         length++;
  132.         }
  133.         else
  134.         if (c==8 && length>0) {
  135.             drwstring(SET,WHITE,BLACK,"   ",x+length*8,y);
  136.             length--;
  137.             data[length] = '_';
  138.             data[length+1] = '\0';
  139.         }
  140.         else
  141.             if (c==13)
  142.             ext = 1;
  143.     }
  144.     if (length==0) {
  145.         free(data);
  146.         return 0;
  147.     }
  148.  
  149.     data[length] = '\0';
  150.     return data;
  151. }
  152.  
  153. char *wgetstr(char * def, int x, int y, button_t ok, button_t cancel, button_t xbutton)
  154. {
  155.     char *data;
  156.     int length, ext;
  157.     int mx, my, mbuts;
  158.     char c;
  159.  
  160.     data = malloc(128);
  161.     length = strlen(def);
  162.     memset(data,0,128);
  163.     movmem(def, data,length);
  164.     length = 0;
  165.     while (data[length] != 0) length++;
  166.     data[length] = '_';
  167.     ext = 0;
  168.     c = '\0';
  169.  
  170.     while(!ext) {
  171.         drwstring(SET,LTGRAY,BLACK,data,x,y);
  172.         while (!kbhit())
  173.         {
  174.            mousestatus(&mx,&my,&mbuts);
  175.            if (mbuts & 1)//check for buttons
  176.            {
  177.           if (hitbutton(mx,my,ok))
  178.           {    data[length] = '\0';
  179.             return data;}
  180.           if (hitbutton(mx,my,cancel))
  181.           {     free(data);
  182.             return 0;}
  183.           if (hitbutton(mx,my,xbutton))
  184.           {     free(data);
  185.             return 0;}
  186.            }
  187.         }
  188.         c = (char)getch();
  189.         if(c>=' ' && c<='~') {
  190.         data[length] = c;
  191.         data[length+1] = '_';
  192.         data[length+2] = '\0';
  193.         length++;
  194.         }
  195.         else
  196.         if (c==8 && length>0) {
  197.             drwstring(SET,WHITE,BLACK,"   ",x+length*8,y);
  198.             length--;
  199.             data[length] = '_';
  200.             data[length+1] = '\0';
  201.         }
  202.         else
  203.             if (c==13)
  204.             ext = 1;
  205.         else
  206.             if (c==27)
  207.             {ext = 1; length=0;}
  208.     }
  209.     if (length==0) {
  210.         free(data);
  211.         return 0;
  212.     }
  213.  
  214.     data[length] = '\0';
  215.     return data;
  216. }
  217.  
  218. int query(char *info)
  219. {
  220.    int k = 0;
  221.    int mx, my, mbuts;
  222.    button_t ok, cancel, xbutton;
  223.    mousehide();
  224.    xbutton = inputbox(200,200,420,275,"Query");
  225.    ok = button(220,250,290,265,"   Ok");
  226.    cancel = button(330,250,400,265," Cancel");
  227.    drwstring(SET,BLACK,LTGRAY,info,210,230);
  228.    mouseshow();
  229.    while (k != 27)
  230.    {
  231.         while (!kbhit() && !mbuts)
  232.         {
  233.            mousestatus(&mx,&my,&mbuts);
  234.            if (mbuts)
  235.            {
  236.           if (hitbutton(mx,my,ok)) return 1;
  237.           if (hitbutton(mx,my,cancel)) return 0;
  238.           if (hitbutton(mx,my,xbutton)) return 0;
  239.            }
  240.         }
  241.         if (!mbuts) k = getkey();
  242.         mbuts = 0;
  243.      if (k == a_ENTER) return 1;
  244.    }
  245.    return 0;
  246. }
  247.  
  248.  
  249. int selectentry(int e)
  250. {
  251.    int k;
  252.    int curE = 1, showE = e;
  253.    int mx, my, mbuts;
  254.    button_t up, down, xbutton;
  255.    mousehide();
  256.    xbutton = inputbox(0,20,320,312,"Select PAK Entry");
  257.    up = scrollbutton(302,280,"");
  258.    down = scrollbutton(302,294,"");
  259.    mouseshow();
  260.    while (k != 27)
  261.    {
  262.      showmenu(showE, curE);
  263.      scrollbar(302,40,278,showE+curE,pak.tablelen);
  264.         while (!kbhit() && !mbuts)
  265.         {
  266.            mousestatus(&mx,&my,&mbuts);
  267.            if (mbuts & 1)//scroll
  268.            {
  269.           if (hitbutton(mx,my,up)) showE-=1;
  270.           if (hitbutton(mx,my,down)) showE+=1;
  271.           if (hitbutton(mx,my,xbutton)) return -1;
  272.           //select
  273.           if (mx < 300 && my < 300 && my > 40)
  274.              curE =  ((my - 40) / 14)+1;
  275.            }
  276.            if (mbuts & 2)//scroll fast
  277.            {
  278.           if (hitbutton(mx,my,up)) showE-=3;
  279.           if (hitbutton(mx,my,down)) showE+=3;
  280.           if (mx < 300 && my < 300 && my > 40) return showE+curE;
  281.            }
  282.  
  283.         }
  284.         if (!mbuts) k = getkey();
  285.         mbuts = 0;
  286.  
  287.      if (k == a_DOWN) curE++;
  288.      if (k == a_UP)   curE--;
  289.      if (k == a_PGDN) showE+=15;
  290.      if (k == a_PGUP) showE-=15;
  291.      if (k == a_HOME) curE=1;
  292.      if (k == a_END)  curE=19;
  293.      if (k == a_ENTER) return showE+curE;
  294.      if (k == a_ESC)   return -1;
  295.      if (curE > 19) {showE++;curE=19;}
  296.      if (curE < 1)  {showE--;curE=1;}
  297.      if (showE < 0) {showE = 0; curE = 1;}
  298.      if (showE + 19 > pak.tablelen) {showE = pak.tablelen - 19; curE=19;}
  299.    }
  300.    return -1;
  301. }
  302.  
  303. void scrollbar(int x, int y0, int y1, int cur, int max)
  304. {
  305.    int yloc = 0;
  306.    yloc = y0+((y1-15)-y0)*((float)cur/max);
  307.    drwbox(SET,BLACK,x,y0,x+11,y1);
  308.    drwfillbox(SET,DKGRAY,x+1,y0+1,x+10,y1-1);
  309.    drwbox(SET, WHITE,x + 1,yloc+1,x + 10 ,yloc+15);
  310.    drwbox(SET, BLACK, x + 2,yloc+2,x + 10 ,yloc+15);
  311.    drwfillbox(SET, LTGRAY, x+2,yloc+2,x+9,yloc+14);
  312. }
  313.  
  314. button_t scrollbutton(int x, int y, char *text)
  315. {
  316.       button_t ret;
  317.       drwfillbox(SET,LTGRAY,x,y,x+11,y+12);
  318.       drwstring(SET,BLACK,LTGRAY,text,x+2,y);
  319.       drwline(SET,BLACK,x+11,y,x+11,y+12);
  320.       drwline(SET,BLACK,x,y+12,x+11,y+12);
  321.       drwline(SET,WHITE,x,y,x+11,y);
  322.       drwline(SET,WHITE,x,y,x,y+12);
  323.       ret.x0 = x;
  324.       ret.x1 = x+11;
  325.       ret.y0 = y;
  326.       ret.y1 = y+12;
  327.       return ret;
  328. }
  329.  
  330. char *wgetstringbox(char *menubar, char *text, char *def)
  331. {
  332.    button_t ok;
  333.    button_t cancel;
  334.    button_t xbutton;
  335.    xbutton = inputbox(100,150,430,225,menubar);
  336. //   drwstring(SET, BLACK,LTGRAY,pakentry.name,110,170);
  337.    drwstring(SET, BLACK,LTGRAY,text,110,185);
  338.    drwfillbox(SET,BLACK,110,200,350,214);
  339.    ok = button(356,183,420,197,"   ok");
  340.    cancel = button(356,203,420,217," cancel");
  341.    return wgetstr(def,113,200, ok, cancel, xbutton);
  342. }
  343.  
  344. void showmenulist(int showE, int curE)
  345. {
  346.    int t,qt;
  347.    char buf[80];
  348.    qt = 40;
  349.    mousehide();
  350.    for (t = 1; t < 20; t++)
  351.    {
  352.      getentry(list[t + showE]);
  353.      if (t != curE)
  354.      {
  355.     drwfillbox(SET, BLACK,6,qt,300,qt+14);
  356.     sprintf(buf, "%s",       pakentry.name);
  357.     drwstring(SET, WHITE,BLACK,buf,11,qt);
  358.      }
  359.      else
  360.      {
  361.     drwfillbox(SET,RED,6,qt,300,qt+14);
  362.     sprintf(buf, "%s",       pakentry.name);
  363.     drwstring(SET, YELLOW,RED,buf,11,qt);
  364.      }
  365.      qt+=14;
  366.    }
  367.    mouseshow();
  368. }
  369.  
  370.  
  371. int selectentrykeyword(int e, char *keyword)
  372. {
  373.    int k;
  374.    int curE = 1, showE = e;
  375.    int mx, my, mbuts;
  376.    int curt = 0;
  377.    button_t up, down, xbutton;
  378.    mousehide();
  379.    numlist = 0;
  380.    xbutton = inputbox(0,20,320,312,"Select PAK Entry");
  381.    up = scrollbutton(302,280,"");
  382.    down = scrollbutton(302,294,"");
  383.    //get entries;
  384.    for (curt = 0; curt < pak.tablelen; curt++)
  385.    {
  386.       getentry(curt);
  387.       if (strstr(pakentry.name,keyword))
  388.     list[numlist++] = curt;
  389.    }
  390.    numlist--;
  391.    mouseshow();
  392.    while (k != 27)
  393.    {
  394.      showmenulist(showE, curE);
  395.      scrollbar(302,40,278,showE+curE,numlist);
  396.         while (!kbhit() && !mbuts)
  397.         {
  398.            mousestatus(&mx,&my,&mbuts);
  399.            if (mbuts & 1)//scroll
  400.            {
  401.           if (hitbutton(mx,my,up)) showE-=1;
  402.           if (hitbutton(mx,my,down)) showE+=1;
  403.           if (hitbutton(mx,my,xbutton)) return -1;
  404.           //select
  405.           if (mx < 300 && my < 300 && my > 40)
  406.              curE =  ((my - 40) / 14)+1;
  407.            }
  408.            if (mbuts & 2)//scroll fast
  409.            {
  410.           if (hitbutton(mx,my,up)) showE-=3;
  411.           if (hitbutton(mx,my,down)) showE+=3;
  412.           if (mx < 300 && my < 300 && my > 40) return list[showE+curE];
  413.            }
  414.  
  415.         }
  416.         if (!mbuts) k = getkey();
  417.         mbuts = 0;
  418.  
  419.      if (k == a_DOWN) curE++;
  420.      if (k == a_UP)   curE--;
  421.      if (k == a_PGDN) showE+=15;
  422.      if (k == a_PGUP) showE-=15;
  423.      if (k == a_HOME) curE=1;
  424.      if (k == a_END)  curE=19;
  425.      if (k == a_ENTER) return list[curE+showE];
  426.      if (k == a_ESC)   return -1;
  427.      if (curE > 19) {showE++;curE=19;}
  428.      if (curE < 1)  {showE--;curE=1;}
  429.      if (showE < 0) {showE = 0; curE = 1;}
  430.      if (numlist > 19)
  431.        if (showE + 19 > numlist) {showE = numlist - 19; curE=19;}
  432.    }
  433.    return -1;
  434. }
  435.  
  436. int wgetintbox(char *menubar, char *text, int def)
  437. {
  438.    button_t ok, cancel, xbutton;
  439.    char *buf, *data, c;
  440.    int length, ext, mx, my, mbuts;
  441.    xbutton = inputbox(100,150,430,225,menubar);
  442.    drwstring(SET, BLACK,LTGRAY,text,110,185);
  443.    drwfillbox(SET,BLACK,110,200,350,214);
  444.    ok = button(356,183,420,197,"   ok");
  445.    cancel = button(356,203,420,217," cancel");
  446.    sprintf(buf,"%i",def);
  447.    data = malloc(128);
  448.    memset(data,0,128);
  449.    length = strlen(buf);
  450.    memmove(data,buf,length);
  451.    length = 0;
  452.    while (data[length] != 0) length++;
  453.    data[length] = '_';
  454.    ext = 0;
  455.    c = '\0';
  456.    while(!ext)
  457.    {
  458.        drwstring(SET,LTGRAY,BLACK,data,113,200);
  459.        while (!kbhit())
  460.        {
  461.       mousestatus(&mx,&my,&mbuts);
  462.       if (mbuts & 1)//check for buttons
  463.       {
  464.          if (hitbutton(mx,my,ok)) ext = 1;
  465.          if (hitbutton(mx,my,cancel)) length = -1;
  466.          if (hitbutton(mx,my,xbutton)) length = -1;
  467.       }
  468.        }
  469.        c = getch();
  470.        if(c >= ' ' && c <= '~')
  471.        {
  472.       data[length] = c;
  473.       data[length+1] = '_';
  474.       data[length+2] = '\0';
  475.       length++;
  476.        }
  477.        if (c == 8 && length > 0)
  478.        {
  479.       drwstring(SET,WHITE,BLACK,"   ",113+length*8,200);
  480.       length--;
  481.       data[length] = '_';
  482.       data[length+1] = '\0';
  483.        }
  484.        if (c == 13) ext = 1;
  485.        if (c == 27) length = -1;
  486.        if (length > 100) length = 100;
  487.        if (length == -1) ext = 1;
  488.    }
  489.    if (length > 0) def = atoi(data);
  490.    free(data);
  491.    return def;
  492. }
  493.  
  494.