home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume11 / reve / part05 / events.c < prev    next >
C/C++ Source or Header  |  1990-11-15  |  11KB  |  386 lines

  1. /*LINTLIBRARY*/
  2.  
  3. /*  @(#)events.c 1.7 90/10/18
  4.  *
  5.  *  Procedures for handling various events in reve.
  6.  *
  7.  *  Copyright (C) 1990 - Rich Burridge & Yves Gallot.
  8.  *  All rights reserved.
  9.  *
  10.  *  Permission is given to distribute these sources, as long as the
  11.  *  introductory messages are not removed, and no monies are exchanged.
  12.  *
  13.  *  You are forbidden from using Reve as is, or in a modified state, in
  14.  *  any tournaments, without the permission of the authors.
  15.  *
  16.  *  No responsibility is taken for any errors on inaccuracies inherent
  17.  *  either to the comments or the code of this program, but if reported
  18.  *  (see README file), then an attempt will be made to fix them.
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include "reve.h"
  23. #include "color.h"
  24. #include "extern.h"
  25.  
  26.  
  27. void
  28. check_button_down(item)
  29. enum panel_type item ;
  30. {
  31.   int n ;
  32.  
  33.   n = (int) item ;
  34.   if (items[n].x == -1) return ;
  35.   if ((curx >  items[n].x) && (curx < (items[n].x + items[n].width)) &&
  36.       (cury >  items[n].y) && (cury < (items[n].y + items[n].height)))
  37.     {
  38.       down = nextc ;
  39.       itemno = n ;
  40.       but_inverted = itemno ;
  41.       draw_button((enum panel_type) itemno, C_LGREY, BUT_INVERT) ;
  42.     }
  43. }
  44.  
  45.  
  46. void
  47. check_cycle_down(item)
  48. enum panel_type item ;
  49. {
  50.   int ix, n, reply ;
  51.  
  52.   n = (int) item ;
  53.   ix = items[n].x + (3 * BWIDTH) + (2 * BGAP) - CWIDTH ;
  54.        if ((curx > ix) && (curx < (ix + (items[n].width / 2))) &&
  55.            (cury > items[n].y) && (cury < (items[n].y + items[n].height)) &&
  56.             nextc != RIGHT_DOWN)
  57.     {
  58.       direction = INCREMENT ;
  59.       down = nextc ;
  60.       itemno = n ;
  61.       draw_cycle((enum panel_type) n, C_LGREY, CY_LINVERT) ;
  62.     }
  63.   else if ((curx > ix) && (curx < (ix + items[n].width)) &&
  64.            (cury > items[n].y) && (cury < (items[n].y + items[n].height)) &&
  65.             nextc != RIGHT_DOWN)
  66.     {
  67.       direction = DECREMENT ;
  68.       down = nextc ;
  69.       itemno = n ;
  70.       draw_cycle((enum panel_type) n, C_LGREY, CY_RINVERT) ;
  71.     }
  72.   else if ((curx > ix) && (curx < (ix + items[n].width)) &&
  73.            (cury > items[n].y) && (cury < (items[n].y + items[n].height)))
  74.     {
  75.       direction = NONE ;
  76.       down = RIGHT_DOWN ;
  77.       itemno = n ;
  78.       reply = do_menu((enum panel_type) itemno) ;
  79.       if (reply)
  80.         {                                                   
  81.           nextc = RIGHT_UP ;                                
  82.           handle_item(reply - 1) ;
  83.           draw_cycle((enum panel_type) itemno, C_LGREY, CY_NORMAL) ;
  84.         }
  85.     }
  86. }
  87.  
  88.  
  89. void
  90. check_item_down()
  91. {
  92.   int n ;
  93.  
  94.   for (n = 0; n < MAXITEMS; n++)
  95.     switch (items[n].type)
  96.       {
  97.         case P_BUTTON  : check_button_down((enum panel_type) n) ;
  98.                          break ;
  99.         case P_CYCLE   : check_cycle_down((enum panel_type) n) ;
  100.                          break ;
  101.         case P_MESSAGE : /* do nothing. */ ;
  102.       }
  103. }
  104.  
  105.  
  106. void
  107. check_item_up()
  108. {
  109.   if ((nextc == LEFT_UP   && down == LEFT_DOWN)   ||
  110.       (nextc == MIDDLE_UP && down == MIDDLE_DOWN) ||
  111.       (nextc == RIGHT_UP  && down == RIGHT_DOWN))
  112.     {
  113.       if (items[itemno].type == P_BUTTON && but_inverted == -1) return ;
  114.       handle_item(items[itemno].value) ;
  115.       if (items[itemno].type == P_BUTTON && items[itemno].x != -1)
  116.         draw_button((enum panel_type) itemno, C_LGREY, BUT_NORMAL) ;
  117.       else if (items[itemno].type == P_CYCLE)
  118.         draw_cycle((enum panel_type) itemno, C_LGREY, CY_NORMAL) ;
  119.     } 
  120. }
  121.  
  122.  
  123. void
  124. do_action()
  125. {
  126.   switch (nextc)
  127.     {
  128.       case MOUSE_MOVING : draw_piece(next_player, piece_x, piece_y, RINV) ;
  129.                           piece_x = curx - PIECE_RAD ;
  130.                           piece_y = cury - PIECE_RAD ;
  131.                           draw_piece(next_player, piece_x, piece_y, RINV) ;
  132.                           break ;
  133.                           
  134.       case ENTER_WINDOW : 
  135.       case EXIT_WINDOW  : set_cursor(CANVASCUR) ;
  136.                           draw_piece(next_player, piece_x, piece_y, RINV) ;
  137.                           cmode = (enum cantype) ((int) cmode - 1) ;
  138.                           break ;
  139.                           
  140.       case LEFT_DOWN    : 
  141.       case LEFT_UP      :
  142.       case MIDDLE_DOWN  :
  143.       case MIDDLE_UP    :
  144.       case RIGHT_DOWN   :
  145.       case RIGHT_UP     : set_cursor(CANVASCUR) ;
  146.                           do_selection(nextc) ;
  147.     }                 
  148. }
  149.  
  150.  
  151. void
  152. do_cycle_key(item, ch)
  153. enum panel_type item ;
  154. int ch ;
  155. {
  156.   int val ;
  157.  
  158.   if (!validkey)
  159.     {
  160.       validkey = cur_ch ;
  161.       message(PANEL_MES, items[(int) item].text) ;
  162.     }
  163.   else
  164.     {
  165.       val = -1 ;
  166.       switch (item)
  167.         {
  168.           case BLACK_PLAYS :
  169.                WHITE_PLAYS : if (item == BLACK_PLAYS) curx = 0 ;
  170.                              else curx = BBORDER + (3*(BWIDTH+BGAP)) ;
  171.                              switch (ch)
  172.                                {
  173.                                  case 'c' :
  174.                                  case 'C' : val = 1 ;
  175.                                             items[(int) item].value = val ;
  176.                                             set_cycle(item,
  177.                                                       player_values[val]) ;
  178.                                             break ;
  179.                                  case 'h' :
  180.                                  case 'H' : val = 0 ;
  181.                                             items[(int) item].value = val ;
  182.                                             set_cycle(item,
  183.                                                       player_values[val]) ;
  184.                                             break ;
  185.                                  default  : return ;
  186.                                }
  187.           case DIFFICULTY     : if (ch >= '1' && ch <= '9') val = ch - '1' ;
  188.                                 items[(int) item].value = val ;
  189.                                 set_cycle(item, diff_values[val]) ;
  190.                                 break ;
  191.           case NOTES          : switch (ch)
  192.                                {
  193.                                  case 'n' :
  194.                                  case 'N' : val = 0 ;
  195.                                             items[(int) item].value = val ;
  196.                                             set_cycle(item, notes_values[val]) ;
  197.                                             break ;
  198.                                  case 'y' :
  199.                                  case 'Y' : val = 1 ;
  200.                                             items[(int) item].value = val ;
  201.                                             set_cycle(item, notes_values[val]) ;
  202.                                             break ;
  203.                                  default  : return ;
  204.                                }
  205.         }
  206.       if (val != -1)
  207.         {
  208.           validkey = 0 ;
  209.           message(PANEL_MES, "") ;
  210.           items[(int) item].value = item_value = val ;
  211.           direction = NONE ;
  212.           (*items[(int) item].func)() ;
  213.         }
  214.     }
  215. }
  216.  
  217.  
  218. void
  219. do_key_move(n1, n2)
  220. int n1, n2 ;
  221. {
  222.   move = (n2 - '1') * BOARD_SIZE + (n1 - 'a') ;
  223.   next_player = (int) cmode - 1 ;
  224.   cmode = (enum cantype) ((int) cmode + 1) ;
  225.   make_move() ;
  226.   validkey = 0 ;
  227. }
  228.  
  229.  
  230. void
  231. get_xy(n, x, y)      /* Return piece coordinates given board index. */
  232. int n, *x,*y ;
  233. {
  234.   *x = (n & 7) * CELL_SIZE + BBORDER + PIECE_MARGIN ;
  235.   *y = (n >> 3) * CELL_SIZE + BBORDER + PIECE_MARGIN ;
  236. }
  237.  
  238.  
  239. void
  240. handle_board_event()
  241. {
  242.   switch (cmode)
  243.     {
  244.       case WHITE_START  :
  245.       case BLACK_START  : next_player = (int) cmode - 1 ;
  246.                           if (nextc == LEFT_DOWN || nextc == MIDDLE_DOWN ||
  247.                               nextc == RIGHT_DOWN)
  248.                             {
  249.                               set_cursor(NOCURSOR) ;
  250.                               piece_x = curx - BBORDER - (PIECE_RAD) ;
  251.                               piece_y = cury - BBORDER - (PIECE_RAD) ;
  252.                               draw_piece(next_player, piece_x, piece_y, RINV) ;
  253.                               cmode = (enum cantype) ((int) cmode + 1) ;
  254.                             }
  255.                           break ;
  256.       case WHITE_MOVING :
  257.       case BLACK_MOVING : do_action() ;
  258.     }
  259. }
  260.  
  261.  
  262. void
  263. handle_event()
  264. {
  265.   process_event() ;
  266.  
  267.        if (nextc == FRAME_REPAINT) init_canvas() ;
  268.   else if (nextc == KEYBOARD)      handle_key() ;
  269.   else if (nextc == EXIT_WINDOW && but_inverted != -1)
  270.     {
  271.       draw_button((enum panel_type) but_inverted, C_LGREY, BUT_NORMAL) ;
  272.       but_inverted = -1 ;
  273.       down = 0 ;
  274.     }
  275.   else if (cury > (CY + BBORDER)) handle_board_event() ;
  276.   else if (nextc == LEFT_UP || nextc == MIDDLE_UP || nextc == RIGHT_UP)
  277.     check_item_up() ;
  278.   else if (nextc == LEFT_DOWN || nextc == MIDDLE_DOWN || nextc == RIGHT_DOWN)
  279.     check_item_down() ;
  280. }
  281.  
  282.  
  283. void
  284. handle_item(val)
  285. int val ;
  286. {
  287.   items[itemno].value = item_value = val ;
  288.   (*items[itemno].func)() ;
  289.   but_inverted = -1 ;
  290.   down = 0 ;
  291. }
  292.  
  293.  
  294. void
  295. handle_key()     /* Process the latest key that the user has pressed. */
  296. {
  297.   char str[9] ;  /* To display half move position. */
  298.   int nextc ;
  299.  
  300.   if (tinput)
  301.     {
  302.       get_filename() ;
  303.       return ;
  304.     }
  305.   if (cur_ch == ESCAPE) validkey = 0 ;
  306.   if (validkey)
  307.     {
  308.       nextc = cur_ch ;
  309.       cur_ch = validkey ;
  310.     }
  311.   switch (cur_ch)
  312.     {
  313.       case 'B' : do_cycle_key(BLACK_PLAYS, nextc) ;    /* Cycle items. */
  314.                  break ;
  315.       case 'D' : do_cycle_key(DIFFICULTY,  nextc) ;
  316.                  break ;
  317.       case 'N' : do_cycle_key(NOTES,       nextc) ;
  318.                  break ;
  319.       case 'W' : do_cycle_key(WHITE_PLAYS, nextc) ;
  320.                  break ;
  321.  
  322.       case 'l' : last() ;                              /* Button items. */
  323.                  break ;
  324.       case 'L' : curx = 0 ;
  325.                  draw_textfield() ;
  326.                  break ;
  327.       case 'n' : new_game() ;
  328.                  break ;
  329.       case 'S' : curx = BBORDER + (2*(BWIDTH+BGAP)) ;
  330.                  draw_textfield() ;
  331.                  break ;
  332.       case 's' : suggest() ;
  333.                  break ;
  334.       case 'u' : undo() ;
  335.                  break ;
  336.  
  337.       case 'q' : destroy_frame() ;
  338.                  exit(0) ;
  339.  
  340.       case '1' :
  341.       case '2' :
  342.       case '3' :
  343.       case '4' :
  344.       case '5' :
  345.       case '6' :
  346.       case '7' :
  347.       case '8' : if (!validkey)
  348.                    {
  349.                      validkey = cur_ch ;
  350.                      SPRINTF(str, "Move: %c", cur_ch) ;
  351.                      message(PANEL_MES, str) ;
  352.                    }
  353.                  else if (nextc >= 'a' && nextc <= 'h')
  354.                    {
  355.                      SPRINTF(str, "Move: %c%c", cur_ch, nextc) ;
  356.                      message(PANEL_MES, str) ;
  357.                      do_key_move(nextc, cur_ch) ;
  358.                    }
  359.                  else validkey = 0 ;
  360.                  break ;
  361.       case 'a' :
  362.       case 'b' :
  363.       case 'c' :
  364.       case 'd' :
  365.       case 'e' :
  366.       case 'f' :
  367.       case 'g' :
  368.       case 'h' : if (!validkey)
  369.                    {
  370.                      validkey = cur_ch ;
  371.                      SPRINTF(str, "Move: %c", cur_ch) ;
  372.                      message(PANEL_MES, str) ;
  373.                    }
  374.                  else if (nextc >= '1' && nextc <= '8')
  375.                    {
  376.                      SPRINTF(str, "Move: %c%c", cur_ch, nextc) ;
  377.                      message(PANEL_MES, str) ;
  378.                      do_key_move(cur_ch, nextc) ;
  379.                    }
  380.                  else validkey = 0 ;
  381.                  break ;
  382.       default  : message(PANEL_MES, "") ;
  383.                  validkey = 0 ;
  384.     }
  385. }
  386.