/*
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.
*/
///////////////////////////////////////////////////////////////////////
// Class DrawUtils
// Overlay drawing utility functions
///////////////////////////////////////////////////////////////////////
var Montage = require("montage/core/core").Montage,
Component = require("montage/ui/component").Component;
var vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils;
var Rectangle = require("js/helper-classes/3D/rectangle").Rectangle;
var StageLine = require("js/helper-classes/3D/StageLine").StageLine;
var DrawUtils = exports.DrawUtils = Montage.create(Component, {
///////////////////////////////////////////////////////////////////////
// Instance variables
///////////////////////////////////////////////////////////////////////
viewUtils: { value: null, writable: true },
snapManager: { value: null },
ElementPlanes : { value: null, writable: true },
// the drawing surface (a canvas)
_drawingSurfaceElt : { value: null, writable: true },
_drawingContext : { value: null, writable: true },
// color to draw the lines
_lineColor : { value: "black", writable: true},
// define a stack for quickly setting graphics states and restoring them
_stateArray : { value: [], writable: true },
// save references to the grid lines for quick redraw
_gridLineArray : {value: [], writable: true },
// state for moveTo, lineTo
_curPt : { value: null, writable: true },
_curVis : { value: null, writable: true },
// the element that defines the coordinate system for the displayed lines
_sourceSpaceElt : { value: null, writable: true },
// maintain a list of objects to hide against
_eltArray : {value: [], writable: true },
// maintain a list of the planes to test against
_planesArray : {value: [], writable: true },
// the working plane.
// a grid may be drawn aligned with this working plane
_workingPlane : { value: null, writable: true },
// save some parameters about the grid.
// these parameters are set when the grid is drawn
_gridHorizontalSpacing : {value: 50, writable: true },
_gridVerticalSpacing : {value: 50, writable: true },
_gridHorizontalLineCount : {value:10, writable: true },
_gridVerticalLineCount : {value:0, writable: true },
_gridOrigin : {value: null, writable: true },
drawXY : {value: false, writable: true },
drawXZ : {value: false, writable: true },
drawYZ : {value: false, writable: true },
drawElementN : {value: false, writable: true },
_selectionCtr : {value: null, writable: true },
// Properties that require element planes to be updated
_updatePlaneProps : {value: ["matrix", "left", "top", "width", "height", "border", "border-width", "border-style", "-webkit-transform"], writable: false },
_recalculateScrollOffsets : { value: false },
///////////////////////////////////////////////////////////////////////
// Property accessors
///////////////////////////////////////////////////////////////////////
setDrawingSurfaceElement : { value: function( s ) { this._drawingSurfaceElt = s; if (s) this._drawingContext = s.getContext("2d"); }},
getDrawingSurfaceElement : { value: function() { return this._drawingSurfaceElt; }},
getDrawingContext : { value: function() { return this._drawingContext; }},
setSourceSpaceElement : { value: function(ss) { this._sourceSpaceElt = ss; }},
getSourceSpaceElement : { value: function() { return this._sourceSpaceElt; }},
getWorkingPlane : { value: function() { return this._workingPlane; }},
setWorkingPlane : { value: function (wp) { this._workingPlane = wp; }},
getGridHorizontalSpacing : { value: function() { return this._gridHorizontalSpacing; }},
getGridVerticalSpacing : { value: function() { return this._gridVerticalSpacing; }},
getGridHorizontalLineCount : { value: function() { return this._gridHorizontalLineCount; }},
getGridVerticalLineCount : { value: function() { return this._gridVerticalLineCount; }},
getGridOrigin : { value: function() { return this._gridOrigin.slice(0); }},
isDrawingGrid : { value: function() { return this.drawXY || this.drawYZ || this.drawXZ; }},
isDrawingElementNormal : { value: function() { return this.drawElementN }},
getLineColor : { value: function() { return this._lineColor; }},
setLineColor : { value: function( color ) { this._lineColor = color; }},
getLineWidth : { value: function() { return this._drawingContext.lineWidth; }},
setLineWidth : { value: function( w ) { this._drawingContext.lineWidth = w; }},
initialize: {
value: function() {
this._gridOrigin = [0,0]; // 2D plane space point
this.eventManager.addEventListener("elementAdded", this, false);
this.eventManager.addEventListener("elementsRemoved", this, false);
this.eventManager.addEventListener("elementChange", this, false);
this.eventManager.addEventListener("elementChanging", this, false);
this.eventManager.addEventListener("elementReplaced", this, false);
}
},
initializeFromDocument:{
value:function(adjustScrollOffsets, useStageValues){
var i,
documentRootChildren = this.application.ninja.currentDocument.model.views.design.getLiveNodeList(true),
stage = this.application.ninja.stage,
len = documentRootChildren.length;
//initialize with current document
this._eltArray = [];
this._planesArray = [];
this.setDrawingSurfaceElement(stage.gridCanvas);
this.setSourceSpaceElement( this.application.ninja.currentDocument.model.documentRoot);
this.setWorkingPlane( [0,0,1,0] );
//Loop through all the top-level children of the current document and call drawUtils.addElement on them
if(len > 0) {
var initL = 0,
initT = 0,
minLeft = 0,
minTop = 0,
docLeft = stage.documentOffsetLeft,
docTop = stage.documentOffsetTop,
l,
t,
plane,
elt;
if(useStageValues) {
initL = stage.userPaddingLeft;
initT = stage.userPaddingTop;
minLeft = stage.templateLeft;
minTop = stage.templateTop;
this._recalculateScrollOffsets = false;
}
for(i=0; i minLeft)) {
this._recalculateScrollOffsets = true;
}
if(t < minTop) {
minTop = t;
stage.minTopElement = elt;
} else if((elt === stage.minTopElement) && (t > minTop)) {
this._recalculateScrollOffsets = true;
}
}
}
}
if(adjustStagePadding) {
if(this._recalculateScrollOffsets && !isChanging) {
this.initializeFromDocument(true, true);
changed = true;
} else {
if(minLeft !== stage.userPaddingLeft) {
stage.userPaddingLeft = minLeft;
changed = true;
}
if(minTop !== stage.userPaddingTop) {
stage.userPaddingTop = minTop;
changed = true;
}
}
}
if(!changed) {
// If we didn't already set userPaddingTop or userPaddingLeft, force stage to redraw
//this.snapManager._isCacheInvalid = true;
// stage.draw3DInfo = true;
stage.needsDraw = true;
}
// TODO - Remove this once all stage drawing is consolidated into a single draw cycle
if(!isChanging && this.application.ninja.toolsData.selectedToolInstance.captureSelectionDrawn) {
this.application.ninja.toolsData.selectedToolInstance.captureSelectionDrawn(null);
}
}
}
},
///////////////////////////////////////////////////////////////////////
// Methods
///////////////////////////////////////////////////////////////////////
addElement: {
value: function( elt ) {
// check if we already know about this object
var n = this._eltArray.length;
for (var i=0; i 3) pt.pop();
var z = pt[2];
var n = this._planesArray.length;
var vis = 0;
for (var i=0; i plane.getZMax()) continue;
// test for containment in the polygon bounds
var contain = MathUtils.boundaryContainsPoint( plane.getBoundaryPoints(), pt, plane.isBackFacing() );
if (contain == MathUtils.OUTSIDE) continue;
if (contain == MathUtils.ON) continue;
// shoot a ray from the point in the +Z direction to get the z value of the plane
var vec = [0,0,1];
var planeEq = plane.getPlaneEq();
var ptOnPlane = MathUtils.vecIntersectPlane( pt, vec, planeEq );
if (ptOnPlane)
{
// in keeping with the convention that a point "on" a face is outside the element,
// check that case.
//if (MathUtils.fpCmp(pt[2], ptOnPlane[2]) == 0) continue;
// if the point is behind the plane, increase the visibility
if (MathUtils.fpCmp(pt[2],ptOnPlane[2]) <= 0) vis++;
}
}
return vis;
}
},
moveTo : {
value: function( pt ) {
if (this._sourceSpaceElt)
pt = this.viewUtils.localToGlobal( pt, this._sourceSpaceElt );
this._curPt = pt.slice(0);
this._curVis = this.getVisibilityAtPoint( pt );
}
},
lineTo :
{
value: function( pt )
{
if (this._sourceSpaceElt)
pt = this.viewUtils.localToGlobal( pt, this._sourceSpaceElt );
var line = Object.create(StageLine, {});
line.setPoints( this._curPt, pt );
line.setVisibility( this._curVis );
// find all the plane intersections
this.getLineIntersections( line );
// draw the line
this._curVis = this.drawIntersectedLine( line, this._drawingContext );
this._curPt = pt.slice(0);
}
},
drawLine :
{
value: function( pt0, pt1 )
{
if (this._drawingContext)
{
// transform the points from local object space to world space
if (this._sourceSpaceElt)
{
pt0 = this.viewUtils.localToGlobal( pt0, this._sourceSpaceElt );
pt1 = this.viewUtils.localToGlobal( pt1, this._sourceSpaceElt );
}
// create the line structure
var line = Object.create(StageLine, {});
line.setPoints( pt0, pt1 );
// find all the plane intersections
this.getLineIntersections( line );
// get the starting visibility
var vis = this.getVisibilityAtPoint( pt0 );
line.setVisibility( vis );
// draw the line
this._curVis = this.drawIntersectedLine( line, this._drawingContext );
}
}
},
drawIntersectedLine :
{
value: function( line )
{
this._drawingContext.strokeStyle = this._lineColor;
this._drawingContext.beginPath();
// get the 2 enpoints of the line
var pt0 = line.getPoint0(),
pt1 = line.getPoint1();
// find the visibility at the start point
var vis = line.getVisibility();
if (vis == 0)
{
this._drawingContext.strokeStyle = this._lineColor;
this._drawingContext.beginPath();
this._drawingContext.moveTo( pt0[0], pt0[1] );
}
// go through each intersection
var n = line.getIntersectionCount();
var t = 0.0;
var iRec = line.getIntersectionList();
for (var i=0; i maxPt[0]) maxPt[0] = x;
if (y < minPt[1]) minPt[1] = y;
if (y > maxPt[1]) maxPt[1] = y;
if (z < minPt[2]) minPt[2] = z;
if (z > maxPt[2]) maxPt[2] = z;
}
}
}
context.beginPath();
var x0 = minPt[0], y0 = minPt[1], z0 = minPt[2],
x1 = maxPt[0], y1 = maxPt[1], z1 = maxPt[2];
var stageWorldCtr = [ 0.5*(x0 + x1), 0.5*(y0 + y1), 0.5*(z0 + z1) ];
this._selectionCtr = MathUtils.transformAndDivideHomogeneousPoint( stageWorldCtr, ssMat );
// get the 8 corners of the parallelpiped in world space
var wc = new Array(); // wc == world cube
wc.push( this.viewUtils.localToGlobal2( [x0,y0,z1], ssMat ) );
wc.push( this.viewUtils.localToGlobal2( [x0,y1,z1], ssMat ) );
wc.push( this.viewUtils.localToGlobal2( [x1,y1,z1], ssMat ) );
wc.push( this.viewUtils.localToGlobal2( [x1,y0,z1], ssMat ) );
wc.push( this.viewUtils.localToGlobal2( [x0,y0,z0], ssMat ) );
wc.push( this.viewUtils.localToGlobal2( [x0,y1,z0], ssMat ) );
wc.push( this.viewUtils.localToGlobal2( [x1,y1,z0], ssMat ) );
wc.push( this.viewUtils.localToGlobal2( [x1,y0,z0], ssMat ) );
// determine the signs of the normals of the faces relative to the view direction.
var front = -MathUtils.fpSign( vecUtils.vecCross(3, vecUtils.vecSubtract(3,wc[2],wc[1]), vecUtils.vecSubtract(3,wc[0],wc[1]))[2] ),
right = -MathUtils.fpSign( vecUtils.vecCross(3, vecUtils.vecSubtract(3,wc[6],wc[2]), vecUtils.vecSubtract(3,wc[3],wc[2]))[2] ),
back = -MathUtils.fpSign( vecUtils.vecCross(3, vecUtils.vecSubtract(3,wc[5],wc[6]), vecUtils.vecSubtract(3,wc[7],wc[6]))[2] ),
left = -MathUtils.fpSign( vecUtils.vecCross(3, vecUtils.vecSubtract(3,wc[1],wc[5]), vecUtils.vecSubtract(3,wc[4],wc[5]))[2] ),
top = -MathUtils.fpSign( vecUtils.vecCross(3, vecUtils.vecSubtract(3,wc[3],wc[0]), vecUtils.vecSubtract(3,wc[4],wc[0]))[2] ),
bottom = -MathUtils.fpSign( vecUtils.vecCross(3, vecUtils.vecSubtract(3,wc[5],wc[1]), vecUtils.vecSubtract(3,wc[2],wc[1]))[2] );
// draw the side faces
var p;
//context.strokeStyle = ((front > 0) || (right > 0)) ? dark : light; context.beginPath();
if ((front > 0) || (right > 0)) {
context.beginPath();
p = this.viewUtils.localToGlobal2( [x1, y0, z1], ssMat ); context.moveTo( p[0], p[1] );
p = this.viewUtils.localToGlobal2( [x1, y1, z1], ssMat ); context.lineTo( p[0], p[1] );
context.closePath(); context.stroke();
}
//context.strokeStyle = ((right > 0) || (back > 0)) ? dark : light; context.beginPath();
if ((right > 0) || (back > 0)) {
context.beginPath();
p = this.viewUtils.localToGlobal2( [x1, y0, z0], ssMat ); context.moveTo( p[0], p[1] );
p = this.viewUtils.localToGlobal2( [x1, y1, z0], ssMat ); context.lineTo( p[0], p[1] );
context.closePath(); context.stroke();
}
//context.strokeStyle = ((back > 0) || (left > 0)) ? dark : light; context.beginPath();
if ((back > 0) || (left > 0)) {
context.beginPath();
p = this.viewUtils.localToGlobal2( [x0, y0, z0], ssMat ); context.moveTo( p[0], p[1] );
p = this.viewUtils.localToGlobal2( [x0, y1, z0], ssMat ); context.lineTo( p[0], p[1] );
context.closePath(); context.stroke();
}
//context.strokeStyle = ((left > 0) || (front > 0)) ? dark : light; context.beginPath();
if ((left > 0) || (front > 0)) {
context.beginPath();
p = this.viewUtils.localToGlobal2( [x0, y0, z1], ssMat ); context.moveTo( p[0], p[1] );
p = this.viewUtils.localToGlobal2( [x0, y1, z1], ssMat ); context.lineTo( p[0], p[1] );
context.closePath(); context.stroke();
}
// draw the top and bottom faces
//context.strokeStyle = ((front > 0) || (top > 0)) ? dark : light; context.beginPath();
if ((front > 0) || (top > 0)) {
context.beginPath();
p = this.viewUtils.localToGlobal2( [x0, y0, z1], ssMat ); context.moveTo( p[0], p[1] );
p = this.viewUtils.localToGlobal2( [x1, y0, z1], ssMat ); context.lineTo( p[0], p[1] );
context.closePath(); context.stroke();
}
//context.strokeStyle = ((top > 0) || (back > 0)) ? dark : light; context.beginPath();
if ((top > 0) || (back > 0)) {
context.beginPath();
p = this.viewUtils.localToGlobal2( [x0, y0, z0], ssMat ); context.moveTo( p[0], p[1] );
p = this.viewUtils.localToGlobal2( [x1, y0, z0], ssMat ); context.lineTo( p[0], p[1] );
context.closePath(); context.stroke();
}
//context.strokeStyle = ((back > 0) || (bottom > 0)) ? dark : light; context.beginPath();
if ((back > 0) || (bottom > 0)) {
context.beginPath();
p = this.viewUtils.localToGlobal2( [x0, y1, z0], ssMat ); context.moveTo( p[0], p[1] );
p = this.viewUtils.localToGlobal2( [x1, y1, z0], ssMat ); context.lineTo( p[0], p[1] );
context.closePath(); context.stroke();
}
//context.strokeStyle = ((bottom > 0) || (front > 0)) ? dark : light; context.beginPath();
if ((bottom > 0) || (front > 0)) {
context.beginPath();
p = this.viewUtils.localToGlobal2( [x0, y1, z1], ssMat ); context.moveTo( p[0], p[1] );
p = this.viewUtils.localToGlobal2( [x1, y1, z1], ssMat ); context.lineTo( p[0], p[1] );
context.closePath(); context.stroke();
}
// and the remaining lines - varying Z
if ((top > 0) || (right > 0)) {
context.beginPath();
p = this.viewUtils.localToGlobal2( [x1, y0, z0], ssMat ); context.moveTo( p[0], p[1] );
p = this.viewUtils.localToGlobal2( [x1, y0, z1], ssMat ); context.lineTo( p[0], p[1] );
context.closePath(); context.stroke();
}
//context.strokeStyle = ((right > 0) || (bottom > 0)) ? dark : light; context.beginPath();
if ((right > 0) || (bottom > 0)) {
context.beginPath();
p = this.viewUtils.localToGlobal2( [x1, y1, z0], ssMat ); context.moveTo( p[0], p[1] );
p = this.viewUtils.localToGlobal2( [x1, y1, z1], ssMat ); context.lineTo( p[0], p[1] );
context.closePath(); context.stroke();
}
//context.strokeStyle = ((bottom > 0) || (left > 0)) ? dark : light; context.beginPath();
if ((bottom > 0) || (left > 0)) {
context.beginPath();
p = this.viewUtils.localToGlobal2( [x0, y1, z0], ssMat ); context.moveTo( p[0], p[1] );
p = this.viewUtils.localToGlobal2( [x0, y1, z1], ssMat ); context.lineTo( p[0], p[1] );
context.closePath(); context.stroke();
}
//context.strokeStyle = ((left > 0) || (top > 0)) ? dark : light; context.beginPath();
if ((left > 0) || (top > 0)) {
context.beginPath();
p = this.viewUtils.localToGlobal2( [x0, y0, z0], ssMat ); context.moveTo( p[0], p[1] );
p = this.viewUtils.localToGlobal2( [x0, y0, z1], ssMat ); context.lineTo( p[0], p[1] );
context.closePath(); context.stroke();
}
} else {
for (i=0; i