| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/*************************************************************************** |
| 3 |
* Copyright (c) 2015 Victor Titov (DeepSOIC) <[email protected]> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 20 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 21 |
* * |
| 22 |
*************************************************************************** |
| 23 |
* * |
| 24 |
* Minor modifications made by Pablo Gil (pablogil) in order to create * |
| 25 |
* a Maya or Unity 3D mouse navigation style: * |
| 26 |
* ALT + left mouse button = orbit * |
| 27 |
* ALT + right mouse button = zoom * |
| 28 |
* ALT + middle mouse button = pan * |
| 29 |
* * |
| 30 |
* Thanks Victor for your help! * |
| 31 |
* * |
| 32 |
***************************************************************************/ |
| 33 |
|
| 34 |
/* |
| 35 |
*A few notes on this style. (by DeepSOIC) |
| 36 |
* |
| 37 |
* In this style, LMB serves dual purpose. It is selecting objects, as well as |
| 38 |
* spinning the view. The trick that enables it is as follows: The mousedown |
| 39 |
* event is consumed an saved, but otherwise remains unprocessed. If a drag is |
| 40 |
* detected while the button is down, the event is finally consumed (the saved |
| 41 |
* one is discarded), and spinning starts. If there is no drag detected before |
| 42 |
* the button is released, the saved mousedown is propagated to inherited, |
| 43 |
* followed by the mouseup. The same trick is used for RMB, so up to two |
| 44 |
* mousedown can be postponed. |
| 45 |
* |
| 46 |
* This navigation style does not exactly follow the structure of other |
| 47 |
* navigation styles, it does not fill many of the global variables defined in |
| 48 |
* NavigationStyle. |
| 49 |
* |
| 50 |
* This mode does not support locking cursor position on screen when |
| 51 |
* navigating, since with absolute pointing devices like pen and touch it makes |
| 52 |
* no sense (this style was specifically crafted for such devices). |
| 53 |
* |
| 54 |
* In this style, setViewing is not used (because I could not figure out how to |
| 55 |
* use it properly, and it seems to just work without it). |
| 56 |
* |
| 57 |
* This style wasn't tested with space during development (I don't have one). |
| 58 |
*/ |
| 59 |
|
| 60 |
|
| 61 |
#include <QApplication> |
| 62 |
|
| 63 |
|
| 64 |
#include <Base/Console.h> |
| 65 |
|
| 66 |
#include "Navigation/NavigationStyle.h" |
| 67 |
#include "SoTouchEvents.h" |
| 68 |
#include "View3DInventorViewer.h" |
| 69 |
|
| 70 |
|
| 71 |
using namespace Gui; |
| 72 |
|
| 73 |
// ---------------------------------------------------------------------------------- |
| 74 |
|
| 75 |
/* TRANSLATOR Gui::MayaGestureNavigationStyle */ |
| 76 |
|
| 77 |
TYPESYSTEM_SOURCE(Gui::MayaGestureNavigationStyle, Gui::UserNavigationStyle) |
| 78 |
|
| 79 |
MayaGestureNavigationStyle::MayaGestureNavigationStyle() |
| 80 |
{ |
| 81 |
mouseMoveThreshold = QApplication::startDragDistance(); |
| 82 |
mouseMoveThresholdBroken = false; |
| 83 |
mousedownConsumedCount = 0; |
| 84 |
thisClickIsComplex = false; |
| 85 |
inGesture = false; |
| 86 |
} |
| 87 |
|
| 88 |
MayaGestureNavigationStyle::~MayaGestureNavigationStyle() = default; |
| 89 |
|
| 90 |
const char* MayaGestureNavigationStyle::mouseButtons(ViewerMode mode) |
| 91 |
{ |
| 92 |
switch (mode) { |
| 93 |
case NavigationStyle::SELECTION: |
| 94 |
return QT_TR_NOOP("Tap OR click left mouse button."); |
| 95 |
case NavigationStyle::PANNING: |
| 96 |
return QT_TR_NOOP("Drag screen with two fingers OR press Alt + middle mouse button."); |
| 97 |
case NavigationStyle::DRAGGING: |
| 98 |
return QT_TR_NOOP( |
| 99 |
"Drag screen with one finger OR press Alt + left mouse button. In Sketcher and " |
| 100 |
"other edit modes, hold Alt in addition." |
| 101 |
); |
| 102 |
case NavigationStyle::ZOOMING: |
| 103 |
return QT_TR_NOOP( |
| 104 |
"Pinch (place two fingers on the screen and drag them apart from or towards each " |
| 105 |
"other) OR scroll mouse wheel OR press Alt + right mouse button OR PgUp/PgDown on " |
| 106 |
"keyboard." |
| 107 |
); |
| 108 |
default: |
| 109 |
return "No description"; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/*! |
| 114 |
* \brief MayaGestureNavigationStyle::testMoveThreshold tests if the mouse has moved far enough to |
| 115 |
* constder it a drag. |
| 116 |
* \param currentPos current position of mouse cursor, in local pixel coordinates. |
| 117 |
* \return true if the mouse was moved far enough. False if it's within the boundary. Ignores |
| 118 |
* MayaGestureNavigationStyle::mouseMoveThresholdBroken flag. |
| 119 |
*/ |
| 120 |
bool MayaGestureNavigationStyle::testMoveThreshold(const SbVec2s currentPos) const |
| 121 |
{ |
| 122 |
SbVec2s movedBy = currentPos - this->mousedownPos; |
| 123 |
return SbVec2f(movedBy).length() >= this->mouseMoveThreshold; |
| 124 |
} |
| 125 |
|
| 126 |
void MayaGestureNavigationStyle::zoomByCursor(const SbVec2f& thispos, const SbVec2f& prevpos) |
| 127 |
{ |
| 128 |
const float dx = thispos[0] - prevpos[0]; |
| 129 |
const float dy = thispos[1] - prevpos[1]; |
| 130 |
|
| 131 |
// Compute zoom based on diagonal difference. |
| 132 |
float value = (dx - dy) * 10.0f; |
| 133 |
|
| 134 |
if (this->invertZoom) { |
| 135 |
value = -value; |
| 136 |
} |
| 137 |
zoom(viewer->getSoRenderManager()->getCamera(), value); |
| 138 |
} |
| 139 |
|
| 140 |
SbBool MayaGestureNavigationStyle::processSoEvent(const SoEvent* const ev) |
| 141 |
{ |
| 142 |
// Events when in "ready-to-seek" mode are ignored, except those |
| 143 |
// which influence the seek mode itself -- these are handled further |
| 144 |
// up the inheritance hierarchy. |
| 145 |
if (this->isSeekMode()) { |
| 146 |
return inherited::processSoEvent(ev); |
| 147 |
} |
| 148 |
// Switch off viewing mode (Bug #0000911) |
| 149 |
if (!this->isSeekMode() && !this->isAnimating() && this->isViewing()) { |
| 150 |
this->setViewing(false); // by default disable viewing mode to render the scene |
| 151 |
} |
| 152 |
// setViewing() is never used in this style, so the previous if is very unlikely to be hit. |
| 153 |
|
| 154 |
const SoType type(ev->getTypeId()); |
| 155 |
// define some shortcuts... |
| 156 |
bool evIsButton = type.isDerivedFrom(SoMouseButtonEvent::getClassTypeId()); |
| 157 |
bool evIsKeyboard = type.isDerivedFrom(SoKeyboardEvent::getClassTypeId()); |
| 158 |
bool evIsLoc2 = type.isDerivedFrom(SoLocation2Event::getClassTypeId()); // mouse movement |
| 159 |
bool evIsLoc3 = type.isDerivedFrom(SoMotion3Event::getClassTypeId()); // spaceball/joystick movement |
| 160 |
bool evIsGesture = type.isDerivedFrom(SoGestureEvent::getClassTypeId()); // touchscreen gesture |
| 161 |
|
| 162 |
const SbVec2f prevnormalized = this->lastmouseposition; |
| 163 |
const SbVec2s pos(ev->getPosition()); // not valid for gestures |
| 164 |
const SbVec2f posn = this->normalizePixelPos(pos); |
| 165 |
// pos: local coordinates of event, in pixels |
| 166 |
// posn: normalized local coordinates of event ((0,0) = lower left corner, (1,1) = upper right |
| 167 |
// corner) |
| 168 |
float ratio = viewer->getSoRenderManager()->getViewportRegion().getViewportAspectRatio(); |
| 169 |
|
| 170 |
if (evIsButton || evIsLoc2) { |
| 171 |
this->lastmouseposition = posn; |
| 172 |
} |
| 173 |
|
| 174 |
const ViewerMode curmode = this->currentmode; |
| 175 |
// ViewerMode newmode = curmode; |
| 176 |
|
| 177 |
// make a unified mouse+modifiers state value (combo) |
| 178 |
enum |
| 179 |
{ |
| 180 |
BUTTON1DOWN = 1 << 0, |
| 181 |
BUTTON2DOWN = 1 << 1, |
| 182 |
BUTTON3DOWN = 1 << 2, |
| 183 |
CTRLDOWN = 1 << 3, |
| 184 |
SHIFTDOWN = 1 << 4, |
| 185 |
ALTDOWN = 1 << 5, |
| 186 |
MASKBUTTONS = BUTTON1DOWN | BUTTON2DOWN | BUTTON3DOWN, |
| 187 |
MASKMODIFIERS = CTRLDOWN | SHIFTDOWN | ALTDOWN |
| 188 |
}; |
| 189 |
unsigned int comboBefore = // before = state before this event |
| 190 |
(this->button1down ? BUTTON1DOWN : 0) | (this->button2down ? BUTTON2DOWN : 0) |
| 191 |
| (this->button3down ? BUTTON3DOWN : 0) | (this->ctrldown ? CTRLDOWN : 0) |
| 192 |
| (this->shiftdown ? SHIFTDOWN : 0) | (this->altdown ? ALTDOWN : 0); |
| 193 |
|
| 194 |
// test for complex clicks |
| 195 |
int cntMBBefore = (comboBefore & BUTTON1DOWN ? 1 : 0) // cntMBBefore = how many buttons were |
| 196 |
// down when this event arrived? |
| 197 |
+ (comboBefore & BUTTON2DOWN ? 1 : 0) + (comboBefore & BUTTON3DOWN ? 1 : 0); |
| 198 |
if (cntMBBefore >= 2) { |
| 199 |
this->thisClickIsComplex = true; |
| 200 |
} |
| 201 |
if (cntMBBefore == 0) { // a good chance to reset some click-related stuff |
| 202 |
this->thisClickIsComplex = false; |
| 203 |
this->mousedownConsumedCount = 0; // shouldn't be necessary, just a fail-safe. |
| 204 |
} |
| 205 |
|
| 206 |
// Mismatches in state of the modifier keys happens if the user |
| 207 |
// presses or releases them outside the viewer window. |
| 208 |
syncModifierKeys(ev); |
| 209 |
// before this block, mouse button states in NavigationStyle::buttonXdown reflected those before |
| 210 |
// current event arrived. track mouse button states |
| 211 |
if (evIsButton) { |
| 212 |
auto const event = (const SoMouseButtonEvent*)ev; |
| 213 |
const int button = event->getButton(); |
| 214 |
const SbBool press // the button was pressed (if false -> released) |
| 215 |
= event->getState() == SoButtonEvent::DOWN ? true : false; |
| 216 |
switch (button) { |
| 217 |
case SoMouseButtonEvent::BUTTON1: |
| 218 |
this->button1down = press; |
| 219 |
break; |
| 220 |
case SoMouseButtonEvent::BUTTON2: |
| 221 |
this->button2down = press; |
| 222 |
break; |
| 223 |
case SoMouseButtonEvent::BUTTON3: |
| 224 |
this->button3down = press; |
| 225 |
break; |
| 226 |
// whatever else, we don't track |
| 227 |
} |
| 228 |
} |
| 229 |
// after this block, the new states of the buttons are already in. |
| 230 |
|
| 231 |
unsigned int comboAfter = // after = state after this event (current, essentially) |
| 232 |
(this->button1down ? BUTTON1DOWN : 0) | (this->button2down ? BUTTON2DOWN : 0) |
| 233 |
| (this->button3down ? BUTTON3DOWN : 0) | (this->ctrldown ? CTRLDOWN : 0) |
| 234 |
| (this->shiftdown ? SHIFTDOWN : 0) | (this->altdown ? ALTDOWN : 0); |
| 235 |
|
| 236 |
// test for complex clicks (again) |
| 237 |
int cntMBAfter = (comboAfter & BUTTON1DOWN ? 1 : 0) // cntMBAfter = how many buttons were down |
| 238 |
// when this event arrived? |
| 239 |
+ (comboAfter & BUTTON2DOWN ? 1 : 0) + (comboAfter & BUTTON3DOWN ? 1 : 0); |
| 240 |
if (cntMBAfter >= 2) { |
| 241 |
this->thisClickIsComplex = true; |
| 242 |
} |
| 243 |
// if (cntMBAfter==0) this->thisClickIsComplex = false;//don't reset the flag now, we need to |
| 244 |
// know that this mouseUp was an end of a click that was complex. The flag will reset by the |
| 245 |
// before-check in the next event. |
| 246 |
|
| 247 |
// test for move detection |
| 248 |
if (evIsLoc2 || evIsButton) { |
| 249 |
this->mouseMoveThresholdBroken |= this->testMoveThreshold(pos); |
| 250 |
} |
| 251 |
|
| 252 |
// track gestures |
| 253 |
if (evIsGesture) { |
| 254 |
auto gesture = static_cast<const SoGestureEvent*>(ev); |
| 255 |
switch (gesture->state) { |
| 256 |
case SoGestureEvent::SbGSStart: |
| 257 |
// assert(!inGesture);//start of another gesture before the first finished? Happens |
| 258 |
// all the time for Pan gesture... No idea why! --DeepSOIC |
| 259 |
inGesture = true; |
| 260 |
break; |
| 261 |
case SoGestureEvent::SbGSUpdate: |
| 262 |
assert(inGesture); // gesture update without start? |
| 263 |
inGesture = true; |
| 264 |
break; |
| 265 |
case SoGestureEvent::SbGSEnd: |
| 266 |
assert(inGesture); // gesture ended without starting? |
| 267 |
inGesture = false; |
| 268 |
break; |
| 269 |
case SoGestureEvent::SbGsCanceled: |
| 270 |
assert(inGesture); // gesture canceled without starting? |
| 271 |
inGesture = false; |
| 272 |
break; |
| 273 |
default: |
| 274 |
assert(0); // shouldn't happen |
| 275 |
inGesture = false; |
| 276 |
} |
| 277 |
} |
| 278 |
if (evIsButton) { |
| 279 |
if (inGesture) { |
| 280 |
inGesture = false; // reset the flag when mouse clicks are received, to ensure enabling |
| 281 |
// mouse navigation back. |
| 282 |
setViewingMode(NavigationStyle::SELECTION); // exit navigation asap, to proceed with |
| 283 |
// regular processing of the click |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
bool suppressLMBDrag = false; |
| 288 |
if (viewer->isEditing()) { |
| 289 |
// in edit mode, disable lmb dragging (spinning). Holding Alt enables it. |
| 290 |
suppressLMBDrag = !(comboAfter & ALTDOWN); |
| 291 |
} |
| 292 |
|
| 293 |
//----------all this were preparations. Now comes the event handling! ---------- |
| 294 |
|
| 295 |
SbBool processed = false; // a return value for the BlahblahblahNavigationStyle::processSoEvent |
| 296 |
bool propagated = false; // an internal flag indicating that the event has been already passed |
| 297 |
// to inherited, to suppress the automatic doing of this at the end. |
| 298 |
// goto finalize = return processed. Might be important to do something before done (none now). |
| 299 |
|
| 300 |
// give the nodes in the foreground root the chance to handle events (e.g color bar) |
| 301 |
if (!viewer->isEditing()) { |
| 302 |
processed = handleEventInForeground(ev); |
| 303 |
} |
| 304 |
if (processed) { |
| 305 |
goto finalize; |
| 306 |
} |
| 307 |
|
| 308 |
// Mode-independent keyboard handling |
| 309 |
if (evIsKeyboard) { |
| 310 |
auto const event = (const SoKeyboardEvent*)ev; |
| 311 |
const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false; |
| 312 |
switch (event->getKey()) { |
| 313 |
case SoKeyboardEvent::H: |
| 314 |
// Disable H key in editing mode because of conflict with sketcher |
| 315 |
if (!viewer->isEditing()) { |
| 316 |
processed = true; |
| 317 |
if (!press) { |
| 318 |
setupPanningPlane(viewer->getCamera()); |
| 319 |
lookAtPoint(event->getPosition()); |
| 320 |
} |
| 321 |
} |
| 322 |
break; |
| 323 |
default: |
| 324 |
break; |
| 325 |
} |
| 326 |
} |
| 327 |
if (processed) { |
| 328 |
goto finalize; |
| 329 |
} |
| 330 |
|
| 331 |
// mode-independent spaceball/joystick handling |
| 332 |
if (evIsLoc3) { |
| 333 |
auto const event = static_cast<const SoMotion3Event*>(ev); |
| 334 |
if (event) { |
| 335 |
this->processMotionEvent(event); |
| 336 |
} |
| 337 |
processed = true; |
| 338 |
} |
| 339 |
if (processed) { |
| 340 |
goto finalize; |
| 341 |
} |
| 342 |
|
| 343 |
// all mode-dependent stuff is within this switch. |
| 344 |
switch (curmode) { |
| 345 |
case NavigationStyle::SELECTION: |
| 346 |
// Prevent interrupting rubber-band selection in sketcher |
| 347 |
if (viewer->isEditing()) { |
| 348 |
if (evIsButton) { |
| 349 |
auto const event = (const SoMouseButtonEvent*)ev; |
| 350 |
const SbBool press = event->getState() == SoButtonEvent::DOWN; |
| 351 |
const int button = event->getButton(); |
| 352 |
|
| 353 |
if (!press && button == SoMouseButtonEvent::BUTTON1) { |
| 354 |
setViewingMode(NavigationStyle::IDLE); |
| 355 |
break; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
if (this->button1down) { |
| 360 |
break; |
| 361 |
} |
| 362 |
} |
| 363 |
[[fallthrough]]; |
| 364 |
case NavigationStyle::IDLE: |
| 365 |
// Prevent interrupting rubber-band selection in sketcher |
| 366 |
if (viewer->isEditing()) { |
| 367 |
if (evIsButton) { |
| 368 |
auto const event = (const SoMouseButtonEvent*)ev; |
| 369 |
const SbBool press = event->getState() == SoButtonEvent::DOWN; |
| 370 |
const int button = event->getButton(); |
| 371 |
|
| 372 |
if (press && button == SoMouseButtonEvent::BUTTON1 && !this->altdown) { |
| 373 |
setViewingMode(NavigationStyle::SELECTION); |
| 374 |
break; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
[[fallthrough]]; |
| 379 |
case NavigationStyle::INTERACT: { |
| 380 |
// idle and interaction |
| 381 |
|
| 382 |
// keyboard |
| 383 |
if (evIsKeyboard) { |
| 384 |
auto const event = (const SoKeyboardEvent*)ev; |
| 385 |
const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false; |
| 386 |
|
| 387 |
switch (event->getKey()) { |
| 388 |
case SoKeyboardEvent::S: |
| 389 |
case SoKeyboardEvent::HOME: |
| 390 |
case SoKeyboardEvent::LEFT_ARROW: |
| 391 |
case SoKeyboardEvent::UP_ARROW: |
| 392 |
case SoKeyboardEvent::RIGHT_ARROW: |
| 393 |
case SoKeyboardEvent::DOWN_ARROW: |
| 394 |
processed = inherited::processSoEvent(ev); |
| 395 |
propagated = true; |
| 396 |
break; |
| 397 |
case SoKeyboardEvent::PAGE_UP: |
| 398 |
if (press) { |
| 399 |
doZoom(viewer->getSoRenderManager()->getCamera(), getDelta(), posn); |
| 400 |
} |
| 401 |
processed = true; |
| 402 |
break; |
| 403 |
case SoKeyboardEvent::PAGE_DOWN: |
| 404 |
if (press) { |
| 405 |
doZoom(viewer->getSoRenderManager()->getCamera(), -getDelta(), posn); |
| 406 |
} |
| 407 |
processed = true; |
| 408 |
break; |
| 409 |
default: |
| 410 |
break; |
| 411 |
} // switch key |
| 412 |
} |
| 413 |
if (processed) { |
| 414 |
goto finalize; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
// Mouse Button / Spaceball Button handling |
| 419 |
if (evIsButton) { |
| 420 |
auto const event = (const SoMouseButtonEvent*)ev; |
| 421 |
const int button = event->getButton(); |
| 422 |
const SbBool press // the button was pressed (if false -> released) |
| 423 |
= event->getState() == SoButtonEvent::DOWN ? true : false; |
| 424 |
switch (button) { |
| 425 |
case SoMouseButtonEvent::BUTTON1: |
| 426 |
case SoMouseButtonEvent::BUTTON2: |
| 427 |
if (press) { |
| 428 |
if (this->thisClickIsComplex && this->mouseMoveThresholdBroken) { |
| 429 |
// this should prevent re-attempts to enter navigation when doing |
| 430 |
// more clicks after a move. |
| 431 |
} |
| 432 |
else { |
| 433 |
// on LMB-down or RMB-down, we don't know yet if we should propagate |
| 434 |
// it or process it. Save the event to be refired later, when it |
| 435 |
// becomes clear. reset/start move detection machine |
| 436 |
this->mousedownPos = pos; |
| 437 |
this->mouseMoveThresholdBroken = false; |
| 438 |
setupPanningPlane( |
| 439 |
viewer->getSoRenderManager()->getCamera() |
| 440 |
); // set up panningplane |
| 441 |
int& cnt = this->mousedownConsumedCount; |
| 442 |
this->mousedownConsumedEvents[cnt] |
| 443 |
= *event; // hopefully, a shallow copy is enough. There are no |
| 444 |
// pointers stored in events, apparently. Will lose a |
| 445 |
// subclass, though. |
| 446 |
cnt++; |
| 447 |
assert(cnt <= 2); |
| 448 |
if (cnt > static_cast<int>(sizeof(mousedownConsumedEvents))) { |
| 449 |
cnt = sizeof(mousedownConsumedEvents); // we are in trouble |
| 450 |
} |
| 451 |
processed = true; // just consume this event, and wait for the move |
| 452 |
// threshold to be broken to start dragging/panning |
| 453 |
} |
| 454 |
} |
| 455 |
else { // release |
| 456 |
if (button == SoMouseButtonEvent::BUTTON2 && !this->thisClickIsComplex) { |
| 457 |
if (!viewer->isEditing() && this->isPopupMenuEnabled()) { |
| 458 |
processed = true; |
| 459 |
this->openPopupMenu(event->getPosition()); |
| 460 |
} |
| 461 |
} |
| 462 |
if (!processed) { |
| 463 |
// re-synthesize all previously-consumed mouseDowns, if any. They |
| 464 |
// might have been re-synthesized already when threshold was broken. |
| 465 |
for (int i = 0; i < this->mousedownConsumedCount; i++) { |
| 466 |
inherited::processSoEvent( |
| 467 |
&(this->mousedownConsumedEvents[i]) |
| 468 |
); // simulate the previously-comsumed mousedown. |
| 469 |
} |
| 470 |
this->mousedownConsumedCount = 0; |
| 471 |
processed = inherited::processSoEvent( |
| 472 |
ev |
| 473 |
); // explicitly, just for clarity that we are sending a full click |
| 474 |
// sequence. |
| 475 |
propagated = true; |
| 476 |
} |
| 477 |
} |
| 478 |
break; |
| 479 |
case SoMouseButtonEvent::BUTTON3: // press the wheel |
| 480 |
// starts PANNING mode |
| 481 |
if (press & this->altdown) { |
| 482 |
setViewingMode(NavigationStyle::PANNING); |
| 483 |
} |
| 484 |
else if (press) { |
| 485 |
// if not PANNING then look at point |
| 486 |
setupPanningPlane(viewer->getCamera()); |
| 487 |
lookAtPoint(event->getPosition()); |
| 488 |
} |
| 489 |
processed = true; |
| 490 |
break; |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
// mouse moves - test for move threshold breaking |
| 495 |
if (evIsLoc2) { |
| 496 |
if (this->mouseMoveThresholdBroken && (this->button1down || this->button2down) |
| 497 |
&& mousedownConsumedCount > 0) { |
| 498 |
// mousemovethreshold has JUST been broken |
| 499 |
|
| 500 |
// test if we should enter navigation |
| 501 |
if ((this->button1down && !suppressLMBDrag && this->altdown) |
| 502 |
|| (this->button2down && this->altdown)) { |
| 503 |
// yes, we are entering navigation. |
| 504 |
// throw away consumed mousedowns. |
| 505 |
this->mousedownConsumedCount = 0; |
| 506 |
|
| 507 |
// start DRAGGING mode (orbit) |
| 508 |
// if not pressing left mouse button then it assumes is right mouse button |
| 509 |
// and starts ZOOMING mode |
| 510 |
saveCursorPosition(ev); |
| 511 |
setViewingMode( |
| 512 |
this->button1down ? NavigationStyle::DRAGGING : NavigationStyle::ZOOMING |
| 513 |
); |
| 514 |
processed = true; |
| 515 |
} |
| 516 |
else { |
| 517 |
// no, we are not entering navigation. |
| 518 |
// re-synthesize all previously-consumed mouseDowns, if any, and propagate |
| 519 |
// this mousemove. |
| 520 |
for (int i = 0; i < this->mousedownConsumedCount; i++) { |
| 521 |
inherited::processSoEvent( |
| 522 |
&(this->mousedownConsumedEvents[i]) |
| 523 |
); // simulate the previously-comsumed mousedown. |
| 524 |
} |
| 525 |
this->mousedownConsumedCount = 0; |
| 526 |
processed = inherited::processSoEvent(ev); // explicitly, just for clarity |
| 527 |
// that we are sending a full |
| 528 |
// click sequence. |
| 529 |
propagated = true; |
| 530 |
} |
| 531 |
} |
| 532 |
if (mousedownConsumedCount > 0) { |
| 533 |
processed = true; // if we are still deciding if it's a drag or not, consume |
| 534 |
// mouseMoves. |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
// gesture start |
| 539 |
if (evIsGesture |
| 540 |
&& /*!this->button1down &&*/ !this->button2down) { // ignore gestures when mouse |
| 541 |
// buttons are down. Button1down |
| 542 |
// check was disabled because of |
| 543 |
// wrong state after doubleclick |
| 544 |
// on sketcher constraint to |
| 545 |
// edit datum |
| 546 |
auto gesture = static_cast<const SoGestureEvent*>(ev); |
| 547 |
if (gesture->state == SoGestureEvent::SbGSStart |
| 548 |
|| gesture->state == SoGestureEvent::SbGSUpdate) { // even if we didn't get a |
| 549 |
// start, assume the first |
| 550 |
// update is a start |
| 551 |
// (sort-of fail-safe). |
| 552 |
if (type.isDerivedFrom(SoGesturePanEvent::getClassTypeId())) { |
| 553 |
setupPanningPlane(viewer->getSoRenderManager()->getCamera()); // set up |
| 554 |
// panning |
| 555 |
// plane |
| 556 |
setViewingMode(NavigationStyle::PANNING); |
| 557 |
processed = true; |
| 558 |
} |
| 559 |
else if (type.isDerivedFrom(SoGesturePinchEvent::getClassTypeId())) { |
| 560 |
setupPanningPlane(viewer->getSoRenderManager()->getCamera()); // set up |
| 561 |
// panning |
| 562 |
// plane |
| 563 |
saveCursorPosition(ev); |
| 564 |
setViewingMode(NavigationStyle::DRAGGING); |
| 565 |
processed = true; |
| 566 |
} // all other gestures - ignore! |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
// loc2 (mousemove) - ignore. |
| 571 |
|
| 572 |
} break; // end of idle and interaction |
| 573 |
case NavigationStyle::DRAGGING: |
| 574 |
case NavigationStyle::ZOOMING: |
| 575 |
case NavigationStyle::PANNING: { |
| 576 |
// actual navigation |
| 577 |
|
| 578 |
// no keyboard. |
| 579 |
|
| 580 |
// Mouse Button / Spaceball Button handling |
| 581 |
if (evIsButton) { |
| 582 |
auto const event = (const SoMouseButtonEvent*)ev; |
| 583 |
const int button = event->getButton(); |
| 584 |
switch (button) { |
| 585 |
case SoMouseButtonEvent::BUTTON1: |
| 586 |
case SoMouseButtonEvent::BUTTON2: |
| 587 |
case SoMouseButtonEvent::BUTTON3: // allows one to release button3 into |
| 588 |
// SELECTION mode |
| 589 |
if (comboAfter & BUTTON1DOWN || comboAfter & BUTTON2DOWN) { |
| 590 |
// don't leave navigation till all buttons have been released |
| 591 |
if (comboAfter & BUTTON1DOWN && comboAfter & BUTTON2DOWN) { |
| 592 |
setRotationCenter(getFocalPoint()); |
| 593 |
} |
| 594 |
else { |
| 595 |
saveCursorPosition(ev); |
| 596 |
} |
| 597 |
setViewingMode( |
| 598 |
(comboAfter & BUTTON1DOWN) ? NavigationStyle::DRAGGING |
| 599 |
: NavigationStyle::PANNING |
| 600 |
); |
| 601 |
processed = true; |
| 602 |
} |
| 603 |
else { // all buttons are released |
| 604 |
// end of dragging/panning/whatever |
| 605 |
setViewingMode(NavigationStyle::IDLE); |
| 606 |
processed = true; |
| 607 |
} // end of else (some buttons down) |
| 608 |
break; |
| 609 |
} // switch(button) |
| 610 |
} // if(evIsButton) |
| 611 |
|
| 612 |
// the essence part 1! |
| 613 |
// mouse movement into camera motion. Suppress if in gesture. Ignore until threshold is |
| 614 |
// surpassed. |
| 615 |
if (evIsLoc2 && !this->inGesture && this->mouseMoveThresholdBroken) { |
| 616 |
if (curmode == NavigationStyle::ZOOMING) { // doesn't happen |
| 617 |
this->zoomByCursor(posn, prevnormalized); |
| 618 |
processed = true; |
| 619 |
} |
| 620 |
else if (curmode == NavigationStyle::PANNING) { |
| 621 |
panCamera( |
| 622 |
viewer->getSoRenderManager()->getCamera(), |
| 623 |
ratio, |
| 624 |
this->panningplane, |
| 625 |
posn, |
| 626 |
prevnormalized |
| 627 |
); |
| 628 |
processed = true; |
| 629 |
} |
| 630 |
else if (curmode == NavigationStyle::DRAGGING) { |
| 631 |
if (comboAfter & BUTTON1DOWN && comboAfter & BUTTON2DOWN) { |
| 632 |
// two mouse buttons down - tilting! |
| 633 |
NavigationStyle::doRotate( |
| 634 |
viewer->getSoRenderManager()->getCamera(), |
| 635 |
(posn - prevnormalized)[0] * (-2), |
| 636 |
SbVec2f(0.5, 0.5) |
| 637 |
); |
| 638 |
processed = true; |
| 639 |
} |
| 640 |
else { // one mouse button - normal spinning |
| 641 |
// this will also handle the single-finger drag (there's no gesture used, |
| 642 |
// pseudomouse is enough) this->addToLog(event->getPosition(), event->getTime()); |
| 643 |
this->spin_simplified(posn, prevnormalized); |
| 644 |
processed = true; |
| 645 |
} |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
// the essence part 2! |
| 650 |
// gesture into camera motion |
| 651 |
if (evIsGesture) { |
| 652 |
auto gesture = static_cast<const SoGestureEvent*>(ev); |
| 653 |
assert(gesture); |
| 654 |
if (gesture->state == SoGestureEvent::SbGSEnd) { |
| 655 |
setViewingMode(NavigationStyle::SELECTION); |
| 656 |
processed = true; |
| 657 |
} |
| 658 |
else if (gesture->state == SoGestureEvent::SbGSUpdate) { |
| 659 |
if (type.isDerivedFrom(SoGesturePinchEvent::getClassTypeId())) { |
| 660 |
auto const event = static_cast<const SoGesturePinchEvent*>(ev); |
| 661 |
if (this->zoomAtCursor) { |
| 662 |
// this is just dealing with the pan part of pinch gesture. Taking care |
| 663 |
// of zooming to pos is done in doZoom. |
| 664 |
SbVec2f panDist = this->normalizePixelPos(event->deltaCenter.getValue()); |
| 665 |
NavigationStyle::panCamera( |
| 666 |
viewer->getSoRenderManager()->getCamera(), |
| 667 |
ratio, |
| 668 |
this->panningplane, |
| 669 |
panDist, |
| 670 |
SbVec2f(0, 0) |
| 671 |
); |
| 672 |
} |
| 673 |
NavigationStyle::doZoom( |
| 674 |
viewer->getSoRenderManager()->getCamera(), |
| 675 |
-logf(event->deltaZoom), |
| 676 |
this->normalizePixelPos(event->curCenter) |
| 677 |
); |
| 678 |
if (event->deltaAngle != 0) { |
| 679 |
NavigationStyle::doRotate( |
| 680 |
viewer->getSoRenderManager()->getCamera(), |
| 681 |
event->deltaAngle, |
| 682 |
this->normalizePixelPos(event->curCenter) |
| 683 |
); |
| 684 |
} |
| 685 |
processed = true; |
| 686 |
} |
| 687 |
if (type.isDerivedFrom(SoGesturePanEvent::getClassTypeId())) { |
| 688 |
auto const event = static_cast<const SoGesturePanEvent*>(ev); |
| 689 |
// this is just dealing with the pan part of pinch gesture. Taking care of |
| 690 |
// zooming to pos is done in doZoom. |
| 691 |
SbVec2f panDist = this->normalizePixelPos(event->deltaOffset); |
| 692 |
NavigationStyle::panCamera( |
| 693 |
viewer->getSoRenderManager()->getCamera(), |
| 694 |
ratio, |
| 695 |
this->panningplane, |
| 696 |
panDist, |
| 697 |
SbVec2f(0, 0) |
| 698 |
); |
| 699 |
processed = true; |
| 700 |
} |
| 701 |
} |
| 702 |
else { |
| 703 |
// shouldn't happen. Gestures are not expected to start in the middle of |
| 704 |
// navigation. we'll consume it, without reacting. |
| 705 |
processed = true; |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
} break; // end of actual navigation |
| 710 |
case NavigationStyle::SEEK_WAIT_MODE: { |
| 711 |
if (evIsButton) { |
| 712 |
auto const event = (const SoMouseButtonEvent*)ev; |
| 713 |
const int button = event->getButton(); |
| 714 |
const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false; |
| 715 |
if (button == SoMouseButtonEvent::BUTTON1 && press) { |
| 716 |
this->seekToPoint(pos); // implicitly calls interactiveCountInc() |
| 717 |
this->setViewingMode(NavigationStyle::SEEK_MODE); |
| 718 |
processed = true; |
| 719 |
} |
| 720 |
} |
| 721 |
}; // not end of SEEK_WAIT_MODE. Fall through by design!!! |
| 722 |
/* FALLTHRU */ |
| 723 |
case NavigationStyle::SPINNING: |
| 724 |
case NavigationStyle::SEEK_MODE: { |
| 725 |
// animation modes |
| 726 |
if (!processed) { |
| 727 |
if (evIsButton || evIsGesture || evIsKeyboard || evIsLoc3) { |
| 728 |
setViewingMode(NavigationStyle::SELECTION); |
| 729 |
} |
| 730 |
} |
| 731 |
} break; // end of animation modes |
| 732 |
case NavigationStyle::BOXZOOM: |
| 733 |
default: |
| 734 |
// all the rest - will be pass on to inherited, later. |
| 735 |
break; |
| 736 |
} |
| 737 |
|
| 738 |
if (!processed && !propagated) { |
| 739 |
processed = inherited::processSoEvent(ev); |
| 740 |
propagated = true; |
| 741 |
} |
| 742 |
|
| 743 |
//-----------------------end of event handling--------------------- |
| 744 |
finalize: |
| 745 |
return processed; |
| 746 |
} |