]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.hxx
755b6d66fd74c8a74f32edaaf190eb39982733a3
[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 update (double dt);
198   virtual void suspend ();
199   virtual void resume ();
200   virtual bool is_suspended () const;
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    * @param k The integer key code, see Main/fg_os.hxx
219    * @param modifiers Modifier keys pressed (bitfield).
220    * @param x The mouse x position at the time of keypress.
221    * @param y The mouse y position at the time of keypress.
222    */
223   virtual void doKey (int k, int modifiers, int x, int y);
224
225
226   /**
227    * Handle a mouse click event.
228    *
229    * @param button The mouse button selected.
230    * @param updown Button status.
231    * @param x The X position of the mouse event, in screen coordinates.
232    * @param y The Y position of the mouse event, in screen coordinates.
233    */
234   virtual void doMouseClick (int button, int updown, int x, int y);
235
236
237   /**
238    * Handle mouse motion.
239    *
240    * @param x The new mouse x position, in screen coordinates.
241    * @param y The new mouse y position, in screen coordinates.
242    */
243   virtual void doMouseMotion (int x, int y);
244
245
246 private:
247                                 // Constants
248   enum 
249   {
250     MAX_KEYS = 1024,
251
252   #ifdef WIN32
253     MAX_JOYSTICKS = 2,
254   #else
255     MAX_JOYSTICKS = 10,
256   #endif
257     MAX_JOYSTICK_AXES = _JS_MAX_AXES,
258     MAX_JOYSTICK_BUTTONS = 32,
259
260     MAX_MICE = 1,
261     MAX_MOUSE_BUTTONS = 8
262   };
263   struct mouse;
264   friend struct mouse;
265
266   typedef vector<FGBinding *> binding_list_t;
267
268   /**
269    * Settings for a key or button.
270    */
271   struct button {
272     button ();
273     virtual ~button ();
274     bool is_repeatable;
275     float interval_sec;
276     float last_dt;
277     int last_state;
278     binding_list_t bindings[KEYMOD_MAX];
279   };
280
281
282   /**
283    * Settings for a single joystick axis.
284    */
285   struct axis {
286     axis ();
287     virtual ~axis ();
288     float last_value;
289     float tolerance;
290     binding_list_t bindings[KEYMOD_MAX];
291     float low_threshold;
292     float high_threshold;
293     struct button low;
294     struct button high;
295     float interval_sec;
296     double last_dt;
297   };
298
299
300   /**
301    * Settings for a joystick.
302    */
303   struct joystick {
304     joystick ();
305     virtual ~joystick ();
306     int jsnum;
307     jsJoystick * js;
308     int naxes;
309     int nbuttons;
310     axis * axes;
311     button * buttons;
312   };
313
314
315   /**
316    * Settings for a mouse mode.
317    */
318   struct mouse_mode {
319     mouse_mode ();
320     virtual ~mouse_mode ();
321     int cursor;
322     bool constrained;
323     bool pass_through;
324     button * buttons;
325     binding_list_t x_bindings[KEYMOD_MAX];
326     binding_list_t y_bindings[KEYMOD_MAX];
327   };
328
329
330   /**
331    * Settings for a mouse.
332    */
333   struct mouse {
334     mouse ();
335     virtual ~mouse ();
336     int x;
337     int y;
338     SGPropertyNode * mode_node;
339     SGPropertyNode * mouse_button_nodes[MAX_MOUSE_BUTTONS];
340     int nModes;
341     int current_mode;
342     double timeout;
343     int save_x;
344     int save_y;
345     mouse_mode * modes;
346   };
347
348
349   /**
350    * Initialize key bindings.
351    */
352   void _init_keyboard ();
353
354
355   /**
356    * Initialize joystick bindings.
357    */
358   void _init_joystick ();
359
360
361   /**
362    * Initialize mouse bindings.
363    */
364   void _init_mouse ();
365
366
367   /**
368    * Initialize a single button.
369    */
370   inline void _init_button (const SGPropertyNode * node,
371                             button &b,
372                             const string name);
373
374
375   /**
376    * Update the keyboard.
377    */
378   void _update_keyboard ();
379
380
381   /**
382    * Update the joystick.
383    */
384   void _update_joystick (double dt);
385
386
387   /**
388    * Update the mouse.
389    */
390   void _update_mouse (double dt);
391
392
393   /**
394    * Update a single button.
395    */
396   inline void _update_button (button &b, int modifiers, bool pressed,
397                               int x, int y);
398
399
400   /**
401    * Read bindings and modifiers.
402    */
403   void _read_bindings (const SGPropertyNode * node,
404                        binding_list_t * binding_list,
405                        int modifiers);
406
407   /**
408    * Look up the bindings for a key code.
409    */
410   const vector<FGBinding *> &_find_key_bindings (unsigned int k,
411                                                  int modifiers);
412
413   button _key_bindings[MAX_KEYS];
414   joystick _joystick_bindings[MAX_JOYSTICKS];
415   mouse _mouse_bindings[MAX_MICE];
416
417 };
418
419 #endif // _INPUT_HXX