Debian Codegraph @

apps/demos/noodle/NoodleSurfaceGizmo.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 | Description:
39 | A component class that creates a gizmo for editing
40 | orientation of normals, crease angle, and the
41 | minimum number of rows/columns in a noodle.
42 |
43 | Author(s) : Paul Isaacs
44 |
45 */
46
47 #include <X11/StringDefs.h>
48 #include <Xm/LabelG.h>
49 #include <Xm/RowColumn.h>
50 #include <Xm/PushB.h>
51 #include <Xm/PushBG.h>
52 #include <Xm/Text.h>
53
54 #include <Inventor/nodes/SoShapeHints.h>
55
56 #include "GeneralizedCylinder.h"
57 #include "NoodleSurfaceGizmo.h"
58 #include "NoodleSlider.h"
59
60 NoodleSurfaceGizmo::NoodleSurfaceGizmo(
61 Widget parent,
62 const char *name,
63 SbBool buildInsideParent)
64 : SoXtComponent(
65 parent,
66 name,
67 buildInsideParent)
68 {
69 // Build the widget!
70 Widget w = buildWidget( getParentWidget() );
71 setBaseWidget(w);
72
73 myNoodle = NULL;
74 }
75
76 NoodleSurfaceGizmo::~NoodleSurfaceGizmo()
77 {
78 }
79
80 Widget
81 NoodleSurfaceGizmo::buildWidget(
82 Widget parent )
83 {
84 Arg resources[20];
85 int n = 0;
86 SbVec2s sliderSize;
87
88 _rowCol = XtCreateManagedWidget(getWidgetName(),xmRowColumnWidgetClass,
89 parent,NULL, 0);
90
91 #define STRING(a) XmStringCreate(a,XmSTRING_DEFAULT_CHARSET)
92
93 // Flip Normals button
94 XtSetArg(resources[n], XmNlabelString, STRING("Flip Normals")); ++n;
95 flipNormButton = XmCreatePushButtonGadget(_rowCol, "flip",
96 resources, n); n = 0;
97 XtAddCallback(flipNormButton, XmNactivateCallback,
98 NoodleSurfaceGizmo::flipNormCallback, (XtPointer)this);
99 XtManageChild(flipNormButton);
100
101 // Crease Angle.
102 // Label...
103 XtSetArg(resources[n], XmNlabelString, STRING("Crease Angle:")); ++n;
104 Widget creaseLabel = XmCreateLabelGadget(_rowCol, "Crease Angle",
105 resources, n); n = 0;
106 XtManageChild(creaseLabel);
107 // Slider...
108 creaseSlider = new NoodleSlider(_rowCol);
109 creaseSlider->addValueChangedCallback(
110 NoodleSurfaceGizmo::creaseCallback, this );
111 sliderSize = creaseSlider->getSize();
112 creaseSlider->setSize( SbVec2s( 200, sliderSize[1] ) );
113 creaseSlider->setMin(0.0);
114 creaseSlider->setMax(90.0);
115 creaseSlider->show();
116
117 // Min Num rows label
118 XtSetArg(resources[n], XmNlabelString, STRING("Min num rows:")); ++n;
119 Widget numRowsLabel = XmCreateLabelGadget(_rowCol, "Min num rows",
120 resources, n); n = 0;
121 XtManageChild(numRowsLabel);
122 // Min Num rows input
123 XtSetArg(resources[n], XmNcolumns, 3); ++n;
124 XtSetArg(resources[n], XmNeditMode, XmSINGLE_LINE_EDIT); ++n;
125 minNumRowsEdit = XmCreateText(_rowCol, "minNumRowsEdit",
126 resources, n); n = 0;
127 XtAddCallback(minNumRowsEdit, XmNactivateCallback,
128 NoodleSurfaceGizmo::minNumRowsCallback, (XtPointer)this);
129 XtManageChild(minNumRowsEdit);
130
131 // Min Num cols label
132 XtSetArg(resources[n], XmNlabelString, STRING("Min num cols:")); ++n;
133 Widget numColsLabel = XmCreateLabelGadget(_rowCol, "Min num cols",
134 resources, n); n = 0;
135 XtManageChild(numColsLabel);
136 // Min Num cols input
137 XtSetArg(resources[n], XmNcolumns, 3); ++n;
138 XtSetArg(resources[n], XmNeditMode, XmSINGLE_LINE_EDIT); ++n;
139 minNumColsEdit = XmCreateText(_rowCol, "minNumColsEdit",
140 resources, n); n = 0;
141 XtAddCallback(minNumColsEdit, XmNactivateCallback,
142 NoodleSurfaceGizmo::minNumColsCallback, (XtPointer)this);
143 XtManageChild(minNumColsEdit);
144
145
146 return _rowCol;
147 }
148
149 void
150 NoodleSurfaceGizmo::setNoodle( GeneralizedCylinder *newNoodle )
151 {
152 myNoodle = newNoodle;
153
154 // Set the values in the widgets to reflect values in the noodle.
155
156 char str1[10];
157 int iv;
158 iv = (myNoodle) ? myNoodle->minNumRows.getValue() : 1;
159 sprintf(str1,"%d", iv );
160 XmTextSetString( minNumRowsEdit, str1 );
161 char str2[10];
162 iv = (myNoodle) ? myNoodle->minNumCols.getValue() : 1;
163 sprintf(str2,"%d", iv );
164 XmTextSetString( minNumColsEdit, str2 );
165
166 // Set the crease angle
167 float newTheta;
168 if (myNoodle) {
169 SoShapeHints *h = SO_GET_PART( myNoodle, "shapeHints", SoShapeHints );
170 newTheta = h->creaseAngle.getValue();
171 newTheta *= 180.0 / M_PI;
172 }
173 else
174 newTheta = M_PI / 6.0;
175 creaseSlider->setValue( newTheta );
176 }
177
178 //
179 //
180 // Callback for the flip normals widget
181 //
182 void
183 NoodleSurfaceGizmo::flipNormCallback(Widget, XtPointer data, XtPointer)
184 {
185 NoodleSurfaceGizmo *myself = (NoodleSurfaceGizmo *) data;
186 GeneralizedCylinder *s = myself->myNoodle;
187 if ( s->normsFlipped.getValue() == TRUE )
188 s->normsFlipped = FALSE;
189 else
190 s->normsFlipped = TRUE;
191 }
192
193 void
194 NoodleSurfaceGizmo::minNumRowsCallback(Widget textWidget, XtPointer data, XtPointer)
195 {
196 NoodleSurfaceGizmo *myself = (NoodleSurfaceGizmo *) data;
197 GeneralizedCylinder *s = myself->myNoodle;
198
199 char *str = XmTextGetString((Widget)textWidget);
200 float tmp;
201 sscanf( str, "%f", &tmp );
202 tmp = ( tmp < 1.0 ) ? 1.0 : tmp;
203 s->minNumRows = (int) tmp;
204 sprintf( str, "%d", (int) tmp );
205 XmTextSetString((Widget)textWidget, str );
206 XtFree(str);
207 }
208
209 void
210 NoodleSurfaceGizmo::minNumColsCallback(Widget textWidget, XtPointer data, XtPointer)
211 {
212 NoodleSurfaceGizmo *myself = (NoodleSurfaceGizmo *) data;
213 GeneralizedCylinder *s = myself->myNoodle;
214
215 char *str = XmTextGetString((Widget)textWidget);
216 float tmp;
217 sscanf( str, "%f", &tmp );
218 tmp = ( tmp < 1.0 ) ? 1.0 : tmp;
219 s->minNumCols = (int) tmp;
220 sprintf( str, "%d", (int) tmp );
221 XmTextSetString((Widget)textWidget, str );
222 XtFree(str);
223 }
224
225 //
226 // Crease Angle callbacks
227 //
228 void
229 NoodleSurfaceGizmo::creaseCallback(void *userData, void *)
230 {
231 NoodleSurfaceGizmo *myself = (NoodleSurfaceGizmo *) userData;
232 float newVal = myself->creaseSlider->getValue();
233 GeneralizedCylinder *s = myself->myNoodle;
234 if ( s != NULL ) {
235 SoShapeHints *h = SO_GET_PART( s, "shapeHints", SoShapeHints );
236 h->creaseAngle = newVal * M_PI / 180.0;
237 }
238 }