| 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 Mentor |
| 39 |
* chapter 10, example 3. |
| 40 |
* |
| 41 |
* The scene graph has 4 objects which may be |
| 42 |
* selected by picking with the left mouse button |
| 43 |
* (use shift key to extend the selection to more |
| 44 |
* than one object). |
| 45 |
* |
| 46 |
* Hitting the up arrow key will increase the size of |
| 47 |
* each selected object; hitting down arrow will decrease |
| 48 |
* the size of each selected object. |
| 49 |
* |
| 50 |
* This also demonstrates selecting objects from a Motif |
| 51 |
* list, and calling select/deselect functions on the |
| 52 |
* SoSelection node to change the selection. Use the Shift |
| 53 |
* key to extend the selection (i.e. pick more than one |
| 54 |
* item in the motif list.) |
| 55 |
*-----------------------------------------------------------*/ |
| 56 |
|
| 57 |
#include <cstdlib> |
| 58 |
#include <X11/Intrinsic.h> |
| 59 |
#include <Xm/Xm.h> |
| 60 |
#include <Xm/List.h> |
| 61 |
|
| 62 |
#include <Inventor/Sb.h> |
| 63 |
#include <Inventor/Xt/SoXt.h> |
| 64 |
#include <Inventor/Xt/SoXtRenderArea.h> |
| 65 |
#include <Inventor/actions/SoBoxHighlightRenderAction.h> |
| 66 |
#include <Inventor/actions/SoSearchAction.h> |
| 67 |
#include <Inventor/events/SoKeyboardEvent.h> |
| 68 |
#include <Inventor/nodes/SoCone.h> |
| 69 |
#include <Inventor/nodes/SoCube.h> |
| 70 |
#include <Inventor/nodes/SoCylinder.h> |
| 71 |
#include <Inventor/nodes/SoDirectionalLight.h> |
| 72 |
#include <Inventor/nodes/SoEventCallback.h> |
| 73 |
#include <Inventor/nodes/SoMaterial.h> |
| 74 |
#include <Inventor/nodes/SoPerspectiveCamera.h> |
| 75 |
#include <Inventor/nodes/SoSphere.h> |
| 76 |
#include <Inventor/nodes/SoSelection.h> |
| 77 |
#include <Inventor/nodes/SoSeparator.h> |
| 78 |
#include <Inventor/nodes/SoTransform.h> |
| 79 |
|
| 80 |
// Function prototypes |
| 81 |
void mySelectionCB(void *, SoPath *); |
| 82 |
void myKeyPressCB(void *, SoEventCallback *); |
| 83 |
void myScaleSelection(SoSelection *, float); |
| 84 |
Widget createList(Display *, SoSelection *); |
| 85 |
void myListPickCB(Widget, char *, XmListCallbackStruct *); |
| 86 |
|
| 87 |
// Global data |
| 88 |
SbViewportRegion viewportRegion; |
| 89 |
Widget motifList; |
| 90 |
SoTransform *cubeTransform, *sphereTransform, |
| 91 |
*coneTransform, *cylTransform; |
| 92 |
|
| 93 |
/////////////////////////////////////////////////////////////// |
| 94 |
// CODE FOR The Inventor Mentor STARTS HERE |
| 95 |
|
| 96 |
enum objects { |
| 97 |
CUBE, |
| 98 |
SPHERE, |
| 99 |
CONE, |
| 100 |
CYL, |
| 101 |
NUM_OBJECTS |
| 102 |
}; |
| 103 |
|
| 104 |
static char *objectNames[] = { |
| 105 |
"Cube", |
| 106 |
"Sphere", |
| 107 |
"Cone", |
| 108 |
"Cylinder" |
| 109 |
}; |
| 110 |
|
| 111 |
// CODE FOR The Inventor Mentor ENDS HERE |
| 112 |
/////////////////////////////////////////////////////////////// |
| 113 |
|
| 114 |
int |
| 115 |
main(int , char **argv) |
| 116 |
{ |
| 117 |
// Print out usage message |
| 118 |
printf("Left mouse button - selects object\n"); |
| 119 |
printf("<shift>Left mouse button - selects multiple objects\n"); |
| 120 |
printf("Up and Down arrows - scale selected objects\n"); |
| 121 |
|
| 122 |
// Initialize Inventor and Xt |
| 123 |
Widget myWindow = SoXt::init(argv[0]); |
| 124 |
if (myWindow == NULL) exit(1); |
| 125 |
|
| 126 |
// Create and set up the selection node |
| 127 |
SoSelection *selectionRoot = new SoSelection; |
| 128 |
selectionRoot->ref(); |
| 129 |
selectionRoot->policy = SoSelection::SHIFT; |
| 130 |
selectionRoot->addSelectionCallback( |
| 131 |
mySelectionCB, (void *) TRUE); |
| 132 |
selectionRoot->addDeselectionCallback( |
| 133 |
mySelectionCB, (void *) FALSE); |
| 134 |
|
| 135 |
// Add a camera and some light |
| 136 |
SoPerspectiveCamera *myCamera = new SoPerspectiveCamera; |
| 137 |
selectionRoot->addChild(myCamera); |
| 138 |
selectionRoot->addChild(new SoDirectionalLight); |
| 139 |
|
| 140 |
// Add an event callback so we can receive key press events |
| 141 |
SoEventCallback *myEventCB = new SoEventCallback; |
| 142 |
myEventCB->addEventCallback( |
| 143 |
SoKeyboardEvent::getClassTypeId(), |
| 144 |
myKeyPressCB, selectionRoot); |
| 145 |
selectionRoot->addChild(myEventCB); |
| 146 |
|
| 147 |
// Add some geometry to the scene |
| 148 |
// a red cube |
| 149 |
SoSeparator *cubeRoot = new SoSeparator; |
| 150 |
SoMaterial *cubeMaterial = new SoMaterial; |
| 151 |
cubeTransform = new SoTransform; |
| 152 |
SoCube *cube = new SoCube; |
| 153 |
cubeRoot->addChild(cubeTransform); |
| 154 |
cubeRoot->addChild(cubeMaterial); |
| 155 |
cubeRoot->addChild(cube); |
| 156 |
cubeTransform->translation.setValue(-2, 2, 0); |
| 157 |
cubeMaterial->diffuseColor.setValue(.8, 0, 0); |
| 158 |
selectionRoot->addChild(cubeRoot); |
| 159 |
|
| 160 |
// a blue sphere |
| 161 |
SoSeparator *sphereRoot = new SoSeparator; |
| 162 |
SoMaterial *sphereMaterial = new SoMaterial; |
| 163 |
sphereTransform = new SoTransform; |
| 164 |
SoSphere *sphere = new SoSphere; |
| 165 |
sphereRoot->addChild(sphereTransform); |
| 166 |
sphereRoot->addChild(sphereMaterial); |
| 167 |
sphereRoot->addChild(sphere); |
| 168 |
sphereTransform->translation.setValue(2, 2, 0); |
| 169 |
sphereMaterial->diffuseColor.setValue(0, 0, .8); |
| 170 |
selectionRoot->addChild(sphereRoot); |
| 171 |
|
| 172 |
// a green cone |
| 173 |
SoSeparator *coneRoot = new SoSeparator; |
| 174 |
SoMaterial *coneMaterial = new SoMaterial; |
| 175 |
coneTransform = new SoTransform; |
| 176 |
SoCone *cone = new SoCone; |
| 177 |
coneRoot->addChild(coneTransform); |
| 178 |
coneRoot->addChild(coneMaterial); |
| 179 |
coneRoot->addChild(cone); |
| 180 |
coneTransform->translation.setValue(2, -2, 0); |
| 181 |
coneMaterial->diffuseColor.setValue(0, .8, 0); |
| 182 |
selectionRoot->addChild(coneRoot); |
| 183 |
|
| 184 |
// a magenta cylinder |
| 185 |
SoSeparator *cylRoot = new SoSeparator; |
| 186 |
SoMaterial *cylMaterial = new SoMaterial; |
| 187 |
cylTransform = new SoTransform; |
| 188 |
SoCylinder *cyl = new SoCylinder; |
| 189 |
cylRoot->addChild(cylTransform); |
| 190 |
cylRoot->addChild(cylMaterial); |
| 191 |
cylRoot->addChild(cyl); |
| 192 |
cylTransform->translation.setValue(-2, -2, 0); |
| 193 |
cylMaterial->diffuseColor.setValue(.8, 0, .8); |
| 194 |
selectionRoot->addChild(cylRoot); |
| 195 |
|
| 196 |
/////////////////////////////////////////////////////////////// |
| 197 |
// CODE FOR The Inventor Mentor STARTS HERE (part 2) |
| 198 |
|
| 199 |
cube->setName(objectNames[CUBE]); |
| 200 |
sphere->setName(objectNames[SPHERE]); |
| 201 |
cone->setName(objectNames[CONE]); |
| 202 |
cyl->setName(objectNames[CYL]); |
| 203 |
|
| 204 |
// CODE FOR The Inventor Mentor ENDS HERE |
| 205 |
/////////////////////////////////////////////////////////////// |
| 206 |
|
| 207 |
// Create a render area for viewing the scene |
| 208 |
SoXtRenderArea *myRenderArea = new SoXtRenderArea(myWindow); |
| 209 |
myRenderArea->setGLRenderAction(new SoBoxHighlightRenderAction()); |
| 210 |
myRenderArea->redrawOnSelectionChange(selectionRoot); |
| 211 |
myRenderArea->setSceneGraph(selectionRoot); |
| 212 |
myRenderArea->setTitle("Motif Selection List"); |
| 213 |
|
| 214 |
// Make the camera see the whole scene |
| 215 |
viewportRegion = myRenderArea->getViewportRegion(); |
| 216 |
myCamera->viewAll(selectionRoot, viewportRegion, 2.0); |
| 217 |
|
| 218 |
// Create a Motif list for selecting objects without picking |
| 219 |
Widget objectList = createList( |
| 220 |
XtDisplay(myWindow), selectionRoot); |
| 221 |
|
| 222 |
// Show our application window, and loop forever... |
| 223 |
myRenderArea->show(); |
| 224 |
SoXt::show(myWindow); |
| 225 |
SoXt::show(objectList); |
| 226 |
SoXt::mainLoop(); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
// Create the object list widget |
| 231 |
Widget |
| 232 |
createList(Display *display, SoSelection *selection) |
| 233 |
{ |
| 234 |
Widget shell; |
| 235 |
Arg args[4]; |
| 236 |
int i, n; |
| 237 |
|
| 238 |
// Create a new shell window for the list |
| 239 |
n = 0; |
| 240 |
XtSetArg(args[n], XmNtitle, "Selection"); |
| 241 |
n++; |
| 242 |
shell = XtAppCreateShell("example", "Inventor", |
| 243 |
topLevelShellWidgetClass, display, args, n); |
| 244 |
|
| 245 |
/////////////////////////////////////////////////////////////// |
| 246 |
// CODE FOR The Inventor Mentor STARTS HERE (part 3) |
| 247 |
|
| 248 |
// Create a table of object names |
| 249 |
XmString *table = new XmString[NUM_OBJECTS]; |
| 250 |
for (i=0; i<NUM_OBJECTS; i++) { |
| 251 |
table[i] = XmStringCreate(objectNames[i], |
| 252 |
XmSTRING_DEFAULT_CHARSET); |
| 253 |
} |
| 254 |
|
| 255 |
// Create the list widget |
| 256 |
n = 0; |
| 257 |
XtSetArg(args[n], XmNitems, table); |
| 258 |
n++; |
| 259 |
XtSetArg(args[n], XmNitemCount, NUM_OBJECTS); |
| 260 |
n++; |
| 261 |
XtSetArg(args[n], XmNselectionPolicy, XmEXTENDED_SELECT); |
| 262 |
n++; |
| 263 |
|
| 264 |
motifList = XmCreateScrolledList(shell, "funcList", args, n); |
| 265 |
XtAddCallback(motifList, XmNextendedSelectionCallback, |
| 266 |
(XtCallbackProc) myListPickCB, (XtPointer) selection); |
| 267 |
|
| 268 |
// CODE FOR The Inventor Mentor ENDS HERE |
| 269 |
/////////////////////////////////////////////////////////////// |
| 270 |
|
| 271 |
// Free the name table |
| 272 |
for (i = 0; i < NUM_OBJECTS; i++) |
| 273 |
XmStringFree(table[i]); |
| 274 |
free(table); |
| 275 |
|
| 276 |
// Manage the list and return the shell |
| 277 |
XtManageChild(motifList); |
| 278 |
|
| 279 |
return shell; |
| 280 |
} |
| 281 |
|
| 282 |
// This callback is invoked every time the user picks |
| 283 |
// an item in the Motif list. |
| 284 |
void |
| 285 |
myListPickCB(Widget, char *userData, |
| 286 |
XmListCallbackStruct *listData) |
| 287 |
{ |
| 288 |
SoSelection *selection = (SoSelection *) userData; |
| 289 |
SoSearchAction mySearchAction; |
| 290 |
|
| 291 |
// Remove the selection callbacks so that we don't get |
| 292 |
// called back while we are updating the selection list |
| 293 |
selection->removeSelectionCallback( |
| 294 |
mySelectionCB, (void *) TRUE); |
| 295 |
selection->removeDeselectionCallback( |
| 296 |
mySelectionCB, (void *) FALSE); |
| 297 |
|
| 298 |
////////////////////////////////////////////////////////////// |
| 299 |
// CODE FOR The Inventor Mentor STARTS HERE (part 4) |
| 300 |
|
| 301 |
// Clear the selection node, then loop through the list |
| 302 |
// and reselect |
| 303 |
selection->deselectAll(); |
| 304 |
|
| 305 |
// Update the SoSelection based on what is selected in |
| 306 |
// the motif list. We do this by extracting the string |
| 307 |
// from the selected XmString, and searching for the |
| 308 |
// object of that name. |
| 309 |
for (int i = 0; i < listData->selected_item_count; i++) { |
| 310 |
mySearchAction.setName( |
| 311 |
SoXt::decodeString(listData->selected_items[i])); |
| 312 |
mySearchAction.apply(selection); |
| 313 |
selection->select(mySearchAction.getPath()); |
| 314 |
} |
| 315 |
|
| 316 |
// CODE FOR The Inventor Mentor ENDS HERE |
| 317 |
////////////////////////////////////////////////////////////// |
| 318 |
|
| 319 |
// Add the selection callbacks again |
| 320 |
selection->addSelectionCallback( |
| 321 |
mySelectionCB, (void *) TRUE); |
| 322 |
selection->addDeselectionCallback( |
| 323 |
mySelectionCB, (void *) FALSE); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
// This is called whenever an object is selected or deselected. |
| 328 |
// if userData is TRUE, then it's a selection; else deselection. |
| 329 |
// (we set this convention up when we registered this callback). |
| 330 |
// The function updates the Motif list to reflect the current |
| 331 |
// selection. |
| 332 |
void |
| 333 |
mySelectionCB(void *userData, SoPath *selectionPath) |
| 334 |
{ |
| 335 |
Arg args[1]; |
| 336 |
SbBool isSelection = (SbBool) (userData != NULL); |
| 337 |
|
| 338 |
// We have to temporarily change the selection policy to |
| 339 |
// MULTIPLE so that we can select and deselect single items. |
| 340 |
XtSetArg(args[0], XmNselectionPolicy, XmMULTIPLE_SELECT); |
| 341 |
XtSetValues(motifList, args, 1); |
| 342 |
|
| 343 |
SoNode *node = selectionPath->getTail(); |
| 344 |
|
| 345 |
for (int i = 0; i < NUM_OBJECTS; i++) { |
| 346 |
if (node->getName() == objectNames[i]) { |
| 347 |
if (isSelection) { |
| 348 |
XmListSelectPos(motifList, i+1, False); |
| 349 |
} else XmListDeselectPos(motifList, i+1); |
| 350 |
XmUpdateDisplay(motifList); |
| 351 |
break; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
// Restore the selection policy to extended. |
| 356 |
XtSetArg(args[0], XmNselectionPolicy, XmEXTENDED_SELECT); |
| 357 |
XtSetValues(motifList, args, 1); |
| 358 |
} |
| 359 |
|
| 360 |
// If the event in the graphics window is down arrow, then |
| 361 |
// scale every object in the selection list by 1/2; if the |
| 362 |
// event is up arrow, scale by 2. |
| 363 |
// userData is the selectionRoot from main(). |
| 364 |
void |
| 365 |
myKeyPressCB(void *userData, SoEventCallback *eventCB) |
| 366 |
{ |
| 367 |
SoSelection *selection = (SoSelection *) userData; |
| 368 |
const SoEvent *event = eventCB->getEvent(); |
| 369 |
|
| 370 |
// check for the Up and Down arrow keys being pressed |
| 371 |
if (SO_KEY_PRESS_EVENT(event, UP_ARROW)) { |
| 372 |
myScaleSelection(selection, 1.1); |
| 373 |
eventCB->setHandled(); |
| 374 |
} else if (SO_KEY_PRESS_EVENT(event, DOWN_ARROW)) { |
| 375 |
myScaleSelection(selection, 1.0/1.1); |
| 376 |
eventCB->setHandled(); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
////////////////////////////////////////////////////////////// |
| 381 |
// CODE FOR The Inventor Mentor STARTS HERE (Example 10-4) |
| 382 |
|
| 383 |
// Scale each object in the selection list |
| 384 |
void |
| 385 |
myScaleSelection(SoSelection *selection, float sf) |
| 386 |
{ |
| 387 |
SoPath *selectedPath; |
| 388 |
SoTransform *xform; |
| 389 |
SbVec3f scaleFactor; |
| 390 |
int i,j; |
| 391 |
|
| 392 |
// Scale each object in the selection list |
| 393 |
|
| 394 |
for (i = 0; i < selection->getNumSelected(); i++) { |
| 395 |
selectedPath = selection->getPath(i); |
| 396 |
xform = NULL; |
| 397 |
|
| 398 |
// Look for the shape node, starting from the tail of the |
| 399 |
// path. Once we know the type of shape, we know which |
| 400 |
// transform to modify |
| 401 |
for (j=0; j < selectedPath->getLength() && |
| 402 |
(xform == NULL); j++) { |
| 403 |
SoNode *n = (SoNode *)selectedPath->getNodeFromTail(j); |
| 404 |
if (n->isOfType(SoCube::getClassTypeId())) { |
| 405 |
xform = cubeTransform; |
| 406 |
} else if (n->isOfType(SoCone::getClassTypeId())) { |
| 407 |
xform = coneTransform; |
| 408 |
} else if (n->isOfType(SoSphere::getClassTypeId())) { |
| 409 |
xform = sphereTransform; |
| 410 |
} else if (n->isOfType(SoCylinder::getClassTypeId())) { |
| 411 |
xform = cylTransform; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
// Apply the scale |
| 416 |
scaleFactor = xform->scaleFactor.getValue(); |
| 417 |
scaleFactor *= sf; |
| 418 |
xform->scaleFactor.setValue(scaleFactor); |
| 419 |
} |
| 420 |
|
| 421 |
} |
| 422 |
|
| 423 |
// CODE FOR The Inventor Mentor ENDS HERE |
| 424 |
////////////////////////////////////////////////////////////// |