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 );
337 fgRegisterMouseClickHandler(mouseClickHandler);
338 fgRegisterMouseMotionHandler(mouseMotionHandler);
341 void FGMouseInput::update ( double dt )
343 int cursorTimeoutMsec = d->cursorTimeoutNode->getDoubleValue() * 1000;
344 int tooltipDelayMsec = d->tooltipShowDelayNode->getIntValue();
346 mouse &m = d->mice[0];
347 int mode = m.mode_node->getIntValue();
348 if (mode != m.current_mode) {
349 // current mode has changed
350 m.current_mode = mode;
351 m.timeSinceLastMove.stamp();
353 if (mode >= 0 && mode < m.nModes) {
354 FGMouseCursor::instance()->setCursor(m.modes[mode].cursor);
355 d->centerMouseCursor(m);
357 SG_LOG(SG_INPUT, SG_WARN, "Mouse mode " << mode << " out of range");
358 FGMouseCursor::instance()->setCursor(FGMouseCursor::CURSOR_ARROW);
362 if ( d->hideCursorNode == NULL || d->hideCursorNode->getBoolValue() ) {
363 // if delay is <= 0, disable tooltips
364 if ( !d->tooltipTimeoutDone &&
365 (tooltipDelayMsec > 0) &&
366 (m.timeSinceLastMove.elapsedMSec() > tooltipDelayMsec))
368 d->tooltipTimeoutDone = true;
369 SGPropertyNode_ptr arg(new SGPropertyNode);
370 globals->get_commands()->execute("tooltip-timeout", arg);
373 if ( m.timeSinceLastMove.elapsedMSec() > cursorTimeoutMsec) {
374 FGMouseCursor::instance()->hideCursorUntilMouseMove();
375 m.timeSinceLastMove.stamp();
379 d->activePickCallbacks.update( dt );
396 mouse_mode::mouse_mode ()
397 : cursor(FGMouseCursor::CURSOR_ARROW),
404 mouse_mode::~mouse_mode ()
406 // FIXME: memory leak
407 // for (int i = 0; i < KEYMOD_MAX; i++) {
409 // for (j = 0; i < x_bindings[i].size(); j++)
410 // delete bindings[i][j];
411 // for (j = 0; j < y_bindings[i].size(); j++)
412 // delete bindings[i][j];
419 void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
421 int modifiers = fgGetKeyModifiers();
423 mouse &m = d->mice[0];
424 mouse_mode &mode = m.modes[m.current_mode];
425 // Let the property manager know.
426 if (b >= 0 && b < MAX_MOUSE_BUTTONS)
427 m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
429 if (!d->rightButtonModeCycleNode->getBoolValue() && (b == 2)) {
430 // in spring-loaded look mode, ignore right clicks entirely here
434 // Pass on to PUI and the panel if
435 // requested, and return if one of
436 // them consumes the event.
438 if (updown != MOUSE_BUTTON_DOWN) {
439 // Execute the mouse up event in any case, may be we should
440 // stop processing here?
441 while (!d->activePickCallbacks[b].empty()) {
442 d->activePickCallbacks[b].front()->buttonReleased();
443 d->activePickCallbacks[b].pop_front();
447 if (mode.pass_through) {
448 // remove once PUI uses standard picking mechanism
449 if (0 <= x && 0 <= y && puMouse(b, updown, x, y))
452 // pui didn't want the click event so compute a
453 // scenegraph intersection point corresponding to the mouse click
454 if (updown == MOUSE_BUTTON_DOWN) {
455 d->activePickCallbacks.init( b, ea );
460 // OK, PUI and the panel didn't want the click
461 if (b >= MAX_MOUSE_BUTTONS) {
462 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
463 << " where only " << MAX_MOUSE_BUTTONS << " expected");
467 m.modes[m.current_mode].buttons[b].update( modifiers, 0 != updown, x, y);
469 if (d->clickTriggersTooltipNode->getBoolValue()) {
470 SGPropertyNode_ptr args(new SGPropertyNode);
471 args->setStringValue("reason", "click");
472 globals->get_commands()->execute("tooltip-timeout", args);
473 d->tooltipTimeoutDone = true;
477 void FGMouseInput::processMotion(int x, int y, const osgGA::GUIEventAdapter* ea)
479 if (!d->activePickCallbacks[0].empty()) {
480 //SG_LOG(SG_GENERAL, SG_INFO, "mouse-motion, have active pick callback");
481 BOOST_FOREACH(SGPickCallback* cb, d->activePickCallbacks[0]) {
487 mouse &m = d->mice[0];
488 int modeIndex = m.current_mode;
489 // are we in spring-loaded look mode?
490 if (!d->rightButtonModeCycleNode->getBoolValue()) {
491 if (m.mouse_button_nodes[2]->getBoolValue()) {
492 // right mouse is down, force look mode
497 if (modeIndex == 0) {
498 osg::Vec2d windowPos;
499 flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
500 d->doHoverPick(windowPos);
501 // mouse has moved, so we may need to issue tooltip-timeout command again
502 d->tooltipTimeoutDone = false;
505 mouse_mode &mode = m.modes[modeIndex];
507 // Pass on to PUI if requested, and return
508 // if PUI consumed the event.
509 if (mode.pass_through && puMouse(x, y)) {
515 // don't fire mouse-movement events at the first update after warping the mouse,
516 // just remember the new mouse position
517 d->haveWarped = false;
521 int modifiers = fgGetKeyModifiers();
522 int xsize = d->xSizeNode ? d->xSizeNode->getIntValue() : 800;
523 int ysize = d->ySizeNode ? d->ySizeNode->getIntValue() : 600;
525 // OK, PUI didn't want the event,
526 // so we can play with it.
529 d->xAccelNode->setIntValue( delta );
530 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
531 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
535 d->yAccelNode->setIntValue( -delta );
536 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
537 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
541 // Constrain the mouse if requested
542 if (mode.constrained) {
543 d->constrainMouse(x, y);
547 void FGMouseInput::doMouseMotion (int x, int y, const osgGA::GUIEventAdapter* ea)
549 mouse &m = d->mice[0];
551 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
557 m.timeSinceLastMove.stamp();
558 FGMouseCursor::instance()->mouseMoved();
560 processMotion(x, y, ea);
564 d->mouseXNode->setIntValue(x);
565 d->mouseYNode->setIntValue(y);