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"
57 #define TGT_PLATFORM "unix"
62 ////////////////////////////////////////////////////////////////////////
63 // General binding support.
64 ////////////////////////////////////////////////////////////////////////
68 * An input binding of some sort.
70 * <p>This class represents a binding that can be assigned to a
71 * keyboard key, a joystick button or axis, or even a panel
74 class FGBinding : public SGConditional
79 * Default constructor.
85 * Convenience constructor.
87 * @param node The binding will be built from this node.
89 FGBinding (const SGPropertyNode * node);
95 virtual ~FGBinding () {}
99 * Get the command name.
101 * @return The string name of the command for this binding.
103 virtual const string &getCommandName () const { return _command_name; }
107 * Get the command itself.
109 * @return The command associated with this binding, or 0 if none
112 virtual SGCommandMgr::command_t getCommand () const { return _command; }
116 * Get the argument that will be passed to the command.
118 * @return A property node that will be passed to the command as its
119 * argument, or 0 if none was supplied.
121 virtual const SGPropertyNode * getArg () { return _arg; }
125 * Read a binding from a property node.
127 * @param node The property node containing the binding.
129 virtual void read (const SGPropertyNode * node);
135 virtual void fire () const;
139 * Fire a binding with a scaled movement (rather than absolute position).
141 virtual void fire (double offset, double max) const;
145 * Fire a binding with a setting (i.e. joystick axis).
147 * A double 'setting' property will be added to the arguments.
149 * @param setting The input setting, usually between -1.0 and 1.0.
151 virtual void fire (double setting) const;
156 FGBinding (const FGBinding &binding);
158 string _command_name;
159 mutable SGCommandMgr::command_t _command;
160 mutable SGPropertyNode_ptr _arg;
161 mutable SGPropertyNode_ptr _setting;
166 ////////////////////////////////////////////////////////////////////////
167 // General input mapping support.
168 ////////////////////////////////////////////////////////////////////////
172 * Generic input module.
174 * <p>This module is designed to handle input from multiple sources --
175 * keyboard, joystick, mouse, or even panel switches -- in a consistent
176 * way, and to allow users to rebind any of the actions at runtime.</p>
178 class FGInput : public SGSubsystem
182 * Default constructor.
192 // Implementation of SGSubsystem.
194 virtual void init ();
195 virtual void update (double dt);
196 virtual void suspend ();
197 virtual void resume ();
198 virtual bool is_suspended () const;
202 * Control whether this is the default module to receive events.
204 * The first input module created will set itself as the default
207 * @param status true if this should be the default module for
208 * events, false otherwise.
210 virtual void makeDefault (bool status = true);
214 * Handle a single keystroke.
216 * @param k The integer key code, see Main/fg_os.hxx
217 * @param modifiers Modifier keys pressed (bitfield).
218 * @param x The mouse x position at the time of keypress.
219 * @param y The mouse y position at the time of keypress.
221 virtual void doKey (int k, int modifiers, int x, int y);
225 * Handle a mouse click event.
227 * @param button The mouse button selected.
228 * @param updown Button status.
229 * @param x The X position of the mouse event, in screen coordinates.
230 * @param y The Y position of the mouse event, in screen coordinates.
232 virtual void doMouseClick (int button, int updown, int x, int y);
236 * Handle mouse motion.
238 * @param x The new mouse x position, in screen coordinates.
239 * @param y The new mouse y position, in screen coordinates.
241 virtual void doMouseMotion (int x, int y);
255 MAX_JOYSTICK_AXES = _JS_MAX_AXES,
256 MAX_JOYSTICK_BUTTONS = 32,
259 MAX_MOUSE_BUTTONS = 8
264 typedef vector<FGBinding *> binding_list_t;
267 * Settings for a key or button.
276 binding_list_t bindings[KEYMOD_MAX];
281 * Settings for a single joystick axis.
288 binding_list_t bindings[KEYMOD_MAX];
290 float high_threshold;
299 * Settings for a joystick.
303 virtual ~joystick ();
314 * Settings for a mouse mode.
318 virtual ~mouse_mode ();
323 binding_list_t x_bindings[KEYMOD_MAX];
324 binding_list_t y_bindings[KEYMOD_MAX];
329 * Settings for a mouse.
336 SGPropertyNode * mode_node;
337 SGPropertyNode * mouse_button_nodes[MAX_MOUSE_BUTTONS];
345 * Initialize key bindings.
347 void _init_keyboard ();
351 * Initialize joystick bindings.
353 void _init_joystick ();
357 * Initialize mouse bindings.
363 * Initialize a single button.
365 inline void _init_button (const SGPropertyNode * node,
371 * Update the keyboard.
373 void _update_keyboard ();
377 * Update the joystick.
379 void _update_joystick (double dt);
385 void _update_mouse ();
389 * Update a single button.
391 inline void _update_button (button &b, int modifiers, bool pressed,
396 * Read bindings and modifiers.
398 void _read_bindings (const SGPropertyNode * node,
399 binding_list_t * binding_list,
403 * Look up the bindings for a key code.
405 const vector<FGBinding *> &_find_key_bindings (unsigned int k,
408 button _key_bindings[MAX_KEYS];
409 joystick _joystick_bindings[MAX_JOYSTICKS];
410 mouse _mouse_bindings[MAX_MICE];