Permalink
Browse files

broken fill bucket kind of working

  • Loading branch information...
1 parent 096e7dc commit 1fd763c7372351dc4f5da1f6fabfd6287fd32694 @zrispo committed Jan 5, 2017
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -159,7 +159,7 @@ var FabricInterface = function (wickEditor) {
wickEditor.syncInterfaces();
if(that.lastTool instanceof Tools.Paintbrush) {
- //wickEditor.paper.updateTouchingPaths();
+ wickEditor.paper.onPathsNeedCleanup();
}
}
@@ -471,7 +471,7 @@ var FabricInterface = function (wickEditor) {
this.forceModifySelectedObjects = function () {
var wickObj = that.getSelectedObject(WickObject);
if(wickObj && wickObj.fontData) {
- that.modifyObjects(wickObj);
+ that.modifyObjects([wickObj]);
}
wickEditor.syncInterfaces();
}
@@ -288,9 +288,13 @@ var PaperInterface = function (wickEditor) {
// Update the paper canvas accordingly.
self.onWickObjectsChange = function () {
+ // Generate a list of all wickobjects in the currentframe.
+ // ALWAYS do this or you'll be modifying a list while forEaching through it (this is bad)
var currentFrame = wickEditor.project.currentObject.getCurrentFrame();
var wickObjects = [];
- currentFrame.wickObjects.forEach(function (wickObject) { wickObjects.push(wickObject); });
+ if(currentFrame) {
+ currentFrame.wickObjects.forEach(function (wickObject) { wickObjects.push(wickObject); });
+ }
// For each wickobject in the current frame:
wickObjects.forEach(function (wickObject) {
@@ -306,23 +310,57 @@ var PaperInterface = function (wickEditor) {
var group = paper.project.importSVG(doc);
group.children.forEach(function (child) {
+ // Convert all paper.Shapes into paper.Paths (Paths have boolean ops, Shapes do not)
if(child instanceof paper.Shape) {
child.remove();
group.addChild(child.toPath());
}
+ // Boolean ops only work with closed paths (potrace generates open paths for some reason)
if(child.closePath) child.closePath();
});
group.position = new paper.Point(wickObject.x, wickObject.y);
+ // Newly drawn paths need to get checked for intersections on next onPathsNeedCleanup
+ if(wickObject.isNewDrawingPath) {
+ group.needsIntersectCheck = true;
+ wickObjects.isNewDrawingPath = undefined;
+ }
+
paperObjectMappings[wickObject.uuid] = group;
// If there is, update the path's position/scale/rotation.
} else {
var path = paperObjectMappings[wickObject.uuid];
- path.position = new paper.Point(wickObject.x, wickObject.y);
+
+ path.applyMatrix = true;
+ path.position.x = wickObject.x;
+ path.position.y = wickObject.y;
+
+ if(path.origRotation === undefined) path.origRotation = 0;
+ var newRotation = wickObject.angle;
+ if(newRotation !== path.origRotation) {
+ path.rotate(newRotation-path.origRotation);
+ wickObject.parentObject.removeChild(wickObject);
+ self.onPaperCanvasChange();
+ wickEditor.syncInterfaces();
+ }
+
+ if(path.origScaleX === undefined) path.origScaleX = 1;
+ var newScaleX = wickObject.scaleX;
+ if(newScaleX !== path.origScaleX) {
+ path.scaling.x = newScaleX*path.origScaleX;
+ path.origScaleX = wickObject.scaleX;
+ }
+
+ if(path.origScaleY === undefined) path.origScaleY = 1;
+ var newScaleY = wickObject.scaleY;
+ if(newScaleY !== path.origScaleY) {
+ path.scaling.y = newScaleY*path.origScaleY;
+ path.origScaleY = wickObject.scaleY;
+ }
}
});
@@ -474,6 +512,81 @@ var PaperInterface = function (wickEditor) {
}
+ self.onFill = function (x,y) {
+
+ var point = new paper.Point(x,y);
+
+ // Try to find a path to fill
+ var pathFilled = false;
+ var paths = getAllPathsInCanvas();
+ paths.forEach(function (path) {
+ if(pathFilled) return;
+
+ if(path.contains(point)) {
+ console.log("Path filled:")
+ console.log(path);
+
+ pathFilled = true;
+ }
+ });
+
+ // No path filled, try to find a hole to fill
+ var holeFilled = false;
+ if(!pathFilled) {
+ console.log("No path filled, looking for holes now");
+
+ // Unite all on-screen paths
+ var allPathsUnion = undefined;
+ var paths = getAllPathsInCanvas();
+ paths.forEach(function (path) {
+ var clone = path.clone({insert:false});
+
+ if(!allPathsUnion) {
+ allPathsUnion = clone.children[0];
+ } else {
+ allPathsUnion = allPathsUnion.unite(clone.children[0]);
+ }
+ });
+
+ if(!allPathsUnion) return;
+
+ // Subtract union of all paths from huge rectangle
+ var hugeRectangle = new paper.Path.Rectangle(new paper.Point(-1000,-1000), new paper.Size(2000,2000));
+ var negativeSpace = hugeRectangle.subtract(allPathsUnion);
+ hugeRectangle.remove();
+ negativeSpace.remove();
+
+ //console.log(allPathsUnion);
+ //console.log(negativeSpace);
+ //console.log(allPathsUnion.exportSVG({insert:false}))
+ //console.log(negativeSpace.exportSVG({insert:false}))
+
+ negativeSpace.children.forEach(function (child) {
+ if(holeFilled) return;
+ if(child.clockwise && child.area !== 4000000 && child.contains(point)) {
+ var clone = child.clone({insert:false});//.intersect(negativeSpace);
+ var group = new paper.Group();
+ group.addChild(clone);
+ clone.clockwise = false;
+ clone.fillColor = 'green';
+ group.fillRule = 'evenodd';
+ holeFilled = true;
+ }
+ });
+ }
+
+ if (pathFilled || holeFilled) {
+ /*getAllPathsInCanvas().forEach(function (path) {
+ path.children.forEach(function (child) {
+ if(child instanceof paper.Path) console.log(child.style);
+ })
+ });*/
+
+ self.onPaperCanvasChange();
+ wickEditor.syncInterfaces();
+ }
+ }
+
self.setPathNeedsIntersectionCheck = function (wickObject) {
var path = paperObjectMappings[wickObject.uuid];
if (path) path.needsIntersectCheck = true;
@@ -264,15 +264,15 @@ var PropertiesInterface = function (wickEditor) {
var newWeight = this.checked ? "bold" : "normal";
wickEditor.fabric.forceModifySelectedObjects();
wickEditor.actionHandler.doAction('modifyObjects', {
- ids: [wickEditor.fabric.getSelectedObject()],
+ objs: [wickEditor.fabric.getSelectedObject()],
modifiedStates: [{ fontWeight : newWeight }]
});
};
document.getElementById('italicCheckbox').onchange = function () {
var newStyle = this.checked ? "italic" : "normal";
wickEditor.fabric.forceModifySelectedObjects();
wickEditor.actionHandler.doAction('modifyObjects', {
- ids: [wickEditor.fabric.getSelectedObject()],
+ objs: [wickEditor.fabric.getSelectedObject()],
modifiedStates: [{ fontStyle : newStyle }]
});
};
@@ -33,6 +33,7 @@ Tools.Ellipse = function (wickEditor) {
WickObject.fromPathFile(svgData, function (wickObject) {
wickObject.x = origX;
wickObject.y = origY;
+ wickObject.isNewDrawingPath = true;
wickEditor.project.addObject(wickObject);
wickEditor.paper.onWickObjectsChange();
});
@@ -16,7 +16,16 @@ Tools.FillBucket = function (wickEditor) {
if(e.e.button != 0) return;
if(!(wickEditor.fabric.currentTool instanceof Tools.FillBucket)) return;
- var onscreenObjects = wickEditor.project.currentObject.getAllActiveChildObjects();
+ var mouseScreenSpace = wickEditor.fabric.screenToCanvasSpace(e.e.offsetX, e.e.offsetY);
+ var mousePointX = mouseScreenSpace.x;
+ var mousePointY = mouseScreenSpace.y;
+ var insideSymbolOffset = wickEditor.project.currentObject.getAbsolutePosition();
+ mousePointX -= insideSymbolOffset.x;
+ mousePointY -= insideSymbolOffset.y;
+
+ wickEditor.paper.onFill(mousePointX, mousePointY);
+
+ /*var onscreenObjects = wickEditor.project.currentObject.getAllActiveChildObjects();
VectorToolUtils.updatePaperDataOnVectorWickObjects(onscreenObjects);
var mouseScreenSpace = wickEditor.fabric.screenToCanvasSpace(e.e.offsetX, e.e.offsetY);
@@ -119,7 +128,7 @@ Tools.FillBucket = function (wickEditor) {
wickObjects: [holeWickObj],
partOfChain: true
});
- });
+ });*/
});
}
@@ -59,6 +59,7 @@ Tools.Paintbrush = function (wickEditor) {
WickObject.fromPathFile(SVGData, function (wickObject) {
wickObject.x = fabricPath.left;
wickObject.y = fabricPath.top;
+ wickObject.isNewDrawingPath = true;
wickEditor.project.addObject(wickObject);
wickEditor.paper.onWickObjectsChange();
});
@@ -29,6 +29,7 @@ Tools.Rectangle = function (wickEditor) {
WickObject.fromPathFile(svgString, function (wickObject) {
wickObject.x = origX;
wickObject.y = origY;
+ wickObject.isNewDrawingPath = true;
wickEditor.project.addObject(wickObject);
wickEditor.paper.onWickObjectsChange();
});
@@ -7,10 +7,14 @@
var WickActionHandler = function (wickEditor) {
+ var self = this;
+
/* WickAction definition. All user actions are expected to be well defined by
this structure in order to properly be done and undone. */
- var WickAction = function (doAction, undoAction) {
+ var WickAction = function (name, doAction, undoAction) {
+
+ this.name = name;
/* To be called when an action is committed by the user. */
this.doAction = doAction;
@@ -47,6 +51,7 @@ var WickActionHandler = function (wickEditor) {
// Create a new WickAction object
var action = new WickAction(
+ actionName,
this.doActions[actionName],
this.undoActions[actionName]
);
@@ -120,6 +125,13 @@ var WickActionHandler = function (wickEditor) {
this.redoStack = [];
}
+ this.printHistory = function () {
+ this.undoStack.forEach(function (action) {
+ console.log("Action " + self.undoStack.indexOf(action) + ":")
+ console.log(action.name);
+ })
+ }
+
// Register all actions
this.registerAction('addObjects',
@@ -278,7 +290,7 @@ var WickActionHandler = function (wickEditor) {
selectedObjects.forEach(function (obj) {
wickEditor.project.currentObject.removeChild(obj);
- if(obj.pathData) wickEditor.paper.updatePaperSceneForObject(obj, true);
+ //if(obj.pathData) wickEditor.paper.updatePaperSceneForObject(obj, true);
obj.inFrameSVG = false;
});

0 comments on commit 1fd763c

Please sign in to comment.