]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.hxx
007e70a9376c703e6605f39a9ea946531f30c47a
[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 a scaled movement (rather than absolute position).
127    */
128   virtual void fire (double offset, double max) 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 };
147
148
149 \f
150 ////////////////////////////////////////////////////////////////////////
151 // General input mapping support.
152 ////////////////////////////////////////////////////////////////////////
153
154
155 /**
156  * Generic input module.
157  *
158  * <p>This module is designed to handle input from multiple sources --
159  * keyboard, joystick, mouse, or even panel switches -- in a consistent
160  * way, and to allow users to rebind any of the actions at runtime.</p>
161  */
162 class FGInput : public FGSubsystem
163 {
164 public:
165
166   enum {
167     FG_MOD_NONE = 0,
168     FG_MOD_UP = 1,              // key- or button-up
169     FG_MOD_SHIFT = 2,
170     FG_MOD_CTRL = 4,
171     FG_MOD_ALT = 8,
172     FG_MOD_MAX = 16             // enough to handle all combinations
173   };
174
175
176   /**
177    * Default constructor.
178    */
179   FGInput ();
180
181
182   /**
183    * Destructor.
184    */
185   virtual ~FGInput();
186
187   //
188   // Implementation of FGSubsystem.
189   //
190   virtual void init ();
191   virtual void bind ();
192   virtual void unbind ();
193   virtual void update (double dt);
194
195
196   /**
197    * Control whether this is the default module to receive events.
198    *
199    * The first input module created will set itself as the default
200    * automatically.
201    *
202    * @param status true if this should be the default module for
203    * events, false otherwise.
204    */
205   virtual void makeDefault (bool status = true);
206
207
208   /**
209    * Handle a single keystroke.
210    *
211    * <p>Note: for special keys, the integer key code will be the Glut
212    * code + 256.</p>
213    *
214    * @param k The integer key code, as returned by glut.
215    * @param modifiers Modifier keys pressed (bitfield).
216    * @param x The mouse x position at the time of keypress.
217    * @param y The mouse y position at the time of keypress.
218    * @see #FG_MOD_SHIFT
219    * @see #FG_MOD_CTRL
220    * @see #FG_MOD_ALT
221    */
222   virtual void doKey (int k, int modifiers, int x, int y);
223
224
225   /**
226    * Handle a mouse click event.
227    *
228    * @param button The mouse button selected.
229    * @param updown Button status.
230    * @param x The X position of the mouse event, in screen coordinates.
231    * @param y The Y position of the mouse event, in screen coordinates.
232    */
233   virtual void doMouseClick (int button, int updown, int x, int y);
234
235
236   /**
237    * Handle mouse motion.
238    *
239    * @param x The new mouse x position, in screen coordinates.
240    * @param y The new mouse y position, in screen coordinates.
241    */
242   virtual void doMouseMotion (int x, int y);
243
244
245 private:
246                                 // Constants
247   enum 
248   {
249     MAX_KEYS = 1024,
250
251   #ifdef WIN32
252     MAX_JOYSTICKS = 2,
253   #else
254     MAX_JOYSTICKS = 10,
255   #endif
256     MAX_JOYSTICK_AXES = _JS_MAX_AXES,
257     MAX_JOYSTICK_BUTTONS = 32,
258
259     MAX_MICE = 1,
260     MAX_MOUSE_BUTTONS = 8
261   };
262   struct mouse;
263   friend struct mouse;
264
265   typedef vector<FGBinding *> binding_list_t;
266
267   /**
268    * Settings for a key or button.
269    */
270   struct button {
271     button ();
272     virtual ~button ();
273     bool is_repeatable;
274     int last_state;
275     binding_list_t bindings[FG_MOD_MAX];
276   };
277
278
279   /**
280    * Settings for a single joystick axis.
281    */
282   struct axis {
283     axis ();
284     virtual ~axis ();
285     float last_value;
286     float tolerance;
287     binding_list_t bindings[FG_MOD_MAX];
288     float low_threshold;
289     float high_threshold;
290     struct button low;
291     struct button high;
292   };
293
294
295   /**
296    * Settings for a joystick.
297    */
298   struct joystick {
299     joystick ();
300     virtual ~joystick ();
301     int jsnum;
302     jsJoystick * js;
303     int naxes;
304     int nbuttons;
305     axis * axes;
306     button * buttons;
307   };
308
309
310   /**
311    * Settings for a mouse mode.
312    */
313   struct mouse_mode {
314     mouse_mode ();
315     virtual ~mouse_mode ();
316     int cursor;
317     bool constrained;
318     bool pass_through;
319     button * buttons;
320     binding_list_t x_bindings[FG_MOD_MAX];
321     binding_list_t y_bindings[FG_MOD_MAX];
322   };
323
324
325   /**
326    * Settings for a mouse.
327    */
328   struct mouse {
329     mouse ();
330     virtual ~mouse ();
331     int x;
332     int y;
333     SGPropertyNode * mode_node;
334     SGPropertyNode * mouse_button_nodes[MAX_MOUSE_BUTTONS];
335     int nModes;
336     int current_mode;
337     mouse_mode * modes;
338   };
339
340
341   /**
342    * Initialize key bindings.
343    */
344   void _init_keyboard ();
345
346
347   /**
348    * Initialize joystick bindings.
349    */
350   void _init_joystick ();
351
352
353   /**
354    * Initialize mouse bindings.
355    */
356   void _init_mouse ();
357
358
359   /**
360    * Initialize a single button.
361    */
362   inline void _init_button (const SGPropertyNode * node,
363                             button &b,
364                             const string name);
365
366
367   /**
368    * Update the keyboard.
369    */
370   void _update_keyboard ();
371
372
373   /**
374    * Update the joystick.
375    */
376   void _update_joystick ();
377
378
379   /**
380    * Update the mouse.
381    */
382   void _update_mouse ();
383
384
385   /**
386    * Update a single button.
387    */
388   inline void _update_button (button &b, int modifiers, bool pressed,
389                               int x, int y);
390
391
392   /**
393    * Read bindings and modifiers.
394    */
395   void _read_bindings (const SGPropertyNode * node,
396                        binding_list_t * binding_list,
397                        int modifiers);
398
399   /**
400    * Look up the bindings for a key code.
401    */
402   const vector<FGBinding *> &_find_key_bindings (unsigned int k,
403                                                  int modifiers);
404
405   button _key_bindings[MAX_KEYS];
406   joystick _joystick_bindings[MAX_JOYSTICKS];
407   mouse _mouse_bindings[MAX_MICE];
408
409 };
410
411
412 \f
413 ////////////////////////////////////////////////////////////////////////
414 // GLUT callbacks.
415 ////////////////////////////////////////////////////////////////////////
416
417 // Handle GLUT events.
418 extern "C" {
419
420 /**
421  * Key-down event handler for Glut.
422  *
423  * <p>Pass the value on to the FGInput module unless PUI wants it.</p>
424  *
425  * @param k The integer value for the key pressed.
426  * @param x (unused)
427  * @param y (unused)
428  */
429 void GLUTkey (unsigned char k, int x, int y);
430
431
432 /**
433  * Key-up event handler for GLUT.
434  *
435  * <p>PUI doesn't use this, so always pass it to the input manager.</p>
436  *
437  * @param k The integer value for the key pressed.
438  * @param x (unused)
439  * @param y (unused)
440  */
441 void GLUTkeyup (unsigned char k, int x, int y);
442
443
444 /**
445  * Special key-down handler for Glut.
446  *
447  * <p>Pass the value on to the FGInput module unless PUI wants it.
448  * The key value will have 256 added to it.</p>
449  *
450  * @param k The integer value for the key pressed (will have 256 added
451  * to it).
452  * @param x (unused)
453  * @param y (unused)
454  */
455 void GLUTspecialkey (int k, int x, int y);
456
457
458 /**
459  * Special key-up handler for Glut.
460  *
461  * @param k The integer value for the key pressed (will have 256 added
462  * to it).
463  * @param x (unused)
464  * @param y (unused)
465  */
466 void GLUTspecialkeyup (int k, int x, int y);
467
468
469 /**
470  * Mouse click handler for Glut.
471  *
472  * @param button The mouse button pressed.
473  * @param updown Press or release flag.
474  * @param x The x-location of the click.
475  * @param y The y-location of the click.
476  */
477 void GLUTmouse (int button, int updown, int x, int y);
478
479
480 /**
481  * Mouse motion handler for Glut.
482  *
483  * @param x The new x-location of the mouse.
484  * @param y The new y-location of the mouse.
485  */
486 void GLUTmotion (int x, int y);
487
488 } // extern "C"
489
490 #endif // _INPUT_HXX