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++
34 #include <simgear/compiler.h>
36 #include <simgear/structure/subsystem_mgr.hxx>
37 #include <simgear/structure/commands.hxx>
38 #include <simgear/props/condition.hxx>
39 #include <simgear/props/props.hxx>
41 #include <Main/fg_os.hxx>
42 #include <Main/fg_props.hxx>
43 #include <Main/globals.hxx>
54 #if defined( UL_WIN32 )
55 #define TGT_PLATFORM "windows"
56 #elif defined ( UL_MAC_OSX )
57 #define TGT_PLATFORM "mac"
59 #define TGT_PLATFORM "unix"
64 ////////////////////////////////////////////////////////////////////////
65 // General binding support.
66 ////////////////////////////////////////////////////////////////////////
70 * An input binding of some sort.
72 * <p>This class represents a binding that can be assigned to a
73 * keyboard key, a joystick button or axis, or even a panel
76 class FGBinding : public SGConditional
81 * Default constructor.
87 * Convenience constructor.
89 * @param node The binding will be built from this node.
91 FGBinding (const SGPropertyNode * node);
97 virtual ~FGBinding () {}
101 * Get the command name.
103 * @return The string name of the command for this binding.
105 virtual const string &getCommandName () const { return _command_name; }
109 * Get the command itself.
111 * @return The command associated with this binding, or 0 if none
114 virtual SGCommandMgr::command_t getCommand () const { return _command; }
118 * Get the argument that will be passed to the command.
120 * @return A property node that will be passed to the command as its
121 * argument, or 0 if none was supplied.
123 virtual const SGPropertyNode * getArg () { return _arg; }
127 * Read a binding from a property node.
129 * @param node The property node containing the binding.
131 virtual void read (const SGPropertyNode * node);
137 virtual void fire () const;
141 * Fire a binding with a scaled movement (rather than absolute position).
143 virtual void fire (double offset, double max) const;
147 * Fire a binding with a setting (i.e. joystick axis).
149 * A double 'setting' property will be added to the arguments.
151 * @param setting The input setting, usually between -1.0 and 1.0.
153 virtual void fire (double setting) const;
158 FGBinding (const FGBinding &binding);
160 string _command_name;
161 mutable SGCommandMgr::command_t _command;
162 mutable SGPropertyNode_ptr _arg;
163 mutable SGPropertyNode_ptr _setting;
168 ////////////////////////////////////////////////////////////////////////
169 // General input mapping support.
170 ////////////////////////////////////////////////////////////////////////
174 * Generic input module.
176 * <p>This module is designed to handle input from multiple sources --
177 * keyboard, joystick, mouse, or even panel switches -- in a consistent
178 * way, and to allow users to rebind any of the actions at runtime.</p>
180 class FGInput : public SGSubsystem
184 * Default constructor.
194 // Implementation of SGSubsystem.
196 virtual void init ();
197 virtual void update (double dt);
198 virtual void suspend ();
199 virtual void resume ();
200 virtual bool is_suspended () const;
204 * Control whether this is the default module to receive events.
206 * The first input module created will set itself as the default
209 * @param status true if this should be the default module for
210 * events, false otherwise.
212 virtual void makeDefault (bool status = true);
216 * Handle a single keystroke.
218 * @param k The integer key code, see Main/fg_os.hxx
219 * @param modifiers Modifier keys pressed (bitfield).
220 * @param x The mouse x position at the time of keypress.
221 * @param y The mouse y position at the time of keypress.
223 virtual void doKey (int k, int modifiers, int x, int y);
227 * Handle a mouse click event.
229 * @param button The mouse button selected.
230 * @param updown Button status.
231 * @param x The X position of the mouse event, in screen coordinates.
232 * @param y The Y position of the mouse event, in screen coordinates.
234 virtual void doMouseClick (int button, int updown, int x, int y);
238 * Handle mouse motion.
240 * @param x The new mouse x position, in screen coordinates.
241 * @param y The new mouse y position, in screen coordinates.
243 virtual void doMouseMotion (int x, int y);
257 MAX_JOYSTICK_AXES = _JS_MAX_AXES,
258 MAX_JOYSTICK_BUTTONS = 32,
261 MAX_MOUSE_BUTTONS = 8
266 typedef vector<FGBinding *> binding_list_t;
269 * Settings for a key or button.
278 binding_list_t bindings[KEYMOD_MAX];
283 * Settings for a single joystick axis.
290 binding_list_t bindings[KEYMOD_MAX];
292 float high_threshold;
301 * Settings for a joystick.
305 virtual ~joystick ();
316 * Settings for a mouse mode.
320 virtual ~mouse_mode ();
325 binding_list_t x_bindings[KEYMOD_MAX];
326 binding_list_t y_bindings[KEYMOD_MAX];
331 * Settings for a mouse.
338 SGPropertyNode * mode_node;
339 SGPropertyNode * mouse_button_nodes[MAX_MOUSE_BUTTONS];
347 * Initialize key bindings.
349 void _init_keyboard ();
353 * Initialize joystick bindings.
355 void _init_joystick ();
359 * Initialize mouse bindings.
365 * Initialize a single button.
367 inline void _init_button (const SGPropertyNode * node,
373 * Update the keyboard.
375 void _update_keyboard ();
379 * Update the joystick.
381 void _update_joystick (double dt);
387 void _update_mouse ();
391 * Update a single button.
393 inline void _update_button (button &b, int modifiers, bool pressed,
398 * Read bindings and modifiers.
400 void _read_bindings (const SGPropertyNode * node,
401 binding_list_t * binding_list,
405 * Look up the bindings for a key code.
407 const vector<FGBinding *> &_find_key_bindings (unsigned int k,
410 button _key_bindings[MAX_KEYS];
411 joystick _joystick_bindings[MAX_JOYSTICKS];
412 mouse _mouse_bindings[MAX_MICE];