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>
36 #include <simgear/scene/model/SGPickAnimation.hxx>
38 #include "FGButton.hxx"
39 #include "Main/globals.hxx"
40 #include <Viewer/renderer.hxx>
42 #include <Model/panelnode.hxx>
43 #include <Cockpit/panel.hxx>
44 #include <Viewer/FGEventHandler.hxx>
45 #include <GUI/MouseCursor.hxx>
49 const int MAX_MICE = 1;
50 const int MAX_MOUSE_BUTTONS = 8;
52 typedef std::vector<SGSceneryPick> SGSceneryPicks;
53 typedef SGSharedPtr<SGPickCallback> SGPickCallbackPtr;
54 typedef std::list<SGPickCallbackPtr> SGPickCallbackList;
56 ////////////////////////////////////////////////////////////////////////
59 * List of currently pressed mouse button events
61 class ActivePickCallbacks:
62 public std::map<int, SGPickCallbackList>
65 void update( double dt, unsigned int keyModState );
66 void init( int button, const osgGA::GUIEventAdapter* ea );
70 void ActivePickCallbacks::init( int button, const osgGA::GUIEventAdapter* ea )
73 flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
75 // Get the list of hit callbacks. Take the first callback that
76 // accepts the mouse button press and ignore the rest of them
77 // That is they get sorted by distance and by scenegraph depth.
78 // The nearest one is the first one and the deepest
79 // (the most specialized one in the scenegraph) is the first.
80 SGSceneryPicks pickList;
81 if (!globals->get_renderer()->pick(pickList, windowPos)) {
85 SGSceneryPicks::const_iterator i;
86 for (i = pickList.begin(); i != pickList.end(); ++i) {
87 if (i->callback->buttonPressed(button, *ea, i->info)) {
88 (*this)[button].push_back(i->callback);
94 void ActivePickCallbacks::update( double dt, unsigned int keyModState )
96 // handle repeatable mouse press events
97 for( iterator mi = begin(); mi != end(); ++mi ) {
98 SGPickCallbackList::iterator li;
99 for (li = mi->second.begin(); li != mi->second.end(); ++li) {
100 (*li)->update(dt, keyModState);
105 ////////////////////////////////////////////////////////////////////////
109 * Settings for a mouse mode.
113 virtual ~mouse_mode ();
114 FGMouseCursor::Cursor cursor;
118 SGBindingList x_bindings[KEYMOD_MAX];
119 SGBindingList y_bindings[KEYMOD_MAX];
124 * Settings for a mouse.
130 SGPropertyNode_ptr mode_node;
131 SGPropertyNode_ptr mouse_button_nodes[MAX_MOUSE_BUTTONS];
135 SGTimeStamp timeSinceLastMove;
141 getPick( const SGSceneryPicks& pick_list,
142 const SGPickCallback* cb )
144 for(size_t i = 0; i < pick_list.size(); ++i)
145 if( pick_list[i].callback == cb )
146 return &pick_list[i];
151 ////////////////////////////////////////////////////////////////////////
153 class FGMouseInput::FGMouseInputPrivate : public SGPropertyChangeListener
156 FGMouseInputPrivate() :
159 xSizeNode(fgGetNode("/sim/startup/xsize", false ) ),
160 ySizeNode(fgGetNode("/sim/startup/ysize", false ) ),
161 xAccelNode(fgGetNode("/devices/status/mice/mouse/accel-x", true ) ),
162 yAccelNode(fgGetNode("/devices/status/mice/mouse/accel-y", true ) ),
163 mouseXNode(fgGetNode("/devices/status/mice/mouse/x", true)),
164 mouseYNode(fgGetNode("/devices/status/mice/mouse/y", true))
166 tooltipTimeoutDone = false;
167 hoverPickScheduled = false;
168 tooltipsEnabled = false;
170 fgGetNode("/sim/mouse/hide-cursor", true )->addChangeListener(this, true);
171 fgGetNode("/sim/mouse/cursor-timeout-sec", true )->addChangeListener(this, true);
172 fgGetNode("/sim/mouse/right-button-mode-cycle-enabled", true)->addChangeListener(this, true);
173 fgGetNode("/sim/mouse/tooltip-delay-msec", true)->addChangeListener(this, true);
174 fgGetNode("/sim/mouse/click-shows-tooltip", true)->addChangeListener(this, true);
175 fgGetNode("/sim/mouse/tooltips-enabled", true)->addChangeListener(this, true);
176 fgGetNode("/sim/mouse/drag-sensitivity", true)->addChangeListener(this, true);
177 fgGetNode("/sim/mouse/invert-mouse-wheel", true)->addChangeListener(this, true);
180 void centerMouseCursor(mouse& m)
183 m.x = (xSizeNode ? xSizeNode->getIntValue() : 800) / 2;
184 m.y = (ySizeNode ? ySizeNode->getIntValue() : 600) / 2;
185 fgWarpMouse(m.x, m.y);
189 void constrainMouse(int x, int y)
192 int xsize = xSizeNode ? xSizeNode->getIntValue() : 800;
193 int ysize = ySizeNode ? ySizeNode->getIntValue() : 600;
195 bool need_warp = false;
196 if (x <= (xsize * .25) || x >= (xsize * .75)) {
197 new_x = int(xsize * .5);
201 if (y <= (ysize * .25) || y >= (ysize * .75)) {
202 new_y = int(ysize * .5);
208 fgWarpMouse(new_x, new_y);
213 void scheduleHoverPick(const osg::Vec2d& windowPos)
215 hoverPickScheduled = true;
216 hoverPos = windowPos;
219 void doHoverPick(const osg::Vec2d& windowPos)
221 FGMouseCursor::Cursor cur = FGMouseCursor::CURSOR_ARROW;
222 bool explicitCursor = false;
223 bool didPick = false;
225 SGPickCallback::Priority priority = SGPickCallback::PriorityScenery;
226 SGSceneryPicks pickList;
227 globals->get_renderer()->pick(pickList, windowPos);
229 SGSceneryPicks::const_iterator i;
230 for( i = pickList.begin(); i != pickList.end(); ++i )
232 bool done = i->callback->hover(windowPos, i->info);
233 std::string curName(i->callback->getCursor());
234 if (!curName.empty()) {
235 explicitCursor = true;
236 cur = FGMouseCursor::cursorFromString(curName.c_str());
239 // if the callback is of higher prioirty (lower enum index),
241 if (i->callback->getPriority() < priority) {
242 priority = i->callback->getPriority();
249 } // of picks iteration
251 // Check if any pick from the previous iteration has disappeared. If so
252 // notify the callback that the mouse has left its element.
253 for( i = _previous_picks.begin(); i != _previous_picks.end(); ++i )
255 if( !getPick(pickList, i->callback) )
256 i->callback->mouseLeave(windowPos);
258 _previous_picks = pickList;
260 if (!explicitCursor && (priority == SGPickCallback::PriorityPanel)) {
261 cur = FGMouseCursor::CURSOR_HAND;
264 FGMouseCursor::instance()->setCursor(cur);
266 SGPropertyNode_ptr args(new SGPropertyNode);
267 globals->get_commands()->execute("update-hover", args);
272 void doMouseMoveWithCallbacks(const osgGA::GUIEventAdapter* ea)
274 FGMouseCursor::Cursor cur = FGMouseCursor::CURSOR_CLOSED_HAND;
276 osg::Vec2d windowPos;
277 flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
279 SGSceneryPicks pickList;
280 if( !globals->get_renderer()->pick(pickList, windowPos) )
283 for( ActivePickCallbacks::iterator mi = activePickCallbacks.begin();
284 mi != activePickCallbacks.end();
287 SGPickCallbackList::iterator li;
288 for( li = mi->second.begin(); li != mi->second.end(); ++li )
290 const SGSceneryPick* pick = getPick(pickList, *li);
291 (*li)->mouseMoved(*ea, pick ? &pick->info : 0);
293 std::string curName((*li)->getCursor());
294 if( !curName.empty() )
295 cur = FGMouseCursor::cursorFromString(curName.c_str());
299 FGMouseCursor::instance()->setCursor(cur);
302 // implement the property-change-listener interfacee
303 virtual void valueChanged( SGPropertyNode * node )
305 if (node->getNameString() == "drag-sensitivity") {
306 SGKnobAnimation::setDragSensitivity(node->getDoubleValue());
307 } else if (node->getNameString() == "invert-mouse-wheel") {
308 SGKnobAnimation::setAlternateMouseWheelDirection(node->getBoolValue());
309 } else if (node->getNameString() == "hide-cursor") {
310 hideCursor = node->getBoolValue();
311 } else if (node->getNameString() == "cursor-timeout-sec") {
312 cursorTimeoutMsec = node->getDoubleValue() * 1000;
313 } else if (node->getNameString() == "tooltip-delay-msec") {
314 tooltipDelayMsec = node->getIntValue();
315 } else if (node->getNameString() == "right-button-mode-cycle-enabled") {
316 rightClickModeCycle = node->getBoolValue();
317 } else if (node->getNameString() == "click-shows-tooltip") {
318 clickTriggersTooltip = node->getBoolValue();
319 } else if (node->getNameString() == "tooltips-enabled") {
320 tooltipsEnabled = node->getBoolValue();
324 ActivePickCallbacks activePickCallbacks;
325 SGSceneryPicks _previous_picks;
327 mouse mice[MAX_MICE];
330 bool hideCursor, haveWarped;
331 bool tooltipTimeoutDone;
332 bool clickTriggersTooltip;
333 int tooltipDelayMsec, cursorTimeoutMsec;
334 bool rightClickModeCycle;
335 bool tooltipsEnabled;
337 SGPropertyNode_ptr xSizeNode;
338 SGPropertyNode_ptr ySizeNode;
339 SGPropertyNode_ptr xAccelNode;
340 SGPropertyNode_ptr yAccelNode;
341 SGPropertyNode_ptr mouseXNode, mouseYNode;
343 bool hoverPickScheduled;
348 ////////////////////////////////////////////////////////////////////////
349 // The Mouse Input Implementation
350 ////////////////////////////////////////////////////////////////////////
352 static FGMouseInput* global_mouseInput = NULL;
354 static void mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
356 if(global_mouseInput)
357 global_mouseInput->doMouseClick(button, updown, x, y, mainWindow, ea);
360 static void mouseMotionHandler(int x, int y, const osgGA::GUIEventAdapter* ea)
362 if (global_mouseInput != 0)
363 global_mouseInput->doMouseMotion(x, y, ea);
368 FGMouseInput::FGMouseInput() :
369 d(new FGMouseInputPrivate)
371 global_mouseInput = this;
374 FGMouseInput::~FGMouseInput()
376 global_mouseInput = NULL;
379 void FGMouseInput::init()
381 if (d->initialized) {
382 SG_LOG(SG_INPUT, SG_WARN, "Duplicate init of FGMouseInput");
387 d->initialized = true;
389 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
390 std::string module = "";
392 SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
393 if (mouse_nodes == 0) {
394 SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
395 mouse_nodes = fgGetNode("/input/mice", true);
399 for (int i = 0; i < MAX_MICE; i++) {
400 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
401 mouse &m = d->mice[i];
403 // Grab node pointers
404 std::ostringstream buf;
405 buf << "/devices/status/mice/mouse[" << i << "]/mode";
406 m.mode_node = fgGetNode(buf.str().c_str());
407 if (m.mode_node == NULL) {
408 m.mode_node = fgGetNode(buf.str().c_str(), true);
409 m.mode_node->setIntValue(0);
411 for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
412 buf.seekp(ios_base::beg);
413 buf << "/devices/status/mice/mouse["<< i << "]/button[" << j << "]";
414 m.mouse_button_nodes[j] = fgGetNode(buf.str().c_str(), true);
415 m.mouse_button_nodes[j]->setBoolValue(false);
418 // Read all the modes
419 m.nModes = mouse_node->getIntValue("mode-count", 1);
420 m.modes = new mouse_mode[m.nModes];
422 for (int j = 0; j < m.nModes; j++) {
424 SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
426 // Read the mouse cursor for this mode
427 m.modes[j].cursor = FGMouseCursor::cursorFromString(mode_node->getStringValue("cursor", "inherit"));
429 // Read other properties for this mode
430 m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
431 m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
433 // Read the button bindings for this mode
434 m.modes[j].buttons = new FGButton[MAX_MOUSE_BUTTONS];
435 std::ostringstream buf;
436 for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
437 buf.seekp(ios_base::beg);
438 buf << "mouse button " << k;
439 m.modes[j].buttons[k].init( mode_node->getChild("button", k), buf.str(), module );
442 // Read the axis bindings for this mode
443 read_bindings(mode_node->getChild("x-axis", 0, true), m.modes[j].x_bindings, KEYMOD_NONE, module );
444 read_bindings(mode_node->getChild("y-axis", 0, true), m.modes[j].y_bindings, KEYMOD_NONE, module );
446 if (mode_node->hasChild("x-axis-ctrl")) {
447 read_bindings(mode_node->getChild("x-axis-ctrl"), m.modes[j].x_bindings, KEYMOD_CTRL, module );
449 if (mode_node->hasChild("x-axis-shift")) {
450 read_bindings(mode_node->getChild("x-axis-shift"), m.modes[j].x_bindings, KEYMOD_SHIFT, module );
452 if (mode_node->hasChild("x-axis-ctrl-shift")) {
453 read_bindings(mode_node->getChild("x-axis-ctrl-shift"), m.modes[j].x_bindings, KEYMOD_CTRL|KEYMOD_SHIFT, module );
456 if (mode_node->hasChild("y-axis-ctrl")) {
457 read_bindings(mode_node->getChild("y-axis-ctrl"), m.modes[j].y_bindings, KEYMOD_CTRL, module );
459 if (mode_node->hasChild("y-axis-shift")) {
460 read_bindings(mode_node->getChild("y-axis-shift"), m.modes[j].y_bindings, KEYMOD_SHIFT, module );
462 if (mode_node->hasChild("y-axis-ctrl-shift")) {
463 read_bindings(mode_node->getChild("y-axis-ctrl-shift"), m.modes[j].y_bindings, KEYMOD_CTRL|KEYMOD_SHIFT, module );
465 } // of modes iteration
468 fgRegisterMouseClickHandler(mouseClickHandler);
469 fgRegisterMouseMotionHandler(mouseMotionHandler);
472 void FGMouseInput::update ( double dt )
474 if (!d->initialized) {
475 SG_LOG(SG_INPUT, SG_WARN, "update of mouse before init");
478 mouse &m = d->mice[0];
479 int mode = m.mode_node->getIntValue();
480 if (mode != m.current_mode) {
481 // current mode has changed
482 m.current_mode = mode;
483 m.timeSinceLastMove.stamp();
485 if (mode >= 0 && mode < m.nModes) {
486 FGMouseCursor::instance()->setCursor(m.modes[mode].cursor);
487 d->centerMouseCursor(m);
489 SG_LOG(SG_INPUT, SG_WARN, "Mouse mode " << mode << " out of range");
490 FGMouseCursor::instance()->setCursor(FGMouseCursor::CURSOR_ARROW);
494 if ((mode == 0) && d->hoverPickScheduled) {
495 d->doHoverPick(d->hoverPos);
496 d->hoverPickScheduled = false;
499 if ( !d->tooltipTimeoutDone &&
500 d->tooltipsEnabled &&
501 (m.timeSinceLastMove.elapsedMSec() > d->tooltipDelayMsec))
503 d->tooltipTimeoutDone = true;
504 SGPropertyNode_ptr arg(new SGPropertyNode);
505 globals->get_commands()->execute("tooltip-timeout", arg);
508 if ( d->hideCursor ) {
509 if ( m.timeSinceLastMove.elapsedMSec() > d->cursorTimeoutMsec) {
510 FGMouseCursor::instance()->hideCursorUntilMouseMove();
511 m.timeSinceLastMove.stamp();
515 d->activePickCallbacks.update( dt, fgGetKeyModifiers() );
532 mouse_mode::mouse_mode ()
533 : cursor(FGMouseCursor::CURSOR_ARROW),
540 mouse_mode::~mouse_mode ()
542 // FIXME: memory leak
543 // for (int i = 0; i < KEYMOD_MAX; i++) {
545 // for (j = 0; i < x_bindings[i].size(); j++)
546 // delete bindings[i][j];
547 // for (j = 0; j < y_bindings[i].size(); j++)
548 // delete bindings[i][j];
555 void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
557 if (!d->initialized) {
558 // can occur during reset
562 int modifiers = fgGetKeyModifiers();
564 mouse &m = d->mice[0];
565 mouse_mode &mode = m.modes[m.current_mode];
566 // Let the property manager know.
567 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
568 m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
570 if (!d->rightClickModeCycle && (b == 2)) {
571 // in spring-loaded look mode, ignore right clicks entirely here
575 // Pass on to PUI and the panel if
576 // requested, and return if one of
577 // them consumes the event.
579 osg::Vec2d windowPos;
580 flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
582 SGSceneryPicks pickList;
583 globals->get_renderer()->pick(pickList, windowPos);
585 if( updown != MOUSE_BUTTON_DOWN )
587 // Execute the mouse up event in any case, may be we should
588 // stop processing here?
590 SGPickCallbackList& callbacks = d->activePickCallbacks[b];
592 while( !callbacks.empty() )
594 SGPickCallbackPtr& cb = callbacks.front();
595 const SGSceneryPick* pick = getPick(pickList, cb);
596 cb->buttonReleased(ea->getModKeyMask(), *ea, pick ? &pick->info : 0);
598 callbacks.pop_front();
602 if (mode.pass_through) {
603 // remove once PUI uses standard picking mechanism
604 if (0 <= x && 0 <= y && puMouse(b, updown, x, y))
605 return; // pui handled it
607 // pui didn't want the click event so compute a
608 // scenegraph intersection point corresponding to the mouse click
609 if (updown == MOUSE_BUTTON_DOWN) {
610 d->activePickCallbacks.init( b, ea );
612 if (d->clickTriggersTooltip) {
613 SGPropertyNode_ptr args(new SGPropertyNode);
614 args->setStringValue("reason", "click");
615 globals->get_commands()->execute("tooltip-timeout", args);
616 d->tooltipTimeoutDone = true;
619 // do a hover pick now, to fix up cursor
620 d->doHoverPick(windowPos);
621 } // mouse button was released
622 } // of pass-through mode
624 // OK, PUI and the panel didn't want the click
625 if (b >= MAX_MOUSE_BUTTONS) {
626 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
627 << " where only " << MAX_MOUSE_BUTTONS << " expected");
631 m.modes[m.current_mode].buttons[b].update( modifiers, 0 != updown, x, y);
634 void FGMouseInput::processMotion(int x, int y, const osgGA::GUIEventAdapter* ea)
636 if (!d->activePickCallbacks[0].empty()) {
637 d->doMouseMoveWithCallbacks(ea);
641 mouse &m = d->mice[0];
642 int modeIndex = m.current_mode;
643 // are we in spring-loaded look mode?
644 if (!d->rightClickModeCycle && m.nModes > 3) {
645 if (m.mouse_button_nodes[2]->getBoolValue()) {
646 // right mouse is down, force look mode
651 if (modeIndex == 0) {
652 osg::Vec2d windowPos;
653 flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
654 d->scheduleHoverPick(windowPos);
655 // mouse has moved, so we may need to issue tooltip-timeout command again
656 d->tooltipTimeoutDone = false;
659 mouse_mode &mode = m.modes[modeIndex];
661 // Pass on to PUI if requested, and return
662 // if PUI consumed the event.
663 if (mode.pass_through && puMouse(x, y)) {
669 // don't fire mouse-movement events at the first update after warping the mouse,
670 // just remember the new mouse position
671 d->haveWarped = false;
675 int modifiers = fgGetKeyModifiers();
676 int xsize = d->xSizeNode ? d->xSizeNode->getIntValue() : 800;
677 int ysize = d->ySizeNode ? d->ySizeNode->getIntValue() : 600;
679 // OK, PUI didn't want the event,
680 // so we can play with it.
683 d->xAccelNode->setIntValue( delta );
684 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
685 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
689 d->yAccelNode->setIntValue( -delta );
690 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
691 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
695 // Constrain the mouse if requested
696 if (mode.constrained) {
697 d->constrainMouse(x, y);
701 void FGMouseInput::doMouseMotion (int x, int y, const osgGA::GUIEventAdapter* ea)
703 if (!d->initialized) {
704 // can occur during reset
708 mouse &m = d->mice[0];
710 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
716 m.timeSinceLastMove.stamp();
717 FGMouseCursor::instance()->mouseMoved();
719 // TODO Get rid of this as soon as soon as cursor hide timeout works globally
720 if( ea->getHandled() )
723 processMotion(x, y, ea);
727 d->mouseXNode->setIntValue(x);
728 d->mouseYNode->setIntValue(y);