]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.hxx
Changes to keep the various autopilot properties from stepping on each
[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/globals.hxx>
40
41 #include <map>
42 #include <vector>
43
44 SG_USING_STD(map);
45 SG_USING_STD(vector);
46
47
48 \f
49 ////////////////////////////////////////////////////////////////////////
50 // General binding support.
51 ////////////////////////////////////////////////////////////////////////
52
53
54 /**
55  * An input binding of some sort.
56  *
57  * <p>This class represents a binding that can be assigned to a
58  * keyboard key, a joystick button or axis, or even a panel
59  * instrument.</p>
60  */
61 class FGBinding : public FGConditional
62 {
63 public:
64
65   /**
66    * Default constructor.
67    */
68   FGBinding ();
69
70
71   /**
72    * Convenience constructor.
73    *
74    * @param node The binding will be built from this node.
75    */
76   FGBinding (const SGPropertyNode * node);
77
78
79   /**
80    * Destructor.
81    */
82   virtual ~FGBinding ();
83
84
85   /**
86    * Get the command name.
87    *
88    * @return The string name of the command for this binding.
89    */
90   virtual const string &getCommandName () const { return _command_name; }
91
92
93   /**
94    * Get the command itself.
95    *
96    * @return The command associated with this binding, or 0 if none
97    * is present.
98    */
99   virtual SGCommandMgr::command_t getCommand () const { return _command; }
100
101
102   /**
103    * Get the argument that will be passed to the command.
104    *
105    * @return A property node that will be passed to the command as its
106    * argument, or 0 if none was supplied.
107    */
108   virtual const SGPropertyNode * getArg () { return _arg; }
109   
110
111   /**
112    * Read a binding from a property node.
113    *
114    * @param node The property node containing the binding.
115    */
116   virtual void read (const SGPropertyNode * node);
117
118
119   /**
120    * Fire a binding.
121    */
122   virtual void fire () const;
123
124
125   /**
126    * Fire a binding with a setting (i.e. joystick axis).
127    *
128    * A double 'setting' property will be added to the arguments.
129    *
130    * @param setting The input setting, usually between -1.0 and 1.0.
131    */
132   virtual void fire (double setting) const;
133
134
135 private:
136   string _command_name;
137   SGCommandMgr::command_t _command;
138   mutable SGPropertyNode * _arg;
139   mutable SGPropertyNode * _setting;
140   mutable SGCommandState * _command_state;
141 };
142
143
144 \f
145 ////////////////////////////////////////////////////////////////////////
146 // General input mapping support.
147 ////////////////////////////////////////////////////////////////////////
148
149
150 /**
151  * Generic input module.
152  *
153  * <p>This module is designed to handle input from multiple sources --
154  * keyboard, joystick, mouse, or even panel switches -- in a consistent
155  * way, and to allow users to rebind any of the actions at runtime.</p>
156  */
157 class FGInput : public FGSubsystem
158 {
159 public:
160
161   enum {
162     FG_MOD_NONE = 0,
163     FG_MOD_UP = 1,              // key- or button-up
164     FG_MOD_SHIFT = 2,
165     FG_MOD_CTRL = 4,
166     FG_MOD_ALT = 8,
167     FG_MOD_MAX = 16             // enough to handle all combinations
168   };
169
170
171   /**
172    * Default constructor.
173    */
174   FGInput ();
175
176
177   /**
178    * Destructor.
179    */
180   virtual ~FGInput();
181
182   //
183   // Implementation of FGSubsystem.
184   //
185   virtual void init ();
186   virtual void bind ();
187   virtual void unbind ();
188   virtual void update (int dt);
189
190
191   /**
192    * Handle a single keystroke.
193    *
194    * <p>Note: for special keys, the integer key code will be the Glut
195    * code + 256.</p>
196    *
197    * @param k The integer key code, as returned by glut.
198    * @param modifiers Modifier keys pressed (bitfield).
199    * @param x The mouse x position at the time of keypress.
200    * @param y The mouse y position at the time of keypress.
201    * @see #FG_MOD_SHIFT
202    * @see #FG_MOD_CTRL
203    * @see #FG_MOD_ALT
204    */
205   virtual void doKey (int k, int modifiers, int x, int y);
206
207
208 private:
209                                 // Constants
210   enum 
211   {
212     MAX_KEYS = 1024,
213
214   #ifdef WIN32
215     MAX_JOYSTICKS = 2,
216   #else
217     MAX_JOYSTICKS = 10,
218   #endif
219     MAX_AXES = _JS_MAX_AXES,
220     MAX_BUTTONS = 32
221   };
222
223
224   typedef vector<FGBinding *> binding_list_t;
225
226   /**
227    * Settings for a key or button.
228    */
229   struct button {
230     button ();
231     virtual ~button ();
232     bool is_repeatable;
233     int last_state;
234     binding_list_t bindings[FG_MOD_MAX];
235   };
236
237
238   /**
239    * Settings for a single joystick axis.
240    */
241   struct axis {
242     axis ();
243     virtual ~axis ();
244     float last_value;
245     float tolerance;
246     binding_list_t bindings[FG_MOD_MAX];
247     float low_threshold;
248     float high_threshold;
249     struct button low;
250     struct button high;
251   };
252
253
254   /**
255    * Settings for a joystick.
256    */
257   struct joystick {
258     joystick ();
259     virtual ~joystick ();
260     int jsnum;
261     jsJoystick * js;
262     int naxes;
263     int nbuttons;
264     axis * axes;
265     button * buttons;
266   };
267
268
269   /**
270    * Initialize key bindings.
271    */
272   void _init_keyboard ();
273
274
275   /**
276    * Initialize joystick bindings.
277    */
278   void _init_joystick ();
279
280
281   /**
282    * Initialize a single button.
283    */
284   inline void _init_button (const SGPropertyNode * node,
285                             button &b,
286                             const string name);
287
288
289   /**
290    * Update the keyboard.
291    */
292   void _update_keyboard ();
293
294
295   /**
296    * Update the joystick.
297    */
298   void _update_joystick ();
299
300
301   /**
302    * Update a single button.
303    */
304   inline void _update_button (button &b, int modifiers, bool pressed);
305
306
307   /**
308    * Read bindings and modifiers.
309    */
310   void _read_bindings (const SGPropertyNode * node,
311                        binding_list_t * binding_list,
312                        int modifiers);
313
314   /**
315    * Look up the bindings for a key code.
316    */
317   const vector<FGBinding *> &_find_key_bindings (unsigned int k,
318                                                  int modifiers);
319
320   button _key_bindings[MAX_KEYS];
321   joystick _joystick_bindings[MAX_JOYSTICKS];
322
323 };
324
325
326 extern FGInput current_input;
327
328
329 \f
330 ////////////////////////////////////////////////////////////////////////
331 // GLUT callbacks.
332 ////////////////////////////////////////////////////////////////////////
333
334 // Handle keyboard events
335 void GLUTkey(unsigned char k, int x, int y);
336 void GLUTkeyup(unsigned char k, int x, int y);
337 void GLUTspecialkey(int k, int x, int y);
338 void GLUTspecialkeyup(int k, int x, int y);
339
340 #endif // _CONTROLS_HXX