home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 February / chip_20022115.iso / amiga / chipgame / scummvm_aga.lha / ScummVM_AGA / src / gui.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-01-05  |  10.4 KB  |  501 lines

  1. #include "stdafx.h"
  2. #include "scumm.h"
  3. #include "gui.h"
  4.  
  5. void Gui::draw(int start,int end) {
  6.     int i;
  7.  
  8.     if (end==-1)
  9.         end=start;
  10.  
  11.     for (i=0; i<sizeof(_widgets) / sizeof(_widgets[0]); i++) {
  12.         const GuiWidget *w = _widgets[i];
  13.         printf("gui.cpp: Draw() for (...\n");
  14.         if (w) {
  15.             _parentX = 0;
  16.             _parentY = 0;
  17.             while (w->_type != GUI_NONE) {
  18.                 if (w->_id>=start && w->_id<=end && (w->_page&(1<<_cur_page)) ) {
  19.                     printf("DrawWidget()\n");
  20.                     drawWidget(w);
  21.                     printf("Retour DrawWidget()\n");
  22.                 }
  23.                 if (w->_flags & GWF_PARENT) {
  24.                     _parentX += w->_x;
  25.                     _parentY += w->_y;
  26.                 }
  27.                 w++;
  28.             }
  29.         }
  30.     }
  31. }
  32.  
  33. const GuiWidget *Gui::widgetFromPos(int x, int y) {
  34.     int i;
  35.  
  36.     for (i=sizeof(_widgets) / sizeof(_widgets[0])-1; i>=0; i--) {
  37.         const GuiWidget *w = _widgets[i];
  38.         if (w) {
  39.             while (w->_type != GUI_NONE) {
  40.                 if ((w->_page&(1<<_cur_page)) && w->_id && 
  41.                         (uint16)(x-w->_x) < w->_w && (uint16)(y-w->_y) < w->_h)
  42.                     return w;
  43.                 if (w->_flags & GWF_PARENT) {
  44.                     x -= w->_x;
  45.                     y -= w->_y;
  46.                 }
  47.                 w++;
  48.             }
  49.         }
  50.     }
  51.     return NULL;
  52. }
  53.  
  54. void Gui::drawString(const char *str, int x, int y, int w, byte color, bool center) {
  55.     StringTab *st = &_s->string[5];
  56.     st->charset = 1;
  57.     st->center = center;
  58.     st->color = color;
  59.     st->xpos = x;
  60.     st->ypos = y;
  61.     st->right = x + w;
  62.     _s->_messagePtr = (byte*)str;
  63.     _s->drawString(5);
  64. }
  65.  
  66. void Gui::drawWidget(const GuiWidget *w) {
  67.     const char *s;
  68.     int x,y;
  69.  
  70.     x = w->_x;
  71.     y = w->_y;
  72.     printf("gui.cpp DrawWidget()\n");
  73.     if (w->_flags & GWF_CLEARBG)
  74.         widgetClear(w);
  75.         
  76.     if (w->_flags & GWF_BORDER) {
  77.         printf("Widget border..\n");
  78.         widgetBorder(w);
  79.         x += 4;
  80.         y += 4;
  81.     }
  82.  
  83.     switch(w->_type) {
  84.     case GUI_TEXT:
  85.         printf("switch: querystring()\n");
  86.         s = queryString(w->_string_number,w->_id);
  87.         printf("Retour query..\n");
  88.         if (s)
  89.             {
  90.             printf("Drawstrnig()\n");
  91.             drawString(s, x+_parentX, y+_parentY, w->_w,
  92.                 (_clickWidget && _clickWidget==w->_id) ? _textcolorhi : _textcolor,
  93.                 false);
  94.            }
  95.         break;
  96.     case GUI_IMAGE:
  97.         ;
  98.     }
  99. }
  100.  
  101. void Gui::widgetClear(const GuiWidget *wid) {
  102.     printf("gui.cpp: WidgetClear()\n");
  103.     int x,y,w,h;
  104.     int i,j;
  105.  
  106.     x = wid->_x;
  107.     y = wid->_y;
  108.     w = wid->_w;
  109.     h = wid->_h;
  110.     
  111.     printf("getBasePtr()\n");
  112.     byte *ptr = getBasePtr(x,y);
  113.     if(ptr==NULL)
  114.         return;
  115.  
  116.     printf("setVirtScreen..\n");
  117.     _s->setVirtscreenDirty(_vs, x+_parentX, y+_parentY, x+_parentX+w, y+_parentY+h);
  118.  
  119.     if (wid->_flags & GWF_BORDER) {
  120.         ptr += 320 + 1;
  121.         w-=2;
  122.         h-=2;
  123.     }
  124.     
  125.     while (--h>=0) {
  126.         for(i=0; i<w; i++)
  127.             ptr[i] = _bgcolor;
  128.         ptr += 320;
  129.     }
  130.  printf("Fin WidgetClear(α\n");
  131. }
  132.  
  133. void Gui::widgetBorder(const GuiWidget *w) {
  134.     int x = w->_x, y = w->_y;
  135.     int x2 = x + w->_w-1, y2 = y + w->_h-1;
  136.     int i;
  137.     byte tmp;
  138.     
  139.     hline(x+1,y,x2-1);
  140.     hline(x,y+1,x2);
  141.     vline(x,y+1,y2-1);
  142.     vline(x+1,y,y2);
  143.  
  144.     tmp = _color;
  145.     _color = _shadowcolor;
  146.  
  147.     hline(x+1,y2-1,x2);
  148.     hline(x+1,y2,x2-1);
  149.     vline(x2,y+1,y2-1);
  150.     vline(x2-1,y+1,y2);
  151.  
  152.     _color = tmp;
  153. }
  154.  
  155. void Gui::hline(int x, int y, int x2) {
  156.     moveto(x,y);
  157.     lineto(x2,y);
  158. }
  159.  
  160. void Gui::vline(int x, int y, int y2) {
  161.     moveto(x,y);
  162.     lineto(x,y2);
  163. }
  164.  
  165. void Gui::moveto(int x, int y) {
  166.     _curX = x;
  167.     _curY = y;
  168. }
  169.  
  170. byte *Gui::getBasePtr(int x, int y) {
  171.     x += _parentX;
  172.     y += _parentY;
  173.     _vs = _s->findVirtScreen(y);
  174.     if (_vs==NULL)
  175.         return NULL;
  176.     return _s->getResourceAddress(rtBuffer, _vs->number+1) + x + (y-_vs->topline)*320 + _s->_screenStartStrip*8;
  177. }
  178.  
  179. void Gui::lineto(int x, int y) {
  180.     byte *ptr;
  181.     int x2 = _curX;
  182.     int y2 = _curY;
  183.  
  184.     _curX = x;
  185.     _curY = y;
  186.  
  187.     if (x2 < x)
  188.         x2^=x^=x2^=x;
  189.  
  190.     if (y2 < y)
  191.         y2^=y^=y2^=y;
  192.  
  193.     ptr = getBasePtr(x, y);
  194.     if (ptr==NULL)
  195.         return;
  196.  
  197.     if (x==x2) {
  198.         /* vertical line */
  199.         while (y++ <= y2) {
  200.             *ptr = _color;
  201.             ptr += 320;
  202.         }
  203.     } else if (y==y2) {
  204.         /* horizontal line */
  205.         while (x++ <= x2) {
  206.             *ptr++ = _color;
  207.         }
  208.     }
  209. }
  210.  
  211. void Gui::leftMouseClick(int x, int y) {
  212.     const GuiWidget *w = widgetFromPos(x,y);
  213.     int old;
  214.     
  215.     _clickTimer = 0;
  216.  
  217.     old = _clickWidget;
  218.     _clickWidget = w?w->_id : 0;
  219.  
  220.     if (old)
  221.         draw(old);
  222.     if (_clickWidget) {
  223.         draw(_clickWidget);
  224.         if(w->_flags&GWF_DELAY)
  225.             _clickTimer = 5;
  226.         else
  227.             handleCommand(_clickWidget);
  228.     }
  229. }
  230.  
  231. const GuiWidget save_load_dialog[] = {
  232.     {GUI_STAT,0xFF,GWF_DEFAULT|GWF_PARENT,30,20,260,120,0,NULL},
  233.     {GUI_TEXT,0x01,0,40,5,128,16,0,1}, /* How may I serve you? */
  234.     {GUI_TEXT,0x02,0,40,5,128,16,0,2}, /* Select a game to LOAD */
  235.     {GUI_TEXT,0x04,0,40,5,128,16,0,3}, /* Name your SAVE game */
  236.  
  237.     {GUI_STAT,0xFF,GWF_DEFAULT,6,16,170,96,0,0},
  238.     {GUI_TEXT,0x01,GWF_DEFAULT,180,20,16,40,0,0},
  239.     {GUI_TEXT,0x01,GWF_DEFAULT,180,66,16,40,0,0},
  240.     {GUI_TEXT,0xFE,GWF_DEFAULT,180,20,16,40,1,0},
  241.     {GUI_TEXT,0xFE,GWF_DEFAULT,180,66,16,40,2,0},
  242.     
  243.     {GUI_TEXT,0x06,GWF_CLEARBG,10,20,160,10,20,0},
  244.     {GUI_TEXT,0x06,GWF_CLEARBG,10,30,160,10,21,0},
  245.     {GUI_TEXT,0x06,GWF_CLEARBG,10,40,160,10,22,0},
  246.     {GUI_TEXT,0x06,GWF_CLEARBG,10,50,160,10,23,0},
  247.     {GUI_TEXT,0x06,GWF_CLEARBG,10,60,160,10,24,0},
  248.     {GUI_TEXT,0x06,GWF_CLEARBG,10,70,160,10,25,0},
  249.     {GUI_TEXT,0x06,GWF_CLEARBG,10,80,160,10,26,0},
  250.     {GUI_TEXT,0x06,GWF_CLEARBG,10,90,160,10,27,0},
  251.     {GUI_TEXT,0x06,GWF_CLEARBG,10,100,160,10,28,0},
  252.  
  253.     {GUI_TEXT,0x01,GWF_BUTTON,200,25,54,16,3,4}, /* Save */
  254.     {GUI_TEXT,0x01,GWF_BUTTON,200,45,54,16,4,5}, /* Load */
  255.     {GUI_TEXT,0x01,GWF_BUTTON,200,65,54,16,5,6}, /* Play */
  256.     {GUI_TEXT,0x01,GWF_BUTTON,200,85,54,16,6,8}, /* Quit */
  257.  
  258.     {GUI_TEXT,0x02,GWF_BUTTON,200,50,54,16,7,7}, /* Cancel */
  259.  
  260.     {GUI_TEXT,0x04,GWF_BUTTON,200,45,54,16,8,9}, /* Ok */
  261.     {GUI_TEXT,0x04,GWF_BUTTON,200,65,54,16,7,7}, /* Cancel */
  262.  
  263.     {0}
  264. };
  265.  
  266. void Gui::handleCommand(int cmd) {
  267.     int lastEdit = _editString;
  268.     showCaret(false);
  269.  
  270.     switch(cmd) {
  271.     case 1: /* up button */
  272.         if (_slotIndex==0)
  273.             return;
  274.         getSavegameNames(_slotIndex-9);
  275.         draw(20,28);
  276.         return;
  277.     case 2:
  278.         if (_slotIndex > 80)
  279.             return;
  280.         getSavegameNames(_slotIndex+9);
  281.         draw(20,28);
  282.         return;
  283.     case 3: /* save button */
  284.         _cur_page = 2;
  285.         getSavegameNames(0);
  286.         draw(0,100);
  287.         return;
  288.     case 4: /* load button */
  289.         _cur_page = 1;
  290.         getSavegameNames(0);
  291.         draw(0,100);
  292.         return;
  293.     case 5: /* play button */
  294.         close();
  295.         return;
  296.     case 6: /* quit button */
  297.         exit(1);
  298.         return;
  299.     case 7: /* cancel button */
  300.         _cur_page = 0;
  301.         draw(0,100);
  302.         return;
  303.     case 8:
  304.         if (lastEdit==-1 || game_names[lastEdit][0]==0)
  305.             return;
  306.         _s->_saveLoadSlot = lastEdit + _slotIndex;
  307.         _s->_saveLoadCompatible = false;
  308.         _s->_saveLoadFlag = 1;
  309.         memcpy(_s->_saveLoadName, game_names[lastEdit], sizeof(_s->_saveLoadName));
  310.         close();
  311.         return;
  312.     default:
  313.         if (cmd>=20 && cmd<=28) {
  314.             if(_cur_page==1) {
  315.                 if (valid_games[cmd-20]) {
  316.                     _s->_saveLoadSlot = cmd-20+_slotIndex;
  317.                     _s->_saveLoadCompatible = false;
  318.                     _s->_saveLoadFlag = 2;
  319.                     close();
  320.                 }
  321.                 return;
  322.             } else if (_cur_page==2) {
  323.                 _clickWidget = cmd;
  324.                 editString(cmd - 20);
  325.             }
  326.         }
  327.     }
  328.  
  329. }
  330.  
  331. void Gui::getSavegameNames(int start) {
  332.     int i;
  333.     _slotIndex = start;
  334.     for(i=0; i<9; i++,start++) {
  335.         valid_games[i] = _s->getSavegameName(start, game_names[i]);
  336.     }
  337. }
  338.  
  339. const byte string_map_table_v6[] = {
  340.     117, /* How may I serve you? */
  341.     109, /* Select a game to LOAD */
  342.     108, /* Name your SAVE game */
  343.   96,  /* Save */
  344.     97,  /* Load */
  345.     98,  /* Play */
  346.     99,  /* Cancel */
  347.     100, /* Quit */
  348.     101, /* Ok */
  349. };
  350.  
  351. const byte string_map_table_v5[] = {
  352.     0, /* How may I serve you? */
  353.     20, /* Select a game to LOAD */
  354.     21, /* Name your SAVE game */
  355.   7,  /* Save */
  356.     8,  /* Load */
  357.     9,  /* Play */
  358.     10,  /* Cancel */
  359.     11, /* Quit */
  360.     12, /* Ok */
  361. };
  362.  
  363. const char *Gui::queryString(int string, int id) {
  364.     static char namebuf[64];
  365.     if (id>=20 && id<=28) {
  366.         printf("id debut\n");
  367.         printf("%2d. %s\n",id-20+_slotIndex, game_names[id-20]);
  368.         sprintf(namebuf, "%2d. %s", id-20+_slotIndex, game_names[id-20]);
  369.         printf("id fin...\n");
  370.         return namebuf;
  371.     }
  372.  
  373.     if (_s->_majorScummVersion==6) {
  374.     printf("major 6\n");
  375.         string = _s->_vars[string_map_table_v6[string-1]];
  376.     } else {
  377.         string = string_map_table_v5[string-1];
  378.     }
  379.     
  380.     printf("getStringAddress...\n");
  381.     return (char*)_s->getStringAddress(string);
  382. }
  383.  
  384. void Gui::showCaret(bool show) {
  385.     int i;
  386.     char *s;
  387.  
  388.     if (_editString==-1)
  389.         return;
  390.  
  391.     i = _editLen;
  392.     s = game_names[_editString];
  393.  
  394.     if (show) {
  395.         if (i < 31) {
  396.             s[i] = '_';
  397.             s[i+1] = 0;
  398.         }
  399.     } else {
  400.         s[i] = 0;
  401.     }
  402.     
  403.     draw(_editString + 20);
  404.  
  405.     if (!show)
  406.         _editString = -1;
  407. }
  408.  
  409. void Gui::editString(int i) {
  410.     char *s = game_names[i];
  411.     if(!valid_games[i]) {
  412.         valid_games[i] = true;
  413.         *s = 0;
  414.     }
  415.     _editString = i;
  416.     _editLen = strlen(s);
  417.     showCaret(true);
  418. }
  419.  
  420. void Gui::addLetter(char letter) {
  421.     if (_editString==-1)
  422.         return;
  423.  
  424.     if (letter==13) {
  425.         handleCommand(8);
  426.         return;
  427.     }
  428.  
  429.     if (letter>=32 && letter<128 && _editLen < 31) {
  430.         game_names[_editString][_editLen++] = letter;    
  431.     } else if (letter==8 && _editLen>0) {
  432.         _editLen--;
  433.     }
  434.     showCaret(true);
  435. }
  436.  
  437. void Gui::saveLoadDialog() {
  438.     printf("gui.cpp: save_load()\n");
  439.     _widgets[0] = save_load_dialog;
  440.     _editString = -1;
  441.     _cur_page = 0;
  442.     _active = 1;
  443. }
  444.  
  445. byte Gui::getDefaultColor(int color) {
  446.     if (_s->_majorScummVersion == 6) {
  447.         if (color==8) color=1;
  448.         return _s->readArray(110, 0, color);
  449.     } else {
  450.         return _s->getStringAddress(21)[color];
  451.     }
  452. }
  453.  
  454.  
  455. void Gui::init(Scumm *s) {
  456.     _s = s;
  457.     _bgcolor = getDefaultColor(0);
  458.     _color = getDefaultColor(1);
  459.     _textcolor = getDefaultColor(2);
  460.     _textcolorhi = getDefaultColor(6);
  461.     _shadowcolor = getDefaultColor(8);
  462. }
  463.  
  464. void Gui::loop() {
  465.     if (_active==1) {
  466.         printf("gui.cpp: Active==1\n");
  467.         _active++;
  468.         draw(0,100);
  469.         _s->_cursorAnimate++;
  470.         _s->gdi._cursorActive = 1;
  471.     }
  472.     
  473.     printf("gui.cpp: GetKeyInput()\n");
  474.     _s->getKeyInput(0);
  475.     printf("Apres getkey...\n");
  476.     if (_s->_mouseButStat&0x8000) {
  477.         leftMouseClick(_s->mouse.x, _s->mouse.y);
  478.     } else if (_s->_lastKeyHit) {
  479.         printf("gui.cpp: Add letter\n");
  480.         addLetter(_s->_lastKeyHit);
  481.     }
  482.     
  483.     if (_clickTimer && !--_clickTimer) {
  484.         printf("gui.cpp:Click !=...\n");
  485.         int old = _clickWidget;
  486.         _clickWidget = 0;
  487.         draw(old);
  488.         handleCommand(old);
  489.     }
  490.     printf("gui.cpp: s>draw...\n");
  491.     _s->drawDirtyScreenParts();
  492.     _s->_mouseButStat = 0;
  493. }
  494.  
  495. void Gui::close() {
  496.     _s->_fullRedraw = true;
  497.     _s->_completeScreenRedraw = true;
  498.     _s->_cursorAnimate--;
  499.     _active = false;
  500. }
  501.