1 // input.hxx -- handle user input from various sources.
3 // Written by David Megginson, started May 2001.
5 // Copyright (C) 2001 David Megginson, david@megginson.com
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.
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.
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.
28 # error This library requires C++
33 #include <simgear/compiler.h>
35 #include <simgear/misc/commands.hxx>
36 #include <simgear/misc/props.hxx>
38 #include <Main/fgfs.hxx>
39 #include <Main/globals.hxx>
48 * An input binding of some sort.
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
59 * Default constructor.
65 * Convenience constructor.
67 * @param node The binding will be built from this node.
69 FGBinding (const SGPropertyNode * node);
75 virtual ~FGBinding ();
79 * Get the command name.
81 * @return The string name of the command for this binding.
83 virtual const string &getCommandName () const { return _command_name; }
87 * Get the command itself.
89 * @return The command associated with this binding, or 0 if none
92 virtual SGCommandMgr::command_t getCommand () const { return _command; }
96 * Get the argument that will be passed to the command.
98 * @return A property node that will be passed to the command as its
99 * argument, or 0 if none was supplied.
101 virtual const SGPropertyNode * getArg () { return _arg; }
105 * Read a binding from a property node.
107 * @param node The property node containing the binding.
109 virtual void read (const SGPropertyNode * node);
115 virtual void fire () const;
119 * Fire a binding with a setting (i.e. joystick axis).
121 * A double 'setting' property will be added to the arguments.
123 * @param setting The input setting, usually between -1.0 and 1.0.
125 virtual void fire (double setting) const;
129 void _fire (const SGPropertyNode *arg) const;
130 string _command_name;
131 SGCommandMgr::command_t _command;
132 const SGPropertyNode * _arg;
137 * Generic input module.
139 * <p>This module is designed to handle input from multiple sources --
140 * keyboard, joystick, mouse, or even panel switches -- in a consistent
141 * way, and to allow users to rebind any of the actions at runtime.</p>
143 class FGInput : public FGSubsystem
149 FG_MOD_UP = 1, // key- or button-up
153 FG_MOD_MAX = 16 // enough to handle all combinations
160 // Implementation of FGSubsystem.
162 virtual void init ();
163 virtual void bind ();
164 virtual void unbind ();
165 virtual void update ();
169 * Handle a single keystroke.
171 * <p>Note: for special keys, the integer key code will be the Glut
174 * @param k The integer key code, as returned by glut.
175 * @param modifiers Modifier keys pressed (bitfield).
176 * @param x The mouse x position at the time of keypress.
177 * @param y The mouse y position at the time of keypress.
182 virtual void doKey (int k, int modifiers, int x, int y);
187 typedef vector<FGBinding> binding_list_t;
190 static const int MAX_KEYS = 1024;
192 static const int MAX_JOYSTICKS = 2;
194 static const int MAX_JOYSTICKS = 10;
196 static const int MAX_AXES = _JS_MAX_AXES;
197 static const int MAX_BUTTONS = 32;
201 * Settings for a key or button.
205 : is_repeatable(true),
210 binding_list_t bindings[FG_MOD_MAX];
215 * Settings for a single joystick axis.
219 : last_value(9999999),
224 binding_list_t bindings[FG_MOD_MAX];
229 * Settings for a joystick.
232 virtual ~joystick () {
246 * Initialize key bindings.
248 void _init_keyboard ();
252 * Initialize joystick bindings.
254 void _init_joystick ();
258 * Update the keyboard.
260 void _update_keyboard ();
264 * Update the joystick.
266 void _update_joystick ();
270 * Read bindings and modifiers.
272 void _read_bindings (const SGPropertyNode * node,
273 binding_list_t * binding_list,
277 * Look up the bindings for a key code.
279 const vector<FGBinding> &_find_key_bindings (unsigned int k, int modifiers);
281 button _key_bindings[MAX_KEYS];
282 joystick _joystick_bindings[MAX_JOYSTICKS];
286 extern FGInput current_input;
288 #endif // _CONTROLS_HXX