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