Debian Codegraph @

src/Gui/Navigation/GestureNavigationStyle.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2019 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 /*
25 *A few notes on this style. (by DeepSOIC)
26 *
27 * In this style, LMB serves dual purpose. It is selecting objects, as well as
28 * spinning the view. The trick that enables it is to consume mouse events
29 * before move threshold is detected, and refire the events if the mouse was
30 * released but not moved.
31 *
32 * This navigation style does not exactly follow the structure of other
33 * navigation styles, it does not fill many of the global variables defined in
34 * NavigationStyle.
35 *
36 * It uses a statemachine based on boost::statechart to simplify differences in
37 * event handling depending on mode.
38 *
39 * Dealing with touchscreen gestures with Qt5 on Windows is a pain in the arse.
40 *
41 * For pinch gesture, Qt5 starts generating mouse input as soon as the first
42 * finger lands on the screen. As the second finger touches, gesture events
43 * begin to arrive. But in the process, more synthetic mouse input keeps
44 * coming, sometimes including RMB press. This mouse input is usually properly
45 * terminated by LMB and RMB release events, but they don't always come;
46 * proofing the logic against this inconsistency was quite a challenge.
47 *
48 * Tap-and-hold was yet another terrible beast. Again, as soon as the finger
49 * touches the screen, LMB press comes in. Then, after the finger is released,
50 * RMB press comes. This one is usually complemented by all release events.
51 * However, with tap-hold-move-release sequence, RMB release event does not
52 * arrive.
53 *
54 * So, to avoid entering Tilt mode, the style implements its own tap-and-hold
55 * detection, and a special Pan state for the state machine - StickyPanState.
56 *
57 * This style wasn't tested with spacemouse during development (I don't have one).
58 *
59 * See also GestureNavigationStyle-state-machine-diagram.docx for a crude
60 * diagram of the state machine.
61 *
62 */
63
64
65 #include <Inventor/SoFullPath.h>
66 #include <Inventor/SoPickedPoint.h>
67 #include <Inventor/actions/SoRayPickAction.h>
68 #include <Inventor/draggers/SoDragger.h>
69 #include <QApplication>
70
71
72 #include <QTapAndHoldGesture>
73
74 #include <FCConfig.h>
75
76 #include <App/Application.h>
77 #include <Base/Interpreter.h>
78 #include <Base/Console.h>
79
80 #include "GestureNavigationStyle.h"
81 #include "Application.h"
82 #include "SoTouchEvents.h"
83 #include "View3DInventorViewer.h"
84
85 #include <boost/statechart/custom_reaction.hpp>
86 #include <boost/statechart/state_machine.hpp>
87 #include <boost/statechart/state.hpp>
88
89
90 namespace sc = boost::statechart;
91 #define NS Gui::GestureNavigationStyle
92
93 namespace Gui
94 {
95
96 class NS::Event: public sc::event<NS::Event>
97 {
98 public:
99 Event()
100 : flags(new Flags)
101 {}
102 virtual ~Event() = default;
103
104 void log() const
105 {
106 if (isPress(1)) {
107 Base::Console().log("button1 press ");
108 }
109 if (isPress(2)) {
110 Base::Console().log("button2 press ");
111 }
112 if (isPress(3)) {
113 Base::Console().log("button3 press ");
114 }
115 if (isRelease(1)) {
116 Base::Console().log("button1 release ");
117 }
118 if (isRelease(2)) {
119 Base::Console().log("button2 release ");
120 }
121 if (isRelease(3)) {
122 Base::Console().log("button3 release ");
123 }
124 if (isMouseButtonEvent()) {
125 Base::Console().log("%x", modifiers);
126 }
127 if (isGestureEvent()) {
128 Base::Console().log("Gesture ");
129 switch (asGestureEvent()->state) {
130 case SoGestureEvent::SbGSStart:
131 Base::Console().log("start ");
132 break;
133 case SoGestureEvent::SbGSEnd:
134 Base::Console().log("end ");
135 break;
136 case SoGestureEvent::SbGSUpdate:
137 Base::Console().log("data ");
138 break;
139 default:
140 Base::Console().log("??? ");
141 }
142
143 Base::Console().log(inventor_event->getTypeId().getName().getString());
144 }
145 if (isMouseButtonEvent() || isGestureEvent()) {
146 Base::Console().log(
147 "(%i,%i)\n",
148 inventor_event->getPosition()[0],
149 inventor_event->getPosition()[1]
150 );
151 }
152 }
153
154 // cast shortcuts
155 bool isMouseButtonEvent() const
156 {
157 return this->inventor_event->isOfType(SoMouseButtonEvent::getClassTypeId());
158 }
159 const SoMouseButtonEvent* asMouseButtonEvent() const
160 {
161 return static_cast<const SoMouseButtonEvent*>(this->inventor_event);
162 }
163 bool isPress(int button_index) const
164 {
165 if (!isMouseButtonEvent()) {
166 return false;
167 }
168 int sobtn = SoMouseButtonEvent::BUTTON1 + button_index - 1;
169 return asMouseButtonEvent()->getButton() == sobtn
170 && asMouseButtonEvent()->getState() == SoMouseButtonEvent::DOWN;
171 }
172 bool isRelease(int button_index) const
173 {
174 if (!isMouseButtonEvent()) {
175 return false;
176 }
177 int sobtn = SoMouseButtonEvent::BUTTON1 + button_index - 1;
178 return asMouseButtonEvent()->getButton() == sobtn
179 && asMouseButtonEvent()->getState() == SoMouseButtonEvent::UP;
180 }
181 bool isKeyboardEvent() const
182 {
183 return this->inventor_event->isOfType(SoKeyboardEvent::getClassTypeId());
184 }
185 const SoKeyboardEvent* asKeyboardEvent() const
186 {
187 return static_cast<const SoKeyboardEvent*>(this->inventor_event);
188 }
189 bool isLocation2Event() const
190 {
191 return this->inventor_event->isOfType(SoLocation2Event::getClassTypeId());
192 }
193 const SoLocation2Event* asLocation2Event() const
194 {
195 return static_cast<const SoLocation2Event*>(this->inventor_event);
196 }
197 bool isMotion3Event() const
198 {
199 return this->inventor_event->isOfType(SoMotion3Event::getClassTypeId());
200 }
201 bool isGestureEvent() const
202 {
203 return this->inventor_event->isOfType(SoGestureEvent::getClassTypeId());
204 }
205 const SoGestureEvent* asGestureEvent() const
206 {
207 return static_cast<const SoGestureEvent*>(this->inventor_event);
208 }
209 bool isGestureActive() const
210 {
211 if (!isGestureEvent()) {
212 return false;
213 }
214 if (asGestureEvent()->state == SoGestureEvent::SbGSStart
215 || asGestureEvent()->state == SoGestureEvent::SbGSUpdate) {
216 return true;
217 }
218 else {
219 return false;
220 }
221 }
222
223 public:
224 enum
225 {
226 // bits: 0-shift-ctrl-alt-0-lmb-mmb-rmb
227 BUTTON1DOWN = 0x00000100,
228 BUTTON2DOWN = 0x00000001,
229 BUTTON3DOWN = 0x00000010,
230 CTRLDOWN = 0x00100000,
231 SHIFTDOWN = 0x01000000,
232 ALTDOWN = 0x00010000,
233 MASKBUTTONS = BUTTON1DOWN | BUTTON2DOWN | BUTTON3DOWN,
234 MASKMODIFIERS = CTRLDOWN | SHIFTDOWN | ALTDOWN
235 };
236
237 public:
238 const SoEvent* inventor_event {nullptr};
239 unsigned int modifiers {0};
240 unsigned int mbstate() const
241 {
242 return modifiers & MASKBUTTONS;
243 }
244 unsigned int kbdstate() const
245 {
246 return modifiers & MASKMODIFIERS;
247 }
248
249 struct Flags
250 {
251 bool processed = false; // the value to be returned by processSoEvent.
252 bool propagated = false; // flag that the event had been passed to superclass
253 };
254 std::shared_ptr<Flags> flags;
255 // storing these values as a separate unit allows one to effectively write to
256 // const object. Statechart passes all events as const, unfortunately, so
257 // this is a workaround. I've considered casting away const instead, but
258 // the internet seems to have mixed opinion if it's undefined behavior or
259 // not. Also, it seems, statechart can copy the event internally. --DeepSOIC
260 };
261
262 //------------------------------state machine ---------------------------
263
264 class NS::NaviMachine: public sc::state_machine<NS::NaviMachine, NS::IdleState>
265 {
266 public:
267 using superclass = sc::state_machine<NS::NaviMachine, NS::IdleState>;
268
269 explicit NaviMachine(NS& ns)
270 : ns(ns)
271 {}
272 NS& ns;
273
274 public:
275 virtual void processEvent(NS::Event& ev)
276 {
277 if (ns.logging) {
278 ev.log();
279 }
280 this->process_event(ev);
281 }
282 };
283
284 class NS::IdleState: public sc::state<NS::IdleState, NS::NaviMachine>
285 {
286 public:
287 using reactions = sc::custom_reaction<NS::Event>;
288
289 explicit IdleState(my_context ctx)
290 : my_base(ctx)
291 {
292 auto& ns = this->outermost_context().ns;
293 ns.setViewingMode(NavigationStyle::IDLE);
294 if (ns.logging) {
295 Base::Console().log(" -> IdleState\n");
296 }
297 }
298 virtual ~IdleState() = default;
299
300 sc::result react(const NS::Event& ev)
301 {
302 auto& ns = this->outermost_context().ns;
303
304 auto posn = ns.normalizePixelPos(ev.inventor_event->getPosition());
305
306 // special handling for some special states of viewer
307 switch (ns.getViewingMode()) {
308 case NavigationStyle::SEEK_WAIT_MODE: {
309 if (ev.isPress(1)) {
310 ns.seekToPoint(ev.inventor_event->getPosition()); // implicitly calls
311 // interactiveCountInc()
312 ns.setViewingMode(NavigationStyle::SEEK_MODE);
313 ev.flags->processed = true;
314 return transit<NS::AwaitingReleaseState>();
315 }
316 }; // not end of SEEK_WAIT_MODE. Fall through by design!!!
317 /* FALLTHRU */
318 case NavigationStyle::SPINNING:
319 case NavigationStyle::SEEK_MODE: {
320 // animation modes
321 if (!ev.flags->processed) {
322 if (ev.isMouseButtonEvent()) {
323 ev.flags->processed = true;
324 return transit<NS::AwaitingReleaseState>();
325 }
326 else if (ev.isGestureEvent() || ev.isKeyboardEvent() || ev.isMotion3Event()) {
327 ns.setViewingMode(NavigationStyle::IDLE);
328 }
329 }
330 } break; // end of animation modes
331 case BOXZOOM:
332 return forward_event();
333 }
334
335 // testing for draggers
336 if (ev.isPress(1) && ev.mbstate() == 0x100) {
337 if (ns.isDraggerUnderCursor(ev.inventor_event->getPosition())) {
338 return transit<NS::InteractState>();
339 }
340 }
341
342 // left and right clicks - special handling, postpone the events
343 if ((ev.isPress(1) && ev.mbstate() == 0x100) || (ev.isPress(2) && ev.mbstate() == 0x001)) {
344 ns.postponedEvents.post(ev);
345 ev.flags->processed = true;
346 return transit<NS::AwaitingMoveState>();
347 }
348
349 // MMB click
350 if (ev.isPress(3) && ev.mbstate() == 0x010) {
351 ev.flags->processed = true;
352 ns.setupPanningPlane(ns.viewer->getCamera());
353 ns.lookAtPoint(ev.inventor_event->getPosition());
354 return transit<NS::AwaitingReleaseState>();
355 }
356
357 // touchscreen gestures
358 if (ev.isGestureActive()) {
359 ev.flags->processed = true;
360 return transit<NS::GestureState>();
361 }
362
363 // keyboard
364 if (ev.isKeyboardEvent()) {
365 auto const& kbev = ev.asKeyboardEvent();
366 ev.flags->processed = true;
367 bool press = (kbev->getState() == SoKeyboardEvent::DOWN);
368 switch (kbev->getKey()) {
369 case SoKeyboardEvent::H:
370 // Disable H key in editing mode because of conflict with sketcher
371 if (!ns.viewer->isEditing() && !press) {
372 ns.setupPanningPlane(ns.viewer->getCamera());
373 ns.lookAtPoint(kbev->getPosition());
374 }
375 break;
376 case SoKeyboardEvent::PAGE_UP:
377 if (!press) {
378 ns.doZoom(ns.viewer->getSoRenderManager()->getCamera(), ns.getDelta(), posn);
379 }
380 break;
381 case SoKeyboardEvent::PAGE_DOWN:
382 if (!press) {
383 ns.doZoom(ns.viewer->getSoRenderManager()->getCamera(), -ns.getDelta(), posn);
384 }
385 break;
386 default:
387 ev.flags->processed = false;
388 }
389 }
390
391 return forward_event();
392 }
393 };
394
395 class NS::AwaitingMoveState: public sc::state<NS::AwaitingMoveState, NS::NaviMachine>
396 {
397 public:
398 using reactions = sc::custom_reaction<NS::Event>;
399
400 private:
401 SbVec2s base_pos;
402 SbTime since; // the time of mouse-down event
403 int hold_timeout; // in milliseconds
404
405 public:
406 explicit AwaitingMoveState(my_context ctx)
407 : my_base(ctx)
408 {
409 auto& ns = this->outermost_context().ns;
410 if (ns.logging) {
411 Base::Console().log(" -> AwaitingMoveState\n");
412 }
413 ns.setViewingMode(NavigationStyle::IDLE);
414 this->base_pos
415 = static_cast<const NS::Event*>(this->triggering_event())->inventor_event->getPosition();
416 this->since
417 = static_cast<const NS::Event*>(this->triggering_event())->inventor_event->getTime();
418
419 ns.mouseMoveThreshold = App::GetApplication()
420 .GetParameterGroupByPath("User parameter:BaseApp/Preferences/View")
421 ->GetInt("GestureMoveThreshold", ns.mouseMoveThreshold);
422
423 this->hold_timeout = int(double(QTapAndHoldGesture::timeout()) * 0.9);
424 this->hold_timeout = App::GetApplication()
425 .GetParameterGroupByPath("User parameter:BaseApp/Preferences/View")
426 ->GetInt("GestureTapHoldTimeout", this->hold_timeout);
427 if (this->hold_timeout == 0) {
428 this->hold_timeout = 650; // a fail-safe
429 }
430 QTapAndHoldGesture::setTimeout(int(double(this->hold_timeout) / 0.9));
431 // Why *0.9? We need tap-and-hold detection to be slightly faster than
432 // Qt's one, to filter out spurious events. It'd be better to disable
433 // Tap-and-hold altogether, but my attempts to use ungrabGesture and
434 // unregisterRecognizer routines failed to affect anything.
435 }
436 virtual ~AwaitingMoveState()
437 {
438 // always clear postponed events when leaving this state.
439 this->outermost_context().ns.postponedEvents.discardAll();
440 }
441
442 sc::result react(const NS::Event& ev)
443 {
444 auto& ns = this->outermost_context().ns;
445
446 /// refire(): forwards all postponed events + this event
447 auto refire = [&] {
448 ns.postponedEvents.forwardAll();
449 ev.flags->processed = ns.processSoEvent_bypass(ev.inventor_event);
450 ev.flags->propagated = true;
451 };
452
453 bool long_click = (ev.inventor_event->getTime() - this->since).getValue() * 1000.0
454 >= this->hold_timeout;
455
456 // this state consumes all mouse events.
457 ev.flags->processed = ev.isMouseButtonEvent() || ev.isLocation2Event();
458
459 // right-click
460 if (ev.isRelease(2) && ev.mbstate() == 0 && !ns.viewer->isEditing()
461 && ns.isPopupMenuEnabled()) {
462 ns.openPopupMenu(ev.inventor_event->getPosition());
463 return transit<NS::IdleState>();
464 }
465
466 // roll gestures
467 // direction is determined at the moment the second button is pressed.
468 if (ev.mbstate() == 0x101) {
469 if (ev.isPress(1)) {
470 ns.rollDir = -1;
471 }
472 if (ev.isPress(2)) {
473 ns.rollDir = +1;
474 }
475 }
476 // The roll gesture is fired when one of the two buttons in then released.
477 if ((ev.isRelease(1) && ev.mbstate() == 0x001) || (ev.isRelease(2) && ev.mbstate() == 0x100)) {
478 ns.onRollGesture(ns.rollDir);
479 return transit<NS::AwaitingReleaseState>();
480 }
481
482 if (ev.isMouseButtonEvent() && ev.mbstate() == 0) {
483 // all buttons released
484 if (long_click) {
485 // emulate RMB-click
486 ns.openPopupMenu(ev.inventor_event->getPosition());
487 return transit<NS::IdleState>();
488 }
489 else {
490 // refire all events && return to idle state
491 ns.setViewingMode(NavigationStyle::SELECTION);
492 refire();
493 return transit<NS::IdleState>();
494 }
495 }
496 if (ev.isPress(3)) {
497 // mmb pressed, exit navigation
498 refire();
499 return transit<NS::IdleState>();
500 }
501 if (ev.isMouseButtonEvent() /* and still not processed*/) {
502 ns.postponedEvents.post(ev);
503 }
504 if (ev.isLocation2Event()) {
505 auto mv = ev.inventor_event->getPosition() - this->base_pos;
506 if (SbVec2f(mv).length() > ns.mouseMoveThreshold) {
507 // mouse moved while buttons are held. decide how to navigate...
508 switch (ev.mbstate()) {
509 case 0x100: {
510 if (!long_click) {
511 bool alt = ev.modifiers & NS::Event::ALTDOWN;
512 bool allowSpin = alt == ns.is2DViewing();
513 if (allowSpin) {
514 return transit<NS::RotateState>();
515 }
516 else {
517 refire();
518 return transit<NS::IdleState>();
519 }
520 }
521 else {
522 return transit<NS::StickyPanState>();
523 }
524 } break;
525 case 0x001:
526 return transit<NS::PanState>();
527 break;
528 case (0x101):
529 return transit<NS::TiltState>();
530 break;
531 default:
532 // MMB was held? refire all events.
533 refire();
534 return transit<NS::IdleState>();
535 }
536 }
537 }
538 if (ev.isGestureActive()) {
539 ev.flags->processed = true;
540 return transit<NS::GestureState>();
541 }
542 return forward_event();
543 }
544 };
545
546 class NS::RotateState: public sc::state<NS::RotateState, NS::NaviMachine>
547 {
548 public:
549 using reactions = sc::custom_reaction<NS::Event>;
550
551 private:
552 SbVec2s base_pos;
553
554 public:
555 explicit RotateState(my_context ctx)
556 : my_base(ctx)
557 {
558 auto& ns = this->outermost_context().ns;
559 const auto inventorEvent
560 = static_cast<const NS::Event*>(this->triggering_event())->inventor_event;
561 ns.saveCursorPosition(inventorEvent);
562 ns.setViewingMode(NavigationStyle::DRAGGING);
563 this->base_pos = inventorEvent->getPosition();
564 if (ns.logging) {
565 Base::Console().log(" -> RotateState\n");
566 }
567 }
568 virtual ~RotateState() = default;
569
570 sc::result react(const NS::Event& ev)
571 {
572 if (ev.isMouseButtonEvent()) {
573 ev.flags->processed = true;
574 if (ev.mbstate() == 0x101) {
575 return transit<NS::TiltState>();
576 }
577 if (ev.mbstate() == 0) {
578 return transit<NS::IdleState>();
579 }
580 }
581 if (ev.isLocation2Event()) {
582 ev.flags->processed = true;
583 SbVec2s pos = ev.inventor_event->getPosition();
584 auto& ns = this->outermost_context().ns;
585 ns.spin_simplified(ns.normalizePixelPos(pos), ns.normalizePixelPos(this->base_pos));
586 this->base_pos = pos;
587 }
588 return forward_event();
589 }
590 };
591
592 class NS::PanState: public sc::state<NS::PanState, NS::NaviMachine>
593 {
594 public:
595 using reactions = sc::custom_reaction<NS::Event>;
596
597 private:
598 SbVec2s base_pos;
599 float ratio;
600
601 public:
602 explicit PanState(my_context ctx)
603 : my_base(ctx)
604 {
605 auto& ns = this->outermost_context().ns;
606 ns.setViewingMode(NavigationStyle::PANNING);
607 this->base_pos
608 = static_cast<const NS::Event*>(this->triggering_event())->inventor_event->getPosition();
609 if (ns.logging) {
610 Base::Console().log(" -> PanState\n");
611 }
612 this->ratio = ns.viewer->getSoRenderManager()->getViewportRegion().getViewportAspectRatio();
613 ns.setupPanningPlane(ns.viewer->getSoRenderManager()->getCamera()); // set up panningplane
614 }
615 virtual ~PanState() = default;
616
617 sc::result react(const NS::Event& ev)
618 {
619 if (ev.isMouseButtonEvent()) {
620 ev.flags->processed = true;
621 if (ev.mbstate() == 0x101) {
622 return transit<NS::TiltState>();
623 }
624 if (ev.mbstate() == 0) {
625 return transit<NS::IdleState>();
626 }
627 }
628 if (ev.isLocation2Event()) {
629 ev.flags->processed = true;
630 SbVec2s pos = ev.inventor_event->getPosition();
631 auto& ns = this->outermost_context().ns;
632 ns.panCamera(
633 ns.viewer->getSoRenderManager()->getCamera(),
634 this->ratio,
635 ns.panningplane,
636 ns.normalizePixelPos(pos),
637 ns.normalizePixelPos(this->base_pos)
638 );
639 this->base_pos = pos;
640 }
641 return forward_event();
642 }
643 };
644
645 class NS::StickyPanState: public sc::state<NS::StickyPanState, NS::NaviMachine>
646 {
647 public:
648 using reactions = sc::custom_reaction<NS::Event>;
649
650 private:
651 SbVec2s base_pos;
652 float ratio;
653
654 public:
655 explicit StickyPanState(my_context ctx)
656 : my_base(ctx)
657 {
658 auto& ns = this->outermost_context().ns;
659 ns.setViewingMode(NavigationStyle::PANNING);
660 this->base_pos
661 = static_cast<const NS::Event*>(this->triggering_event())->inventor_event->getPosition();
662 if (ns.logging) {
663 Base::Console().log(" -> StickyPanState\n");
664 }
665 this->ratio = ns.viewer->getSoRenderManager()->getViewportRegion().getViewportAspectRatio();
666 ns.setupPanningPlane(ns.viewer->getSoRenderManager()->getCamera()); // set up panningplane
667 }
668 virtual ~StickyPanState()
669 {
670 auto& ns = this->outermost_context().ns;
671 ns.button2down = false; // a workaround for dealing with Qt not sending UP event after a
672 // tap-hold-drag sequence.
673 }
674
675 sc::result react(const NS::Event& ev)
676 {
677 if (ev.isMouseButtonEvent()) {
678 ev.flags->processed = true;
679 if (ev.isRelease(1)) {
680 return transit<NS::IdleState>();
681 }
682 }
683 if (ev.isLocation2Event()) {
684 ev.flags->processed = true;
685 SbVec2s pos = ev.inventor_event->getPosition();
686 auto& ns = this->outermost_context().ns;
687 ns.panCamera(
688 ns.viewer->getSoRenderManager()->getCamera(),
689 this->ratio,
690 ns.panningplane,
691 ns.normalizePixelPos(pos),
692 ns.normalizePixelPos(this->base_pos)
693 );
694 this->base_pos = pos;
695 }
696 return forward_event();
697 }
698 };
699
700 class NS::TiltState: public sc::state<NS::TiltState, NS::NaviMachine>
701 {
702 public:
703 using reactions = sc::custom_reaction<NS::Event>;
704
705 private:
706 SbVec2s base_pos;
707
708 public:
709 explicit TiltState(my_context ctx)
710 : my_base(ctx)
711 {
712 auto& ns = this->outermost_context().ns;
713 ns.setRotationCenter(ns.getFocalPoint());
714 ns.setViewingMode(NavigationStyle::DRAGGING);
715 this->base_pos
716 = static_cast<const NS::Event*>(this->triggering_event())->inventor_event->getPosition();
717 if (ns.logging) {
718 Base::Console().log(" -> TiltState\n");
719 }
720 ns.setupPanningPlane(ns.viewer->getSoRenderManager()->getCamera()); // set up panningplane
721 }
722 virtual ~TiltState() = default;
723
724 sc::result react(const NS::Event& ev)
725 {
726 if (ev.isMouseButtonEvent()) {
727 ev.flags->processed = true;
728 if (ev.mbstate() == 0x001) {
729 return transit<NS::PanState>();
730 }
731 if (ev.mbstate() == 0x100) {
732 return transit<NS::RotateState>();
733 }
734 if (ev.mbstate() == 0) {
735 return transit<NS::IdleState>();
736 }
737 }
738 if (ev.isLocation2Event()) {
739 ev.flags->processed = true;
740 auto& ns = this->outermost_context().ns;
741 SbVec2s pos = ev.inventor_event->getPosition();
742 float dx = (ns.normalizePixelPos(pos) - ns.normalizePixelPos(base_pos))[0];
743 ns.doRotate(ns.viewer->getSoRenderManager()->getCamera(), dx * (-2), SbVec2f(0.5, 0.5));
744 this->base_pos = pos;
745 }
746 return forward_event();
747 }
748 };
749
750
751 class NS::GestureState: public sc::state<NS::GestureState, NS::NaviMachine>
752 {
753 public:
754 using reactions = sc::custom_reaction<NS::Event>;
755
756 private:
757 SbVec2s base_pos;
758 float ratio;
759 bool enableTilt = false;
760
761 public:
762 explicit GestureState(my_context ctx)
763 : my_base(ctx)
764 {
765 auto& ns = this->outermost_context().ns;
766 ns.setViewingMode(NavigationStyle::PANNING);
767 this->base_pos
768 = static_cast<const NS::Event*>(this->triggering_event())->inventor_event->getPosition();
769 if (ns.logging) {
770 Base::Console().log(" -> GestureState\n");
771 }
772 ns.setupPanningPlane(ns.viewer->getSoRenderManager()->getCamera()); // set up panningplane
773 this->ratio = ns.viewer->getSoRenderManager()->getViewportRegion().getViewportAspectRatio();
774 enableTilt = !(App::GetApplication()
775 .GetParameterGroupByPath("User parameter:BaseApp/Preferences/View")
776 ->GetBool("DisableTouchTilt", true));
777 }
778 virtual ~GestureState()
779 {
780 auto& ns = this->outermost_context().ns;
781 // a workaround for Qt not always sending release evends during touchecreen gestures on Windows
782 ns.button1down = false;
783 ns.button2down = false;
784 }
785
786 sc::result react(const NS::Event& ev)
787 {
788 auto& ns = this->outermost_context().ns;
789 if (ev.isMouseButtonEvent()) {
790 ev.flags->processed = true;
791 if (ev.mbstate() == 0) {
792 // a fail-safe: if gesture end event doesn't arrive, a mouse click should be able to
793 // stop this mode.
794 Base::Console().warning("leaving gesture state by mouse-click (fail-safe)\n");
795 return transit<NS::IdleState>();
796 }
797 }
798 if (ev.isLocation2Event()) {
799 // consume all mouse events that Qt fires during the gesture (stupid Qt, so far it only
800 // causes trouble)
801 ev.flags->processed = true;
802 }
803 if (ev.isGestureEvent()) {
804 ev.flags->processed = true;
805 if (ev.asGestureEvent()->state == SoGestureEvent::SbGSEnd) {
806 return transit<NS::IdleState>();
807 }
808 else if (ev.asGestureEvent()->state == SoGestureEvent::SbGsCanceled) {
809 // should maybe undo the camera change caused by gesture events received so far...
810 return transit<NS::IdleState>();
811 //} else if (ev.asGestureEvent()->state == SoGestureEvent::SbGSStart){
812 // //ignore?
813 }
814 else if (ev.inventor_event->isOfType(SoGesturePanEvent::getClassTypeId())) {
815 auto const& pangesture = static_cast<const SoGesturePanEvent*>(ev.inventor_event);
816 SbVec2f panDist = ns.normalizePixelPos(pangesture->deltaOffset);
817 ns.panCamera(
818 ns.viewer->getSoRenderManager()->getCamera(),
819 ratio,
820 ns.panningplane,
821 panDist,
822 SbVec2f(0, 0)
823 );
824 }
825 else if (ev.inventor_event->isOfType(SoGesturePinchEvent::getClassTypeId())) {
826 const auto pinch = static_cast<const SoGesturePinchEvent*>(ev.inventor_event);
827 SbVec2f panDist = ns.normalizePixelPos(pinch->deltaCenter.getValue());
828 ns.panCamera(
829 ns.viewer->getSoRenderManager()->getCamera(),
830 ratio,
831 ns.panningplane,
832 panDist,
833 SbVec2f(0, 0)
834 );
835 ns.doZoom(
836 ns.viewer->getSoRenderManager()->getCamera(),
837 -logf(float(pinch->deltaZoom)),
838 ns.normalizePixelPos(pinch->curCenter)
839 );
840 if (pinch->deltaAngle != 0.0 && enableTilt) {
841 ns.doRotate(
842 ns.viewer->getSoRenderManager()->getCamera(),
843 float(pinch->deltaAngle),
844 ns.normalizePixelPos(pinch->curCenter)
845 );
846 }
847 }
848 else {
849 // unknown gesture
850 ev.flags->processed = false;
851 }
852 }
853 return forward_event();
854 }
855 };
856
857 class NS::AwaitingReleaseState: public sc::state<NS::AwaitingReleaseState, NS::NaviMachine>
858 {
859 public:
860 using reactions = sc::custom_reaction<NS::Event>;
861
862 public:
863 explicit AwaitingReleaseState(my_context ctx)
864 : my_base(ctx)
865 {
866 auto& ns = this->outermost_context().ns;
867 if (ns.logging) {
868 Base::Console().log(" -> AwaitingReleaseState\n");
869 }
870 }
871 virtual ~AwaitingReleaseState() = default;
872
873 sc::result react(const NS::Event& ev)
874 {
875 auto& ns = this->outermost_context().ns;
876
877 if (ev.isMouseButtonEvent()) {
878 ev.flags->processed = true;
879 if (ev.mbstate() == 0) {
880 return transit<NS::IdleState>();
881 }
882 }
883
884 // roll gestures (same as in AwaitingMoveState, but how to merge them - I don't know
885 // --DeepSOIC) direction is determined at the moment the second button is pressed.
886 if (ev.mbstate() == 0x101) {
887 if (ev.isPress(1)) {
888 ns.rollDir = -1;
889 }
890 if (ev.isPress(2)) {
891 ns.rollDir = +1;
892 }
893 }
894 // The roll gesture is fired when one of the two buttons in then released.
895 if ((ev.isRelease(1) && ev.mbstate() == 0x001) || (ev.isRelease(2) && ev.mbstate() == 0x100)) {
896 ns.onRollGesture(ns.rollDir);
897 }
898
899 if (ev.isLocation2Event()) {
900 ev.flags->processed = true;
901 }
902 if (ev.isGestureActive()) {
903 ev.flags->processed = true;
904 // another gesture can start...
905 return transit<NS::GestureState>();
906 }
907 return forward_event();
908 }
909 };
910
911 class NS::InteractState: public sc::state<NS::InteractState, NS::NaviMachine>
912 {
913 public:
914 using reactions = sc::custom_reaction<NS::Event>;
915
916 public:
917 explicit InteractState(my_context ctx)
918 : my_base(ctx)
919 {
920 auto& ns = this->outermost_context().ns;
921 ns.setViewingMode(NavigationStyle::INTERACT);
922 if (ns.logging) {
923 Base::Console().log(" -> InteractState\n");
924 }
925 }
926 virtual ~InteractState() = default;
927
928 sc::result react(const NS::Event& ev)
929 {
930 if (ev.isMouseButtonEvent()) {
931 ev.flags->processed = false; // feed all events to the dragger/whatever
932 if (ev.mbstate() == 0) { // all buttons released?
933 return transit<NS::IdleState>();
934 }
935 }
936 return forward_event();
937 }
938 };
939
940 //------------------------------/state machine ---------------------------
941
942
943 /* TRANSLATOR Gui::GestureNavigationStyle */
944
945 TYPESYSTEM_SOURCE(Gui::GestureNavigationStyle, Gui::UserNavigationStyle)
946
947
948 GestureNavigationStyle::GestureNavigationStyle()
949 : naviMachine(new NS::NaviMachine(*this))
950 , postponedEvents(*this)
951 {
952 this->logging = App::GetApplication()
953 .GetParameterGroupByPath("User parameter:BaseApp/Preferences/View")
954 ->GetBool("NavigationDebug");
955 mouseMoveThreshold = QApplication::startDragDistance();
956 naviMachine->initiate();
957 }
958
959 GestureNavigationStyle::~GestureNavigationStyle() = default;
960
961 const char* GestureNavigationStyle::mouseButtons(ViewerMode mode)
962 {
963 switch (mode) {
964 case NavigationStyle::SELECTION:
965 return QT_TR_NOOP("Tap OR click left mouse button.");
966 case NavigationStyle::PANNING:
967 return QT_TR_NOOP("Drag screen with two fingers OR press right mouse button.");
968 case NavigationStyle::DRAGGING:
969 return QT_TR_NOOP(
970 "Drag screen with one finger OR press left mouse button. In Sketcher and other "
971 "edit modes, hold Alt in addition."
972 );
973 case NavigationStyle::ZOOMING:
974 return QT_TR_NOOP(
975 "Pinch (place two fingers on the screen and drag them apart from or towards each "
976 "other) OR scroll mouse wheel OR PgUp/PgDown on keyboard."
977 );
978 default:
979 return "No description";
980 }
981 }
982
983
984 SbBool GestureNavigationStyle::processSoEvent(const SoEvent* const ev)
985 {
986 // Events when in "ready-to-seek" mode are ignored, except those
987 // which influence the seek mode itself -- these are handled further
988 // up the inheritance hierarchy.
989 if (this->isSeekMode()) {
990 return superclass::processSoEvent(ev);
991 }
992 // Switch off viewing mode (Bug #0000911)
993 if (!this->isSeekMode() && !this->isAnimating() && this->isViewing()) {
994 this->setViewing(false); // by default disable viewing mode to render the scene
995 }
996
997 NS::Event smev;
998 smev.inventor_event = ev;
999
1000 // mode-independent spaceball/joystick handling
1001 if (ev->isOfType(SoMotion3Event::getClassTypeId())) {
1002 smev.flags->processed = true;
1003 this->processMotionEvent(static_cast<const SoMotion3Event*>(ev));
1004 return true;
1005 }
1006
1007 // give the nodes in the foreground root the chance to handle events (e.g color bar)
1008 if (!viewer->isEditing()) {
1009 bool processed = handleEventInForeground(ev);
1010 if (processed) {
1011 return true;
1012 }
1013 }
1014
1015 if ((smev.isRelease(1) && !this->button1down) || (smev.isRelease(2) && !this->button2down)
1016 || (smev.isRelease(3) && !this->button3down)) {
1017 // a button release event cane, but we didn't see the corresponding down
1018 // event. Discard it. This discarding is relied upon in some hacks to
1019 // overcome buggy synthetic mouse input coming from Qt when doing
1020 // touchecteen gestures.
1021 return true;
1022 }
1023
1024
1025 if (smev.isMouseButtonEvent()) {
1026 const int button = smev.asMouseButtonEvent()->getButton();
1027 const SbBool press // the button was pressed (if false -> released)
1028 = smev.asMouseButtonEvent()->getState() == SoButtonEvent::DOWN ? true : false;
1029 switch (button) {
1030 case SoMouseButtonEvent::BUTTON1:
1031 this->button1down = press;
1032 break;
1033 case SoMouseButtonEvent::BUTTON2:
1034 this->button2down = press;
1035 break;
1036 case SoMouseButtonEvent::BUTTON3:
1037 this->button3down = press;
1038 break;
1039 // whatever else, we don't track
1040 }
1041 }
1042
1043 syncModifierKeys(ev);
1044
1045 smev.modifiers = (this->button1down ? NS::Event::BUTTON1DOWN : 0)
1046 | (this->button2down ? NS::Event::BUTTON2DOWN : 0)
1047 | (this->button3down ? NS::Event::BUTTON3DOWN : 0)
1048 | (this->ctrldown ? NS::Event::CTRLDOWN : 0) | (this->shiftdown ? NS::Event::SHIFTDOWN : 0)
1049 | (this->altdown ? NS::Event::ALTDOWN : 0);
1050
1051 #ifdef FC_OS_MACOSX
1052 // On Mac, Qt gesture events seem to be broken. At least that's what event
1053 // logs from @chrisb tell me. So, for until a developer on a mac gets here to
1054 // make gestures work, I disable them. --DeepSOIC
1055
1056 if (smev.isGestureEvent()) {
1057 return superclass::processSoEvent(ev);
1058 }
1059 #endif
1060
1061
1062 if (!smev.flags->processed) {
1063 this->naviMachine->processEvent(smev);
1064 }
1065 if (!smev.flags->propagated && !smev.flags->processed) {
1066 return superclass::processSoEvent(ev);
1067 }
1068 else {
1069 return smev.flags->processed;
1070 }
1071 }
1072
1073 SbBool GestureNavigationStyle::processSoEvent_bypass(const SoEvent* const ev)
1074 {
1075 return superclass::processSoEvent(ev);
1076 }
1077
1078 bool GestureNavigationStyle::is2DViewing() const
1079 {
1080 // #FIXME: detect sketch editing, ! any editing
1081 return this->viewer->isEditing();
1082 }
1083
1084 void GestureNavigationStyle::onRollGesture(int direction)
1085 {
1086 std::string cmd;
1087 if (direction == +1) {
1088 if (logging) {
1089 Base::Console().log("Roll forward gesture\n");
1090 }
1091 cmd = App::GetApplication()
1092 .GetParameterGroupByPath("User parameter:BaseApp/Preferences/View")
1093 ->GetASCII("GestureRollFwdCommand");
1094 }
1095 else if (direction == -1) {
1096 if (logging) {
1097 Base::Console().log("Roll backward gesture\n");
1098 }
1099 cmd = App::GetApplication()
1100 .GetParameterGroupByPath("User parameter:BaseApp/Preferences/View")
1101 ->GetASCII("GestureRollBackCommand");
1102 }
1103 if (cmd.empty()) {
1104 return;
1105 }
1106 std::stringstream code;
1107 code << "Gui.runCommand(\"" << cmd << "\")";
1108 try {
1109 Base::Interpreter().runString(code.str().c_str());
1110 }
1111 catch (Base::PyException& exc) {
1112 exc.reportException();
1113 }
1114 catch (...) {
1115 Base::Console().error(
1116 "GestureNavigationStyle::onRollGesture: unknown C++ exception when invoking command "
1117 "%s\n",
1118 cmd.c_str()
1119 );
1120 }
1121 }
1122
1123 void GestureNavigationStyle::EventQueue::post(const NS::Event& ev)
1124 {
1125 ev.flags->processed = true;
1126 this->push(*ev.asMouseButtonEvent());
1127 if (ns.logging) {
1128 Base::Console().log("postponed: ");
1129 ev.log();
1130 }
1131 }
1132
1133 void GestureNavigationStyle::EventQueue::discardAll()
1134 {
1135 while (!this->empty()) {
1136 this->pop();
1137 }
1138 }
1139
1140 void GestureNavigationStyle::EventQueue::forwardAll()
1141 {
1142 while (!this->empty()) {
1143 auto v = this->front();
1144 this->ns.processSoEvent_bypass(&v);
1145 this->pop();
1146 }
1147 }
1148
1149 } // namespace Gui