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