1 // FGMouseInput.cxx -- handle user input from mouse devices
3 // Written by Torsten Dreyer, started August 2009
4 // Based on work from David Megginson, started May 2001.
6 // Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
7 // Copyright (C) 2001 David Megginson, david@megginson.com
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "FGMouseInput.hxx"
28 void ActivePickCallbacks::init( int b, const osgGA::GUIEventAdapter* ea )
30 // Get the list of hit callbacks. Take the first callback that
31 // accepts the mouse button press and ignore the rest of them
32 // That is they get sorted by distance and by scenegraph depth.
33 // The nearest one is the first one and the deepest
34 // (the most specialized one in the scenegraph) is the first.
35 std::vector<SGSceneryPick> pickList;
36 if (FGRenderer::pick(pickList, ea)) {
37 std::vector<SGSceneryPick>::const_iterator i;
38 for (i = pickList.begin(); i != pickList.end(); ++i) {
39 if (i->callback->buttonPressed(b, i->info)) {
40 (*this)[b].push_back(i->callback);
47 void ActivePickCallbacks::update( double dt )
49 // handle repeatable mouse press events
50 for( iterator mi = begin(); mi != end(); ++mi ) {
51 std::list<SGSharedPtr<SGPickCallback> >::iterator li;
52 for (li = mi->second.begin(); li != mi->second.end(); ++li) {
60 #include <Model/panelnode.hxx>
61 #include <Cockpit/panel.hxx>
62 ////////////////////////////////////////////////////////////////////////
63 // The Mouse Input Implementation
64 ////////////////////////////////////////////////////////////////////////
66 const FGMouseInput::MouseCursorMap FGMouseInput::mouse_cursor_map[] = {
67 { "none", MOUSE_CURSOR_NONE },
68 { "inherit", MOUSE_CURSOR_POINTER },
69 { "wait", MOUSE_CURSOR_WAIT },
70 { "crosshair", MOUSE_CURSOR_CROSSHAIR },
71 { "left-right", MOUSE_CURSOR_LEFTRIGHT },
75 FGMouseInput * FGMouseInput::mouseInput = NULL;
77 FGMouseInput::FGMouseInput() :
78 xSizeNode(fgGetNode("/sim/startup/xsize", false ) ),
79 ySizeNode(fgGetNode("/sim/startup/ysize", false ) ),
80 xAccelNode(fgGetNode("/devices/status/mice/mouse/accel-x", true ) ),
81 yAccelNode(fgGetNode("/devices/status/mice/mouse/accel-y", true ) ),
82 hideCursorNode(fgGetNode("/sim/mouse/hide-cursor", true ) ),
83 cursorTimeoutNode(fgGetNode("/sim/mouse/cursor-timeout-sec", true ) )
85 if( mouseInput == NULL )
89 FGMouseInput::~FGMouseInput()
91 if( mouseInput == this )
95 void FGMouseInput::init()
97 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
100 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
101 if (mouse_nodes == 0) {
102 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
103 mouse_nodes = fgGetNode("/input/mice", true);
107 for (int i = 0; i < MAX_MICE; i++) {
108 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
109 mouse &m = bindings[i];
111 // Grab node pointers
112 std::ostringstream buf;
113 buf << "/devices/status/mice/mouse[" << i << "]/mode";
114 m.mode_node = fgGetNode(buf.str().c_str());
115 if (m.mode_node == NULL) {
116 m.mode_node = fgGetNode(buf.str().c_str(), true);
117 m.mode_node->setIntValue(0);
119 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
121 buf << "/devices/status/mice/mouse["<< i << "]/button[" << j << "]";
122 m.mouse_button_nodes[j] = fgGetNode(buf.str().c_str(), true);
123 m.mouse_button_nodes[j]->setBoolValue(false);
126 // Read all the modes
127 m.nModes = mouse_node->getIntValue("mode-count", 1);
128 m.modes = new mouse_mode[m.nModes];
130 for (int j = 0; j < m.nModes; j++) {
133 // Read the mouse cursor for this mode
134 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
135 const char * cursor_name =
136 mode_node->getStringValue("cursor", "inherit");
137 m.modes[j].cursor = MOUSE_CURSOR_POINTER;
138 for (k = 0; mouse_cursor_map[k].name != 0; k++) {
139 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
140 m.modes[j].cursor = mouse_cursor_map[k].cursor;
145 // Read other properties for this mode
146 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
147 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
149 // Read the button bindings for this mode
150 m.modes[j].buttons = new FGButton[MAX_MOUSE_BUTTONS];
151 std::ostringstream buf;
152 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
154 buf << "mouse button " << k;
155 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
156 m.modes[j].buttons[k].init( mode_node->getChild("button", k), buf.str(), module );
159 // Read the axis bindings for this mode
160 read_bindings(mode_node->getChild("x-axis", 0, true), m.modes[j].x_bindings, KEYMOD_NONE, module );
161 read_bindings(mode_node->getChild("y-axis", 0, true), m.modes[j].y_bindings, KEYMOD_NONE, module );
165 fgRegisterMouseClickHandler(mouseClickHandler);
166 fgRegisterMouseMotionHandler(mouseMotionHandler);
169 void FGMouseInput::update ( double dt )
171 double cursorTimeout = cursorTimeoutNode ? cursorTimeoutNode->getDoubleValue() : 10.0;
173 mouse &m = bindings[0];
174 int mode = m.mode_node->getIntValue();
175 if (mode != m.current_mode) {
176 m.current_mode = mode;
177 m.timeout = cursorTimeout;
178 if (mode >= 0 && mode < m.nModes) {
179 fgSetMouseCursor(m.modes[mode].cursor);
180 m.x = (xSizeNode ? xSizeNode->getIntValue() : 800) / 2;
181 m.y = (ySizeNode ? ySizeNode->getIntValue() : 600) / 2;
182 fgWarpMouse(m.x, m.y);
184 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
185 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
189 if ( hideCursorNode ==NULL || hideCursorNode->getBoolValue() ) {
190 if ( m.x != m.save_x || m.y != m.save_y ) {
191 m.timeout = cursorTimeout;
192 if (fgGetMouseCursor() == MOUSE_CURSOR_NONE)
193 fgSetMouseCursor(m.modes[mode].cursor);
196 if ( m.timeout <= 0.0 ) {
197 fgSetMouseCursor(MOUSE_CURSOR_NONE);
205 activePickCallbacks.update( dt );
208 FGMouseInput::mouse::mouse ()
220 FGMouseInput::mouse::~mouse ()
225 FGMouseInput::mouse_mode::mouse_mode ()
226 : cursor(MOUSE_CURSOR_POINTER),
233 FGMouseInput::mouse_mode::~mouse_mode ()
235 // FIXME: memory leak
236 // for (int i = 0; i < KEYMOD_MAX; i++) {
238 // for (j = 0; i < x_bindings[i].size(); j++)
239 // delete bindings[i][j];
240 // for (j = 0; j < y_bindings[i].size(); j++)
241 // delete bindings[i][j];
246 void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
248 int modifiers = fgGetKeyModifiers();
250 mouse &m = bindings[0];
251 mouse_mode &mode = m.modes[m.current_mode];
253 // Let the property manager know.
254 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
255 m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
257 // Pass on to PUI and the panel if
258 // requested, and return if one of
259 // them consumes the event.
261 if (updown != MOUSE_BUTTON_DOWN) {
262 // Execute the mouse up event in any case, may be we should
263 // stop processing here?
264 while (!activePickCallbacks[b].empty()) {
265 activePickCallbacks[b].front()->buttonReleased();
266 activePickCallbacks[b].pop_front();
270 if (mode.pass_through) {
271 if (0 <= x && 0 <= y && puMouse(b, updown, x, y))
273 else if (0 <= x && 0 <= y && (globals->get_current_panel() != 0) &&
274 globals->get_current_panel()->getVisibility() &&
275 globals->get_current_panel()->doMouseAction(b, updown, x, y))
277 else if (0 <= x && 0 <= y && fgHandle3DPanelMouseEvent(b, updown, x, y))
280 // pui didn't want the click event so compute a
281 // scenegraph intersection point corresponding to the mouse click
282 if (updown == MOUSE_BUTTON_DOWN) {
283 activePickCallbacks.init( b, ea );
288 // OK, PUI and the panel didn't want the click
289 if (b >= MAX_MOUSE_BUTTONS) {
290 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
291 << " where only " << MAX_MOUSE_BUTTONS << " expected");
295 m.modes[m.current_mode].buttons[b].update( modifiers, 0 != updown, x, y);
298 void FGMouseInput::doMouseMotion (int x, int y)
300 // Don't call fgGetKeyModifiers() here, until we are using a
301 // toolkit that supports getting the mods from outside a key
302 // callback. Glut doesn't.
303 int modifiers = KEYMOD_NONE;
305 int xsize = xSizeNode ? xSizeNode->getIntValue() : 800;
306 int ysize = ySizeNode ? ySizeNode->getIntValue() : 600;
308 mouse &m = bindings[0];
310 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
315 mouse_mode &mode = m.modes[m.current_mode];
317 // Pass on to PUI if requested, and return
318 // if PUI consumed the event.
319 if (mode.pass_through && puMouse(x, y)) {
325 // OK, PUI didn't want the event,
326 // so we can play with it.
329 xAccelNode->setIntValue( delta );
330 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
331 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
335 yAccelNode->setIntValue( -delta );
336 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
337 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
340 // Constrain the mouse if requested
341 if (mode.constrained) {
342 bool need_warp = false;
343 if (x <= (xsize * .25) || x >= (xsize * .75)) {
348 if (y <= (ysize * .25) || y >= (ysize * .75)) {
358 fgSetInt("/devices/status/mice/mouse/x", m.x = x);
361 fgSetInt("/devices/status/mice/mouse/y", m.y = y);
364 void FGMouseInput::mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
367 mouseInput->doMouseClick(button, updown, x, y, mainWindow, ea);
370 void FGMouseInput::mouseMotionHandler(int x, int y)
373 mouseInput->doMouseMotion(x, y);