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 reinit ();
198 virtual void update (double dt);
199 virtual void suspend ();
200 virtual void resume ();
201 virtual bool is_suspended () const;
205 * Control whether this is the default module to receive events.
207 * The first input module created will set itself as the default
210 * @param status true if this should be the default module for
211 * events, false otherwise.
213 virtual void makeDefault (bool status = true);
217 * Handle a single keystroke.
219 * @param k The integer key code, see Main/fg_os.hxx
220 * @param modifiers Modifier keys pressed (bitfield).
221 * @param x The mouse x position at the time of keypress.
222 * @param y The mouse y position at the time of keypress.
224 virtual void doKey (int k, int modifiers, int x, int y);
228 * Handle a mouse click event.
230 * @param button The mouse button selected.
231 * @param updown Button status.
232 * @param x The X position of the mouse event, in screen coordinates.
233 * @param y The Y position of the mouse event, in screen coordinates.
235 virtual void doMouseClick (int button, int updown, int x, int y);
239 * Handle mouse motion.
241 * @param x The new mouse x position, in screen coordinates.
242 * @param y The new mouse y position, in screen coordinates.
244 virtual void doMouseMotion (int x, int y);
258 MAX_JOYSTICK_AXES = _JS_MAX_AXES,
259 MAX_JOYSTICK_BUTTONS = 32,
262 MAX_MOUSE_BUTTONS = 8
267 typedef vector<FGBinding *> binding_list_t;
270 * Settings for a key or button.
279 binding_list_t bindings[KEYMOD_MAX];
284 * Settings for a single joystick axis.
291 binding_list_t bindings[KEYMOD_MAX];
293 float high_threshold;
302 * Settings for a joystick.
306 virtual ~joystick ();
317 * Settings for a mouse mode.
321 virtual ~mouse_mode ();
326 binding_list_t x_bindings[KEYMOD_MAX];
327 binding_list_t y_bindings[KEYMOD_MAX];
332 * Settings for a mouse.
339 SGPropertyNode * mode_node;
340 SGPropertyNode * mouse_button_nodes[MAX_MOUSE_BUTTONS];
351 * Initialize key bindings.
353 void _init_keyboard ();
357 * Initialize joystick bindings.
359 void _init_joystick ();
363 * Initialize mouse bindings.
369 * Initialize a single button.
371 inline void _init_button (const SGPropertyNode * node,
377 * Update the keyboard.
379 void _update_keyboard ();
383 * Update the joystick.
385 void _update_joystick (double dt);
391 void _update_mouse (double dt);
395 * Update a single button.
397 inline void _update_button (button &b, int modifiers, bool pressed,
402 * Read bindings and modifiers.
404 void _read_bindings (const SGPropertyNode * node,
405 binding_list_t * binding_list,
409 * Look up the bindings for a key code.
411 const vector<FGBinding *> &_find_key_bindings (unsigned int k,
414 button _key_bindings[MAX_KEYS];
415 joystick _joystick_bindings[MAX_JOYSTICKS];
416 mouse _mouse_bindings[MAX_MICE];