FreeCAD

Форк
0
/
BlenderNavigationStyle.cpp 
307 строк · 11.7 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23

24
#include "PreCompiled.h"
25
#ifndef _PreComp_
26
# include <Inventor/nodes/SoCamera.h>
27
# include <QApplication>
28
#endif
29

30
#include "NavigationStyle.h"
31
#include "View3DInventorViewer.h"
32

33

34
using namespace Gui;
35

36
// ----------------------------------------------------------------------------------
37

38
/* TRANSLATOR Gui::BlenderNavigationStyle */
39

40
TYPESYSTEM_SOURCE(Gui::BlenderNavigationStyle, Gui::UserNavigationStyle)
41

42
BlenderNavigationStyle::BlenderNavigationStyle() : lockButton1(false)
43
{
44
}
45

46
BlenderNavigationStyle::~BlenderNavigationStyle() = default;
47

48
const char* BlenderNavigationStyle::mouseButtons(ViewerMode mode)
49
{
50
    switch (mode) {
51
    case NavigationStyle::SELECTION:
52
        return QT_TR_NOOP("Press left mouse button");
53
    case NavigationStyle::PANNING:
54
        return QT_TR_NOOP("Press SHIFT and middle mouse button");
55
    case NavigationStyle::DRAGGING:
56
        return QT_TR_NOOP("Press middle mouse button");
57
    case NavigationStyle::ZOOMING:
58
        return QT_TR_NOOP("Scroll mouse wheel");
59
    default:
60
        return "No description";
61
    }
62
}
63

64
SbBool BlenderNavigationStyle::processSoEvent(const SoEvent * const ev)
65
{
66
    // Events when in "ready-to-seek" mode are ignored, except those
67
    // which influence the seek mode itself -- these are handled further
68
    // up the inheritance hierarchy.
69
    if (this->isSeekMode()) {
70
        return inherited::processSoEvent(ev);
71
    }
72
    // Switch off viewing mode (Bug #0000911)
73
    if (!this->isSeekMode() && !this->isAnimating() && this->isViewing())
74
        this->setViewing(false); // by default disable viewing mode to render the scene
75

76
    const SoType type(ev->getTypeId());
77

78
    const SbViewportRegion & vp = viewer->getSoRenderManager()->getViewportRegion();
79
    const SbVec2s pos(ev->getPosition());
80
    const SbVec2f posn = normalizePixelPos(pos);
81

82
    const SbVec2f prevnormalized = this->lastmouseposition;
83
    this->lastmouseposition = posn;
84

85
    // Set to true if any event processing happened. Note that it is not
86
    // necessary to restrict ourselves to only do one "action" for an
87
    // event, we only need this flag to see if any processing happened
88
    // at all.
89
    SbBool processed = false;
90

91
    const ViewerMode curmode = this->currentmode;
92
    ViewerMode newmode = curmode;
93

94
    // Mismatches in state of the modifier keys happens if the user
95
    // presses or releases them outside the viewer window.
96
    syncModifierKeys(ev);
97

98
    // give the nodes in the foreground root the chance to handle events (e.g color bar)
99
    if (!viewer->isEditing()) {
100
        processed = handleEventInForeground(ev);
101
        if (processed)
102
            return true;
103
    }
104

105
    // Keyboard handling
106
    if (type.isDerivedFrom(SoKeyboardEvent::getClassTypeId())) {
107
        const auto event = static_cast<const SoKeyboardEvent *>(ev);
108
        processed = processKeyboardEvent(event);
109
    }
110

111
    // Mouse Button / Spaceball Button handling
112
    if (type.isDerivedFrom(SoMouseButtonEvent::getClassTypeId())) {
113
        const auto * const event = (const SoMouseButtonEvent *) ev;
114
        const int button = event->getButton();
115
        const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false;
116

117
        //SoDebugError::postInfo("processSoEvent", "button = %d", button);
118
        switch (button) {
119
        case SoMouseButtonEvent::BUTTON1:
120
            this->lockrecenter = true;
121
            this->button1down = press;
122
            if (press && (this->currentmode == NavigationStyle::SEEK_WAIT_MODE)) {
123
                newmode = NavigationStyle::SEEK_MODE;
124
                this->seekToPoint(pos); // implicitly calls interactiveCountInc()
125
                processed = true;
126
            }
127
            else if (press && (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 && (this->currentmode == NavigationStyle::PANNING ||
164
                          this->currentmode == NavigationStyle::ZOOMING)) {
165
                newmode = NavigationStyle::DRAGGING;
166
                saveCursorPosition(ev);
167
                this->centerTime = ev->getTime();
168
                processed = true;
169
            }
170
            this->button2down = press;
171
            break;
172
        case SoMouseButtonEvent::BUTTON3:
173
            if (press) {
174
                this->centerTime = ev->getTime();
175
                setupPanningPlane(getCamera());
176
                this->lockrecenter = false;
177
            }
178
            else {
179
                SbTime tmp = (ev->getTime() - this->centerTime);
180
                float dci = (float)QApplication::doubleClickInterval()/1000.0f;
181
                // is it just a middle click?
182
                if (tmp.getValue() < dci && !this->lockrecenter) {
183
                    lookAtPoint(pos);
184
                    processed = true;
185
                }
186
            }
187
            this->button3down = press;
188
            break;
189
        default:
190
            break;
191
        }
192
    }
193

194
    // Mouse Movement handling
195
    if (type.isDerivedFrom(SoLocation2Event::getClassTypeId())) {
196
        this->lockrecenter = true;
197
        const auto * const event = (const SoLocation2Event *) ev;
198
        if (this->currentmode == NavigationStyle::ZOOMING) {
199
            this->zoomByCursor(posn, prevnormalized);
200
            newmode = NavigationStyle::SELECTION;
201
            processed = true;
202
        }
203
        else if (this->currentmode == NavigationStyle::PANNING) {
204
            float ratio = vp.getViewportAspectRatio();
205
            panCamera(viewer->getSoRenderManager()->getCamera(), ratio, this->panningplane, posn, prevnormalized);
206
            newmode = NavigationStyle::SELECTION;
207
            processed = true;
208
        }
209
        else if (this->currentmode == NavigationStyle::DRAGGING) {
210
            this->addToLog(event->getPosition(), event->getTime());
211
            this->spin(posn);
212
            moveCursorPosition();
213
            processed = true;
214
        }
215
    }
216

217
    // Spaceball & Joystick handling
218
    if (type.isDerivedFrom(SoMotion3Event::getClassTypeId())) {
219
        const auto * const event = static_cast<const SoMotion3Event *>(ev);
220
        if (event)
221
            this->processMotionEvent(event);
222
        processed = true;
223
    }
224

225
    enum {
226
        BUTTON1DOWN = 1 << 0,
227
        BUTTON3DOWN = 1 << 1,
228
        CTRLDOWN =    1 << 2,
229
        SHIFTDOWN =   1 << 3,
230
        BUTTON2DOWN = 1 << 4
231
    };
232
    unsigned int combo =
233
        (this->button1down ? BUTTON1DOWN : 0) |
234
        (this->button2down ? BUTTON2DOWN : 0) |
235
        (this->button3down ? BUTTON3DOWN : 0) |
236
        (this->ctrldown ? CTRLDOWN : 0) |
237
        (this->shiftdown ? SHIFTDOWN : 0);
238

239
    switch (combo) {
240
    case 0:
241
        if (curmode == NavigationStyle::SPINNING) { break; }
242
        newmode = NavigationStyle::IDLE;
243
        // The left mouse button has been released right now
244
        if (this->lockButton1) {
245
            this->lockButton1 = false;
246
            if (curmode != NavigationStyle::SELECTION) {
247
                processed = true;
248
            }
249
        }
250
        break;
251
    case BUTTON1DOWN:
252
    case CTRLDOWN|BUTTON1DOWN:
253
        // make sure not to change the selection when stopping spinning
254
        if (curmode == NavigationStyle::SPINNING
255
            || (this->lockButton1 && curmode != NavigationStyle::SELECTION)) {
256
            newmode = NavigationStyle::IDLE;
257
        }
258
        else {
259
            newmode = NavigationStyle::SELECTION;
260
        }
261
        break;
262
    case BUTTON1DOWN|BUTTON2DOWN:
263
        newmode = NavigationStyle::PANNING;
264
        break;
265
    case SHIFTDOWN|BUTTON3DOWN:
266
        newmode = NavigationStyle::PANNING;
267
        break;
268
    case BUTTON3DOWN:
269
        if (newmode != NavigationStyle::DRAGGING) {
270
            saveCursorPosition(ev);
271
        }
272
        newmode = NavigationStyle::DRAGGING;
273
        break;
274
    case CTRLDOWN|SHIFTDOWN|BUTTON2DOWN:
275
    case CTRLDOWN|BUTTON3DOWN:
276
        newmode = NavigationStyle::ZOOMING;
277
        break;
278

279
    default:
280
        break;
281
    }
282

283
    // If the selection button is pressed together with another button
284
    // and the other button is released, don't switch to selection mode.
285
    // Process when selection button is pressed together with other buttons that could trigger different actions.
286
    if (this->button1down && (this->button2down || this->button3down)) {
287
        this->lockButton1 = true;
288
        processed = true;
289
    }
290

291
    // Prevent interrupting rubber-band selection in sketcher
292
    if (viewer->isEditing() && curmode == NavigationStyle::SELECTION && newmode != NavigationStyle::IDLE) {
293
        newmode = NavigationStyle::SELECTION;
294
        processed = false;
295
    }
296

297
    if (newmode != curmode) {
298
        this->setViewingMode(newmode);
299
    }
300

301
    // If not handled in this class, pass on upwards in the inheritance
302
    // hierarchy.
303
    if (!processed)
304
        processed = inherited::processSoEvent(ev);
305

306
    return processed;
307
}
308

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.