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