Debian Codegraph @

src/Gui/NaviCube.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2017 Kustaa Nyholm <[email protected]> *
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 #include <FCConfig.h>
24
25 #include <algorithm>
26 #include <numbers>
27 #ifdef FC_OS_WIN32
28 # include <windows.h>
29 #endif
30 #ifdef FC_OS_MACOSX
31 # include <OpenGL/gl.h>
32 #else
33 # include <GL/gl.h>
34 #endif
35 #include <boost/math/constants/constants.hpp>
36 #include <Inventor/nodes/SoOrthographicCamera.h>
37 #include <Inventor/events/SoEvent.h>
38 #include <Inventor/events/SoLocation2Event.h>
39 #include <Inventor/events/SoMouseButtonEvent.h>
40 #include <QApplication>
41 #include <QCursor>
42 #include <QImage>
43 #include <QMenu>
44 #include <QOpenGLFramebufferObject>
45 #include <QOpenGLTexture>
46 #include <QOpenGLWidget>
47 #include <QPainterPath>
48
49 #include <Base/Color.h>
50 #include <Base/Tools.h>
51 #include <Eigen/Dense>
52
53 #include "NaviCube.h"
54 #include "Application.h"
55 #include "Command.h"
56 #include "Action.h"
57 #include "MainWindow.h"
58 #include "Navigation/NavigationAnimation.h"
59 #include "View3DInventorViewer.h"
60 #include "View3DInventor.h"
61 #include "ViewParams.h"
62
63
64 using namespace Eigen;
65 using namespace std;
66 using namespace Gui;
67
68 class NaviCubeImplementation
69 {
70 public:
71 explicit NaviCubeImplementation(Gui::View3DInventorViewer*);
72 ~NaviCubeImplementation();
73 void drawNaviCube();
74 void createContextMenu(const std::vector<std::string>& cmd);
75 void createCubeFaceTextures();
76
77 void moveToCorner(NaviCube::Corner c);
78 void setLabels(const std::vector<std::string>& labels);
79
80 bool processSoEvent(const SoEvent* ev);
81 void setSize(int size);
82
83 private:
84 enum class PickId
85 {
86 None,
87 Front,
88 Top,
89 Right,
90 Rear,
91 Bottom,
92 Left,
93 FrontTop,
94 FrontBottom,
95 FrontRight,
96 FrontLeft,
97 RearTop,
98 RearBottom,
99 RearRight,
100 RearLeft,
101 TopRight,
102 TopLeft,
103 BottomRight,
104 BottomLeft,
105 FrontTopRight,
106 FrontTopLeft,
107 FrontBottomRight,
108 FrontBottomLeft,
109 RearTopRight,
110 RearTopLeft,
111 RearBottomRight,
112 RearBottomLeft,
113 ArrowNorth,
114 ArrowSouth,
115 ArrowEast,
116 ArrowWest,
117 ArrowRight,
118 ArrowLeft,
119 DotBackside,
120 ViewMenu
121 };
122 enum class DirId
123 {
124 Custom,
125 Up,
126 Right,
127 Out
128 };
129 enum class ShapeId
130 {
131 None,
132 Main,
133 Edge,
134 Corner,
135 Button
136 };
137 struct Face
138 {
139 ShapeId type;
140 vector<Vector3f> vertexArray;
141 // The rotation is the standard orientation for the faces of the cube
142 // For the flat buttons the rotation contains the direction of the rotation
143 // The standard orientation is the desired camera orientation when a face is selected and
144 // rotate to nearest is disabled
145 SbRotation rotation;
146 };
147 struct LabelTexture
148 {
149 vector<Vector3f> vertexArray;
150 qreal fontSize;
151 QOpenGLTexture* texture = nullptr;
152 string label;
153 };
154 bool mousePressed(short x, short y);
155 bool mouseReleased(short x, short y);
156 bool mouseMoved(short x, short y);
157 PickId pickFace(short x, short y);
158 bool inDragZone(short x, short y);
159
160 void prepare();
161 void handleResize();
162 void handleMenu();
163
164 void setHilite(PickId);
165
166 void addCubeFace(const Vector3f&, const Vector3f&, ShapeId, PickId, float rotZ = 0.0);
167 void addButtonFace(PickId, const SbVec3f& direction = SbVec3f(0, 0, 0));
168
169 QString str(const char* str);
170 QMenu* createNaviCubeMenu();
171 void drawNaviCube(bool picking, float opacity);
172 bool isFramebufferValid() const;
173 void ensureFramebufferValid();
174
175 SbRotation getNearestOrientation(PickId pickId);
176 qreal getPhysicalCubeWidgetSize();
177
178 public:
179 static int m_CubeWidgetSize;
180 QColor m_BaseColor;
181 QColor m_EmphaseColor;
182 QColor m_HiliteColor;
183 bool m_ShowCS = true;
184 PickId m_HiliteId = PickId::None;
185 double m_BorderWidth = 1.1;
186 bool m_RotateToNearest = true;
187 int m_NaviStepByTurn = 8;
188 float m_FontZoom = 0.3F;
189 float m_Chamfer = 0.12F;
190 std::string m_TextFont;
191 int m_FontWeight = 0;
192 int m_FontStretch = 0;
193 float m_InactiveOpacity = 0.5;
194 SbVec2s m_PosOffset = SbVec2s(0, 0);
195
196 Base::Color m_xColor;
197 Base::Color m_yColor;
198 Base::Color m_zColor;
199
200 bool m_Prepared = false;
201 static vector<string> m_commands;
202 bool m_Draggable = false;
203 SbVec2s m_ViewSize = SbVec2s(0, 0);
204
205 private:
206 bool m_MouseDown = false;
207 bool m_Dragging = false;
208 bool m_MightDrag = false;
209 bool m_Hovering = false;
210
211 SbVec2f m_RelPos = SbVec2f(1.0f, 1.0f);
212 SbVec2s m_PosAreaBase = SbVec2s(0, 0);
213 SbVec2s m_PosAreaSize = SbVec2s(0, 0);
214 qreal m_DevicePixelRatio = 1.0;
215
216 QOpenGLFramebufferObject* m_PickingFramebuffer;
217 Gui::View3DInventorViewer* m_View3DInventorViewer;
218
219 map<PickId, Face> m_Faces;
220 map<PickId, LabelTexture> m_LabelTextures;
221
222 QMenu* m_Menu;
223
224 std::shared_ptr<NavigationAnimation> m_flatButtonAnimation;
225 SbRotation m_flatButtonTargetOrientation;
226 };
227
228 int NaviCubeImplementation::m_CubeWidgetSize = 132;
229
230 int NaviCube::getNaviCubeSize()
231 {
232 return NaviCubeImplementation::m_CubeWidgetSize;
233 }
234
235 NaviCube::NaviCube(Gui::View3DInventorViewer* viewer)
236 {
237 m_NaviCubeImplementation = new NaviCubeImplementation(viewer);
238 updateColors();
239 }
240
241 NaviCube::~NaviCube()
242 {
243 delete m_NaviCubeImplementation;
244 }
245
246 void NaviCube::drawNaviCube()
247 {
248 m_NaviCubeImplementation->drawNaviCube();
249 }
250
251 void NaviCube::createContextMenu(const std::vector<std::string>& cmd)
252 {
253 m_NaviCubeImplementation->createContextMenu(cmd);
254 }
255
256 bool NaviCube::processSoEvent(const SoEvent* ev)
257 {
258 return m_NaviCubeImplementation->processSoEvent(ev);
259 }
260
261 vector<string> NaviCubeImplementation::m_commands;
262
263 void NaviCube::setCorner(Corner c)
264 {
265 m_NaviCubeImplementation->moveToCorner(c);
266 }
267
268 void NaviCube::setOffset(int x, int y)
269 {
270 m_NaviCubeImplementation->m_PosOffset = SbVec2s(x, y);
271 m_NaviCubeImplementation->m_ViewSize = SbVec2s(0, 0);
272 }
273
274 bool NaviCube::isDraggable()
275 {
276 return m_NaviCubeImplementation->m_Draggable;
277 }
278
279 void NaviCube::setDraggable(bool draggable)
280 {
281 m_NaviCubeImplementation->m_Draggable = draggable;
282 }
283
284 void NaviCube::setSize(int size)
285 {
286 m_NaviCubeImplementation->setSize(size);
287 }
288
289 void NaviCube::setChamfer(float chamfer)
290 {
291 m_NaviCubeImplementation->m_Chamfer = min(max(0.05f, chamfer), 0.18f);
292 m_NaviCubeImplementation->m_Prepared = false;
293 }
294
295 void NaviCube::setNaviRotateToNearest(bool toNearest)
296 {
297 m_NaviCubeImplementation->m_RotateToNearest = toNearest;
298 }
299
300 void NaviCube::setNaviStepByTurn(int steps)
301 {
302 m_NaviCubeImplementation->m_NaviStepByTurn = steps;
303 }
304
305 void NaviCube::setFont(std::string font)
306 {
307 m_NaviCubeImplementation->m_TextFont = font;
308 m_NaviCubeImplementation->m_Prepared = false;
309 }
310
311 void NaviCube::setFontWeight(int weight)
312 {
313 m_NaviCubeImplementation->m_FontWeight = weight;
314 m_NaviCubeImplementation->m_Prepared = false;
315 }
316
317 void NaviCube::setFontStretch(int stretch)
318 {
319 m_NaviCubeImplementation->m_FontStretch = stretch;
320 m_NaviCubeImplementation->m_Prepared = false;
321 }
322
323 void NaviCube::setFontZoom(float zoom)
324 {
325 m_NaviCubeImplementation->m_FontZoom = zoom;
326 m_NaviCubeImplementation->m_Prepared = false;
327 }
328
329 void NaviCube::setBaseColor(QColor bColor)
330 {
331 m_NaviCubeImplementation->m_BaseColor = bColor;
332 }
333
334 void NaviCube::setEmphaseColor(QColor eColor)
335 {
336 m_NaviCubeImplementation->m_EmphaseColor = eColor;
337 m_NaviCubeImplementation->m_Prepared = false;
338 }
339
340 void NaviCube::setHiliteColor(QColor HiliteColor)
341 {
342 m_NaviCubeImplementation->m_HiliteColor = HiliteColor;
343 }
344
345 void NaviCube::setBorderWidth(double BorderWidth)
346 {
347 m_NaviCubeImplementation->m_BorderWidth = BorderWidth;
348 }
349
350 void NaviCube::setShowCS(bool showCS)
351 {
352 m_NaviCubeImplementation->m_ShowCS = showCS;
353 }
354
355 void NaviCube::setNaviCubeLabels(const std::vector<std::string>& labels)
356 {
357 m_NaviCubeImplementation->setLabels(labels);
358 }
359
360 void NaviCube::setInactiveOpacity(float opacity)
361 {
362 m_NaviCubeImplementation->m_InactiveOpacity = opacity;
363 }
364
365 qreal NaviCubeImplementation::getPhysicalCubeWidgetSize()
366 {
367 return m_CubeWidgetSize * m_DevicePixelRatio;
368 }
369
370 void NaviCubeImplementation::setLabels(const std::vector<std::string>& labels)
371 {
372 m_LabelTextures[PickId::Front].label = labels[0];
373 m_LabelTextures[PickId::Top].label = labels[1];
374 m_LabelTextures[PickId::Right].label = labels[2];
375 m_LabelTextures[PickId::Rear].label = labels[3];
376 m_LabelTextures[PickId::Bottom].label = labels[4];
377 m_LabelTextures[PickId::Left].label = labels[5];
378 m_Prepared = false;
379 }
380
381 NaviCubeImplementation::NaviCubeImplementation(Gui::View3DInventorViewer* viewer)
382 : m_BaseColor {226, 232, 239}
383 , m_HiliteColor {170, 226, 255}
384 {
385 m_View3DInventorViewer = viewer;
386 m_PickingFramebuffer = nullptr;
387 m_Menu = createNaviCubeMenu();
388 }
389
390 NaviCubeImplementation::~NaviCubeImplementation()
391 {
392 delete m_Menu;
393 if (m_PickingFramebuffer) {
394 delete m_PickingFramebuffer;
395 }
396 for (auto tex : m_LabelTextures) {
397 delete tex.second.texture;
398 }
399 }
400
401 void NaviCubeImplementation::moveToCorner(NaviCube::Corner c)
402 {
403 if (c == NaviCube::TopLeftCorner) {
404 m_RelPos = SbVec2f(0.0f, 1.0f);
405 }
406 else if (c == NaviCube::TopRightCorner) {
407 m_RelPos = SbVec2f(1.0f, 1.0f);
408 }
409 else if (c == NaviCube::BottomLeftCorner) {
410 m_RelPos = SbVec2f(0.0f, 0.0f);
411 }
412 else if (c == NaviCube::BottomRightCorner) {
413 m_RelPos = SbVec2f(1.0f, 0.0f);
414 }
415 }
416
417 auto convertWeights = [](int weight) -> QFont::Weight {
418 if (weight >= 87) {
419 return QFont::Black;
420 }
421 if (weight >= 81) {
422 return QFont::ExtraBold;
423 }
424 if (weight >= 75) {
425 return QFont::Bold;
426 }
427 if (weight >= 63) {
428 return QFont::DemiBold;
429 }
430 if (weight >= 57) {
431 return QFont::Medium;
432 }
433 if (weight >= 50) {
434 return QFont::Normal;
435 }
436 if (weight >= 25) {
437 return QFont::Light;
438 }
439 if (weight >= 12) {
440 return QFont::ExtraLight;
441 }
442 return QFont::Thin;
443 };
444
445 int imageVerticalBalance(QImage p, int sizeHint)
446 {
447 if (sizeHint < 0) {
448 return 0;
449 }
450
451 int h = p.height();
452 int startRow = (h - sizeHint) / 2;
453 bool done = false;
454 int x, bottom, top;
455 for (top = startRow; top < h; top++) {
456 for (x = 0; x < p.width(); x++) {
457 if (qAlpha(p.pixel(x, top))) {
458 done = true;
459 break;
460 }
461 }
462 if (done) {
463 break;
464 }
465 }
466 for (bottom = startRow; bottom < h; bottom++) {
467 for (x = 0; x < p.width(); x++) {
468 if (qAlpha(p.pixel(x, h - 1 - bottom))) {
469 return (bottom - top) / 2;
470 }
471 }
472 }
473 return 0;
474 }
475
476 void NaviCubeImplementation::createCubeFaceTextures()
477 {
478 int texSize = 192; // Works well for the max cube size 1024
479 QFont font;
480 if (m_TextFont.empty()) {
481 font.fromString(QStringLiteral("Arial"));
482 }
483 else {
484 font.fromString(QString::fromStdString(m_TextFont));
485 }
486 font.setStyleHint(QFont::SansSerif);
487 if (m_FontWeight > 0) {
488 font.setWeight(convertWeights(m_FontWeight));
489 }
490 if (m_FontStretch > 0) {
491 font.setStretch(m_FontStretch);
492 }
493 font.setPointSizeF(texSize);
494 QFontMetrics fm(font);
495 qreal minFontSize = texSize;
496 qreal maxFontSize = 0.;
497 vector<PickId> mains
498 = {PickId::Front, PickId::Top, PickId::Right, PickId::Rear, PickId::Bottom, PickId::Left};
499 for (PickId pickId : mains) {
500 auto t = QString::fromUtf8(m_LabelTextures[pickId].label.c_str());
501 QRect br = fm.boundingRect(t);
502 float scale = (float)texSize / max(br.width(), br.height());
503 m_LabelTextures[pickId].fontSize = texSize * scale;
504 minFontSize = std::min(minFontSize, m_LabelTextures[pickId].fontSize);
505 maxFontSize = std::max(maxFontSize, m_LabelTextures[pickId].fontSize);
506 }
507 if (m_FontZoom > 0.0) {
508 maxFontSize = minFontSize + (maxFontSize - minFontSize) * m_FontZoom;
509 }
510 else {
511 maxFontSize = minFontSize * std::pow(2.0, m_FontZoom);
512 }
513 for (PickId pickId : mains) {
514 QImage image(texSize, texSize, QImage::Format_ARGB32);
515 image.fill(qRgba(255, 255, 255, 0));
516 if (m_LabelTextures[pickId].fontSize > 0.5) {
517 // 5% margin looks nice and prevents some artifacts
518 font.setPointSizeF(std::min(m_LabelTextures[pickId].fontSize, maxFontSize) * 0.9);
519 QPainter paint;
520 paint.begin(&image);
521 paint.setRenderHints(
522 QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform
523 );
524 paint.setPen(Qt::white);
525 QString text = QString::fromUtf8(m_LabelTextures[pickId].label.c_str());
526 paint.setFont(font);
527 paint.drawText(QRect(0, 0, texSize, texSize), Qt::AlignCenter, text);
528 int offset = imageVerticalBalance(image, font.pointSize());
529 image.fill(qRgba(255, 255, 255, 0));
530 paint.drawText(QRect(0, offset, texSize, texSize), Qt::AlignCenter, text);
531 paint.end();
532 }
533
534 if (m_LabelTextures[pickId].texture) {
535 delete m_LabelTextures[pickId].texture;
536 }
537 m_LabelTextures[pickId].texture = new QOpenGLTexture(image.mirrored());
538 m_LabelTextures[pickId].texture->setMaximumAnisotropy(4.0);
539 m_LabelTextures[pickId].texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
540 m_LabelTextures[pickId].texture->setMagnificationFilter(QOpenGLTexture::Linear);
541 m_LabelTextures[pickId].texture->generateMipMaps();
542 }
543 }
544
545 void NaviCubeImplementation::addButtonFace(PickId pickId, const SbVec3f& direction)
546 {
547 if (m_Faces[pickId].vertexArray.size()) {
548 m_Faces[pickId].vertexArray.clear();
549 }
550 float scale = 0.005F;
551 float offx = 0.5F;
552 float offy = 0.5F;
553 vector<float> pointData;
554
555 switch (pickId) {
556 default:
557 break;
558 case PickId::ArrowRight:
559 case PickId::ArrowLeft: {
560 pointData = {66.6F, -66.6F, // outer curve
561 58.3F, -74.0F, 49.2F, -80.3F, 39.4F,
562 -85.5F, 29.0F, -89.5F, 25.3F, -78.1F, // inner curve
563 34.3F, -74.3F, 42.8F, -69.9F, 50.8F,
564 -64.4F, 58.1F, -58.1F, 53.8F, -53.8F, // arrowhead
565 74.7F, -46.8F, 70.7F, -70.4F};
566 break;
567 }
568 case PickId::ArrowWest:
569 case PickId::ArrowNorth:
570 case PickId::ArrowSouth:
571 case PickId::ArrowEast: {
572 pointData = {100., 0., 80., -18., 80., 18.};
573 break;
574 }
575 case PickId::ViewMenu: {
576 offx = 0.84F;
577 offy = 0.84F;
578 pointData = {0., 0., // top rhombus
579 15., -6., 0., -12., -15., -6., 0., 0., // left rhombus
580 -15., -6., -15., 12., 0., 18., 0., 0., // right rhombus
581 0., 18., 15., 12., 15., -6.};
582 break;
583 }
584 case PickId::DotBackside: {
585 int steps = 16;
586 for (int i = 0; i < steps; i++) {
587 float angle = 2.0f * std::numbers::pi_v<float> * ((float)i + 0.5) / (float)steps;
588 pointData.emplace_back(10. * cos(angle) + 87.);
589 pointData.emplace_back(10. * sin(angle) - 87.);
590 }
591 break;
592 }
593 }
594
595 int count = static_cast<int>(pointData.size()) / 2;
596 m_Faces[pickId].vertexArray.reserve(count);
597 for (int i = 0; i < count; i++) {
598 float x = pointData[i * 2] * scale + offx;
599 float y = pointData[i * 2 + 1] * scale + offy;
600 if (pickId == PickId::ArrowNorth || pickId == PickId::ArrowWest
601 || pickId == PickId::ArrowLeft) {
602 x = 1.0 - x;
603 }
604 if (pickId == PickId::ArrowSouth || pickId == PickId::ArrowNorth) {
605 m_Faces[pickId].vertexArray.emplace_back(Vector3f(y, x, 0.0));
606 }
607 else {
608 m_Faces[pickId].vertexArray.emplace_back(Vector3f(x, y, 0.0));
609 }
610 }
611 m_Faces[pickId].type = ShapeId::Button;
612 m_Faces[pickId].rotation = SbRotation(direction, 1).inverse();
613 }
614
615 void NaviCubeImplementation::addCubeFace(
616 const Vector3f& x,
617 const Vector3f& z,
618 ShapeId shapeType,
619 PickId pickId,
620 float rotZ
621 )
622 {
623 m_Faces[pickId].vertexArray.clear();
624 m_Faces[pickId].type = shapeType;
625
626 Vector3f y = x.cross(-z);
627
628 // Determine the standard orientations based on vector x and vector z
629 // Rotate by an additional rotZ if vector x and vector z are not already the standard orientation
630
631 // Create normalized vectors for x, y and z
632 SbVec3f xN(x.x(), x.y(), x.z());
633 SbVec3f yN(y.x(), y.y(), y.z());
634 SbVec3f zN(z.x(), z.y(), z.z());
635 xN.normalize();
636 yN.normalize();
637 zN.normalize();
638
639 // Create a rotation matrix
640 SbMatrix R(xN[0], yN[0], zN[0], 0, xN[1], yN[1], zN[1], 0, xN[2], yN[2], zN[2], 0, 0, 0, 0, 1);
641
642 // Store the standard orientation
643 m_Faces[pickId].rotation = (SbRotation(R) * SbRotation(SbVec3f(0, 0, 1), rotZ)).inverse();
644
645 if (shapeType == ShapeId::Corner) {
646 auto xC = x * m_Chamfer;
647 auto yC = y * m_Chamfer;
648 auto zC = (1 - 2 * m_Chamfer) * z;
649 m_Faces[pickId].vertexArray.reserve(6);
650 m_Faces[pickId].vertexArray.emplace_back(zC - 2 * xC);
651 m_Faces[pickId].vertexArray.emplace_back(zC - xC - yC);
652 m_Faces[pickId].vertexArray.emplace_back(zC + xC - yC);
653 m_Faces[pickId].vertexArray.emplace_back(zC + 2 * xC);
654 m_Faces[pickId].vertexArray.emplace_back(zC + xC + yC);
655 m_Faces[pickId].vertexArray.emplace_back(zC - xC + yC);
656 }
657 else if (shapeType == ShapeId::Edge) {
658 auto x4 = x * (1 - m_Chamfer * 4);
659 auto yE = y * m_Chamfer;
660 auto zE = z * (1 - m_Chamfer);
661 m_Faces[pickId].vertexArray.reserve(4);
662 m_Faces[pickId].vertexArray.emplace_back(zE - x4 - yE);
663 m_Faces[pickId].vertexArray.emplace_back(zE + x4 - yE);
664 m_Faces[pickId].vertexArray.emplace_back(zE + x4 + yE);
665 m_Faces[pickId].vertexArray.emplace_back(zE - x4 + yE);
666 }
667 else if (shapeType == ShapeId::Main) {
668 auto x2 = x * (1 - m_Chamfer * 2);
669 auto y2 = y * (1 - m_Chamfer * 2);
670 auto x4 = x * (1 - m_Chamfer * 4);
671 auto y4 = y * (1 - m_Chamfer * 4);
672 m_Faces[pickId].vertexArray.reserve(8);
673 m_Faces[pickId].vertexArray.emplace_back(z - x2 - y4);
674 m_Faces[pickId].vertexArray.emplace_back(z - x4 - y2);
675 m_Faces[pickId].vertexArray.emplace_back(z + x4 - y2);
676 m_Faces[pickId].vertexArray.emplace_back(z + x2 - y4);
677
678 m_Faces[pickId].vertexArray.emplace_back(z + x2 + y4);
679 m_Faces[pickId].vertexArray.emplace_back(z + x4 + y2);
680 m_Faces[pickId].vertexArray.emplace_back(z - x4 + y2);
681 m_Faces[pickId].vertexArray.emplace_back(z - x2 + y4);
682
683 m_LabelTextures[pickId].vertexArray.clear();
684 m_LabelTextures[pickId].vertexArray.emplace_back(z - x2 - y2);
685 m_LabelTextures[pickId].vertexArray.emplace_back(z + x2 - y2);
686 m_LabelTextures[pickId].vertexArray.emplace_back(z + x2 + y2);
687 m_LabelTextures[pickId].vertexArray.emplace_back(z - x2 + y2);
688 }
689 }
690
691 void NaviCubeImplementation::setSize(int size)
692 {
693 m_CubeWidgetSize = size;
694 m_ViewSize = SbVec2s(0, 0);
695 m_Prepared = false;
696 }
697
698 void NaviCubeImplementation::prepare()
699 {
700 constexpr float pi = std::numbers::pi_v<float>;
701 constexpr float pi1_2 = pi / 2;
702
703 createCubeFaceTextures();
704
705 Vector3f x(1, 0, 0);
706 Vector3f y(0, 1, 0);
707 Vector3f z(0, 0, 1);
708
709 // create the main faces
710 addCubeFace(x, z, ShapeId::Main, PickId::Top);
711 addCubeFace(x, -y, ShapeId::Main, PickId::Front);
712 addCubeFace(-y, -x, ShapeId::Main, PickId::Left);
713 addCubeFace(-x, y, ShapeId::Main, PickId::Rear);
714 addCubeFace(y, x, ShapeId::Main, PickId::Right);
715 addCubeFace(x, -z, ShapeId::Main, PickId::Bottom);
716
717 // create corner faces
718 addCubeFace(-x - y, x - y + z, ShapeId::Corner, PickId::FrontTopRight, pi);
719 addCubeFace(-x + y, -x - y + z, ShapeId::Corner, PickId::FrontTopLeft, pi);
720 addCubeFace(x + y, x - y - z, ShapeId::Corner, PickId::FrontBottomRight);
721 addCubeFace(x - y, -x - y - z, ShapeId::Corner, PickId::FrontBottomLeft);
722 addCubeFace(x - y, x + y + z, ShapeId::Corner, PickId::RearTopRight, pi);
723 addCubeFace(x + y, -x + y + z, ShapeId::Corner, PickId::RearTopLeft, pi);
724 addCubeFace(-x + y, x + y - z, ShapeId::Corner, PickId::RearBottomRight);
725 addCubeFace(-x - y, -x + y - z, ShapeId::Corner, PickId::RearBottomLeft);
726
727 // create edge faces
728 addCubeFace(x, z - y, ShapeId::Edge, PickId::FrontTop);
729 addCubeFace(x, -z - y, ShapeId::Edge, PickId::FrontBottom);
730 addCubeFace(x, y - z, ShapeId::Edge, PickId::RearBottom, pi);
731 addCubeFace(x, y + z, ShapeId::Edge, PickId::RearTop, pi);
732 addCubeFace(z, x + y, ShapeId::Edge, PickId::RearRight, pi1_2);
733 addCubeFace(z, x - y, ShapeId::Edge, PickId::FrontRight, pi1_2);
734 addCubeFace(z, -x - y, ShapeId::Edge, PickId::FrontLeft, pi1_2);
735 addCubeFace(z, y - x, ShapeId::Edge, PickId::RearLeft, pi1_2);
736 addCubeFace(y, z - x, ShapeId::Edge, PickId::TopLeft, pi);
737 addCubeFace(y, x + z, ShapeId::Edge, PickId::TopRight);
738 addCubeFace(y, x - z, ShapeId::Edge, PickId::BottomRight);
739 addCubeFace(y, -z - x, ShapeId::Edge, PickId::BottomLeft, pi);
740
741 // create the flat buttons
742 addButtonFace(PickId::ArrowNorth, SbVec3f(-1, 0, 0));
743 addButtonFace(PickId::ArrowSouth, SbVec3f(1, 0, 0));
744 addButtonFace(PickId::ArrowEast, SbVec3f(0, 1, 0));
745 addButtonFace(PickId::ArrowWest, SbVec3f(0, -1, 0));
746 addButtonFace(PickId::ArrowLeft, SbVec3f(0, 0, 1));
747 addButtonFace(PickId::ArrowRight, SbVec3f(0, 0, -1));
748 addButtonFace(PickId::DotBackside, SbVec3f(0, 1, 0));
749 addButtonFace(PickId::ViewMenu);
750
751 if (m_PickingFramebuffer) {
752 delete m_PickingFramebuffer;
753 m_PickingFramebuffer = nullptr;
754 }
755
756 ensureFramebufferValid();
757 m_View3DInventorViewer->getSoRenderManager()->scheduleRedraw();
758 }
759
760 void NaviCubeImplementation::drawNaviCube()
761 {
762 ensureFramebufferValid();
763 handleResize();
764 qreal physicalCubeWidgetSize = getPhysicalCubeWidgetSize();
765 int posX = (int)(m_RelPos[0] * m_PosAreaSize[0]) + m_PosAreaBase[0] - physicalCubeWidgetSize / 2;
766 int posY = (int)(m_RelPos[1] * m_PosAreaSize[1]) + m_PosAreaBase[1] - physicalCubeWidgetSize / 2;
767 glViewport(posX, posY, physicalCubeWidgetSize, physicalCubeWidgetSize);
768 drawNaviCube(false, m_Hovering ? 1.f : m_InactiveOpacity);
769 }
770
771 void NaviCubeImplementation::createContextMenu(const std::vector<std::string>& cmd)
772 {
773 CommandManager& rcCmdMgr = Application::Instance->commandManager();
774 m_Menu->clear();
775
776 for (const auto& i : cmd) {
777 Command* cmd = rcCmdMgr.getCommandByName(i.c_str());
778 if (cmd) {
779 cmd->addTo(m_Menu);
780 }
781 }
782 }
783
784 void NaviCubeImplementation::handleResize()
785 {
786 qreal devicePixelRatio = m_View3DInventorViewer->devicePixelRatio();
787 SbVec2s viewSize = m_View3DInventorViewer->getSoRenderManager()->getSize();
788 if (viewSize != m_ViewSize || devicePixelRatio != m_DevicePixelRatio) {
789 m_DevicePixelRatio = devicePixelRatio;
790 qreal physicalCubeWidgetSize = getPhysicalCubeWidgetSize();
791 m_PosAreaBase[0]
792 = std::min((int)(m_PosOffset[0] + physicalCubeWidgetSize * 0.55), viewSize[0] / 2);
793 m_PosAreaBase[1]
794 = std::min((int)(m_PosOffset[1] + physicalCubeWidgetSize * 0.55), viewSize[1] / 2);
795 m_PosAreaSize[0] = viewSize[0] - 2 * m_PosAreaBase[0];
796 m_PosAreaSize[1] = viewSize[1] - 2 * m_PosAreaBase[1];
797 m_ViewSize = viewSize;
798 }
799 }
800
801 void NaviCubeImplementation::drawNaviCube(bool pickMode, float opacity)
802 {
803 if (!m_Prepared) {
804 if (!m_View3DInventorViewer->viewport()) {
805 return;
806 }
807 prepare();
808 m_Prepared = true;
809 m_View3DInventorViewer->getSoRenderManager()->scheduleRedraw();
810 return;
811 }
812
813 ensureFramebufferValid();
814
815 SoCamera* cam = m_View3DInventorViewer->getSoRenderManager()->getCamera();
816 if (!cam) {
817 return;
818 }
819
820 // Store GL state.
821 glPushAttrib(GL_ALL_ATTRIB_BITS);
822
823 // configure
824 glEnable(GL_DEPTH_TEST);
825 glDepthMask(GL_TRUE);
826 glDepthRange(0.f, 1.f);
827 glClearDepth(1.f);
828 glClear(GL_DEPTH_BUFFER_BIT);
829 glDepthFunc(GL_LEQUAL);
830
831 glDisable(GL_LIGHTING);
832
833 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
834 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
835 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
836
837 glEnable(GL_CULL_FACE);
838 glCullFace(GL_BACK);
839 glFrontFace(GL_CCW);
840
841 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
842
843 if (pickMode) {
844 glDisable(GL_BLEND);
845 glShadeModel(GL_FLAT);
846 glDisable(GL_DITHER);
847 glDisable(GL_POLYGON_SMOOTH);
848 glClearColor(0, 0, 0, 1);
849 glClear(GL_COLOR_BUFFER_BIT);
850 }
851 else {
852 glEnable(GL_POLYGON_OFFSET_FILL);
853 glPolygonOffset(1.0f, 1.0f);
854 glEnable(GL_BLEND);
855 glShadeModel(GL_SMOOTH);
856 }
857
858 // mimic 3d view projection
859 glMatrixMode(GL_PROJECTION);
860 glPushMatrix();
861 glLoadIdentity();
862 const float NEARVAL = 0.1f;
863 const float FARVAL = 10.1f;
864 if (cam->getTypeId().isDerivedFrom(SoOrthographicCamera::getClassTypeId())) {
865 glOrtho(-2.1, 2.1, -2.1, 2.1, NEARVAL, FARVAL);
866 }
867 else {
868 const float dim = NEARVAL * float(tan(std::numbers::pi / 8.0)) * 1.1;
869 glFrustum(-dim, dim, -dim, dim, NEARVAL, FARVAL);
870 }
871 glMatrixMode(GL_MODELVIEW);
872 glPushMatrix();
873 SbMatrix mx;
874 mx = cam->orientation.getValue();
875 mx = mx.inverse();
876 mx[3][2] = -5.1F;
877 glLoadMatrixf((float*)mx);
878
879 glEnableClientState(GL_VERTEX_ARRAY);
880 QColor& cb = m_EmphaseColor;
881
882 // Draw coordinate system
883 if (!pickMode && m_ShowCS) {
884 glLineWidth(m_BorderWidth * 2.f);
885 glPointSize(m_BorderWidth * 2.f);
886 float a = -1.1f;
887 float b = -1.05f;
888 float c = 0.5f;
889
890 float pointData[] = {
891 b, a, a, // X1
892 c, a, a, // X2
893 a, b, a, // Y1
894 a, c, a, // Y2
895 a, a, b, // Z1
896 a, a, c, // Z2
897 a, a, a // 0
898 };
899 glVertexPointer(3, GL_FLOAT, 0, pointData);
900
901 glColor4f(m_xColor.r, m_xColor.g, m_xColor.b, opacity);
902 glDrawArrays(GL_LINES, 0, 2);
903 glDrawArrays(GL_POINTS, 0, 2);
904
905 glColor4f(m_yColor.r, m_yColor.g, m_yColor.b, opacity);
906 glDrawArrays(GL_LINES, 2, 2);
907 glDrawArrays(GL_POINTS, 2, 2);
908
909 glColor4f(m_zColor.r, m_zColor.g, m_zColor.b, opacity);
910 glDrawArrays(GL_LINES, 4, 2);
911 glDrawArrays(GL_POINTS, 4, 2);
912 }
913
914 // cube faces
915 for (const auto& pair : m_Faces) {
916 auto f = pair.second;
917 if (f.type == ShapeId::Button) {
918 continue;
919 }
920 auto pickId = pair.first;
921 if (pickMode) {
922 glColor3ub(static_cast<GLubyte>(pickId), 0, 0);
923 }
924 else {
925 QColor& c = m_HiliteId == pickId ? m_HiliteColor : m_BaseColor;
926 glColor4f(c.redF(), c.greenF(), c.blueF(), c.alphaF() * opacity);
927 }
928 glVertexPointer(3, GL_FLOAT, 0, f.vertexArray.data());
929 glDrawArrays(GL_TRIANGLE_FAN, 0, f.vertexArray.size());
930 }
931 if (!pickMode) {
932 // cube borders
933 glLineWidth(m_BorderWidth);
934 for (const auto& pair : m_Faces) {
935 auto f = pair.second;
936 if (f.type == ShapeId::Button) {
937 continue;
938 }
939 glColor4f(cb.redF(), cb.greenF(), cb.blueF(), cb.alphaF() * opacity);
940 glVertexPointer(3, GL_FLOAT, 0, f.vertexArray.data());
941 glDrawArrays(GL_LINES, 0, f.vertexArray.size());
942 }
943
944 // Label textures
945 glDisable(GL_POLYGON_OFFSET_FILL); // make sure labels are on top
946 glEnable(GL_TEXTURE_2D);
947 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
948 float texCoords[] = {0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f};
949 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
950 QColor& c = m_EmphaseColor;
951 glColor4f(c.redF(), c.greenF(), c.blueF(), c.alphaF() * opacity);
952 for (const auto& pair : m_LabelTextures) {
953 auto f = pair.second;
954 PickId pickId = pair.first;
955 glVertexPointer(3, GL_FLOAT, 0, m_LabelTextures[pickId].vertexArray.data());
956 glBindTexture(GL_TEXTURE_2D, f.texture->textureId());
957 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
958 }
959 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
960 glDisable(GL_TEXTURE_2D);
961 glEnable(GL_POLYGON_OFFSET_FILL);
962 }
963
964 // Draw the flat buttons
965 glDisable(GL_CULL_FACE);
966 glMatrixMode(GL_PROJECTION);
967 glLoadIdentity();
968 glOrtho(0.0, 1.0, 1.0, 0.0, 0.0, 1.0);
969 glMatrixMode(GL_MODELVIEW);
970 glLoadIdentity();
971
972 for (const auto& pair : m_Faces) {
973 auto f = pair.second;
974 if (f.type != ShapeId::Button) {
975 continue;
976 }
977 PickId pickId = pair.first;
978 if (pickMode) {
979 glColor3ub(static_cast<GLubyte>(pickId), 0, 0);
980 }
981 else {
982 QColor& c = m_HiliteId == pickId ? m_HiliteColor : m_BaseColor;
983 glColor4f(c.redF(), c.greenF(), c.blueF(), c.alphaF() * opacity);
984 }
985 glVertexPointer(3, GL_FLOAT, 0, f.vertexArray.data());
986 glDrawArrays(GL_TRIANGLE_FAN, 0, f.vertexArray.size());
987 if (!pickMode) {
988 glColor4f(cb.redF(), cb.greenF(), cb.blueF(), cb.alphaF() * opacity);
989 glDrawArrays(GL_LINE_LOOP, 0, f.vertexArray.size());
990 }
991 }
992
993 // Restore original state.
994 glPopMatrix();
995 glMatrixMode(GL_PROJECTION);
996 glPopMatrix();
997 glPopAttrib();
998 }
999
1000 bool NaviCubeImplementation::isFramebufferValid() const
1001 {
1002 return m_PickingFramebuffer && m_PickingFramebuffer->isValid();
1003 }
1004
1005
1006 void NaviCubeImplementation::ensureFramebufferValid()
1007 {
1008 if (!isFramebufferValid()) {
1009 if (m_PickingFramebuffer) {
1010
1011 if (!m_PickingFramebuffer->isValid()) {
1012 Base::Console().developerWarning(
1013 "NaviCube",
1014 "The frame buffer has become invalid, a new frame buffer will be created\n"
1015 );
1016 }
1017
1018 delete m_PickingFramebuffer;
1019 m_PickingFramebuffer = nullptr;
1020 }
1021
1022 qreal physicalCubeWidgetSize = getPhysicalCubeWidgetSize();
1023 m_PickingFramebuffer = new QOpenGLFramebufferObject(
1024 2 * physicalCubeWidgetSize,
1025 2 * physicalCubeWidgetSize,
1026 QOpenGLFramebufferObject::CombinedDepthStencil
1027 );
1028 }
1029 }
1030
1031 NaviCubeImplementation::PickId NaviCubeImplementation::pickFace(short x, short y)
1032 {
1033 qreal physicalCubeWidgetSize = getPhysicalCubeWidgetSize();
1034 GLubyte pixels[4] = {0};
1035 ensureFramebufferValid();
1036 if (m_PickingFramebuffer && std::abs(x) <= physicalCubeWidgetSize / 2
1037 && std::abs(y) <= physicalCubeWidgetSize / 2) {
1038 static_cast<QOpenGLWidget*>(m_View3DInventorViewer->viewport())->makeCurrent();
1039 m_PickingFramebuffer->bind();
1040
1041 glViewport(0, 0, physicalCubeWidgetSize * 2, physicalCubeWidgetSize * 2);
1042
1043 drawNaviCube(true, 1.f);
1044
1045 glFinish();
1046 glReadPixels(
1047 2 * x + physicalCubeWidgetSize,
1048 2 * y + physicalCubeWidgetSize,
1049 1,
1050 1,
1051 GL_RGBA,
1052 GL_UNSIGNED_BYTE,
1053 &pixels
1054 );
1055 m_PickingFramebuffer->release();
1056 static_cast<QOpenGLWidget*>(m_View3DInventorViewer->viewport())->doneCurrent();
1057 }
1058 return pixels[3] == 255 ? static_cast<PickId>(pixels[0]) : PickId::None;
1059 }
1060
1061 bool NaviCubeImplementation::mousePressed(short x, short y)
1062 {
1063 m_MouseDown = true;
1064 m_MightDrag = inDragZone(x, y);
1065 PickId pick = pickFace(x, y);
1066 setHilite(pick);
1067 return pick != PickId::None;
1068 }
1069
1070 void NaviCubeImplementation::handleMenu()
1071 {
1072 m_Menu->exec(QCursor::pos());
1073 }
1074
1075 SbRotation NaviCubeImplementation::getNearestOrientation(PickId pickId)
1076 {
1077 SbRotation cameraOrientation = m_View3DInventorViewer->getCameraOrientation();
1078 SbRotation standardOrientation = m_Faces[pickId].rotation;
1079
1080 SbVec3f cameraZ;
1081 cameraOrientation.multVec(SbVec3f(0, 0, 1), cameraZ);
1082
1083 SbVec3f standardZ;
1084 standardOrientation.multVec(SbVec3f(0, 0, 1), standardZ);
1085
1086 // Cleanup near zero values
1087 for (int i = 0; i < 3; i++) {
1088 if (abs(standardZ[i]) < 1e-6) {
1089 standardZ[i] = 0.0F;
1090 }
1091 }
1092 standardZ.normalize();
1093
1094 // Rotate the camera to the selected face by the smallest angle to align the z-axis
1095 SbRotation intermediateOrientation = cameraOrientation * SbRotation(cameraZ, standardZ);
1096
1097 // Find an axis and angle to go from the intermediateOrientation to the standardOrientation
1098 SbVec3f axis;
1099 float angle;
1100 SbRotation rotation = intermediateOrientation.inverse() * standardOrientation;
1101 rotation.getValue(axis, angle);
1102
1103 // Make sure the found axis aligns with the standardZ axis
1104 if (standardZ.dot(axis) < 0) {
1105 axis.negate();
1106 angle *= -1;
1107 }
1108
1109 constexpr float pi = std::numbers::pi_v<float>;
1110
1111 // Make angle positive
1112 if (angle < 0) {
1113 angle += 2 * pi;
1114 }
1115
1116 // f is a small value used to control orientation priority when the camera is almost exactly
1117 // between two orientations (e.g. +45 and -45 degrees). The standard orientation is preferred
1118 // compared to +90 and -90 degree orientations and the +90 and -90 degree orientations are
1119 // preferred compared to an upside down standard orientation
1120 float f = 0.00001F;
1121
1122 // Find the angle to rotate to the nearest orientation
1123 if (m_Faces[pickId].type == ShapeId::Corner) {
1124 // 6 possible orientations for the corners
1125 if (angle <= (pi / 6 + f)) {
1126 angle = 0;
1127 }
1128 else if (angle <= (pi / 2 + f)) {
1129 angle = pi / 3;
1130 }
1131 else if (angle < (5 * pi / 6 - f)) {
1132 angle = 2 * pi / 3;
1133 }
1134 else if (angle <= (pi + pi / 6 + f)) {
1135 angle = pi;
1136 }
1137 else if (angle < (pi + pi / 2 - f)) {
1138 angle = pi + pi / 3;
1139 }
1140 else if (angle < (pi + 5 * pi / 6 - f)) {
1141 angle = pi + 2 * pi / 3;
1142 }
1143 else {
1144 angle = 0;
1145 }
1146 }
1147 else {
1148 // 4 possible orientations for the main and edge faces
1149 if (angle <= (pi / 4 + f)) {
1150 angle = 0;
1151 }
1152 else if (angle <= (3 * pi / 4 + f)) {
1153 angle = pi / 2;
1154 }
1155 else if (angle < (pi + pi / 4 - f)) {
1156 angle = pi;
1157 }
1158 else if (angle < (pi + 3 * pi / 4 - f)) {
1159 angle = pi + pi / 2;
1160 }
1161 else {
1162 angle = 0;
1163 }
1164 }
1165
1166 // Set the rotation to go from the standard orientation to the nearest orientation
1167 rotation.setValue(standardZ, angle);
1168
1169 return standardOrientation * rotation.inverse();
1170 }
1171
1172 bool NaviCubeImplementation::mouseReleased(short x, short y)
1173 {
1174 static const float pi = boost::math::constants::pi<float>();
1175
1176 setHilite(PickId::None);
1177 m_MouseDown = false;
1178
1179 if (m_Dragging) {
1180 m_Dragging = false;
1181 }
1182 else {
1183 PickId pickId = pickFace(x, y);
1184 long step = Base::clamp(long(m_NaviStepByTurn), 4L, 36L);
1185 float rotStepAngle = (2 * std::numbers::pi) / step;
1186
1187 if (m_Faces[pickId].type == ShapeId::Main || m_Faces[pickId].type == ShapeId::Edge
1188 || m_Faces[pickId].type == ShapeId::Corner) {
1189 // Handle the cube faces
1190 SbRotation orientation;
1191 if (m_RotateToNearest) {
1192 orientation = getNearestOrientation(pickId);
1193 }
1194 else {
1195 orientation = m_Faces[pickId].rotation;
1196 }
1197 m_View3DInventorViewer->setCameraOrientation(orientation);
1198 }
1199 else if (m_Faces[pickId].type == ShapeId::Button) {
1200
1201 // Handle the menu
1202 if (pickId == PickId::ViewMenu) {
1203 handleMenu();
1204 return true;
1205 }
1206
1207 // Handle the flat buttons
1208 SbRotation rotation = m_Faces[pickId].rotation;
1209 if (pickId == PickId::DotBackside) {
1210 rotation.scaleAngle(pi);
1211 }
1212 else {
1213 rotation.scaleAngle(rotStepAngle);
1214 }
1215
1216 // If the previous flat button animation is still active then apply the rotation to the
1217 // previous target orientation, otherwise apply the rotation to the current camera
1218 // orientation
1219 if (m_flatButtonAnimation != nullptr
1220 && m_flatButtonAnimation->state() == QAbstractAnimation::Running) {
1221 m_flatButtonTargetOrientation = rotation * m_flatButtonTargetOrientation;
1222 }
1223 else {
1224 m_flatButtonTargetOrientation = rotation
1225 * m_View3DInventorViewer->getCameraOrientation();
1226 }
1227
1228 m_flatButtonAnimation = m_View3DInventorViewer->setCameraOrientation(
1229 m_flatButtonTargetOrientation
1230 );
1231 }
1232 else {
1233 return false;
1234 }
1235 }
1236 return true;
1237 }
1238
1239 void NaviCubeImplementation::setHilite(PickId hilite)
1240 {
1241 if (hilite != m_HiliteId) {
1242 m_HiliteId = hilite;
1243 m_View3DInventorViewer->getSoRenderManager()->scheduleRedraw();
1244 }
1245 }
1246
1247 bool NaviCubeImplementation::inDragZone(short x, short y)
1248 {
1249 qreal physicalCubeWidgetSize = getPhysicalCubeWidgetSize();
1250 int limit = physicalCubeWidgetSize / 4;
1251 return std::abs(x) < limit && std::abs(y) < limit;
1252 }
1253
1254 bool NaviCubeImplementation::mouseMoved(short x, short y)
1255 {
1256 qreal physicalCubeWidgetSize = getPhysicalCubeWidgetSize();
1257 bool hovering = std::abs(x) <= physicalCubeWidgetSize / 2
1258 && std::abs(y) <= physicalCubeWidgetSize / 2;
1259
1260 if (hovering != m_Hovering) {
1261 m_Hovering = hovering;
1262 m_View3DInventorViewer->getSoRenderManager()->scheduleRedraw();
1263 }
1264
1265 if (!m_Dragging) {
1266 setHilite(pickFace(x, y));
1267 }
1268
1269 if (m_MouseDown && m_Draggable) {
1270 if (m_MightDrag && !m_Dragging) {
1271 m_Dragging = true;
1272 setHilite(PickId::None);
1273 }
1274 if (m_Dragging && (std::abs(x) || std::abs(y))) {
1275 float newX = m_RelPos[0] + (float)(x) / m_PosAreaSize[0];
1276 float newY = m_RelPos[1] + (float)(y) / m_PosAreaSize[1];
1277 m_RelPos[0] = std::min(std::max(newX, 0.0f), 1.0f);
1278 m_RelPos[1] = std::min(std::max(newY, 0.0f), 1.0f);
1279
1280 m_View3DInventorViewer->getSoRenderManager()->scheduleRedraw();
1281 return true;
1282 }
1283 }
1284 return false;
1285 }
1286
1287 bool NaviCubeImplementation::processSoEvent(const SoEvent* ev)
1288 {
1289 short x, y;
1290 ev->getPosition().getValue(x, y);
1291 // translate to internal cube center based coordinates
1292 short rx = x - (short)(m_PosAreaSize[0] * m_RelPos[0]) - m_PosAreaBase[0];
1293 short ry = y - (short)(m_PosAreaSize[1] * m_RelPos[1]) - m_PosAreaBase[1];
1294 if (ev->getTypeId().isDerivedFrom(SoMouseButtonEvent::getClassTypeId())) {
1295 const auto mbev = static_cast<const SoMouseButtonEvent*>(ev);
1296 if (mbev->isButtonPressEvent(mbev, SoMouseButtonEvent::BUTTON1)) {
1297 return mousePressed(rx, ry);
1298 }
1299 if (mbev->isButtonReleaseEvent(mbev, SoMouseButtonEvent::BUTTON1)) {
1300 return mouseReleased(rx, ry);
1301 }
1302 }
1303 if (ev->getTypeId().isDerivedFrom(SoLocation2Event::getClassTypeId())) {
1304 return mouseMoved(rx, ry);
1305 }
1306 return false;
1307 }
1308
1309 QString NaviCubeImplementation::str(const char* str)
1310 {
1311 return QString::fromLatin1(str);
1312 }
1313
1314 void NaviCube::updateColors()
1315 {
1316 unsigned long colorLong;
1317
1318 colorLong = Gui::ViewParams::instance()->getAxisXColor();
1319 m_NaviCubeImplementation->m_xColor = Base::Color(static_cast<uint32_t>(colorLong));
1320 colorLong = Gui::ViewParams::instance()->getAxisYColor();
1321 m_NaviCubeImplementation->m_yColor = Base::Color(static_cast<uint32_t>(colorLong));
1322 colorLong = Gui::ViewParams::instance()->getAxisZColor();
1323 m_NaviCubeImplementation->m_zColor = Base::Color(static_cast<uint32_t>(colorLong));
1324 }
1325
1326 void NaviCube::setNaviCubeCommands(const std::vector<std::string>& cmd)
1327 {
1328 NaviCubeImplementation::m_commands = cmd;
1329 }
1330
1331 DEF_STD_CMD_AC(NaviCubeDraggableCmd)
1332
1333 NaviCubeDraggableCmd::NaviCubeDraggableCmd()
1334 : Command("NaviCubeDraggableCmd")
1335 {
1336 sGroup = "";
1337 sMenuText = QT_TR_NOOP("Movable Navigation Cube");
1338 sToolTipText = QT_TR_NOOP("Drag and place NaviCube");
1339 sWhatsThis = "";
1340 sStatusTip = sToolTipText;
1341 eType = Alter3DView;
1342 }
1343 void NaviCubeDraggableCmd::activated(int iMsg)
1344 {
1345 auto view = qobject_cast<View3DInventor*>(getMainWindow()->activeWindow());
1346 view->getViewer()->getNaviCube()->setDraggable(iMsg == 1 ? true : false);
1347 }
1348 bool NaviCubeDraggableCmd::isActive()
1349 {
1350 Gui::MDIView* view = Gui::getMainWindow()->activeWindow();
1351 if (view && view->isDerivedFrom<Gui::View3DInventor>()) {
1352 bool check = _pcAction->isChecked();
1353 auto view = qobject_cast<View3DInventor*>(getMainWindow()->activeWindow());
1354 bool mode = view->getViewer()->getNaviCube()->isDraggable();
1355 if (mode != check) {
1356 _pcAction->setChecked(mode);
1357 }
1358 return true;
1359 }
1360 return false;
1361 }
1362 Gui::Action* NaviCubeDraggableCmd::createAction()
1363 {
1364 Gui::Action* pcAction = Command::createAction();
1365 pcAction->setCheckable(true);
1366 return pcAction;
1367 }
1368
1369
1370 QMenu* NaviCubeImplementation::createNaviCubeMenu()
1371 {
1372 auto menu = new QMenu(getMainWindow());
1373 menu->setObjectName(str("NaviCube_Menu"));
1374
1375 CommandManager& rcCmdMgr = Application::Instance->commandManager();
1376 static bool init = true;
1377 if (init) {
1378 init = false;
1379 rcCmdMgr.addCommand(new NaviCubeDraggableCmd);
1380 }
1381
1382 vector<string> commands = NaviCubeImplementation::m_commands;
1383 if (commands.empty()) {
1384 commands.emplace_back("Std_OrthographicCamera");
1385 commands.emplace_back("Std_PerspectiveCamera");
1386 commands.emplace_back("Std_ViewIsometric");
1387 commands.emplace_back("Separator");
1388 commands.emplace_back("Std_ViewFitAll");
1389 commands.emplace_back("Std_ViewFitSelection");
1390 commands.emplace_back("Std_AlignToSelection");
1391 commands.emplace_back("Separator");
1392 commands.emplace_back("NaviCubeDraggableCmd");
1393 }
1394
1395 for (const auto& command : commands) {
1396 if (command == "Separator") {
1397 menu->addSeparator();
1398 }
1399 else {
1400 Command* cmd = rcCmdMgr.getCommandByName(command.c_str());
1401 if (cmd) {
1402 cmd->addTo(menu);
1403 }
1404 }
1405 }
1406 return menu;
1407 }