]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.hxx
MIPSpro 7.4 fixes
[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/structure/subsystem_mgr.hxx>
36 #include <simgear/structure/commands.hxx>
37 #include <simgear/props/condition.hxx>
38 #include <simgear/props/props.hxx>
39
40 #include <Main/fg_props.hxx>
41 #include <Main/globals.hxx>
42
43 #include <map>
44 #include <vector>
45
46 SG_USING_STD(map);
47 SG_USING_STD(vector);
48
49
50
51
52 \f
53 ////////////////////////////////////////////////////////////////////////
54 // General binding support.
55 ////////////////////////////////////////////////////////////////////////
56
57
58 /**
59  * An input binding of some sort.
60  *
61  * <p>This class represents a binding that can be assigned to a
62  * keyboard key, a joystick button or axis, or even a panel
63  * instrument.</p>
64  */
65 class FGBinding : public SGConditional
66 {
67 public:
68
69   /**
70    * Default constructor.
71    */
72   FGBinding ();
73
74
75   /**
76    * Convenience constructor.
77    *
78    * @param node The binding will be built from this node.
79    */
80   FGBinding (const SGPropertyNode * node);
81
82
83   /**
84    * Destructor.
85    */
86   virtual ~FGBinding () {}
87
88
89   /**
90    * Get the command name.
91    *
92    * @return The string name of the command for this binding.
93    */
94   virtual const string &getCommandName () const { return _command_name; }
95
96
97   /**
98    * Get the command itself.
99    *
100    * @return The command associated with this binding, or 0 if none
101    * is present.
102    */
103   virtual SGCommandMgr::command_t getCommand () const { return _command; }
104
105
106   /**
107    * Get the argument that will be passed to the command.
108    *
109    * @return A property node that will be passed to the command as its
110    * argument, or 0 if none was supplied.
111    */
112   virtual const SGPropertyNode * getArg () { return _arg; }
113   
114
115   /**
116    * Read a binding from a property node.
117    *
118    * @param node The property node containing the binding.
119    */
120   virtual void read (const SGPropertyNode * node);
121
122
123   /**
124    * Fire a binding.
125    */
126   virtual void fire () const;
127
128
129   /**
130    * Fire a binding with a scaled movement (rather than absolute position).
131    */
132   virtual void fire (double offset, double max) const;
133
134
135   /**
136    * Fire a binding with a setting (i.e. joystick axis).
137    *
138    * A double 'setting' property will be added to the arguments.
139    *
140    * @param setting The input setting, usually between -1.0 and 1.0.
141    */
142   virtual void fire (double setting) const;
143
144
145 private:
146                                 // just to be safe.
147   FGBinding (const FGBinding &binding);
148
149   string _command_name;
150   mutable SGCommandMgr::command_t _command;
151   mutable SGPropertyNode_ptr _arg;
152   mutable SGPropertyNode_ptr _setting;
153 };
154
155
156 \f
157 ////////////////////////////////////////////////////////////////////////
158 // General input mapping support.
159 ////////////////////////////////////////////////////////////////////////
160
161
162 /**
163  * Generic input module.
164  *
165  * <p>This module is designed to handle input from multiple sources --
166  * keyboard, joystick, mouse, or even panel switches -- in a consistent
167  * way, and to allow users to rebind any of the actions at runtime.</p>
168  */
169 class FGInput : public SGSubsystem
170 {
171 public:
172
173   enum {
174     FG_MOD_NONE = 0,
175     FG_MOD_UP = 1,              // key- or button-up
176     FG_MOD_SHIFT = 2,
177     FG_MOD_CTRL = 4,
178     FG_MOD_ALT = 8,
179     FG_MOD_MAX = 16             // enough to handle all combinations
180   };
181
182
183   /**
184    * Default constructor.
185    */
186   FGInput ();
187
188
189   /**
190    * Destructor.
191    */
192   virtual ~FGInput();
193
194   //
195   // Implementation of SGSubsystem.
196   //
197   virtual void init ();
198   virtual void update (double dt);
199   virtual void suspend ();
200   virtual void resume ();
201   virtual bool is_suspended () const;
202
203
204   /**
205    * Control whether this is the default module to receive events.
206    *
207    * The first input module created will set itself as the default
208    * automatically.
209    *
210    * @param status true if this should be the default module for
211    * events, false otherwise.
212    */
213   virtual void makeDefault (bool status = true);
214
215
216   /**
217    * Handle a single keystroke.
218    *
219    * <p>Note: for special keys, the integer key code will be the Glut
220    * code + 256.</p>
221    *
222    * @param k The integer key code, as returned by glut.
223    * @param modifiers Modifier keys pressed (bitfield).
224    * @param x The mouse x position at the time of keypress.
225    * @param y The mouse y position at the time of keypress.
226    * @see #FG_MOD_SHIFT
227    * @see #FG_MOD_CTRL
228    * @see #FG_MOD_ALT
229    */
230   virtual void doKey (int k, int modifiers, int x, int y);
231
232
233   /**
234    * Handle a mouse click event.
235    *
236    * @param button The mouse button selected.
237    * @param updown Button status.
238    * @param x The X position of the mouse event, in screen coordinates.
239    * @param y The Y position of the mouse event, in screen coordinates.
240    */
241   virtual void doMouseClick (int button, int updown, int x, int y);
242
243
244   /**
245    * Handle mouse motion.
246    *
247    * @param x The new mouse x position, in screen coordinates.
248    * @param y The new mouse y position, in screen coordinates.
249    */
250   virtual void doMouseMotion (int x, int y);
251
252
253 private:
254                                 // Constants
255   enum 
256   {
257     MAX_KEYS = 1024,
258
259   #ifdef WIN32
260     MAX_JOYSTICKS = 2,
261   #else
262     MAX_JOYSTICKS = 10,
263   #endif
264     MAX_JOYSTICK_AXES = _JS_MAX_AXES,
265     MAX_JOYSTICK_BUTTONS = 32,
266
267     MAX_MICE = 1,
268     MAX_MOUSE_BUTTONS = 8
269   };
270   struct mouse;
271   friend struct mouse;
272
273   typedef vector<FGBinding *> binding_list_t;
274
275   /**
276    * Settings for a key or button.
277    */
278   struct button {
279     button ();
280     virtual ~button ();
281     bool is_repeatable;
282     float interval_sec;
283     float last_dt;
284     int last_state;
285     binding_list_t bindings[FG_MOD_MAX];
286   };
287
288
289   /**
290    * Settings for a single joystick axis.
291    */
292   struct axis {
293     axis ();
294     virtual ~axis ();
295     float last_value;
296     float tolerance;
297     binding_list_t bindings[FG_MOD_MAX];
298     float low_threshold;
299     float high_threshold;
300     struct button low;
301     struct button high;
302     float interval_sec;
303     double last_dt;
304   };
305
306
307   /**
308    * Settings for a joystick.
309    */
310   struct joystick {
311     joystick ();
312     virtual ~joystick ();
313     int jsnum;
314     jsJoystick * js;
315     int naxes;
316     int nbuttons;
317     axis * axes;
318     button * buttons;
319   };
320
321
322   /**
323    * Settings for a mouse mode.
324    */
325   struct mouse_mode {
326     mouse_mode ();
327     virtual ~mouse_mode ();
328     int cursor;
329     bool constrained;
330     bool pass_through;
331     button * buttons;
332     binding_list_t x_bindings[FG_MOD_MAX];
333     binding_list_t y_bindings[FG_MOD_MAX];
334   };
335
336
337   /**
338    * Settings for a mouse.
339    */
340   struct mouse {
341     mouse ();
342     virtual ~mouse ();
343     int x;
344     int y;
345     SGPropertyNode * mode_node;
346     SGPropertyNode * mouse_button_nodes[MAX_MOUSE_BUTTONS];
347     int nModes;
348     int current_mode;
349     mouse_mode * modes;
350   };
351
352
353   /**
354    * Initialize key bindings.
355    */
356   void _init_keyboard ();
357
358
359   /**
360    * Initialize joystick bindings.
361    */
362   void _init_joystick ();
363
364
365   /**
366    * Initialize mouse bindings.
367    */
368   void _init_mouse ();
369
370
371   /**
372    * Initialize a single button.
373    */
374   inline void _init_button (const SGPropertyNode * node,
375                             button &b,
376                             const string name);
377
378
379   /**
380    * Update the keyboard.
381    */
382   void _update_keyboard ();
383
384
385   /**
386    * Update the joystick.
387    */
388   void _update_joystick (double dt);
389
390
391   /**
392    * Update the mouse.
393    */
394   void _update_mouse ();
395
396
397   /**
398    * Update a single button.
399    */
400   inline void _update_button (button &b, int modifiers, bool pressed,
401                               int x, int y);
402
403
404   /**
405    * Read bindings and modifiers.
406    */
407   void _read_bindings (const SGPropertyNode * node,
408                        binding_list_t * binding_list,
409                        int modifiers);
410
411   /**
412    * Look up the bindings for a key code.
413    */
414   const vector<FGBinding *> &_find_key_bindings (unsigned int k,
415                                                  int modifiers);
416
417   button _key_bindings[MAX_KEYS];
418   joystick _joystick_bindings[MAX_JOYSTICKS];
419   mouse _mouse_bindings[MAX_MICE];
420
421 };
422
423
424 \f
425 ////////////////////////////////////////////////////////////////////////
426 // GLUT callbacks.
427 ////////////////////////////////////////////////////////////////////////
428
429 // Handle GLUT events.
430 extern "C" {
431
432 /**
433  * Key-down event handler for Glut.
434  *
435  * <p>Pass the value on to the FGInput module unless PUI wants it.</p>
436  *
437  * @param k The integer value for the key pressed.
438  * @param x (unused)
439  * @param y (unused)
440  */
441 void GLUTkey (unsigned char k, int x, int y);
442
443
444 /**
445  * Key-up event handler for GLUT.
446  *
447  * <p>PUI doesn't use this, so always pass it to the input manager.</p>
448  *
449  * @param k The integer value for the key pressed.
450  * @param x (unused)
451  * @param y (unused)
452  */
453 void GLUTkeyup (unsigned char k, int x, int y);
454
455
456 /**
457  * Special key-down handler for Glut.
458  *
459  * <p>Pass the value on to the FGInput module unless PUI wants it.
460  * The key value will have 256 added to it.</p>
461  *
462  * @param k The integer value for the key pressed (will have 256 added
463  * to it).
464  * @param x (unused)
465  * @param y (unused)
466  */
467 void GLUTspecialkey (int k, int x, int y);
468
469
470 /**
471  * Special key-up handler for Glut.
472  *
473  * @param k The integer value for the key pressed (will have 256 added
474  * to it).
475  * @param x (unused)
476  * @param y (unused)
477  */
478 void GLUTspecialkeyup (int k, int x, int y);
479
480
481 /**
482  * Mouse click handler for Glut.
483  *
484  * @param button The mouse button pressed.
485  * @param updown Press or release flag.
486  * @param x The x-location of the click.
487  * @param y The y-location of the click.
488  */
489 void GLUTmouse (int button, int updown, int x, int y);
490
491
492 /**
493  * Mouse motion handler for Glut.
494  *
495  * @param x The new x-location of the mouse.
496  * @param y The new y-location of the mouse.
497  */
498 void GLUTmotion (int x, int y);
499
500 } // extern "C"
501
502 #endif // _INPUT_HXX