1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-09-01 17:14:15 -04:00

Move printing a prompt into answer_yesno(); document wait_for_key()

This commit is contained in:
John Zaitseff 2011-07-21 13:10:51 +10:00
parent eac09fecb5
commit ee992fa02a
4 changed files with 83 additions and 70 deletions

View File

@ -219,14 +219,8 @@ void init_game (void)
newtxwin(5, 44, 6, WCENTER(44), true, ATTR_NORMAL_WINDOW); newtxwin(5, 44, 6, WCENTER(44), true, ATTR_NORMAL_WINDOW);
mvwaddstr(curwin, 2, 2, "Do you need any instructions? "); mvwaddstr(curwin, 2, 2, "Do you need any instructions?");
waddstr(curwin, "["); if (answer_yesno(curwin, ATTR_KEYCODE)) {
attrpr(curwin, ATTR_KEYCODE, "Y");
waddstr(curwin, "/");
attrpr(curwin, ATTR_KEYCODE, "N");
waddstr(curwin, "] ");
if (answer_yesno(curwin)) {
show_help(); show_help();
} }
@ -319,14 +313,8 @@ void init_game (void)
newtxwin(5, 50, 6, WCENTER(50), true, ATTR_NORMAL_WINDOW); newtxwin(5, 50, 6, WCENTER(50), true, ATTR_NORMAL_WINDOW);
mvwaddstr(curwin, 2, 2, "Does any player need instructions? "); mvwaddstr(curwin, 2, 2, "Does any player need instructions?");
waddstr(curwin, "["); if (answer_yesno(curwin, ATTR_KEYCODE)) {
attrpr(curwin, ATTR_KEYCODE, "Y");
waddstr(curwin, "/");
attrpr(curwin, ATTR_KEYCODE, "N");
waddstr(curwin, "] ");
if (answer_yesno(curwin)) {
show_help(); show_help();
} }

View File

@ -422,18 +422,8 @@ int center3 (WINDOW *win, int y, chtype attr1, chtype attr3, chtype attr2,
} }
/************************************************************************ /***********************************************************************/
* Input routines * // gettxchar: Read a character from the keyboard
************************************************************************/
/*-----------------------------------------------------------------------
Function: gettxchar - Read a keyboard character
Arguments: win - Window to use
Returns: int - Keyboard character
This function reads a single character from the keyboard. The key is
NOT echoed to the screen and the cursor visibility is NOT affected.
*/
int gettxchar (WINDOW *win) int gettxchar (WINDOW *win)
{ {
@ -1448,42 +1438,43 @@ int gettxlong (WINDOW *win, long *result, long min, long max, long emptyval,
} }
/*----------------------------------------------------------------------- /***********************************************************************/
Function: answer_yesno - Read a Yes/No answer and return true/false // answer_yesno: Wait for a Yes/No answer
Arguments: win - Window to use
Returns: bool - true if Yes ("Y") was selected, else false
This function waits for either "Y" or "N" to be pressed on the bool answer_yesno (WINDOW *win, chtype attr_keys)
keyboard. If "Y" was pressed, "Yes." is printed and true is returned.
If "N" was pressed, "No." is printed and false is returned. Note that
the cursor becomes invisible after this function.
*/
bool answer_yesno (WINDOW *win)
{ {
int key, oldattr; int key;
bool ok; bool done;
chtype oldattr = getattrs(win);
chtype oldbkgd = getbkgd(win);
keypad(win, true); keypad(win, true);
meta(win, true); meta(win, true);
wtimeout(win, -1); wtimeout(win, -1);
oldattr = getbkgd(win) & ~A_CHARTEXT; waddstr(curwin, " [");
wbkgdset(win, A_NORMAL | (oldattr & A_COLOR)); attrpr(curwin, attr_keys, "Y");
wattron(win, A_BOLD); waddstr(curwin, "/");
attrpr(curwin, attr_keys, "N");
waddstr(curwin, "] ");
curs_set(CURS_ON); curs_set(CURS_ON);
do { done = false;
while (! done) {
key = toupper(wgetch(win)); key = toupper(wgetch(win));
ok = ((key == 'Y') || (key == 'N'));
if (! ok) { if (key == 'Y' || key == 'N') {
done = true;
} else {
beep(); beep();
} }
} while (! ok); }
curs_set(CURS_OFF); curs_set(CURS_OFF);
wattron(win, A_BOLD);
if (key == 'Y') { if (key == 'Y') {
waddstr(win, "Yes"); waddstr(win, "Yes");
@ -1491,24 +1482,18 @@ bool answer_yesno (WINDOW *win)
waddstr(win, "No"); waddstr(win, "No");
} }
wbkgdset(win, oldbkgd);
wattrset(win, oldattr); wattrset(win, oldattr);
wbkgdset(win, oldattr);
wrefresh(win); wrefresh(win);
return (key == 'Y'); return (key == 'Y');
} }
/*----------------------------------------------------------------------- /***********************************************************************/
Function: wait_for_key - Print a message and wait for any key // wait_for_key: Print a message and wait for any key
Arguments: win - Window to use
y - Line on which to print message
attr - Window attributes to use for message
Returns: (nothing)
This function prints a message, then waits for any key to be pressed. void wait_for_key (WINDOW *win, int y, chtype attr)
*/
void wait_for_key (WINDOW *win, int y, int attr)
{ {
keypad(win, true); keypad(win, true);
meta(win, true); meta(win, true);

View File

@ -384,9 +384,22 @@ extern int center3 (WINDOW *win, int y, chtype attr1, chtype attr3,
__attribute__((format (printf, 8, 9))); __attribute__((format (printf, 8, 9)));
// Input routines /*
Function: gettxchar - Read a character from the keyboard
Parameters: win - Window to use (should be curwin)
Returns: int - The keyboard character
This function reads a single character from the keyboard. The key is
NOT echoed to the terminal display, nor is the cursor visibility
affected.
This implementation does not handle multibyte characters correctly:
each part of the multibyte character most likely appears as a separate
keyboard press.
*/
extern int gettxchar (WINDOW *win); extern int gettxchar (WINDOW *win);
extern int gettxline (WINDOW *win, char *buf, int bufsize, bool multifield, extern int gettxline (WINDOW *win, char *buf, int bufsize, bool multifield,
int maxlen, const char *emptyval, const char *defaultval, int maxlen, const char *emptyval, const char *defaultval,
const char *allowed, bool stripspc, int y, int x, const char *allowed, bool stripspc, int y, int x,
@ -400,8 +413,41 @@ extern int gettxlong (WINDOW *win, long *result, long min, long max,
long emptyval, long defaultval, int y, int x, long emptyval, long defaultval, int y, int x,
int fieldsize, int attr); int fieldsize, int attr);
extern bool answer_yesno (WINDOW *win);
extern void wait_for_key (WINDOW *win, int y, int attr); /*
Function: answer_yesno - Wait for a Yes/No answer
Parameters: win - Window to use (should be curwin)
attr_keys - Window rendition to use for key choices
Returns: bool - True if Yes was selected, false if No
This function prompts the user by printing " [Y/N] " using appropriate
character renditions ("Y" and "N" in attr_keys, the rest in the current
rendition), then waits for the user to press either "Y" (for Yes) or
"N" (for No) on the keyboard, then prints the answer using A_BOLD.
True is returned if "Y" was selected, false if "N". Note that the
cursor becomes invisible after calling this function.
*/
extern bool answer_yesno (WINDOW *win, chtype attr_keys);
/*
Function: wait_for_key - Print a message and wait for any key
Parameters: win - Window to use (should be curwin)
y - Line on which to print message
attr - Character rendition to use for message
Returns: (nothing)
This function displays the message "Press <SPACE> to continue" in the
centre of line y in window win, then waits for any key to be pressed.
The reason the user is not asked "Press any key to continue" is
historical: many, many people used to ask "where is the <ANY> key?" :-)
The current implementation does not handle multibyte characters
correctly: only the first byte of the character is consumed, with
further bytes left in the keyboard queue.
*/
extern void wait_for_key (WINDOW *win, int y, chtype attr);
#endif /* included_INTF_H */ #endif /* included_INTF_H */

View File

@ -274,14 +274,8 @@ selection_t get_move (void)
wrefresh(curwin); wrefresh(curwin);
// Ask the player to confirm their choice // Ask the player to confirm their choice
mvwaddstr(curwin, 2, 22, "Are you sure? "); mvwaddstr(curwin, 2, 22, "Are you sure?");
waddstr(curwin, "["); if (! answer_yesno(curwin, ATTR_KEYCODE)) {
attrpr(curwin, ATTR_KEYCODE, "Y");
waddstr(curwin, "/");
attrpr(curwin, ATTR_KEYCODE, "N");
waddstr(curwin, "] ");
if (! answer_yesno(curwin)) {
selection = SEL_NONE; selection = SEL_NONE;
} }