| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2025 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
**************************************************************************/ |
| 23 |
|
| 24 |
#include <boost/statechart/custom_reaction.hpp> |
| 25 |
#include <boost/statechart/state_machine.hpp> |
| 26 |
#include <boost/statechart/state.hpp> |
| 27 |
|
| 28 |
#include "Camera.h" |
| 29 |
#include "NavigationStateChart.h" |
| 30 |
#include "View3DInventorViewer.h" |
| 31 |
|
| 32 |
|
| 33 |
using namespace Gui; |
| 34 |
namespace sc = boost::statechart; |
| 35 |
using NS = NavigationStateChart; |
| 36 |
|
| 37 |
NS::Event::Event() |
| 38 |
: flags(new Flags) |
| 39 |
{} |
| 40 |
|
| 41 |
bool NS::Event::isMouseButtonEvent() const |
| 42 |
{ |
| 43 |
return this->inventor_event->isOfType(SoMouseButtonEvent::getClassTypeId()); |
| 44 |
} |
| 45 |
|
| 46 |
const SoMouseButtonEvent* NS::Event::asMouseButtonEvent() const |
| 47 |
{ |
| 48 |
return static_cast<const SoMouseButtonEvent*>(this->inventor_event); |
| 49 |
} |
| 50 |
|
| 51 |
bool NS::Event::isPress(Button button) const |
| 52 |
{ |
| 53 |
return SoMouseButtonEvent::isButtonPressEvent(this->inventor_event, button); |
| 54 |
} |
| 55 |
|
| 56 |
bool NS::Event::isRelease(Button button) const |
| 57 |
{ |
| 58 |
return SoMouseButtonEvent::isButtonReleaseEvent(this->inventor_event, button); |
| 59 |
} |
| 60 |
|
| 61 |
bool NS::Event::isKeyPress(Key key) const |
| 62 |
{ |
| 63 |
return SoKeyboardEvent::isKeyPressEvent(this->inventor_event, key); |
| 64 |
} |
| 65 |
|
| 66 |
bool NS::Event::isKeyRelease(Key key) const |
| 67 |
{ |
| 68 |
return SoKeyboardEvent::isKeyReleaseEvent(this->inventor_event, key); |
| 69 |
} |
| 70 |
|
| 71 |
bool NS::Event::isKeyboardEvent() const |
| 72 |
{ |
| 73 |
return this->inventor_event->isOfType(SoKeyboardEvent::getClassTypeId()); |
| 74 |
} |
| 75 |
|
| 76 |
const SoKeyboardEvent* NS::Event::asKeyboardEvent() const |
| 77 |
{ |
| 78 |
return static_cast<const SoKeyboardEvent*>(this->inventor_event); |
| 79 |
} |
| 80 |
|
| 81 |
bool NS::Event::isLocation2Event() const |
| 82 |
{ |
| 83 |
return this->inventor_event->isOfType(SoLocation2Event::getClassTypeId()); |
| 84 |
} |
| 85 |
|
| 86 |
const SoLocation2Event* NS::Event::asLocation2Event() const |
| 87 |
{ |
| 88 |
return static_cast<const SoLocation2Event*>(this->inventor_event); |
| 89 |
} |
| 90 |
|
| 91 |
bool NS::Event::isMotion3Event() const |
| 92 |
{ |
| 93 |
return this->inventor_event->isOfType(SoMotion3Event::getClassTypeId()); |
| 94 |
} |
| 95 |
|
| 96 |
const SoMotion3Event* NS::Event::asMotion3Event() const |
| 97 |
{ |
| 98 |
return static_cast<const SoMotion3Event*>(this->inventor_event); |
| 99 |
} |
| 100 |
|
| 101 |
bool NS::Event::isDownButton(unsigned int state) const |
| 102 |
{ |
| 103 |
return mbstate() == state; |
| 104 |
} |
| 105 |
|
| 106 |
bool NS::Event::isDownNoButton() const |
| 107 |
{ |
| 108 |
return mbstate() == 0; |
| 109 |
} |
| 110 |
|
| 111 |
bool NS::Event::isDownButton1() const |
| 112 |
{ |
| 113 |
return (mbstate() & BUTTON1DOWN) == BUTTON1DOWN; |
| 114 |
} |
| 115 |
|
| 116 |
bool NS::Event::isDownButton2() const |
| 117 |
{ |
| 118 |
return (mbstate() & BUTTON2DOWN) == BUTTON2DOWN; |
| 119 |
} |
| 120 |
|
| 121 |
bool NS::Event::isDownButton3() const |
| 122 |
{ |
| 123 |
return (mbstate() & BUTTON3DOWN) == BUTTON3DOWN; |
| 124 |
} |
| 125 |
|
| 126 |
bool NS::Event::isDownControl() const |
| 127 |
{ |
| 128 |
return (kbstate() & CTRLDOWN) == CTRLDOWN; |
| 129 |
} |
| 130 |
|
| 131 |
bool NS::Event::isDownShift() const |
| 132 |
{ |
| 133 |
return (kbstate() & SHIFTDOWN) == SHIFTDOWN; |
| 134 |
} |
| 135 |
|
| 136 |
bool NS::Event::isDownAlt() const |
| 137 |
{ |
| 138 |
return (kbstate() & ALTDOWN) == ALTDOWN; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/* TRANSLATOR Gui::NavigationStateChart */ |
| 143 |
|
| 144 |
TYPESYSTEM_SOURCE_ABSTRACT(Gui::NavigationStateChart, Gui::UserNavigationStyle) |
| 145 |
|
| 146 |
NavigationStateChart::NavigationStateChart() |
| 147 |
{} |
| 148 |
|
| 149 |
NavigationStateChart::~NavigationStateChart() |
| 150 |
{ |
| 151 |
naviMachine.reset(); |
| 152 |
} |
| 153 |
|
| 154 |
SbBool NavigationStateChart::processSoEvent(const SoEvent* const ev) |
| 155 |
{ |
| 156 |
// Events when in "ready-to-seek" mode are ignored, except those |
| 157 |
// which influence the seek mode itself -- these are handled further |
| 158 |
// up the inheritance hierarchy. |
| 159 |
if (this->isSeekMode()) { |
| 160 |
return inherited::processSoEvent(ev); |
| 161 |
} |
| 162 |
|
| 163 |
// Switch off viewing mode (Bug #0000911) |
| 164 |
if (!this->isSeekMode() && !this->isAnimating() && this->isViewing()) { |
| 165 |
this->setViewing(false); // by default disable viewing mode to render the scene |
| 166 |
} |
| 167 |
|
| 168 |
// Mismatches in state of the modifier keys happens if the user |
| 169 |
// presses or releases them outside the viewer window. |
| 170 |
syncModifierKeys(ev); |
| 171 |
|
| 172 |
// give the nodes in the foreground root the chance to handle events (e.g color bar) |
| 173 |
if (!viewer->isEditing()) { |
| 174 |
if (handleEventInForeground(ev)) { |
| 175 |
return true; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
NS::Event smev; |
| 180 |
smev.inventor_event = ev; |
| 181 |
|
| 182 |
// Spaceball/joystick handling |
| 183 |
if (ev->isOfType(SoMotion3Event::getClassTypeId())) { |
| 184 |
smev.flags->processed = true; |
| 185 |
this->processMotionEvent(static_cast<const SoMotion3Event*>(ev)); |
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
// Keyboard handling |
| 190 |
if (ev->isOfType(SoKeyboardEvent::getClassTypeId())) { |
| 191 |
const auto event = static_cast<const SoKeyboardEvent*>(ev); |
| 192 |
smev.flags->processed = processKeyboardEvent(event); |
| 193 |
} |
| 194 |
|
| 195 |
if (smev.isMouseButtonEvent()) { |
| 196 |
const auto button = smev.asMouseButtonEvent()->getButton(); |
| 197 |
const SbBool press = smev.asMouseButtonEvent()->getState() == SoButtonEvent::DOWN; |
| 198 |
switch (button) { |
| 199 |
case SoMouseButtonEvent::BUTTON1: |
| 200 |
this->button1down = press; |
| 201 |
break; |
| 202 |
case SoMouseButtonEvent::BUTTON2: |
| 203 |
this->button2down = press; |
| 204 |
break; |
| 205 |
case SoMouseButtonEvent::BUTTON3: |
| 206 |
this->button3down = press; |
| 207 |
break; |
| 208 |
default: |
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
smev.modifiers = (this->button1down ? NS::Event::BUTTON1DOWN : 0) |
| 214 |
| (this->button2down ? NS::Event::BUTTON2DOWN : 0) |
| 215 |
| (this->button3down ? NS::Event::BUTTON3DOWN : 0) |
| 216 |
| (this->ctrldown ? NS::Event::CTRLDOWN : 0) | (this->shiftdown ? NS::Event::SHIFTDOWN : 0) |
| 217 |
| (this->altdown ? NS::Event::ALTDOWN : 0); |
| 218 |
|
| 219 |
if (!smev.flags->processed) { |
| 220 |
this->naviMachine->process_event(smev); |
| 221 |
} |
| 222 |
|
| 223 |
if (!smev.flags->propagated && !smev.flags->processed) { |
| 224 |
return inherited::processSoEvent(ev); |
| 225 |
} |
| 226 |
|
| 227 |
return smev.flags->processed; |
| 228 |
} |