From 79fb912bdaa22b8107bbddc93d1779820af80cf9 Mon Sep 17 00:00:00 2001 From: Pushkar Joshi Date: Tue, 3 Apr 2012 14:50:34 -0700 Subject: working File I/O for pen tool created paths and some minor bug fixes (correctly setting the canvas for brush stroke file I/O, correct plane matrix for file I/O) --- js/document/html-document.js | 4 + js/lib/drawing/world.js | 7 +- js/lib/geom/brush-stroke.js | 14 ++-- js/lib/geom/sub-path.js | 181 ++++++++++++++++++------------------------- js/tools/PenTool.js | 9 ++- 5 files changed, 103 insertions(+), 112 deletions(-) diff --git a/js/document/html-document.js b/js/document/html-document.js index 42a7d537..5538229c 100755 --- a/js/document/html-document.js +++ b/js/document/html-document.js @@ -345,6 +345,10 @@ exports.HTMLDocument = Montage.create(TextDocument, { elementModel.pi = "BrushStrokePi"; break; + case root.GEOM_TYPE_CUBIC_BEZIER: + elementModel.selection = "Subpath"; + elementModel.pi = "SubpathPi"; + break; default: console.log( "geometry type not supported for file I/O, " + root.geomType()); diff --git a/js/lib/drawing/world.js b/js/lib/drawing/world.js index 5a054224..a1a5b5ae 100755 --- a/js/lib/drawing/world.js +++ b/js/lib/drawing/world.js @@ -943,7 +943,7 @@ World.prototype.importObjectJSON = function( jObj, parentGeomObj ) { var type = jObj.type; var BrushStroke = require("js/lib/geom/brush-stroke").BrushStroke; - + var SubPath = require("js/lib/geom/sub-path").SubPath; var obj; switch (type) { @@ -962,6 +962,11 @@ World.prototype.importObjectJSON = function( jObj, parentGeomObj ) obj.importJSON( jObj ); break; + case 5: //cubic bezier + obj = new SubPath(); + obj.importJSON(jObj); + break; + case 6: //brush stroke obj = new BrushStroke(); obj.importJSON(jObj); diff --git a/js/lib/geom/brush-stroke.js b/js/lib/geom/brush-stroke.js index e93c9382..2a751a89 100755 --- a/js/lib/geom/brush-stroke.js +++ b/js/lib/geom/brush-stroke.js @@ -541,6 +541,10 @@ var BrushStroke = function GLBrushStroke() { var bboxWidth = bboxMax[0] - bboxMin[0]; var bboxHeight = bboxMax[1] - bboxMin[1]; + if (!this._canvas){ + //set the canvas by querying the world + this._canvas = this.getWorld().getCanvas(); + } if (this._canvas) { var newLeft = Math.round(this._stageWorldCenter[0] - 0.5 * bboxWidth); var newTop = Math.round(this._stageWorldCenter[1] - 0.5 * bboxHeight); @@ -550,7 +554,7 @@ var BrushStroke = function GLBrushStroke() { CanvasController.setProperty(this._canvas, "width", bboxWidth+"px"); CanvasController.setProperty(this._canvas, "height", bboxHeight+"px"); - this._canvas.elementModel.shapeModel.GLWorld.setViewportFromCanvas(this._canvas); + //this._canvas.elementModel.shapeModel.GLWorld.setViewportFromCanvas(this._canvas); } @@ -662,8 +666,8 @@ var BrushStroke = function GLBrushStroke() { this._copyCoordinates3D(this._OrigLocalPoints, retObject.origLocalPoints); //todo retObject.stageWorldCenter = [this._stageWorldCenter[0],this._stageWorldCenter[1],this._stageWorldCenter[2]]; - retObject.planeMat = [this._planeMat[0],this._planeMat[1],this._planeMat[2],this._planeMat[3]]; - retObject.planeMatInv = [this._planeMatInv[0],this._planeMatInv[1],this._planeMatInv[2],this._planeMatInv[3]]; + retObject.planeMat = this._planeMat; + retObject.planeMatInv = this._planeMatInv; retObject.dragPlane = [this._dragPlane[0],this._dragPlane[1],this._dragPlane[2],this._dragPlane[3]]; //stroke appearance properties @@ -691,8 +695,8 @@ var BrushStroke = function GLBrushStroke() { this._copyCoordinates3D(jo.origLocalPoints, this._OrigLocalPoints); //todo this._stageWorldCenter = [jo.stageWorldCenter[0],jo.stageWorldCenter[1],jo.stageWorldCenter[2]]; - this._planeMat = [jo.planeMat[0], jo.planeMat[1],jo.planeMat[2],jo.planeMat[3]]; - this._planeMatInv = [jo.planeMatInv[0],jo.planeMatInv[1],jo.planeMatInv[2],jo.planeMatInv[3]]; + this._planeMat = jo.planeMat; + this._planeMatInv = jo.planeMatInv; this._dragPlane = [jo.dragPlane[0],jo.dragPlane[1],jo.dragPlane[2],jo.dragPlane[3]]; //stroke appearance properties diff --git a/js/lib/geom/sub-path.js b/js/lib/geom/sub-path.js index d8d74928..35070915 100755 --- a/js/lib/geom/sub-path.js +++ b/js/lib/geom/sub-path.js @@ -95,6 +95,7 @@ var GLSubpath = function GLSubpath() { this._planeMat = null; this._planeMatInv = null; this._planeCenter = null; + this._dragPlane = null; //used to query what the user selected, OR-able for future extensions this.SEL_NONE = 0; //nothing was selected @@ -130,7 +131,10 @@ var GLSubpath = function GLSubpath() { // get the world var world = this.getWorld(); if (!world) throw( "null world in subpath render" ); - + if (!this._canvas){ + //set the canvas by querying the world + this._canvas = this.getWorld().getCanvas(); + } // get the context var ctx = world.get2DContext(); if (!ctx) throw ("null context in subpath render"); @@ -145,42 +149,19 @@ var GLSubpath = function GLSubpath() { //build the coordinates of the samples in 2D (canvas) space (localDirty bit checked in buildLocalCoord this.buildLocalCoord(); - // + //figure the size of the area we will draw into var bboxWidth=0, bboxHeight=0; - //var bboxMid=[0,0,0]; if (useLocalCoord){ bboxWidth = this._LocalBBoxMax[0] - this._LocalBBoxMin[0]; bboxHeight = this._LocalBBoxMax[1] - this._LocalBBoxMin[1]; - //bboxMid = [0.5 * (this._LocalBBoxMax[0] + this._LocalBBoxMin[0]), 0.5 * (this._LocalBBoxMax[1] + this._LocalBBoxMin[1]), 0.5 * (this._LocalBBoxMax[2] + this._LocalBBoxMin[2])]; } else { var bboxMin = this.getBBoxMin(); var bboxMax = this.getBBoxMax(); bboxWidth = bboxMax[0] - bboxMin[0]; bboxHeight = bboxMax[1] - bboxMin[1]; - //bboxMid = [0.5 * (bboxMax[0] + bboxMin[0]), 0.5 * (bboxMax[1] + bboxMin[1]), 0.5 * (bboxMax[2] + bboxMin[2])]; } - if (this._canvas) { - /* - var ViewUtils = require("js/helper-classes/3D/view-utils").ViewUtils; - //compute the plane center as the midpoint of the local bbox converted to stage world space - var planeCenter = ViewUtils.localToStageWorld(bboxMid, this._canvas); - planeCenter[0]+=400; planeCenter[1]+=300; //todo replace these lines with the correct call for the offset - console.log("PEN: local bboxMid: "+ bboxMid +", stage-world bboxMid: "+ planeCenter); - var newLeft = planeCenter[0] - 0.5*bboxWidth; - console.log("PEN: newLeft: "+ newLeft +", bboxWidth: "+bboxWidth); - newLeft = Math.round(newLeft);//Math.round(this._planeCenter[0] - 0.5 * bboxWidth); - console.log("PEN: newLeft rounded: "+ newLeft); - var newTop = Math.round(planeCenter[1] - 0.5 * bboxHeight);//Math.round(this._planeCenter[1] - 0.5 * bboxHeight); - //assign the new position, width, and height as the canvas dimensions through the canvas controller - CanvasController.setProperty(this._canvas, "left", newLeft+"px"); - CanvasController.setProperty(this._canvas, "top", newTop+"px"); - CanvasController.setProperty(this._canvas, "width", bboxWidth+"px"); - CanvasController.setProperty(this._canvas, "height", bboxHeight+"px"); - this._canvas.elementModel.shapeModel.GLWorld.setViewportFromCanvas(this._canvas); - */ - } ctx.save(); ctx.clearRect(0, 0, bboxWidth, bboxHeight); @@ -354,6 +335,10 @@ GLSubpath.prototype.setPlaneMatrix = function(planeMat){ this._planeMat = planeMat; }; +GLSubpath.prototype.setDragPlane = function(p){ + this._dragPlane = p; +}; + GLSubpath.prototype.setPlaneMatrixInverse = function(planeMatInv){ this._planeMatInv = planeMatInv; }; @@ -867,6 +852,12 @@ GLSubpath.prototype.getStrokeWidth = function () { }; GLSubpath.prototype.translateSubpathPerCanvas = function(elemMediator){ + if (!this._canvas){ + if (!this.getWorld()) + return; //cannot do anything if there is no world + //set the canvas by querying the world + this._canvas = this.getWorld().getCanvas(); + } //check if the canvas was translated var penCanvasCurrentLeft = parseInt(elemMediator.getProperty(this._canvas, "left"));//parseFloat(DocumentControllerModule.DocumentController.GetElementStyle(this._penCanvas, "left")); var penCanvasCurrentTop = parseInt(elemMediator.getProperty(this._canvas, "top"));//parseFloat(DocumentControllerModule.DocumentController.GetElementStyle(this._penCanvas, "top")); @@ -1516,95 +1507,75 @@ GLSubpath.prototype.isPointInQuad2D = function(r0,r1,r2,r3,p){ return false; }; -GLSubpath.prototype.export = function() { - var rtnStr = "type: " + this.geomType() + "\n"; - - rtnStr += "strokeWidth: " + this._strokeWidth + "\n"; - rtnStr += "strokeStyle: " + this._strokeStyle + "\n"; - - rtnStr += "strokeMat: "; - if (this._strokeMaterial) - rtnStr += this._strokeMaterial.getName(); - else - rtnStr += "flatMaterial"; - rtnStr += "\n"; - - rtnStr += "fillMat: "; - if (this._fillMaterial) - rtnStr += this._fillMaterial.getName(); - else - rtnStr += "flatMaterial"; - rtnStr += "\n"; - - var isClosedStr = "false"; - if (this._isClosed) - isClosedStr = "true"; - rtnStr += "isClosed: " + isClosedStr + "\n"; - - //add the anchor points - var numAnchors = this._Anchors.length; - rtnStr += "numAnchors: " + numAnchors + "\n"; - for (var i=0;i