File size: 35,652 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
 *   Copyright (c) 2015 Victor Titov (DeepSOIC) <vv.titov@gmail.com>       *
 *                                                                         *
 *   This file is part of the FreeCAD CAx development system.              *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License as published by the Free Software Foundation; either          *
 *   version 2 of the License, or (at your option) any later version.      *
 *                                                                         *
 *   This library  is distributed in the hope that it will be useful,      *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU Library General Public License for more details.                  *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; see the file COPYING.LIB. If not,    *
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *   Minor modifications made by Pablo Gil (pablogil) in order to create   *
 *   a Maya or Unity 3D mouse navigation style:                            *
 *         ALT + left mouse button    = orbit                              *
 *         ALT + right mouse button   = zoom                               *
 *         ALT + middle mouse button  = pan                                *
 *                                                                         *
 *   Thanks Victor for your help!                                          *
 *                                                                         *
 ***************************************************************************/

/*
 *A few notes on this style. (by DeepSOIC)
 *
 * In this style, LMB serves dual purpose. It is selecting objects, as well as
 * spinning the view. The trick that enables it is as follows: The mousedown
 * event is consumed an saved, but otherwise remains unprocessed. If a drag is
 * detected while the button is down, the event is finally consumed (the saved
 * one is discarded), and spinning starts. If there is no drag detected before
 * the button is released, the saved mousedown is propagated to inherited,
 * followed by the mouseup. The same trick is used for RMB, so up to two
 * mousedown can be postponed.
 *
 * This navigation style does not exactly follow the structure of other
 * navigation styles, it does not fill many of the global variables defined in
 * NavigationStyle.
 *
 * This mode does not support locking cursor position on screen when
 * navigating, since with absolute pointing devices like pen and touch it makes
 * no sense (this style was specifically crafted for such devices).
 *
 * In this style, setViewing is not used (because I could not figure out how to
 * use it properly, and it seems to just work without it).
 *
 * This style wasn't tested with space during development (I don't have one).
 */


#include <QApplication>


#include <Base/Console.h>

#include "Navigation/NavigationStyle.h"
#include "SoTouchEvents.h"
#include "View3DInventorViewer.h"


using namespace Gui;

// ----------------------------------------------------------------------------------

/* TRANSLATOR Gui::MayaGestureNavigationStyle */

TYPESYSTEM_SOURCE(Gui::MayaGestureNavigationStyle, Gui::UserNavigationStyle)

MayaGestureNavigationStyle::MayaGestureNavigationStyle()
{
    mouseMoveThreshold = QApplication::startDragDistance();
    mouseMoveThresholdBroken = false;
    mousedownConsumedCount = 0;
    thisClickIsComplex = false;
    inGesture = false;
}

MayaGestureNavigationStyle::~MayaGestureNavigationStyle() = default;

const char* MayaGestureNavigationStyle::mouseButtons(ViewerMode mode)
{
    switch (mode) {
        case NavigationStyle::SELECTION:
            return QT_TR_NOOP("Tap OR click left mouse button.");
        case NavigationStyle::PANNING:
            return QT_TR_NOOP("Drag screen with two fingers OR press Alt + middle mouse button.");
        case NavigationStyle::DRAGGING:
            return QT_TR_NOOP(
                "Drag screen with one finger OR press Alt + left mouse button. In Sketcher and "
                "other edit modes, hold Alt in addition."
            );
        case NavigationStyle::ZOOMING:
            return QT_TR_NOOP(
                "Pinch (place two fingers on the screen and drag them apart from or towards each "
                "other) OR scroll mouse wheel OR press Alt + right mouse button OR PgUp/PgDown on "
                "keyboard."
            );
        default:
            return "No description";
    }
}

/*!
 * \brief MayaGestureNavigationStyle::testMoveThreshold tests if the mouse has moved far enough to
 * constder it a drag.
 * \param currentPos current position of mouse cursor, in local pixel coordinates.
 * \return true if the mouse was moved far enough. False if it's within the boundary. Ignores
 * MayaGestureNavigationStyle::mouseMoveThresholdBroken flag.
 */
bool MayaGestureNavigationStyle::testMoveThreshold(const SbVec2s currentPos) const
{
    SbVec2s movedBy = currentPos - this->mousedownPos;
    return SbVec2f(movedBy).length() >= this->mouseMoveThreshold;
}

void MayaGestureNavigationStyle::zoomByCursor(const SbVec2f& thispos, const SbVec2f& prevpos)
{
    const float dx = thispos[0] - prevpos[0];
    const float dy = thispos[1] - prevpos[1];

    // Compute zoom based on diagonal difference.
    float value = (dx - dy) * 10.0f;

    if (this->invertZoom) {
        value = -value;
    }
    zoom(viewer->getSoRenderManager()->getCamera(), value);
}

SbBool MayaGestureNavigationStyle::processSoEvent(const SoEvent* const ev)
{
    // Events when in "ready-to-seek" mode are ignored, except those
    // which influence the seek mode itself -- these are handled further
    // up the inheritance hierarchy.
    if (this->isSeekMode()) {
        return inherited::processSoEvent(ev);
    }
    // Switch off viewing mode (Bug #0000911)
    if (!this->isSeekMode() && !this->isAnimating() && this->isViewing()) {
        this->setViewing(false);  // by default disable viewing mode to render the scene
    }
    // setViewing() is never used in this style, so the previous if is very unlikely to be hit.

    const SoType type(ev->getTypeId());
    // define some shortcuts...
    bool evIsButton = type.isDerivedFrom(SoMouseButtonEvent::getClassTypeId());
    bool evIsKeyboard = type.isDerivedFrom(SoKeyboardEvent::getClassTypeId());
    bool evIsLoc2 = type.isDerivedFrom(SoLocation2Event::getClassTypeId());  // mouse movement
    bool evIsLoc3 = type.isDerivedFrom(SoMotion3Event::getClassTypeId());  // spaceball/joystick movement
    bool evIsGesture = type.isDerivedFrom(SoGestureEvent::getClassTypeId());  // touchscreen gesture

    const SbVec2f prevnormalized = this->lastmouseposition;
    const SbVec2s pos(ev->getPosition());  // not valid for gestures
    const SbVec2f posn = this->normalizePixelPos(pos);
    // pos: local coordinates of event, in pixels
    // posn: normalized local coordinates of event ((0,0) = lower left corner, (1,1) = upper right
    // corner)
    float ratio = viewer->getSoRenderManager()->getViewportRegion().getViewportAspectRatio();

    if (evIsButton || evIsLoc2) {
        this->lastmouseposition = posn;
    }

    const ViewerMode curmode = this->currentmode;
    // ViewerMode newmode = curmode;

    // make a unified mouse+modifiers state value (combo)
    enum
    {
        BUTTON1DOWN = 1 << 0,
        BUTTON2DOWN = 1 << 1,
        BUTTON3DOWN = 1 << 2,
        CTRLDOWN = 1 << 3,
        SHIFTDOWN = 1 << 4,
        ALTDOWN = 1 << 5,
        MASKBUTTONS = BUTTON1DOWN | BUTTON2DOWN | BUTTON3DOWN,
        MASKMODIFIERS = CTRLDOWN | SHIFTDOWN | ALTDOWN
    };
    unsigned int comboBefore =  // before = state before this event
        (this->button1down ? BUTTON1DOWN : 0) | (this->button2down ? BUTTON2DOWN : 0)
        | (this->button3down ? BUTTON3DOWN : 0) | (this->ctrldown ? CTRLDOWN : 0)
        | (this->shiftdown ? SHIFTDOWN : 0) | (this->altdown ? ALTDOWN : 0);

    // test for complex clicks
    int cntMBBefore = (comboBefore & BUTTON1DOWN ? 1 : 0)  // cntMBBefore = how many buttons were
                                                           // down when this event arrived?
        + (comboBefore & BUTTON2DOWN ? 1 : 0) + (comboBefore & BUTTON3DOWN ? 1 : 0);
    if (cntMBBefore >= 2) {
        this->thisClickIsComplex = true;
    }
    if (cntMBBefore == 0) {  // a good chance to reset some click-related stuff
        this->thisClickIsComplex = false;
        this->mousedownConsumedCount = 0;  // shouldn't be necessary, just a fail-safe.
    }

    // Mismatches in state of the modifier keys happens if the user
    // presses or releases them outside the viewer window.
    syncModifierKeys(ev);
    // before this block, mouse button states in NavigationStyle::buttonXdown reflected those before
    // current event arrived. track mouse button states
    if (evIsButton) {
        auto const event = (const SoMouseButtonEvent*)ev;
        const int button = event->getButton();
        const SbBool press  // the button was pressed (if false -> released)
            = event->getState() == SoButtonEvent::DOWN ? true : false;
        switch (button) {
            case SoMouseButtonEvent::BUTTON1:
                this->button1down = press;
                break;
            case SoMouseButtonEvent::BUTTON2:
                this->button2down = press;
                break;
            case SoMouseButtonEvent::BUTTON3:
                this->button3down = press;
                break;
                // whatever else, we don't track
        }
    }
    // after this block, the new states of the buttons are already in.

    unsigned int comboAfter =  // after = state after this event (current, essentially)
        (this->button1down ? BUTTON1DOWN : 0) | (this->button2down ? BUTTON2DOWN : 0)
        | (this->button3down ? BUTTON3DOWN : 0) | (this->ctrldown ? CTRLDOWN : 0)
        | (this->shiftdown ? SHIFTDOWN : 0) | (this->altdown ? ALTDOWN : 0);

    // test for complex clicks (again)
    int cntMBAfter = (comboAfter & BUTTON1DOWN ? 1 : 0)  // cntMBAfter = how many buttons were down
                                                         // when this event arrived?
        + (comboAfter & BUTTON2DOWN ? 1 : 0) + (comboAfter & BUTTON3DOWN ? 1 : 0);
    if (cntMBAfter >= 2) {
        this->thisClickIsComplex = true;
    }
    // if (cntMBAfter==0) this->thisClickIsComplex = false;//don't reset the flag now, we need to
    // know that this mouseUp was an end of a click that was complex. The flag will reset by the
    // before-check in the next event.

    // test for move detection
    if (evIsLoc2 || evIsButton) {
        this->mouseMoveThresholdBroken |= this->testMoveThreshold(pos);
    }

    // track gestures
    if (evIsGesture) {
        auto gesture = static_cast<const SoGestureEvent*>(ev);
        switch (gesture->state) {
            case SoGestureEvent::SbGSStart:
                // assert(!inGesture);//start of another gesture before the first finished? Happens
                // all the time for Pan gesture... No idea why!  --DeepSOIC
                inGesture = true;
                break;
            case SoGestureEvent::SbGSUpdate:
                assert(inGesture);  // gesture update without start?
                inGesture = true;
                break;
            case SoGestureEvent::SbGSEnd:
                assert(inGesture);  // gesture ended without starting?
                inGesture = false;
                break;
            case SoGestureEvent::SbGsCanceled:
                assert(inGesture);  // gesture canceled without starting?
                inGesture = false;
                break;
            default:
                assert(0);  // shouldn't happen
                inGesture = false;
        }
    }
    if (evIsButton) {
        if (inGesture) {
            inGesture = false;  // reset the flag when mouse clicks are received, to ensure enabling
                                // mouse navigation back.
            setViewingMode(NavigationStyle::SELECTION);  // exit navigation asap, to proceed with
                                                         // regular processing of the click
        }
    }

    bool suppressLMBDrag = false;
    if (viewer->isEditing()) {
        // in edit mode, disable lmb dragging (spinning). Holding Alt enables it.
        suppressLMBDrag = !(comboAfter & ALTDOWN);
    }

    //----------all this were preparations. Now comes the event handling! ----------

    SbBool processed = false;  // a return value for the BlahblahblahNavigationStyle::processSoEvent
    bool propagated = false;   // an internal flag indicating that the event has been already passed
                               // to inherited, to suppress the automatic doing of this at the end.
    // goto finalize = return processed. Might be important to do something before done (none now).

    // give the nodes in the foreground root the chance to handle events (e.g color bar)
    if (!viewer->isEditing()) {
        processed = handleEventInForeground(ev);
    }
    if (processed) {
        goto finalize;
    }

    // Mode-independent keyboard handling
    if (evIsKeyboard) {
        auto const event = (const SoKeyboardEvent*)ev;
        const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false;
        switch (event->getKey()) {
            case SoKeyboardEvent::H:
                // Disable H key in editing mode because of conflict with sketcher
                if (!viewer->isEditing()) {
                    processed = true;
                    if (!press) {
                        setupPanningPlane(viewer->getCamera());
                        lookAtPoint(event->getPosition());
                    }
                }
                break;
            default:
                break;
        }
    }
    if (processed) {
        goto finalize;
    }

    // mode-independent spaceball/joystick handling
    if (evIsLoc3) {
        auto const event = static_cast<const SoMotion3Event*>(ev);
        if (event) {
            this->processMotionEvent(event);
        }
        processed = true;
    }
    if (processed) {
        goto finalize;
    }

    // all mode-dependent stuff is within this switch.
    switch (curmode) {
        case NavigationStyle::SELECTION:
            // Prevent interrupting rubber-band selection in sketcher
            if (viewer->isEditing()) {
                if (evIsButton) {
                    auto const event = (const SoMouseButtonEvent*)ev;
                    const SbBool press = event->getState() == SoButtonEvent::DOWN;
                    const int button = event->getButton();

                    if (!press && button == SoMouseButtonEvent::BUTTON1) {
                        setViewingMode(NavigationStyle::IDLE);
                        break;
                    }
                }

                if (this->button1down) {
                    break;
                }
            }
            [[fallthrough]];
        case NavigationStyle::IDLE:
            // Prevent interrupting rubber-band selection in sketcher
            if (viewer->isEditing()) {
                if (evIsButton) {
                    auto const event = (const SoMouseButtonEvent*)ev;
                    const SbBool press = event->getState() == SoButtonEvent::DOWN;
                    const int button = event->getButton();

                    if (press && button == SoMouseButtonEvent::BUTTON1 && !this->altdown) {
                        setViewingMode(NavigationStyle::SELECTION);
                        break;
                    }
                }
            }
            [[fallthrough]];
        case NavigationStyle::INTERACT: {
            // idle and interaction

            // keyboard
            if (evIsKeyboard) {
                auto const event = (const SoKeyboardEvent*)ev;
                const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false;

                switch (event->getKey()) {
                    case SoKeyboardEvent::S:
                    case SoKeyboardEvent::HOME:
                    case SoKeyboardEvent::LEFT_ARROW:
                    case SoKeyboardEvent::UP_ARROW:
                    case SoKeyboardEvent::RIGHT_ARROW:
                    case SoKeyboardEvent::DOWN_ARROW:
                        processed = inherited::processSoEvent(ev);
                        propagated = true;
                        break;
                    case SoKeyboardEvent::PAGE_UP:
                        if (press) {
                            doZoom(viewer->getSoRenderManager()->getCamera(), getDelta(), posn);
                        }
                        processed = true;
                        break;
                    case SoKeyboardEvent::PAGE_DOWN:
                        if (press) {
                            doZoom(viewer->getSoRenderManager()->getCamera(), -getDelta(), posn);
                        }
                        processed = true;
                        break;
                    default:
                        break;
                }  // switch key
            }
            if (processed) {
                goto finalize;
            }


            // Mouse Button / Spaceball Button handling
            if (evIsButton) {
                auto const event = (const SoMouseButtonEvent*)ev;
                const int button = event->getButton();
                const SbBool press  // the button was pressed (if false -> released)
                    = event->getState() == SoButtonEvent::DOWN ? true : false;
                switch (button) {
                    case SoMouseButtonEvent::BUTTON1:
                    case SoMouseButtonEvent::BUTTON2:
                        if (press) {
                            if (this->thisClickIsComplex && this->mouseMoveThresholdBroken) {
                                // this should prevent re-attempts to enter navigation when doing
                                // more clicks after a move.
                            }
                            else {
                                // on LMB-down or RMB-down, we don't know yet if we should propagate
                                // it or process it. Save the event to be refired later, when it
                                // becomes clear. reset/start move detection machine
                                this->mousedownPos = pos;
                                this->mouseMoveThresholdBroken = false;
                                setupPanningPlane(
                                    viewer->getSoRenderManager()->getCamera()
                                );  // set up panningplane
                                int& cnt = this->mousedownConsumedCount;
                                this->mousedownConsumedEvents[cnt]
                                    = *event;  // hopefully, a shallow copy is enough. There are no
                                               // pointers stored in events, apparently. Will lose a
                                               // subclass, though.
                                cnt++;
                                assert(cnt <= 2);
                                if (cnt > static_cast<int>(sizeof(mousedownConsumedEvents))) {
                                    cnt = sizeof(mousedownConsumedEvents);  // we are in trouble
                                }
                                processed = true;  // just consume this event, and wait for the move
                                                   // threshold to be broken to start dragging/panning
                            }
                        }
                        else {  // release
                            if (button == SoMouseButtonEvent::BUTTON2 && !this->thisClickIsComplex) {
                                if (!viewer->isEditing() && this->isPopupMenuEnabled()) {
                                    processed = true;
                                    this->openPopupMenu(event->getPosition());
                                }
                            }
                            if (!processed) {
                                // re-synthesize all previously-consumed mouseDowns, if any. They
                                // might have been re-synthesized already when threshold was broken.
                                for (int i = 0; i < this->mousedownConsumedCount; i++) {
                                    inherited::processSoEvent(
                                        &(this->mousedownConsumedEvents[i])
                                    );  // simulate the previously-comsumed mousedown.
                                }
                                this->mousedownConsumedCount = 0;
                                processed = inherited::processSoEvent(
                                    ev
                                );  // explicitly, just for clarity that we are sending a full click
                                    // sequence.
                                propagated = true;
                            }
                        }
                        break;
                    case SoMouseButtonEvent::BUTTON3:  // press the wheel
                        // starts PANNING mode
                        if (press & this->altdown) {
                            setViewingMode(NavigationStyle::PANNING);
                        }
                        else if (press) {
                            // if not PANNING then look at point
                            setupPanningPlane(viewer->getCamera());
                            lookAtPoint(event->getPosition());
                        }
                        processed = true;
                        break;
                }
            }

            // mouse moves - test for move threshold breaking
            if (evIsLoc2) {
                if (this->mouseMoveThresholdBroken && (this->button1down || this->button2down)
                    && mousedownConsumedCount > 0) {
                    // mousemovethreshold has JUST been broken

                    // test if we should enter navigation
                    if ((this->button1down && !suppressLMBDrag && this->altdown)
                        || (this->button2down && this->altdown)) {
                        // yes, we are entering navigation.
                        // throw away consumed mousedowns.
                        this->mousedownConsumedCount = 0;

                        // start DRAGGING mode (orbit)
                        // if not pressing left mouse button then it assumes is right mouse button
                        // and starts ZOOMING mode
                        saveCursorPosition(ev);
                        setViewingMode(
                            this->button1down ? NavigationStyle::DRAGGING : NavigationStyle::ZOOMING
                        );
                        processed = true;
                    }
                    else {
                        // no, we are not entering navigation.
                        // re-synthesize all previously-consumed mouseDowns, if any, and propagate
                        // this mousemove.
                        for (int i = 0; i < this->mousedownConsumedCount; i++) {
                            inherited::processSoEvent(
                                &(this->mousedownConsumedEvents[i])
                            );  // simulate the previously-comsumed mousedown.
                        }
                        this->mousedownConsumedCount = 0;
                        processed = inherited::processSoEvent(ev);  // explicitly, just for clarity
                                                                    // that we are sending a full
                                                                    // click sequence.
                        propagated = true;
                    }
                }
                if (mousedownConsumedCount > 0) {
                    processed = true;  // if we are still deciding if it's a drag or not, consume
                                       // mouseMoves.
                }
            }

            // gesture start
            if (evIsGesture
                && /*!this->button1down &&*/ !this->button2down) {  // ignore gestures when mouse
                                                                    // buttons are down. Button1down
                                                                    // check was disabled because of
                                                                    // wrong state after doubleclick
                                                                    // on sketcher constraint to
                                                                    // edit datum
                auto gesture = static_cast<const SoGestureEvent*>(ev);
                if (gesture->state == SoGestureEvent::SbGSStart
                    || gesture->state == SoGestureEvent::SbGSUpdate) {  // even if we didn't get a
                                                                        // start, assume the first
                                                                        // update is a start
                                                                        // (sort-of fail-safe).
                    if (type.isDerivedFrom(SoGesturePanEvent::getClassTypeId())) {
                        setupPanningPlane(viewer->getSoRenderManager()->getCamera());  // set up
                                                                                       // panning
                                                                                       // plane
                        setViewingMode(NavigationStyle::PANNING);
                        processed = true;
                    }
                    else if (type.isDerivedFrom(SoGesturePinchEvent::getClassTypeId())) {
                        setupPanningPlane(viewer->getSoRenderManager()->getCamera());  // set up
                                                                                       // panning
                                                                                       // plane
                        saveCursorPosition(ev);
                        setViewingMode(NavigationStyle::DRAGGING);
                        processed = true;
                    }  // all other gestures - ignore!
                }
            }

            // loc2 (mousemove) - ignore.

        } break;  // end of idle and interaction
        case NavigationStyle::DRAGGING:
        case NavigationStyle::ZOOMING:
        case NavigationStyle::PANNING: {
            // actual navigation

            // no keyboard.

            // Mouse Button / Spaceball Button handling
            if (evIsButton) {
                auto const event = (const SoMouseButtonEvent*)ev;
                const int button = event->getButton();
                switch (button) {
                    case SoMouseButtonEvent::BUTTON1:
                    case SoMouseButtonEvent::BUTTON2:
                    case SoMouseButtonEvent::BUTTON3:  // allows one to release button3 into
                                                       // SELECTION mode
                        if (comboAfter & BUTTON1DOWN || comboAfter & BUTTON2DOWN) {
                            // don't leave navigation till all buttons have been released
                            if (comboAfter & BUTTON1DOWN && comboAfter & BUTTON2DOWN) {
                                setRotationCenter(getFocalPoint());
                            }
                            else {
                                saveCursorPosition(ev);
                            }
                            setViewingMode(
                                (comboAfter & BUTTON1DOWN) ? NavigationStyle::DRAGGING
                                                           : NavigationStyle::PANNING
                            );
                            processed = true;
                        }
                        else {  // all buttons are released
                            // end of dragging/panning/whatever
                            setViewingMode(NavigationStyle::IDLE);
                            processed = true;
                        }  // end of else (some buttons down)
                        break;
                }  // switch(button)
            }  // if(evIsButton)

            // the essence part 1!
            // mouse movement into camera motion. Suppress if in gesture. Ignore until threshold is
            // surpassed.
            if (evIsLoc2 && !this->inGesture && this->mouseMoveThresholdBroken) {
                if (curmode == NavigationStyle::ZOOMING) {  // doesn't happen
                    this->zoomByCursor(posn, prevnormalized);
                    processed = true;
                }
                else if (curmode == NavigationStyle::PANNING) {
                    panCamera(
                        viewer->getSoRenderManager()->getCamera(),
                        ratio,
                        this->panningplane,
                        posn,
                        prevnormalized
                    );
                    processed = true;
                }
                else if (curmode == NavigationStyle::DRAGGING) {
                    if (comboAfter & BUTTON1DOWN && comboAfter & BUTTON2DOWN) {
                        // two mouse buttons down - tilting!
                        NavigationStyle::doRotate(
                            viewer->getSoRenderManager()->getCamera(),
                            (posn - prevnormalized)[0] * (-2),
                            SbVec2f(0.5, 0.5)
                        );
                        processed = true;
                    }
                    else {  // one mouse button - normal spinning
                        // this will also handle the single-finger drag (there's no gesture used,
                        // pseudomouse is enough) this->addToLog(event->getPosition(), event->getTime());
                        this->spin_simplified(posn, prevnormalized);
                        processed = true;
                    }
                }
            }

            // the essence part 2!
            // gesture into camera motion
            if (evIsGesture) {
                auto gesture = static_cast<const SoGestureEvent*>(ev);
                assert(gesture);
                if (gesture->state == SoGestureEvent::SbGSEnd) {
                    setViewingMode(NavigationStyle::SELECTION);
                    processed = true;
                }
                else if (gesture->state == SoGestureEvent::SbGSUpdate) {
                    if (type.isDerivedFrom(SoGesturePinchEvent::getClassTypeId())) {
                        auto const event = static_cast<const SoGesturePinchEvent*>(ev);
                        if (this->zoomAtCursor) {
                            // this is just dealing with the pan part of pinch gesture. Taking care
                            // of zooming to pos is done in doZoom.
                            SbVec2f panDist = this->normalizePixelPos(event->deltaCenter.getValue());
                            NavigationStyle::panCamera(
                                viewer->getSoRenderManager()->getCamera(),
                                ratio,
                                this->panningplane,
                                panDist,
                                SbVec2f(0, 0)
                            );
                        }
                        NavigationStyle::doZoom(
                            viewer->getSoRenderManager()->getCamera(),
                            -logf(event->deltaZoom),
                            this->normalizePixelPos(event->curCenter)
                        );
                        if (event->deltaAngle != 0) {
                            NavigationStyle::doRotate(
                                viewer->getSoRenderManager()->getCamera(),
                                event->deltaAngle,
                                this->normalizePixelPos(event->curCenter)
                            );
                        }
                        processed = true;
                    }
                    if (type.isDerivedFrom(SoGesturePanEvent::getClassTypeId())) {
                        auto const event = static_cast<const SoGesturePanEvent*>(ev);
                        // this is just dealing with the pan part of pinch gesture. Taking care of
                        // zooming to pos is done in doZoom.
                        SbVec2f panDist = this->normalizePixelPos(event->deltaOffset);
                        NavigationStyle::panCamera(
                            viewer->getSoRenderManager()->getCamera(),
                            ratio,
                            this->panningplane,
                            panDist,
                            SbVec2f(0, 0)
                        );
                        processed = true;
                    }
                }
                else {
                    // shouldn't happen. Gestures are not expected to start in the middle of
                    // navigation. we'll consume it, without reacting.
                    processed = true;
                }
            }

        } break;  // end of actual navigation
        case NavigationStyle::SEEK_WAIT_MODE: {
            if (evIsButton) {
                auto const event = (const SoMouseButtonEvent*)ev;
                const int button = event->getButton();
                const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false;
                if (button == SoMouseButtonEvent::BUTTON1 && press) {
                    this->seekToPoint(pos);  // implicitly calls interactiveCountInc()
                    this->setViewingMode(NavigationStyle::SEEK_MODE);
                    processed = true;
                }
            }
        };  // not end of SEEK_WAIT_MODE. Fall through by design!!!
            /* FALLTHRU */
        case NavigationStyle::SPINNING:
        case NavigationStyle::SEEK_MODE: {
            // animation modes
            if (!processed) {
                if (evIsButton || evIsGesture || evIsKeyboard || evIsLoc3) {
                    setViewingMode(NavigationStyle::SELECTION);
                }
            }
        } break;  // end of animation modes
        case NavigationStyle::BOXZOOM:
        default:
            // all the rest - will be pass on to inherited, later.
            break;
    }

    if (!processed && !propagated) {
        processed = inherited::processSoEvent(ev);
        propagated = true;
    }

    //-----------------------end of event handling---------------------
finalize:
    return processed;
}