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>
49 ////////////////////////////////////////////////////////////////////////
50 // General binding support.
51 ////////////////////////////////////////////////////////////////////////
55 * An input binding of some sort.
57 * <p>This class represents a binding that can be assigned to a
58 * keyboard key, a joystick button or axis, or even a panel
61 class FGBinding : public FGConditional
66 * Default constructor.
72 * Convenience constructor.
74 * @param node The binding will be built from this node.
76 FGBinding (const SGPropertyNode * node);
82 virtual ~FGBinding ();
86 * Get the command name.
88 * @return The string name of the command for this binding.
90 virtual const string &getCommandName () const { return _command_name; }
94 * Get the command itself.
96 * @return The command associated with this binding, or 0 if none
99 virtual SGCommandMgr::command_t getCommand () const { return _command; }
103 * Get the argument that will be passed to the command.
105 * @return A property node that will be passed to the command as its
106 * argument, or 0 if none was supplied.
108 virtual const SGPropertyNode * getArg () { return _arg; }
112 * Read a binding from a property node.
114 * @param node The property node containing the binding.
116 virtual void read (const SGPropertyNode * node);
122 virtual void fire () const;
126 * Fire a binding with a scaled movement (rather than absolute position).
128 virtual void fire (double offset, double max) const;
132 * Fire a binding with a setting (i.e. joystick axis).
134 * A double 'setting' property will be added to the arguments.
136 * @param setting The input setting, usually between -1.0 and 1.0.
138 virtual void fire (double setting) const;
142 string _command_name;
143 SGCommandMgr::command_t _command;
144 mutable SGPropertyNode * _arg;
145 mutable SGPropertyNode * _setting;
146 mutable SGCommandState * _command_state;
151 ////////////////////////////////////////////////////////////////////////
152 // General input mapping support.
153 ////////////////////////////////////////////////////////////////////////
157 * Generic input module.
159 * <p>This module is designed to handle input from multiple sources --
160 * keyboard, joystick, mouse, or even panel switches -- in a consistent
161 * way, and to allow users to rebind any of the actions at runtime.</p>
163 class FGInput : public FGSubsystem
169 FG_MOD_UP = 1, // key- or button-up
173 FG_MOD_MAX = 16 // enough to handle all combinations
178 * Default constructor.
189 // Implementation of FGSubsystem.
191 virtual void init ();
192 virtual void bind ();
193 virtual void unbind ();
194 virtual void update (int dt);
198 * Handle a single keystroke.
200 * <p>Note: for special keys, the integer key code will be the Glut
203 * @param k The integer key code, as returned by glut.
204 * @param modifiers Modifier keys pressed (bitfield).
205 * @param x The mouse x position at the time of keypress.
206 * @param y The mouse y position at the time of keypress.
211 virtual void doKey (int k, int modifiers, int x, int y);
215 * Handle a mouse click event.
217 * @param button The mouse button selected.
218 * @param updown Button status.
219 * @param x The X position of the mouse event, in screen coordinates.
220 * @param y The Y position of the mouse event, in screen coordinates.
222 virtual void doMouseClick (int button, int updown, int x, int y);
226 * Handle mouse motion.
228 * @param x The new mouse x position, in screen coordinates.
229 * @param y The new mouse y position, in screen coordinates.
231 virtual void doMouseMotion (int x, int y);
245 MAX_JOYSTICK_AXES = _JS_MAX_AXES,
246 MAX_JOYSTICK_BUTTONS = 32,
249 MAX_MOUSE_BUTTONS = 8
254 typedef vector<FGBinding *> binding_list_t;
257 * Settings for a key or button.
264 binding_list_t bindings[FG_MOD_MAX];
269 * Settings for a single joystick axis.
276 binding_list_t bindings[FG_MOD_MAX];
278 float high_threshold;
285 * Settings for a joystick.
289 virtual ~joystick ();
300 * Settings for a mouse mode.
304 virtual ~mouse_mode ();
309 binding_list_t x_bindings[FG_MOD_MAX];
310 binding_list_t y_bindings[FG_MOD_MAX];
315 * Settings for a mouse.
322 SGPropertyNode * mode_node;
323 SGPropertyNode * mouse_button_nodes[MAX_MOUSE_BUTTONS];
331 * Initialize key bindings.
333 void _init_keyboard ();
337 * Initialize joystick bindings.
339 void _init_joystick ();
343 * Initialize mouse bindings.
349 * Initialize a single button.
351 inline void _init_button (const SGPropertyNode * node,
357 * Update the keyboard.
359 void _update_keyboard ();
363 * Update the joystick.
365 void _update_joystick ();
371 void _update_mouse ();
375 * Update a single button.
377 inline void _update_button (button &b, int modifiers, bool pressed,
382 * Read bindings and modifiers.
384 void _read_bindings (const SGPropertyNode * node,
385 binding_list_t * binding_list,
389 * Look up the bindings for a key code.
391 const vector<FGBinding *> &_find_key_bindings (unsigned int k,
394 button _key_bindings[MAX_KEYS];
395 joystick _joystick_bindings[MAX_JOYSTICKS];
396 mouse _mouse_bindings[MAX_MICE];
401 extern FGInput current_input;
405 ////////////////////////////////////////////////////////////////////////
407 ////////////////////////////////////////////////////////////////////////
409 // Handle GLUT events.
413 * Key-down event handler for Glut.
415 * <p>Pass the value on to the FGInput module unless PUI wants it.</p>
417 * @param k The integer value for the key pressed.
421 void GLUTkey (unsigned char k, int x, int y);
425 * Key-up event handler for GLUT.
427 * <p>PUI doesn't use this, so always pass it to the input manager.</p>
429 * @param k The integer value for the key pressed.
433 void GLUTkeyup (unsigned char k, int x, int y);
437 * Special key-down handler for Glut.
439 * <p>Pass the value on to the FGInput module unless PUI wants it.
440 * The key value will have 256 added to it.</p>
442 * @param k The integer value for the key pressed (will have 256 added
447 void GLUTspecialkey (int k, int x, int y);
451 * Special key-up handler for Glut.
453 * @param k The integer value for the key pressed (will have 256 added
458 void GLUTspecialkeyup (int k, int x, int y);
462 * Mouse click handler for Glut.
464 * @param button The mouse button pressed.
465 * @param updown Press or release flag.
466 * @param x The x-location of the click.
467 * @param y The y-location of the click.
469 void GLUTmouse (int button, int updown, int x, int y);
473 * Mouse motion handler for Glut.
475 * @param x The new x-location of the mouse.
476 * @param y The new y-location of the mouse.
478 void GLUTmotion (int x, int y);