Debian Codegraph @

src/Gui/Inventor/So3DAnnotation.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2024 Kacper Donat <[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 #ifdef FC_OS_MACOSX
26 # include <OpenGL/gl.h>
27 #else
28 # ifdef FC_OS_WIN32
29 # include <windows.h>
30 # endif
31 # include <GL/gl.h>
32 #endif
33
34 #include <Inventor/elements/SoCacheElement.h>
35 #include <algorithm>
36
37 #include "So3DAnnotation.h"
38 #include <Gui/Selection/Selection.h>
39
40 using namespace Gui;
41
42 SO_ELEMENT_SOURCE(SoDelayedAnnotationsElement);
43
44 bool SoDelayedAnnotationsElement::isProcessingDelayedPaths = false;
45
46 void SoDelayedAnnotationsElement::init(SoState* state)
47 {
48 SoElement::init(state);
49 paths.clear();
50 }
51
52 void SoDelayedAnnotationsElement::initClass()
53 {
54 SO_ELEMENT_INIT_CLASS(SoDelayedAnnotationsElement, inherited);
55
56 SO_ENABLE(SoGLRenderAction, SoDelayedAnnotationsElement);
57 }
58
59 void SoDelayedAnnotationsElement::addDelayedPath(SoState* state, SoPath* path, int priority)
60 {
61 auto elt = static_cast<SoDelayedAnnotationsElement*>(state->getElementNoPush(classStackIndex));
62
63 // add to unified storage with specified priority (default = 0)
64 elt->paths.emplace_back(path, priority);
65 }
66
67 SoPathList SoDelayedAnnotationsElement::getDelayedPaths(SoState* state)
68 {
69 auto elt = static_cast<SoDelayedAnnotationsElement*>(state->getElementNoPush(classStackIndex));
70
71 if (elt->paths.empty()) {
72 return {};
73 }
74
75 // sort by priority (lower numbers render first)
76 std::stable_sort(
77 elt->paths.begin(),
78 elt->paths.end(),
79 [](const PriorityPath& a, const PriorityPath& b) { return a.priority < b.priority; }
80 );
81
82 SoPathList sortedPaths;
83 for (const auto& priorityPath : elt->paths) {
84 sortedPaths.append(priorityPath.path);
85 }
86
87 // Clear storage
88 elt->paths.clear();
89
90 return sortedPaths;
91 }
92
93 void SoDelayedAnnotationsElement::processDelayedPathsWithPriority(SoState* state, SoGLRenderAction* action)
94 {
95 auto elt = static_cast<SoDelayedAnnotationsElement*>(state->getElementNoPush(classStackIndex));
96
97 if (elt->paths.empty()) {
98 return;
99 }
100
101 std::stable_sort(
102 elt->paths.begin(),
103 elt->paths.end(),
104 [](const PriorityPath& a, const PriorityPath& b) { return a.priority < b.priority; }
105 );
106
107 isProcessingDelayedPaths = true;
108
109 for (const auto& priorityPath : elt->paths) {
110 SoPathList singlePath;
111 singlePath.append(priorityPath.path);
112
113 action->apply(singlePath, TRUE);
114 }
115
116 isProcessingDelayedPaths = false;
117
118 elt->paths.clear();
119 }
120
121 SO_NODE_SOURCE(So3DAnnotation);
122
123 bool So3DAnnotation::render = false;
124
125 So3DAnnotation::So3DAnnotation()
126 {
127 SO_NODE_CONSTRUCTOR(So3DAnnotation);
128 }
129
130 void So3DAnnotation::initClass()
131 {
132 SO_NODE_INIT_CLASS(So3DAnnotation, SoSeparator, "3DAnnotation");
133 }
134
135 void So3DAnnotation::GLRender(SoGLRenderAction* action)
136 {
137 switch (action->getCurPathCode()) {
138 case SoAction::NO_PATH:
139 case SoAction::BELOW_PATH:
140 this->GLRenderBelowPath(action);
141 break;
142 case SoAction::OFF_PATH:
143 // do nothing. Separator will reset state.
144 break;
145 case SoAction::IN_PATH:
146 this->GLRenderInPath(action);
147 break;
148 }
149 }
150
151 void So3DAnnotation::GLRenderBelowPath(SoGLRenderAction* action)
152 {
153 if (render) {
154 inherited::GLRenderBelowPath(action);
155 }
156 else {
157 SoCacheElement::invalidate(action->getState());
158 SoDelayedAnnotationsElement::addDelayedPath(action->getState(), action->getCurPath()->copy());
159 }
160 }
161
162 void So3DAnnotation::GLRenderInPath(SoGLRenderAction* action)
163 {
164 if (render) {
165 inherited::GLRenderInPath(action);
166 }
167 else {
168 SoCacheElement::invalidate(action->getState());
169 SoDelayedAnnotationsElement::addDelayedPath(action->getState(), action->getCurPath()->copy());
170 }
171 }
172
173 void So3DAnnotation::GLRenderOffPath(SoGLRenderAction* /* action */)
174 {
175 // should never render, this is a separator node
176 }