| 1 | // FGMouseInput.cxx -- handle user input from mouse devices |
| 2 | // |
| 3 | // Written by Torsten Dreyer, started August 2009 |
| 4 | // Based on work from David Megginson, started May 2001. |
| 5 | // |
| 6 | // Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de |
| 7 | // Copyright (C) 2001 David Megginson, [email protected] |
| 8 | // |
| 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. |
| 13 | // |
| 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. |
| 18 | // |
| 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. |
| 22 | // |
| 23 | // $Id$ |
| 24 | |
| 25 | #ifdef HAVE_CONFIG_H |
| 26 | # include "config.h" |
| 27 | #endif |
| 28 | |
| 29 | #include <memory> |
| 30 | |
| 31 | #include "FGMouseInput.hxx" |
| 32 | |
| 33 | #include <osgGA/GUIEventAdapter> |
| 34 | |
| 35 | #include <simgear/scene/util/SGPickCallback.hxx> |
| 36 | #include <simgear/timing/timestamp.hxx> |
| 37 | #include <simgear/scene/model/SGPickAnimation.hxx> |
| 38 | |
| 39 | #include "FGButton.hxx" |
| 40 | #include "Main/globals.hxx" |
| 41 | #include <Viewer/renderer.hxx> |
| 42 | #include <Viewer/sview.hxx> |
| 43 | #include <Model/panelnode.hxx> |
| 44 | #include <Cockpit/panel.hxx> |
| 45 | #include <Viewer/FGEventHandler.hxx> |
| 46 | #include <GUI/MouseCursor.hxx> |
| 47 | |
| 48 | using std::ios_base; |
| 49 | |
| 50 | const int MAX_MICE = 1; |
| 51 | const int MAX_MOUSE_BUTTONS = 8; |
| 52 | |
| 53 | typedef std::vector<SGSceneryPick> SGSceneryPicks; |
| 54 | typedef SGSharedPtr<SGPickCallback> SGPickCallbackPtr; |
| 55 | typedef std::list<SGPickCallbackPtr> SGPickCallbackList; |
| 56 | |
| 57 | //////////////////////////////////////////////////////////////////////// |
| 58 | |
| 59 | /** |
| 60 | * List of currently pressed mouse button events |
| 61 | */ |
| 62 | class ActivePickCallbacks: |
| 63 | public std::map<int, SGPickCallbackList> |
| 64 | { |
| 65 | public: |
| 66 | void update( double dt, unsigned int keyModState ); |
| 67 | void init( int button, const osgGA::GUIEventAdapter* ea ); |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | void ActivePickCallbacks::init( int button, const osgGA::GUIEventAdapter* ea ) |
| 72 | { |
| 73 | osg::Vec2d windowPos; |
| 74 | flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y()); |
| 75 | |
| 76 | // Get the list of hit callbacks. Take the first callback that |
| 77 | // accepts the mouse button press and ignore the rest of them |
| 78 | // That is they get sorted by distance and by scenegraph depth. |
| 79 | // The nearest one is the first one and the deepest |
| 80 | // (the most specialized one in the scenegraph) is the first. |
| 81 | SGSceneryPicks pickList = globals->get_renderer()->pick(windowPos); |
| 82 | if (pickList.empty()) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | SGSceneryPicks::const_iterator i; |
| 87 | for (i = pickList.begin(); i != pickList.end(); ++i) { |
| 88 | if (i->callback->buttonPressed(button, *ea, i->info)) { |
| 89 | (*this)[button].push_back(i->callback); |
| 90 | return; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void ActivePickCallbacks::update( double dt, unsigned int keyModState ) |
| 96 | { |
| 97 | // handle repeatable mouse press events |
| 98 | for( iterator mi = begin(); mi != end(); ++mi ) { |
| 99 | SGPickCallbackList::iterator li; |
| 100 | for (li = mi->second.begin(); li != mi->second.end(); ++li) { |
| 101 | (*li)->update(dt, keyModState); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | //////////////////////////////////////////////////////////////////////// |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * Settings for a mouse mode. |
| 111 | */ |
| 112 | struct mouse_mode { |
| 113 | mouse_mode (); |
| 114 | |
| 115 | FGMouseCursor::Cursor cursor; |
| 116 | bool constrained; |
| 117 | bool pass_through; |
| 118 | std::unique_ptr<FGButton[]> buttons; |
| 119 | SGBindingList x_bindings[KEYMOD_MAX]; |
| 120 | SGBindingList y_bindings[KEYMOD_MAX]; |
| 121 | }; |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * Settings for a mouse. |
| 126 | */ |
| 127 | struct mouse { |
| 128 | mouse (); |
| 129 | |
| 130 | int x, y; |
| 131 | SGPropertyNode_ptr mode_node; |
| 132 | SGPropertyNode_ptr mouse_button_nodes[MAX_MOUSE_BUTTONS]; |
| 133 | int nModes; |
| 134 | int current_mode; |
| 135 | |
| 136 | SGTimeStamp timeSinceLastMove; |
| 137 | std::unique_ptr<mouse_mode[]> modes; |
| 138 | }; |
| 139 | |
| 140 | static |
| 141 | const SGSceneryPick* |
| 142 | getPick( const SGSceneryPicks& pick_list, |
| 143 | const SGPickCallback* cb ) |
| 144 | { |
| 145 | for(size_t i = 0; i < pick_list.size(); ++i) |
| 146 | if( pick_list[i].callback == cb ) |
| 147 | return &pick_list[i]; |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | //////////////////////////////////////////////////////////////////////// |
| 153 | |
| 154 | class FGMouseInput::FGMouseInputPrivate : public SGPropertyChangeListener |
| 155 | { |
| 156 | public: |
| 157 | FGMouseInputPrivate() : |
| 158 | haveWarped(false), |
| 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)) |
| 165 | { |
| 166 | tooltipTimeoutDone = false; |
| 167 | hoverPickScheduled = false; |
| 168 | |
| 169 | fgGetNode("/sim/mouse/hide-cursor", true )->addChangeListener(this, true); |
| 170 | fgGetNode("/sim/mouse/cursor-timeout-sec", true )->addChangeListener(this, true); |
| 171 | fgGetNode("/sim/mouse/right-button-mode-cycle-enabled", true)->addChangeListener(this, true); |
| 172 | fgGetNode("/sim/mouse/tooltip-delay-msec", true)->addChangeListener(this, true); |
| 173 | fgGetNode("/sim/mouse/click-shows-tooltip", true)->addChangeListener(this, true); |
| 174 | fgGetNode("/sim/mouse/tooltips-enabled", true)->addChangeListener(this, true); |
| 175 | fgGetNode("/sim/mouse/tooltip-commands-registered", 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); |
| 178 | } |
| 179 | |
| 180 | bool areTooltipsEnabled() const |
| 181 | { |
| 182 | return _tooltipsEnabled && _tooltipsCommandsRegistered; |
| 183 | } |
| 184 | |
| 185 | void centerMouseCursor(mouse& m) |
| 186 | { |
| 187 | // center the cursor |
| 188 | m.x = (xSizeNode ? xSizeNode->getIntValue() : 800) / 2; |
| 189 | m.y = (ySizeNode ? ySizeNode->getIntValue() : 600) / 2; |
| 190 | fgWarpMouse(m.x, m.y); |
| 191 | haveWarped = true; |
| 192 | } |
| 193 | |
| 194 | void constrainMouse(int x, int y) |
| 195 | { |
| 196 | int new_x=x,new_y=y; |
| 197 | int xsize = xSizeNode ? xSizeNode->getIntValue() : 800; |
| 198 | int ysize = ySizeNode ? ySizeNode->getIntValue() : 600; |
| 199 | |
| 200 | bool need_warp = false; |
| 201 | if (x <= (xsize * .25) || x >= (xsize * .75)) { |
| 202 | new_x = int(xsize * .5); |
| 203 | need_warp = true; |
| 204 | } |
| 205 | |
| 206 | if (y <= (ysize * .25) || y >= (ysize * .75)) { |
| 207 | new_y = int(ysize * .5); |
| 208 | need_warp = true; |
| 209 | } |
| 210 | |
| 211 | if (need_warp) |
| 212 | { |
| 213 | fgWarpMouse(new_x, new_y); |
| 214 | haveWarped = true; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void scheduleHoverPick(const osg::Vec2d& windowPos) |
| 219 | { |
| 220 | hoverPickScheduled = true; |
| 221 | hoverPos = windowPos; |
| 222 | } |
| 223 | |
| 224 | void doHoverPick(const osg::Vec2d& windowPos) |
| 225 | { |
| 226 | FGMouseCursor::Cursor cur = FGMouseCursor::CURSOR_ARROW; |
| 227 | bool explicitCursor = false; |
| 228 | bool didPick = false; |
| 229 | |
| 230 | SGPickCallback::Priority priority = SGPickCallback::PriorityScenery; |
| 231 | SGSceneryPicks pickList = globals->get_renderer()->pick(windowPos); |
| 232 | |
| 233 | SGSceneryPicks::const_iterator i; |
| 234 | for( i = pickList.begin(); i != pickList.end(); ++i ) |
| 235 | { |
| 236 | bool done = i->callback->hover(windowPos, i->info); |
| 237 | std::string curName(i->callback->getCursor()); |
| 238 | if (!curName.empty()) { |
| 239 | explicitCursor = true; |
| 240 | cur = FGMouseCursor::cursorFromString(curName.c_str()); |
| 241 | } |
| 242 | |
| 243 | // if the callback is of higher prioirty (lower enum index), |
| 244 | // record that. |
| 245 | if (i->callback->getPriority() < priority) { |
| 246 | priority = i->callback->getPriority(); |
| 247 | } |
| 248 | |
| 249 | if (done) { |
| 250 | didPick = true; |
| 251 | break; |
| 252 | } |
| 253 | } // of picks iteration |
| 254 | |
| 255 | // Check if any pick from the previous iteration has disappeared. If so |
| 256 | // notify the callback that the mouse has left its element. |
| 257 | for( i = _previous_picks.begin(); i != _previous_picks.end(); ++i ) |
| 258 | { |
| 259 | if( !getPick(pickList, i->callback) ) |
| 260 | i->callback->mouseLeave(windowPos); |
| 261 | } |
| 262 | _previous_picks = pickList; |
| 263 | |
| 264 | if (!explicitCursor && (priority == SGPickCallback::PriorityPanel)) { |
| 265 | cur = FGMouseCursor::CURSOR_HAND; |
| 266 | } |
| 267 | |
| 268 | FGMouseCursor::instance()->setCursor(cur); |
| 269 | if (!didPick && areTooltipsEnabled()) { |
| 270 | SGPropertyNode_ptr args(new SGPropertyNode); |
| 271 | globals->get_commands()->execute("update-hover", args, nullptr); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void doMouseMoveWithCallbacks(const osgGA::GUIEventAdapter* ea) |
| 276 | { |
| 277 | FGMouseCursor::Cursor cur = FGMouseCursor::CURSOR_CLOSED_HAND; |
| 278 | |
| 279 | osg::Vec2d windowPos; |
| 280 | const bool ok = flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y()); |
| 281 | if (!ok) { |
| 282 | SG_LOG(SG_GUI, SG_WARN, "doMouseMoveWithCallbacks: ignoring mouse move with missing context/traits"); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | // Do not compute scenery picks unless a callback requests it, as it is costly. |
| 287 | SGSceneryPicks pickList; |
| 288 | bool did_pick = false; |
| 289 | |
| 290 | for( ActivePickCallbacks::iterator mi = activePickCallbacks.begin(); |
| 291 | mi != activePickCallbacks.end(); |
| 292 | ++mi ) |
| 293 | { |
| 294 | SGPickCallbackList::iterator li; |
| 295 | for( li = mi->second.begin(); li != mi->second.end(); ++li ) |
| 296 | { |
| 297 | if (!did_pick && (*li)->needsDragPosition()) |
| 298 | { |
| 299 | pickList = globals->get_renderer()->pick(windowPos); |
| 300 | did_pick = true; |
| 301 | } |
| 302 | |
| 303 | const SGSceneryPick* pick = getPick(pickList, *li); |
| 304 | (*li)->mouseMoved(*ea, pick ? &pick->info : 0); |
| 305 | |
| 306 | std::string curName((*li)->getCursor()); |
| 307 | if( !curName.empty() ) |
| 308 | cur = FGMouseCursor::cursorFromString(curName.c_str()); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | FGMouseCursor::instance()->setCursor(cur); |
| 313 | } |
| 314 | |
| 315 | // implement the property-change-listener interfacee |
| 316 | void valueChanged(SGPropertyNode* node) override |
| 317 | { |
| 318 | if (node->getNameString() == "drag-sensitivity") { |
| 319 | SGKnobAnimation::setDragSensitivity(node->getDoubleValue()); |
| 320 | } else if (node->getNameString() == "invert-mouse-wheel") { |
| 321 | SGKnobAnimation::setAlternateMouseWheelDirection(node->getBoolValue()); |
| 322 | } else if (node->getNameString() == "hide-cursor") { |
| 323 | hideCursor = node->getBoolValue(); |
| 324 | } else if (node->getNameString() == "cursor-timeout-sec") { |
| 325 | cursorTimeoutMsec = node->getDoubleValue() * 1000; |
| 326 | } else if (node->getNameString() == "tooltip-delay-msec") { |
| 327 | tooltipDelayMsec = node->getIntValue(); |
| 328 | } else if (node->getNameString() == "right-button-mode-cycle-enabled") { |
| 329 | rightClickModeCycle = node->getBoolValue(); |
| 330 | } else if (node->getNameString() == "click-shows-tooltip") { |
| 331 | clickTriggersTooltip = node->getBoolValue(); |
| 332 | } else if (node->getNameString() == "tooltips-enabled") { |
| 333 | _tooltipsEnabled = node->getBoolValue(); |
| 334 | } else if (node->getNameString() == "tooltip-commands-registered") { |
| 335 | _tooltipsCommandsRegistered = node->getBoolValue(); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | ActivePickCallbacks activePickCallbacks; |
| 340 | SGSceneryPicks _previous_picks; |
| 341 | |
| 342 | mouse mice[MAX_MICE]; |
| 343 | |
| 344 | bool hideCursor, haveWarped; |
| 345 | bool tooltipTimeoutDone; |
| 346 | bool clickTriggersTooltip; |
| 347 | int tooltipDelayMsec, cursorTimeoutMsec; |
| 348 | bool rightClickModeCycle; |
| 349 | |
| 350 | bool _tooltipsEnabled = false; |
| 351 | bool _tooltipsCommandsRegistered = false; ///< avoid errors if the mouse moves before Nasal init |
| 352 | |
| 353 | SGPropertyNode_ptr xSizeNode; |
| 354 | SGPropertyNode_ptr ySizeNode; |
| 355 | SGPropertyNode_ptr xAccelNode; |
| 356 | SGPropertyNode_ptr yAccelNode; |
| 357 | SGPropertyNode_ptr mouseXNode, mouseYNode; |
| 358 | |
| 359 | bool hoverPickScheduled; |
| 360 | osg::Vec2d hoverPos; |
| 361 | }; |
| 362 | |
| 363 | |
| 364 | //////////////////////////////////////////////////////////////////////// |
| 365 | // The Mouse Input Implementation |
| 366 | //////////////////////////////////////////////////////////////////////// |
| 367 | |
| 368 | static FGMouseInput* global_mouseInput = nullptr; |
| 369 | |
| 370 | static void mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea) |
| 371 | { |
| 372 | if(global_mouseInput) |
| 373 | global_mouseInput->doMouseClick(button, updown, x, y, mainWindow, ea); |
| 374 | } |
| 375 | |
| 376 | static void mouseMotionHandler(int x, int y, const osgGA::GUIEventAdapter* ea) |
| 377 | { |
| 378 | if (global_mouseInput != 0) |
| 379 | global_mouseInput->doMouseMotion(x, y, ea); |
| 380 | } |
| 381 | |
| 382 | FGMouseInput::FGMouseInput() = default; |
| 383 | |
| 384 | void FGMouseInput::init() |
| 385 | { |
| 386 | SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings"); |
| 387 | |
| 388 | d.reset(new FGMouseInputPrivate()); |
| 389 | std::string module = ""; |
| 390 | |
| 391 | SGPropertyNode * mouse_nodes = fgGetNode("/input/mice"); |
| 392 | if (mouse_nodes == 0) { |
| 393 | SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!"); |
| 394 | mouse_nodes = fgGetNode("/input/mice", true); |
| 395 | } |
| 396 | |
| 397 | int j; |
| 398 | for (int i = 0; i < MAX_MICE; i++) { |
| 399 | SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true); |
| 400 | mouse &m = d->mice[i]; |
| 401 | |
| 402 | // Grab node pointers |
| 403 | std::ostringstream buf; |
| 404 | buf << "/devices/status/mice/mouse[" << i << "]/mode"; |
| 405 | m.mode_node = fgGetNode(buf.str().c_str()); |
| 406 | if (m.mode_node == NULL) { |
| 407 | m.mode_node = fgGetNode(buf.str().c_str(), true); |
| 408 | m.mode_node->setIntValue(0); |
| 409 | } |
| 410 | for (j = 0; j < MAX_MOUSE_BUTTONS; j++) { |
| 411 | // According to <https://stackoverflow.com/a/12112642/4756009> and other |
| 412 | // similar questions on stackoverflow.com, it seems safer not to try to |
| 413 | // reuse the 'buf' variable we have above. |
| 414 | std::ostringstream buf; |
| 415 | buf << "/devices/status/mice/mouse["<< i << "]/button[" << j << "]"; |
| 416 | m.mouse_button_nodes[j] = fgGetNode(buf.str().c_str(), true); |
| 417 | m.mouse_button_nodes[j]->setBoolValue(false); |
| 418 | } |
| 419 | |
| 420 | // Read all the modes |
| 421 | m.nModes = mouse_node->getIntValue("mode-count", 1); |
| 422 | m.modes.reset(new mouse_mode[m.nModes]); |
| 423 | |
| 424 | for (int j = 0; j < m.nModes; j++) { |
| 425 | int k; |
| 426 | SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true); |
| 427 | |
| 428 | // Read the mouse cursor for this mode |
| 429 | m.modes[j].cursor = FGMouseCursor::cursorFromString(mode_node->getStringValue("cursor", "inherit").c_str()); |
| 430 | |
| 431 | // Read other properties for this mode |
| 432 | m.modes[j].constrained = mode_node->getBoolValue("constrained", false); |
| 433 | m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false); |
| 434 | |
| 435 | // Read the button bindings for this mode |
| 436 | m.modes[j].buttons.reset(new FGButton[MAX_MOUSE_BUTTONS]); |
| 437 | for (k = 0; k < MAX_MOUSE_BUTTONS; k++) { |
| 438 | std::ostringstream buf; |
| 439 | buf << "mouse button " << k; |
| 440 | m.modes[j].buttons[k].init( mode_node->getChild("button", k), buf.str(), module ); |
| 441 | } |
| 442 | |
| 443 | // Read the axis bindings for this mode |
| 444 | read_bindings(mode_node->getChild("x-axis", 0, true), m.modes[j].x_bindings, KEYMOD_NONE, module ); |
| 445 | read_bindings(mode_node->getChild("y-axis", 0, true), m.modes[j].y_bindings, KEYMOD_NONE, module ); |
| 446 | |
| 447 | if (mode_node->hasChild("x-axis-ctrl")) { |
| 448 | read_bindings(mode_node->getChild("x-axis-ctrl"), m.modes[j].x_bindings, KEYMOD_CTRL, module ); |
| 449 | } |
| 450 | if (mode_node->hasChild("x-axis-shift")) { |
| 451 | read_bindings(mode_node->getChild("x-axis-shift"), m.modes[j].x_bindings, KEYMOD_SHIFT, module ); |
| 452 | } |
| 453 | if (mode_node->hasChild("x-axis-ctrl-shift")) { |
| 454 | read_bindings(mode_node->getChild("x-axis-ctrl-shift"), m.modes[j].x_bindings, KEYMOD_CTRL|KEYMOD_SHIFT, module ); |
| 455 | } |
| 456 | |
| 457 | if (mode_node->hasChild("y-axis-ctrl")) { |
| 458 | read_bindings(mode_node->getChild("y-axis-ctrl"), m.modes[j].y_bindings, KEYMOD_CTRL, module ); |
| 459 | } |
| 460 | if (mode_node->hasChild("y-axis-shift")) { |
| 461 | read_bindings(mode_node->getChild("y-axis-shift"), m.modes[j].y_bindings, KEYMOD_SHIFT, module ); |
| 462 | } |
| 463 | if (mode_node->hasChild("y-axis-ctrl-shift")) { |
| 464 | read_bindings(mode_node->getChild("y-axis-ctrl-shift"), m.modes[j].y_bindings, KEYMOD_CTRL|KEYMOD_SHIFT, module ); |
| 465 | } |
| 466 | } // of modes iteration |
| 467 | } |
| 468 | |
| 469 | fgRegisterMouseClickHandler(mouseClickHandler); |
| 470 | fgRegisterMouseMotionHandler(mouseMotionHandler); |
| 471 | global_mouseInput = this; |
| 472 | } |
| 473 | |
| 474 | void FGMouseInput::shutdown() |
| 475 | { |
| 476 | SG_LOG(SG_INPUT, SG_DEBUG, "Shutting down mouse bindings"); |
| 477 | |
| 478 | // This ensures that mouseClickHandler and mouseMotionHandler are no-ops. |
| 479 | global_mouseInput = nullptr; |
| 480 | // Reset the Pimpl |
| 481 | d.reset(); |
| 482 | } |
| 483 | |
| 484 | void FGMouseInput::reinit() |
| 485 | { |
| 486 | shutdown(); |
| 487 | init(); |
| 488 | } |
| 489 | |
| 490 | void FGMouseInput::update ( double dt ) |
| 491 | { |
| 492 | if (!d) { |
| 493 | SG_LOG(SG_INPUT, SG_WARN, "update of mouse before init"); |
| 494 | } |
| 495 | |
| 496 | mouse &m = d->mice[0]; |
| 497 | int mode = m.mode_node->getIntValue(); |
| 498 | if (mode != m.current_mode) { |
| 499 | // current mode has changed |
| 500 | m.current_mode = mode; |
| 501 | m.timeSinceLastMove.stamp(); |
| 502 | |
| 503 | if (mode >= 0 && mode < m.nModes) { |
| 504 | FGMouseCursor::instance()->setCursor(m.modes[mode].cursor); |
| 505 | d->centerMouseCursor(m); |
| 506 | } else { |
| 507 | SG_LOG(SG_INPUT, SG_WARN, "Mouse mode " << mode << " out of range"); |
| 508 | FGMouseCursor::instance()->setCursor(FGMouseCursor::CURSOR_ARROW); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | if ((mode == 0) && d->hoverPickScheduled) { |
| 513 | d->doHoverPick(d->hoverPos); |
| 514 | d->hoverPickScheduled = false; |
| 515 | } |
| 516 | |
| 517 | if (!d->tooltipTimeoutDone && |
| 518 | d->areTooltipsEnabled() && |
| 519 | (m.timeSinceLastMove.elapsedMSec() > d->tooltipDelayMsec)) { |
| 520 | d->tooltipTimeoutDone = true; |
| 521 | SGPropertyNode_ptr arg(new SGPropertyNode); |
| 522 | globals->get_commands()->execute("tooltip-timeout", arg, nullptr); |
| 523 | } |
| 524 | |
| 525 | if ( d->hideCursor ) { |
| 526 | if ( m.timeSinceLastMove.elapsedMSec() > d->cursorTimeoutMsec) { |
| 527 | FGMouseCursor::instance()->hideCursorUntilMouseMove(); |
| 528 | m.timeSinceLastMove.stamp(); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | d->activePickCallbacks.update( dt, fgGetKeyModifiers() ); |
| 533 | } |
| 534 | |
| 535 | mouse::mouse () |
| 536 | : x(-1), |
| 537 | y(-1), |
| 538 | nModes(1), |
| 539 | current_mode(0), |
| 540 | modes() |
| 541 | { |
| 542 | } |
| 543 | |
| 544 | mouse_mode::mouse_mode () |
| 545 | : cursor(FGMouseCursor::CURSOR_ARROW), |
| 546 | constrained(false), |
| 547 | pass_through(false), |
| 548 | buttons() |
| 549 | { |
| 550 | } |
| 551 | |
| 552 | void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea) |
| 553 | { |
| 554 | if (!d) { |
| 555 | // can occur during reset |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | int modifiers = fgGetKeyModifiers(); |
| 560 | |
| 561 | mouse &m = d->mice[0]; |
| 562 | mouse_mode &mode = m.modes[m.current_mode]; |
| 563 | // Let the property manager know. |
| 564 | if (b >= 0 && b < MAX_MOUSE_BUTTONS) |
| 565 | m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN); |
| 566 | |
| 567 | if (!d->rightClickModeCycle && (b == 2)) { |
| 568 | // in spring-loaded look mode, ignore right clicks entirely here |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | |
| 573 | // Pass on to PUI and the panel if |
| 574 | // requested, and return if one of |
| 575 | // them consumes the event. |
| 576 | |
| 577 | osg::Vec2d windowPos; |
| 578 | bool ok = flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y()); |
| 579 | if (!ok) { |
| 580 | SG_LOG(SG_GUI, SG_WARN, "Ignoring mouse click with null context/traits"); |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | SGSceneryPicks pickList; |
| 585 | |
| 586 | if (isRightDragLookActive() && (updown == MOUSE_BUTTON_DOWN)) { |
| 587 | // when spring-loaded mode is active, don't do scene selection for picks |
| 588 | // https://sourceforge.net/p/flightgear/codetickets/2108/ |
| 589 | } else { |
| 590 | pickList = globals->get_renderer()->pick(windowPos); |
| 591 | } |
| 592 | |
| 593 | if( updown == MOUSE_BUTTON_UP ) |
| 594 | { |
| 595 | // Execute the mouse up event in any case, may be we should |
| 596 | // stop processing here? |
| 597 | |
| 598 | SGPickCallbackList& callbacks = d->activePickCallbacks[b]; |
| 599 | |
| 600 | while( !callbacks.empty() ) |
| 601 | { |
| 602 | SGPickCallbackPtr& cb = callbacks.front(); |
| 603 | const SGSceneryPick* pick = getPick(pickList, cb); |
| 604 | cb->buttonReleased(ea->getModKeyMask(), *ea, pick ? &pick->info : nullptr); |
| 605 | |
| 606 | callbacks.pop_front(); |
| 607 | } |
| 608 | |
| 609 | if (ea->getHandled()) { |
| 610 | // for https://sourceforge.net/p/flightgear/codetickets/2347/ |
| 611 | // we cleared the active picks, but don't do further processing |
| 612 | return; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | if (mode.pass_through) { |
| 617 | // compute a |
| 618 | // scenegraph intersection point corresponding to the mouse click |
| 619 | if (updown == MOUSE_BUTTON_DOWN) { |
| 620 | d->activePickCallbacks.init( b, ea ); |
| 621 | |
| 622 | if (d->clickTriggersTooltip && d->areTooltipsEnabled()) { |
| 623 | SGPropertyNode_ptr args(new SGPropertyNode); |
| 624 | args->setStringValue("reason", "click"); |
| 625 | globals->get_commands()->execute("tooltip-timeout", args, nullptr); |
| 626 | d->tooltipTimeoutDone = true; |
| 627 | } |
| 628 | } else { |
| 629 | // do a hover pick now, to fix up cursor |
| 630 | d->doHoverPick(windowPos); |
| 631 | } // mouse button was released |
| 632 | } // of pass-through mode |
| 633 | |
| 634 | if (b >= MAX_MOUSE_BUTTONS) { |
| 635 | SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b |
| 636 | << " where only " << MAX_MOUSE_BUTTONS << " expected"); |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | m.modes[m.current_mode].buttons[b].update( modifiers, 0 != updown, x, y); |
| 641 | } |
| 642 | |
| 643 | void FGMouseInput::processMotion(int x, int y, const osgGA::GUIEventAdapter* ea) |
| 644 | { |
| 645 | if (!d->activePickCallbacks[0].empty()) { |
| 646 | d->doMouseMoveWithCallbacks(ea); |
| 647 | return; |
| 648 | } |
| 649 | |
| 650 | if (SviewMouseMotion(x, y, *ea)) { |
| 651 | return; |
| 652 | } |
| 653 | |
| 654 | mouse &m = d->mice[0]; |
| 655 | int modeIndex = m.current_mode; |
| 656 | |
| 657 | if (isRightDragLookActive()) { |
| 658 | // right mouse is down, force look mode |
| 659 | modeIndex = 3; |
| 660 | } |
| 661 | |
| 662 | if (modeIndex == 0) { |
| 663 | osg::Vec2d windowPos; |
| 664 | flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y()); |
| 665 | |
| 666 | // omly do hover picks if no buttons are down |
| 667 | if (ea->getButtonMask() == 0) { |
| 668 | d->scheduleHoverPick(windowPos); |
| 669 | } |
| 670 | |
| 671 | // mouse has moved, so we may need to issue tooltip-timeout command again |
| 672 | d->tooltipTimeoutDone = false; |
| 673 | } |
| 674 | |
| 675 | mouse_mode &mode = m.modes[modeIndex]; |
| 676 | |
| 677 | if (d->haveWarped) |
| 678 | { |
| 679 | // don't fire mouse-movement events at the first update after warping the mouse, |
| 680 | // just remember the new mouse position |
| 681 | d->haveWarped = false; |
| 682 | } |
| 683 | else |
| 684 | { |
| 685 | int modifiers = fgGetKeyModifiers(); |
| 686 | int xsize = d->xSizeNode ? d->xSizeNode->getIntValue() : 800; |
| 687 | int ysize = d->ySizeNode ? d->ySizeNode->getIntValue() : 600; |
| 688 | |
| 689 | // OK, PUI didn't want the event, |
| 690 | // so we can play with it. |
| 691 | if (x != m.x) { |
| 692 | int delta = x - m.x; |
| 693 | d->xAccelNode->setIntValue( delta ); |
| 694 | for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++) |
| 695 | mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize)); |
| 696 | } |
| 697 | if (y != m.y) { |
| 698 | int delta = y - m.y; |
| 699 | d->yAccelNode->setIntValue( -delta ); |
| 700 | for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++) |
| 701 | mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize)); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | // Constrain the mouse if requested |
| 706 | if (mode.constrained) { |
| 707 | d->constrainMouse(x, y); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | void FGMouseInput::doMouseMotion (int x, int y, const osgGA::GUIEventAdapter* ea) |
| 712 | { |
| 713 | if (!d) { |
| 714 | // can occur during reset |
| 715 | return; |
| 716 | } |
| 717 | |
| 718 | mouse &m = d->mice[0]; |
| 719 | |
| 720 | if (m.current_mode < 0 || m.current_mode >= m.nModes) { |
| 721 | m.x = x; |
| 722 | m.y = y; |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | m.timeSinceLastMove.stamp(); |
| 727 | FGMouseCursor::instance()->mouseMoved(); |
| 728 | |
| 729 | // TODO Get rid of this as soon as soon as cursor hide timeout works globally |
| 730 | |
| 731 | if (!ea->getHandled()) { |
| 732 | processMotion(x, y, ea); |
| 733 | } |
| 734 | |
| 735 | m.x = x; |
| 736 | m.y = y; |
| 737 | d->mouseXNode->setIntValue(x); |
| 738 | d->mouseYNode->setIntValue(y); |
| 739 | } |
| 740 | |
| 741 | bool FGMouseInput::isRightDragToLookEnabled() const |
| 742 | { |
| 743 | if (!d) { |
| 744 | return false; |
| 745 | } |
| 746 | |
| 747 | return (d->rightClickModeCycle == false); |
| 748 | } |
| 749 | |
| 750 | bool FGMouseInput::isActiveModePassThrough() const |
| 751 | { |
| 752 | if (!d) { |
| 753 | return false; |
| 754 | } |
| 755 | |
| 756 | mouse &m = d->mice[0]; |
| 757 | int mode = m.current_mode; |
| 758 | if (isRightDragToLookEnabled() && m.mouse_button_nodes[2]->getBoolValue()) { |
| 759 | mode = 3; |
| 760 | } |
| 761 | |
| 762 | return m.modes[mode].pass_through; |
| 763 | } |
| 764 | |
| 765 | bool FGMouseInput::isRightDragLookActive() const |
| 766 | { |
| 767 | if (!d) { |
| 768 | return false; |
| 769 | } |
| 770 | |
| 771 | const auto& m = d->mice[0]; |
| 772 | if (!d->rightClickModeCycle && m.nModes > 3) { |
| 773 | return m.mouse_button_nodes[2]->getBoolValue(); |
| 774 | } |
| 775 | |
| 776 | return false; |
| 777 | } |
| 778 | |
| 779 | |
| 780 | // Register the subsystem. |
| 781 | SGSubsystemMgr::Registrant<FGMouseInput> registrantFGMouseInput; |