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/fg_props.hxx>
40 #include <Main/globals.hxx>
52 ////////////////////////////////////////////////////////////////////////
53 // General binding support.
54 ////////////////////////////////////////////////////////////////////////
58 * An input binding of some sort.
60 * <p>This class represents a binding that can be assigned to a
61 * keyboard key, a joystick button or axis, or even a panel
64 class FGBinding : public FGConditional
69 * Default constructor.
75 * Convenience constructor.
77 * @param node The binding will be built from this node.
79 FGBinding (const SGPropertyNode * node);
85 virtual ~FGBinding ();
89 * Get the command name.
91 * @return The string name of the command for this binding.
93 virtual const string &getCommandName () const { return _command_name; }
97 * Get the command itself.
99 * @return The command associated with this binding, or 0 if none
102 virtual SGCommandMgr::command_t getCommand () const { return _command; }
106 * Get the argument that will be passed to the command.
108 * @return A property node that will be passed to the command as its
109 * argument, or 0 if none was supplied.
111 virtual const SGPropertyNode * getArg () { return _arg; }
115 * Read a binding from a property node.
117 * @param node The property node containing the binding.
119 virtual void read (const SGPropertyNode * node);
125 virtual void fire () const;
129 * Fire a binding with a scaled movement (rather than absolute position).
131 virtual void fire (double offset, double max) const;
135 * Fire a binding with a setting (i.e. joystick axis).
137 * A double 'setting' property will be added to the arguments.
139 * @param setting The input setting, usually between -1.0 and 1.0.
141 virtual void fire (double setting) const;
146 FGBinding (const FGBinding &binding);
148 string _command_name;
149 mutable SGCommandMgr::command_t _command;
150 mutable SGPropertyNode * _arg;
151 mutable SGPropertyNode_ptr _setting;
156 ////////////////////////////////////////////////////////////////////////
157 // General input mapping support.
158 ////////////////////////////////////////////////////////////////////////
162 * Generic input module.
164 * <p>This module is designed to handle input from multiple sources --
165 * keyboard, joystick, mouse, or even panel switches -- in a consistent
166 * way, and to allow users to rebind any of the actions at runtime.</p>
168 class FGInput : public FGSubsystem
174 FG_MOD_UP = 1, // key- or button-up
178 FG_MOD_MAX = 16 // enough to handle all combinations
183 * Default constructor.
194 // Implementation of FGSubsystem.
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 * <p>Note: for special keys, the integer key code will be the Glut
221 * @param k The integer key code, as returned by glut.
222 * @param modifiers Modifier keys pressed (bitfield).
223 * @param x The mouse x position at the time of keypress.
224 * @param y The mouse y position at the time of keypress.
229 virtual void doKey (int k, int modifiers, int x, int y);
233 * Handle a mouse click event.
235 * @param button The mouse button selected.
236 * @param updown Button status.
237 * @param x The X position of the mouse event, in screen coordinates.
238 * @param y The Y position of the mouse event, in screen coordinates.
240 virtual void doMouseClick (int button, int updown, int x, int y);
244 * Handle mouse motion.
246 * @param x The new mouse x position, in screen coordinates.
247 * @param y The new mouse y position, in screen coordinates.
249 virtual void doMouseMotion (int x, int y);
263 MAX_JOYSTICK_AXES = _JS_MAX_AXES,
264 MAX_JOYSTICK_BUTTONS = 32,
267 MAX_MOUSE_BUTTONS = 8
272 typedef vector<FGBinding *> binding_list_t;
275 * Settings for a key or button.
282 binding_list_t bindings[FG_MOD_MAX];
287 * Settings for a single joystick axis.
294 binding_list_t bindings[FG_MOD_MAX];
296 float high_threshold;
303 * Settings for a joystick.
307 virtual ~joystick ();
318 * Settings for a mouse mode.
322 virtual ~mouse_mode ();
327 binding_list_t x_bindings[FG_MOD_MAX];
328 binding_list_t y_bindings[FG_MOD_MAX];
333 * Settings for a mouse.
340 SGPropertyNode * mode_node;
341 SGPropertyNode * mouse_button_nodes[MAX_MOUSE_BUTTONS];
349 * Initialize key bindings.
351 void _init_keyboard ();
355 * Initialize joystick bindings.
357 void _init_joystick ();
361 * Initialize mouse bindings.
367 * Initialize a single button.
369 inline void _init_button (const SGPropertyNode * node,
375 * Update the keyboard.
377 void _update_keyboard ();
381 * Update the joystick.
383 void _update_joystick ();
389 void _update_mouse ();
393 * Update a single button.
395 inline void _update_button (button &b, int modifiers, bool pressed,
400 * Read bindings and modifiers.
402 void _read_bindings (const SGPropertyNode * node,
403 binding_list_t * binding_list,
407 * Look up the bindings for a key code.
409 const vector<FGBinding *> &_find_key_bindings (unsigned int k,
412 button _key_bindings[MAX_KEYS];
413 joystick _joystick_bindings[MAX_JOYSTICKS];
414 mouse _mouse_bindings[MAX_MICE];
420 ////////////////////////////////////////////////////////////////////////
422 ////////////////////////////////////////////////////////////////////////
424 // Handle GLUT events.
428 * Key-down event handler for Glut.
430 * <p>Pass the value on to the FGInput module unless PUI wants it.</p>
432 * @param k The integer value for the key pressed.
436 void GLUTkey (unsigned char k, int x, int y);
440 * Key-up event handler for GLUT.
442 * <p>PUI doesn't use this, so always pass it to the input manager.</p>
444 * @param k The integer value for the key pressed.
448 void GLUTkeyup (unsigned char k, int x, int y);
452 * Special key-down handler for Glut.
454 * <p>Pass the value on to the FGInput module unless PUI wants it.
455 * The key value will have 256 added to it.</p>
457 * @param k The integer value for the key pressed (will have 256 added
462 void GLUTspecialkey (int k, int x, int y);
466 * Special key-up handler for Glut.
468 * @param k The integer value for the key pressed (will have 256 added
473 void GLUTspecialkeyup (int k, int x, int y);
477 * Mouse click handler for Glut.
479 * @param button The mouse button pressed.
480 * @param updown Press or release flag.
481 * @param x The x-location of the click.
482 * @param y The y-location of the click.
484 void GLUTmouse (int button, int updown, int x, int y);
488 * Mouse motion handler for Glut.
490 * @param x The new x-location of the mouse.
491 * @param y The new y-location of the mouse.
493 void GLUTmotion (int x, int y);