Debian Codegraph @

apps/examples/Toolmaker/08.Manips/TranslateRadialDragger.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*--------------------------------------------------------------
38 * This is an example from the Inventor Toolmaker,
39 * chapter 8, examples 3 through 12.
40 *
41 * Source file for "TranslateRadialDragger"
42 *------------------------------------------------------------*/
43
44 #include <Inventor/SoPath.h>
45 #include <Inventor/sensors/SoFieldSensor.h>
46 #include <Inventor/projectors/SbLineProjector.h>
47 #include <Inventor/nodes/SoRotation.h>
48 #include <Inventor/nodes/SoSeparator.h>
49 #include <Inventor/nodes/SoSwitch.h>
50
51 // Include file for our new class.
52 #include "TranslateRadialDragger.h"
53
54 // This file contains the variable
55 // TranslateRadialDragger::geomBuffer, which describes
56 // the default geometry resources for this dragger.
57 #include "TranslateRadialDraggerGeom.h"
58
59 SO_KIT_SOURCE(TranslateRadialDragger);
60
61 // Initializes the type ID for this dragger node. This
62 // should be called once after SoInteraction::init().
63 void
64 TranslateRadialDragger::initClass()
65 {
66 SO_KIT_INIT_CLASS(TranslateRadialDragger, SoDragger, "Dragger");
67 }
68
69 TranslateRadialDragger::TranslateRadialDragger()
70 {
71 SO_KIT_CONSTRUCTOR(TranslateRadialDragger);
72
73 // Put this under geomSeparator so it draws efficiently.
74 SO_KIT_ADD_CATALOG_ENTRY(translatorSwitch, SoSwitch, TRUE,
75 geomSeparator, ,FALSE);
76 SO_KIT_ADD_CATALOG_ENTRY(translator, SoSeparator, TRUE,
77 translatorSwitch, ,TRUE);
78 SO_KIT_ADD_CATALOG_ENTRY(translatorActive,SoSeparator,TRUE,
79 translatorSwitch, ,TRUE);
80 SO_KIT_ADD_CATALOG_ENTRY(feedbackRotate, SoRotation, TRUE,
81 geomSeparator, ,TRUE);
82 SO_KIT_ADD_CATALOG_ENTRY(feedbackSwitch, SoSwitch, TRUE,
83 geomSeparator, ,FALSE);
84 SO_KIT_ADD_CATALOG_ENTRY(feedback, SoSeparator, TRUE,
85 feedbackSwitch, ,TRUE);
86 SO_KIT_ADD_CATALOG_ENTRY(feedbackActive, SoSeparator, TRUE,
87 feedbackSwitch, ,TRUE);
88
89 // Read geometry resources. Only do this the first time we
90 // construct one. 'geomBuffer' contains our compiled in
91 // defaults. The user can override these by specifying new
92 // scene graphs in the file:
93 // $(SO_DRAGGER_DIR)/translateRadialDragger.iv
94 if (SO_KIT_IS_FIRST_INSTANCE())
95 readDefaultParts("translateRadialDragger.iv", geomBuffer,
96 sizeof(geomBuffer));
97
98 // Field that always shows current position of the dragger.
99 SO_KIT_ADD_FIELD(translation, (0.0, 0.0, 0.0));
100
101 // Creates the parts list for this nodekit
102 SO_KIT_INIT_INSTANCE();
103
104 // Create the parts of the dragger. This dragger has five
105 // parts that we need to create: "translator",
106 // "translatorActive", "feedback," and "feedbackActive" will
107 // be created using the resource mechanism. They are looked
108 // up in the global dictionary.
109 // "rotator," used to position the feedback so it points in
110 // the direction selected by the user, will just be a plain
111 // old SoRotation node.
112 // We call 'setPartAsDefault' because we are installing
113 // default geometries from the resource files. By calling
114 // 'setPartAsDefault' instead of 'setPart', we insure that
115 // these parts will not write to file unless they are
116 // changed later.
117 setPartAsDefault("translator",
118 "translateRadialTranslator");
119 setPartAsDefault("translatorActive",
120 "translateRadialTranslatorActive");
121 setPartAsDefault("feedback",
122 "translateRadialFeedback");
123 setPartAsDefault("feedbackActive",
124 "translateRadialFeedbackActive");
125
126 // Set the switch parts to 0 to display the inactive parts.
127 // The parts "translatorSwitch" and "feedbackSwitch"
128 // are not public parts. (i.e., when making the catalog, the
129 // isPublic flag was set FALSE, so users cannot access them)
130 // To retrieve the parts we must use the SO_GET_ANY_PART
131 // macro which calls the protected method getAnyPart().
132 SoSwitch *sw;
133 sw = SO_GET_ANY_PART(this, "translatorSwitch", SoSwitch);
134 setSwitchValue(sw, 0);
135 sw = SO_GET_ANY_PART(this, "feedbackSwitch", SoSwitch);
136 setSwitchValue(sw, 0);
137
138 // This dragger does motion along a line,
139 // so we create a line projector.
140 lineProj = new SbLineProjector();
141
142 // Add the callback functions that will be called when
143 // the user clicks, drags, and releases.
144 addStartCallback(&TranslateRadialDragger::startCB);
145 addMotionCallback(&TranslateRadialDragger::motionCB);
146 addFinishCallback(&TranslateRadialDragger::finishCB);
147
148 // Updates the translation field when the dragger moves.
149 addValueChangedCallback(
150 &TranslateRadialDragger::valueChangedCB);
151
152 // Updates the motionMatrix (and thus moves the dragger
153 // through space) to a new location whenever the translation
154 // field is changed from the outside.
155 fieldSensor = new SoFieldSensor(
156 &TranslateRadialDragger::fieldSensorCB, this);
157 fieldSensor->setPriority(0);
158
159 setUpConnections( TRUE, TRUE );
160 }
161
162 TranslateRadialDragger::~TranslateRadialDragger()
163 {
164 // Delete what we created in the constructor.
165 delete lineProj;
166 if (fieldSensor)
167 delete fieldSensor;
168 }
169
170 SbBool
171 TranslateRadialDragger::setUpConnections( SbBool onOff,
172 SbBool doItAlways)
173 {
174 if ( !doItAlways && connectionsSetUp == onOff)
175 return onOff;
176
177 if (onOff) {
178 // We connect AFTER base class.
179 SoDragger::setUpConnections(onOff, doItAlways);
180
181 // Call the sensor CB to make things up-to-date.
182 fieldSensorCB(this, NULL);
183
184 // Connect the field sensor
185 if (fieldSensor->getAttachedField() != &translation)
186 fieldSensor->attach( &translation );
187 }
188 else {
189 // We disconnect BEFORE base class.
190
191 // Disconnect the field sensor.
192 if (fieldSensor->getAttachedField())
193 fieldSensor->detach();
194
195 SoDragger::setUpConnections(onOff, doItAlways);
196 }
197
198 return !(connectionsSetUp = onOff);
199 }
200
201 // Static callback functions called by SoDragger when when the
202 // mouse goes down (over this dragger), drags, and releases.
203 void
204 TranslateRadialDragger::startCB(void *, SoDragger *dragger)
205 {
206 TranslateRadialDragger *myself
207 = (TranslateRadialDragger *) dragger;
208 myself->dragStart();
209 }
210 void
211 TranslateRadialDragger::motionCB(void *, SoDragger *dragger)
212 {
213 TranslateRadialDragger *myself
214 = (TranslateRadialDragger *) dragger;
215 myself->drag();
216 }
217 void
218 TranslateRadialDragger::finishCB(void *, SoDragger *dragger)
219 {
220 TranslateRadialDragger *myself
221 = (TranslateRadialDragger *) dragger;
222 myself->dragFinish();
223 }
224
225 // Called when user clicks down on this dragger. Sets up the
226 // projector and switches parts to their "active" versions.
227 void
228 TranslateRadialDragger::dragStart()
229 {
230 // Display the 'active' parts...
231 SoSwitch *sw;
232 sw = SO_GET_ANY_PART(this, "translatorSwitch", SoSwitch);
233 setSwitchValue(sw, 1);
234 sw = SO_GET_ANY_PART(this, "feedbackSwitch", SoSwitch);
235 setSwitchValue(sw, 1);
236
237 // Establish the projector line.
238 // The direction of translation goes from the center of the
239 // dragger toward the point that was hit, in local space.
240 // For the center, use (0,0,0).
241 SbVec3f startLocalHitPt = getLocalStartingPoint();
242 lineProj->setLine(SbLine(SbVec3f(0,0,0), startLocalHitPt));
243
244 // orient the feedback geometry.
245 orientFeedbackGeometry(startLocalHitPt);
246 }
247
248 // Sets the feedbackRotation node so that the feedback
249 // geometry will be aligned with the direction of motion in
250 // local space.
251 void
252 TranslateRadialDragger::orientFeedbackGeometry(
253 const SbVec3f &localDir)
254 {
255 // By default, feedback geometry aligns with the x axis.
256 // Rotate so that it points in the given direction.
257 SbRotation rotXToDir = SbRotation(SbVec3f(1,0,0), localDir);
258
259 // Give this rotation to the "feedbackRotate" part.
260 SoRotation *myPart = SO_GET_ANY_PART(this, "feedbackRotate",
261 SoRotation);
262 myPart->rotation.setValue(rotXToDir);
263 }
264
265
266 // Called when the mouse translates during dragging. Moves
267 // the dragger based on the mouse motion.
268 void
269 TranslateRadialDragger::drag()
270 {
271 // Things can change between renderings. To be safe, update
272 // the projector with the current values.
273 lineProj->setViewVolume(getViewVolume());
274 lineProj->setWorkingSpace(getLocalToWorldMatrix());
275
276 // Find the new intersection on the projector.
277 SbVec3f newHitPt
278 = lineProj->project(getNormalizedLocaterPosition());
279
280 // Get initial point expressed in our current local space.
281 SbVec3f startHitPt = getLocalStartingPoint();
282
283 // Motion in local space is difference between old and
284 // new positions.
285 SbVec3f motion = newHitPt - startHitPt;
286
287 // Append this to the startMotionMatrix, which was saved
288 // automatically at the beginning of the drag, to find
289 // the current motion matrix.
290 setMotionMatrix(
291 appendTranslation(getStartMotionMatrix(), motion));
292 }
293
294 // Called when mouse button is released and drag is completed.
295 void
296 TranslateRadialDragger::dragFinish()
297 {
298 // Display inactive versions of parts...
299 SoSwitch *sw;
300 sw = SO_GET_ANY_PART(this, "translatorSwitch", SoSwitch);
301 setSwitchValue(sw, 0);
302 sw = SO_GET_ANY_PART(this, "feedbackSwitch", SoSwitch);
303 setSwitchValue(sw, 0);
304
305 // Get rid of the "feedbackRotate" part. We don't need
306 // it since we aren't showing the feedback any more.
307 setAnyPart("feedbackRotate", NULL);
308 }
309
310 // Called when the motionMatrix changes. Sets the 'translation'
311 // field based on the new motionMatrix.
312 void
313 TranslateRadialDragger::valueChangedCB(void *,
314 SoDragger *inDragger)
315 {
316 TranslateRadialDragger *myself
317 = (TranslateRadialDragger *) inDragger;
318
319 // Get translation by decomposing motionMatrix.
320 SbMatrix motMat = myself->getMotionMatrix();
321 SbVec3f trans, scale;
322 SbRotation rot, scaleOrient;
323 motMat.getTransform(trans, rot, scale, scaleOrient);
324
325 // Set "translation", disconnecting sensor while doing so.
326 myself->fieldSensor->detach();
327 if (myself->translation.getValue() != trans)
328 myself->translation = trans;
329 myself->fieldSensor->attach(&(myself->translation));
330 }
331
332 // If the "translation" field is set from outside, update
333 // motionMatrix accordingly.
334 void
335 TranslateRadialDragger::fieldSensorCB(void *inDragger,
336 SoSensor *)
337 {
338 TranslateRadialDragger *myself
339 = (TranslateRadialDragger *) inDragger;
340
341 SbMatrix motMat = myself->getMotionMatrix();
342 myself->workFieldsIntoTransform(motMat);
343
344 myself->setMotionMatrix(motMat);
345 }