]> git.mxchange.org Git - flightgear.git/blob - src/Input/input.hxx
46d8c6c2c2305d52f46239d4890981c2776dd55b
[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 <simgear/compiler.h>
32
33 #include <simgear/misc/props.hxx>
34
35 #include <Main/fgfs.hxx>
36 #include <Main/globals.hxx>
37
38 #include <map>
39 #include <vector>
40
41 SG_USING_STD(map);
42 SG_USING_STD(vector);
43
44 /**
45  * An input binding of some sort.
46  *
47  * <p>This class represents a binding that can be assigned to a
48  * keyboard key, a joystick button or axis, or even a panel
49  * instrument.</p>
50  */
51 class FGBinding
52 {
53 public:
54
55   enum Action {
56     ACTION_NONE,
57     ACTION_SWITCH,
58     ACTION_ADJUST,
59     ACTION_ASSIGN
60   };
61
62   FGBinding ();
63   FGBinding (const SGPropertyNode * node);
64   virtual ~FGBinding ();
65
66   virtual Action getAction () const { return _action; }
67   virtual SGPropertyNode * getProperty () { return _node; }
68   virtual const SGPropertyNode * getProperty () const { return _node; }
69   virtual const SGValue * getAdjustStep () const { return _adjust_step; }
70   virtual const SGValue * getAssignValue () const { return _assign_value; }
71   
72   virtual void setAction (Action action);
73   virtual void setProperty (SGPropertyNode * node);
74   virtual void setAdjustStep (const SGValue * step);
75   virtual void setAssignValue (const SGValue * value);
76
77   virtual void read (const SGPropertyNode * node);
78
79   virtual void fire () const;
80 //   virtual void fire (double value);
81 //   virtual void fire (int xdelta, int ydelta);
82
83 private:
84   Action _action;
85   SGPropertyNode * _node;
86   const SGValue * _adjust_step;
87   const SGValue * _assign_value;
88 };
89
90
91 /**
92  * Generic input module.
93  *
94  * <p>This module is designed to handle input from multiple sources --
95  * keyboard, joystick, mouse, or even panel switches -- in a consistent
96  * way, and to allow users to rebind any of the actions at runtime.</p>
97  */
98 class FGInput : public FGSubsystem
99 {
100 public:
101
102   enum {
103     FG_MOD_NONE = 0,
104     FG_MOD_SHIFT = 1,
105     FG_MOD_CTRL = 2,
106     FG_MOD_ALT = 4,
107     FG_MOD_MAX = 8                      // one past all modifiers
108   };
109
110   FGInput();
111   virtual ~FGInput();
112
113   //
114   // Implementation of FGSubsystem.
115   //
116   virtual void init ();
117   virtual void bind ();
118   virtual void unbind ();
119   virtual void update ();
120
121
122   /**
123    * Handle a single keystroke.
124    *
125    * <p>Note: for special keys, the integer key code will be the Glut
126    * code + 256.</p>
127    *
128    * @param k The integer key code, as returned by glut.
129    * @param modifiers Modifier keys pressed (bitfield).
130    * @param x The mouse x position at the time of keypress.
131    * @param y The mouse y position at the time of keypress.
132    * @see #FG_MOD_SHIFT
133    * @see #FG_MOD_CTRL
134    * @see #FG_MOD_ALT
135    */
136   virtual void doKey (int k, int modifiers, int x, int y);
137
138
139   /**
140    * Fire off a single-trigger action.
141    *
142    * <p>This method fires an action triggered by a key or button
143    * press, with no additional quantity information.</p> 
144    *
145    * @param binding The property node with the binding.
146    */
147   virtual void action (const SGPropertyNode * binding);
148
149
150   /**
151    * Fire off a quantity action.
152    *
153    * <p>This method fires an action triggered by a change in value,
154    * such as a slider or axis.</p>
155    *
156    * @param action The property node with the binding.
157    * @param value The new value.
158    */
159 //   virtual void action (const SGPropertyNode * binding, double value);
160
161
162   /**
163    * Fire off a movement action.
164    *
165    * <p>This method fires an action triggered by relative movement
166    * rather than an absolute value; it is especially applicable to
167    * mouse-movement bindings.</p>
168    *
169    * @param binding The property node containing the binding.
170    * @param xdelta The amount of X movement.
171    * @param ydelta The amount of Y movement.
172    */
173 //   virtual void action (const SGPropertyNode * binding, int xdelta, int ydelta);
174
175 private:
176
177   /**
178    * Look up the bindings for a key code.
179    */
180   const vector<FGBinding> * _find_bindings (int k, int modifiers);
181
182   typedef map<int,vector<FGBinding> > keyboard_map;
183   keyboard_map _key_bindings[FG_MOD_MAX];
184
185 };
186
187 extern FGInput current_input;
188
189 #endif // _CONTROLS_HXX