home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / inventor / noodle / NoodleSurfaceGizmo.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.4 KB  |  219 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18. |   Description:
  19. |      A component class that creates a gizmo for editing 
  20. |      orientation of normals, crease angle, and the 
  21. |      minimum number of rows/columns in a noodle.
  22. |
  23. |   Author(s)          : Paul Isaacs
  24. |
  25. */
  26.  
  27. #include <X11/StringDefs.h>
  28. #include <Xm/LabelG.h>
  29. #include <Xm/RowColumn.h>
  30. #include <Xm/PushB.h>
  31. #include <Xm/PushBG.h>
  32. #include <Xm/Text.h>
  33.  
  34. #include <Inventor/nodes/SoShapeHints.h>
  35.  
  36. #include "GeneralizedCylinder.h"
  37. #include "NoodleSurfaceGizmo.h"
  38. #include "NoodleSlider.h"
  39.  
  40. NoodleSurfaceGizmo::NoodleSurfaceGizmo(
  41.     Widget parent,
  42.     const char *name, 
  43.     SbBool buildInsideParent) 
  44.     : SoXtComponent(
  45.         parent,
  46.         name, 
  47.         buildInsideParent)
  48. {
  49.     // Build the widget!
  50.     Widget w = buildWidget( getParentWidget() );
  51.     setBaseWidget(w);
  52.  
  53.     myNoodle = NULL;
  54. }
  55.  
  56. NoodleSurfaceGizmo::~NoodleSurfaceGizmo()
  57. {
  58. }
  59.  
  60. Widget
  61. NoodleSurfaceGizmo::buildWidget( 
  62.     Widget parent )
  63. {
  64.     Arg resources[20];
  65.     int n = 0;
  66.     SbVec2s sliderSize;
  67.  
  68.     _rowCol = XtCreateManagedWidget(getWidgetName(),xmRowColumnWidgetClass,
  69.                     parent,NULL, 0);
  70.  
  71. #define STRING(a) XmStringCreate(a,XmSTRING_DEFAULT_CHARSET)
  72.  
  73.     // Flip Normals button
  74.     XtSetArg(resources[n], XmNlabelString, STRING("Flip Normals")); ++n;
  75.     flipNormButton = XmCreatePushButtonGadget(_rowCol, "flip",
  76.                          resources, n); n = 0;
  77.     XtAddCallback(flipNormButton, XmNactivateCallback,
  78.           NoodleSurfaceGizmo::flipNormCallback, (XtPointer)this);
  79.     XtManageChild(flipNormButton);
  80.     
  81.     // Crease Angle.
  82.     // Label...
  83.     XtSetArg(resources[n], XmNlabelString, STRING("Crease Angle:")); ++n;
  84.     Widget creaseLabel = XmCreateLabelGadget(_rowCol, "Crease Angle",
  85.                          resources, n); n = 0;
  86.     XtManageChild(creaseLabel);
  87.     // Slider...
  88.     creaseSlider = new NoodleSlider(_rowCol);
  89.     creaseSlider->addValueChangedCallback( 
  90.             NoodleSurfaceGizmo::creaseCallback, this );
  91.     sliderSize = creaseSlider->getSize();
  92.     creaseSlider->setSize( SbVec2s( 200, sliderSize[1] ) );
  93.     creaseSlider->setMin(0.0);
  94.     creaseSlider->setMax(90.0);
  95.     creaseSlider->show();
  96.     
  97.     // Min Num rows label
  98.     XtSetArg(resources[n], XmNlabelString, STRING("Min num rows:")); ++n;
  99.     Widget numRowsLabel = XmCreateLabelGadget(_rowCol, "Min num rows",
  100.                          resources, n); n = 0;
  101.     XtManageChild(numRowsLabel);
  102.     // Min Num rows input
  103.     XtSetArg(resources[n], XmNcolumns, 3); ++n;
  104.     XtSetArg(resources[n], XmNeditMode, XmSINGLE_LINE_EDIT); ++n;
  105.     minNumRowsEdit = XmCreateText(_rowCol, "minNumRowsEdit",
  106.                      resources, n); n = 0;
  107.     XtAddCallback(minNumRowsEdit, XmNactivateCallback,
  108.           NoodleSurfaceGizmo::minNumRowsCallback, (XtPointer)this);
  109.     XtManageChild(minNumRowsEdit);
  110.  
  111.     // Min Num cols label
  112.     XtSetArg(resources[n], XmNlabelString, STRING("Min num cols:")); ++n;
  113.     Widget numColsLabel = XmCreateLabelGadget(_rowCol, "Min num cols",
  114.                          resources, n); n = 0;
  115.     XtManageChild(numColsLabel);
  116.     // Min Num cols input
  117.     XtSetArg(resources[n], XmNcolumns, 3); ++n;
  118.     XtSetArg(resources[n], XmNeditMode, XmSINGLE_LINE_EDIT); ++n;
  119.     minNumColsEdit = XmCreateText(_rowCol, "minNumColsEdit",
  120.                      resources, n); n = 0;
  121.     XtAddCallback(minNumColsEdit, XmNactivateCallback,
  122.           NoodleSurfaceGizmo::minNumColsCallback, (XtPointer)this);
  123.     XtManageChild(minNumColsEdit);
  124.  
  125.  
  126.     return _rowCol;
  127. }
  128.  
  129. void 
  130. NoodleSurfaceGizmo::setNoodle( GeneralizedCylinder *newNoodle )
  131. {
  132.     myNoodle = newNoodle;
  133.  
  134.     // Set the values in the widgets to reflect values in the noodle.
  135.  
  136.     char str1[10];
  137.     int iv;
  138.     iv = (myNoodle) ? myNoodle->minNumRows.getValue() : 1;
  139.     sprintf(str1,"%d", iv );
  140.     XmTextSetString( minNumRowsEdit, str1 );
  141.     char str2[10];
  142.     iv = (myNoodle) ? myNoodle->minNumCols.getValue() : 1;
  143.     sprintf(str2,"%d", iv );
  144.     XmTextSetString( minNumColsEdit, str2 );
  145.  
  146.     // Set the crease angle
  147.     float newTheta;
  148.     if (myNoodle) {
  149.     SoShapeHints *h = SO_GET_PART( myNoodle, "shapeHints", SoShapeHints );
  150.     newTheta = h->creaseAngle.getValue();
  151.     newTheta *= 180.0 / M_PI;
  152.     }
  153.     else 
  154.     newTheta = M_PI / 6.0;
  155.     creaseSlider->setValue( newTheta );
  156. }
  157.  
  158. //
  159. //
  160. // Callback for the flip normals  widget 
  161. //
  162. void
  163. NoodleSurfaceGizmo::flipNormCallback(Widget,  XtPointer data, XtPointer)
  164. {
  165.     NoodleSurfaceGizmo  *myself = (NoodleSurfaceGizmo *) data;
  166.     GeneralizedCylinder *s = myself->myNoodle;
  167.     if ( s->normsFlipped.getValue() == TRUE )
  168.         s->normsFlipped = FALSE;
  169.     else
  170.         s->normsFlipped = TRUE;
  171. }
  172.  
  173. void
  174. NoodleSurfaceGizmo::minNumRowsCallback(Widget textWidget, XtPointer data, XtPointer)
  175. {
  176.     NoodleSurfaceGizmo  *myself = (NoodleSurfaceGizmo *) data;
  177.     GeneralizedCylinder *s = myself->myNoodle;
  178.  
  179.     char *str = XmTextGetString((Widget)textWidget);
  180.     float tmp;
  181.     sscanf( str, "%f", &tmp );
  182.     tmp = ( tmp < 1.0 ) ? 1.0 : tmp;
  183.     s->minNumRows = (int) tmp;
  184.     sprintf( str, "%d", (int) tmp );
  185.     XmTextSetString((Widget)textWidget, str );
  186.     XtFree(str);
  187. }
  188.  
  189. void
  190. NoodleSurfaceGizmo::minNumColsCallback(Widget textWidget, XtPointer data, XtPointer)
  191. {
  192.     NoodleSurfaceGizmo  *myself = (NoodleSurfaceGizmo *) data;
  193.     GeneralizedCylinder *s = myself->myNoodle;
  194.  
  195.     char *str = XmTextGetString((Widget)textWidget);
  196.     float tmp;
  197.     sscanf( str, "%f", &tmp );
  198.     tmp = ( tmp < 1.0 ) ? 1.0 : tmp;
  199.     s->minNumCols = (int) tmp;
  200.     sprintf( str, "%d", (int) tmp );
  201.     XmTextSetString((Widget)textWidget, str );
  202.     XtFree(str);
  203. }
  204.  
  205. //
  206. // Crease Angle callbacks
  207. //
  208. void
  209. NoodleSurfaceGizmo::creaseCallback(void *userData, void *)
  210. {
  211.     NoodleSurfaceGizmo  *myself = (NoodleSurfaceGizmo *) userData;
  212.     float newVal = myself->creaseSlider->getValue();
  213.     GeneralizedCylinder *s = myself->myNoodle;
  214.     if ( s != NULL ) {
  215.     SoShapeHints *h = SO_GET_PART( s, "shapeHints", SoShapeHints );
  216.     h->creaseAngle = newVal * M_PI / 180.0;
  217.     }
  218. }
  219.