home *** CD-ROM | disk | FTP | other *** search
/ Flash MX Savvy / FlashMX Savvy.iso / pc / MAC / Amapi3D / Amapi3DTrial_Edition / 3SPACE / AnnotationBehavior.js < prev    next >
Encoding:
Text File  |  2001-02-20  |  3.1 KB  |  103 lines  |  [AMAS/AMAP]

  1. // -* AnnotationBehavior.js *-
  2. //
  3. // Name: Annotation behavior
  4. // Description: 
  5. // Author:
  6. // Version: $Id: AnnotationBehavior.js,v 1.7 2001/01/03 10:51:50 consumer Exp $
  7. //
  8.  
  9. // Keep an array of the solids using this behavior
  10. var annotationSolids = new Array(1);
  11.  
  12. function AnnotationBehavior(solidName, text, size, camera)
  13. {
  14.   // Member methods of the behavior
  15.   this.start = AnnotationBehaviorStart;
  16.   this.stop = AnnotationBehaviorStop;
  17.  
  18.   // Member variables of the behavior
  19.   this.cameraId = camera;
  20.   this.solidId = TSMakeUniqID("AnnotationSolid_" + solidName);
  21.   this.textId = TSMakeUniqID("AnnotationText_" + solidName);
  22.   this.axisId = TSMakeUniqID("AxisLink_" + solidName);
  23.  
  24.   // Initialize the behavior
  25.   var solidPos = TSSolidGetPosition(solidName);
  26.   var position = TSMakePoint(solidPos.x, solidPos.y + TSSolidGetLengthY(solidName) * 0.8, solidPos.z);
  27.  
  28.   // Make a new solid
  29.   TSMakeSolid(this.solidId, '0', TSMakeStringFromPoint(position));
  30.   TSSetAttribute(this.solidId, 'mass', '0');
  31.   TSAppendChild(TSGetSceneId(), this.solidId);
  32.  
  33.   // Make axis link
  34.   var axis = TSMakePoint(0, 1, 0);
  35.   var point = TSMakePoint(0, 0, 0);
  36.   TSMakeAxisLink(this.axisId, axis, point, this.solidId)
  37.   TSAppendChild(solidName, this.axisId);
  38.   TSUpdateNode(solidName);
  39.       
  40.   // Make a text
  41.   TSMakeText(this.textId, text, 1, '1 1 1');
  42.   TSAppendChild(this.solidId, this.textId);
  43.   
  44.   TSUpdateNodeAttribute(this.textId, 'visible', '0');
  45.   TSUpdateNode(this.solidId);
  46.  
  47.   // Calculate the size of the text
  48.   var len1 = TSSolidGetLengthX(solidName);
  49.   var len2 = TSSolidGetLengthX(this.solidId);
  50.   var s = (len1 / len2) * size;
  51.   var thickness = TSSolidGetLengthZ(solidName) / 20.0;
  52.  
  53.   TSSetAttribute(this.textId, 'size', s.toString());
  54.   TSSetAttribute(this.textId, 'thickness', thickness.toString());
  55.   TSUpdateNode(this.textId);
  56. }
  57.  
  58. function AnnotationBehaviorStart()
  59. {
  60.   var position = TSCameraGetPosition(this.cameraId);
  61.   var targetPoint = TSCameraGetTargetPosition(this.cameraId);
  62.  
  63.   // Create the vector between the camera and the target point (in spherical coordinates)
  64.   var sphericCoords = TSVectorToSphericCoords(TSMakeVector(targetPoint, position));
  65.   var phiDeg = TSRadToDeg(sphericCoords.phi);
  66.   var thetaDeg = TSRadToDeg(sphericCoords.theta);
  67.   var rot = TSMakeStringFromPoint(TSMakePoint(0, thetaDeg, 0));
  68.  
  69.   TSUpdateNodeAttribute(this.solidId, 'rotation', rot);
  70.   TSUpdateNodeAttribute(this.textId, 'visible', '1');
  71. }
  72.  
  73. function AnnotationBehaviorStop()
  74. {
  75.   TSUpdateNodeAttribute(this.textId, 'visible', '0');
  76. }
  77.  
  78. //
  79. // Event functions
  80. //
  81.  
  82. function AnnotationBehaviorStartEvent(obj, event)
  83. {
  84.   if (annotationSolids[obj] == null) {
  85.     var text = TSGetExtraParam(event, 'text');
  86.     var size = TSGetExtraParam(event, 'size');
  87.     var camera = TSGetExtraParam(event, 'camera');
  88.     var targetSolid = TSGetExtraParam(event, 'targetSolid');
  89.  
  90.     if (targetSolid == "")
  91.       annotationSolids[obj] = new AnnotationBehavior(obj, text, size, camera);
  92.     else
  93.       annotationSolids[obj] = new AnnotationBehavior(targetSolid, text, size, camera);
  94.   }
  95.  
  96.   annotationSolids[obj].start();
  97. }
  98.  
  99. function AnnotationBehaviorStopEvent(obj, event)
  100. {
  101.   annotationSolids[obj].stop();
  102. }
  103.