/* <copyright>
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.
</copyright> */
///////////////////////////////////////////////////////////////////////
// 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<len; i++) {
elt = documentRootChildren[i];
plane = this.addElement(elt);
if(adjustScrollOffsets) {
l = plane._rect.m_left - docLeft;
t = plane._rect.m_top - docTop;
if(l < minLeft) {
minLeft = l;
stage.minLeftElement = elt;
}
if(t < minTop) {
minTop = t;
stage.minTopElement = elt;
}
}
}
if(minLeft !== initL) {
stage.userPaddingLeft = (minLeft < 0) ? minLeft : 0;
}
if(minTop !== initT) {
stage.userPaddingTop = (minTop < 0) ? minTop : 0;
}
}
}
},
handleElementAdded: {
value: function(event) {
var elements = event.detail;
if(Array.isArray(elements)) {
elements.forEach(function(element) {
this.addElement(element);
|