home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_GEN / FACETV.ZIP / RBUTTONS.CPP < prev    next >
C/C++ Source or Header  |  1994-01-04  |  4KB  |  166 lines

  1. /************************************************************************
  2. **
  3. ** @(#)rbuttons.cpp    01/03/94    Chris Ahlstrom
  4. **
  5. **    This module interfaces C++ with code for handling various kinds
  6. ** of button devices.  This base class implements the keyboard
  7. ** buttons by default.
  8. **
  9. **    Also, until we decide that Koala support is really necessary,
  10. ** this class will take the place of any Koala device selection.
  11. ** Tentative support is given in korbutt.cpp/h.
  12. **
  13. **    See rbuttons.h to see what the keys mean.
  14. **
  15. *************************************************************************/
  16.  
  17. #define RBUTTONS_cpp
  18.  
  19. #include <conio.h>        // getch() function
  20. #include <ctype.h>        // toupper() function
  21.  
  22. #include "rbuttons.h"        // ResponseButton class
  23.  
  24.  
  25.  
  26. /************************************************************************
  27. ** ResponseButton constructor and destructor
  28. *************************************************************************/
  29.  
  30. ResponseButton::ResponseButton
  31. (
  32.     ResponseType buttondevice
  33. )
  34.   :
  35.     responseDevice    (buttondevice),
  36.     lastResponse    (NO_BUTTON)
  37. {
  38. }
  39.  
  40.  
  41. ResponseButton::~ResponseButton ()
  42. {
  43. }
  44.  
  45.  
  46. /************************************************************************
  47. ** readKey()
  48. **
  49. **    readKey() simply checks the device for a button-pressing
  50. ** status.  After the check, the routine immediately returns a status,
  51. ** with no waiting.
  52. **
  53. **    This default implementation supports input from the keyboard;
  54. ** the buttons (keys, here) to achieve each status are listed in
  55. ** square brackets.
  56. **
  57. ** readkey() returns the following statuses (see the Response type in
  58. ** rbuttons.h):
  59. **
  60. **    0 if no button is pressed
  61. **    1 if button 1 is pressed        [key '1']
  62. **    2 if button 2 is pressed        [key '2']
  63. **    3 if both buttons are pressed        [key 'b' or ESC]
  64. **    4 if button 3 is pressed        [key '3']
  65. **    7 if all three buttons were pressed.    [key 'a']
  66. **
  67. *************************************************************************/
  68.  
  69. int
  70. ResponseButton::readKey ()
  71. {
  72.     int button = (int) NO_BUTTON;
  73.  
  74.     if (kbhit())
  75.     {
  76.     button = getch();
  77.     if (button == KEY_BUTTON_1)
  78.         button = (int) BUTTON_1;
  79.     else if (button == KEY_BUTTON_2)
  80.         button = (int) BUTTON_2;
  81.     else if (button == KEY_BUTTON_3)
  82.         button = (int) BUTTON_3;
  83.     else
  84.     {
  85.         button = toupper(button);
  86.         if (button == KEY_BOTH_BUTTONS || button == KEY_BOTH_ESCAPE)
  87.         button = (int) BOTH_BUTTONS;
  88.         else if (button == KEY_ALL_BUTTONS)
  89.         button = (int) ALL_BUTTONS;
  90.     }
  91.     }
  92.     lastResponse = (Response) button;
  93.     return button;
  94. }
  95.  
  96. /************************************************************************
  97. ** getLastResponse ()
  98. **
  99. **    This function returns the value of lastResponse.
  100. ** It is useful when the action to be caused by the response has to
  101. ** occur sometime after the response occurred.
  102. **
  103. *************************************************************************/
  104.  
  105. inline int
  106. ResponseButton::getLastResponse ()
  107. {
  108.     return (int) lastResponse;
  109. }
  110.  
  111.  
  112. /************************************************************************
  113. ** waitKey()
  114. **
  115. **    Waits for a button to be pressed [repeatedly calls readKey()],
  116. ** and returns the value.
  117. **
  118. **    Returns same values as readKey(), except for NO_BUTTON.
  119. **
  120. **    At present, it looks like this routine does not need overriding.
  121. **
  122. *************************************************************************/
  123.  
  124. int
  125. ResponseButton::waitKey ()
  126. {
  127.     int button;
  128.  
  129.     do
  130.     {
  131.     button = readKey();
  132.     
  133.     } while (button == (int) NO_BUTTON);
  134.  
  135.     return button;
  136. }
  137.  
  138.  
  139. /************************************************************************
  140. ** checkAbort()
  141. **
  142. **    Checks if both buttons (of the KeyResponse set) were pressed --
  143. ** if so, returns a non-zero value.
  144. **
  145. **    At present, it looks like this routine does not need overriding.
  146. **
  147. *************************************************************************/
  148.  
  149. int
  150. ResponseButton::checkAbort ()
  151. {
  152.     int putzcounter;
  153.     int chk = 0;
  154.  
  155.     for (putzcounter = 1; putzcounter <= 100; putzcounter++)
  156.     {
  157.     if (readKey() == BOTH_BUTTONS)
  158.     {
  159.         chk = 1;
  160.         break;
  161.     }
  162.     }
  163.     return chk;
  164. }
  165.  
  166.