home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
games
/
volume11
/
reve
/
part05
/
events.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-11-15
|
11KB
|
386 lines
/*LINTLIBRARY*/
/* @(#)events.c 1.7 90/10/18
*
* Procedures for handling various events in reve.
*
* Copyright (C) 1990 - Rich Burridge & Yves Gallot.
* All rights reserved.
*
* Permission is given to distribute these sources, as long as the
* introductory messages are not removed, and no monies are exchanged.
*
* You are forbidden from using Reve as is, or in a modified state, in
* any tournaments, without the permission of the authors.
*
* No responsibility is taken for any errors on inaccuracies inherent
* either to the comments or the code of this program, but if reported
* (see README file), then an attempt will be made to fix them.
*/
#include <stdio.h>
#include "reve.h"
#include "color.h"
#include "extern.h"
void
check_button_down(item)
enum panel_type item ;
{
int n ;
n = (int) item ;
if (items[n].x == -1) return ;
if ((curx > items[n].x) && (curx < (items[n].x + items[n].width)) &&
(cury > items[n].y) && (cury < (items[n].y + items[n].height)))
{
down = nextc ;
itemno = n ;
but_inverted = itemno ;
draw_button((enum panel_type) itemno, C_LGREY, BUT_INVERT) ;
}
}
void
check_cycle_down(item)
enum panel_type item ;
{
int ix, n, reply ;
n = (int) item ;
ix = items[n].x + (3 * BWIDTH) + (2 * BGAP) - CWIDTH ;
if ((curx > ix) && (curx < (ix + (items[n].width / 2))) &&
(cury > items[n].y) && (cury < (items[n].y + items[n].height)) &&
nextc != RIGHT_DOWN)
{
direction = INCREMENT ;
down = nextc ;
itemno = n ;
draw_cycle((enum panel_type) n, C_LGREY, CY_LINVERT) ;
}
else if ((curx > ix) && (curx < (ix + items[n].width)) &&
(cury > items[n].y) && (cury < (items[n].y + items[n].height)) &&
nextc != RIGHT_DOWN)
{
direction = DECREMENT ;
down = nextc ;
itemno = n ;
draw_cycle((enum panel_type) n, C_LGREY, CY_RINVERT) ;
}
else if ((curx > ix) && (curx < (ix + items[n].width)) &&
(cury > items[n].y) && (cury < (items[n].y + items[n].height)))
{
direction = NONE ;
down = RIGHT_DOWN ;
itemno = n ;
reply = do_menu((enum panel_type) itemno) ;
if (reply)
{
nextc = RIGHT_UP ;
handle_item(reply - 1) ;
draw_cycle((enum panel_type) itemno, C_LGREY, CY_NORMAL) ;
}
}
}
void
check_item_down()
{
int n ;
for (n = 0; n < MAXITEMS; n++)
switch (items[n].type)
{
case P_BUTTON : check_button_down((enum panel_type) n) ;
break ;
case P_CYCLE : check_cycle_down((enum panel_type) n) ;
break ;
case P_MESSAGE : /* do nothing. */ ;
}
}
void
check_item_up()
{
if ((nextc == LEFT_UP && down == LEFT_DOWN) ||
(nextc == MIDDLE_UP && down == MIDDLE_DOWN) ||
(nextc == RIGHT_UP && down == RIGHT_DOWN))
{
if (items[itemno].type == P_BUTTON && but_inverted == -1) return ;
handle_item(items[itemno].value) ;
if (items[itemno].type == P_BUTTON && items[itemno].x != -1)
draw_button((enum panel_type) itemno, C_LGREY, BUT_NORMAL) ;
else if (items[itemno].type == P_CYCLE)
draw_cycle((enum panel_type) itemno, C_LGREY, CY_NORMAL) ;
}
}
void
do_action()
{
switch (nextc)
{
case MOUSE_MOVING : draw_piece(next_player, piece_x, piece_y, RINV) ;
piece_x = curx - PIECE_RAD ;
piece_y = cury - PIECE_RAD ;
draw_piece(next_player, piece_x, piece_y, RINV) ;
break ;
case ENTER_WINDOW :
case EXIT_WINDOW : set_cursor(CANVASCUR) ;
draw_piece(next_player, piece_x, piece_y, RINV) ;
cmode = (enum cantype) ((int) cmode - 1) ;
break ;
case LEFT_DOWN :
case LEFT_UP :
case MIDDLE_DOWN :
case MIDDLE_UP :
case RIGHT_DOWN :
case RIGHT_UP : set_cursor(CANVASCUR) ;
do_selection(nextc) ;
}
}
void
do_cycle_key(item, ch)
enum panel_type item ;
int ch ;
{
int val ;
if (!validkey)
{
validkey = cur_ch ;
message(PANEL_MES, items[(int) item].text) ;
}
else
{
val = -1 ;
switch (item)
{
case BLACK_PLAYS :
WHITE_PLAYS : if (item == BLACK_PLAYS) curx = 0 ;
else curx = BBORDER + (3*(BWIDTH+BGAP)) ;
switch (ch)
{
case 'c' :
case 'C' : val = 1 ;
items[(int) item].value = val ;
set_cycle(item,
player_values[val]) ;
break ;
case 'h' :
case 'H' : val = 0 ;
items[(int) item].value = val ;
set_cycle(item,
player_values[val]) ;
break ;
default : return ;
}
case DIFFICULTY : if (ch >= '1' && ch <= '9') val = ch - '1' ;
items[(int) item].value = val ;
set_cycle(item, diff_values[val]) ;
break ;
case NOTES : switch (ch)
{
case 'n' :
case 'N' : val = 0 ;
items[(int) item].value = val ;
set_cycle(item, notes_values[val]) ;
break ;
case 'y' :
case 'Y' : val = 1 ;
items[(int) item].value = val ;
set_cycle(item, notes_values[val]) ;
break ;
default : return ;
}
}
if (val != -1)
{
validkey = 0 ;
message(PANEL_MES, "") ;
items[(int) item].value = item_value = val ;
direction = NONE ;
(*items[(int) item].func)() ;
}
}
}
void
do_key_move(n1, n2)
int n1, n2 ;
{
move = (n2 - '1') * BOARD_SIZE + (n1 - 'a') ;
next_player = (int) cmode - 1 ;
cmode = (enum cantype) ((int) cmode + 1) ;
make_move() ;
validkey = 0 ;
}
void
get_xy(n, x, y) /* Return piece coordinates given board index. */
int n, *x,*y ;
{
*x = (n & 7) * CELL_SIZE + BBORDER + PIECE_MARGIN ;
*y = (n >> 3) * CELL_SIZE + BBORDER + PIECE_MARGIN ;
}
void
handle_board_event()
{
switch (cmode)
{
case WHITE_START :
case BLACK_START : next_player = (int) cmode - 1 ;
if (nextc == LEFT_DOWN || nextc == MIDDLE_DOWN ||
nextc == RIGHT_DOWN)
{
set_cursor(NOCURSOR) ;
piece_x = curx - BBORDER - (PIECE_RAD) ;
piece_y = cury - BBORDER - (PIECE_RAD) ;
draw_piece(next_player, piece_x, piece_y, RINV) ;
cmode = (enum cantype) ((int) cmode + 1) ;
}
break ;
case WHITE_MOVING :
case BLACK_MOVING : do_action() ;
}
}
void
handle_event()
{
process_event() ;
if (nextc == FRAME_REPAINT) init_canvas() ;
else if (nextc == KEYBOARD) handle_key() ;
else if (nextc == EXIT_WINDOW && but_inverted != -1)
{
draw_button((enum panel_type) but_inverted, C_LGREY, BUT_NORMAL) ;
but_inverted = -1 ;
down = 0 ;
}
else if (cury > (CY + BBORDER)) handle_board_event() ;
else if (nextc == LEFT_UP || nextc == MIDDLE_UP || nextc == RIGHT_UP)
check_item_up() ;
else if (nextc == LEFT_DOWN || nextc == MIDDLE_DOWN || nextc == RIGHT_DOWN)
check_item_down() ;
}
void
handle_item(val)
int val ;
{
items[itemno].value = item_value = val ;
(*items[itemno].func)() ;
but_inverted = -1 ;
down = 0 ;
}
void
handle_key() /* Process the latest key that the user has pressed. */
{
char str[9] ; /* To display half move position. */
int nextc ;
if (tinput)
{
get_filename() ;
return ;
}
if (cur_ch == ESCAPE) validkey = 0 ;
if (validkey)
{
nextc = cur_ch ;
cur_ch = validkey ;
}
switch (cur_ch)
{
case 'B' : do_cycle_key(BLACK_PLAYS, nextc) ; /* Cycle items. */
break ;
case 'D' : do_cycle_key(DIFFICULTY, nextc) ;
break ;
case 'N' : do_cycle_key(NOTES, nextc) ;
break ;
case 'W' : do_cycle_key(WHITE_PLAYS, nextc) ;
break ;
case 'l' : last() ; /* Button items. */
break ;
case 'L' : curx = 0 ;
draw_textfield() ;
break ;
case 'n' : new_game() ;
break ;
case 'S' : curx = BBORDER + (2*(BWIDTH+BGAP)) ;
draw_textfield() ;
break ;
case 's' : suggest() ;
break ;
case 'u' : undo() ;
break ;
case 'q' : destroy_frame() ;
exit(0) ;
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' : if (!validkey)
{
validkey = cur_ch ;
SPRINTF(str, "Move: %c", cur_ch) ;
message(PANEL_MES, str) ;
}
else if (nextc >= 'a' && nextc <= 'h')
{
SPRINTF(str, "Move: %c%c", cur_ch, nextc) ;
message(PANEL_MES, str) ;
do_key_move(nextc, cur_ch) ;
}
else validkey = 0 ;
break ;
case 'a' :
case 'b' :
case 'c' :
case 'd' :
case 'e' :
case 'f' :
case 'g' :
case 'h' : if (!validkey)
{
validkey = cur_ch ;
SPRINTF(str, "Move: %c", cur_ch) ;
message(PANEL_MES, str) ;
}
else if (nextc >= '1' && nextc <= '8')
{
SPRINTF(str, "Move: %c%c", cur_ch, nextc) ;
message(PANEL_MES, str) ;
do_key_move(cur_ch, nextc) ;
}
else validkey = 0 ;
break ;
default : message(PANEL_MES, "") ;
validkey = 0 ;
}
}