planarDrag.vwf.yaml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 control behavior
  14. ##
  15. ## @name control.vwf
  16. ## @namespace
  17. ---
  18. methods:
  19. mouseInit:
  20. linePlaneIntersection:
  21. events:
  22. pointerDown:
  23. pointerMove:
  24. pointerUp:
  25. scripts:
  26. - |
  27. this.initialize = function() {
  28. this.planeNormal = undefined;
  29. this.planePoint = undefined;
  30. this.cameraTranslation = undefined;
  31. this.pointerVector = goog.vec.Vec3.create();
  32. }
  33. // Sets up the mouse pointer information used for dragging.
  34. this.mouseInit = function() {
  35. this.input = {
  36. "pointerInfo": undefined,
  37. "pickInfo": undefined,
  38. "previous": {
  39. "pointerInfo": undefined,
  40. "pickInfo": undefined,
  41. },
  42. update: function( pointerInfo, pickInfo ){
  43. if(!this.previous.pointerInfo) {
  44. this.previous.pointerInfo = this.pointerInfo;
  45. this.previous.pickInfo = this.pickInfo;
  46. }
  47. this.pointerInfo = pointerInfo;
  48. this.pickInfo = pickInfo;
  49. },
  50. clear: function(){
  51. this.previous.pointerInfo = undefined;
  52. this.previous.pickInfo = undefined;
  53. this.pointerInfo = undefined;
  54. this.pickInfo = undefined;
  55. },
  56. change: function() {
  57. var ret = [ 0, 0 ]
  58. if ( this.pointerInfo && this.previous.pointerInfo ) {
  59. ret[0] = this.pointerInfo.position[0] - this.previous.pointerInfo.position[0];
  60. ret[1] = this.pointerInfo.position[1] - this.previous.pointerInfo.position[1];
  61. }
  62. return ret;
  63. }
  64. };
  65. }
  66. this.linePlaneIntersection = function( planePoint, planeNormal, linePoint, lineVec ){
  67. var intersection, dotNumerator, dotDenominator;
  68. var vector = goog.vec.Vec3.create();
  69. //calculate the distance between the linePoint and the line-plane intersection point
  70. var temp = goog.vec.Vec3.create();
  71. goog.vec.Vec3.subtract( planePoint, linePoint, temp );
  72. dotNumerator = goog.vec.Vec3.dot( temp, planeNormal );
  73. dotDenominator = goog.vec.Vec3.dot( lineVec, planeNormal );
  74. //line and plane are not parallel
  75. if( dotDenominator != 0.0 ){
  76. intersection = goog.vec.Vec3.create();
  77. //create a vector from the linePoint to the intersection point
  78. goog.vec.Vec3.scale( lineVec, dotNumerator / dotDenominator, vector );
  79. //get the coordinates of the line-plane intersection point
  80. goog.vec.Vec3.add( linePoint, vector, intersection );
  81. }
  82. return intersection;
  83. }
  84. this.pointerDown = function( pointerInfo, pickInfo ) {
  85. if ( this.input === undefined ) this.mouseInit();
  86. this.input.clear();
  87. this.input.pointerInfo = pointerInfo;
  88. this.input.pickInfo = pickInfo;
  89. this.planeNormal = goog.vec.Vec3.createFloat32FromArray( pickInfo.globalNormal );
  90. this.planePoint = goog.vec.Vec3.createFloat32FromArray( pickInfo.globalPosition );
  91. this.cameraTranslation = goog.vec.Vec3.createFloat32FromArray( pickInfo.globalSource );
  92. goog.vec.Vec3.subtract( this.planePoint, this.cameraTranslation, this.pointerVector );
  93. }
  94. this.pointerUp = function( pointerInfo, pickInfo ) {
  95. this.input.clear();
  96. }
  97. this.pointerMove = function( pointerInfo, pickInfo ) {
  98. // update the camera location and the view vector
  99. this.cameraTranslation = goog.vec.Vec3.createFloat32FromArray( pickInfo.globalSource );
  100. // we need a way of getting the pointerVector here without the using the
  101. // globalPosition, because that might not have a value
  102. if ( pickInfo.pointerVector ) {
  103. this.pointerVector = goog.vec.Vec3.createFloat32FromArray( pickInfo.pointerVector );
  104. } else {
  105. var hitPoint = goog.vec.Vec3.createFloat32FromArray( pickInfo.globalPosition );
  106. goog.vec.Vec3.subtract( hitPoint, this.cameraTranslation, this.pointerVector );
  107. goog.vec.Vec3.normalize( this.pointerVector, this.pointerVector );
  108. }
  109. var newPlaneIntersection = this.linePlaneIntersection( this.planePoint,
  110. this.planeNormal, this.cameraTranslation, this.pointerVector );
  111. if ( newPlaneIntersection !== undefined ) {
  112. var translation = goog.vec.Vec3.createFloat32FromArray( this.parent.translation );
  113. var diff = goog.vec.Vec3.create();
  114. goog.vec.Vec3.subtract( newPlaneIntersection, this.planePoint, diff );
  115. this.parent.translation = goog.vec.Vec3.add( translation, diff, goog.vec.Vec3.create() );
  116. this.planePoint = goog.vec.Vec3.clone( newPlaneIntersection );
  117. }
  118. this.input.previous.pointerInfo = pointerInfo;
  119. this.input.previous.pickInfo = pickInfo;
  120. } //@ sourceURL=planarDrag.vwf