scale-on-move.vwf.yaml 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # Copyright 2012 United States Government, as represented by the Secretary of Defense, Under
  2. # Secretary of Defense (Personnel & Readiness).
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. # in compliance with the License. You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software distributed under the License
  10. # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. # or implied. See the License for the specific language governing permissions and limitations under
  12. # the License.
  13. ## The component representation of a scale behavior (on move)
  14. ##
  15. ## @name scale-on-click.vwf
  16. ## @namespace
  17. ---
  18. extends:
  19. http://vwf.example.com/node3.vwf
  20. properties:
  21. ## Scale rate
  22. ##
  23. ## @name scale-on-move.vwf#scale-rate
  24. ## @property
  25. scale-rate: 0.14
  26. events:
  27. valueChanged:
  28. methods:
  29. ## Scale function. Updates component scale
  30. ##
  31. ## @name scale-on-move.vwf#updateScale
  32. ## @function
  33. ##
  34. ## @returns undefined
  35. updateScale:
  36. ## Initialization function
  37. ##
  38. ## @name scale-on-move.vwf#init
  39. ## @function
  40. ##
  41. ## @returns undefined
  42. init:
  43. scripts:
  44. - |
  45. this.init = function() {
  46. this.input = {
  47. "pointerInfo": undefined,
  48. "pickInfo": undefined,
  49. "previous": {
  50. "pointerInfo": undefined,
  51. "pickInfo": undefined,
  52. },
  53. "pointerDown": {
  54. "pointerInfo": undefined,
  55. "pickInfo": undefined,
  56. },
  57. "update": function( pointerInfo, pickInfo ){
  58. this.previous.pointerInfo = this.pointerInfo;
  59. this.previous.pickInfo = this.pickInfo;
  60. this.pointerInfo = pointerInfo;
  61. this.pickInfo = pickInfo;
  62. },
  63. "clear": function(){
  64. this.previous.pointerInfo = undefined;
  65. this.previous.pickInfo = undefined;
  66. this.pointerInfo = undefined;
  67. this.pickInfo = undefined;
  68. },
  69. "change": function() {
  70. var ret = [ 0, 0 ]
  71. if ( this.pointerInfo && this.previous.pointerInfo ) {
  72. ret[0] = this.pointerInfo.position[0] - this.previous.pointerInfo.position[0];
  73. ret[1] = this.pointerInfo.position[1] - this.previous.pointerInfo.position[1];
  74. }
  75. return ret;
  76. }
  77. };
  78. }
  79. this.pointerDown = function( pointerInfo, pickInfo ) {
  80. if ( !this.input ) this.init();
  81. this.input.clear();
  82. this.input.pointerDown.pointerInfo = pointerInfo;
  83. this.input.pointerDown.pickInfo = pickInfo;
  84. this.worldTransOnDown = goog.vec.Mat4.createFromArray( this.parent.worldTransform );
  85. this.scaleOnDown = goog.vec.Vec3.createFromArray( this.parent.scale );
  86. this.mouseDownPos = goog.vec.Vec3.createFromArray( pickInfo.globalPosition );
  87. this.objectPos = goog.vec.Vec3.createFromValues( this.worldTransOnDown[12], this.worldTransOnDown[13], this.worldTransOnDown[14] );
  88. this.cameraPos = goog.vec.Vec3.createFromValues( pickInfo.globalSource[0], pickInfo.globalSource[1], pickInfo.globalSource[2] );
  89. // backwards?
  90. //this.objectVec3OnDown = goog.vec.Vec3.subtract( this.cameraPos, this.objectPos, goog.vec.Vec3.create() );
  91. //this.pointerVec3OnDown = goog.vec.Vec3.subtract( this.cameraPos, this.mouseDownPos, goog.vec.Vec3.create() );
  92. this.objectVec3OnDown = goog.vec.Vec3.subtract( this.objectPos, this.cameraPos, goog.vec.Vec3.create() );
  93. this.pointerVec3OnDown = goog.vec.Vec3.subtract( this.mouseDownPos, this.cameraPos, goog.vec.Vec3.create() );
  94. goog.vec.Vec3.normalize( this.objectVec3OnDown, this.objectVec3OnDown );
  95. goog.vec.Vec3.normalize( this.pointerVec3OnDown, this.pointerVec3OnDown );
  96. this.lastAngle = Math.acos( goog.vec.Vec3.dot( this.objectVec3OnDown, this.pointerVec3OnDown ) ) * (180 / Math.PI);
  97. // TO DO
  98. // if listAngle is very small then need to modify the
  99. // scale rate, given this approach
  100. // TO DO
  101. // also the distance to the object should be taken into consideration
  102. // the further away the object is, then the change in the angle
  103. // from the center doesn't change much when trying to decrease the scale
  104. }
  105. this.pointerUp = function( pointerInfo, pickInfo ) {
  106. if ( !this.input ) this.init();
  107. this.input.clear();
  108. }
  109. this.pointerMove = function( pointerInfo, pickInfo ) {
  110. this.input.update( pointerInfo, pickInfo );
  111. this.updateScale();
  112. }
  113. this.updateScale = function(){
  114. var currentScale = this.parent.scale;
  115. var currentPointer = goog.vec.Vec3.createFromArray( this.input.pickInfo.pointerVector );
  116. goog.vec.Vec3.normalize( currentPointer, currentPointer );
  117. var currentAngle = Math.acos( goog.vec.Vec3.dot( this.objectVec3OnDown, currentPointer ) ) * (180 / Math.PI);
  118. if ( currentAngle > this.lastAngle ) {
  119. // moving away from the center
  120. this.parent.scale = goog.vec.Vec3.add( currentScale,
  121. goog.vec.Vec3.scale(
  122. goog.vec.Vec3.createFromValues( 1, 1, 1 ),
  123. ( currentAngle - this.lastAngle ) * this[ "scale-rate" ],
  124. goog.vec.Vec3.create()
  125. ),
  126. goog.vec.Vec3.create() );
  127. this.valueChanged( this.parent.scale );
  128. this.lastAngle = currentAngle;
  129. } else if ( currentAngle < this.lastAngle ){
  130. // moving towards from the center
  131. var newScale = goog.vec.Vec3.subtract( currentScale,
  132. goog.vec.Vec3.scale(
  133. goog.vec.Vec3.createFromValues( 1, 1, 1 ),
  134. ( this.lastAngle - currentAngle ) * this[ "scale-rate" ],
  135. goog.vec.Vec3.create()
  136. ),
  137. goog.vec.Vec3.create() );
  138. // this needs to decrease more as the user
  139. if ( goog.vec.Vec3.magnitude( newScale ) > 0 ) {
  140. this.parent.scale = newScale;
  141. this.valueChanged( this.parent.scale );
  142. this.lastAngle = currentAngle;
  143. }
  144. }
  145. } //@ sourceURL=scale-on-move.vwf