Debian Codegraph @

src/Gui/Navigation/BlenderNavigationStyle.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net> *
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 #include <Inventor/nodes/SoCamera.h>
25 #include <QApplication>
26
27 #include "Navigation/NavigationStyle.h"
28 #include "View3DInventorViewer.h"
29
30
31 using namespace Gui;
32
33 // ----------------------------------------------------------------------------------
34
35 /* TRANSLATOR Gui::BlenderNavigationStyle */
36
37 TYPESYSTEM_SOURCE(Gui::BlenderNavigationStyle, Gui::UserNavigationStyle)
38
39 BlenderNavigationStyle::BlenderNavigationStyle()
40 : lockButton1(false)
41 {}
42
43 BlenderNavigationStyle::~BlenderNavigationStyle() = default;
44
45 const char* BlenderNavigationStyle::mouseButtons(ViewerMode mode)
46 {
47 switch (mode) {
48 case NavigationStyle::SELECTION:
49 return QT_TR_NOOP("Press left mouse button");
50 case NavigationStyle::PANNING:
51 return QT_TR_NOOP("Press Shift and middle mouse button");
52 case NavigationStyle::DRAGGING:
53 return QT_TR_NOOP("Press middle mouse button");
54 case NavigationStyle::ZOOMING:
55 return QT_TR_NOOP("Scroll mouse wheel");
56 default:
57 return "No description";
58 }
59 }
60
61 SbBool BlenderNavigationStyle::processSoEvent(const SoEvent* const ev)
62 {
63 // Events when in "ready-to-seek" mode are ignored, except those
64 // which influence the seek mode itself -- these are handled further
65 // up the inheritance hierarchy.
66 if (this->isSeekMode()) {
67 return inherited::processSoEvent(ev);
68 }
69 // Switch off viewing mode (Bug #0000911)
70 if (!this->isSeekMode() && !this->isAnimating() && this->isViewing()) {
71 this->setViewing(false); // by default disable viewing mode to render the scene
72 }
73
74 const SoType type(ev->getTypeId());
75
76 const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion();
77 const SbVec2s pos(ev->getPosition());
78 const SbVec2f posn = normalizePixelPos(pos);
79
80 const SbVec2f prevnormalized = this->lastmouseposition;
81 this->lastmouseposition = posn;
82
83 // Set to true if any event processing happened. Note that it is not
84 // necessary to restrict ourselves to only do one "action" for an
85 // event, we only need this flag to see if any processing happened
86 // at all.
87 SbBool processed = false;
88
89 const ViewerMode curmode = this->currentmode;
90 ViewerMode newmode = curmode;
91
92 // Mismatches in state of the modifier keys happens if the user
93 // presses or releases them outside the viewer window.
94 syncModifierKeys(ev);
95
96 // give the nodes in the foreground root the chance to handle events (e.g color bar)
97 if (!viewer->isEditing()) {
98 processed = handleEventInForeground(ev);
99 if (processed) {
100 return true;
101 }
102 }
103
104 // Keyboard handling
105 if (type.isDerivedFrom(SoKeyboardEvent::getClassTypeId())) {
106 const auto event = static_cast<const SoKeyboardEvent*>(ev);
107 processed = processKeyboardEvent(event);
108 }
109
110 // Mouse Button / Spaceball Button handling
111 if (type.isDerivedFrom(SoMouseButtonEvent::getClassTypeId())) {
112 const auto* const event = (const SoMouseButtonEvent*)ev;
113 const int button = event->getButton();
114 const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false;
115
116 // SoDebugError::postInfo("processSoEvent", "button = %d", button);
117 switch (button) {
118 case SoMouseButtonEvent::BUTTON1:
119 this->lockrecenter = true;
120 this->button1down = press;
121 if (press && (this->currentmode == NavigationStyle::SEEK_WAIT_MODE)) {
122 newmode = NavigationStyle::SEEK_MODE;
123 this->seekToPoint(pos); // implicitly calls interactiveCountInc()
124 processed = true;
125 }
126 else if (press
127 && (this->currentmode == NavigationStyle::PANNING
128 || this->currentmode == NavigationStyle::ZOOMING)) {
129 newmode = NavigationStyle::DRAGGING;
130 saveCursorPosition(ev);
131 this->centerTime = ev->getTime();
132 processed = true;
133 }
134 else if (!press && (this->currentmode == NavigationStyle::DRAGGING)) {
135 processed = true;
136 }
137 else if (viewer->isEditing() && (this->currentmode == NavigationStyle::SPINNING)) {
138 processed = true;
139 }
140 else {
141 processed = processClickEvent(event);
142 }
143 break;
144 case SoMouseButtonEvent::BUTTON2:
145 // If we are in edit mode then simply ignore the RMB events
146 // to pass the event to the base class.
147 this->lockrecenter = true;
148
149 // Don't show the context menu after dragging, panning or zooming
150 if (!press && (hasDragged || hasPanned || hasZoomed)) {
151 processed = true;
152 }
153 else if (!press && !viewer->isEditing()) {
154 if (this->currentmode != NavigationStyle::ZOOMING
155 && this->currentmode != NavigationStyle::PANNING
156 && this->currentmode != NavigationStyle::DRAGGING) {
157 if (this->isPopupMenuEnabled()) {
158 this->openPopupMenu(event->getPosition());
159 }
160 }
161 }
162 // Alternative way of rotating & zooming
163 if (press
164 && (this->currentmode == NavigationStyle::PANNING
165 || this->currentmode == NavigationStyle::ZOOMING)) {
166 newmode = NavigationStyle::DRAGGING;
167 saveCursorPosition(ev);
168 this->centerTime = ev->getTime();
169 processed = true;
170 }
171 this->button2down = press;
172 break;
173 case SoMouseButtonEvent::BUTTON3:
174 if (press) {
175 this->centerTime = ev->getTime();
176 setupPanningPlane(getCamera());
177 this->lockrecenter = false;
178 }
179 else {
180 SbTime tmp = (ev->getTime() - this->centerTime);
181 float dci = (float)QApplication::doubleClickInterval() / 1000.0f;
182 // is it just a middle click?
183 if (tmp.getValue() < dci && !this->lockrecenter) {
184 lookAtPoint(pos);
185 processed = true;
186 }
187 }
188 this->button3down = press;
189 break;
190 default:
191 break;
192 }
193 }
194
195 // Mouse Movement handling
196 if (type.isDerivedFrom(SoLocation2Event::getClassTypeId())) {
197 this->lockrecenter = true;
198 const auto* const event = (const SoLocation2Event*)ev;
199 if (this->currentmode == NavigationStyle::ZOOMING) {
200 this->zoomByCursor(posn, prevnormalized);
201 processed = true;
202 }
203 else if (this->currentmode == NavigationStyle::PANNING) {
204 float ratio = vp.getViewportAspectRatio();
205 panCamera(
206 viewer->getSoRenderManager()->getCamera(),
207 ratio,
208 this->panningplane,
209 posn,
210 prevnormalized
211 );
212 processed = true;
213 }
214 else if (this->currentmode == NavigationStyle::DRAGGING) {
215 this->addToLog(event->getPosition(), event->getTime());
216 this->spin(posn);
217 moveCursorPosition();
218 processed = true;
219 }
220 }
221
222 // Spaceball & Joystick handling
223 if (type.isDerivedFrom(SoMotion3Event::getClassTypeId())) {
224 const auto* const event = static_cast<const SoMotion3Event*>(ev);
225 if (event) {
226 this->processMotionEvent(event);
227 }
228 processed = true;
229 }
230
231 enum
232 {
233 BUTTON1DOWN = 1 << 0,
234 BUTTON3DOWN = 1 << 1,
235 CTRLDOWN = 1 << 2,
236 SHIFTDOWN = 1 << 3,
237 BUTTON2DOWN = 1 << 4
238 };
239 unsigned int combo = (this->button1down ? BUTTON1DOWN : 0)
240 | (this->button2down ? BUTTON2DOWN : 0) | (this->button3down ? BUTTON3DOWN : 0)
241 | (this->ctrldown ? CTRLDOWN : 0) | (this->shiftdown ? SHIFTDOWN : 0);
242
243 switch (combo) {
244 case 0:
245 if (curmode == NavigationStyle::SPINNING) {
246 break;
247 }
248 newmode = NavigationStyle::IDLE;
249 // The left mouse button has been released right now
250 if (this->lockButton1) {
251 this->lockButton1 = false;
252 if (curmode != NavigationStyle::SELECTION) {
253 processed = true;
254 }
255 }
256 break;
257 case BUTTON1DOWN:
258 case CTRLDOWN | BUTTON1DOWN:
259 // make sure not to change the selection when stopping spinning
260 if (curmode == NavigationStyle::SPINNING
261 || (this->lockButton1 && curmode != NavigationStyle::SELECTION)) {
262 newmode = NavigationStyle::IDLE;
263 }
264 else {
265 newmode = NavigationStyle::SELECTION;
266 }
267 break;
268 case BUTTON1DOWN | BUTTON2DOWN:
269 newmode = NavigationStyle::PANNING;
270 break;
271 case SHIFTDOWN | BUTTON3DOWN:
272 newmode = NavigationStyle::PANNING;
273 break;
274 case BUTTON3DOWN:
275 if (newmode != NavigationStyle::DRAGGING) {
276 saveCursorPosition(ev);
277 }
278 newmode = NavigationStyle::DRAGGING;
279 break;
280 case CTRLDOWN | SHIFTDOWN | BUTTON2DOWN:
281 case CTRLDOWN | BUTTON3DOWN:
282 newmode = NavigationStyle::ZOOMING;
283 break;
284
285 default:
286 // Reset mode to IDLE when button 3 is released
287 // This stops the PANNING when button 3 is released but SHIFT is still pressed
288 // This stops the ZOOMING when button 3 is released but CTRL is still pressed
289 if ((curmode == NavigationStyle::PANNING || curmode == NavigationStyle::ZOOMING)
290 && !this->button3down) {
291 newmode = NavigationStyle::IDLE;
292 }
293 break;
294 }
295
296 // If the selection button is pressed together with another button
297 // and the other button is released, don't switch to selection mode.
298 // Process when selection button is pressed together with other buttons that could trigger
299 // different actions.
300 if (this->button1down && (this->button2down || this->button3down)) {
301 this->lockButton1 = true;
302 processed = true;
303 }
304
305 // Prevent interrupting rubber-band selection in sketcher
306 if (viewer->isEditing() && curmode == NavigationStyle::SELECTION
307 && newmode != NavigationStyle::IDLE) {
308 if (!button1down || !button2down) { // Allow canceling rubber-band in sketcher if both
309 // button 1 and button 2 are pressed
310 newmode = NavigationStyle::SELECTION;
311 }
312 processed = false;
313 }
314
315 // Reset flags when newmode is IDLE and the buttons are released
316 if (newmode == IDLE && !button1down && !button2down && !button3down) {
317 hasPanned = false;
318 hasDragged = false;
319 hasZoomed = false;
320 }
321
322 if (newmode != curmode) {
323 this->setViewingMode(newmode);
324 }
325
326 // If not handled in this class, pass on upwards in the inheritance
327 // hierarchy.
328 if (!processed) {
329 processed = inherited::processSoEvent(ev);
330 }
331
332 return processed;
333 }