| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/**************************************************************************** |
| 3 |
* * |
| 4 |
* Copyright (c) 2025 Sayantan Deb <sayantandebin[at]gmail.com> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
#include "Gizmo.h" |
| 25 |
|
| 26 |
#include <cmath> |
| 27 |
|
| 28 |
#include <Inventor/nodes/SoOrthographicCamera.h> |
| 29 |
#include <Inventor/nodes/SoPerspectiveCamera.h> |
| 30 |
#include <Inventor/nodes/SoPickStyle.h> |
| 31 |
#include <algorithm> |
| 32 |
|
| 33 |
#include <App/Application.h> |
| 34 |
#include <Base/Converter.h> |
| 35 |
#include <Base/Parameter.h> |
| 36 |
#include <Base/Precision.h> |
| 37 |
#include "Base/ServiceProvider.h" |
| 38 |
#include <Base/Tools.h> |
| 39 |
#include <Document.h> |
| 40 |
#include <Gui/Inventor/Draggers/GizmoStyleParameters.h> |
| 41 |
#include <Gui/Inventor/So3DAnnotation.h> |
| 42 |
#include <Gui/Inventor/SoToggleSwitch.h> |
| 43 |
#include <Gui/ViewProviderDragger.h> |
| 44 |
#include <Gui/QuantitySpinBox.h> |
| 45 |
#include <Gui/Utilities.h> |
| 46 |
#include <Gui/View3DInventorViewer.h> |
| 47 |
|
| 48 |
#include "SoLinearDragger.h" |
| 49 |
#include "SoLinearDraggerGeometry.h" |
| 50 |
#include "SoRotationDragger.h" |
| 51 |
#include "SoRotationDraggerGeometry.h" |
| 52 |
|
| 53 |
using namespace Gui; |
| 54 |
|
| 55 |
void Gizmo::setDraggerPlacement(const Base::Vector3d& pos, const Base::Vector3d& dir) |
| 56 |
{ |
| 57 |
setDraggerPlacement(Base::convertTo<SbVec3f>(pos), Base::convertTo<SbVec3f>(dir)); |
| 58 |
} |
| 59 |
|
| 60 |
bool Gizmo::isDelayedUpdateEnabled() |
| 61 |
{ |
| 62 |
static Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter().GetGroup( |
| 63 |
"BaseApp/Preferences/Gui/Gizmos" |
| 64 |
); |
| 65 |
|
| 66 |
return hGrp->GetBool("DelayedGizmoUpdate", false); |
| 67 |
} |
| 68 |
|
| 69 |
double Gizmo::getMultFactor() |
| 70 |
{ |
| 71 |
return multFactor; |
| 72 |
} |
| 73 |
|
| 74 |
double Gizmo::getAddFactor() |
| 75 |
{ |
| 76 |
return addFactor; |
| 77 |
} |
| 78 |
|
| 79 |
bool Gizmo::getVisibility() |
| 80 |
{ |
| 81 |
return visible; |
| 82 |
} |
| 83 |
|
| 84 |
LinearGizmo::LinearGizmo(QuantitySpinBox* property) |
| 85 |
{ |
| 86 |
this->property = property; |
| 87 |
} |
| 88 |
|
| 89 |
SoInteractionKit* LinearGizmo::initDragger() |
| 90 |
{ |
| 91 |
draggerContainer = new SoLinearDraggerContainer; |
| 92 |
draggerContainer->color.setValue(1, 0, 0); |
| 93 |
dragger = draggerContainer->getDragger(); |
| 94 |
|
| 95 |
dragger->addStartCallback( |
| 96 |
[](void* data, SoDragger*) { static_cast<LinearGizmo*>(data)->draggingStarted(); }, |
| 97 |
this |
| 98 |
); |
| 99 |
dragger->addFinishCallback( |
| 100 |
[](void* data, SoDragger*) { static_cast<LinearGizmo*>(data)->draggingFinished(); }, |
| 101 |
this |
| 102 |
); |
| 103 |
dragger->addMotionCallback( |
| 104 |
[](void* data, SoDragger*) { static_cast<LinearGizmo*>(data)->draggingContinued(); }, |
| 105 |
this |
| 106 |
); |
| 107 |
|
| 108 |
dragger->labelVisible = false; |
| 109 |
|
| 110 |
dragger->instantiateBaseGeometry(); |
| 111 |
|
| 112 |
// change the dragger dimensions |
| 113 |
auto arrow = SO_GET_PART(dragger, "arrow", SoArrowGeometry); |
| 114 |
arrow->cylinderHeight = 3.5; |
| 115 |
arrow->cylinderRadius = 0.2; |
| 116 |
|
| 117 |
updateColorTheme(); |
| 118 |
|
| 119 |
setProperty(property); |
| 120 |
|
| 121 |
return draggerContainer; |
| 122 |
} |
| 123 |
|
| 124 |
void LinearGizmo::uninitDragger() |
| 125 |
{ |
| 126 |
dragger = nullptr; |
| 127 |
draggerContainer = nullptr; |
| 128 |
} |
| 129 |
|
| 130 |
void LinearGizmo::updateColorTheme() |
| 131 |
{ |
| 132 |
auto* styleParameterManager = Base::provideService<Gui::StyleParameters::ParameterManager>(); |
| 133 |
Base::Color baseColor = styleParameterManager->resolve(StyleParameters::LinearGizmoBaseColor); |
| 134 |
Base::Color activeColor = styleParameterManager->resolve(StyleParameters::LinearGizmoActiveColor); |
| 135 |
|
| 136 |
dragger->color = baseColor.asValue<SbColor>(); |
| 137 |
dragger->activeColor = activeColor.asValue<SbColor>(); |
| 138 |
|
| 139 |
auto baseGeom = SO_GET_PART(dragger, "baseGeom", SoArrowBase); |
| 140 |
Base::Color baseGeomColor = styleParameterManager->resolve( |
| 141 |
StyleParameters::DimensionVisualizerColor |
| 142 |
); |
| 143 |
baseGeom->color = baseGeomColor.asValue<SbColor>(); |
| 144 |
} |
| 145 |
|
| 146 |
GizmoPlacement LinearGizmo::getDraggerPlacement() |
| 147 |
{ |
| 148 |
assert(draggerContainer && "Forgot to call GizmoContainer::initGizmos?"); |
| 149 |
return {draggerContainer->translation.getValue(), draggerContainer->getPointerDirection()}; |
| 150 |
} |
| 151 |
|
| 152 |
void LinearGizmo::setDraggerPlacement(const SbVec3f& pos, const SbVec3f& dir) |
| 153 |
{ |
| 154 |
assert(draggerContainer && "Forgot to call GizmoContainer::initGizmos?"); |
| 155 |
draggerContainer->translation = pos; |
| 156 |
draggerContainer->setPointerDirection(dir); |
| 157 |
} |
| 158 |
|
| 159 |
void LinearGizmo::reverseDir() |
| 160 |
{ |
| 161 |
auto dir = getDraggerContainer()->getPointerDirection(); |
| 162 |
getDraggerContainer()->setPointerDirection(dir * -1); |
| 163 |
} |
| 164 |
|
| 165 |
double LinearGizmo::getDragLength() |
| 166 |
{ |
| 167 |
double dragLength = dragger->translationIncrementCount.getValue() |
| 168 |
* dragger->translationIncrement.getValue(); |
| 169 |
|
| 170 |
return (dragLength - addFactor) / multFactor; |
| 171 |
} |
| 172 |
|
| 173 |
void LinearGizmo::setDragLength(double dragLength) |
| 174 |
{ |
| 175 |
dragLength = dragLength * multFactor + addFactor; |
| 176 |
dragger->translation = {0, static_cast<float>(dragLength), 0}; |
| 177 |
} |
| 178 |
|
| 179 |
void LinearGizmo::setGeometryScale(float scale) |
| 180 |
{ |
| 181 |
dragger->geometryScale = SbVec3f(scale, scale, scale); |
| 182 |
// Scales the dragger increment in exponents of 10 based on the zoom level (scale) |
| 183 |
constexpr float base = 10.0F; |
| 184 |
dragger->translationIncrement = multFactor * std::pow(base, std::floor(std::log10(scale))); |
| 185 |
} |
| 186 |
|
| 187 |
SoLinearDraggerContainer* LinearGizmo::getDraggerContainer() |
| 188 |
{ |
| 189 |
assert(draggerContainer && "Forgot to call GizmoContainer::initGizmos?"); |
| 190 |
return draggerContainer; |
| 191 |
} |
| 192 |
|
| 193 |
void LinearGizmo::setProperty(QuantitySpinBox* property) |
| 194 |
{ |
| 195 |
QuantitySpinBox::disconnect(quantityChangedConnection); |
| 196 |
QuantitySpinBox::disconnect(formulaDialogConnection); |
| 197 |
|
| 198 |
this->property = property; |
| 199 |
quantityChangedConnection = QuantitySpinBox::connect( |
| 200 |
property, |
| 201 |
qOverload<double>(&Gui::QuantitySpinBox::valueChanged), |
| 202 |
[this](double value) { setDragLength(value); } |
| 203 |
); |
| 204 |
formulaDialogConnection |
| 205 |
= QuantitySpinBox::connect(property, &Gui::QuantitySpinBox::showFormulaDialog, [this](bool) { |
| 206 |
// This will set the visibility of the actual geometry to true or false |
| 207 |
// based on if an expression is bound and the externally set visibility |
| 208 |
setVisibility(visible); |
| 209 |
}); |
| 210 |
|
| 211 |
// Updates the gizmo state based on the new property |
| 212 |
setDragLength(property->rawValue()); |
| 213 |
setVisibility(visible); |
| 214 |
} |
| 215 |
|
| 216 |
void LinearGizmo::setMultFactor(const double val) |
| 217 |
{ |
| 218 |
multFactor = val; |
| 219 |
setDragLength(property->value().getValue()); |
| 220 |
} |
| 221 |
|
| 222 |
void LinearGizmo::setAddFactor(const double val) |
| 223 |
{ |
| 224 |
addFactor = val; |
| 225 |
setDragLength(property->value().getValue()); |
| 226 |
} |
| 227 |
|
| 228 |
void LinearGizmo::setVisibility(bool visible) |
| 229 |
{ |
| 230 |
this->visible = visible; |
| 231 |
getDraggerContainer()->visible = visible && !property->hasExpression(); |
| 232 |
} |
| 233 |
|
| 234 |
void LinearGizmo::draggingStarted() |
| 235 |
{ |
| 236 |
initialValue = property->value().getValue(); |
| 237 |
dragger->translationIncrementCount.setValue(0); |
| 238 |
|
| 239 |
if (isDelayedUpdateEnabled()) { |
| 240 |
property->blockSignals(true); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
void LinearGizmo::draggingFinished() |
| 245 |
{ |
| 246 |
if (isDelayedUpdateEnabled()) { |
| 247 |
property->blockSignals(false); |
| 248 |
property->valueChanged(property->value().getValue()); |
| 249 |
} |
| 250 |
|
| 251 |
property->setFocus(); |
| 252 |
property->selectAll(); |
| 253 |
} |
| 254 |
|
| 255 |
void LinearGizmo::draggingContinued() |
| 256 |
{ |
| 257 |
double value = initialValue + getDragLength(); |
| 258 |
// TODO: Need to change the lower limit to sudoThis->property->minimum() once the |
| 259 |
// two direction extrude work gets merged |
| 260 |
value = std::clamp(value, dragger->translationIncrement.getValue(), property->maximum()); |
| 261 |
|
| 262 |
property->setValue(value); |
| 263 |
setDragLength(value); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
RotationGizmo::RotationGizmo(QuantitySpinBox* property) |
| 268 |
{ |
| 269 |
this->property = property; |
| 270 |
} |
| 271 |
|
| 272 |
RotationGizmo::~RotationGizmo() |
| 273 |
{ |
| 274 |
translationSensor.detach(); |
| 275 |
translationSensor.setData(nullptr); |
| 276 |
translationSensor.setFunction(nullptr); |
| 277 |
} |
| 278 |
|
| 279 |
SoInteractionKit* RotationGizmo::initDragger() |
| 280 |
{ |
| 281 |
multFactor = std::numbers::pi_v<float> / 180.0; |
| 282 |
draggerContainer = new SoRotationDraggerContainer; |
| 283 |
|
| 284 |
draggerContainer->color.setValue(1, 0, 0); |
| 285 |
dragger = draggerContainer->getDragger(); |
| 286 |
dragger->rotationIncrement = std::numbers::pi / 90.0; |
| 287 |
|
| 288 |
auto rotator = new SoRotatorGeometry; |
| 289 |
rotator->arcAngle = std::numbers::pi_v<float> / 6.0f; |
| 290 |
rotator->arcRadius = 16.0f; |
| 291 |
dragger->setPart("rotator", rotator); |
| 292 |
|
| 293 |
dragger->addStartCallback( |
| 294 |
[](void* data, SoDragger*) { static_cast<RotationGizmo*>(data)->draggingStarted(); }, |
| 295 |
this |
| 296 |
); |
| 297 |
dragger->addFinishCallback( |
| 298 |
[](void* data, SoDragger*) { static_cast<RotationGizmo*>(data)->draggingFinished(); }, |
| 299 |
this |
| 300 |
); |
| 301 |
dragger->addMotionCallback( |
| 302 |
[](void* data, SoDragger*) { static_cast<RotationGizmo*>(data)->draggingContinued(); }, |
| 303 |
this |
| 304 |
); |
| 305 |
|
| 306 |
setProperty(property); |
| 307 |
|
| 308 |
updateColorTheme(); |
| 309 |
|
| 310 |
return draggerContainer; |
| 311 |
} |
| 312 |
|
| 313 |
void RotationGizmo::uninitDragger() |
| 314 |
{ |
| 315 |
dragger = nullptr; |
| 316 |
draggerContainer = nullptr; |
| 317 |
|
| 318 |
translationSensor.detach(); |
| 319 |
translationSensor.setData(nullptr); |
| 320 |
translationSensor.setFunction(nullptr); |
| 321 |
} |
| 322 |
|
| 323 |
void RotationGizmo::updateColorTheme() |
| 324 |
{ |
| 325 |
auto* styleParameterManager = Base::provideService<Gui::StyleParameters::ParameterManager>(); |
| 326 |
Base::Color baseColor = styleParameterManager->resolve(StyleParameters::RotationGizmoBaseColor); |
| 327 |
Base::Color activeColor = styleParameterManager->resolve(StyleParameters::RotationGizmoActiveColor); |
| 328 |
|
| 329 |
dragger->color = baseColor.asValue<SbColor>(); |
| 330 |
dragger->activeColor = activeColor.asValue<SbColor>(); |
| 331 |
} |
| 332 |
|
| 333 |
GizmoPlacement RotationGizmo::getDraggerPlacement() |
| 334 |
{ |
| 335 |
assert(draggerContainer && "Forgot to call GizmoContainer::initGizmos?"); |
| 336 |
return {draggerContainer->translation.getValue(), draggerContainer->getPointerDirection()}; |
| 337 |
} |
| 338 |
|
| 339 |
void RotationGizmo::setDraggerPlacement(const SbVec3f& pos, const SbVec3f& dir) |
| 340 |
{ |
| 341 |
assert(draggerContainer && "Forgot to call GizmoContainer::initGizmos?"); |
| 342 |
draggerContainer->translation = pos; |
| 343 |
draggerContainer->setPointerDirection(dir); |
| 344 |
} |
| 345 |
|
| 346 |
void RotationGizmo::reverseDir() |
| 347 |
{ |
| 348 |
auto dir = getDraggerContainer()->getPointerDirection(); |
| 349 |
getDraggerContainer()->setPointerDirection(dir * -1); |
| 350 |
} |
| 351 |
|
| 352 |
void RotationGizmo::placeOverLinearGizmo(LinearGizmo* gizmo) |
| 353 |
{ |
| 354 |
linearGizmo = gizmo; |
| 355 |
|
| 356 |
GizmoPlacement placement = gizmo->getDraggerPlacement(); |
| 357 |
|
| 358 |
draggerContainer->translation = Base::convertTo<SbVec3f>(placement.pos); |
| 359 |
draggerContainer->setPointerDirection(placement.dir); |
| 360 |
|
| 361 |
translationSensor.setData(this); |
| 362 |
translationSensor.setFunction(translationSensorCB); |
| 363 |
translationSensor.setPriority(0); |
| 364 |
SoSFVec3f& translation = gizmo->getDraggerContainer()->getDragger()->translation; |
| 365 |
translationSensor.attach(&translation); |
| 366 |
translation.touch(); |
| 367 |
|
| 368 |
automaticOrientation = true; |
| 369 |
} |
| 370 |
|
| 371 |
void RotationGizmo::translationSensorCB(void* data, SoSensor* sensor) |
| 372 |
{ |
| 373 |
auto sudoThis = static_cast<RotationGizmo*>(data); |
| 374 |
auto translationSensor = static_cast<SoFieldSensor*>(sensor); |
| 375 |
|
| 376 |
GizmoPlacement placement = sudoThis->linearGizmo->getDraggerPlacement(); |
| 377 |
|
| 378 |
SbVec3f translation = static_cast<SoSFVec3f*>(translationSensor->getAttachedField())->getValue(); |
| 379 |
float yComp = translation.getValue()[1]; |
| 380 |
SbVec3f dir = placement.dir; |
| 381 |
dir.normalize(); |
| 382 |
sudoThis->draggerContainer->translation = placement.pos + dir * (yComp + sudoThis->sepDistance); |
| 383 |
} |
| 384 |
|
| 385 |
void RotationGizmo::placeBelowLinearGizmo(LinearGizmo* gizmo) |
| 386 |
{ |
| 387 |
linearGizmo = gizmo; |
| 388 |
|
| 389 |
GizmoPlacement placement = gizmo->getDraggerPlacement(); |
| 390 |
|
| 391 |
draggerContainer->translation = Base::convertTo<SbVec3f>(placement.pos); |
| 392 |
draggerContainer->setPointerDirection(-placement.dir); |
| 393 |
|
| 394 |
translationSensor.setData(this); |
| 395 |
translationSensor.setFunction(translationSensorCB); |
| 396 |
translationSensor.setPriority(0); |
| 397 |
SoSFVec3f& translation = gizmo->getDraggerContainer()->getDragger()->translation; |
| 398 |
translationSensor.attach(&translation); |
| 399 |
translation.touch(); |
| 400 |
} |
| 401 |
|
| 402 |
double RotationGizmo::getRotAngle() |
| 403 |
{ |
| 404 |
double rotAngle = dragger->rotationIncrementCount.getValue() |
| 405 |
* dragger->rotationIncrement.getValue(); |
| 406 |
|
| 407 |
return (rotAngle - addFactor) / multFactor; |
| 408 |
} |
| 409 |
|
| 410 |
void RotationGizmo::setRotAngle(double angle) |
| 411 |
{ |
| 412 |
angle = multFactor * angle + addFactor; |
| 413 |
dragger->rotation = SbRotation({0, 0, 1.0f}, static_cast<float>(angle)); |
| 414 |
} |
| 415 |
|
| 416 |
void RotationGizmo::setGeometryScale(float scale) |
| 417 |
{ |
| 418 |
dragger->geometryScale = SbVec3f(scale, scale, scale); |
| 419 |
} |
| 420 |
|
| 421 |
SoRotationDraggerContainer* RotationGizmo::getDraggerContainer() |
| 422 |
{ |
| 423 |
assert(draggerContainer && "Forgot to call GizmoContainer::initGizmos?"); |
| 424 |
return draggerContainer; |
| 425 |
} |
| 426 |
|
| 427 |
void RotationGizmo::draggingStarted() |
| 428 |
{ |
| 429 |
initialValue = property->value().getValue(); |
| 430 |
dragger->rotationIncrementCount.setValue(0); |
| 431 |
|
| 432 |
if (isDelayedUpdateEnabled()) { |
| 433 |
property->blockSignals(true); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
void RotationGizmo::draggingFinished() |
| 438 |
{ |
| 439 |
if (isDelayedUpdateEnabled()) { |
| 440 |
property->blockSignals(false); |
| 441 |
property->valueChanged(property->value().getValue()); |
| 442 |
} |
| 443 |
|
| 444 |
property->setFocus(); |
| 445 |
property->selectAll(); |
| 446 |
} |
| 447 |
|
| 448 |
void RotationGizmo::draggingContinued() |
| 449 |
{ |
| 450 |
double value = initialValue + getRotAngle(); |
| 451 |
value = Base::clampAngle( |
| 452 |
value, |
| 453 |
property->minimum(), |
| 454 |
property->maximum(), |
| 455 |
Base::Precision::Confusion() |
| 456 |
); |
| 457 |
|
| 458 |
property->setValue(value); |
| 459 |
setRotAngle(value); |
| 460 |
} |
| 461 |
|
| 462 |
void RotationGizmo::orientAlongCamera(SoCamera* camera) |
| 463 |
{ |
| 464 |
if (linearGizmo == nullptr || automaticOrientation == false) { |
| 465 |
return; |
| 466 |
} |
| 467 |
|
| 468 |
SbVec3f cameraDir {0, 0, 1}; |
| 469 |
camera->orientation.getValue().multVec(cameraDir, cameraDir); |
| 470 |
SbVec3f pointerDir = linearGizmo->getDraggerContainer()->getPointerDirection(); |
| 471 |
|
| 472 |
pointerDir.normalize(); |
| 473 |
auto proj = cameraDir - cameraDir.dot(pointerDir) * pointerDir; |
| 474 |
if (proj.equals(SbVec3f {0, 0, 0}, 0.001)) { |
| 475 |
return; |
| 476 |
} |
| 477 |
|
| 478 |
assert(draggerContainer && "Forgot to call GizmoContainer::initGizmos?"); |
| 479 |
draggerContainer->setArcNormalDirection(proj); |
| 480 |
} |
| 481 |
|
| 482 |
void RotationGizmo::setProperty(QuantitySpinBox* property) |
| 483 |
{ |
| 484 |
QuantitySpinBox::disconnect(quantityChangedConnection); |
| 485 |
QuantitySpinBox::disconnect(formulaDialogConnection); |
| 486 |
|
| 487 |
this->property = property; |
| 488 |
quantityChangedConnection = QuantitySpinBox::connect( |
| 489 |
property, |
| 490 |
qOverload<double>(&Gui::QuantitySpinBox::valueChanged), |
| 491 |
[this](double value) { setRotAngle(value); } |
| 492 |
); |
| 493 |
formulaDialogConnection |
| 494 |
= QuantitySpinBox::connect(property, &Gui::QuantitySpinBox::showFormulaDialog, [this](bool) { |
| 495 |
// This will set the visibility of the actual geometry to true or false |
| 496 |
// based on if an expression is bound and the externally set visibility |
| 497 |
setVisibility(visible); |
| 498 |
}); |
| 499 |
|
| 500 |
// Updates the gizmo state based on the new property |
| 501 |
setRotAngle(property->rawValue()); |
| 502 |
setVisibility(visible); |
| 503 |
} |
| 504 |
|
| 505 |
void RotationGizmo::setMultFactor(const double val) |
| 506 |
{ |
| 507 |
multFactor = val; |
| 508 |
setRotAngle(property->value().getValue()); |
| 509 |
} |
| 510 |
|
| 511 |
void RotationGizmo::setAddFactor(const double val) |
| 512 |
{ |
| 513 |
addFactor = val; |
| 514 |
setRotAngle(property->value().getValue()); |
| 515 |
} |
| 516 |
|
| 517 |
void RotationGizmo::setVisibility(bool visible) |
| 518 |
{ |
| 519 |
this->visible = visible; |
| 520 |
getDraggerContainer()->visible = visible && !property->hasExpression(); |
| 521 |
} |
| 522 |
|
| 523 |
DirectedRotationGizmo::DirectedRotationGizmo(QuantitySpinBox* property) |
| 524 |
: RotationGizmo(property) |
| 525 |
{} |
| 526 |
|
| 527 |
SoInteractionKit* DirectedRotationGizmo::initDragger() |
| 528 |
{ |
| 529 |
SoInteractionKit* ret = inherited::initDragger(); |
| 530 |
|
| 531 |
auto dragger = getDraggerContainer()->getDragger(); |
| 532 |
auto rotator = new SoRotatorGeometry2; |
| 533 |
rotator->arcAngle = std::numbers::pi_v<float> / 6.0f; |
| 534 |
rotator->arcRadius = 16.0f; |
| 535 |
rotator->rightArrowVisible = false; |
| 536 |
dragger->setPart("rotator", rotator); |
| 537 |
|
| 538 |
updateColorTheme(); |
| 539 |
|
| 540 |
return ret; |
| 541 |
} |
| 542 |
|
| 543 |
void DirectedRotationGizmo::flipArrow() |
| 544 |
{ |
| 545 |
auto dragger = getDraggerContainer()->getDragger(); |
| 546 |
auto rotator = SO_GET_PART(dragger, "rotator", SoRotatorGeometry2); |
| 547 |
|
| 548 |
rotator->toggleArrowVisibility(); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
RadialGizmo::RadialGizmo(QuantitySpinBox* property) |
| 553 |
: RotationGizmo(property) |
| 554 |
{} |
| 555 |
|
| 556 |
SoInteractionKit* RadialGizmo::initDragger() |
| 557 |
{ |
| 558 |
SoInteractionKit* ret = inherited::initDragger(); |
| 559 |
|
| 560 |
auto dragger = getDraggerContainer()->getDragger(); |
| 561 |
auto rotator = new SoRotatorArrow; |
| 562 |
rotator->geometryScale.connectFrom(&dragger->geometryScale); |
| 563 |
dragger->setPart("rotator", rotator); |
| 564 |
|
| 565 |
dragger->instantiateBaseGeometry(); |
| 566 |
|
| 567 |
updateColorTheme(); |
| 568 |
|
| 569 |
return ret; |
| 570 |
} |
| 571 |
|
| 572 |
void RadialGizmo::setRadius(float radius) |
| 573 |
{ |
| 574 |
auto dragger = getDraggerContainer()->getDragger(); |
| 575 |
auto rotator = SO_GET_PART(dragger, "rotator", SoRotatorArrow); |
| 576 |
auto baseGeom = SO_GET_PART(dragger, "baseGeom", SoRotatorBase); |
| 577 |
|
| 578 |
rotator->radius = baseGeom->arcRadius = radius; |
| 579 |
} |
| 580 |
|
| 581 |
void RadialGizmo::flipArrow() |
| 582 |
{ |
| 583 |
auto dragger = getDraggerContainer()->getDragger(); |
| 584 |
auto rotator = SO_GET_PART(dragger, "rotator", SoRotatorArrow); |
| 585 |
|
| 586 |
rotator->flipArrow(); |
| 587 |
} |
| 588 |
|
| 589 |
void RadialGizmo::updateColorTheme() |
| 590 |
{ |
| 591 |
auto dragger = getDraggerContainer()->getDragger(); |
| 592 |
|
| 593 |
auto* styleParameterManager = Base::provideService<Gui::StyleParameters::ParameterManager>(); |
| 594 |
Base::Color baseColor = styleParameterManager->resolve(StyleParameters::RotationGizmoBaseColor); |
| 595 |
Base::Color activeColor = styleParameterManager->resolve(StyleParameters::RotationGizmoActiveColor); |
| 596 |
|
| 597 |
dragger->color = baseColor.asValue<SbColor>(); |
| 598 |
dragger->activeColor = activeColor.asValue<SbColor>(); |
| 599 |
|
| 600 |
auto baseGeom = SO_GET_PART(dragger, "baseGeom", SoRotatorBase); |
| 601 |
Base::Color baseGeomColor = styleParameterManager->resolve( |
| 602 |
StyleParameters::DimensionVisualizerColor |
| 603 |
); |
| 604 |
baseGeom->color = baseGeomColor.asValue<SbColor>(); |
| 605 |
} |
| 606 |
|
| 607 |
SO_KIT_SOURCE(GizmoContainer) |
| 608 |
|
| 609 |
void GizmoContainer::initClass() |
| 610 |
{ |
| 611 |
SO_KIT_INIT_CLASS(GizmoContainer, SoBaseKit, "BaseKit"); |
| 612 |
} |
| 613 |
|
| 614 |
GizmoContainer::GizmoContainer() |
| 615 |
: viewProvider(nullptr) |
| 616 |
{ |
| 617 |
SO_KIT_CONSTRUCTOR(GizmoContainer); |
| 618 |
|
| 619 |
#if defined(Q_OS_MACOS) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) |
| 620 |
this->ref(); |
| 621 |
#endif |
| 622 |
|
| 623 |
FC_ADD_CATALOG_ENTRY(annotation, So3DAnnotation, this); |
| 624 |
FC_ADD_CATALOG_ENTRY(pickStyle, SoPickStyle, annotation); |
| 625 |
FC_ADD_CATALOG_ENTRY(toggleSwitch, SoToggleSwitch, annotation); |
| 626 |
FC_ADD_CATALOG_ENTRY(geometry, SoSeparator, toggleSwitch); |
| 627 |
|
| 628 |
SO_KIT_INIT_INSTANCE(); |
| 629 |
|
| 630 |
SO_KIT_ADD_FIELD(visible, (1)); |
| 631 |
|
| 632 |
auto pickStyle = SO_GET_ANY_PART(this, "pickStyle", SoPickStyle); |
| 633 |
pickStyle->style = SoPickStyle::SHAPE_ON_TOP; |
| 634 |
|
| 635 |
auto toggleSwitch = SO_GET_ANY_PART(this, "toggleSwitch", SoToggleSwitch); |
| 636 |
toggleSwitch->on.connectFrom(&visible); |
| 637 |
|
| 638 |
setPart("geometry", new SoSeparator); |
| 639 |
|
| 640 |
cameraSensor.setFunction(&GizmoContainer::cameraChangeCallback); |
| 641 |
cameraSensor.setData(this); |
| 642 |
|
| 643 |
cameraPositionSensor.setData(this); |
| 644 |
cameraPositionSensor.setFunction(cameraPositionChangeCallback); |
| 645 |
} |
| 646 |
|
| 647 |
GizmoContainer::~GizmoContainer() |
| 648 |
{ |
| 649 |
cameraSensor.setData(nullptr); |
| 650 |
cameraSensor.detach(); |
| 651 |
|
| 652 |
cameraPositionSensor.setData(nullptr); |
| 653 |
cameraPositionSensor.detach(); |
| 654 |
|
| 655 |
uninitGizmos(); |
| 656 |
|
| 657 |
if (!viewProvider.expired()) { |
| 658 |
viewProvider->setGizmoContainer(nullptr); |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
void GizmoContainer::initGizmos() |
| 663 |
{ |
| 664 |
auto geometry = SO_GET_ANY_PART(this, "geometry", SoSeparator); |
| 665 |
for (auto gizmo : gizmos) { |
| 666 |
geometry->addChild(gizmo->initDragger()); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
void GizmoContainer::uninitGizmos() |
| 671 |
{ |
| 672 |
for (auto gizmo : gizmos) { |
| 673 |
gizmo->uninitDragger(); |
| 674 |
delete gizmo; |
| 675 |
} |
| 676 |
gizmos.clear(); |
| 677 |
} |
| 678 |
|
| 679 |
void GizmoContainer::addGizmos(std::initializer_list<Gui::Gizmo*> gizmos) |
| 680 |
{ |
| 681 |
assert(this->gizmos.size() == 0 && "Already called GizmoContainer::addGizmos?"); |
| 682 |
|
| 683 |
for (auto gizmo : gizmos) { |
| 684 |
addGizmo(gizmo); |
| 685 |
} |
| 686 |
initGizmos(); |
| 687 |
} |
| 688 |
|
| 689 |
void GizmoContainer::addGizmo(Gizmo* gizmo) |
| 690 |
{ |
| 691 |
assert(std::ranges::find(gizmos, gizmo) == gizmos.end() && "this gizmo is already added!"); |
| 692 |
gizmos.push_back(gizmo); |
| 693 |
} |
| 694 |
|
| 695 |
void GizmoContainer::attachViewer(Gui::View3DInventorViewer* viewer, Base::Placement& origin) |
| 696 |
{ |
| 697 |
if (!viewer) { |
| 698 |
return; |
| 699 |
} |
| 700 |
|
| 701 |
setUpAutoScale(viewer->getSoRenderManager()->getCamera()); |
| 702 |
|
| 703 |
auto mat = origin.toMatrix(); |
| 704 |
|
| 705 |
viewer->getDocument()->setEditingTransform(mat); |
| 706 |
So3DAnnotation* annotation = SO_GET_ANY_PART(this, "annotation", So3DAnnotation); |
| 707 |
viewer->setupEditingRoot(annotation, &mat); |
| 708 |
} |
| 709 |
|
| 710 |
void GizmoContainer::setUpAutoScale(SoCamera* cameraIn) |
| 711 |
{ |
| 712 |
if (cameraIn->getTypeId() == SoOrthographicCamera::getClassTypeId()) { |
| 713 |
auto localCamera = dynamic_cast<SoOrthographicCamera*>(cameraIn); |
| 714 |
cameraSensor.attach(&localCamera->height); |
| 715 |
cameraPositionSensor.attach(&localCamera->orientation); |
| 716 |
calculateScaleAndOrientation(); |
| 717 |
} |
| 718 |
else if (cameraIn->getTypeId() == SoPerspectiveCamera::getClassTypeId()) { |
| 719 |
auto localCamera = dynamic_cast<SoPerspectiveCamera*>(cameraIn); |
| 720 |
cameraSensor.attach(&localCamera->position); |
| 721 |
cameraPositionSensor.attach(&localCamera->orientation); |
| 722 |
calculateScaleAndOrientation(); |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
void GizmoContainer::calculateScaleAndOrientation() |
| 727 |
{ |
| 728 |
if (cameraSensor.getAttachedField()) { |
| 729 |
cameraChangeCallback(this, nullptr); |
| 730 |
cameraPositionChangeCallback(this, nullptr); |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
void GizmoContainer::cameraChangeCallback(void* data, SoSensor*) |
| 735 |
{ |
| 736 |
auto sudoThis = static_cast<GizmoContainer*>(data); |
| 737 |
|
| 738 |
SoField* field = sudoThis->cameraSensor.getAttachedField(); |
| 739 |
if (!field) { |
| 740 |
return; |
| 741 |
} |
| 742 |
|
| 743 |
auto camera = static_cast<SoCamera*>(field->getContainer()); |
| 744 |
|
| 745 |
SbViewVolume viewVolume = camera->getViewVolume(); |
| 746 |
for (auto gizmo : sudoThis->gizmos) { |
| 747 |
float localScale = viewVolume.getWorldToScreenScale(gizmo->getDraggerPlacement().pos, 0.015); |
| 748 |
gizmo->setGeometryScale(localScale); |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
void GizmoContainer::cameraPositionChangeCallback(void* data, SoSensor*) |
| 753 |
{ |
| 754 |
auto sudoThis = static_cast<GizmoContainer*>(data); |
| 755 |
|
| 756 |
SoField* field = sudoThis->cameraSensor.getAttachedField(); |
| 757 |
if (field) { |
| 758 |
auto camera = static_cast<SoCamera*>(field->getContainer()); |
| 759 |
|
| 760 |
for (auto gizmo : sudoThis->gizmos) { |
| 761 |
gizmo->orientAlongCamera(camera); |
| 762 |
} |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
bool GizmoContainer::isEnabled() |
| 767 |
{ |
| 768 |
static Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter().GetGroup( |
| 769 |
"BaseApp/Preferences/Gui/Gizmos" |
| 770 |
); |
| 771 |
|
| 772 |
return hGrp->GetBool("EnableGizmos", true); |
| 773 |
} |
| 774 |
|
| 775 |
std::unique_ptr<GizmoContainer> GizmoContainer::create( |
| 776 |
std::initializer_list<Gui::Gizmo*> gizmos, |
| 777 |
ViewProviderDragger* vp |
| 778 |
) |
| 779 |
{ |
| 780 |
auto gizmoContainer = std::make_unique<GizmoContainer>(); |
| 781 |
gizmoContainer->addGizmos(gizmos); |
| 782 |
gizmoContainer->viewProvider = vp; |
| 783 |
|
| 784 |
vp->setGizmoContainer(gizmoContainer.get()); |
| 785 |
|
| 786 |
return gizmoContainer; |
| 787 |
} |