Debian Codegraph @

src/Gui/Inventor/Draggers/SoTransformDragger.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2015 Thomas Anderson <blobfish[at]gmx.com> *
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 <cassert>
24 #include <numbers>
25
26 #include <Inventor/SbRotation.h>
27 #include <Inventor/actions/SoGLRenderAction.h>
28 #include <Inventor/engines/SoComposeVec3f.h>
29 #include <Inventor/nodes/SoLightModel.h>
30 #include <Inventor/nodes/SoDrawStyle.h>
31 #include <Inventor/nodes/SoBaseColor.h>
32 #include <Inventor/nodes/SoCone.h>
33 #include <Inventor/nodes/SoCoordinate3.h>
34 #include <Inventor/nodes/SoCylinder.h>
35 #include <Inventor/nodes/SoCube.h>
36 #include <Inventor/nodes/SoGroup.h>
37 #include <Inventor/nodes/SoLineSet.h>
38 #include <Inventor/nodes/SoOrthographicCamera.h>
39 #include <Inventor/nodes/SoPerspectiveCamera.h>
40 #include <Inventor/nodes/SoPickStyle.h>
41 #include <Inventor/nodes/SoRotation.h>
42 #include <Inventor/nodes/SoSeparator.h>
43 #include <Inventor/nodes/SoSphere.h>
44 #include <Inventor/nodes/SoSwitch.h>
45 #include <Inventor/nodes/SoTranslation.h>
46 #include <Inventor/nodes/SoText2.h>
47 #include <Inventor/nodes/SoAnnotation.h>
48 #include <Inventor/nodes/SoFontStyle.h>
49
50 #include <Base/Quantity.h>
51 #include <Base/Converter.h>
52
53 #include "SoTransformDragger.h"
54 #include "SoLinearDragger.h"
55 #include "SoPlanarDragger.h"
56 #include "SoRotationDragger.h"
57 #include "Utilities.h"
58
59 #include <SoTextLabel.h>
60
61
62 /*
63 GENERAL NOTE ON COIN3D CUSTOM DRAGGERS
64 * You basically have two choices for creating custom dragger geometry.
65 * 1) create an .iv file and set environment variable to the file path. This
66 * comes with install headaches.
67 * 2) create an .iv file and run through a mock compiler that generates a header
68 * file to include in the project. I would have gone this way but after installing
69 * inventor-demo(ubuntu), the mock compiler tool was there only in source and make
70 * didn't do anything. Didn't want to put any time into something I didn't like anyway.
71 *
72 * static SbList <SoNode*> * defaultdraggerparts = NULL; is a global definition
73 * in SoInteractionKit that contains the geometry. There doesn't appear to be anyway
74 * to add to this other than readDefaultParts, that takes a file. So maybe a temp file?
75 *
76 * naming appears to be central to the core. It looks like as long as an object
77 * is alive SoNode::getByName() will find it. So maybe just create my own little
78 * container of objects to keep the default geometry alive....This appears to be
79 * working and I like this solution.
80 *
81 * SoInteractionKit warns about these
82 * names all being the same scope and do NOT have to be unique. Need to make names
83 * descriptive to avoid collisions.
84
85 * this is point of the SoGroup accessed from SoFCDB::getStorage().
86 */
87
88 using namespace Gui;
89
90 SO_KIT_SOURCE(SoTransformDragger)
91
92 void SoTransformDragger::initClass()
93 {
94 SoLinearDraggerContainer::initClass();
95 SoPlanarDragger::initClass();
96 SoRotationDraggerContainer::initClass();
97 SO_KIT_INIT_CLASS(SoTransformDragger, SoDragger, "Dragger");
98 }
99
100 SoTransformDragger::SoTransformDragger()
101 : axisScale(1.0f, 1.0f, 1.0f)
102 {
103 SO_KIT_CONSTRUCTOR(SoTransformDragger);
104
105 #if defined(Q_OS_MACOS) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD)
106 this->ref();
107 #endif
108
109 SO_KIT_ADD_CATALOG_ENTRY(annotation, So3DAnnotation, TRUE, geomSeparator, "", TRUE);
110 SO_KIT_ADD_CATALOG_ENTRY(scaleNode, SoScale, TRUE, annotation, "", TRUE);
111 SO_KIT_ADD_CATALOG_ENTRY(pickStyle, SoPickStyle, TRUE, annotation, "", TRUE);
112
113 // Translator
114 SO_KIT_ADD_CATALOG_ENTRY(xTranslatorDragger, SoLinearDraggerContainer, TRUE, annotation, "", TRUE);
115 SO_KIT_ADD_CATALOG_ENTRY(yTranslatorDragger, SoLinearDraggerContainer, TRUE, annotation, "", TRUE);
116 SO_KIT_ADD_CATALOG_ENTRY(zTranslatorDragger, SoLinearDraggerContainer, TRUE, annotation, "", TRUE);
117
118 // Planar Translator
119
120 SO_KIT_ADD_CATALOG_ENTRY(xyPlanarTranslatorSwitch, SoSwitch, TRUE, annotation, "", TRUE);
121 SO_KIT_ADD_CATALOG_ENTRY(yzPlanarTranslatorSwitch, SoSwitch, TRUE, annotation, "", TRUE);
122 SO_KIT_ADD_CATALOG_ENTRY(zxPlanarTranslatorSwitch, SoSwitch, TRUE, annotation, "", TRUE);
123
124 SO_KIT_ADD_CATALOG_ENTRY(
125 xyPlanarTranslatorSeparator,
126 SoSeparator,
127 TRUE,
128 xyPlanarTranslatorSwitch,
129 "",
130 TRUE
131 );
132 SO_KIT_ADD_CATALOG_ENTRY(
133 yzPlanarTranslatorSeparator,
134 SoSeparator,
135 TRUE,
136 yzPlanarTranslatorSwitch,
137 "",
138 TRUE
139 );
140 SO_KIT_ADD_CATALOG_ENTRY(
141 zxPlanarTranslatorSeparator,
142 SoSeparator,
143 TRUE,
144 zxPlanarTranslatorSwitch,
145 "",
146 TRUE
147 );
148
149 SO_KIT_ADD_CATALOG_ENTRY(
150 xyPlanarTranslatorColor,
151 SoBaseColor,
152 TRUE,
153 xyPlanarTranslatorSeparator,
154 "",
155 TRUE
156 );
157 SO_KIT_ADD_CATALOG_ENTRY(
158 yzPlanarTranslatorColor,
159 SoBaseColor,
160 TRUE,
161 yzPlanarTranslatorSeparator,
162 "",
163 TRUE
164 );
165 SO_KIT_ADD_CATALOG_ENTRY(
166 zxPlanarTranslatorColor,
167 SoBaseColor,
168 TRUE,
169 zxPlanarTranslatorSeparator,
170 "",
171 TRUE
172 );
173
174 SO_KIT_ADD_CATALOG_ENTRY(
175 xyPlanarTranslatorRotation,
176 SoRotation,
177 TRUE,
178 xyPlanarTranslatorSeparator,
179 "",
180 TRUE
181 );
182 SO_KIT_ADD_CATALOG_ENTRY(
183 yzPlanarTranslatorRotation,
184 SoRotation,
185 TRUE,
186 yzPlanarTranslatorSeparator,
187 "",
188 TRUE
189 );
190 SO_KIT_ADD_CATALOG_ENTRY(
191 zxPlanarTranslatorRotation,
192 SoRotation,
193 TRUE,
194 zxPlanarTranslatorSeparator,
195 "",
196 TRUE
197 );
198
199 SO_KIT_ADD_CATALOG_ENTRY(
200 xyPlanarTranslatorDragger,
201 SoPlanarDragger,
202 TRUE,
203 xyPlanarTranslatorSeparator,
204 "",
205 TRUE
206 );
207 SO_KIT_ADD_CATALOG_ENTRY(
208 yzPlanarTranslatorDragger,
209 SoPlanarDragger,
210 TRUE,
211 yzPlanarTranslatorSeparator,
212 "",
213 TRUE
214 );
215 SO_KIT_ADD_CATALOG_ENTRY(
216 zxPlanarTranslatorDragger,
217 SoPlanarDragger,
218 TRUE,
219 zxPlanarTranslatorSeparator,
220 "",
221 TRUE
222 );
223
224 // Rotator
225 SO_KIT_ADD_CATALOG_ENTRY(xRotatorDragger, SoRotationDraggerContainer, TRUE, annotation, "", TRUE);
226 SO_KIT_ADD_CATALOG_ENTRY(yRotatorDragger, SoRotationDraggerContainer, TRUE, annotation, "", TRUE);
227 SO_KIT_ADD_CATALOG_ENTRY(zRotatorDragger, SoRotationDraggerContainer, TRUE, annotation, "", TRUE);
228
229 // Other
230 SO_KIT_ADD_FIELD(translation, (0.0, 0.0, 0.0));
231 SO_KIT_ADD_FIELD(translationIncrement, (1.0));
232 SO_KIT_ADD_FIELD(translationIncrementCountX, (0));
233 SO_KIT_ADD_FIELD(translationIncrementCountY, (0));
234 SO_KIT_ADD_FIELD(translationIncrementCountZ, (0));
235
236 SO_KIT_ADD_FIELD(rotation, (SbVec3f(0.0, 0.0, 1.0), 0.0));
237 SO_KIT_ADD_FIELD(rotationIncrement, (std::numbers::pi / 8.0));
238 SO_KIT_ADD_FIELD(rotationIncrementCountX, (0));
239 SO_KIT_ADD_FIELD(rotationIncrementCountY, (0));
240 SO_KIT_ADD_FIELD(rotationIncrementCountZ, (0));
241
242 SO_KIT_ADD_FIELD(draggerSize, (1.0));
243 SO_KIT_ADD_FIELD(autoScaleResult, (1.0));
244
245 SO_KIT_ADD_FIELD(xAxisLabel, ("X"));
246 SO_KIT_ADD_FIELD(yAxisLabel, ("Y"));
247 SO_KIT_ADD_FIELD(zAxisLabel, ("Z"));
248
249 SO_KIT_INIT_INSTANCE();
250
251 // Colors
252 setAxisColors(
253 SbColor(1.0, 0, 0).getPackedValue(0.0f),
254 SbColor(0, 1.0, 0).getPackedValue(0.0f),
255 SbColor(0, 0, 1.0).getPackedValue(0.0f)
256 );
257
258 // Translator
259 setupTranslationDraggers();
260
261 // Planar Translator
262 SoPlanarDragger* tPlanarDragger;
263 tPlanarDragger = SO_GET_ANY_PART(this, "xyPlanarTranslatorDragger", SoPlanarDragger);
264 tPlanarDragger->translationIncrement.connectFrom(&this->translationIncrement);
265 tPlanarDragger->autoScaleResult.connectFrom(&this->autoScaleResult);
266 translationIncrementCountX.appendConnection(&tPlanarDragger->translationIncrementXCount);
267 translationIncrementCountY.appendConnection(&tPlanarDragger->translationIncrementYCount);
268 tPlanarDragger = SO_GET_ANY_PART(this, "yzPlanarTranslatorDragger", SoPlanarDragger);
269 tPlanarDragger->translationIncrement.connectFrom(&this->translationIncrement);
270 tPlanarDragger->autoScaleResult.connectFrom(&this->autoScaleResult);
271 translationIncrementCountZ.appendConnection(&tPlanarDragger->translationIncrementXCount);
272 translationIncrementCountY.appendConnection(&tPlanarDragger->translationIncrementYCount);
273 tPlanarDragger = SO_GET_ANY_PART(this, "zxPlanarTranslatorDragger", SoPlanarDragger);
274 tPlanarDragger->translationIncrement.connectFrom(&this->translationIncrement);
275 tPlanarDragger->autoScaleResult.connectFrom(&this->autoScaleResult);
276 translationIncrementCountX.appendConnection(&tPlanarDragger->translationIncrementXCount);
277 translationIncrementCountZ.appendConnection(&tPlanarDragger->translationIncrementYCount);
278
279 // Rotator
280 setupRotationDraggers();
281
282 // Switches
283 SoSwitch* sw;
284 // Planar Translator
285 sw = SO_GET_ANY_PART(this, "xyPlanarTranslatorSwitch", SoSwitch);
286 SoInteractionKit::setSwitchValue(sw, SO_SWITCH_ALL);
287 sw = SO_GET_ANY_PART(this, "yzPlanarTranslatorSwitch", SoSwitch);
288 SoInteractionKit::setSwitchValue(sw, SO_SWITCH_ALL);
289 sw = SO_GET_ANY_PART(this, "zxPlanarTranslatorSwitch", SoSwitch);
290 SoInteractionKit::setSwitchValue(sw, SO_SWITCH_ALL);
291
292 // Rotations
293
294 SoRotation* localRotation;
295 SbRotation tempRotation;
296 auto angle = static_cast<float>(std::numbers::pi / 2.0);
297 // Planar Translator
298 localRotation = SO_GET_ANY_PART(this, "xyPlanarTranslatorRotation", SoRotation);
299 localRotation->rotation.setValue(SbRotation::identity());
300 localRotation = SO_GET_ANY_PART(this, "yzPlanarTranslatorRotation", SoRotation);
301 localRotation->rotation.setValue(SbVec3f(0.0, -1.0, 0.0), angle);
302 localRotation = SO_GET_ANY_PART(this, "zxPlanarTranslatorRotation", SoRotation);
303 localRotation->rotation.setValue(SbVec3f(1.0, 0.0, 0.0), angle);
304 // Rotator
305 SoRotationDraggerContainer* rDragger;
306 rDragger = SO_GET_ANY_PART(this, "xRotatorDragger", SoRotationDraggerContainer);
307 tempRotation = SbRotation(SbVec3f(1.0, 0.0, 0.0), angle);
308 tempRotation *= SbRotation(SbVec3f(0.0, 0.0, 1.0), angle);
309 rDragger->rotation.setValue(tempRotation);
310 rDragger = SO_GET_ANY_PART(this, "yRotatorDragger", SoRotationDraggerContainer);
311 tempRotation = SbRotation(SbVec3f(0.0, -1.0, 0.0), angle);
312 tempRotation *= SbRotation(SbVec3f(0.0, 0.0, -1.0), angle);
313 rDragger->rotation.setValue(tempRotation);
314 rDragger = SO_GET_ANY_PART(this, "zRotatorDragger", SoRotationDraggerContainer);
315 rDragger->rotation.setValue(SbRotation::identity());
316
317 // this is for non-autoscale mode. this will be disconnected for autoscale
318 // and won't be used. see setUpAutoScale.
319 auto scaleEngine = new SoComposeVec3f(); // uses coin ref scheme.
320 scaleEngine->x.connectFrom(&draggerSize);
321 scaleEngine->y.connectFrom(&draggerSize);
322 scaleEngine->z.connectFrom(&draggerSize);
323 SoScale* localScaleNode = SO_GET_ANY_PART(this, "scaleNode", SoScale);
324 localScaleNode->scaleFactor.connectFrom(&scaleEngine->vector);
325 autoScaleResult.connectFrom(&draggerSize);
326
327 SoPickStyle* localPickStyle = SO_GET_ANY_PART(this, "pickStyle", SoPickStyle);
328 localPickStyle->style = SoPickStyle::SHAPE_ON_TOP;
329
330 addValueChangedCallback(&SoTransformDragger::valueChangedCB);
331
332 translationSensor.setFunction(&SoTransformDragger::translationSensorCB);
333 translationSensor.setData(this);
334 translationSensor.setPriority(0);
335
336 rotationSensor.setFunction(&SoTransformDragger::rotationSensorCB);
337 rotationSensor.setData(this);
338 rotationSensor.setPriority(0);
339
340 cameraSensor.setFunction(&SoTransformDragger::cameraCB);
341 cameraSensor.setData(this);
342
343 idleSensor.setFunction(&SoTransformDragger::idleCB);
344 idleSensor.setData(this);
345
346 this->addFinishCallback(&SoTransformDragger::finishDragCB, this);
347
348 this->setUpConnections(TRUE, TRUE);
349 }
350
351 SoTransformDragger::~SoTransformDragger()
352 {
353 translationSensor.setData(nullptr);
354 translationSensor.detach();
355 rotationSensor.setData(nullptr);
356 rotationSensor.detach();
357 cameraSensor.setData(nullptr);
358 cameraSensor.detach();
359 idleSensor.setData(nullptr);
360 idleSensor.unschedule();
361
362 removeValueChangedCallback(&SoTransformDragger::valueChangedCB);
363 removeFinishCallback(&SoTransformDragger::finishDragCB, this);
364 }
365
366
367 SbBool SoTransformDragger::setUpConnections(SbBool onoff, SbBool doitalways)
368 {
369 if (!doitalways && (connectionsSetUp == onoff)) {
370 return onoff;
371 }
372
373 auto tDraggerX = SO_GET_ANY_PART(this, "xTranslatorDragger", SoLinearDraggerContainer);
374 auto tDraggerY = SO_GET_ANY_PART(this, "yTranslatorDragger", SoLinearDraggerContainer);
375 auto tDraggerZ = SO_GET_ANY_PART(this, "zTranslatorDragger", SoLinearDraggerContainer);
376 SoPlanarDragger* tPlanarDraggerXZ
377 = SO_GET_ANY_PART(this, "xyPlanarTranslatorDragger", SoPlanarDragger);
378 SoPlanarDragger* tPlanarDraggerYZ
379 = SO_GET_ANY_PART(this, "yzPlanarTranslatorDragger", SoPlanarDragger);
380 SoPlanarDragger* tPlanarDraggerZX
381 = SO_GET_ANY_PART(this, "zxPlanarTranslatorDragger", SoPlanarDragger);
382 auto rDraggerX = SO_GET_ANY_PART(this, "xRotatorDragger", SoRotationDraggerContainer);
383 auto rDraggerY = SO_GET_ANY_PART(this, "yRotatorDragger", SoRotationDraggerContainer);
384 auto rDraggerZ = SO_GET_ANY_PART(this, "zRotatorDragger", SoRotationDraggerContainer);
385
386 if (onoff) {
387 inherited::setUpConnections(onoff, doitalways);
388
389 registerChildDragger(tDraggerX->getDragger());
390 registerChildDragger(tDraggerY->getDragger());
391 registerChildDragger(tDraggerZ->getDragger());
392 registerChildDragger(tPlanarDraggerXZ);
393 registerChildDragger(tPlanarDraggerYZ);
394 registerChildDragger(tPlanarDraggerZX);
395 registerChildDragger(rDraggerX->getDragger());
396 registerChildDragger(rDraggerY->getDragger());
397 registerChildDragger(rDraggerZ->getDragger());
398
399 translationSensorCB(this, nullptr);
400 if (this->translationSensor.getAttachedField() != &this->translation) {
401 this->translationSensor.attach(&this->translation);
402 }
403
404 rotationSensorCB(this, nullptr);
405 if (this->rotationSensor.getAttachedField() != &this->rotation) {
406 this->rotationSensor.attach(&this->rotation);
407 }
408 }
409 else {
410 unregisterChildDragger(tDraggerX->getDragger());
411 unregisterChildDragger(tDraggerY->getDragger());
412 unregisterChildDragger(tDraggerZ->getDragger());
413 unregisterChildDragger(tPlanarDraggerXZ);
414 unregisterChildDragger(tPlanarDraggerYZ);
415 unregisterChildDragger(tPlanarDraggerZX);
416 unregisterChildDragger(rDraggerX->getDragger());
417 unregisterChildDragger(rDraggerY->getDragger());
418 unregisterChildDragger(rDraggerZ->getDragger());
419
420 inherited::setUpConnections(onoff, doitalways);
421
422 if (this->translationSensor.getAttachedField()) {
423 this->translationSensor.detach();
424 }
425
426 if (this->rotationSensor.getAttachedField()) {
427 this->rotationSensor.detach();
428 }
429 }
430 return !(this->connectionsSetUp = onoff);
431 }
432
433 void SoTransformDragger::translationSensorCB(void* f, SoSensor*)
434 {
435 auto sudoThis = static_cast<SoTransformDragger*>(f);
436 if (!f) {
437 return;
438 }
439
440 SbMatrix matrix = sudoThis->getMotionMatrix(); // clazy:exclude=rule-of-two-soft
441 sudoThis->workFieldsIntoTransform(matrix);
442 sudoThis->setMotionMatrix(matrix);
443 }
444
445 void SoTransformDragger::rotationSensorCB(void* f, SoSensor*)
446 {
447 auto sudoThis = static_cast<SoTransformDragger*>(f);
448 if (!f) {
449 return;
450 }
451
452 SbMatrix matrix = sudoThis->getMotionMatrix(); // clazy:exclude=rule-of-two-soft
453 sudoThis->workFieldsIntoTransform(matrix);
454 sudoThis->setMotionMatrix(matrix);
455 }
456
457 void SoTransformDragger::valueChangedCB(void*, SoDragger* d)
458 {
459 auto sudoThis = dynamic_cast<SoTransformDragger*>(d);
460 assert(sudoThis);
461 SbMatrix matrix = sudoThis->getMotionMatrix(); // clazy:exclude=rule-of-two-soft
462
463 // all this just to get the translation?
464 SbVec3f localTranslation, scaleDummy;
465 SbRotation localRotation, scaleOrientationDummy;
466 matrix.getTransform(localTranslation, localRotation, scaleDummy, scaleOrientationDummy);
467
468 sudoThis->translationSensor.detach();
469 if (sudoThis->translation.getValue() != localTranslation) {
470 sudoThis->translation = localTranslation;
471 }
472 sudoThis->translationSensor.attach(&sudoThis->translation);
473
474 sudoThis->rotationSensor.detach();
475 if (sudoThis->rotation.getValue() != localRotation) {
476 sudoThis->rotation = localRotation;
477 }
478 sudoThis->rotationSensor.attach(&sudoThis->rotation);
479 }
480
481 void SoTransformDragger::setUpAutoScale(SoCamera* cameraIn)
482 {
483 // note: sofieldsensor checks if the current sensor is already attached
484 // and takes appropriate action. So it is safe to attach to a field without
485 // checking current attachment state.
486 if (cameraIn->getTypeId() == SoOrthographicCamera::getClassTypeId()) {
487 auto localCamera = dynamic_cast<SoOrthographicCamera*>(cameraIn);
488 assert(localCamera);
489 cameraSensor.attach(&localCamera->height);
490 SoScale* localScaleNode = SO_GET_ANY_PART(this, "scaleNode", SoScale);
491 localScaleNode->scaleFactor.disconnect();
492 // This check shouldn't be needed but since CAM has its own
493 // ViewProvider classes that implement setEdit but doesn't inherit from
494 // ViewProviderDragger, we need to call Std_TransformManip twice.
495 // This causes setEditViewer to be called twice and Coin throws an error
496 // for trying to disconnect twice.
497 if (autoScaleResult.isConnectedFromField()) {
498 autoScaleResult.disconnect(&draggerSize);
499 }
500 cameraCB(this, nullptr);
501 }
502 else if (cameraIn->getTypeId() == SoPerspectiveCamera::getClassTypeId()) {
503 auto localCamera = dynamic_cast<SoPerspectiveCamera*>(cameraIn);
504 assert(localCamera);
505 cameraSensor.attach(&localCamera->position);
506 SoScale* localScaleNode = SO_GET_ANY_PART(this, "scaleNode", SoScale);
507 localScaleNode->scaleFactor.disconnect();
508 if (autoScaleResult.isConnectedFromField()) {
509 autoScaleResult.disconnect(&draggerSize);
510 }
511 cameraCB(this, nullptr);
512 }
513 }
514
515 void SoTransformDragger::cameraCB(void* data, SoSensor*)
516 {
517 auto sudoThis = static_cast<SoTransformDragger*>(data);
518 if (!sudoThis) {
519 return;
520 }
521 if (!sudoThis->idleSensor.isScheduled()) {
522 sudoThis->idleSensor.schedule();
523 }
524 }
525
526 void SoTransformDragger::GLRender(SoGLRenderAction* action)
527 {
528 if (!scaleInited) {
529 scaleInited = true;
530 updateDraggerCache(action->getCurPath());
531 updateAxisScale();
532 }
533
534 inherited::GLRender(action);
535 }
536
537 void SoTransformDragger::updateAxisScale()
538 {
539 SbMatrix localToWorld = getLocalToWorldMatrix();
540 SbVec3f origin;
541 localToWorld.multVecMatrix(SbVec3f(0.0, 0.0, 0.0), origin);
542 SbVec3f vx, vy, vz;
543 localToWorld.multVecMatrix(SbVec3f(1.0f, 0.0f, 0.0f), vx);
544 localToWorld.multVecMatrix(SbVec3f(0.0f, 1.0f, 0.0f), vy);
545 localToWorld.multVecMatrix(SbVec3f(0.0f, 0.0f, 1.0f), vz);
546 float x = std::max((vx - origin).length(), 1e-7f);
547 float y = std::max((vy - origin).length(), 1e-7f);
548 float z = std::max((vz - origin).length(), 1e-7f);
549 if (!axisScale.equals(SbVec3f(x, y, z), 1e-7f)) {
550 axisScale.setValue(x, y, z);
551 idleCB(this, &idleSensor);
552 }
553 }
554
555 void SoTransformDragger::handleEvent(SoHandleEventAction* action)
556 {
557 this->ref();
558
559 inherited::handleEvent(action);
560 updateAxisScale();
561
562 this->unref();
563 }
564
565 void SoTransformDragger::idleCB(void* data, SoSensor*)
566 {
567 auto sudoThis = static_cast<SoTransformDragger*>(data);
568 if (!data) {
569 return;
570 }
571 SoField* field = sudoThis->cameraSensor.getAttachedField();
572 if (field) {
573 auto camera = static_cast<SoCamera*>(field->getContainer());
574 SbMatrix localToWorld = sudoThis->getLocalToWorldMatrix();
575 SbVec3f origin;
576 localToWorld.multVecMatrix(SbVec3f(0.0, 0.0, 0.0), origin);
577
578 SbViewVolume viewVolume = camera->getViewVolume();
579 float radius = sudoThis->draggerSize.getValue() / 2.0;
580 float localScale = viewVolume.getWorldToScreenScale(origin, radius);
581 float sx, sy, sz;
582 sudoThis->axisScale.getValue(sx, sy, sz);
583 SbVec3f scaleVector(localScale / sx, localScale / sy, localScale / sz);
584 SoScale* localScaleNode = SO_GET_ANY_PART(sudoThis, "scaleNode", SoScale);
585 localScaleNode->scaleFactor.setValue(scaleVector);
586 sudoThis->autoScaleResult.setValue(localScale);
587 }
588 }
589
590 void SoTransformDragger::finishDragCB(void* data, SoDragger*)
591 {
592 auto sudoThis = static_cast<SoTransformDragger*>(data);
593 assert(sudoThis);
594
595 // note: when creating a second view of the document and then closing
596 // the first viewer it deletes the camera. However, the attached field
597 // of the cameraSensor will be detached automatically.
598 SoField* field = sudoThis->cameraSensor.getAttachedField();
599 if (field) {
600 auto camera = static_cast<SoCamera*>(field->getContainer());
601 if (camera->getTypeId() == SoPerspectiveCamera::getClassTypeId()) {
602 cameraCB(sudoThis, nullptr);
603 }
604 }
605 }
606
607 void SoTransformDragger::clearIncrementCounts()
608 {
609 translationIncrementCountX.setValue(0);
610 translationIncrementCountY.setValue(0);
611 translationIncrementCountZ.setValue(0);
612 rotationIncrementCountX.setValue(0);
613 rotationIncrementCountY.setValue(0);
614 rotationIncrementCountZ.setValue(0);
615 }
616
617 void SoTransformDragger::setAxisColors(unsigned long x, unsigned long y, unsigned long z)
618 {
619 SbColor colorX;
620 SbColor colorY;
621 SbColor colorZ;
622
623 float t = 0.0f;
624 colorX.setPackedValue(x, t);
625 colorY.setPackedValue(y, t);
626 colorZ.setPackedValue(z, t);
627
628 SoBaseColor* color;
629
630 // Translator
631 SoLinearDraggerContainer* tDragger;
632 tDragger = SO_GET_ANY_PART(this, "xTranslatorDragger", SoLinearDraggerContainer);
633 tDragger->color.setValue(colorX[0], colorX[1], colorX[2]);
634 tDragger = SO_GET_ANY_PART(this, "yTranslatorDragger", SoLinearDraggerContainer);
635 tDragger->color.setValue(colorY[0], colorY[1], colorY[2]);
636 tDragger = SO_GET_ANY_PART(this, "zTranslatorDragger", SoLinearDraggerContainer);
637 tDragger->color.setValue(colorZ[0], colorZ[1], colorZ[2]);
638 // Planar Translator
639 color = SO_GET_ANY_PART(this, "xyPlanarTranslatorColor", SoBaseColor);
640 color->rgb.setValue(colorZ[0], colorZ[1], colorZ[2]);
641 color = SO_GET_ANY_PART(this, "yzPlanarTranslatorColor", SoBaseColor);
642 color->rgb.setValue(colorX[0], colorX[1], colorX[2]);
643 color = SO_GET_ANY_PART(this, "zxPlanarTranslatorColor", SoBaseColor);
644 color->rgb.setValue(colorY[0], colorY[1], colorY[2]);
645 // Rotator
646 SoRotationDraggerContainer* rDragger;
647 rDragger = SO_GET_ANY_PART(this, "xRotatorDragger", SoRotationDraggerContainer);
648 rDragger->color.setValue(colorX[0], colorX[1], colorX[2]);
649 rDragger = SO_GET_ANY_PART(this, "yRotatorDragger", SoRotationDraggerContainer);
650 rDragger->color.setValue(colorY[0], colorY[1], colorY[2]);
651 rDragger = SO_GET_ANY_PART(this, "zRotatorDragger", SoRotationDraggerContainer);
652 rDragger->color.setValue(colorZ[0], colorZ[1], colorZ[2]);
653 }
654
655 // Visibility API Functions
656
657 // Translator
658 void SoTransformDragger::showTranslationX()
659 {
660 auto tDragger = SO_GET_ANY_PART(this, "xTranslatorDragger", SoLinearDraggerContainer);
661 tDragger->visible = true;
662 }
663 void SoTransformDragger::showTranslationY()
664 {
665 auto tDragger = SO_GET_ANY_PART(this, "yTranslatorDragger", SoLinearDraggerContainer);
666 tDragger->visible = true;
667 }
668 void SoTransformDragger::showTranslationZ()
669 {
670 auto tDragger = SO_GET_ANY_PART(this, "zTranslatorDragger", SoLinearDraggerContainer);
671 tDragger->visible = true;
672 }
673
674 void SoTransformDragger::hideTranslationX()
675 {
676 auto tDragger = SO_GET_ANY_PART(this, "xTranslatorDragger", SoLinearDraggerContainer);
677 tDragger->visible = false;
678 }
679 void SoTransformDragger::hideTranslationY()
680 {
681 auto tDragger = SO_GET_ANY_PART(this, "yTranslatorDragger", SoLinearDraggerContainer);
682 tDragger->visible = false;
683 }
684 void SoTransformDragger::hideTranslationZ()
685 {
686 auto tDragger = SO_GET_ANY_PART(this, "zTranslatorDragger", SoLinearDraggerContainer);
687 tDragger->visible = false;
688 }
689
690 bool SoTransformDragger::isShownTranslationX()
691 {
692 auto tDragger = SO_GET_ANY_PART(this, "xTranslatorDragger", SoLinearDraggerContainer);
693 return tDragger->visible.getValue();
694 }
695 bool SoTransformDragger::isShownTranslationY()
696 {
697 auto tDragger = SO_GET_ANY_PART(this, "yTranslatorDragger", SoLinearDraggerContainer);
698 return tDragger->visible.getValue();
699 }
700 bool SoTransformDragger::isShownTranslationZ()
701 {
702 auto tDragger = SO_GET_ANY_PART(this, "zTranslatorDragger", SoLinearDraggerContainer);
703 return tDragger->visible.getValue();
704 }
705
706 // Planar Translator
707 void SoTransformDragger::showPlanarTranslationXY()
708 {
709 SoSwitch* sw = SO_GET_ANY_PART(this, "xyPlanarTranslatorSwitch", SoSwitch);
710 SoInteractionKit::setSwitchValue(sw, SO_SWITCH_ALL);
711 }
712 void SoTransformDragger::showPlanarTranslationYZ()
713 {
714 SoSwitch* sw = SO_GET_ANY_PART(this, "yzPlanarTranslatorSwitch", SoSwitch);
715 SoInteractionKit::setSwitchValue(sw, SO_SWITCH_ALL);
716 }
717 void SoTransformDragger::showPlanarTranslationZX()
718 {
719 SoSwitch* sw = SO_GET_ANY_PART(this, "zxPlanarTranslatorSwitch", SoSwitch);
720 SoInteractionKit::setSwitchValue(sw, SO_SWITCH_ALL);
721 }
722
723 void SoTransformDragger::hidePlanarTranslationXY()
724 {
725 SoSwitch* sw = SO_GET_ANY_PART(this, "xyPlanarTranslatorSwitch", SoSwitch);
726 SoInteractionKit::setSwitchValue(sw, SO_SWITCH_NONE);
727 }
728 void SoTransformDragger::hidePlanarTranslationYZ()
729 {
730 SoSwitch* sw = SO_GET_ANY_PART(this, "yzPlanarTranslatorSwitch", SoSwitch);
731 SoInteractionKit::setSwitchValue(sw, SO_SWITCH_NONE);
732 }
733 void SoTransformDragger::hidePlanarTranslationZX()
734 {
735 SoSwitch* sw = SO_GET_ANY_PART(this, "zxPlanarTranslatorSwitch", SoSwitch);
736 SoInteractionKit::setSwitchValue(sw, SO_SWITCH_NONE);
737 }
738
739 bool SoTransformDragger::isShownPlanarTranslationXY()
740 {
741 SoSwitch* sw = SO_GET_ANY_PART(this, "xyPlanarTranslatorSwitch", SoSwitch);
742 return (sw->whichChild.getValue() == SO_SWITCH_ALL);
743 }
744 bool SoTransformDragger::isShownPlanarTranslationYZ()
745 {
746 SoSwitch* sw = SO_GET_ANY_PART(this, "yzPlanarTranslatorSwitch", SoSwitch);
747 return (sw->whichChild.getValue() == SO_SWITCH_ALL);
748 }
749 bool SoTransformDragger::isShownPlanarTranslationZX()
750 {
751 SoSwitch* sw = SO_GET_ANY_PART(this, "zxPlanarTranslatorSwitch", SoSwitch);
752 return (sw->whichChild.getValue() == SO_SWITCH_ALL);
753 }
754
755 bool SoTransformDragger::isHiddenPlanarTranslationXY()
756 {
757 SoSwitch* sw = SO_GET_ANY_PART(this, "xyPlanarTranslatorSwitch", SoSwitch);
758 return (sw->whichChild.getValue() == SO_SWITCH_NONE);
759 }
760 bool SoTransformDragger::isHiddenPlanarTranslationYZ()
761 {
762 SoSwitch* sw = SO_GET_ANY_PART(this, "yzPlanarTranslatorSwitch", SoSwitch);
763 return (sw->whichChild.getValue() == SO_SWITCH_NONE);
764 }
765 bool SoTransformDragger::isHiddenPlanarTranslationZX()
766 {
767 SoSwitch* sw = SO_GET_ANY_PART(this, "zxPlanarTranslatorSwitch", SoSwitch);
768 return (sw->whichChild.getValue() == SO_SWITCH_NONE);
769 }
770
771 // Rotator
772 void SoTransformDragger::showRotationX()
773 {
774 SoRotationDraggerContainer* rDragger
775 = SO_GET_ANY_PART(this, "xRotatorDragger", SoRotationDraggerContainer);
776 rDragger->visible = true;
777 }
778 void SoTransformDragger::showRotationY()
779 {
780 SoRotationDraggerContainer* rDragger
781 = SO_GET_ANY_PART(this, "yRotatorDragger", SoRotationDraggerContainer);
782 rDragger->visible = true;
783 }
784 void SoTransformDragger::showRotationZ()
785 {
786 SoRotationDraggerContainer* rDragger
787 = SO_GET_ANY_PART(this, "zRotatorDragger", SoRotationDraggerContainer);
788 rDragger->visible = true;
789 }
790
791 void SoTransformDragger::hideRotationX()
792 {
793 SoRotationDraggerContainer* rDragger
794 = SO_GET_ANY_PART(this, "xRotatorDragger", SoRotationDraggerContainer);
795 rDragger->visible = false;
796 }
797 void SoTransformDragger::hideRotationY()
798 {
799 SoRotationDraggerContainer* rDragger
800 = SO_GET_ANY_PART(this, "yRotatorDragger", SoRotationDraggerContainer);
801 rDragger->visible = false;
802 }
803 void SoTransformDragger::hideRotationZ()
804 {
805 SoRotationDraggerContainer* rDragger
806 = SO_GET_ANY_PART(this, "zRotatorDragger", SoRotationDraggerContainer);
807 rDragger->visible = false;
808 }
809
810 bool SoTransformDragger::isShownRotationX()
811 {
812 SoRotationDraggerContainer* rDragger
813 = SO_GET_ANY_PART(this, "xRotatorDragger", SoRotationDraggerContainer);
814 return rDragger->visible.getValue();
815 }
816 bool SoTransformDragger::isShownRotationY()
817 {
818 SoRotationDraggerContainer* rDragger
819 = SO_GET_ANY_PART(this, "yRotatorDragger", SoRotationDraggerContainer);
820 return rDragger->visible.getValue();
821 }
822 bool SoTransformDragger::isShownRotationZ()
823 {
824 SoRotationDraggerContainer* rDragger
825 = SO_GET_ANY_PART(this, "zRotatorDragger", SoRotationDraggerContainer);
826 return rDragger->visible.getValue();
827 }
828
829 void SoTransformDragger::setupTranslationDraggers()
830 {
831 setupTranslationDragger(
832 "xTranslatorDragger",
833 &xAxisLabel,
834 translationIncrementCountX,
835 SbVec3f(1.0, 0.0, 0.0)
836 );
837 setupTranslationDragger(
838 "yTranslatorDragger",
839 &yAxisLabel,
840 translationIncrementCountY,
841 SbVec3f(0.0, 1.0, 0.0)
842 );
843 setupTranslationDragger(
844 "zTranslatorDragger",
845 &zAxisLabel,
846 translationIncrementCountZ,
847 SbVec3f(0.0, 0.0, 1.0)
848 );
849 }
850
851 void SoTransformDragger::setupTranslationDragger(
852 const std::string& name,
853 SoSFString* label,
854 SoSFInt32& incrementCount,
855 const SbVec3f& rotDir
856 )
857 {
858 SoLinearDraggerContainer* draggerContainer
859 = SO_GET_ANY_PART(this, name.c_str(), SoLinearDraggerContainer);
860 SoLinearDragger* dragger = draggerContainer->getDragger();
861
862 dragger->translationIncrement.connectFrom(&this->translationIncrement);
863 dragger->autoScaleResult.connectFrom(&this->autoScaleResult);
864 dragger->label.connectFrom(label);
865 incrementCount.connectFrom(&dragger->translationIncrementCount);
866
867 draggerContainer->setPointerDirection(rotDir);
868 }
869
870 void SoTransformDragger::setupRotationDraggers()
871 {
872 setupRotationDragger("xRotatorDragger", rotationIncrementCountX);
873 setupRotationDragger("yRotatorDragger", rotationIncrementCountY);
874 setupRotationDragger("zRotatorDragger", rotationIncrementCountZ);
875 }
876
877 void SoTransformDragger::setupRotationDragger(const std::string& name, SoSFInt32& incrementCount)
878 {
879 SoRotationDraggerContainer* draggerContainer
880 = SO_GET_ANY_PART(this, name.c_str(), SoRotationDraggerContainer);
881 SoRotationDragger* dragger = draggerContainer->getDragger();
882
883 dragger->rotationIncrement.connectFrom(&this->rotationIncrement);
884 incrementCount.connectFrom(&dragger->rotationIncrementCount);
885 }