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.
29 #include "FGMouseInput.hxx"
31 #include <boost/foreach.hpp>
32 #include <osgGA/GUIEventAdapter>
34 #include <simgear/scene/util/SGPickCallback.hxx>
35 #include <simgear/timing/timestamp.hxx>
37 #include "FGButton.hxx"
38 #include "Main/globals.hxx"
39 #include <Viewer/renderer.hxx>
41 #include <Model/panelnode.hxx>
42 #include <Cockpit/panel.hxx>
43 #include <Viewer/FGEventHandler.hxx>
44 #include <GUI/MouseCursor.hxx>
48 const int MAX_MICE = 1;
49 const int MAX_MOUSE_BUTTONS = 8;
51 ////////////////////////////////////////////////////////////////////////
54 * List of currently pressed mouse button events
56 class ActivePickCallbacks : public std::map<int, std::list<SGSharedPtr<SGPickCallback> > > {
58 void update( double dt );
59 void init( int button, const osgGA::GUIEventAdapter* ea );
63 void ActivePickCallbacks::init( int button, const osgGA::GUIEventAdapter* ea )
66 flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
68 // Get the list of hit callbacks. Take the first callback that
69 // accepts the mouse button press and ignore the rest of them
70 // That is they get sorted by distance and by scenegraph depth.
71 // The nearest one is the first one and the deepest
72 // (the most specialized one in the scenegraph) is the first.
73 std::vector<SGSceneryPick> pickList;
74 if (!globals->get_renderer()->pick(pickList, windowPos)) {
78 std::vector<SGSceneryPick>::const_iterator i;
79 for (i = pickList.begin(); i != pickList.end(); ++i) {
80 if (i->callback->buttonPressed(button, ea, i->info)) {
81 (*this)[button].push_back(i->callback);
87 void ActivePickCallbacks::update( double dt )
89 // handle repeatable mouse press events
90 for( iterator mi = begin(); mi != end(); ++mi ) {
91 std::list<SGSharedPtr<SGPickCallback> >::iterator li;
92 for (li = mi->second.begin(); li != mi->second.end(); ++li) {
98 ////////////////////////////////////////////////////////////////////////
102 * Settings for a mouse mode.
106 virtual ~mouse_mode ();
107 FGMouseCursor::Cursor cursor;
111 SGBindingList x_bindings[KEYMOD_MAX];
112 SGBindingList y_bindings[KEYMOD_MAX];
117 * Settings for a mouse.
123 SGPropertyNode_ptr mode_node;
124 SGPropertyNode_ptr mouse_button_nodes[MAX_MOUSE_BUTTONS];
128 SGTimeStamp timeSinceLastMove;
132 ////////////////////////////////////////////////////////////////////////
134 class FGMouseInput::FGMouseInputPrivate
137 FGMouseInputPrivate() :
139 xSizeNode(fgGetNode("/sim/startup/xsize", false ) ),
140 ySizeNode(fgGetNode("/sim/startup/ysize", false ) ),
141 xAccelNode(fgGetNode("/devices/status/mice/mouse/accel-x", true ) ),
142 yAccelNode(fgGetNode("/devices/status/mice/mouse/accel-y", true ) ),
143 hideCursorNode(fgGetNode("/sim/mouse/hide-cursor", true ) ),
144 cursorTimeoutNode(fgGetNode("/sim/mouse/cursor-timeout-sec", true ) ),
145 rightButtonModeCycleNode(fgGetNode("/sim/mouse/right-button-mode-cycle-enabled", true)),
146 tooltipShowDelayNode( fgGetNode("/sim/mouse/tooltip-delay-msec", true) ),
147 clickTriggersTooltipNode( fgGetNode("/sim/mouse/click-shows-tooltip", true) ),
148 mouseXNode(fgGetNode("/devices/status/mice/mouse/x", true)),
149 mouseYNode(fgGetNode("/devices/status/mice/mouse/y", true))
151 tooltipTimeoutDone = false;
154 void centerMouseCursor(mouse& m)
157 m.x = (xSizeNode ? xSizeNode->getIntValue() : 800) / 2;
158 m.y = (ySizeNode ? ySizeNode->getIntValue() : 600) / 2;
159 fgWarpMouse(m.x, m.y);
163 void constrainMouse(int x, int y)
166 int xsize = xSizeNode ? xSizeNode->getIntValue() : 800;
167 int ysize = ySizeNode ? ySizeNode->getIntValue() : 600;
169 bool need_warp = false;
170 if (x <= (xsize * .25) || x >= (xsize * .75)) {
171 new_x = int(xsize * .5);
175 if (y <= (ysize * .25) || y >= (ysize * .75)) {
176 new_y = int(ysize * .5);
182 fgWarpMouse(new_x, new_y);
187 void doHoverPick(const osg::Vec2d& windowPos)
189 std::vector<SGSceneryPick> pickList;
190 SGPickCallback::Priority priority = SGPickCallback::PriorityScenery;
192 if (globals->get_renderer()->pick(pickList, windowPos)) {
194 std::vector<SGSceneryPick>::const_iterator i;
195 for (i = pickList.begin(); i != pickList.end(); ++i) {
196 if (i->callback->hover(windowPos, i->info)) {
200 // if the callback is of higher prioirty (lower enum index),
202 if (i->callback->getPriority() < priority) {
203 priority = i->callback->getPriority();
206 } // of have valid pick
208 if (priority == SGPickCallback::PriorityPanel) {
209 FGMouseCursor::instance()->setCursor(FGMouseCursor::CURSOR_HAND);
211 // restore normal cursor
212 FGMouseCursor::instance()->setCursor(FGMouseCursor::CURSOR_ARROW);
220 SGPropertyNode_ptr args(new SGPropertyNode);
221 globals->get_commands()->execute("update-hover", args);
225 ActivePickCallbacks activePickCallbacks;
227 mouse mice[MAX_MICE];
230 bool tooltipTimeoutDone;
232 SGPropertyNode_ptr xSizeNode;
233 SGPropertyNode_ptr ySizeNode;
234 SGPropertyNode_ptr xAccelNode;
235 SGPropertyNode_ptr yAccelNode;
236 SGPropertyNode_ptr hideCursorNode;
237 SGPropertyNode_ptr cursorTimeoutNode;
238 SGPropertyNode_ptr rightButtonModeCycleNode;
239 SGPropertyNode_ptr tooltipShowDelayNode;
240 SGPropertyNode_ptr clickTriggersTooltipNode;
241 SGPropertyNode_ptr mouseXNode, mouseYNode;
245 ////////////////////////////////////////////////////////////////////////
246 // The Mouse Input Implementation
247 ////////////////////////////////////////////////////////////////////////
249 static FGMouseInput* global_mouseInput = NULL;
251 static void mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
253 if(global_mouseInput)
254 global_mouseInput->doMouseClick(button, updown, x, y, mainWindow, ea);
257 static void mouseMotionHandler(int x, int y, const osgGA::GUIEventAdapter* ea)
259 if (global_mouseInput != 0)
260 global_mouseInput->doMouseMotion(x, y, ea);
265 FGMouseInput::FGMouseInput() :
266 d(new FGMouseInputPrivate)
268 global_mouseInput = this;
271 FGMouseInput::~FGMouseInput()
273 global_mouseInput = NULL;
276 void FGMouseInput::init()
278 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
281 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
282 if (mouse_nodes == 0) {
283 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
284 mouse_nodes = fgGetNode("/input/mice", true);
288 for (int i = 0; i < MAX_MICE; i++) {
289 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
290 mouse &m = d->mice[i];
292 // Grab node pointers
293 std::ostringstream buf;
294 buf << "/devices/status/mice/mouse[" << i << "]/mode";
295 m.mode_node = fgGetNode(buf.str().c_str());
296 if (m.mode_node == NULL) {
297 m.mode_node = fgGetNode(buf.str().c_str(), true);
298 m.mode_node->setIntValue(0);
300 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
301 buf.seekp(ios_base::beg);
302 buf << "/devices/status/mice/mouse["<< i << "]/button[" << j << "]";
303 m.mouse_button_nodes[j] = fgGetNode(buf.str().c_str(), true);
304 m.mouse_button_nodes[j]->setBoolValue(false);
307 // Read all the modes
308 m.nModes = mouse_node->getIntValue("mode-count", 1);
309 m.modes = new mouse_mode[m.nModes];
311 for (int j = 0; j < m.nModes; j++) {
313 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
315 // Read the mouse cursor for this mode
316 m.modes[j].cursor = FGMouseCursor::cursorFromString(mode_node->getStringValue("cursor", "inherit"));
318 // Read other properties for this mode
319 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
320 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
322 // Read the button bindings for this mode
323 m.modes[j].buttons = new FGButton[MAX_MOUSE_BUTTONS];
324 std::ostringstream buf;
325 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
326 buf.seekp(ios_base::beg);
327 buf << "mouse button " << k;
328 m.modes[j].buttons[k].init( mode_node->getChild("button", k), buf.str(), module );
331 // Read the axis bindings for this mode
332 read_bindings(mode_node->getChild("x-axis", 0, true), m.modes[j].x_bindings, KEYMOD_NONE, module );
333 read_bindings(mode_node->getChild("y-axis", 0, true), m.modes[j].y_bindings, KEYMOD_NONE, module );
335 if (mode_node->hasChild("x-axis-ctrl")) {
336 read_bindings(mode_node->getChild("x-axis-ctrl"), m.modes[j].x_bindings, KEYMOD_CTRL, module );
339 if (mode_node->hasChild("y-axis-ctrl")) {
340 read_bindings(mode_node->getChild("y-axis-ctrl"), m.modes[j].y_bindings, KEYMOD_CTRL, module );
342 } // of modes iteration
345 fgRegisterMouseClickHandler(mouseClickHandler);
346 fgRegisterMouseMotionHandler(mouseMotionHandler);
349 void FGMouseInput::update ( double dt )
351 int cursorTimeoutMsec = d->cursorTimeoutNode->getDoubleValue() * 1000;
352 int tooltipDelayMsec = d->tooltipShowDelayNode->getIntValue();
354 mouse &m = d->mice[0];
355 int mode = m.mode_node->getIntValue();
356 if (mode != m.current_mode) {
357 // current mode has changed
358 m.current_mode = mode;
359 m.timeSinceLastMove.stamp();
361 if (mode >= 0 && mode < m.nModes) {
362 FGMouseCursor::instance()->setCursor(m.modes[mode].cursor);
363 d->centerMouseCursor(m);
365 SG_LOG(SG_INPUT, SG_WARN, "Mouse mode " << mode << " out of range");
366 FGMouseCursor::instance()->setCursor(FGMouseCursor::CURSOR_ARROW);
370 if ( d->hideCursorNode == NULL || d->hideCursorNode->getBoolValue() ) {
371 // if delay is <= 0, disable tooltips
372 if ( !d->tooltipTimeoutDone &&
373 (tooltipDelayMsec > 0) &&
374 (m.timeSinceLastMove.elapsedMSec() > tooltipDelayMsec))
376 d->tooltipTimeoutDone = true;
377 SGPropertyNode_ptr arg(new SGPropertyNode);
378 globals->get_commands()->execute("tooltip-timeout", arg);
381 if ( m.timeSinceLastMove.elapsedMSec() > cursorTimeoutMsec) {
382 FGMouseCursor::instance()->hideCursorUntilMouseMove();
383 m.timeSinceLastMove.stamp();
387 d->activePickCallbacks.update( dt );
404 mouse_mode::mouse_mode ()
405 : cursor(FGMouseCursor::CURSOR_ARROW),
412 mouse_mode::~mouse_mode ()
414 // FIXME: memory leak
415 // for (int i = 0; i < KEYMOD_MAX; i++) {
417 // for (j = 0; i < x_bindings[i].size(); j++)
418 // delete bindings[i][j];
419 // for (j = 0; j < y_bindings[i].size(); j++)
420 // delete bindings[i][j];
427 void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
429 int modifiers = fgGetKeyModifiers();
431 mouse &m = d->mice[0];
432 mouse_mode &mode = m.modes[m.current_mode];
433 // Let the property manager know.
434 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
435 m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
437 if (!d->rightButtonModeCycleNode->getBoolValue() && (b == 2)) {
438 // in spring-loaded look mode, ignore right clicks entirely here
442 // Pass on to PUI and the panel if
443 // requested, and return if one of
444 // them consumes the event.
446 if (updown != MOUSE_BUTTON_DOWN) {
447 // Execute the mouse up event in any case, may be we should
448 // stop processing here?
449 while (!d->activePickCallbacks[b].empty()) {
450 d->activePickCallbacks[b].front()->buttonReleased();
451 d->activePickCallbacks[b].pop_front();
455 if (mode.pass_through) {
456 // remove once PUI uses standard picking mechanism
457 if (0 <= x && 0 <= y && puMouse(b, updown, x, y))
460 // pui didn't want the click event so compute a
461 // scenegraph intersection point corresponding to the mouse click
462 if (updown == MOUSE_BUTTON_DOWN) {
463 d->activePickCallbacks.init( b, ea );
468 // OK, PUI and the panel didn't want the click
469 if (b >= MAX_MOUSE_BUTTONS) {
470 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
471 << " where only " << MAX_MOUSE_BUTTONS << " expected");
475 m.modes[m.current_mode].buttons[b].update( modifiers, 0 != updown, x, y);
477 if (d->clickTriggersTooltipNode->getBoolValue()) {
478 SGPropertyNode_ptr args(new SGPropertyNode);
479 args->setStringValue("reason", "click");
480 globals->get_commands()->execute("tooltip-timeout", args);
481 d->tooltipTimeoutDone = true;
485 void FGMouseInput::processMotion(int x, int y, const osgGA::GUIEventAdapter* ea)
487 if (!d->activePickCallbacks[0].empty()) {
488 //SG_LOG(SG_GENERAL, SG_INFO, "mouse-motion, have active pick callback");
489 BOOST_FOREACH(SGPickCallback* cb, d->activePickCallbacks[0]) {
495 mouse &m = d->mice[0];
496 int modeIndex = m.current_mode;
497 // are we in spring-loaded look mode?
498 if (!d->rightButtonModeCycleNode->getBoolValue()) {
499 if (m.mouse_button_nodes[2]->getBoolValue()) {
500 // right mouse is down, force look mode
505 if (modeIndex == 0) {
506 osg::Vec2d windowPos;
507 flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
508 d->doHoverPick(windowPos);
509 // mouse has moved, so we may need to issue tooltip-timeout command again
510 d->tooltipTimeoutDone = false;
513 mouse_mode &mode = m.modes[modeIndex];
515 // Pass on to PUI if requested, and return
516 // if PUI consumed the event.
517 if (mode.pass_through && puMouse(x, y)) {
523 // don't fire mouse-movement events at the first update after warping the mouse,
524 // just remember the new mouse position
525 d->haveWarped = false;
529 int modifiers = fgGetKeyModifiers();
530 int xsize = d->xSizeNode ? d->xSizeNode->getIntValue() : 800;
531 int ysize = d->ySizeNode ? d->ySizeNode->getIntValue() : 600;
533 // OK, PUI didn't want the event,
534 // so we can play with it.
537 d->xAccelNode->setIntValue( delta );
538 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
539 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
543 d->yAccelNode->setIntValue( -delta );
544 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
545 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
549 // Constrain the mouse if requested
550 if (mode.constrained) {
551 d->constrainMouse(x, y);
555 void FGMouseInput::doMouseMotion (int x, int y, const osgGA::GUIEventAdapter* ea)
557 mouse &m = d->mice[0];
559 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
565 m.timeSinceLastMove.stamp();
566 FGMouseCursor::instance()->mouseMoved();
568 processMotion(x, y, ea);
572 d->mouseXNode->setIntValue(x);
573 d->mouseYNode->setIntValue(y);