# Copyright 2012 United States Government, as represented by the Secretary of Defense, Under # Secretary of Defense (Personnel & Readiness). # # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except # in compliance with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software distributed under the License # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express # or implied. See the License for the specific language governing permissions and limitations under # the License. ## The component representation of a control behavior ## ## @name control.vwf ## @namespace --- methods: mouseInit: linePlaneIntersection: events: pointerDown: pointerMove: pointerUp: scripts: - | this.initialize = function() { this.planeNormal = undefined; this.planePoint = undefined; this.cameraTranslation = undefined; this.pointerVector = goog.vec.Vec3.create(); } // Sets up the mouse pointer information used for dragging. this.mouseInit = function() { this.input = { "pointerInfo": undefined, "pickInfo": undefined, "previous": { "pointerInfo": undefined, "pickInfo": undefined, }, update: function( pointerInfo, pickInfo ){ if(!this.previous.pointerInfo) { this.previous.pointerInfo = this.pointerInfo; this.previous.pickInfo = this.pickInfo; } this.pointerInfo = pointerInfo; this.pickInfo = pickInfo; }, clear: function(){ this.previous.pointerInfo = undefined; this.previous.pickInfo = undefined; this.pointerInfo = undefined; this.pickInfo = undefined; }, change: function() { var ret = [ 0, 0 ] if ( this.pointerInfo && this.previous.pointerInfo ) { ret[0] = this.pointerInfo.position[0] - this.previous.pointerInfo.position[0]; ret[1] = this.pointerInfo.position[1] - this.previous.pointerInfo.position[1]; } return ret; } }; } this.linePlaneIntersection = function( planePoint, planeNormal, linePoint, lineVec ){ var intersection, dotNumerator, dotDenominator; var vector = goog.vec.Vec3.create(); //calculate the distance between the linePoint and the line-plane intersection point var temp = goog.vec.Vec3.create(); goog.vec.Vec3.subtract( planePoint, linePoint, temp ); dotNumerator = goog.vec.Vec3.dot( temp, planeNormal ); dotDenominator = goog.vec.Vec3.dot( lineVec, planeNormal ); //line and plane are not parallel if( dotDenominator != 0.0 ){ intersection = goog.vec.Vec3.create(); //create a vector from the linePoint to the intersection point goog.vec.Vec3.scale( lineVec, dotNumerator / dotDenominator, vector ); //get the coordinates of the line-plane intersection point goog.vec.Vec3.add( linePoint, vector, intersection ); } return intersection; } this.pointerDown = function( pointerInfo, pickInfo ) { if ( this.input === undefined ) this.mouseInit(); this.input.clear(); this.input.pointerInfo = pointerInfo; this.input.pickInfo = pickInfo; this.planeNormal = goog.vec.Vec3.createFloat32FromArray( pickInfo.globalNormal ); this.planePoint = goog.vec.Vec3.createFloat32FromArray( pickInfo.globalPosition ); this.cameraTranslation = goog.vec.Vec3.createFloat32FromArray( pickInfo.globalSource ); goog.vec.Vec3.subtract( this.planePoint, this.cameraTranslation, this.pointerVector ); } this.pointerUp = function( pointerInfo, pickInfo ) { this.input.clear(); } this.pointerMove = function( pointerInfo, pickInfo ) { // update the camera location and the view vector this.cameraTranslation = goog.vec.Vec3.createFloat32FromArray( pickInfo.globalSource ); // we need a way of getting the pointerVector here without the using the // globalPosition, because that might not have a value if ( pickInfo.pointerVector ) { this.pointerVector = goog.vec.Vec3.createFloat32FromArray( pickInfo.pointerVector ); } else { var hitPoint = goog.vec.Vec3.createFloat32FromArray( pickInfo.globalPosition ); goog.vec.Vec3.subtract( hitPoint, this.cameraTranslation, this.pointerVector ); goog.vec.Vec3.normalize( this.pointerVector, this.pointerVector ); } var newPlaneIntersection = this.linePlaneIntersection( this.planePoint, this.planeNormal, this.cameraTranslation, this.pointerVector ); if ( newPlaneIntersection !== undefined ) { var translation = goog.vec.Vec3.createFloat32FromArray( this.parent.translation ); var diff = goog.vec.Vec3.create(); goog.vec.Vec3.subtract( newPlaneIntersection, this.planePoint, diff ); this.parent.translation = goog.vec.Vec3.add( translation, diff, goog.vec.Vec3.create() ); this.planePoint = goog.vec.Vec3.clone( newPlaneIntersection ); } this.input.previous.pointerInfo = pointerInfo; this.input.previous.pickInfo = pickInfo; } //@ sourceURL=planarDrag.vwf