1 /*
2 This file is part of Warzone 2100.
3 Copyright (C) 1999-2004 Eidos Interactive
4 Copyright (C) 2005-2020 Warzone 2100 Project
5
6 Warzone 2100 is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 Warzone 2100 is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Warzone 2100; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 /*! \file input.h
21 * \brief Prototypes for the keyboard and mouse input functions.
22 */
23 #ifndef _input_h
24 #define _input_h
25
26 /* Check the header files have been included from frame.h if they
27 * are used outside of the framework library.
28 */
29 #if !defined(_frame_h) && !defined(FRAME_LIB_INCLUDE)
30 #error Framework header files MUST be included from Frame.h ONLY.
31 #endif
32
33 #include "types.h"
34 #include "lib/framework/utf.h"
35 #include "vector.h"
36 #include <vector>
37
38
39 /** Defines for all the key codes used. */
40 enum KEY_CODE
41 {
42 KEY_ESC = 27,
43 KEY_1 = '1',
44 KEY_2 = '2',
45 KEY_3 = '3',
46 KEY_4 = '4',
47 KEY_5 = '5',
48 KEY_6 = '6',
49 KEY_7 = '7',
50 KEY_8 = '8',
51 KEY_9 = '9',
52 KEY_0 = '0',
53 KEY_MINUS = '-',
54 KEY_EQUALS = '=',
55 KEY_BACKSPACE = '\b',
56 KEY_TAB = '\t',
57 KEY_Q = 'q',
58 KEY_W = 'w',
59 KEY_E = 'e',
60 KEY_R = 'r',
61 KEY_T = 't',
62 KEY_Y = 'y',
63 KEY_U = 'u',
64 KEY_I = 'i',
65 KEY_O = 'o',
66 KEY_P = 'p',
67 KEY_LBRACE = '[',
68 KEY_RBRACE = ']',
69 KEY_RETURN = '\r', // Comes from SDL, don't know why not '\n'.
70 KEY_LCTRL = 306,
71 KEY_A = 'a',
72 KEY_S = 's',
73 KEY_D = 'd',
74 KEY_F = 'f',
75 KEY_G = 'g',
76 KEY_H = 'h',
77 KEY_J = 'j',
78 KEY_K = 'k',
79 KEY_L = 'l',
80 KEY_SEMICOLON = ';',
81 KEY_QUOTE = '\'',
82 KEY_BACKQUOTE = '`',
83 KEY_LSHIFT = 304,
84 KEY_LMETA = 310, // TODO Added after Qt branched.
85 KEY_LSUPER = 311, // TODO Added after Qt branched.
86 KEY_BACKSLASH = '\\',
87 KEY_Z = 'z',
88 KEY_X = 'x',
89 KEY_C = 'c',
90 KEY_V = 'v',
91 KEY_B = 'b',
92 KEY_N = 'n',
93 KEY_M = 'm',
94 KEY_COMMA = ',',
95 KEY_FULLSTOP = '.',
96 KEY_FORWARDSLASH = '/',
97 KEY_RSHIFT = 303,
98 KEY_RMETA = 309, // TODO Added after Qt branched.
99 KEY_RSUPER = 312, // TODO Added after Qt branched.
100 KEY_KP_STAR = 268,
101 KEY_LALT = 308,
102 KEY_SPACE = ' ',
103 KEY_CAPSLOCK = 301,
104 KEY_F1 = 282,
105 KEY_F2 = 283,
106 KEY_F3 = 284,
107 KEY_F4 = 285,
108 KEY_F5 = 286,
109 KEY_F6 = 287,
110 KEY_F7 = 288,
111 KEY_F8 = 289,
112 KEY_F9 = 290,
113 KEY_F10 = 291,
114 KEY_NUMLOCK = 300,
115 KEY_SCROLLLOCK = 302,
116 KEY_KP_7 = 263,
117 KEY_KP_8 = 264,
118 KEY_KP_9 = 265,
119 KEY_KP_MINUS = 269,
120 KEY_KP_4 = 260,
121 KEY_KP_5 = 261,
122 KEY_KP_6 = 262,
123 KEY_KP_PLUS = 270,
124 KEY_KP_1 = 257,
125 KEY_KP_2 = 258,
126 KEY_KP_3 = 259,
127 KEY_KP_0 = 256,
128 KEY_KP_FULLSTOP = 266,
129 KEY_F11 = 292,
130 KEY_F12 = 293,
131 KEY_RCTRL = 305,
132 KEY_KP_BACKSLASH = 267, ///< Most keypads just have a forward slash.
133 KEY_RALT = 307,
134 KEY_HOME = 278,
135 KEY_UPARROW = 273,
136 KEY_PAGEUP = 280,
137 KEY_LEFTARROW = 276,
138 KEY_RIGHTARROW = 275,
139 KEY_END = 279,
140 KEY_DOWNARROW = 274,
141 KEY_PAGEDOWN = 281,
142 KEY_INSERT = 277,
143 KEY_DELETE = 127,
144 KEY_KPENTER = 271,
145
146 KEY_MAXSCAN = 323, ///< The largest possible scan code.
147
148 KEY_IGNORE = 5190
149 };
150 enum MOUSE_KEY_CODE
151 {
152 MOUSE_LMB = 1,
153 MOUSE_MMB,
154 MOUSE_RMB,
155 MOUSE_X1, // mouse button 4
156 MOUSE_X2, // mouse button 5
157 MOUSE_WUP,
158 MOUSE_WDN,
159 MOUSE_END // end of our enum
160 };
161
162 struct MousePress
163 {
164 enum Action {None, Press, Release};
165
actionMousePress166 MousePress(Action action = None, MOUSE_KEY_CODE key = MOUSE_END) : action(action), key(key), pos(0,0) {}
emptyMousePress167 bool empty() const
168 {
169 return action == None;
170 }
171
172 Action action;
173 MOUSE_KEY_CODE key;
174 Vector2i pos;
175 };
176 typedef std::vector<MousePress> MousePresses;
177
178 /** Tell the input system that we have lost the focus. */
179 void inputLoseFocus();
180 void StopTextInput();
181 /** Converts the key code into an ascii string. */
182 WZ_DECL_NONNULL(2) void keyScanToString(KEY_CODE code, char *ascii, UDWORD maxStringSize);
183
184 /** Initialise the input module. */
185 void inputInitialise();
186
187 /** This returns true if the key is currently depressed. */
188 bool keyDown(KEY_CODE code);
189
190 /** This returns true if the key went from being up to being down this frame. */
191 bool keyPressed(KEY_CODE code);
192
193 /** This returns true if the key went from being down to being up this frame. */
194 bool keyReleased(KEY_CODE code);
195
196 /** Return the current X position of the mouse. */
197 uint16_t mouseX() WZ_DECL_PURE;
198
199 /** Return the current Y position of the mouse. */
200 uint16_t mouseY() WZ_DECL_PURE;
201
202 Vector2i const& getMouseWheelSpeed();
203
204 /// Returns true iff the mouse is on the window.
205 bool wzMouseInWindow();
206
207 /// Return the position of the mouse where it was clicked last.
208 Vector2i mousePressPos_DEPRECATED(MOUSE_KEY_CODE code) WZ_DECL_PURE;
209
210 /// Return the position of the mouse where it was released last.
211 Vector2i mouseReleasePos_DEPRECATED(MOUSE_KEY_CODE code) WZ_DECL_PURE;
212
213 /** This returns true if the mouse key is currently depressed. */
214 bool mouseDown(MOUSE_KEY_CODE code);
215
216 /** This returns true if the mouse key was double clicked. */
217 bool mouseDClicked(MOUSE_KEY_CODE code);
218
219 /** This returns true if the mouse key went from being up to being down this frame. */
220 bool mousePressed(MOUSE_KEY_CODE code);
221
222 /** This returns true if the mouse key went from being down to being up this frame. */
223 bool mouseReleased(MOUSE_KEY_CODE code);
224
225 /** Check for a mouse drag, return the drag start coords if dragging. */
226 WZ_DECL_NONNULL(2, 3) bool mouseDrag(MOUSE_KEY_CODE code, UDWORD *px, UDWORD *py);
227
228 void setMouseWarp(bool value);
229 bool getMouseWarp();
230
231 /* The input buffer can contain normal character codes and these control codes */
232 #define INPBUF_LEFT KEY_LEFTARROW
233 #define INPBUF_RIGHT KEY_RIGHTARROW
234 #define INPBUF_UP KEY_UPARROW
235 #define INPBUF_DOWN KEY_DOWNARROW
236 #define INPBUF_HOME KEY_HOME
237 #define INPBUF_END KEY_END
238 #define INPBUF_INS KEY_INSERT
239 #define INPBUF_DEL KEY_DELETE
240 #define INPBUF_PGUP KEY_PAGEUP
241 #define INPBUF_PGDN KEY_PAGEDOWN
242
243 /* Some defines for keys that map into the normal character space */
244 #define INPBUF_BKSPACE KEY_BACKSPACE
245 #define INPBUF_TAB KEY_TAB
246 #define INPBUF_CR KEY_RETURN
247 #define INPBUF_ESC KEY_ESC
248
249 /** Return the next key press or 0 if no key in the buffer.
250 * The key returned will have been remapped to the correct ascii code for the
251 * US layout (approximately) key map.
252 * All key presses are buffered up (including auto repeat).
253 * @param unicode is filled (unless NULL) with the unicode character corresponding
254 * to the key press (using the user's native layout).
255 */
256 UDWORD inputGetKey(utf_32_char *unicode);
257
258 /// Returns all clicks/releases since last update.
259 MousePresses const &inputGetClicks();
260
261 /** Clear the input buffer. */
262 void inputClearBuffer();
263
264 /* This is called once a frame so that the system can tell
265 * whether a key was pressed this turn or held down from the last frame.
266 */
267 void inputNewFrame();
268
specialOrderKeyDown()269 static inline bool specialOrderKeyDown()
270 {
271 return keyDown(KEY_LALT) || keyDown(KEY_RALT) || keyDown(KEY_LMETA) || keyDown(KEY_RMETA) || keyDown(KEY_LSUPER) || keyDown(KEY_RSUPER);
272 }
273
274 #endif // _input_h
275