| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/*************************************************************************** |
| 3 |
* Copyright (c) 2005 Jürgen Riegel <[email protected]> * |
| 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 |
#ifndef GUI_SOFCUNIFIEDSELECTION_H |
| 25 |
#define GUI_SOFCUNIFIEDSELECTION_H |
| 26 |
|
| 27 |
#include <list> |
| 28 |
#include <unordered_map> |
| 29 |
#include <unordered_set> |
| 30 |
|
| 31 |
#include <Inventor/elements/SoLazyElement.h> |
| 32 |
#include <Inventor/fields/SoSFBool.h> |
| 33 |
#include <Inventor/fields/SoSFColor.h> |
| 34 |
#include <Inventor/fields/SoSFEnum.h> |
| 35 |
#include <Inventor/nodes/SoSeparator.h> |
| 36 |
|
| 37 |
#include "SoFCSelectionContext.h" |
| 38 |
#include "../View3DInventorViewer.h" |
| 39 |
|
| 40 |
|
| 41 |
class SoFullPath; |
| 42 |
class SoPickedPoint; |
| 43 |
class SoDetail; |
| 44 |
|
| 45 |
|
| 46 |
namespace Gui |
| 47 |
{ |
| 48 |
|
| 49 |
class Document; |
| 50 |
class ViewProviderDocumentObject; |
| 51 |
|
| 52 |
/** Unified Selection node |
| 53 |
* This is the new selection node for the 3D Viewer which will |
| 54 |
* gradually remove all the low level selection nodes in the view |
| 55 |
* provider. The handling of the preselection and the selection will |
| 56 |
* be unified here. |
| 57 |
* \author Jürgen Riegel |
| 58 |
*/ |
| 59 |
class GuiExport SoFCUnifiedSelection: public SoSeparator |
| 60 |
{ |
| 61 |
using inherited = SoSeparator; |
| 62 |
|
| 63 |
SO_NODE_HEADER(Gui::SoFCUnifiedSelection); |
| 64 |
|
| 65 |
public: |
| 66 |
static void initClass(); |
| 67 |
static void finish(); |
| 68 |
SoFCUnifiedSelection(); |
| 69 |
void applySettings(); |
| 70 |
|
| 71 |
enum SelectionModes |
| 72 |
{ |
| 73 |
AUTO, |
| 74 |
ON, |
| 75 |
OFF |
| 76 |
}; |
| 77 |
|
| 78 |
const char* getFileFormatName() const override; |
| 79 |
void write(SoWriteAction* action) override; |
| 80 |
|
| 81 |
SoSFColor colorHighlight; |
| 82 |
SoSFColor colorSelection; |
| 83 |
SoSFEnum preselectionMode; |
| 84 |
SoSFEnum selectionMode; |
| 85 |
SoSFBool selectionEnabled; |
| 86 |
SoSFBool useNewSelection; |
| 87 |
|
| 88 |
void doAction(SoAction* action) override; |
| 89 |
|
| 90 |
void handleEvent(SoHandleEventAction* action) override; |
| 91 |
void GLRenderBelowPath(SoGLRenderAction* action) override; |
| 92 |
|
| 93 |
static bool hasHighlight(); |
| 94 |
|
| 95 |
friend class View3DInventorViewer; |
| 96 |
|
| 97 |
protected: |
| 98 |
~SoFCUnifiedSelection() override; |
| 99 |
|
| 100 |
private: |
| 101 |
static int getPriority(const SoPickedPoint* p); |
| 102 |
|
| 103 |
struct PickedInfo |
| 104 |
{ |
| 105 |
const SoPickedPoint* pp {nullptr}; |
| 106 |
ViewProviderDocumentObject* vpd {nullptr}; |
| 107 |
std::string element; |
| 108 |
}; |
| 109 |
|
| 110 |
bool setPreselect(const PickedInfo&); |
| 111 |
bool setPreselect( |
| 112 |
SoFullPath* path, |
| 113 |
const SoDetail* det, |
| 114 |
ViewProviderDocumentObject* vpd, |
| 115 |
const char* element, |
| 116 |
float x, |
| 117 |
float y, |
| 118 |
float z |
| 119 |
); |
| 120 |
bool setSelection(const std::vector<PickedInfo>&, bool ctrlDown = false); |
| 121 |
|
| 122 |
std::vector<PickedInfo> getPickedList(SoHandleEventAction* action, bool singlePick) const; |
| 123 |
|
| 124 |
Gui::Document* pcDocument {nullptr}; |
| 125 |
|
| 126 |
static SoFullPath* currentHighlightPath; |
| 127 |
SoFullPath* detailPath; |
| 128 |
|
| 129 |
SbBool setPreSelection; |
| 130 |
|
| 131 |
// -1 = not handled, 0 = not selected, 1 = selected |
| 132 |
int32_t preSelection; |
| 133 |
SoColorPacker colorpacker; |
| 134 |
}; |
| 135 |
|
| 136 |
class GuiExport SoFCPathAnnotation: public SoSeparator |
| 137 |
{ |
| 138 |
using inherited = SoSeparator; |
| 139 |
|
| 140 |
SO_NODE_HEADER(Gui::SoFCPathAnnotation); |
| 141 |
|
| 142 |
public: |
| 143 |
static void initClass(); |
| 144 |
static void finish(); |
| 145 |
SoFCPathAnnotation(); |
| 146 |
|
| 147 |
void setPath(SoPath*); |
| 148 |
SoPath* getPath() |
| 149 |
{ |
| 150 |
return path; |
| 151 |
} |
| 152 |
void setDetail(SoDetail* d); |
| 153 |
SoDetail* getDetail() |
| 154 |
{ |
| 155 |
return det; |
| 156 |
} |
| 157 |
|
| 158 |
void GLRenderBelowPath(SoGLRenderAction* action) override; |
| 159 |
void GLRender(SoGLRenderAction* action) override; |
| 160 |
void GLRenderInPath(SoGLRenderAction* action) override; |
| 161 |
|
| 162 |
void getBoundingBox(SoGetBoundingBoxAction* action) override; |
| 163 |
|
| 164 |
protected: |
| 165 |
~SoFCPathAnnotation() override; |
| 166 |
|
| 167 |
protected: |
| 168 |
SoPath* path; |
| 169 |
SoTempPath* tmpPath; |
| 170 |
SoDetail* det; |
| 171 |
}; |
| 172 |
|
| 173 |
class GuiExport SoFCSeparator: public SoSeparator |
| 174 |
{ |
| 175 |
using inherited = SoSeparator; |
| 176 |
|
| 177 |
SO_NODE_HEADER(Gui::SoFCSeparator); |
| 178 |
|
| 179 |
public: |
| 180 |
static void initClass(); |
| 181 |
static void finish(); |
| 182 |
explicit SoFCSeparator(bool trackCacheMode = true); |
| 183 |
|
| 184 |
void GLRenderBelowPath(SoGLRenderAction* action) override; |
| 185 |
|
| 186 |
static void setCacheMode(CacheEnabled mode) |
| 187 |
{ |
| 188 |
CacheMode = mode; |
| 189 |
} |
| 190 |
static CacheEnabled getCacheMode() |
| 191 |
{ |
| 192 |
return CacheMode; |
| 193 |
} |
| 194 |
|
| 195 |
private: |
| 196 |
bool trackCacheMode; |
| 197 |
static CacheEnabled CacheMode; |
| 198 |
}; |
| 199 |
|
| 200 |
class GuiExport SoFCSelectionRoot: public SoFCSeparator |
| 201 |
{ |
| 202 |
using inherited = SoFCSeparator; |
| 203 |
|
| 204 |
SO_NODE_HEADER(Gui::SoFCSelectionRoot); |
| 205 |
|
| 206 |
public: |
| 207 |
static void initClass(); |
| 208 |
static void finish(); |
| 209 |
explicit SoFCSelectionRoot(bool trackCacheMode = false); |
| 210 |
|
| 211 |
void GLRenderBelowPath(SoGLRenderAction* action) override; |
| 212 |
void GLRenderInPath(SoGLRenderAction* action) override; |
| 213 |
|
| 214 |
void doAction(SoAction* action) override; |
| 215 |
void pick(SoPickAction* action) override; |
| 216 |
void rayPick(SoRayPickAction* action) override; |
| 217 |
void handleEvent(SoHandleEventAction* action) override; |
| 218 |
void search(SoSearchAction* action) override; |
| 219 |
void getPrimitiveCount(SoGetPrimitiveCountAction* action) override; |
| 220 |
void getBoundingBox(SoGetBoundingBoxAction* action) override; |
| 221 |
void getMatrix(SoGetMatrixAction* action) override; |
| 222 |
void callback(SoCallbackAction* action) override; |
| 223 |
|
| 224 |
template<class T> |
| 225 |
static std::shared_ptr<T> getRenderContext( |
| 226 |
SoNode* node, |
| 227 |
std::shared_ptr<T> def = std::shared_ptr<T>() |
| 228 |
) |
| 229 |
{ |
| 230 |
return std::dynamic_pointer_cast<T>(getNodeContext(SelStack, node, def)); |
| 231 |
} |
| 232 |
|
| 233 |
/** Returns selection context for rendering. |
| 234 |
* |
| 235 |
* @param node: the querying node |
| 236 |
* @param def: default context if none is found |
| 237 |
* @param ctx2: secondary context output |
| 238 |
* |
| 239 |
* @return Returned the primary context for selection, and the context is |
| 240 |
* always stored in the first encountered SoFCSelectionRoot in the path. It |
| 241 |
* is keyed using the entire sequence of SoFCSelectionRoot along the path |
| 242 |
* to \c node, replacing the first SoFCSelectionRoot with the given node. |
| 243 |
* |
| 244 |
* @return Secondary context returned in \c ctx2 is for customized |
| 245 |
* highlighting, and is not affected by mouse event. The highlight is |
| 246 |
* applied manually using SoSelectionElementAction. It is stored in the |
| 247 |
* last encountered SoFCSelectionRoot, and is keyed using the querying |
| 248 |
* \c node and (if there are more than one SoFCSelectionRoot along the |
| 249 |
* path) the first SoFCSelectionRoot. The reason is so that any link to a |
| 250 |
* node (new links means additional SoFCSelectionRoot added in front) with |
| 251 |
* customized subelement highlight will also show the highlight. Secondary |
| 252 |
* context can be chained, which why the secondary context type must provide |
| 253 |
* an function called merge() for getRenderContext() to merge the context. |
| 254 |
* See SoFCSelectionContext::merge() for an implementation of merging multiple |
| 255 |
* context. |
| 256 |
* |
| 257 |
* @note For simplicity reason, currently secondary context is only freed |
| 258 |
* when the storage SoFCSSelectionRoot node is freed. |
| 259 |
*/ |
| 260 |
template<class T> |
| 261 |
static std::shared_ptr<T> getRenderContext( |
| 262 |
SoNode* node, |
| 263 |
std::shared_ptr<T> def, |
| 264 |
std::shared_ptr<T>& ctx2 |
| 265 |
) |
| 266 |
{ |
| 267 |
ctx2 = std::dynamic_pointer_cast<T>(getNodeContext2(SelStack, node, T::merge)); |
| 268 |
return std::dynamic_pointer_cast<T>(getNodeContext(SelStack, node, def)); |
| 269 |
} |
| 270 |
|
| 271 |
/** Get the selection context for an action. |
| 272 |
* |
| 273 |
* @param action: the action. SoSelectionElementAction has any option to |
| 274 |
* query for secondary context. \sa getRenderContext for detail about |
| 275 |
* secondary context |
| 276 |
* @param node: the querying node |
| 277 |
* @param def: default context if none is found, only used if querying |
| 278 |
* non-secondary context |
| 279 |
* @param create: create a new context if none is found |
| 280 |
* |
| 281 |
* @return If no SoFCSelectionRoot is found in the current path of action, |
| 282 |
* \c def is returned. Otherwise a selection context returned. A new one |
| 283 |
* will be created if none is found. |
| 284 |
*/ |
| 285 |
template<class T> |
| 286 |
static std::shared_ptr<T> getActionContext( |
| 287 |
SoAction* action, |
| 288 |
SoNode* node, |
| 289 |
std::shared_ptr<T> def = std::shared_ptr<T>(), |
| 290 |
bool create = true |
| 291 |
) |
| 292 |
{ |
| 293 |
auto res = findActionContext(action, node, create, false); |
| 294 |
if (!res.second) { |
| 295 |
if (res.first) { |
| 296 |
return std::shared_ptr<T>(); |
| 297 |
} |
| 298 |
// default context is only applicable for non-secondary context query |
| 299 |
return def; |
| 300 |
} |
| 301 |
// make a new context if there is none |
| 302 |
auto& ctx = *res.second; |
| 303 |
if (ctx) { |
| 304 |
auto ret = std::dynamic_pointer_cast<T>(ctx); |
| 305 |
if (!ret) { |
| 306 |
ctx.reset(); |
| 307 |
} |
| 308 |
} |
| 309 |
if (!ctx && create) { |
| 310 |
ctx = std::make_shared<T>(); |
| 311 |
} |
| 312 |
return std::static_pointer_cast<T>(ctx); |
| 313 |
} |
| 314 |
|
| 315 |
static bool removeActionContext(SoAction* action, SoNode* node) |
| 316 |
{ |
| 317 |
return findActionContext(action, node, false, true).second != 0; |
| 318 |
} |
| 319 |
|
| 320 |
template<class T> |
| 321 |
static std::shared_ptr<T> getSecondaryActionContext(SoAction* action, SoNode* node) |
| 322 |
{ |
| 323 |
auto it = ActionStacks.find(action); |
| 324 |
if (it == ActionStacks.end()) { |
| 325 |
return std::shared_ptr<T>(); |
| 326 |
} |
| 327 |
return std::dynamic_pointer_cast<T>(getNodeContext2(it->second, node, T::merge)); |
| 328 |
} |
| 329 |
|
| 330 |
static void checkSelection(bool& sel, SbColor& selColor, bool& hl, SbColor& hlColor); |
| 331 |
|
| 332 |
static void moveActionStack(SoAction* from, SoAction* to, bool erase); |
| 333 |
|
| 334 |
static SoNode* getCurrentRoot(bool front, SoNode* def); |
| 335 |
|
| 336 |
void resetContext(); |
| 337 |
|
| 338 |
static bool checkColorOverride(SoState* state); |
| 339 |
|
| 340 |
bool hasColorOverride() const |
| 341 |
{ |
| 342 |
return overrideColor; |
| 343 |
} |
| 344 |
|
| 345 |
void setColorOverride(Base::Color c) |
| 346 |
{ |
| 347 |
overrideColor = true; |
| 348 |
colorOverride = SbColor(c.r, c.g, c.b); |
| 349 |
transOverride = 1.0 - c.a; |
| 350 |
} |
| 351 |
|
| 352 |
void removeColorOverride() |
| 353 |
{ |
| 354 |
overrideColor = false; |
| 355 |
} |
| 356 |
|
| 357 |
enum SelectStyles |
| 358 |
{ |
| 359 |
Full, |
| 360 |
Box, |
| 361 |
PassThrough |
| 362 |
}; |
| 363 |
SoSFEnum selectionStyle; |
| 364 |
|
| 365 |
static bool renderBBox(SoGLRenderAction* action, SoNode* node, SbColor color); |
| 366 |
|
| 367 |
protected: |
| 368 |
~SoFCSelectionRoot() override; |
| 369 |
|
| 370 |
void renderPrivate(SoGLRenderAction*, bool inPath); |
| 371 |
bool _renderPrivate(SoGLRenderAction*, bool inPath); |
| 372 |
|
| 373 |
class Stack: public std::vector<SoNode*> |
| 374 |
{ |
| 375 |
public: |
| 376 |
std::unordered_set<SoNode*> nodeSet; |
| 377 |
size_t offset = 0; |
| 378 |
}; |
| 379 |
|
| 380 |
static SoFCSelectionContextBasePtr getNodeContext( |
| 381 |
Stack& stack, |
| 382 |
SoNode* node, |
| 383 |
SoFCSelectionContextBasePtr def |
| 384 |
); |
| 385 |
static SoFCSelectionContextBasePtr getNodeContext2( |
| 386 |
Stack& stack, |
| 387 |
SoNode* node, |
| 388 |
SoFCSelectionContextBase::MergeFunc* merge |
| 389 |
); |
| 390 |
static std::pair<bool, SoFCSelectionContextBasePtr*> findActionContext( |
| 391 |
SoAction* action, |
| 392 |
SoNode* node, |
| 393 |
bool create, |
| 394 |
bool erase |
| 395 |
); |
| 396 |
|
| 397 |
static Stack SelStack; |
| 398 |
static std::unordered_map<SoAction*, Stack> ActionStacks; |
| 399 |
struct StackComp |
| 400 |
{ |
| 401 |
bool operator()(const Stack& a, const Stack& b) const; |
| 402 |
}; |
| 403 |
|
| 404 |
using ContextMap = std::map<Stack, SoFCSelectionContextBasePtr, StackComp>; |
| 405 |
ContextMap contextMap; |
| 406 |
ContextMap contextMap2; // holding secondary context |
| 407 |
|
| 408 |
struct SelContext: SoFCSelectionContextBase |
| 409 |
{ |
| 410 |
public: |
| 411 |
SbColor selColor; |
| 412 |
SbColor hlColor; |
| 413 |
bool selAll = false; |
| 414 |
bool hlAll = false; |
| 415 |
bool hideAll = false; |
| 416 |
static MergeFunc merge; |
| 417 |
}; |
| 418 |
using SelContextPtr = std::shared_ptr<SelContext>; |
| 419 |
using ColorStack = std::vector<SbColor>; |
| 420 |
static ColorStack SelColorStack; |
| 421 |
static ColorStack HlColorStack; |
| 422 |
static SoFCSelectionRoot* ShapeColorNode; |
| 423 |
bool overrideColor = false; |
| 424 |
SbColor colorOverride; |
| 425 |
float transOverride = 0.0f; |
| 426 |
SoColorPacker shapeColorPacker; |
| 427 |
|
| 428 |
bool doActionPrivate(Stack& stack, SoAction*); |
| 429 |
}; |
| 430 |
|
| 431 |
/** |
| 432 |
* @author Werner Mayer |
| 433 |
*/ |
| 434 |
class GuiExport SoHighlightElementAction: public SoAction |
| 435 |
{ |
| 436 |
SO_ACTION_HEADER(SoHighlightElementAction); |
| 437 |
|
| 438 |
public: |
| 439 |
SoHighlightElementAction(); |
| 440 |
~SoHighlightElementAction() override; |
| 441 |
|
| 442 |
void setHighlighted(SbBool); |
| 443 |
SbBool isHighlighted() const; |
| 444 |
void setColor(const SbColor&); |
| 445 |
const SbColor& getColor() const; |
| 446 |
void setElement(const SoDetail*); |
| 447 |
const SoDetail* getElement() const; |
| 448 |
|
| 449 |
static void initClass(); |
| 450 |
|
| 451 |
protected: |
| 452 |
void beginTraversal(SoNode* node) override; |
| 453 |
|
| 454 |
private: |
| 455 |
static void callDoAction(SoAction* action, SoNode* node); |
| 456 |
|
| 457 |
private: |
| 458 |
SbBool _highlight {false}; |
| 459 |
SbColor _color; |
| 460 |
const SoDetail* _det {nullptr}; |
| 461 |
}; |
| 462 |
|
| 463 |
/** |
| 464 |
* @author Werner Mayer |
| 465 |
*/ |
| 466 |
class GuiExport SoSelectionElementAction: public SoAction |
| 467 |
{ |
| 468 |
SO_ACTION_HEADER(SoSelectionElementAction); |
| 469 |
|
| 470 |
public: |
| 471 |
enum Type |
| 472 |
{ |
| 473 |
None, |
| 474 |
Append, |
| 475 |
Remove, |
| 476 |
All, |
| 477 |
Color, |
| 478 |
Hide, |
| 479 |
Show |
| 480 |
}; |
| 481 |
|
| 482 |
explicit SoSelectionElementAction(Type = None, bool secondary = false); |
| 483 |
~SoSelectionElementAction() override; |
| 484 |
|
| 485 |
Type getType() const; |
| 486 |
void setType(Type type) |
| 487 |
{ |
| 488 |
_type = type; |
| 489 |
} |
| 490 |
|
| 491 |
void setColor(const SbColor&); |
| 492 |
const SbColor& getColor() const; |
| 493 |
void setElement(const SoDetail*); |
| 494 |
const SoDetail* getElement() const; |
| 495 |
|
| 496 |
bool isSecondary() const |
| 497 |
{ |
| 498 |
return _secondary; |
| 499 |
} |
| 500 |
void setSecondary(bool enable) |
| 501 |
{ |
| 502 |
_secondary = enable; |
| 503 |
} |
| 504 |
|
| 505 |
const std::map<std::string, Base::Color>& getColors() const |
| 506 |
{ |
| 507 |
return _colors; |
| 508 |
} |
| 509 |
void setColors(const std::map<std::string, Base::Color>& colors) |
| 510 |
{ |
| 511 |
_colors = colors; |
| 512 |
} |
| 513 |
void swapColors(std::map<std::string, Base::Color>& colors) |
| 514 |
{ |
| 515 |
_colors.swap(colors); |
| 516 |
} |
| 517 |
|
| 518 |
static void initClass(); |
| 519 |
|
| 520 |
protected: |
| 521 |
void beginTraversal(SoNode* node) override; |
| 522 |
|
| 523 |
private: |
| 524 |
static void callDoAction(SoAction* action, SoNode* node); |
| 525 |
|
| 526 |
private: |
| 527 |
Type _type; |
| 528 |
SbColor _color; |
| 529 |
const SoDetail* _det {nullptr}; |
| 530 |
std::map<std::string, Base::Color> _colors; |
| 531 |
bool _secondary; |
| 532 |
}; |
| 533 |
|
| 534 |
/** |
| 535 |
* @author Werner Mayer |
| 536 |
*/ |
| 537 |
class GuiExport SoVRMLAction: public SoAction |
| 538 |
{ |
| 539 |
SO_ACTION_HEADER(SoVRMLAction); |
| 540 |
|
| 541 |
public: |
| 542 |
SoVRMLAction(); |
| 543 |
~SoVRMLAction() override; |
| 544 |
void setOverrideMode(SbBool); |
| 545 |
SbBool isOverrideMode() const; |
| 546 |
|
| 547 |
static void initClass(); |
| 548 |
|
| 549 |
private: |
| 550 |
SbBool overrideMode {true}; |
| 551 |
std::list<int> bindList; |
| 552 |
static void callDoAction(SoAction* action, SoNode* node); |
| 553 |
}; |
| 554 |
|
| 555 |
|
| 556 |
} // namespace Gui |
| 557 |
|
| 558 |
#endif // !GUI_SOFCUNIFIEDSELECTION_H |