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