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