/* This file contains proprietary software owned by Motorola Mobility, Inc.
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
*/ /////////////////////////////////////////////////////////////////////// // 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"], writable: 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){ 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; for(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; } } } // restore the root ID this.viewUtils.setRootElement (saveRoot ); context.beginPath(); var x0 = minPt[0], y0 = minPt[1], z0 = minPt[2], x1 = maxPt[0], y1 = maxPt[1], z1 = maxPt[2]; this._selectionCtr = [x0, y0, z0]; this._selectionCtr[0] += (x1-x0)/2; this._selectionCtr[1] += (y1-y0)/2; this._selectionCtr[2] += (z1-z0)/2; // 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(); } } } } }, drawElementNormal: { value: function( elt ) { if (!this.isDrawingElementNormal()) return; // set the element to be the viewport object - temporarily this.viewUtils.pushViewportObj( elt ); // save the source space object and set to the target object var saveSource = this._sourceSpaceElt; this._sourceSpaceElt = elt; // temporarily set the line color var saveColor = this._lineColor; this._lineColor = "blue"; var base = this.viewUtils.getCenterOfProjection(); base[2] = 1; // Z - make it just off the plane to avoid numerical issues var zAxis = base.slice(0); zAxis[2] += 50; this.moveTo( base ); this.lineTo( zAxis ); // draw the arrowhead var headWidth = 6; var head = MathUtils.interpolateLine3D( base, zAxis, 0.7 ); var p0 = head.slice(0); p0[1] += headWidth; this.drawLine( zAxis, p0 ); var p1 = head.slice(0); p1[0] += headWidth; this.drawLine( zAxis, p1 ); var p2 = head.slice(0); p2[1] -= headWidth; this.drawLine( zAxis, p2 ); var p3 = head.slice(0); p3[0] -= headWidth; this.drawLine( zAxis, p3 ); this.moveTo( p0 ); this.lineTo( p1 ); this.lineTo( p2 ); this.lineTo( p3 ); this.lineTo( p0 ); // restore the state this.viewUtils.popViewportObj(); this._lineColor = saveColor; this._sourceSpaceElt = saveSource; } }, draw3DCompass : { value: function() { // set the element to be the viewport object - temporarily var tmpCanvas = this.application.ninja.stage.canvas; var tmpStage = this.application.ninja.currentDocument.model.documentRoot; // this.viewUtils.pushViewportObj( tmpCanvas ); // save the source space object and set to the target object var saveSource = this._sourceSpaceElt; this._sourceSpaceElt = tmpStage; // temporarily set the line color var saveColor = this._lineColor; var saveLineWidth = this._lineWidth; var origLeft = 60; var origTop = tmpCanvas.height - 60; var mat = this.viewUtils.getMatrixFromElement( this._sourceSpaceElt ); var tMat = Matrix.Translation([origLeft,origTop,0]); mat[12] = 0; mat[13] = 0; mat[14] = 0; //var resMat = tMat.multiply(mat); var resMat = glmat4.multiply( tMat, mat, [] ); var origin = [0,0,0,1]; var zoomFactor = this.application.ninja.documentBar.zoomFactor/100.0; var arrowSize = 50 / zoomFactor; var xAxis = [arrowSize,0,0,1]; //var rO = resMat.multiply(origin); var rO = glmat4.multiplyVec3( resMat, origin, []); //var xO = resMat.multiply(xAxis); var xO = glmat4.multiplyVec3( resMat, xAxis, []); var yAxis = [0,arrowSize,0,1]; var yO = glmat4.multiplyVec3( resMat, yAxis, []); var zAxis = [0,0,arrowSize,1]; var zO = glmat4.multiplyVec3( resMat, zAxis, []); var saveContext = this.getDrawingSurfaceElement(); //this.setDrawingSurfaceElement(window.stageManager.layoutCanvas); this.setDrawingSurfaceElement(this.application.ninja.stage.layoutCanvas); // clear just the 3d compass area this._drawingContext.save(); this._drawingContext.rect(10, origTop-60, 100, 110); this._drawingContext.clip(); this._drawingContext.lineWidth = 2.0; this._drawingContext.beginPath(); this._drawingContext.strokeStyle = "red"; this._drawingContext.moveTo(rO[0], rO[1]); this._drawingContext.lineTo(xO[0], xO[1]); this._drawingContext.closePath(); this._drawingContext.stroke(); this.drawArrowHead(rO, xO); this._drawingContext.beginPath(); this._drawingContext.strokeStyle = "green"; this._drawingContext.moveTo(rO[0], rO[1]); this._drawingContext.lineTo(yO[0], yO[1]); this._drawingContext.closePath(); this._drawingContext.stroke(); this.drawArrowHead(rO, yO); this._drawingContext.beginPath(); this._drawingContext.strokeStyle = "blue"; this._drawingContext.moveTo(rO[0], rO[1]); this._drawingContext.lineTo(zO[0], zO[1]); this._drawingContext.closePath(); this._drawingContext.stroke(); this.drawArrowHead(rO, zO); // restore the state // this.viewUtils.popViewportObj(); this._drawingContext.restore(); this.setDrawingSurfaceElement(saveContext); this._lineColor = saveColor; this._lineWidth = saveLineWidth; this._sourceSpaceElt = saveSource; } }, drawArrowHead : { value: function(base, onAxis) { var headWidth = 6; // draw the arrowhead var head = MathUtils.interpolateLine3D( base, onAxis, 0.7 ); p0 = head.slice(0); p0[1] += headWidth; p1 = head.slice(0); p1[0] += headWidth; p2 = head.slice(0); p2[1] -= headWidth; p3 = head.slice(0); p3[0] -= headWidth; this._drawingContext.beginPath(); this._drawingContext.moveTo(base[0], base[1]); this._drawingContext.lineTo(onAxis[0], onAxis[1]); this._drawingContext.moveTo(onAxis[0], onAxis[1]); this._drawingContext.lineTo(p0[0], p0[1]); this._drawingContext.moveTo(onAxis[0], onAxis[1]); this._drawingContext.lineTo(p1[0], p1[1]); this._drawingContext.moveTo(onAxis[0], onAxis[1]); this._drawingContext.lineTo(p2[0], p2[1]); this._drawingContext.moveTo(onAxis[0], onAxis[1]); this._drawingContext.lineTo(p3[0], p3[1]); this._drawingContext.moveTo( p0[0], p0[1] ); this._drawingContext.lineTo( p1[0], p1[1] ); this._drawingContext.lineTo( p2[0], p2[1] ); this._drawingContext.lineTo( p3[0], p3[1] ); this._drawingContext.lineTo( p0[0], p0[1] ); this._drawingContext.closePath(); this._drawingContext.stroke(); } }, drawGridAxes : { value: function (ctr, width, height, headSize) { this._drawingContext.lineWidth = 2.0; this._lineColor = "red"; var pt0 = ctr.slice(0), pt1 = pt0.slice(0); pt1[0] += width; this.moveTo(pt0); this.lineTo(pt1); var pt2 = pt1.slice(0); pt2[0] -= headSize; pt2[1] += headSize; this.lineTo(pt2); //this.moveTo(pt1); pt2[1] -= 2 * headSize; this.lineTo(pt2); this.lineTo(pt1); this._lineColor = "green"; pt1 = pt0.slice(0); pt1[1] += height; this.moveTo(pt0); this.lineTo(pt1); pt2 = pt1.slice(0); pt2[1] -= headSize; pt2[0] += headSize; this.lineTo(pt2); //this.moveTo(pt1); pt2[0] -= 2 * headSize; this.lineTo(pt2); this.lineTo(pt1); this._lineColor = "blue"; pt1 = pt0.slice(0); pt1[2] += height < width ? height : width; this.moveTo(pt0); this.lineTo(pt1); pt2 = pt1.slice(0); pt2[2] -= headSize; pt2[1] += headSize; this.lineTo(pt2); //this.moveTo(pt1); pt2[1] -= 2 * headSize; this.lineTo(pt2); this.lineTo(pt1); } } });