/*
Copyright (c) 2012, Motorola Mobility LLC.
All Rights Reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Motorola Mobility LLC nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
var Montage = require("montage/core/core").Montage,
Component = require("montage/ui/component").Component,
viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils,
vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils,
drawUtils = require("js/helper-classes/3D/draw-utils").DrawUtils;
var ToolHandle = exports.ToolHandle = Montage.create(Component, {
_x: {
value: 0,
writable: true
},
_y: {
value: 0,
writable: true
},
_width: {
value: 4,
writable: true
},
_height: {
value: 4,
writable: true
},
VERTEX_HIT_RAD: {
value: 4,
writable: true
},
_cursor: {
value: "default",
writable: true
},
_strokeStyle: {
value: 'rgba(255,255,255,0.6)'
},
_fillStyle: {
value: 'rgba(0,0,0,1)'
},
init: {
value: function (cursorStyle) {
this._cursor = cursorStyle;
}
},
draw: {
value: function(x, y) {
var context = this.application.ninja.stage.drawingContext;
context.save();
context.fillStyle = this._fillStyle;
this._x = x - this._width/2;
this._y = y - this._height/2;
context.fillRect(this._x, this._y, this._width, this._height);
context.strokeStyle = this._strokeStyle;
this._x = x - this._width/2 - 1;
this._y = y - this._height/2 - 1;
context.strokeRect(this._x, this._y, this._width + 2, this._height + 2);
context.restore();
}
},
collidesWithPoint: {
value:function (x, y) {
if(x < (this._x - this.VERTEX_HIT_RAD)) return false;
if(x > (this._x + this._width + this.VERTEX_HIT_RAD)) return false;
if(y < this._y - this.VERTEX_HIT_RAD) return false;
if(y > (this._y + this._height + this.VERTEX_HIT_RAD)) return false;
return true;
}
}
});
exports.RotateHandle = Montage.create(ToolHandle, {
_originL: {
value: null,
writable: true
},
_origin: {
value: null,
writable: true
},
_dirVec: {
value: null,
writable: true
},
_dirVecL: {
value: null,
writable: true
},
_radius: {
value: 50,
writable: true
},
_transformCenterRadius: {
value: 5,
writable: true
},
_cursor: {
value: "default",
writable: true
},
_strokeStyle: {
value: 'rgba(255,0,255,1)'
},
_axis: {
value: null,
writable: true
},
_lineWidth: {
value: 2
},
_fillStyle: {
value: 'rgba(255,0,255,1)'
},
_nTriangles: {
value: 30,
writable: true
},
_rotMat: {
value: null,
writable: true
},
_rotMatInv: {
value: null,
writable: true
},
_planeEq: {
value: null,
writable: true
},
_matW: {
value: null,
writable: true
},
_matL: {
value: null,
writable: true
},
_vec: {
value: null,
writable: true
},
_vec2: {
value: null,
writable: true
},
_vec3: {
value: null,
writable: true
},
_dragPlane: {
value: null,
writable: true
},
init: {
value: function (cursorStyle, color, axis) {
this._cursor = cursorStyle;
this._strokeStyle = color;
this._fillStyle = color;
this._axis = axis;
switch(this._axis)
{
case "x":
this._vec = [1, 0, 0];
this._vec2 = [0, 1, 0];
this._vec3 = [0, 0, 1];
break;
case "y":
this._vec = [0, 1, 0];
this._vec2 = [1, 0, 0];
this._vec3 = [0, 0, 1];
break;
case "z":
this._vec = [0, 0, 1];
this._vec2 = [1, 0, 0];
this._vec3 = [0, 1, 0];
break;
}
// get a matrix to rotate a point around the circle
var angle = 2.0*Math.PI/Number(this._nTriangles);
this._rotMat = Matrix.RotationZ( angle );
this._rotMatInv = glmat4.inverse(this._rotMat, []);
}
},
draw: {
value: function(base, item, inLocalMode) {
var context = this.application.ninja.stage.drawingContext;
context.save();
context.strokeStyle = this._strokeStyle;
context.fillStyle = this._fillStyle;
context.lineWidth = this._lineWidth;
context.shadowBlur = 2;
context.shadowColor = "rgba(0, 0, 0, 0.8)";
var pointOnElt = base.slice(0);
// this._origin = viewUtils.localToGlobal(pointOnElt, item);
this._origin = pointOnElt;
var viewMat = viewUtils.getMatrixFromElement(this.application.ninja.currentDocument.model.documentRoot);
var transMat = viewMat.slice(0);
if(inLocalMode)
{
var objMat = viewUtils.getMatrixFromElement(item);
glmat4.multiply(viewMat, objMat, transMat);
}
this._planeEq = MathUtils.transformVector(this._vec, transMat);
this._planeEq2 = MathUtils.transformVector(this._vec2, transMat);
this._planeEq3 = MathUtils.transformVector(this._vec3, transMat);
var viewVec = [0, 0, 1];
var angle2 = MathUtils.getAngleBetweenVectors(this._planeEq2, viewVec);
var angle3 = MathUtils.getAngleBetweenVectors(this._planeEq3, viewVec);
if(angle3 < angle2)
{
this._dirVec = vecUtils.vecNormalize(3, this._planeEq2, this._radius);
}
else
{
this._dirVec = vecUtils.vecNormalize(3, this._planeEq3, this._radius);
}
this._matW = drawUtils.getPlaneToWorldMatrix(this._planeEq, this._origin);
this._matL = glmat4.inverse(this._matW, []);
this._originL = MathUtils.transformPoint(this._origin, this._matL);
this._planeEq[3] = -vecUtils.vecDot(3, this._planeEq, this._origin);
this._dirVecL = MathUtils.transformPoint(this._dirVec, this._matL);
context.beginPath();
var pt = [this._radius, 0.0, 0.0];
var pts;
for (var i=0; i= -0.1) )
{
return 1;
}
else if ( (t <= 1) && (t >= -1) )
{
return 2;
}
}
return 0;
}
localPt = MathUtils.transformPoint(localPt, this._matL);
var theta = Math.atan2(localPt[1], localPt[0]);
var xC = this._transformCenterRadius*Math.cos(theta);
var yC = this._transformCenterRadius*Math.sin(theta);
var ptOnCircle = [xC, yC, 0];
var dist = vecUtils.vecDist( 2, localPt, ptOnCircle );
if ( dist <= 5 )
{
return 1;
}
xC = this._radius*Math.cos(theta);
yC = this._radius*Math.sin(theta);
ptOnCircle = [xC, yC, 0];
dist = vecUtils.vecDist( 2, localPt, ptOnCircle );
if ( dist <= 5 )
{
return 2;
}
return 0;
}
},
drawShadedAngle: {
value: function(angle, localPt) {
var theta = Math.atan2(localPt[1], localPt[0]);
var xC = this._radius*Math.cos(theta);
var yC = this._radius*Math.sin(theta);
var pt = [xC, yC, 0];
var context = this.application.ninja.stage.drawingContext;
context.save();
context.strokeStyle = "rgba(0,0,0,1)";
context.lineWidth = 2;
context.fillStyle = this._fillStyle;
context.globalAlpha = 0.2;
context.beginPath();
context.moveTo(this._origin[0], this._origin[1]);
var pts = MathUtils.transformPoint(pt, this._matW);
context.lineTo(pts[0], pts[1]);
var n = Math.ceil(Math.abs( (this._nTriangles*angle) / (2*Math.PI) ) );
for (var i=0; i= 0) )
return 2;
}
return 0;
}
}
});