]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.hxx
95731f8f056622b03a4e3da1e20f321983363ebd
[flightgear.git] / src / Input / input.hxx
1 // input.hxx -- handle user input from various sources.
2 //
3 // Written by David Megginson, started May 2001.
4 //
5 // Copyright (C) 2001 David Megginson, david@megginson.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifndef _INPUT_HXX
25 #define _INPUT_HXX
26
27 #ifndef __cplusplus                                                          
28 # error This library requires C++
29 #endif
30
31 #include <plib/js.h>
32
33 #include <simgear/compiler.h>
34
35 #include <simgear/misc/commands.hxx>
36 #include <simgear/misc/props.hxx>
37
38 #include <Main/fgfs.hxx>
39 #include <Main/globals.hxx>
40
41 #include <map>
42 #include <vector>
43
44 SG_USING_STD(map);
45 SG_USING_STD(vector);
46
47
48 \f
49 ////////////////////////////////////////////////////////////////////////
50 // General binding support.
51 ////////////////////////////////////////////////////////////////////////
52
53
54 /**
55  * An input binding of some sort.
56  *
57  * <p>This class represents a binding that can be assigned to a
58  * keyboard key, a joystick button or axis, or even a panel
59  * instrument.</p>
60  */
61 class FGBinding : public FGConditional
62 {
63 public:
64
65   /**
66    * Default constructor.
67    */
68   FGBinding ();
69
70
71   /**
72    * Convenience constructor.
73    *
74    * @param node The binding will be built from this node.
75    */
76   FGBinding (const SGPropertyNode * node);
77
78
79   /**
80    * Destructor.
81    */
82   virtual ~FGBinding ();
83
84
85   /**
86    * Get the command name.
87    *
88    * @return The string name of the command for this binding.
89    */
90   virtual const string &getCommandName () const { return _command_name; }
91
92
93   /**
94    * Get the command itself.
95    *
96    * @return The command associated with this binding, or 0 if none
97    * is present.
98    */
99   virtual SGCommandMgr::command_t getCommand () const { return _command; }
100
101
102   /**
103    * Get the argument that will be passed to the command.
104    *
105    * @return A property node that will be passed to the command as its
106    * argument, or 0 if none was supplied.
107    */
108   virtual const SGPropertyNode * getArg () { return _arg; }
109   
110
111   /**
112    * Read a binding from a property node.
113    *
114    * @param node The property node containing the binding.
115    */
116   virtual void read (const SGPropertyNode * node);
117
118
119   /**
120    * Fire a binding.
121    */
122   virtual void fire () const;
123
124
125   /**
126    * Fire a binding with x and y positions.
127    */
128   virtual void fire (int x, int y) const;
129
130
131   /**
132    * Fire a binding with a setting (i.e. joystick axis).
133    *
134    * A double 'setting' property will be added to the arguments.
135    *
136    * @param setting The input setting, usually between -1.0 and 1.0.
137    */
138   virtual void fire (double setting) const;
139
140
141 private:
142   string _command_name;
143   SGCommandMgr::command_t _command;
144   mutable SGPropertyNode * _arg;
145   mutable SGPropertyNode * _setting;
146   mutable SGCommandState * _command_state;
147 };
148
149
150 \f
151 ////////////////////////////////////////////////////////////////////////
152 // General input mapping support.
153 ////////////////////////////////////////////////////////////////////////
154
155
156 /**
157  * Generic input module.
158  *
159  * <p>This module is designed to handle input from multiple sources --
160  * keyboard, joystick, mouse, or even panel switches -- in a consistent
161  * way, and to allow users to rebind any of the actions at runtime.</p>
162  */
163 class FGInput : public FGSubsystem
164 {
165 public:
166
167   enum {
168     FG_MOD_NONE = 0,
169     FG_MOD_UP = 1,              // key- or button-up
170     FG_MOD_SHIFT = 2,
171     FG_MOD_CTRL = 4,
172     FG_MOD_ALT = 8,
173     FG_MOD_MAX = 16             // enough to handle all combinations
174   };
175
176
177   /**
178    * Default constructor.
179    */
180   FGInput ();
181
182
183   /**
184    * Destructor.
185    */
186   virtual ~FGInput();
187
188   //
189   // Implementation of FGSubsystem.
190   //
191   virtual void init ();
192   virtual void bind ();
193   virtual void unbind ();
194   virtual void update (int dt);
195
196
197   /**
198    * Handle a single keystroke.
199    *
200    * <p>Note: for special keys, the integer key code will be the Glut
201    * code + 256.</p>
202    *
203    * @param k The integer key code, as returned by glut.
204    * @param modifiers Modifier keys pressed (bitfield).
205    * @param x The mouse x position at the time of keypress.
206    * @param y The mouse y position at the time of keypress.
207    * @see #FG_MOD_SHIFT
208    * @see #FG_MOD_CTRL
209    * @see #FG_MOD_ALT
210    */
211   virtual void doKey (int k, int modifiers, int x, int y);
212
213
214   /**
215    * Handle a mouse click event.
216    *
217    * @param button The mouse button selected.
218    * @param updown Button status.
219    * @param x The X position of the mouse event, in screen coordinates.
220    * @param y The Y position of the mouse event, in screen coordinates.
221    */
222   virtual void doMouseClick (int button, int updown, int x, int y);
223
224
225   /**
226    * Handle mouse motion.
227    *
228    * @param x The new mouse x position, in screen coordinates.
229    * @param y The new mouse y position, in screen coordinates.
230    */
231   virtual void doMouseMotion (int x, int y);
232
233
234 private:
235                                 // Constants
236   enum 
237   {
238     MAX_KEYS = 1024,
239
240   #ifdef WIN32
241     MAX_JOYSTICKS = 2,
242   #else
243     MAX_JOYSTICKS = 10,
244   #endif
245     MAX_JOYSTICK_AXES = _JS_MAX_AXES,
246     MAX_JOYSTICK_BUTTONS = 32,
247
248     MAX_MICE = 1,
249     MAX_MOUSE_BUTTONS = 3
250   };
251
252   typedef vector<FGBinding *> binding_list_t;
253
254   /**
255    * Settings for a key or button.
256    */
257   struct button {
258     button ();
259     virtual ~button ();
260     bool is_repeatable;
261     int last_state;
262     binding_list_t bindings[FG_MOD_MAX];
263   };
264
265
266   /**
267    * Settings for a single joystick axis.
268    */
269   struct axis {
270     axis ();
271     virtual ~axis ();
272     float last_value;
273     float tolerance;
274     binding_list_t bindings[FG_MOD_MAX];
275     float low_threshold;
276     float high_threshold;
277     struct button low;
278     struct button high;
279   };
280
281
282   /**
283    * Settings for a joystick.
284    */
285   struct joystick {
286     joystick ();
287     virtual ~joystick ();
288     int jsnum;
289     jsJoystick * js;
290     int naxes;
291     int nbuttons;
292     axis * axes;
293     button * buttons;
294   };
295
296
297   /**
298    * Settings for a mouse.
299    */
300   struct mouse {
301     mouse ();
302     virtual ~mouse ();
303     int nModes;
304     int * cursors;
305     button * buttons;
306   };
307
308
309   /**
310    * Initialize key bindings.
311    */
312   void _init_keyboard ();
313
314
315   /**
316    * Initialize joystick bindings.
317    */
318   void _init_joystick ();
319
320
321   /**
322    * Initialize mouse bindings.
323    */
324   void _init_mouse ();
325
326
327   /**
328    * Initialize a single button.
329    */
330   inline void _init_button (const SGPropertyNode * node,
331                             button &b,
332                             const string name);
333
334
335   /**
336    * Update the keyboard.
337    */
338   void _update_keyboard ();
339
340
341   /**
342    * Update the joystick.
343    */
344   void _update_joystick ();
345
346
347   /**
348    * Update the mouse.
349    */
350   void _update_mouse ();
351
352
353   /**
354    * Update a single button.
355    */
356   inline void _update_button (button &b, int modifiers, bool pressed,
357                               int x, int y);
358
359
360   /**
361    * Read bindings and modifiers.
362    */
363   void _read_bindings (const SGPropertyNode * node,
364                        binding_list_t * binding_list,
365                        int modifiers);
366
367   /**
368    * Look up the bindings for a key code.
369    */
370   const vector<FGBinding *> &_find_key_bindings (unsigned int k,
371                                                  int modifiers);
372
373   button _key_bindings[MAX_KEYS];
374   joystick _joystick_bindings[MAX_JOYSTICKS];
375   mouse _mouse_bindings[MAX_MICE];
376
377   int _current_mouse_mode;
378   int _last_mouse_mode;
379
380 };
381
382
383 extern FGInput current_input;
384
385
386 \f
387 ////////////////////////////////////////////////////////////////////////
388 // GLUT callbacks.
389 ////////////////////////////////////////////////////////////////////////
390
391 // Handle GLUT events.
392 extern "C" {
393
394 /**
395  * Key-down event handler for Glut.
396  *
397  * <p>Pass the value on to the FGInput module unless PUI wants it.</p>
398  *
399  * @param k The integer value for the key pressed.
400  * @param x (unused)
401  * @param y (unused)
402  */
403 void GLUTkey (unsigned char k, int x, int y);
404
405
406 /**
407  * Key-up event handler for GLUT.
408  *
409  * <p>PUI doesn't use this, so always pass it to the input manager.</p>
410  *
411  * @param k The integer value for the key pressed.
412  * @param x (unused)
413  * @param y (unused)
414  */
415 void GLUTkeyup (unsigned char k, int x, int y);
416
417
418 /**
419  * Special key-down handler for Glut.
420  *
421  * <p>Pass the value on to the FGInput module unless PUI wants it.
422  * The key value will have 256 added to it.</p>
423  *
424  * @param k The integer value for the key pressed (will have 256 added
425  * to it).
426  * @param x (unused)
427  * @param y (unused)
428  */
429 void GLUTspecialkey (int k, int x, int y);
430
431
432 /**
433  * Special key-up handler for Glut.
434  *
435  * @param k The integer value for the key pressed (will have 256 added
436  * to it).
437  * @param x (unused)
438  * @param y (unused)
439  */
440 void GLUTspecialkeyup (int k, int x, int y);
441
442
443 /**
444  * Mouse click handler for Glut.
445  *
446  * @param button The mouse button pressed.
447  * @param updown Press or release flag.
448  * @param x The x-location of the click.
449  * @param y The y-location of the click.
450  */
451 void GLUTmouse (int button, int updown, int x, int y);
452
453
454 /**
455  * Mouse motion handler for Glut.
456  *
457  * @param x The new x-location of the mouse.
458  * @param y The new y-location of the mouse.
459  */
460 void GLUTmotion (int x, int y);
461
462 } // extern "C"
463
464 #endif // _INPUT_HXX