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