Debian Codegraph @

lib/interaction/src/draggers/SoTranslate1Dragger.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 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.1.1.1 $
44 |
45 | Classes:
46 | SoTranslate1Dragger
47 |
48 | Author(s): Paul Isaacs
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54
55 #include <cstdio>
56 #include <Inventor/SbLinear.h>
57 #include <Inventor/SoDB.h>
58 #include <Inventor/sensors/SoFieldSensor.h>
59 #include <Inventor/SoInput.h>
60 #include <Inventor/SoPath.h>
61 #include <Inventor/projectors/SbLineProjector.h>
62 #include <Inventor/draggers/SoTranslate1Dragger.h>
63 #include <Inventor/nodes/SoSeparator.h>
64 #include <Inventor/nodes/SoSwitch.h>
65
66 #include "geom/SoTranslate1DraggerGeom.h"
67
68
69 SO_KIT_SOURCE(SoTranslate1Dragger);
70
71 ////////////////////////////////////////////////////////////////////////
72 //
73 // Description:
74 // Constructor
75 //
76 SoTranslate1Dragger::SoTranslate1Dragger()
77 //
78 ////////////////////////////////////////////////////////////////////////
79 {
80 SO_KIT_CONSTRUCTOR(SoTranslate1Dragger);
81
82 isBuiltIn = TRUE;
83
84 // Put this stuff under the geomSeparator so it will draw more
85 // efficiently.
86 SO_KIT_ADD_CATALOG_ENTRY(translatorSwitch, SoSwitch, TRUE,
87 geomSeparator, ,FALSE);
88 SO_KIT_ADD_CATALOG_ENTRY(translator, SoSeparator, TRUE,
89 translatorSwitch, ,TRUE);
90 SO_KIT_ADD_CATALOG_ENTRY(translatorActive, SoSeparator, TRUE,
91 translatorSwitch, ,TRUE);
92 SO_KIT_ADD_CATALOG_ENTRY(feedbackSwitch, SoSwitch, TRUE,
93 geomSeparator, ,FALSE);
94 SO_KIT_ADD_CATALOG_ENTRY(feedback, SoSeparator, TRUE,
95 feedbackSwitch, ,TRUE);
96 SO_KIT_ADD_CATALOG_ENTRY(feedbackActive, SoSeparator, TRUE,
97 feedbackSwitch, ,TRUE);
98
99 // read geometry for shared parts
100 if (SO_KIT_IS_FIRST_INSTANCE())
101 readDefaultParts("translate1Dragger.iv", geomBuffer, sizeof(geomBuffer) );
102
103 SO_KIT_ADD_FIELD(translation, (0.0, 0.0, 0.0));
104
105 SO_KIT_INIT_INSTANCE();
106
107 // create the parts...
108 setPartAsDefault("translator", "translate1Translator");
109 setPartAsDefault("translatorActive","translate1TranslatorActive");
110 setPartAsDefault("feedback", "translate1Feedback");
111 setPartAsDefault("feedbackActive", "translate1FeedbackActive");
112
113 // Set the switches to 0...
114 setSwitchValue( translatorSwitch.getValue(), 0 );
115 setSwitchValue( feedbackSwitch.getValue(), 0 );
116
117 // Create the line projector
118 lineProj = new SbLineProjector();
119
120 // add the callbacks to perform the dragging
121 addStartCallback( &SoTranslate1Dragger::startCB );
122 addMotionCallback( &SoTranslate1Dragger::motionCB );
123 addFinishCallback( &SoTranslate1Dragger::finishCB );
124
125 // Updates the translation field when the motionMatrix is set.
126 addValueChangedCallback( &SoTranslate1Dragger::valueChangedCB );
127
128 // Updates the motionMatrix when the translation field is set.
129 fieldSensor = new SoFieldSensor( &SoTranslate1Dragger::fieldSensorCB, this);
130 fieldSensor->setPriority( 0 );
131
132 setUpConnections( TRUE, TRUE );
133 }
134
135 ////////////////////////////////////////////////////////////////////////
136 //
137 // Description:
138 // Destructor
139 //
140 SoTranslate1Dragger::~SoTranslate1Dragger()
141 //
142 ////////////////////////////////////////////////////////////////////////
143 {
144 delete lineProj;
145 if (fieldSensor)
146 delete fieldSensor;
147 }
148
149 // detach/attach any sensors, callbacks, and/or field connections.
150 // Called by: start/end of SoBaseKit::readInstance
151 // and on new copy by: start/end of SoBaseKit::copy.
152 // Classes that redefine must call setUpConnections(TRUE,TRUE)
153 // at end of constructor.
154 // Returns the state of the node when this was called.
155 SbBool
156 SoTranslate1Dragger::setUpConnections( SbBool onOff, SbBool doItAlways )
157 {
158 if ( !doItAlways && connectionsSetUp == onOff)
159 return onOff;
160
161 if ( onOff ) {
162
163 // We connect AFTER base class.
164 SoDragger::setUpConnections( onOff, FALSE );
165
166 // Call the sensor CBs to make things are up-to-date.
167 fieldSensorCB( this, NULL );
168
169 // Connect the field sensors
170 if (fieldSensor->getAttachedField() != &translation)
171 fieldSensor->attach( &translation );
172 }
173 else {
174
175 // We disconnect BEFORE base class.
176
177 // Disconnect the field sensors.
178 if (fieldSensor->getAttachedField())
179 fieldSensor->detach();
180
181 SoDragger::setUpConnections( onOff, FALSE );
182 }
183
184 return !(connectionsSetUp = onOff);
185 }
186
187 ////////////////////////////////////////////////////////////////////////
188 //
189 // Description:
190 // Set up the highlighting, projector, and the initial hit on
191 // the dragger
192 //
193 // Use: private
194 //
195 void
196 SoTranslate1Dragger::dragStart()
197 //
198 ////////////////////////////////////////////////////////////////////////
199 {
200 // Set the switches to 1...
201 setSwitchValue( translatorSwitch.getValue(), 1 );
202 setSwitchValue( feedbackSwitch.getValue(), 1 );
203
204 // Establish the projector line in working space.
205 // Working space is space at end of motion matrix.
206 // translate direction is defined as (1,0,0) in local space.
207 SbVec3f startLocalHitPt = getLocalStartingPoint();
208 lineProj->setLine(SbLine(startLocalHitPt,
209 startLocalHitPt + SbVec3f(1,0,0)));
210 }
211
212 ////////////////////////////////////////////////////////////////////////
213 //
214 // Description:
215 // Stretch the dragger according to the motion along the line
216 // projector
217 //
218 // Use: private
219 //
220 void
221 SoTranslate1Dragger::drag()
222 //
223 ////////////////////////////////////////////////////////////////////////
224 {
225 // Set up the projector space and view.
226 // Working space is space at end of motion matrix.
227 lineProj->setViewVolume( getViewVolume() );
228 lineProj->setWorkingSpace( getLocalToWorldMatrix() );
229
230 // Get newHitPt and startHitPt in workspace.
231 SbVec3f newHitPt = lineProj->project( getNormalizedLocaterPosition());
232 SbVec3f startHitPt = getLocalStartingPoint();
233
234 // Figure out the translation relative to start position.
235 SbVec3f motion = newHitPt - startHitPt;
236
237 // Append this to the startMotionMatrix, which we saved at the beginning
238 // of the drag, to find the current motion matrix.
239 setMotionMatrix( appendTranslation( getStartMotionMatrix(), motion ) );
240 }
241
242 ////////////////////////////////////////////////////////////////////////
243 //
244 // Description:
245 //
246 // Use: private
247 //
248 void
249 SoTranslate1Dragger::dragFinish()
250 //
251 ////////////////////////////////////////////////////////////////////////
252 {
253 // Set the switches to 0...
254 setSwitchValue( translatorSwitch.getValue(), 0 );
255 setSwitchValue( feedbackSwitch.getValue(), 0 );
256 }
257
258 ////////////////////////////////////////////////////////////////////
259 // Stubs for callbacks
260 ////////////////////////////////////////////////////////////////////
261 void
262 SoTranslate1Dragger::startCB( void *, SoDragger *inDragger )
263 {
264 SoTranslate1Dragger *m = (SoTranslate1Dragger *) inDragger;
265 m->dragStart();
266 }
267
268 void
269 SoTranslate1Dragger::motionCB( void *, SoDragger *inDragger )
270 {
271 SoTranslate1Dragger *m = (SoTranslate1Dragger *) inDragger;
272 m->drag();
273 }
274
275 void
276 SoTranslate1Dragger::finishCB( void *, SoDragger *inDragger )
277 {
278 SoTranslate1Dragger *m = (SoTranslate1Dragger *) inDragger;
279 m->dragFinish();
280 }
281
282 void
283 SoTranslate1Dragger::valueChangedCB( void *, SoDragger *inDragger )
284 {
285 SoTranslate1Dragger *m = (SoTranslate1Dragger *) inDragger;
286 SbMatrix motMat = m->getMotionMatrix();
287
288 SbVec3f trans, scale;
289 SbRotation rot, scaleOrient;
290 getTransformFast( motMat, trans, rot, scale, scaleOrient );
291
292 // Disconnect the field sensor
293 m->fieldSensor->detach();
294
295 if ( m->translation.getValue() != trans )
296 m->translation = trans;
297
298 // Reconnect the field sensor
299 m->fieldSensor->attach( &(m->translation) );
300 }
301
302 void
303 SoTranslate1Dragger::fieldSensorCB( void *inDragger, SoSensor *)
304 {
305 SoTranslate1Dragger *dragger = (SoTranslate1Dragger *) inDragger;
306
307 // Incorporate the new field value into the matrix...
308 SbMatrix motMat = dragger->getMotionMatrix();
309 dragger->workFieldsIntoTransform( motMat );
310
311 dragger->setMotionMatrix( motMat );
312 }