1 /**
2  * @file handler_keyboard.h
3  * @brief Handler of the keyboard and mouse
4  * @date 2012-10-13
5  * @copyright 1991-2014 TLK Games
6  * @author Bruno Ethvignot
7  * @version $Revision: 24 $
8  */
9 /*
10  * copyright (c) 1991-2014 TLK Games all rights reserved
11  * $Id: handler_keyboard.h 24 2014-09-28 15:30:04Z bruno.ethvignot@gmail.com $
12  *
13  * TecnoballZ is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * TecnoballZ is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26  * MA  02110-1301, USA.
27  */
28 #ifndef __HANDLER_KEYBOARD__
29 #define __HANDLER_KEYBOARD__
30 
31 #include "../include/tecnoballz.h"
32 
33 class handler_keyboard:public virtual tecnoballz
34   {
35   public:
36     static const Sint32 NULL_YCOORD = -10240;
37 
38   private:
39 #ifdef TECNOBALLZ_GP2X
40     /* GP2X button codes, as received through SDL joystick events */
41     typedef enum
42     {
43       GP2X_BUTTON_UP,
44       GP2X_BUTTON_UPLEFT,
45       GP2X_BUTTON_LEFT,
46       GP2X_BUTTON_DOWNLEFT,
47       GP2X_BUTTON_DOWN,
48       GP2X_BUTTON_DOWNRIGHT,
49       GP2X_BUTTON_RIGHT,
50       GP2X_BUTTON_UPRIGHT,
51       GP2X_BUTTON_START,
52       GP2X_BUTTON_SELECT,
53       GP2X_BUTTON_R,
54       GP2X_BUTTON_L,
55       GP2X_BUTTON_A,
56       GP2X_BUTTON_B,
57       GP2X_BUTTON_Y,
58       GP2X_BUTTON_X,
59       GP2X_BUTTON_VOLUP,
60       GP2X_BUTTON_VOLDOWN,
61       GP2X_BUTTON_CLICK,
62       GP2X_NUM_BUTTONS
63     } GP2X_BUTTONS_CODE;
64     /* The current state of all the GP2X buttons is stored in
65      * this array - used to handle multi-key actions */
66     static bool gp2x_buttons[GP2X_NUM_BUTTONS];
67 #endif
68 #ifdef TECNOBALLZ_PSP
69     /* PSP button codes, as received through SDL joystick events */
70     typedef enum
71     {
72       PSP_BUTTON_Y,
73       PSP_BUTTON_B,
74       PSP_BUTTON_A,
75       PSP_BUTTON_X,
76       PSP_BUTTON_L,
77       PSP_BUTTON_R,
78       PSP_BUTTON_DOWN,
79       PSP_BUTTON_LEFT,
80       PSP_BUTTON_UP,
81       PSP_BUTTON_RIGHT,
82       PSP_BUTTON_SELECT,
83       PSP_BUTTON_START,
84       PSP_NUM_BUTTONS
85     } PSP_BUTTONS_CODE;
86     static bool psp_buttons[PSP_NUM_BUTTONS];
87 #endif
88 
89     typedef enum
90     {
91       IJOY_LEFT = 1,
92       IJOY_RIGHT,
93       IJOY_TOP,
94       IJOY_DOWN,
95       IJOY_FIRE,
96       IJOY_RELEASE,
97       IJOY_GIGABLITZ,
98       IJOY_OPTION
99     }
100     JOYCODE_ENUM;
101 
102   public:
103     typedef enum
104     {
105       COMMAND_KEY_PAUSE,
106       QUIT_TECNOBALLZ,
107       QUIT_TO_MAIN_MENU,
108       CAUSE_GAME_OVER,
109       TOGGLE_AUDIO,
110       TOGGLE_SOUND,
111       TOGGLE_MUSIC,
112       TOGGLE_FULLSCREEN,
113       TOGGLE_POPUP_MENU,
114       DISABLE_TIMER,
115       VOLUME_DOWN,
116       VOLUME_UP,
117       NUMOF_COMMAND_KEYS
118     }
119     COMAND_KEYS_ENUM;
120 
121     typedef enum
122     {
123       K_LEFT,
124       K_RIGHT,
125       K_UP,
126       K_DOWN,
127       K_RELEASE_BALL,
128       K_FIRE,
129       K_GIGABLITZ,
130       K_VOLUME_UP,
131       K_VOLUME_DOWN,
132       K_MAXOF
133     } KEY_CODES_ENUM;
134 
135   private:
136     static bool last_command_keys[NUMOF_COMMAND_KEYS];
137     static bool command_keys[NUMOF_COMMAND_KEYS];
138     /** Predefinded keys to control the paddle */
139     static Uint32 key_codes[K_MAXOF];
140     static handler_keyboard *keyboard_singleton;
141 
142   private:
143     /** True if grabs mouse and keyboard input */
144     bool is_grab_input;
145     /** Number of available joysticks */
146     Uint32 numof_joysticks;
147     SDL_Joystick **sdl_joysticks;
148     bool joy_fire;
149     bool joy_release;
150     bool joy_gigablitz;
151     bool joy_option;
152     bool joy_left;
153     bool joy_right;
154     bool joy_top;
155     bool joy_down;
156 
157     Uint32 joy_code_down;
158     Uint32 joy_code_down_prev;
159     Uint32 joy_up;
160     Sint32 input_joy_tempo;
161 
162     Sint32 mouse_x_offset;
163     Sint32 mouse_y_offset;
164 
165     /* x and y coordinates of the mouse pointer */
166     Sint32 mouse_x_coord;
167     Sint32 mouse_y_coord;
168 
169     bool is_left_button_down;
170     bool is_right_button_down;
171     bool is_middle_button_down;
172     bool is_left_button_released;
173     bool is_right_button_released;
174 
175     /* keyboard input delay */
176     Sint32 key_delay;
177     Uint32 key_code_down;
178     Uint32 previous_key_code_down;
179     Uint32 code_keyup;
180     /* cursor position into input string */
181     Sint32 string_cursor_pos;
182     /* input string size */
183     Sint32 string_input_size;
184     /* pointer to the current input string */
185     char *current_input_string;
186 
187     bool is_key_waiting;
188     Sint32 wait_key_pressed;
189   private:
190     handler_keyboard ();
191 #ifdef TECNOBALLZ_HANDHELD_CONSOLE
192     void handle_console_buttons (SDL_Event * event);
193 #endif
194   public:
195     ~handler_keyboard ();
196     static handler_keyboard *get_instance ();
197 
198     void set_grab_input (bool mode);
199     void read_events ();
200     void clear_command_keys ();
201     bool command_is_pressed (Uint32 code, bool clear = false);
202     bool is_left_button ();
203     bool is_right_button ();
204     bool is_gigablitz_or_tilt ();
205     //bool is_right_left_buttons ();
206     bool is_left_button_up (Sint32 * off_x, Sint32 * off_y);
207     bool is_right_button_up (Sint32 * off_x, Sint32 * off_y);
208     Sint32 get_mouse_x_offset ();
209     Sint32 get_mouse_y_offset ();
210     Sint32 get_mouse_x ();
211     Sint32 get_mouse_y ();
212     bool key_is_pressed (Sint32 code);
213     bool key_is_released (Sint32 code);
214     bool control_is_pressed (Uint32 code);
215     void set_input_string (char *str, Uint32 size);
216     Sint32 get_input_cursor_pos ();
217     void stop_string_input ();
218     Uint32 get_key_down_code ();
219     bool wait_key ();
220 
221   private:
222     void toggle_popup_menu ();
223     void toggle_pause ();
224     void init_joysticks ();
225     void set_joy (Uint32 code);
226     void clr_joy (Uint32 code);
227     void input_string ();
228     void input_string (Uint32 code);
229     void set_key_code_down (Uint32 code);
230     void set_keycode_up (Uint32 code);
231   };
232 #endif
233