home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / gchsrc31 / buttons.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-27  |  4.7 KB  |  203 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  This file is part of the Atari graphical interface for GNU Chess,
  4. //  and is Copyright 1992 by Warwick W. Allison.
  5. //
  6. //  You are free to copy and modify these sources, provided you acknowledge
  7. //  the origin by retaining this notice, and adhere to the conditions
  8. //  of the CHESS General Public License described in the main chess file
  9. //  gnuchess.cc.
  10. //
  11. //////////////////////////////////////////////////////////////////////////////
  12.  
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include "Buttons.h"
  16. #include "Help.h"
  17.  
  18. GameButton **Button;
  19. int MaxNumButtons=20;
  20. int NumButtons;
  21. int MyLevel=1;
  22. const int TopButtonX=0;
  23. const int TopButtonY=5;
  24.  
  25. static char* const IncTextTemplate="Up level (now %d)";
  26. static char* const DecTextTemplate="Down level (now %d)";
  27. static char IncText[25];
  28. static char DecText[25];
  29.  
  30. Sprite *Indicator;
  31. static int LevX,LevY;
  32.  
  33. DrawIndicator()
  34. {
  35.     Indicator->MoveTo(LevX+MyLevel*3,LevY);
  36.     Indicator->Draw();
  37.     Pages->Flop();
  38.     Indicator->Draw();
  39.     Pages->Flop();
  40.  
  41.     sprintf(IncText,IncTextTemplate,MyLevel);
  42.     sprintf(DecText,DecTextTemplate,MyLevel);
  43.     NoteHelpChanged(IncText);
  44.     NoteHelpChanged(DecText);
  45. }
  46.  
  47. class LevelButton : public GameButton
  48. {
  49. public:
  50.     LevelButton(Sprite* S,int i,int x,int y,int w,int h,char *commandstr,char *HelpText,int Inc);
  51.     virtual char* const Push();
  52.     virtual void Release();
  53.  
  54. private:
  55.     int inc;
  56. };
  57.  
  58. LevelButton::LevelButton(Sprite* S,int i,int x,int y,int w,int h,char *commandstr,char *HelpText,int Inc) :
  59.     GameButton(S,i,x,y,w,h,commandstr,HelpText),
  60.     inc(Inc)
  61. { }
  62.  
  63. void InitButtons(Screen& Sc)
  64. {
  65.     ColourIncarnation* Pointer=new ColourIncarnation(4);
  66.     Pointer->GetImage(Sc,304,166);
  67.     Indicator=new Sprite(Pointer);
  68.  
  69.     Incarnation **P=new Incarnation*[2];
  70.     P[0]=new WideColourIncarnation(15);
  71.     P[1]=new ColourIncarnation(15);
  72.  
  73.     P[0]->GetImage(Sc,288,185);
  74.     P[1]->GetImage(Sc,288,155);
  75.  
  76.     Sprite *S=new Sprite(P,2);
  77.  
  78.     Button=new GameButton*[MaxNumButtons];
  79.     int b=0;
  80.     int w=32;
  81.     int h=15;
  82.     int x=TopButtonX;
  83.     int y=TopButtonY-h;
  84.  
  85.     // First column
  86.     Button[b++]=new GameButton(S,0,x,y+=h,w,h,"quit","Quit GNUchess");
  87.     Button[b++]=new GameButton(S,0,x,y+=h,w,h,"new","New game");
  88.     Button[b++]=new GameButton(S,0,x,y+=h,w,h,"help","Information");
  89.     Button[b++]=new GameButton(S,0,x,y+=h,w,h,"edit","Edit board");
  90.     Button[b++]=new GameButton(S,0,x,y+=h,w,h,"book","Disable openings book");
  91.  
  92.     Button[b++]=new LevelButton(S,1,x,y+=h,w/2,h,"level",DecText,-1);
  93.     Button[b++]=new LevelButton(S,1,x+w/2,y,w/2,h,"level",IncText,+1);
  94.     LevX=x-3;
  95.     LevY=y+15-4;
  96.  
  97.     // Second column
  98.     y=TopButtonY-h;
  99.     x+=w;
  100.     Button[b++]=new GameButton(S,0,x,y+=h,w,h,"get","Load game");
  101.     Button[b++]=new GameButton(S,0,x,y+=h,w,h,"save","Save game");
  102.     Button[b++]=new GameButton(S,0,x,y+=h,w,h,"savecnf","Save configuration");
  103.     Button[b++]=new GameButton(S,0,x,y+=h,w,h,"list","List game to CHESS.LST");
  104.     y+=h;// modem
  105.     y+=h;// humans
  106.  
  107.     // Third column
  108.     y=TopButtonY-h;
  109.     x+=w;
  110.     Button[b++]=new GameButton(S,0,x,y+=h,w,h,"both","Start CPU duel");
  111.     Button[b++]=new GameButton(S,0,x,y+=h,w,h,"remove","Back two moves");
  112.     Button[b++]=new GameButton(S,0,x,y+=h,w,h,"reverse","Reverse board");
  113.     Button[b++]=new GameButton(S,0,x,y+=h,w,h,"hint","Hint");
  114.     Button[b++]=new GameButton(S,0,x,y+=h,w,h,"switch","Advance (Swap sides)");
  115.     Button[b++]=new GameButton(S,0,x,y+=h,w,h,"undo","Undo move");
  116.  
  117.     NumButtons=b;
  118.  
  119.     DrawIndicator();
  120. }
  121.  
  122. static GameButton* Pushed;
  123.  
  124. void ReleaseButton()
  125. {
  126.     if (Pushed) {
  127.         Pushed->Release();
  128.         Pushed=0;
  129.     }
  130. }
  131.  
  132. GameButton::GameButton(Sprite* S,int i,int x,int y,int w,int h,char *commandstr,char *HelpText)
  133.     : X(x), Y(y), W(w), H(h), cmdstr(commandstr), Pusher(S), I(i)
  134. {
  135.     AddHelp(x,y,w,h,HelpText);
  136. }
  137.  
  138. void GameButton::Release()
  139. {
  140.     Pusher->Wipe();
  141.     Pages->Flop();
  142.     Pusher->Wipe();
  143.     Pages->Flop();
  144. }
  145.  
  146. char* const GameButton::Push()
  147. {
  148.     ReleaseButton();
  149.     Pushed=this;
  150.     Pusher->ShapeTo(I);
  151.     Pusher->MoveTo(X,Y);
  152.     Pusher->Draw();
  153.     Pages->Flop();
  154.     Pusher->Draw();
  155.     Pages->Flop();
  156.     return cmdstr;
  157. }
  158.  
  159. char* const LevelButton::Push()
  160. {
  161.     MyLevel+=inc;
  162.     if (MyLevel<1) {
  163.         MyLevel=1;
  164.         return " ";
  165.     } else if (MyLevel>10) {
  166.         MyLevel=10;
  167.         return " ";
  168.     }
  169.  
  170.     Indicator->Wipe();
  171.     Pages->Flop();
  172.     Indicator->Wipe();
  173.     Pages->Flop();
  174.  
  175.     char* const result=GameButton::Push();
  176.  
  177.     DrawIndicator();
  178.     return result;
  179. }
  180.  
  181. void LevelButton::Release()
  182. {
  183.     Indicator->Wipe();
  184.     Pages->Flop();
  185.     Indicator->Wipe();
  186.     Pages->Flop();
  187.  
  188.     GameButton::Release();
  189.  
  190.     DrawIndicator();
  191. }
  192.  
  193. bool PressButton(int x, int y, char *cmd)
  194. {
  195.     for (int b=0; b<NumButtons; b++) {
  196.         if (Button[b]->Contains(x,y)) {
  197.             strcpy(cmd,Button[b]->Push());
  198.             return TRUE;
  199.         }
  200.     }
  201.     return FALSE;
  202. }
  203.