Debian Codegraph @

apps/demos/noodle/NoodleTextureGizmo.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 | Classes:
39 | NoodleTextureGizmo is a component for editing textures of a noodle.
40 |
41 | Author(s) : Paul Isaacs
42 |
43 */
44
45 #include <X11/StringDefs.h>
46 #include <Xm/LabelG.h>
47 #include <Xm/RowColumn.h>
48 #include <Xm/ToggleB.h>
49 #include <Xm/ToggleBG.h>
50
51 #include <Inventor/nodes/SoTexture2Transform.h>
52
53 #include "GeneralizedCylinder.h"
54 #include "NoodleTextureGizmo.h"
55 #include "NoodleSlider.h"
56
57 NoodleTextureGizmo::NoodleTextureGizmo(
58 Widget parent,
59 const char *name,
60 SbBool buildInsideParent)
61 : SoXtComponent(
62 parent,
63 name,
64 buildInsideParent)
65 {
66 // Build the widget!
67 Widget w = buildWidget( getParentWidget() );
68 setBaseWidget(w);
69
70 myNoodle = NULL;
71 }
72
73 NoodleTextureGizmo::~NoodleTextureGizmo()
74 {
75 }
76
77 Widget
78 NoodleTextureGizmo::buildWidget(
79 Widget parent )
80 {
81 Arg resources[20];
82 int n = 0;
83 SbVec2s sliderSize;
84
85 _rowCol = XtCreateManagedWidget(getWidgetName(),xmRowColumnWidgetClass,
86 parent,NULL, 0);
87
88 #define STRING(a) XmStringCreate(a,XmSTRING_DEFAULT_CHARSET)
89
90 // Texture on/off Toggle Button
91 XtSetArg(resources[n], XmNlabelString, STRING("TextureOnOff")); ++n;
92 textureToggle = XmCreateToggleButtonGadget(_rowCol, "textureOnOff",
93 resources, n); n = 0;
94 XtAddCallback(textureToggle, XmNvalueChangedCallback,
95 NoodleTextureGizmo::textureToggleCallback, (XtPointer)this);
96 XtManageChild(textureToggle);
97
98 // Texture repeat in X-direction.
99 // Label...
100 XtSetArg(resources[n], XmNlabelString, STRING("X repeat:")); ++n;
101 Widget xRepeatLabel = XmCreateLabelGadget(_rowCol, "X repeat",
102 resources, n); n = 0;
103 XtManageChild(xRepeatLabel);
104 // Slider...
105 xRepeatSlider = new NoodleSlider(_rowCol);
106 xRepeatSlider->addValueChangedCallback( NoodleTextureGizmo::xRepeatCallback, this );
107 sliderSize = xRepeatSlider->getSize();
108 xRepeatSlider->setSize( SbVec2s( 200, sliderSize[1] ) );
109
110 xRepeatSlider->setValue(1.0);
111 xRepeatSlider->setMin(0.0);
112 xRepeatSlider->setMax(30.0);
113 xRepeatSlider->show();
114
115 // Texture repeat in Y-direction.
116 // Label...
117 XtSetArg(resources[n], XmNlabelString, STRING("Y repeat:")); ++n;
118 Widget yRepeatLabel = XmCreateLabelGadget(_rowCol, "Y repeat",
119 resources, n); n = 0;
120 XtManageChild(yRepeatLabel);
121 // Slider...
122 yRepeatSlider = new NoodleSlider(_rowCol);
123 yRepeatSlider->addValueChangedCallback( NoodleTextureGizmo::yRepeatCallback, this );
124 sliderSize = yRepeatSlider->getSize();
125 yRepeatSlider->setSize( SbVec2s( 200, sliderSize[1] ) );
126
127 yRepeatSlider->setValue(1.0);
128 yRepeatSlider->setMin(0.0);
129 yRepeatSlider->setMax(30.0);
130 yRepeatSlider->show();
131
132
133 // Texture translate in X-direction.
134 // Label...
135 XtSetArg(resources[n], XmNlabelString, STRING("X transl:")); ++n;
136 Widget xTranslLabel = XmCreateLabelGadget(_rowCol, "X transl",
137 resources, n); n = 0;
138 XtManageChild(xTranslLabel);
139 // Slider...
140 xTranslSlider = new NoodleSlider(_rowCol);
141 xTranslSlider->addValueChangedCallback( NoodleTextureGizmo::xTranslCallback, this );
142 sliderSize = xTranslSlider->getSize();
143 xTranslSlider->setSize( SbVec2s( 200, sliderSize[1] ) );
144
145 xTranslSlider->setValue(0.0);
146 xTranslSlider->setMin(-1.0);
147 xTranslSlider->setMax(1.0);
148 xTranslSlider->show();
149
150 // Texture translate in Y-direction.
151 // Label...
152 XtSetArg(resources[n], XmNlabelString, STRING("Y transl:")); ++n;
153 Widget yTranslLabel = XmCreateLabelGadget(_rowCol, "Y transl",
154 resources, n); n = 0;
155 XtManageChild(yTranslLabel);
156 // Slider...
157 yTranslSlider = new NoodleSlider(_rowCol);
158 yTranslSlider->addValueChangedCallback( NoodleTextureGizmo::yTranslCallback, this );
159 sliderSize = yTranslSlider->getSize();
160 yTranslSlider->setSize( SbVec2s( 200, sliderSize[1] ) );
161
162 yTranslSlider->setValue(0.0);
163 yTranslSlider->setMin(-1.0);
164 yTranslSlider->setMax(1.0);
165 yTranslSlider->show();
166
167
168 // Texture rotation angle.
169 // Label...
170 XtSetArg(resources[n], XmNlabelString, STRING("Rotation:")); ++n;
171 Widget texRotationLabel = XmCreateLabelGadget(_rowCol, "Rotation",
172 resources, n); n = 0;
173 XtManageChild(texRotationLabel);
174 // Slider...
175 texRotationSlider = new NoodleSlider(_rowCol);
176 texRotationSlider->addValueChangedCallback(NoodleTextureGizmo::texRotationCallback, this);
177 sliderSize = texRotationSlider->getSize();
178 texRotationSlider->setSize( SbVec2s( 200, sliderSize[1] ) );
179
180 texRotationSlider->setValue(0.0);
181 texRotationSlider->setMin(0.0);
182 texRotationSlider->setMax(6.28);
183 texRotationSlider->show();
184
185 return _rowCol;
186 }
187
188 void
189 NoodleTextureGizmo::setNoodle( GeneralizedCylinder *newNoodle )
190 {
191 myNoodle = newNoodle;
192
193 // Set the values in the widgets to reflect values in the noodle.
194
195 SbBool bv = (newNoodle) ? newNoodle->withTextureCoords.getValue() : FALSE;
196 XmToggleButtonSetState(textureToggle, bv, FALSE);
197
198 if (newNoodle) {
199 SoTexture2Transform *txXf
200 = SO_GET_PART(newNoodle, "texture2Transform", SoTexture2Transform );
201 xTranslSlider->setValue( txXf->translation.getValue()[0]);
202 yTranslSlider->setValue( txXf->translation.getValue()[1]);
203 xRepeatSlider->setValue( txXf->scaleFactor.getValue()[0] );
204 yRepeatSlider->setValue( txXf->scaleFactor.getValue()[1] );
205 texRotationSlider->setValue( txXf->rotation.getValue() );
206 }
207 else {
208 xTranslSlider->setValue( 0 );
209 yTranslSlider->setValue( 0 );
210 xRepeatSlider->setValue( 1 );
211 yRepeatSlider->setValue( 1 );
212 texRotationSlider->setValue( 0 );
213 }
214 }
215
216 //
217 // Callback for the texture on/off toggle buttons
218 //
219 void
220 NoodleTextureGizmo::textureToggleCallback(Widget w, XtPointer data, XtPointer)
221 {
222 SbBool test = XmToggleButtonGetState( w );
223 NoodleTextureGizmo *myself = (NoodleTextureGizmo *) data;
224 if ( myself->myNoodle != NULL )
225 myself->myNoodle->withTextureCoords = test;
226 }
227
228 //
229 // Texture parameter callbacks
230 //
231 void
232 NoodleTextureGizmo::xTranslCallback(void *userData, void *)
233 {
234 NoodleTextureGizmo *myself = (NoodleTextureGizmo *) userData;
235 float newVal = myself->xTranslSlider->getValue();
236 if ( myself->myNoodle != NULL ) {
237 SoTexture2Transform *txXf
238 = SO_GET_PART( myself->myNoodle, "texture2Transform", SoTexture2Transform );
239 SbVec2f old = txXf->translation.getValue();
240 old[0] = newVal;
241 txXf->translation = old;
242 }
243 }
244 void
245 NoodleTextureGizmo::yTranslCallback(void *userData, void *)
246 {
247 NoodleTextureGizmo *myself = (NoodleTextureGizmo *) userData;
248 float newVal = myself->yTranslSlider->getValue();
249 if ( myself->myNoodle != NULL ) {
250 SoTexture2Transform *txXf
251 = SO_GET_PART( myself->myNoodle, "texture2Transform", SoTexture2Transform );
252 SbVec2f old = txXf->translation.getValue();
253 old[1] = newVal;
254 txXf->translation = old;
255 }
256 }
257 void
258 NoodleTextureGizmo::xRepeatCallback(void *userData, void *)
259 {
260 NoodleTextureGizmo *myself = (NoodleTextureGizmo *) userData;
261 float newVal = myself->xRepeatSlider->getValue();
262 if ( myself->myNoodle != NULL ) {
263 SoTexture2Transform *txXf
264 = SO_GET_PART( myself->myNoodle, "texture2Transform", SoTexture2Transform );
265 SbVec2f old = txXf->scaleFactor.getValue();
266 old[0] = newVal;
267 txXf->scaleFactor = old;
268 }
269 }
270 void
271 NoodleTextureGizmo::yRepeatCallback(void *userData, void *)
272 {
273 NoodleTextureGizmo *myself = (NoodleTextureGizmo *) userData;
274 float newVal = myself->yRepeatSlider->getValue();
275 if ( myself->myNoodle != NULL ) {
276 SoTexture2Transform *txXf
277 = SO_GET_PART( myself->myNoodle, "texture2Transform", SoTexture2Transform );
278 SbVec2f old = txXf->scaleFactor.getValue();
279 old[1] = newVal;
280 txXf->scaleFactor = old;
281 }
282 }
283 void
284 NoodleTextureGizmo::texRotationCallback(void *userData, void *)
285 {
286 NoodleTextureGizmo *myself = (NoodleTextureGizmo *) userData;
287 float newVal = myself->texRotationSlider->getValue();
288 if ( myself->myNoodle != NULL ) {
289 SoTexture2Transform *txXf
290 = SO_GET_PART( myself->myNoodle, "texture2Transform", SoTexture2Transform );
291 txXf->rotation = newVal;
292 }
293 }