node3edit.vwf.yaml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 an editable node3 behavior
  14. ##
  15. ## @name node3edit.vwf
  16. ## @namespace
  17. ---
  18. extends: http://vwf.example.com/node3.vwf
  19. properties:
  20. ## Edit mode
  21. ##
  22. ## @name node3edit.vwf#editMode
  23. ## @property
  24. editMode:
  25. set: |
  26. switch ( value ) {
  27. case "translate":
  28. case "rotate":
  29. case "scale":
  30. this.editMode = value;
  31. break;
  32. }
  33. ## Axis
  34. ##
  35. ## @name node3edit.vwf#axis
  36. ## @property
  37. axis: [ 0, 0, 1 ]
  38. methods:
  39. ## Translate
  40. ##
  41. ## @name node3edit.vwf#translate
  42. ## @function
  43. ##
  44. ## @returns undefined
  45. translate:
  46. ## Rotate
  47. ##
  48. ## @name node3edit.vwf#rotate
  49. ## @function
  50. ##
  51. ## @returns undefined
  52. rotate:
  53. ## Scaling
  54. ##
  55. ## @name node3edit.vwf#scaling
  56. ## @function
  57. ##
  58. ## @returns undefined
  59. scaling:
  60. ## Edit
  61. ##
  62. ## @name node3edit.vwf#edit
  63. ## @function
  64. ##
  65. ## @returns undefined
  66. edit:
  67. ## Set Axis
  68. ##
  69. ## @name node3edit.vwf#setAxis
  70. ## @function
  71. ##
  72. ## @returns undefined
  73. setAxis:
  74. ## Initialize function
  75. ##
  76. ## @name node3edit.vwf#init
  77. ## @function
  78. ##
  79. ## @returns undefined
  80. init:
  81. scripts:
  82. - |
  83. this.init = function() {
  84. this.rotationAxis = 'z';
  85. this.input = {
  86. "initialTransform": undefined,
  87. "initialValue": undefined,
  88. "pointerInfo": undefined,
  89. "pickInfo": undefined,
  90. "previous": {
  91. "pointerInfo": undefined,
  92. "pickInfo": undefined,
  93. },
  94. pointerDown: {
  95. "pointerInfo": undefined,
  96. "pickInfo": undefined,
  97. },
  98. update: function( pointerInfo, pickInfo ){
  99. this.previous.pointerInfo = this.pointerInfo;
  100. this.previous.pickInfo = this.pickInfo;
  101. this.pointerInfo = pointerInfo;
  102. this.pickInfo = pickInfo;
  103. },
  104. clear: function(){
  105. this.previous.pointerInfo = undefined;
  106. this.previous.pickInfo = undefined;
  107. this.pointerInfo = undefined;
  108. this.pickInfo = undefined;
  109. this.initialValue = undefined;
  110. },
  111. change: function() {
  112. var ret = [ 0, 0 ]
  113. if ( this.pointerInfo && this.previous.pointerInfo ) {
  114. ret[0] = this.pointerInfo.position[0] - this.previous.pointerInfo.position[0];
  115. ret[1] = this.pointerInfo.position[1] - this.previous.pointerInfo.position[1];
  116. }
  117. return ret;
  118. },
  119. pointChange: function() {
  120. var ret = [ 0, 0, 0 ];
  121. if ( this.pickInfo && this.previous.pickInfo ) {
  122. var oldPt = this.previous.pickInfo.globalSource;
  123. var newPt = this.pickInfo.globalSource;
  124. ret[0] = newPt[0] - oldPt[0];
  125. ret[1] = newPt[1] - oldPt[1];
  126. ret[2] = newPt[2] - oldPt[2];
  127. }
  128. return ret;
  129. },
  130. };
  131. }
  132. this.pointerDown = this.events.add( function( pointerInfo, pickInfo ) {
  133. if ( !this.input ) { this.init(); }
  134. this.input.clear();
  135. this.input.initialTransform = this.transform;
  136. this.input.pointerDown.pointerInfo = pointerInfo;
  137. this.input.pointerDown.pointerInfo = pickInfo;
  138. this.input.update( pointerInfo, pickInfo );
  139. this.setAxis();
  140. }, "capture", this );
  141. this.pointerUp = this.events.add( function( pointerInfo, pickInfo ) {
  142. this.input.update( pointerInfo, pickInfo );
  143. this.edit();
  144. this.input.clear();
  145. }, "capture", this );
  146. this.pointerMove = this.events.add( function( pointerInfo, pickInfo ) {
  147. this.input.update( pointerInfo, pickInfo );
  148. this.edit();
  149. }, "capture", this );
  150. this.edit = function() {
  151. switch( this.editMode ) {
  152. case "translate":
  153. this.translate();
  154. break;
  155. case "rotate":
  156. this.rotate();
  157. break;
  158. case "scale":
  159. this.scaling();
  160. break;
  161. }
  162. }
  163. this.setAxis = function() {
  164. var info = this.input.pickInfo;
  165. if ( info && info.globalNormal ) {
  166. var largest = 0;
  167. var val = -1;
  168. for ( var i = 0; i < 3; i++ ) {
  169. if ( val >= info.globalNormal[i] ) {
  170. largest = i;
  171. val = info.globalNormal[i];
  172. }
  173. }
  174. switch( largest ) {
  175. case 0:
  176. this.axis = [ 1, 0, 0 ];
  177. this.rotationAxis = "x";
  178. break;
  179. case 1:
  180. this.axis = [ 0, 1, 0 ];
  181. this.rotationAxis = "y";
  182. break;
  183. default:
  184. this.axis = [ 0, 0, 1 ];
  185. this.rotationAxis = "z";
  186. break;
  187. }
  188. }
  189. }
  190. this.translate = function() {
  191. if ( !this.input.initialValue ) {
  192. this.input.initialValue = {
  193. transform: this.transform,
  194. }
  195. }
  196. var pc = this.input.pointChange();
  197. var pos = this.position;
  198. pos[0] += pc[0];
  199. pos[1] += pc[1];
  200. pos[2] += pc[2];
  201. this.position = pos;
  202. }
  203. this.rotate = function() {
  204. if ( !this.input.initialValue ) {
  205. this.input.initialValue = {
  206. transform: this.transform,
  207. }
  208. }
  209. var diff = this.input.change();
  210. var rot = this.rotation;
  211. switch( this.rotationAxis ) {
  212. case "x":
  213. this.rotateBy( [ 1, 0, 0, rot[3] + (diff[0] * 4) ], 0 );
  214. break;
  215. case "y":
  216. this.rotateBy( [ 0, 1, 0, rot[3] + (diff[0] * 4) ], 0 );
  217. break;
  218. case "z":
  219. this.rotateBy( [ 0, 0, 1, rot[3] + (diff[0] * 4) ], 0 );
  220. break;
  221. default:
  222. this.rotateBy( [ this.axis[0], this.axis[1], this.axis[2], rot[3] + (diff[0] * 4) ], 0 );
  223. break;
  224. }
  225. }
  226. this.scaling = function() {
  227. if ( !this.input.initialValue ) {
  228. this.input.initialValue = {
  229. transform: this.transform,
  230. }
  231. }
  232. var diff = this.input.change();
  233. var scale = this.scale;
  234. scale[0] += diff[0];
  235. scale[1] += diff[0];
  236. scale[2] += diff[0];
  237. if ( scale[0] != 0 && scale[1] != 0 && scale[2] != 0 ) {
  238. this.scale = scale;
  239. }
  240. } //@ sourceURL=node3edit.vwf