File size: 32,347 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2005 Jürgen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include <QString>
#include <Inventor/SoFullPath.h>
#include <Inventor/SoPickedPoint.h>
#include <Inventor/actions/SoGLRenderAction.h>
#include <Inventor/actions/SoHandleEventAction.h>
#include <Inventor/details/SoFaceDetail.h>
#include <Inventor/details/SoLineDetail.h>
#include <Inventor/elements/SoLazyElement.h>
#include <Inventor/elements/SoMaterialBindingElement.h>
#include <Inventor/elements/SoOverrideElement.h>
#include <Inventor/elements/SoWindowElement.h>
#include <Inventor/events/SoKeyboardEvent.h>
#include <Inventor/events/SoLocation2Event.h>
#include <Inventor/events/SoMouseButtonEvent.h>
#include <Inventor/misc/SoState.h>
#include <Base/UnitsApi.h>
#include "SoFCSelection.h"
#include "MainWindow.h"
#include "SoFCSelectionAction.h"
#include "SoFCUnifiedSelection.h"
#include "ViewParams.h"
using namespace Gui;
namespace Gui
{
void printPreselectionInfo(
const char* documentName,
const char* objectName,
const char* subElementName,
float x,
float y,
float z,
double precision
);
}
SoFullPath* Gui::SoFCSelection::currenthighlight = nullptr;
// *************************************************************************
SO_NODE_SOURCE(SoFCSelection)
/*!
Constructor.
*/
SoFCSelection::SoFCSelection()
{
SO_NODE_CONSTRUCTOR(SoFCSelection);
SO_NODE_ADD_FIELD(colorHighlight, (SbColor(0.8f, 0.1f, 0.1f)));
SO_NODE_ADD_FIELD(colorSelection, (SbColor(0.1f, 0.8f, 0.1f)));
SO_NODE_ADD_FIELD(style, (EMISSIVE));
SO_NODE_ADD_FIELD(preselectionMode, (AUTO));
SO_NODE_ADD_FIELD(selectionMode, (SEL_ON));
SO_NODE_ADD_FIELD(selected, (NOTSELECTED));
SO_NODE_ADD_FIELD(documentName, (""));
SO_NODE_ADD_FIELD(objectName, (""));
SO_NODE_ADD_FIELD(subElementName, (""));
SO_NODE_ADD_FIELD(useNewSelection, (true));
SO_NODE_DEFINE_ENUM_VALUE(Styles, EMISSIVE);
SO_NODE_DEFINE_ENUM_VALUE(Styles, EMISSIVE_DIFFUSE);
SO_NODE_DEFINE_ENUM_VALUE(Styles, BOX);
SO_NODE_SET_SF_ENUM_TYPE(style, Styles);
SO_NODE_DEFINE_ENUM_VALUE(PreselectionModes, AUTO);
SO_NODE_DEFINE_ENUM_VALUE(PreselectionModes, ON);
SO_NODE_DEFINE_ENUM_VALUE(PreselectionModes, OFF);
SO_NODE_SET_SF_ENUM_TYPE(preselectionMode, PreselectionModes);
SO_NODE_DEFINE_ENUM_VALUE(SelectionModes, SEL_ON);
SO_NODE_DEFINE_ENUM_VALUE(SelectionModes, SEL_OFF);
SO_NODE_SET_SF_ENUM_TYPE(selectionMode, SelectionModes);
SO_NODE_DEFINE_ENUM_VALUE(Selected, NOTSELECTED);
SO_NODE_DEFINE_ENUM_VALUE(Selected, SELECTED);
SO_NODE_SET_SF_ENUM_TYPE(selected, Selected);
highlighted = false;
bShift = false;
bCtrl = false;
selected = NOTSELECTED;
useNewSelection = ViewParams::instance()->getUseNewSelection();
selContext = std::make_shared<SelContext>();
selContext2 = std::make_shared<SelContext>();
}
/*!
Destructor.
*/
SoFCSelection::~SoFCSelection()
{
// If we're being deleted and we're the current highlight,
// NULL out that variable
if (currenthighlight
&& (!currenthighlight->getTail()->isOfType(SoFCSelection::getClassTypeId()))) {
currenthighlight->unref();
currenthighlight = nullptr;
}
// delete THIS;
}
// doc from parent
void SoFCSelection::initClass()
{
SO_NODE_INIT_CLASS(SoFCSelection, SoGroup, "Group");
}
void SoFCSelection::finish()
{
atexit_cleanup();
}
/*!
Static method that can be used to turn off the current highlight.
*/
void SoFCSelection::turnOffCurrentHighlight(SoGLRenderAction* action)
{
SoFCSelection::turnoffcurrent(action);
}
void SoFCSelection::doAction(SoAction* action)
{
if (useNewSelection.getValue() && action->getCurPathCode() != SoAction::OFF_PATH) {
if (action->getTypeId() == Gui::SoHighlightElementAction::getClassTypeId()) {
auto hlaction = static_cast<Gui::SoHighlightElementAction*>(action);
if (!hlaction->isHighlighted()) {
auto ctx = Gui::SoFCSelectionRoot::getActionContext(action, this, selContext, false);
if (ctx && ctx->isHighlighted()) {
ctx->highlightIndex = -1;
touch();
}
}
else {
auto ctx = Gui::SoFCSelectionRoot::getActionContext(action, this, selContext);
if (ctx) {
ctx->highlightColor = hlaction->getColor();
if (!ctx->isHighlighted()) {
ctx->highlightIndex = 0;
touch();
}
}
}
return;
}
else if (action->getTypeId() == Gui::SoSelectionElementAction::getClassTypeId()) {
auto selaction = static_cast<Gui::SoSelectionElementAction*>(action);
if (selaction->getType() == Gui::SoSelectionElementAction::All
|| selaction->getType() == Gui::SoSelectionElementAction::Append) {
SelContextPtr ctx = Gui::SoFCSelectionRoot::getActionContext(action, this, selContext);
if (ctx) {
ctx->selectionColor = selaction->getColor();
if (!ctx->isSelectAll()) {
ctx->selectAll();
this->touch();
}
}
}
else if (selaction->getType() == Gui::SoSelectionElementAction::None
|| selaction->getType() == Gui::SoSelectionElementAction::Remove) {
SelContextPtr ctx
= Gui::SoFCSelectionRoot::getActionContext(action, this, selContext, false);
if (ctx && ctx->isSelected()) {
ctx->selectionIndex.clear();
this->touch();
}
}
return;
}
}
if (action->getTypeId() == SoFCDocumentAction::getClassTypeId()) {
auto docaction = static_cast<SoFCDocumentAction*>(action);
this->documentName = docaction->documentName;
}
if (action->getTypeId() == SoFCDocumentObjectAction::getClassTypeId()) {
auto objaction = static_cast<SoFCDocumentObjectAction*>(action);
objaction->documentName = this->documentName.getValue();
objaction->objectName = this->objectName.getValue();
objaction->componentName = this->subElementName.getValue();
objaction->setHandled();
}
if (!useNewSelection.getValue()) {
if (action->getTypeId() == SoFCEnablePreselectionAction::getClassTypeId()) {
auto preaction = static_cast<SoFCEnablePreselectionAction*>(action);
if (preaction->enabled) {
this->preselectionMode = SoFCSelection::AUTO;
}
else {
this->preselectionMode = SoFCSelection::OFF;
}
}
if (action->getTypeId() == SoFCEnableSelectionAction::getClassTypeId()) {
auto selaction = static_cast<SoFCEnableSelectionAction*>(action);
if (selaction->enabled) {
this->selectionMode = SoFCSelection::SEL_ON;
}
else {
this->selectionMode = SoFCSelection::SEL_OFF;
if (selected.getValue() == SELECTED) {
this->selected = NOTSELECTED;
}
}
}
if (action->getTypeId() == SoFCSelectionColorAction::getClassTypeId()) {
auto colaction = static_cast<SoFCSelectionColorAction*>(action);
this->colorSelection = colaction->selectionColor;
}
if (action->getTypeId() == SoFCHighlightColorAction::getClassTypeId()) {
auto colaction = static_cast<SoFCHighlightColorAction*>(action);
this->colorHighlight = colaction->highlightColor;
}
if (selectionMode.getValue() == SEL_ON
&& action->getTypeId() == SoFCSelectionAction::getClassTypeId()) {
auto selaction = static_cast<SoFCSelectionAction*>(action);
if (selaction->SelChange.Type == SelectionChanges::AddSelection
|| selaction->SelChange.Type == SelectionChanges::RmvSelection) {
if (documentName.getValue() == selaction->SelChange.pDocName
&& objectName.getValue() == selaction->SelChange.pObjectName
&& (subElementName.getValue() == selaction->SelChange.pSubName
|| *(selaction->SelChange.pSubName) == '\0')) {
if (selaction->SelChange.Type == SelectionChanges::AddSelection) {
if (selected.getValue() == NOTSELECTED) {
selected = SELECTED;
}
}
else {
if (selected.getValue() == SELECTED) {
selected = NOTSELECTED;
}
}
return;
}
}
else if (selaction->SelChange.Type == SelectionChanges::ClrSelection) {
if (documentName.getValue() == selaction->SelChange.pDocName
|| strcmp(selaction->SelChange.pDocName, "") == 0) {
if (selected.getValue() == SELECTED) {
selected = NOTSELECTED;
}
}
}
else if (selaction->SelChange.Type == SelectionChanges::SetSelection) {
bool sel = Selection().isSelected(
documentName.getValue().getString(),
objectName.getValue().getString() /*,
subElementName.getValue().getString()*/
);
if (sel) {
if (selected.getValue() == NOTSELECTED) {
selected = SELECTED;
}
}
else {
if (selected.getValue() == SELECTED) {
selected = NOTSELECTED;
}
}
}
}
}
inherited::doAction(action);
}
int SoFCSelection::getPriority(const SoPickedPoint* p)
{
const SoDetail* detail = p->getDetail();
if (!detail) {
return 0;
}
if (detail->isOfType(SoFaceDetail::getClassTypeId())) {
return 1;
}
if (detail->isOfType(SoLineDetail::getClassTypeId())) {
return 2;
}
if (detail->isOfType(SoPointDetail::getClassTypeId())) {
return 3;
}
return 0;
}
const SoPickedPoint* SoFCSelection::getPickedPoint(SoHandleEventAction* action) const
{
// To identify the picking of lines in a concave area we have to
// get all intersection points. If we have two or more intersection
// points where the first is of a face and the second of a line with
// almost similar coordinates we use the second point, instead.
const SoPickedPointList& points = action->getPickedPointList();
if (points.getLength() == 0) {
return nullptr;
}
else if (points.getLength() == 1) {
return points[0];
}
const SoPickedPoint* picked = points[0];
int picked_prio = getPriority(picked);
const SbVec3f& picked_pt = picked->getPoint();
for (int i = 1; i < points.getLength(); i++) {
const SoPickedPoint* cur = points[i];
int cur_prio = getPriority(cur);
const SbVec3f& cur_pt = cur->getPoint();
if ((cur_prio > picked_prio) && picked_pt.equals(cur_pt, 0.01f)) {
picked = cur;
picked_prio = cur_prio;
}
}
return picked;
}
// doc from parent
void SoFCSelection::handleEvent(SoHandleEventAction* action)
{
if (useNewSelection.getValue()) {
inherited::handleEvent(action);
return;
}
static char buf[513];
auto mymode = static_cast<PreselectionModes>(this->preselectionMode.getValue());
const SoEvent* event = action->getEvent();
// mouse move events for preselection
if (event->isOfType(SoLocation2Event::getClassTypeId())) {
// NOTE: If preselection is off then we do not check for a picked point because otherwise
// this search may slow down extremely the system on really big data sets. In this case we
// just check for a picked point if the data set has been selected.
if (mymode == AUTO || mymode == ON) {
const SoPickedPoint* pp = this->getPickedPoint(action);
if (pp && pp->getPath()->containsPath(action->getCurPath())) {
if (!highlighted) {
if (Gui::Selection().setPreselect(
documentName.getValue().getString(),
objectName.getValue().getString(),
subElementName.getValue().getString(),
pp->getPoint()[0],
pp->getPoint()[1],
pp->getPoint()[2]
)) {
SoFCSelection::turnoffcurrent(action);
SoFCSelection::currenthighlight = static_cast<SoFullPath*>(
action->getCurPath()->copy()
);
SoFCSelection::currenthighlight->ref();
highlighted = true;
this->touch(); // force scene redraw
this->redrawHighlighted(action, true);
}
}
const auto& pt = pp->getPoint();
printPreselectionInfo(
documentName.getValue().getString(),
objectName.getValue().getString(),
subElementName.getValue().getString(),
pt[0],
pt[1],
pt[2],
1e-7
);
}
else { // picked point
if (highlighted) {
if (mymode == AUTO) {
SoFCSelection::turnoffcurrent(action);
}
// FIXME: I think we should set 'highlighted' to false whenever no point is
// picked else
highlighted = false;
Gui::Selection().rmvPreselect();
}
}
}
} // key press events
else if (event->isOfType(SoKeyboardEvent ::getClassTypeId())) {
auto const e = static_cast<const SoKeyboardEvent*>(event);
if (SoKeyboardEvent::isKeyPressEvent(e, SoKeyboardEvent::LEFT_SHIFT)
|| SoKeyboardEvent::isKeyPressEvent(e, SoKeyboardEvent::RIGHT_SHIFT)) {
bShift = true;
}
if (SoKeyboardEvent::isKeyReleaseEvent(e, SoKeyboardEvent::LEFT_SHIFT)
|| SoKeyboardEvent::isKeyReleaseEvent(e, SoKeyboardEvent::RIGHT_SHIFT)) {
bShift = false;
}
if (SoKeyboardEvent::isKeyPressEvent(e, SoKeyboardEvent::LEFT_CONTROL)
|| SoKeyboardEvent::isKeyPressEvent(e, SoKeyboardEvent::RIGHT_CONTROL)) {
bCtrl = true;
}
if (SoKeyboardEvent::isKeyReleaseEvent(e, SoKeyboardEvent::LEFT_CONTROL)
|| SoKeyboardEvent::isKeyReleaseEvent(e, SoKeyboardEvent::RIGHT_CONTROL)) {
bCtrl = false;
}
} // mouse press events for (de)selection
else if (event->isOfType(SoMouseButtonEvent::getClassTypeId())) {
auto const e = static_cast<const SoMouseButtonEvent*>(event);
if (SoMouseButtonEvent::isButtonReleaseEvent(e, SoMouseButtonEvent::BUTTON1)) {
// FIXME: Shouldn't we remove the preselection for newly selected objects?
// Otherwise the tree signals that an object is preselected even though it is
// hidden. (Werner)
const SoPickedPoint* pp = this->getPickedPoint(action);
if (pp && pp->getPath()->containsPath(action->getCurPath())) {
const auto& pt = pp->getPoint();
if (bCtrl) {
if (Gui::Selection().isSelected(
documentName.getValue().getString(),
objectName.getValue().getString(),
subElementName.getValue().getString()
)) {
Gui::Selection().rmvSelection(
documentName.getValue().getString(),
objectName.getValue().getString(),
subElementName.getValue().getString()
);
}
else {
Gui::Selection().addSelection(
documentName.getValue().getString(),
objectName.getValue().getString(),
subElementName.getValue().getString(),
pt[0],
pt[1],
pt[2]
);
if (mymode == OFF) {
snprintf(
buf,
512,
"Selected: %s.%s.%s (%g, %g, %g)",
documentName.getValue().getString(),
objectName.getValue().getString(),
subElementName.getValue().getString(),
fabs(pt[0]) > 1e-7 ? pt[0] : 0.0,
fabs(pt[1]) > 1e-7 ? pt[1] : 0.0,
fabs(pt[2]) > 1e-7 ? pt[2] : 0.0
);
getMainWindow()->showMessage(QString::fromLatin1(buf));
}
}
}
else { // Ctrl
if (!Gui::Selection().isSelected(
documentName.getValue().getString(),
objectName.getValue().getString(),
subElementName.getValue().getString()
)) {
Gui::Selection().clearSelection(documentName.getValue().getString());
Gui::Selection().addSelection(
documentName.getValue().getString(),
objectName.getValue().getString(),
subElementName.getValue().getString(),
pt[0],
pt[1],
pt[2]
);
}
else {
Gui::Selection().clearSelection(documentName.getValue().getString());
Gui::Selection().addSelection(
documentName.getValue().getString(),
objectName.getValue().getString(),
nullptr,
pt[0],
pt[1],
pt[2]
);
}
if (mymode == OFF) {
snprintf(
buf,
512,
"Selected: %s.%s.%s (%g, %g, %g)",
documentName.getValue().getString(),
objectName.getValue().getString(),
subElementName.getValue().getString(),
fabs(pt[0]) > 1e-7 ? pt[0] : 0.0,
fabs(pt[1]) > 1e-7 ? pt[1] : 0.0,
fabs(pt[2]) > 1e-7 ? pt[2] : 0.0
);
getMainWindow()->showMessage(QString::fromLatin1(buf));
}
}
action->setHandled();
} // picked point
} // mouse release
}
inherited::handleEvent(action);
}
// doc from parent
void SoFCSelection::GLRenderBelowPath(SoGLRenderAction* action)
{
SoState* state = action->getState();
SelContextPtr ctx = Gui::SoFCSelectionRoot::getRenderContext<SelContext>(this, selContext);
if (selContext2->checkGlobal(ctx)) {
ctx = selContext2;
}
if (!useNewSelection.getValue() && selContext == ctx) {
ctx->selectionColor = this->colorSelection.getValue();
ctx->highlightColor = this->colorHighlight.getValue();
if (this->selected.getValue() == SELECTED) {
ctx->selectAll();
}
else {
ctx->selectionIndex.clear();
}
ctx->highlightIndex = this->highlighted ? 0 : -1;
}
// check if preselection is active
if (this->setOverride(action, ctx)) {
inherited::GLRenderBelowPath(action);
state->pop();
}
else {
inherited::GLRenderBelowPath(action);
}
}
void SoFCSelection::GLRender(SoGLRenderAction* action)
{
SoState* state = action->getState();
SelContextPtr ctx = Gui::SoFCSelectionRoot::getRenderContext<SelContext>(this, selContext);
if (selContext2->checkGlobal(ctx)) {
ctx = selContext2;
}
if (!useNewSelection.getValue() && selContext == ctx) {
ctx->selectionColor = this->colorSelection.getValue();
ctx->highlightColor = this->colorHighlight.getValue();
if (this->selected.getValue() == SELECTED) {
ctx->selectAll();
}
else {
ctx->selectionIndex.clear();
}
ctx->highlightIndex = this->highlighted ? 0 : -1;
}
// check if preselection is active
if (this->setOverride(action, ctx)) {
inherited::GLRender(action);
state->pop();
}
else {
inherited::GLRender(action);
}
}
// doc from parent
void SoFCSelection::GLRenderInPath(SoGLRenderAction* action)
{
SelContextPtr ctx = Gui::SoFCSelectionRoot::getRenderContext<SelContext>(this, selContext);
if (selContext2->checkGlobal(ctx)) {
ctx = selContext2;
}
if (!useNewSelection.getValue() && selContext == ctx) {
ctx->selectionColor = this->colorSelection.getValue();
ctx->highlightColor = this->colorHighlight.getValue();
if (this->selected.getValue() == SELECTED) {
ctx->selectAll();
}
else {
ctx->selectionIndex.clear();
}
ctx->highlightIndex = this->highlighted ? 0 : -1;
}
// check if preselection is active
SoState* state = action->getState();
if (this->setOverride(action, ctx)) {
inherited::GLRenderInPath(action);
state->pop();
}
else {
inherited::GLRenderInPath(action);
}
}
SbBool SoFCSelection::preRender(SoGLRenderAction* action, GLint& oldDepthFunc)
//
////////////////////////////////////////////////////////////////////////
{
// If not performing locate highlighting, just return.
if (preselectionMode.getValue() == OFF) {
return false;
}
SoState* state = action->getState();
// ??? prevent caching at this level - for some reason the
// ??? SoWindowElement::copyMatchInfo() method get called, which should
// ??? never be called. We are not caching this node correctly yet....
// SoCacheElement::invalidate(state);
SbBool drawHighlighted
= (preselectionMode.getValue() == ON || isHighlighted(action)
|| selected.getValue() == SELECTED);
if (drawHighlighted) {
// prevent diffuse & emissive color from leaking out...
state->push();
SbColor col;
if (selected.getValue() == SELECTED) {
col = colorSelection.getValue();
}
else {
col = colorHighlight.getValue();
}
// Emissive Color
SoLazyElement::setEmissive(state, &col);
SoOverrideElement::setEmissiveColorOverride(state, this, true);
// Diffuse Color
if (style.getValue() == EMISSIVE_DIFFUSE) {
SoLazyElement::setDiffuse(state, this, 1, &col, &colorpacker);
SoOverrideElement::setDiffuseColorOverride(state, this, true);
}
}
// Draw on top of other things at same z-buffer depth if:
// [a] we're highlighted
// [b] this is the highlighting pass. This occurs when changing from
// non-hilit to lit OR VICE VERSA.
// Otherwise, leave it alone...
if (drawHighlighted || highlighted) {
glGetIntegerv(GL_DEPTH_FUNC, &oldDepthFunc);
if (oldDepthFunc != GL_LEQUAL) {
glDepthFunc(GL_LEQUAL);
}
}
return drawHighlighted;
}
/*!
Empty method in Coin. Can be used by subclasses to be told
when status change.
*/
void SoFCSelection::redrawHighlighted(SoAction* action, SbBool doHighlight)
{
Q_UNUSED(action);
Q_UNUSED(doHighlight);
}
SbBool SoFCSelection::readInstance(SoInput* in, unsigned short flags)
{
// Note: The read in document name can be false, so the caller must ensure pointing to the
// correct document
SbBool ret = inherited::readInstance(in, flags);
return ret;
}
//
// update override state before rendering
//
bool SoFCSelection::setOverride(SoGLRenderAction* action, SelContextPtr ctx)
{
auto mymode = static_cast<PreselectionModes>(this->preselectionMode.getValue());
bool preselected = ctx && ctx->isHighlighted() && (useNewSelection.getValue() || mymode == AUTO);
if (!preselected && mymode != ON && (!ctx || !ctx->isSelected())) {
return false;
}
// uniqueId is returned by SoNode::getNodeId(). It is used to notify change
// and for render cache update. In order to update cache on selection state
// change, We manually change the id here by using a combined hash of the
// original id and context pointer.
auto oldId = this->uniqueId;
this->uniqueId ^= std::hash<void*>()(ctx.get()) + 0x9e3779b9 + (oldId << 6) + (oldId >> 2);
auto mystyle = static_cast<Styles>(this->style.getValue());
if (mystyle == SoFCSelection::BOX) {
if (ctx) {
SoFCSelectionRoot::renderBBox(
action,
this,
preselected ? ctx->highlightColor : ctx->selectionColor
);
}
this->uniqueId = oldId;
return false;
}
SoState* state = action->getState();
state->push();
SoMaterialBindingElement::set(state, SoMaterialBindingElement::OVERALL);
SoOverrideElement::setMaterialBindingOverride(state, this, true);
if (!preselected && ctx) {
SoLazyElement::setEmissive(state, &ctx->selectionColor);
}
else if (ctx) {
SoLazyElement::setEmissive(state, &ctx->highlightColor);
}
SoOverrideElement::setEmissiveColorOverride(state, this, true);
if (SoLazyElement::getLightModel(state) == SoLazyElement::BASE_COLOR
|| mystyle == SoFCSelection::EMISSIVE_DIFFUSE) {
if (!preselected && ctx) {
SoLazyElement::setDiffuse(state, this, 1, &ctx->selectionColor, &colorpacker);
}
else if (ctx) {
SoLazyElement::setDiffuse(state, this, 1, &ctx->highlightColor, &colorpacker);
}
SoOverrideElement::setDiffuseColorOverride(state, this, true);
}
this->uniqueId = oldId;
return true;
}
// private convenience method
void SoFCSelection::turnoffcurrent(SoAction* action)
{
if (SoFCSelection::currenthighlight && SoFCSelection::currenthighlight->getLength()) {
SoNode* tail = SoFCSelection::currenthighlight->getTail();
if (tail->isOfType(SoFCSelection::getClassTypeId())) {
static_cast<SoFCSelection*>(tail)->highlighted = false;
static_cast<SoFCSelection*>(tail)->touch(); // force scene redraw
if (action) {
static_cast<SoFCSelection*>(tail)->redrawHighlighted(action, false);
}
}
}
if (SoFCSelection::currenthighlight) {
SoFCSelection::currenthighlight->unref();
SoFCSelection::currenthighlight = nullptr;
}
}
SbBool SoFCSelection::isHighlighted(SoAction* action)
//
////////////////////////////////////////////////////////////////////////
{
auto actionPath = static_cast<const SoFullPath*>(action->getCurPath());
return (
currenthighlight && currenthighlight->getTail() == actionPath->getTail() && // nested SoHL!
*currenthighlight == *actionPath
);
}
void SoFCSelection::applySettings()
{
// TODO Some view providers got copy of this code: make them use this (2015-09-03, Fat-Zer)
// Note: SoFCUnifiedSelection got the same code, keep in sync or think about a way to share it
float transparency;
ParameterGrp::handle hGrp = Gui::WindowParameter::getDefaultParameter()->GetGroup("View");
bool enablePre = hGrp->GetBool("EnablePreselection", true);
bool enableSel = hGrp->GetBool("EnableSelection", true);
if (!enablePre) {
this->preselectionMode = Gui::SoFCSelection::OFF;
}
else {
// Search for a user defined value with the current color as default
SbColor highlightColor = this->colorHighlight.getValue();
auto highlight = (unsigned long)(highlightColor.getPackedValue());
highlight = hGrp->GetUnsigned("HighlightColor", highlight);
highlightColor.setPackedValue((uint32_t)highlight, transparency);
this->colorHighlight.setValue(highlightColor);
}
if (!enableSel) {
this->selectionMode = Gui::SoFCSelection::SEL_OFF;
}
else {
// Do the same with the selection color
SbColor selectionColor = this->colorSelection.getValue();
auto selection = (unsigned long)(selectionColor.getPackedValue());
selection = hGrp->GetUnsigned("SelectionColor", selection);
selectionColor.setPackedValue((uint32_t)selection, transparency);
this->colorSelection.setValue(selectionColor);
}
}
|