2009-06-23 05:17:53 +00:00
|
|
|
if(!window.console) {
|
2009-09-22 21:50:55 +00:00
|
|
|
window.console = {};
|
|
|
|
window.console.log = function(str) {};
|
|
|
|
window.console.dir = function(str) {};
|
|
|
|
}
|
|
|
|
if( window.opera ) {
|
|
|
|
window.console.log = function(str) {opera.postError(str);}
|
2009-06-23 05:17:53 +00:00
|
|
|
}
|
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// this defines which elements and attributes that we support
|
2009-08-14 22:50:34 +00:00
|
|
|
// TODO: add <a> elements to this
|
2009-09-05 22:20:36 +00:00
|
|
|
// TODO: add <tspan> to this
|
2009-07-10 05:04:06 +00:00
|
|
|
var svgWhiteList = {
|
2009-08-29 18:38:34 +00:00
|
|
|
"circle": ["cx", "cy", "fill", "fill-opacity", "id", "opacity", "r", "stroke", "stroke-dasharray", "stroke-opacity", "stroke-width", "transform"],
|
2009-07-14 12:52:39 +00:00
|
|
|
"defs": [],
|
2009-08-29 18:38:34 +00:00
|
|
|
"ellipse": ["cx", "cy", "fill", "fill-opacity", "id", "opacity", "rx", "ry", "stroke", "stroke-dasharray", "stroke-opacity", "stroke-width", "transform"],
|
2009-09-24 04:48:40 +00:00
|
|
|
"g": ["id", "display", "transform"],
|
2009-09-05 11:34:51 +00:00
|
|
|
"image": ["height", "id", "opacity", "transform", "width", "x", "xlink:href", "xlink:title", "y"],
|
2009-08-29 18:38:34 +00:00
|
|
|
"line": ["fill", "fill-opacity", "id", "opacity", "stroke", "stroke-dasharray", "stroke-linecap", "stroke-opacity", "stroke-width", "transform", "x1", "x2", "y1", "y2"],
|
2009-08-14 22:36:54 +00:00
|
|
|
"linearGradient": ["id", "gradientTransform", "gradientUnits", "spreadMethod", "x1", "x2", "y1", "y2"],
|
2009-08-29 18:38:34 +00:00
|
|
|
"path": ["d", "fill", "fill-opacity", "id", "opacity", "stroke", "stroke-dasharray", "stroke-linecap", "stroke-linejoin", "stroke-opacity", "stroke-width", "transform"],
|
|
|
|
"polygon": ["id", "fill", "fill-opacity", "id", "opacity", "points", "stroke", "stroke-dasharray", "stroke-linecap", "stroke-linejoin", "stroke-opacity", "stroke-width", "transform"],
|
2009-08-31 22:41:17 +00:00
|
|
|
"polyline": ["id", "fill", "fill-opacity", "opacity", "points", "stroke", "stroke-dasharray", "stroke-linecap", "stroke-linejoin", "stroke-opacity", "stroke-width", "transform"],
|
2009-08-14 22:36:54 +00:00
|
|
|
"radialGradient": ["id", "cx", "cy", "fx", "fy", "gradientTransform", "gradientUnits", "r", "spreadMethod"],
|
2009-08-29 18:38:34 +00:00
|
|
|
"rect": ["fill", "fill-opacity", "height", "id", "opacity", "rx", "ry", "stroke", "stroke-dasharray", "stroke-linecap", "stroke-linejoin", "stroke-opacity", "stroke-width", "transform", "width", "x", "y"],
|
2009-07-30 22:45:59 +00:00
|
|
|
"stop": ["id", "offset", "stop-color", "stop-opacity"],
|
2009-09-09 17:41:28 +00:00
|
|
|
"svg": ["id", "height", "transform", "viewBox", "width", "xmlns", "xmlns:xlink"],
|
2009-08-28 23:12:46 +00:00
|
|
|
"text": ["fill", "fill-opacity", "font-family", "font-size", "font-style", "font-weight", "id", "opacity", "stroke", "stroke-dasharray", "stroke-linecap", "stroke-linejoin", "stroke-opacity", "stroke-width", "transform", "text-anchor", "x", "y"],
|
2009-09-20 23:04:57 +00:00
|
|
|
"title": [],
|
2009-08-17 06:56:55 +00:00
|
|
|
};
|
2009-07-10 05:04:06 +00:00
|
|
|
|
2009-09-04 05:10:48 +00:00
|
|
|
function SvgCanvas(c)
|
|
|
|
{
|
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
// These command objects are used for the Undo/Redo stack
|
|
|
|
// attrs contains the values that the attributes had before the change
|
|
|
|
function ChangeElementCommand(elem, attrs, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text ? ("Change " + elem.tagName + " " + text) : ("Change " + elem.tagName);
|
|
|
|
this.newValues = {};
|
|
|
|
this.oldValues = attrs;
|
|
|
|
for (attr in attrs) {
|
|
|
|
if (attr == "#text") this.newValues[attr] = elem.textContent;
|
|
|
|
else this.newValues[attr] = elem.getAttribute(attr);
|
|
|
|
}
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.apply = function() {
|
2009-09-06 03:51:16 +00:00
|
|
|
var bChangedTransform = false;
|
2009-06-25 02:54:07 +00:00
|
|
|
for( attr in this.newValues ) {
|
|
|
|
if (this.newValues[attr]) {
|
|
|
|
if (attr == "#text") this.elem.textContent = this.newValues[attr];
|
|
|
|
else this.elem.setAttribute(attr, this.newValues[attr]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (attr != "#text") this.elem.textContent = "";
|
|
|
|
else this.elem.removeAttribute(attr);
|
|
|
|
}
|
2009-09-06 03:51:16 +00:00
|
|
|
if (attr == "transform") { bChangedTransform = true; }
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
// relocate rotational transform, if necessary
|
2009-09-06 03:51:16 +00:00
|
|
|
if(!bChangedTransform) {
|
2009-09-04 05:10:48 +00:00
|
|
|
var angle = canvas.getRotationAngle(elem);
|
|
|
|
if (angle) {
|
|
|
|
var bbox = elem.getBBox();
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(bbox.x + bbox.width/2),
|
|
|
|
cy = round(bbox.y + bbox.height/2);
|
2009-09-04 05:10:48 +00:00
|
|
|
var rotate = ["rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
|
|
|
if (rotate != elem.getAttribute("transform")) {
|
|
|
|
elem.setAttribute("transform", rotate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-22 02:42:31 +00:00
|
|
|
// if we are changing layer names, re-identify all layers
|
|
|
|
if (this.elem.tagName == "title" && this.elem.parentNode.parentNode == svgzoom) {
|
|
|
|
canvas.identifyLayers();
|
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
return true;
|
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.unapply = function() {
|
2009-09-06 03:51:16 +00:00
|
|
|
var bChangedTransform = false;
|
2009-06-25 02:54:07 +00:00
|
|
|
for( attr in this.oldValues ) {
|
|
|
|
if (this.oldValues[attr]) {
|
|
|
|
if (attr == "#text") this.elem.textContent = this.oldValues[attr];
|
|
|
|
else this.elem.setAttribute(attr, this.oldValues[attr]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (attr == "#text") this.elem.textContent = "";
|
|
|
|
else this.elem.removeAttribute(attr);
|
|
|
|
}
|
2009-09-06 03:51:16 +00:00
|
|
|
if (attr == "transform") { bChangedTransform = true; }
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
// relocate rotational transform, if necessary
|
2009-09-06 03:51:16 +00:00
|
|
|
if(!bChangedTransform) {
|
2009-09-04 05:10:48 +00:00
|
|
|
var angle = canvas.getRotationAngle(elem);
|
|
|
|
if (angle) {
|
|
|
|
var bbox = elem.getBBox();
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(bbox.x + bbox.width/2),
|
|
|
|
cy = round(bbox.y + bbox.height/2);
|
2009-09-04 05:10:48 +00:00
|
|
|
var rotate = ["rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
|
|
|
if (rotate != elem.getAttribute("transform")) {
|
|
|
|
elem.setAttribute("transform", rotate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-22 02:42:31 +00:00
|
|
|
// if we are changing layer names, re-identify all layers
|
|
|
|
if (this.elem.tagName == "title" && this.elem.parentNode.parentNode == svgzoom) {
|
|
|
|
canvas.identifyLayers();
|
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
return true;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.elements = function() { return [this.elem]; }
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function InsertElementCommand(elem, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text || ("Create " + elem.tagName);
|
|
|
|
this.parent = elem.parentNode;
|
|
|
|
|
2009-09-22 01:00:24 +00:00
|
|
|
this.apply = function() {
|
|
|
|
this.elem = this.parent.insertBefore(this.elem, this.elem.nextSibling);
|
|
|
|
if (this.parent == svgzoom) {
|
|
|
|
canvas.identifyLayers();
|
|
|
|
}
|
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.unapply = function() {
|
|
|
|
this.parent = this.elem.parentNode;
|
|
|
|
this.elem = this.elem.parentNode.removeChild(this.elem);
|
2009-09-22 01:00:24 +00:00
|
|
|
if (this.parent == svgzoom) {
|
|
|
|
canvas.identifyLayers();
|
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.elements = function() { return [this.elem]; };
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function RemoveElementCommand(elem, parent, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text || ("Delete " + elem.tagName);
|
|
|
|
this.parent = parent;
|
|
|
|
|
|
|
|
this.apply = function() {
|
|
|
|
this.parent = this.elem.parentNode;
|
2009-06-29 14:38:30 +00:00
|
|
|
this.elem = this.parent.removeChild(this.elem);
|
2009-09-22 01:00:24 +00:00
|
|
|
if (this.parent == svgzoom) {
|
|
|
|
canvas.identifyLayers();
|
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
};
|
|
|
|
|
2009-09-22 01:00:24 +00:00
|
|
|
this.unapply = function() {
|
|
|
|
this.elem = this.parent.insertBefore(this.elem, this.elem.nextSibling);
|
|
|
|
if (this.parent == svgzoom) {
|
|
|
|
canvas.identifyLayers();
|
|
|
|
}
|
|
|
|
};
|
2009-07-01 21:28:22 +00:00
|
|
|
|
|
|
|
this.elements = function() { return [this.elem]; };
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function MoveElementCommand(elem, oldNextSibling, oldParent, text) {
|
|
|
|
this.elem = elem;
|
2009-09-06 03:51:16 +00:00
|
|
|
this.text = text ? ("Move " + elem.tagName + " to " + text) : ("Move " + elem.tagName);
|
2009-06-25 02:54:07 +00:00
|
|
|
this.oldNextSibling = oldNextSibling;
|
|
|
|
this.oldParent = oldParent;
|
|
|
|
this.newNextSibling = elem.nextSibling;
|
|
|
|
this.newParent = elem.parentNode;
|
2009-06-29 14:38:30 +00:00
|
|
|
|
|
|
|
this.apply = function() {
|
2009-06-25 02:54:07 +00:00
|
|
|
this.elem = this.newParent.insertBefore(this.elem, this.newNextSibling);
|
2009-09-22 01:00:24 +00:00
|
|
|
if (this.newParent == svgzoom) {
|
|
|
|
canvas.identifyLayers();
|
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
|
|
|
this.unapply = function() {
|
2009-06-25 02:54:07 +00:00
|
|
|
this.elem = this.oldParent.insertBefore(this.elem, this.oldNextSibling);
|
2009-09-22 01:00:24 +00:00
|
|
|
if (this.oldParent == svgzoom) {
|
|
|
|
canvas.identifyLayers();
|
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
};
|
2009-07-01 21:28:22 +00:00
|
|
|
|
|
|
|
this.elements = function() { return [this.elem]; };
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
2009-06-29 03:58:34 +00:00
|
|
|
// TODO: create a 'typing' command object that tracks changes in text
|
|
|
|
// if a new Typing command is created and the top command on the stack is also a Typing
|
|
|
|
// and they both affect the same element, then collapse the two commands into one
|
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
// this command object acts an arbitrary number of subcommands
|
|
|
|
function BatchCommand(text) {
|
|
|
|
this.text = text || "Batch Command";
|
|
|
|
this.stack = [];
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.apply = function() {
|
2009-07-08 00:03:08 +00:00
|
|
|
var len = this.stack.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-07-01 21:28:22 +00:00
|
|
|
this.stack[i].apply();
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.unapply = function() {
|
|
|
|
for (var i = this.stack.length-1; i >= 0; i--) {
|
|
|
|
this.stack[i].unapply();
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
this.elements = function() {
|
|
|
|
// iterate through all our subcommands and find all the elements we are changing
|
|
|
|
var elems = [];
|
|
|
|
var cmd = this.stack.length;
|
|
|
|
while (cmd--) {
|
|
|
|
var thisElems = this.stack[cmd].elements();
|
|
|
|
var elem = thisElems.length;
|
|
|
|
while (elem--) {
|
|
|
|
if (elems.indexOf(thisElems[elem]) == -1) elems.push(thisElems[elem]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return elems;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-02 04:50:24 +00:00
|
|
|
this.addSubCommand = function(cmd) { this.stack.push(cmd); };
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.isEmpty = function() { return this.stack.length == 0; };
|
|
|
|
}
|
2009-06-29 03:58:34 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// private members
|
|
|
|
|
|
|
|
// **************************************************************************************
|
|
|
|
// FIXME: what's the right way to make this 'class' private to the SelectorManager?
|
|
|
|
function Selector(id, elem) {
|
|
|
|
// this is the selector's unique number
|
|
|
|
this.id = id;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to the element for which this selector is being used
|
|
|
|
this.selectedElement = elem;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this is a flag used internally to track whether the selector is being used or not
|
2009-06-29 03:58:34 +00:00
|
|
|
this.locked = true;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this function is used to reset the id and element that the selector is attached to
|
|
|
|
this.reset = function(e) {
|
|
|
|
this.locked = true;
|
|
|
|
this.selectedElement = e;
|
2009-07-01 15:34:15 +00:00
|
|
|
this.resize();
|
2009-08-14 18:35:48 +00:00
|
|
|
selectorManager.update();
|
2009-06-29 13:20:39 +00:00
|
|
|
this.selectorGroup.setAttribute("display", "inline");
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to the <g> element that holds all visual elements of the selector
|
|
|
|
this.selectorGroup = addSvgElementFromJson({ "element": "g",
|
|
|
|
"attr": {"id": ("selectorGroup"+this.id)}
|
|
|
|
});
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to <rect> element
|
|
|
|
this.selectorRect = this.selectorGroup.appendChild( addSvgElementFromJson({
|
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"id": ("selectedBox"+this.id),
|
|
|
|
"fill": "none",
|
|
|
|
"stroke": "blue",
|
|
|
|
"stroke-width": "1",
|
|
|
|
"stroke-dasharray": "5,5",
|
|
|
|
"width": 1,
|
|
|
|
"height": 1,
|
|
|
|
// need to specify this so that the rect is not selectable
|
2009-08-04 03:14:23 +00:00
|
|
|
"style": "pointer-events:none"
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
|
|
|
}) );
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to the grip elements for this selector
|
|
|
|
this.selectorGrips = { "nw":null,
|
|
|
|
"n":null,
|
|
|
|
"ne":null,
|
|
|
|
"e":null,
|
2009-08-26 13:59:25 +00:00
|
|
|
"se":null,
|
2009-06-29 13:20:39 +00:00
|
|
|
"s":null,
|
2009-08-26 13:59:25 +00:00
|
|
|
"sw":null,
|
|
|
|
"w":null
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-08-26 19:09:13 +00:00
|
|
|
this.rotateGripConnector = this.selectorGroup.appendChild( addSvgElementFromJson({
|
|
|
|
"element": "line",
|
|
|
|
"attr": {
|
|
|
|
"id": ("selectorGrip_rotate_connector_" + this.id),
|
|
|
|
"stroke": "blue",
|
2009-09-28 18:19:22 +00:00
|
|
|
"stroke-width": "1"
|
2009-08-26 19:09:13 +00:00
|
|
|
}
|
|
|
|
}) );
|
|
|
|
this.rotateGrip = this.selectorGroup.appendChild( addSvgElementFromJson({
|
|
|
|
"element": "circle",
|
|
|
|
"attr": {
|
|
|
|
"id": ("selectorGrip_rotate_" + this.id),
|
|
|
|
"fill": "lime",
|
|
|
|
"r": 4,
|
|
|
|
"stroke": "blue",
|
2009-09-28 18:19:22 +00:00
|
|
|
"stroke-width": 2,
|
|
|
|
"style": "cursor:url(images/rotate.png) 12 12, auto;"
|
2009-08-26 19:09:13 +00:00
|
|
|
}
|
|
|
|
}) );
|
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// add the corner grips
|
|
|
|
for (dir in this.selectorGrips) {
|
2009-07-01 21:59:11 +00:00
|
|
|
this.selectorGrips[dir] = this.selectorGroup.appendChild(
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"id": ("selectorGrip_" + dir + "_" + this.id),
|
|
|
|
"fill": "blue",
|
|
|
|
"width": 6,
|
|
|
|
"height": 6,
|
|
|
|
"style": ("cursor:" + dir + "-resize"),
|
|
|
|
// This expands the mouse-able area of the grips making them
|
|
|
|
// easier to grab with the mouse.
|
|
|
|
// This works in Opera and WebKit, but does not work in Firefox
|
|
|
|
// see https://bugzilla.mozilla.org/show_bug.cgi?id=500174
|
|
|
|
"stroke-width": 2,
|
|
|
|
"pointer-events":"all",
|
|
|
|
"display":"none"
|
|
|
|
}
|
|
|
|
}) );
|
2009-06-29 13:20:39 +00:00
|
|
|
$('#'+this.selectorGrips[dir].id).mousedown( function() {
|
|
|
|
current_mode = "resize";
|
2009-07-01 19:28:08 +00:00
|
|
|
current_resize_mode = this.id.substr(13,this.id.indexOf("_",13)-13);
|
2009-06-29 13:20:39 +00:00
|
|
|
});
|
2009-08-26 19:45:28 +00:00
|
|
|
$('#selectorGrip_rotate_'+id).mousedown( function() {
|
|
|
|
current_mode = "rotate";
|
|
|
|
});
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
this.showGrips = function(show) {
|
2009-07-30 22:45:59 +00:00
|
|
|
// TODO: use suspendRedraw() here
|
2009-08-26 19:09:13 +00:00
|
|
|
var bShow = show ? "inline" : "none";
|
2009-08-27 14:44:23 +00:00
|
|
|
this.rotateGrip.setAttribute("display", bShow);
|
|
|
|
this.rotateGripConnector.setAttribute("display", bShow);
|
|
|
|
var elem = this.selectedElement;
|
2009-09-13 15:50:43 +00:00
|
|
|
if(elem && (elem.tagName == "text" || elem.tagName == "g")) bShow = "none";
|
2009-06-29 13:20:39 +00:00
|
|
|
for (dir in this.selectorGrips) {
|
2009-08-26 19:09:13 +00:00
|
|
|
this.selectorGrips[dir].setAttribute("display", bShow);
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
2009-08-31 20:05:52 +00:00
|
|
|
if(elem) this.updateGripCursors(canvas.getRotationAngle(elem));
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-08-26 13:59:25 +00:00
|
|
|
|
|
|
|
// Updates cursors for corner grips on rotation so arrows point the right way
|
|
|
|
this.updateGripCursors = function(angle) {
|
|
|
|
var dir_arr = [];
|
|
|
|
var steps = Math.round(angle / 45);
|
2009-08-27 12:37:02 +00:00
|
|
|
if(steps < 0) steps += 8;
|
2009-08-26 13:59:25 +00:00
|
|
|
for (dir in this.selectorGrips) {
|
|
|
|
dir_arr.push(dir);
|
|
|
|
}
|
|
|
|
while(steps > 0) {
|
|
|
|
dir_arr.push(dir_arr.shift());
|
|
|
|
steps--;
|
|
|
|
}
|
|
|
|
var i = 0;
|
|
|
|
for (dir in this.selectorGrips) {
|
|
|
|
this.selectorGrips[dir].setAttribute('style', ("cursor:" + dir_arr[i] + "-resize"));
|
|
|
|
i++;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2009-08-21 15:53:36 +00:00
|
|
|
this.resize = function(cur_bbox) {
|
2009-06-29 13:20:39 +00:00
|
|
|
var selectedBox = this.selectorRect;
|
|
|
|
var selectedGrips = this.selectorGrips;
|
|
|
|
var selected = this.selectedElement;
|
2009-09-17 14:47:54 +00:00
|
|
|
var sw = round(selected.getAttribute("stroke-width"));
|
|
|
|
var offset = 1/canvas.getZoom();
|
|
|
|
if (selected.getAttribute("stroke") != "none" && !isNaN(sw)) {
|
2009-06-29 13:20:39 +00:00
|
|
|
offset += sw/2;
|
|
|
|
}
|
|
|
|
if (selected.tagName == "text") {
|
2009-09-17 14:47:54 +00:00
|
|
|
offset += 2/canvas.getZoom();
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
2009-08-27 04:01:19 +00:00
|
|
|
var oldbox = canvas.getBBox(this.selectedElement);
|
|
|
|
var bbox = cur_bbox || oldbox;
|
2009-06-29 13:20:39 +00:00
|
|
|
var l=bbox.x-offset, t=bbox.y-offset, w=bbox.width+(offset<<1), h=bbox.height+(offset<<1);
|
2009-08-24 19:19:27 +00:00
|
|
|
var sr_handle = svgroot.suspendRedraw(100);
|
2009-09-09 14:18:24 +00:00
|
|
|
l*=current_zoom;
|
|
|
|
t*=current_zoom;
|
|
|
|
w*=current_zoom;
|
|
|
|
h*=current_zoom;
|
2009-08-24 18:39:22 +00:00
|
|
|
assignAttributes(selectedBox, {
|
|
|
|
'x': l,
|
|
|
|
'y': t,
|
|
|
|
'width': w,
|
|
|
|
'height': h
|
|
|
|
});
|
2009-08-24 19:19:27 +00:00
|
|
|
|
2009-08-24 18:39:22 +00:00
|
|
|
var gripCoords = {
|
|
|
|
nw: [l-3, t-3],
|
|
|
|
ne: [l+w-3, t-3],
|
|
|
|
sw: [l-3, t+h-3],
|
|
|
|
se: [l+w-3, t+h-3],
|
|
|
|
n: [l+w/2-3, t-3],
|
|
|
|
w: [l-3, t+h/2-3],
|
|
|
|
e: [l+w-3, t+h/2-3],
|
|
|
|
s: [l+w/2-3, t+h-3]
|
|
|
|
};
|
|
|
|
$.each(gripCoords, function(dir, coords) {
|
|
|
|
assignAttributes(selectedGrips[dir], {
|
|
|
|
x: coords[0], y: coords[1]
|
|
|
|
});
|
|
|
|
});
|
2009-08-21 15:53:36 +00:00
|
|
|
|
2009-08-26 19:09:13 +00:00
|
|
|
assignAttributes(this.rotateGripConnector, { x1: l+w/2, y1: t-20, x2: l+w/2, y2: t });
|
|
|
|
assignAttributes(this.rotateGrip, { cx: l+w/2, cy: t-20 });
|
|
|
|
|
2009-08-21 15:53:36 +00:00
|
|
|
// empty out the transform attribute
|
|
|
|
this.selectorGroup.setAttribute("transform", "");
|
|
|
|
this.selectorGroup.removeAttribute("transform");
|
|
|
|
|
|
|
|
// align selector group with element coordinate axes
|
2009-08-27 04:01:19 +00:00
|
|
|
var elem = this.selectedElement;
|
|
|
|
var transform = elem.getAttribute("transform");
|
|
|
|
var angle = canvas.getRotationAngle(elem);
|
|
|
|
if (angle) {
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(oldbox.x + oldbox.width/2) * current_zoom
|
|
|
|
cy = round(oldbox.y + oldbox.height/2) * current_zoom;
|
2009-08-27 04:01:19 +00:00
|
|
|
this.selectorGroup.setAttribute("transform", "rotate("+angle+" " + cx + "," + cy + ")");
|
2009-08-21 15:53:36 +00:00
|
|
|
}
|
2009-08-24 19:19:27 +00:00
|
|
|
svgroot.unsuspendRedraw(sr_handle);
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 15:34:15 +00:00
|
|
|
// now initialize the selector
|
|
|
|
this.reset(elem);
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-06-01 17:35:22 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
function SelectorManager() {
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this will hold the <g> element that contains all selector rects/grips
|
|
|
|
this.selectorParentGroup = null;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-30 06:24:41 +00:00
|
|
|
// this is a special rect that is used for multi-select
|
|
|
|
this.rubberBandBox = null;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this will hold objects of type Selector (see above)
|
|
|
|
this.selectors = [];
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-30 06:37:39 +00:00
|
|
|
// this holds a map of SVG elements to their Selector object
|
|
|
|
this.selectorMap = {};
|
2009-06-30 06:24:41 +00:00
|
|
|
|
|
|
|
// local reference to this object
|
|
|
|
var mgr = this;
|
2009-09-04 06:36:37 +00:00
|
|
|
|
|
|
|
this.initGroup = function() {
|
2009-06-30 06:24:41 +00:00
|
|
|
mgr.selectorParentGroup = addSvgElementFromJson({
|
|
|
|
"element": "g",
|
|
|
|
"attr": {"id": "selectorParentGroup"}
|
2009-08-17 06:56:55 +00:00
|
|
|
});
|
2009-09-04 17:44:08 +00:00
|
|
|
mgr.selectorMap = {};
|
|
|
|
mgr.selectors = [];
|
|
|
|
mgr.rubberBandBox = null;
|
2009-09-10 20:34:20 +00:00
|
|
|
mgr.update();
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
this.requestSelector = function(elem) {
|
2009-07-01 15:34:15 +00:00
|
|
|
if (elem == null) return null;
|
2009-06-29 13:20:39 +00:00
|
|
|
var N = this.selectors.length;
|
2009-06-30 14:29:28 +00:00
|
|
|
// if we've already acquired one for this element, return it
|
2009-07-01 21:59:11 +00:00
|
|
|
if (typeof(this.selectorMap[elem.id]) == "object") {
|
|
|
|
this.selectorMap[elem.id].locked = true;
|
|
|
|
return this.selectorMap[elem.id];
|
2009-06-30 14:29:28 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
for (var i = 0; i < N; ++i) {
|
|
|
|
if (this.selectors[i] && !this.selectors[i].locked) {
|
|
|
|
this.selectors[i].locked = true;
|
2009-07-01 15:34:15 +00:00
|
|
|
this.selectors[i].reset(elem);
|
2009-07-01 21:59:11 +00:00
|
|
|
this.selectorMap[elem.id] = this.selectors[i];
|
2009-06-29 13:20:39 +00:00
|
|
|
return this.selectors[i];
|
2009-06-29 03:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-29 13:20:39 +00:00
|
|
|
// if we reached here, no available selectors were found, we create one
|
|
|
|
this.selectors[N] = new Selector(N, elem);
|
|
|
|
this.selectorParentGroup.appendChild(this.selectors[N].selectorGroup);
|
2009-07-01 21:59:11 +00:00
|
|
|
this.selectorMap[elem.id] = this.selectors[N];
|
2009-06-29 13:20:39 +00:00
|
|
|
return this.selectors[N];
|
|
|
|
};
|
2009-07-01 15:34:15 +00:00
|
|
|
this.releaseSelector = function(elem) {
|
|
|
|
if (elem == null) return;
|
2009-06-29 13:20:39 +00:00
|
|
|
var N = this.selectors.length;
|
2009-07-01 21:59:11 +00:00
|
|
|
var sel = this.selectorMap[elem.id];
|
2009-06-29 13:20:39 +00:00
|
|
|
for (var i = 0; i < N; ++i) {
|
|
|
|
if (this.selectors[i] && this.selectors[i] == sel) {
|
|
|
|
if (sel.locked == false) {
|
|
|
|
console.log("WARNING! selector was released but was already unlocked");
|
|
|
|
}
|
2009-07-01 21:59:11 +00:00
|
|
|
delete this.selectorMap[elem.id];
|
2009-06-29 13:20:39 +00:00
|
|
|
sel.locked = false;
|
|
|
|
sel.selectedElement = null;
|
2009-07-01 19:28:08 +00:00
|
|
|
sel.showGrips(false);
|
2009-06-29 13:20:39 +00:00
|
|
|
|
|
|
|
// remove from DOM and store reference in JS but only if it exists in the DOM
|
|
|
|
try {
|
|
|
|
sel.selectorGroup.setAttribute("display", "none");
|
|
|
|
} catch(e) { }
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this keeps the selector groups as the last child in the document
|
|
|
|
this.update = function() {
|
|
|
|
this.selectorParentGroup = svgroot.appendChild(this.selectorParentGroup);
|
2009-08-07 05:44:55 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-30 06:24:41 +00:00
|
|
|
this.getRubberBandBox = function() {
|
|
|
|
if (this.rubberBandBox == null) {
|
2009-06-30 17:50:08 +00:00
|
|
|
this.rubberBandBox = this.selectorParentGroup.appendChild(
|
2009-07-01 19:28:08 +00:00
|
|
|
addSvgElementFromJson({ "element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"id": "selectorRubberBand",
|
|
|
|
"fill": "blue",
|
|
|
|
"fill-opacity": 0.15,
|
|
|
|
"stroke": "blue",
|
|
|
|
"stroke-width": 0.5,
|
|
|
|
"display": "none",
|
2009-08-04 03:14:23 +00:00
|
|
|
"style": "pointer-events:none"
|
2009-07-01 19:28:08 +00:00
|
|
|
}
|
|
|
|
}));
|
2009-06-30 06:24:41 +00:00
|
|
|
}
|
|
|
|
return this.rubberBandBox;
|
2009-08-07 05:44:55 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-04 06:36:37 +00:00
|
|
|
this.initGroup();
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
|
|
|
// **************************************************************************************
|
2009-06-25 08:22:30 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
var addSvgElementFromJson = function(data) {
|
|
|
|
return canvas.updateElementFromJson(data)
|
|
|
|
};
|
|
|
|
|
2009-08-24 19:19:27 +00:00
|
|
|
var assignAttributes = function(node, attrs, suspendLength) {
|
|
|
|
if(!suspendLength) suspendLength = 0;
|
|
|
|
var handle = svgroot.suspendRedraw(suspendLength);
|
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
for (i in attrs) {
|
|
|
|
node.setAttributeNS(null, i, attrs[i]);
|
|
|
|
}
|
2009-08-24 19:19:27 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// remove unneeded attributes
|
|
|
|
// makes resulting SVG smaller
|
|
|
|
var cleanupElement = function(element) {
|
|
|
|
var handle = svgroot.suspendRedraw(60);
|
|
|
|
if (element.getAttribute('fill-opacity') == '1')
|
|
|
|
element.removeAttribute('fill-opacity');
|
|
|
|
if (element.getAttribute('opacity') == '1')
|
|
|
|
element.removeAttribute('opacity');
|
|
|
|
if (element.getAttribute('stroke') == 'none')
|
|
|
|
element.removeAttribute('stroke');
|
|
|
|
if (element.getAttribute('stroke-dasharray') == 'none')
|
|
|
|
element.removeAttribute('stroke-dasharray');
|
|
|
|
if (element.getAttribute('stroke-opacity') == '1')
|
|
|
|
element.removeAttribute('stroke-opacity');
|
|
|
|
if (element.getAttribute('stroke-width') == '1')
|
|
|
|
element.removeAttribute('stroke-width');
|
|
|
|
if (element.getAttribute('rx') == '0')
|
|
|
|
element.removeAttribute('rx')
|
|
|
|
if (element.getAttribute('ry') == '0')
|
|
|
|
element.removeAttribute('ry')
|
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-08-17 06:56:55 +00:00
|
|
|
};
|
2009-08-07 05:44:55 +00:00
|
|
|
|
|
|
|
this.updateElementFromJson = function(data) {
|
|
|
|
var shape = svgdoc.getElementById(data.attr.id);
|
|
|
|
// if shape is a path but we need to create a rect/ellipse, then remove the path
|
|
|
|
if (shape && data.element != shape.tagName) {
|
2009-09-21 02:05:58 +00:00
|
|
|
current_layer.removeChild(shape);
|
2009-08-07 05:44:55 +00:00
|
|
|
shape = null;
|
|
|
|
}
|
|
|
|
if (!shape) {
|
|
|
|
shape = svgdoc.createElementNS(svgns, data.element);
|
2009-09-21 02:05:58 +00:00
|
|
|
if (current_layer) {
|
|
|
|
current_layer.appendChild(shape);
|
|
|
|
}
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(shape, data.attr, 100);
|
2009-08-07 05:44:55 +00:00
|
|
|
cleanupElement(shape);
|
|
|
|
return shape;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-13 12:55:15 +00:00
|
|
|
// TODO: declare the variables and set them as null, then move this setup stuff to
|
|
|
|
// an initialization function - probably just use clear()
|
2009-06-09 22:13:13 +00:00
|
|
|
var canvas = this;
|
2009-06-07 00:17:21 +00:00
|
|
|
var container = c;
|
2009-06-01 21:24:30 +00:00
|
|
|
var svgns = "http://www.w3.org/2000/svg";
|
2009-09-04 23:08:27 +00:00
|
|
|
var xlinkns = "http://www.w3.org/1999/xlink";
|
2009-06-12 06:45:37 +00:00
|
|
|
var idprefix = "svg_";
|
2009-06-07 00:17:21 +00:00
|
|
|
var svgdoc = c.ownerDocument;
|
|
|
|
var svgroot = svgdoc.createElementNS(svgns, "svg");
|
2009-06-07 10:27:55 +00:00
|
|
|
svgroot.setAttribute("width", 640);
|
|
|
|
svgroot.setAttribute("height", 480);
|
2009-06-08 15:05:12 +00:00
|
|
|
svgroot.setAttribute("id", "svgroot");
|
2009-06-08 08:09:15 +00:00
|
|
|
svgroot.setAttribute("xmlns", svgns);
|
2009-09-04 23:08:27 +00:00
|
|
|
svgroot.setAttribute("xmlns:xlink", xlinkns);
|
2009-06-07 00:17:21 +00:00
|
|
|
container.appendChild(svgroot);
|
2009-09-09 14:18:24 +00:00
|
|
|
var svgzoom = svgdoc.createElementNS(svgns, "svg");
|
|
|
|
svgzoom.setAttribute('id', 'svgzoom');
|
|
|
|
svgzoom.setAttribute('viewBox', '0 0 640 480');
|
2009-09-09 17:41:28 +00:00
|
|
|
svgzoom.setAttribute("xmlns", svgns);
|
|
|
|
svgzoom.setAttribute("xmlns:xlink", xlinkns);
|
2009-09-09 14:18:24 +00:00
|
|
|
svgroot.appendChild(svgzoom);
|
2009-09-09 17:41:28 +00:00
|
|
|
var comment = svgdoc.createComment(" created with SVG-edit - http://svg-edit.googlecode.com/ ");
|
|
|
|
svgzoom.appendChild(comment);
|
2009-09-23 14:28:03 +00:00
|
|
|
// TODO For Issue 208: this is a start on a thumbnail
|
|
|
|
// var svgthumb = svgdoc.createElementNS(svgns, "use");
|
|
|
|
// svgthumb.setAttribute('width', '100');
|
|
|
|
// svgthumb.setAttribute('height', '100');
|
|
|
|
// svgthumb.setAttributeNS(xlinkns, 'href', '#svgzoom');
|
|
|
|
// svgroot.appendChild(svgthumb);
|
2009-09-21 22:16:44 +00:00
|
|
|
// z-ordered array of tuples containing layer names and <g> elements
|
2009-09-23 03:05:27 +00:00
|
|
|
// the first layer is the one at the bottom of the rendering
|
2009-09-21 22:16:44 +00:00
|
|
|
var all_layers = [];
|
2009-09-21 02:05:58 +00:00
|
|
|
// pointer to the current layer <g>
|
|
|
|
var current_layer = null;
|
|
|
|
|
2009-06-02 16:40:33 +00:00
|
|
|
var d_attr = null;
|
|
|
|
var started = false;
|
2009-06-01 21:24:30 +00:00
|
|
|
var obj_num = 1;
|
2009-06-02 16:40:33 +00:00
|
|
|
var start_x = null;
|
|
|
|
var start_y = null;
|
2009-06-27 03:18:50 +00:00
|
|
|
var current_mode = "select";
|
2009-06-23 05:17:53 +00:00
|
|
|
var current_resize_mode = "none";
|
2009-08-31 14:54:59 +00:00
|
|
|
|
|
|
|
var all_properties = {
|
|
|
|
shape: {
|
|
|
|
fill: "#FF0000",
|
|
|
|
fill_paint: null,
|
|
|
|
fill_opacity: 1,
|
|
|
|
stroke: "#000000",
|
|
|
|
stroke_paint: null,
|
|
|
|
stroke_opacity: 1,
|
|
|
|
stroke_width: 5,
|
|
|
|
stroke_style: 'none',
|
|
|
|
opacity: 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
all_properties.text = $.extend(true, {}, all_properties.shape);
|
|
|
|
$.extend(all_properties.text, {
|
|
|
|
fill: "#000000",
|
|
|
|
stroke_width: 0,
|
|
|
|
font_size: '12pt',
|
|
|
|
font_family: 'serif'
|
|
|
|
});
|
|
|
|
|
|
|
|
var cur_shape = all_properties.shape;
|
|
|
|
var cur_text = all_properties.text;
|
2009-08-31 15:59:42 +00:00
|
|
|
var cur_properties = cur_shape;
|
2009-08-31 14:54:59 +00:00
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
var freehand_min_x = null;
|
|
|
|
var freehand_max_x = null;
|
|
|
|
var freehand_min_y = null;
|
|
|
|
var freehand_max_y = null;
|
2009-08-08 14:03:14 +00:00
|
|
|
var current_poly = null;
|
2009-08-07 05:44:55 +00:00
|
|
|
var current_poly_pts = [];
|
2009-08-08 22:17:59 +00:00
|
|
|
var current_poly_pt_drag = -1;
|
2009-08-29 22:10:52 +00:00
|
|
|
var current_poly_oldd = null;
|
2009-09-25 17:59:32 +00:00
|
|
|
var current_ctrl_pt_drag = -1;
|
2009-09-09 14:18:24 +00:00
|
|
|
var current_zoom = 1;
|
2009-06-29 22:22:13 +00:00
|
|
|
// this will hold all the currently selected elements
|
|
|
|
// default size of 1 until it needs to grow bigger
|
|
|
|
var selectedElements = new Array(1);
|
2009-06-30 06:24:41 +00:00
|
|
|
// this holds the selected's bbox
|
2009-07-01 19:28:08 +00:00
|
|
|
var selectedBBoxes = new Array(1);
|
2009-09-06 23:25:12 +00:00
|
|
|
var justSelected = null;
|
2009-06-30 06:24:41 +00:00
|
|
|
// this object manages selectors for us
|
2009-06-29 13:20:39 +00:00
|
|
|
var selectorManager = new SelectorManager();
|
2009-06-30 06:24:41 +00:00
|
|
|
var rubberBox = null;
|
2009-06-06 18:34:42 +00:00
|
|
|
var events = {};
|
2009-06-25 02:54:07 +00:00
|
|
|
var undoStackPointer = 0;
|
|
|
|
var undoStack = [];
|
2009-08-26 19:59:52 +00:00
|
|
|
var curBBoxes = [];
|
|
|
|
|
2009-09-17 14:47:54 +00:00
|
|
|
// This method rounds the incoming value to the nearest value based on the current_zoom
|
|
|
|
var round = function(val){
|
|
|
|
return parseInt(val*current_zoom)/current_zoom;
|
|
|
|
};
|
|
|
|
|
2009-07-02 04:50:24 +00:00
|
|
|
// This method sends back an array or a NodeList full of elements that
|
2009-09-24 03:13:25 +00:00
|
|
|
// intersect the multi-select rubber-band-box on the current_layer only.
|
2009-07-02 04:50:24 +00:00
|
|
|
//
|
|
|
|
// Since the only browser that supports the SVG DOM getIntersectionList is Opera,
|
|
|
|
// we need to provide an implementation here. We brute-force it for now.
|
|
|
|
//
|
|
|
|
// Reference:
|
2009-07-01 19:28:08 +00:00
|
|
|
// Firefox does not implement getIntersectionList(), see https://bugzilla.mozilla.org/show_bug.cgi?id=501421
|
|
|
|
// Webkit does not implement getIntersectionList(), see https://bugs.webkit.org/show_bug.cgi?id=11274
|
2009-07-01 15:34:15 +00:00
|
|
|
var getIntersectionList = function(rect) {
|
2009-07-02 04:50:24 +00:00
|
|
|
if (rubberBox == null) { return null; }
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-26 19:59:52 +00:00
|
|
|
if(!curBBoxes.length) {
|
|
|
|
// Cache all bboxes
|
2009-09-24 03:13:25 +00:00
|
|
|
curBBoxes = canvas.getVisibleElements(current_layer, true);
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
2009-08-26 19:59:52 +00:00
|
|
|
|
|
|
|
var resultList = null;
|
|
|
|
try {
|
2009-09-21 02:05:58 +00:00
|
|
|
resultList = current_layer.getIntersectionList(rect, null);
|
2009-08-26 19:59:52 +00:00
|
|
|
} catch(e) { }
|
|
|
|
|
|
|
|
if (resultList == null || typeof(resultList.item) != "function") {
|
|
|
|
resultList = [];
|
|
|
|
|
|
|
|
var rubberBBox = rubberBox.getBBox();
|
2009-09-10 19:20:26 +00:00
|
|
|
$.each(rubberBBox, function(key, val) {
|
|
|
|
rubberBBox[key] = val / current_zoom;
|
|
|
|
});
|
2009-08-26 19:59:52 +00:00
|
|
|
var i = curBBoxes.length;
|
|
|
|
while (i--) {
|
|
|
|
if (Utils.rectsIntersect(rubberBBox, curBBoxes[i].bbox)) {
|
|
|
|
resultList.push(curBBoxes[i].elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-02 04:50:24 +00:00
|
|
|
// addToSelection expects an array, but it's ok to pass a NodeList
|
|
|
|
// because using square-bracket notation is allowed:
|
|
|
|
// http://www.w3.org/TR/DOM-Level-2-Core/ecma-script-binding.html
|
2009-07-01 15:34:15 +00:00
|
|
|
return resultList;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 14:38:30 +00:00
|
|
|
// FIXME: we MUST compress consecutive text changes to the same element
|
2009-06-25 02:54:07 +00:00
|
|
|
// (right now each keystroke is saved as a separate command that includes the
|
|
|
|
// entire text contents of the text element)
|
|
|
|
// TODO: consider limiting the history that we store here (need to do some slicing)
|
2009-06-30 06:24:41 +00:00
|
|
|
var addCommandToHistory = function(cmd) {
|
2009-06-25 02:54:07 +00:00
|
|
|
// if our stack pointer is not at the end, then we have to remove
|
|
|
|
// all commands after the pointer and insert the new command
|
|
|
|
if (undoStackPointer < undoStack.length && undoStack.length > 0) {
|
|
|
|
undoStack = undoStack.splice(0, undoStackPointer);
|
|
|
|
}
|
2009-07-02 04:50:24 +00:00
|
|
|
undoStack.push(cmd);
|
2009-06-25 02:54:07 +00:00
|
|
|
undoStackPointer = undoStack.length;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-25 02:54:07 +00:00
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
// private functions
|
2009-06-05 12:33:02 +00:00
|
|
|
var getId = function() {
|
2009-06-25 08:22:30 +00:00
|
|
|
if (events["getid"]) return call("getid", obj_num);
|
|
|
|
return idprefix + obj_num;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-11 13:15:16 +00:00
|
|
|
var getNextId = function() {
|
|
|
|
// ensure the ID does not exist
|
|
|
|
var id = getId();
|
|
|
|
while (svgdoc.getElementById(id)) {
|
|
|
|
obj_num++;
|
|
|
|
id = getId();
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
return id;
|
2009-07-11 13:15:16 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 18:34:42 +00:00
|
|
|
var call = function(event, arg) {
|
|
|
|
if (events[event]) {
|
|
|
|
return events[event](this,arg);
|
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 18:34:42 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// this function sanitizes the input node and its children
|
|
|
|
// this function only keeps what is allowed from our whitelist defined above
|
|
|
|
var sanitizeSvg = function(node) {
|
|
|
|
// we only care about element nodes
|
2009-07-13 02:57:11 +00:00
|
|
|
// automatically return for all comment, etc nodes
|
|
|
|
// for text, we do a whitespace trim
|
|
|
|
if (node.nodeType == 3) {
|
|
|
|
node.nodeValue = node.nodeValue.replace(/^\s+|\s+$/g, "");
|
|
|
|
}
|
2009-07-10 05:04:06 +00:00
|
|
|
if (node.nodeType != 1) return;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
var doc = node.ownerDocument;
|
|
|
|
var parent = node.parentNode;
|
|
|
|
// can parent ever be null here? I think the root node's parent is the document...
|
|
|
|
if (!doc || !parent) return;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
var allowedAttrs = svgWhiteList[node.nodeName];
|
|
|
|
// if this element is allowed
|
|
|
|
if (allowedAttrs != undefined) {
|
|
|
|
var i = node.attributes.length;
|
|
|
|
while (i--) {
|
|
|
|
// if the attribute is not in our whitelist, then remove it
|
|
|
|
// could use jQuery's inArray(), but I don't know if that's any better
|
|
|
|
var attrName = node.attributes.item(i).nodeName;
|
|
|
|
if (allowedAttrs.indexOf(attrName) == -1) {
|
|
|
|
// TODO: do I need to call setAttribute(..., "") here for Fx2?
|
|
|
|
node.removeAttribute(attrName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// recurse to children
|
|
|
|
i = node.childNodes.length;
|
|
|
|
while (i--) { sanitizeSvg(node.childNodes.item(i)); }
|
|
|
|
}
|
|
|
|
// else, remove this element
|
|
|
|
else {
|
|
|
|
// remove all children from this node and insert them before this node
|
|
|
|
var children = [];
|
|
|
|
while (node.hasChildNodes()) {
|
|
|
|
children.push(parent.insertBefore(node.firstChild, node));
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// remove this node from the document altogether
|
|
|
|
parent.removeChild(node);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// call sanitizeSvg on each of those children
|
|
|
|
var i = children.length;
|
|
|
|
while (i--) { sanitizeSvg(children[i]); }
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
}
|
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
var removeUnusedGrads = function() {
|
2009-09-09 17:41:28 +00:00
|
|
|
var defs = svgzoom.getElementsByTagNameNS(svgns, "defs");
|
2009-08-25 12:40:58 +00:00
|
|
|
if(!defs || !defs.length) return;
|
2009-08-19 17:08:05 +00:00
|
|
|
|
2009-09-09 17:41:28 +00:00
|
|
|
var all_els = svgzoom.getElementsByTagNameNS(svgns, '*');
|
2009-08-19 17:08:05 +00:00
|
|
|
var grad_uses = [];
|
|
|
|
|
|
|
|
$.each(all_els, function(i, el) {
|
|
|
|
var fill = el.getAttribute('fill');
|
|
|
|
if(fill && fill.indexOf('url(#') == 0) {
|
|
|
|
//found gradient
|
|
|
|
grad_uses.push(fill);
|
|
|
|
}
|
|
|
|
|
|
|
|
var stroke = el.getAttribute('stroke');
|
|
|
|
if(stroke && stroke.indexOf('url(#') == 0) {
|
|
|
|
//found gradient
|
|
|
|
grad_uses.push(stroke);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2009-09-09 17:41:28 +00:00
|
|
|
var lgrads = svgzoom.getElementsByTagNameNS(svgns, "linearGradient");
|
2009-08-19 17:08:05 +00:00
|
|
|
var grad_ids = [];
|
|
|
|
var i = lgrads.length;
|
|
|
|
while (i--) {
|
|
|
|
var grad = lgrads[i];
|
|
|
|
var id = grad.getAttribute('id');
|
|
|
|
var url_id = 'url(#' + id + ')';
|
|
|
|
if($.inArray(url_id, grad_uses) == -1) {
|
|
|
|
// Not found, so remove
|
|
|
|
grad.parentNode.removeChild(grad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove defs if empty
|
|
|
|
var i = defs.length;
|
|
|
|
while (i--) {
|
|
|
|
var def = defs[i];
|
|
|
|
if(!def.getElementsByTagNameNS(svgns,'*').length) {
|
|
|
|
def.parentNode.removeChild(def);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 14:18:24 +00:00
|
|
|
|
|
|
|
var svgCanvasToString = function() {
|
|
|
|
removeUnusedGrads();
|
2009-09-25 17:59:32 +00:00
|
|
|
canvas.clearPoly(true);
|
2009-09-09 17:41:28 +00:00
|
|
|
var output = svgToString(svgzoom, 0);
|
2009-09-09 14:18:24 +00:00
|
|
|
return output;
|
|
|
|
}
|
2009-08-19 17:08:05 +00:00
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
var svgToString = function(elem, indent) {
|
2009-07-10 05:04:06 +00:00
|
|
|
var out = new Array();
|
2009-06-01 21:24:30 +00:00
|
|
|
if (elem) {
|
|
|
|
var attrs = elem.attributes;
|
|
|
|
var attr;
|
|
|
|
var i;
|
|
|
|
var childs = elem.childNodes;
|
2009-07-10 05:04:06 +00:00
|
|
|
for (i=0; i<indent; i++) out.push(" ");
|
|
|
|
out.push("<"); out.push(elem.nodeName);
|
2009-06-01 21:24:30 +00:00
|
|
|
for (i=attrs.length-1; i>=0; i--) {
|
|
|
|
attr = attrs.item(i);
|
2009-06-16 02:52:36 +00:00
|
|
|
if (attr.nodeValue != "") {
|
2009-09-05 11:59:25 +00:00
|
|
|
out.push(" ");
|
2009-09-05 18:13:20 +00:00
|
|
|
// map various namespaces to our fixed namespace prefixes
|
|
|
|
// TODO: put this into a map and do a look-up instead of if-else
|
2009-09-05 11:59:25 +00:00
|
|
|
if (attr.namespaceURI == 'http://www.w3.org/1999/xlink') {
|
|
|
|
out.push('xlink:');
|
|
|
|
}
|
2009-09-05 18:13:20 +00:00
|
|
|
else if(attr.namespaceURI == 'http://www.w3.org/2000/xmlns/' && attr.localName != 'xmlns') {
|
|
|
|
out.push('xmlns:');
|
|
|
|
}
|
|
|
|
out.push(attr.localName); out.push("=\"");
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push(attr.nodeValue); out.push("\"");
|
2009-06-16 02:52:36 +00:00
|
|
|
}
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
if (elem.hasChildNodes()) {
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push(">");
|
2009-06-01 21:24:30 +00:00
|
|
|
indent++;
|
2009-06-27 03:42:12 +00:00
|
|
|
var bOneLine = false;
|
2009-06-01 21:24:30 +00:00
|
|
|
for (i=0; i<childs.length; i++)
|
|
|
|
{
|
2009-06-30 17:50:08 +00:00
|
|
|
var child = childs.item(i);
|
|
|
|
if (child.id == "selectorParentGroup") continue;
|
2009-07-13 03:23:58 +00:00
|
|
|
switch(child.nodeType) {
|
|
|
|
case 1: // element node
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("\n");
|
|
|
|
out.push(svgToString(childs.item(i), indent));
|
2009-07-13 03:23:58 +00:00
|
|
|
break;
|
|
|
|
case 3: // text node
|
2009-07-13 02:57:11 +00:00
|
|
|
var str = child.nodeValue.replace(/^\s+|\s+$/g, "");
|
|
|
|
if (str != "") {
|
|
|
|
bOneLine = true;
|
|
|
|
out.push(str + "");
|
2009-07-13 00:17:15 +00:00
|
|
|
}
|
2009-07-13 03:23:58 +00:00
|
|
|
break;
|
|
|
|
case 8: // comment
|
|
|
|
out.push("\n");
|
|
|
|
out.push(new Array(indent+1).join(" "));
|
|
|
|
out.push("<!--");
|
|
|
|
out.push(child.data);
|
|
|
|
out.push("-->");
|
|
|
|
break;
|
|
|
|
} // switch on node type
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
indent--;
|
2009-06-29 14:38:30 +00:00
|
|
|
if (!bOneLine) {
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("\n");
|
2009-07-30 22:45:59 +00:00
|
|
|
for (i=0; i<indent; i++) out.push(" ");
|
2009-06-27 03:42:12 +00:00
|
|
|
}
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("</"); out.push(elem.nodeName); out.push(">");
|
2009-06-01 21:24:30 +00:00
|
|
|
} else {
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("/>");
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-10 05:04:06 +00:00
|
|
|
return out.join('');
|
2009-06-30 06:24:41 +00:00
|
|
|
}; // end svgToString()
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
var recalculateAllSelectedDimensions = function() {
|
2009-07-01 21:28:22 +00:00
|
|
|
var text = (current_resize_mode == "none" ? "position" : "size");
|
|
|
|
var batchCmd = new BatchCommand(text);
|
2009-07-01 19:54:51 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
var i = selectedElements.length;
|
|
|
|
while(i--) {
|
2009-09-05 23:09:46 +00:00
|
|
|
var cmd = recalculateDimensions(selectedElements[i],selectedBBoxes[i]);
|
2009-07-08 00:03:08 +00:00
|
|
|
if (cmd) {
|
|
|
|
batchCmd.addSubCommand(cmd);
|
2009-07-01 19:54:51 +00:00
|
|
|
}
|
2009-07-08 00:03:08 +00:00
|
|
|
}
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
if (!batchCmd.isEmpty()) {
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
call("changed", selectedElements);
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-15 04:37:08 +00:00
|
|
|
// this is how we map paths to our preferred relative segment types
|
|
|
|
var pathMap = [ 0, 'z', 'm', 'm', 'l', 'l', 'c', 'c', 'q', 'q', 'a', 'a',
|
|
|
|
'l', 'l', 'l', 'l', // TODO: be less lazy below and map them to h and v
|
|
|
|
's', 's', 't', 't' ];
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-16 14:56:22 +00:00
|
|
|
// this function returns the command which resulted from the selected change
|
2009-09-05 23:09:46 +00:00
|
|
|
var recalculateDimensions = function(selected,selectedBBox) {
|
|
|
|
if (selected == null || selectedBBox == null) return null;
|
2009-08-16 15:07:00 +00:00
|
|
|
var box = canvas.getBBox(selected);
|
2009-07-08 00:03:08 +00:00
|
|
|
|
|
|
|
// if we have not moved/resized, then immediately leave
|
2009-09-06 03:51:16 +00:00
|
|
|
var xform = selected.getAttribute("transform");
|
|
|
|
if ( (!xform || xform == "") && box.x == selectedBBox.x && box.y == selectedBBox.y &&
|
|
|
|
box.width == selectedBBox.width && box.height == selectedBBox.height)
|
|
|
|
{
|
2009-07-08 00:03:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
// after this point, we have some change to this element
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-26 05:08:34 +00:00
|
|
|
var remap = function(x,y) {
|
2009-09-21 20:23:42 +00:00
|
|
|
// Prevent division by 0
|
|
|
|
if(!box.height) box.height = 1;
|
|
|
|
if(!box.width) box.width = 1;
|
2009-08-26 05:08:34 +00:00
|
|
|
return {
|
2009-09-21 00:03:47 +00:00
|
|
|
'x':(((x-box.x)/box.width)*selectedBBox.width + selectedBBox.x),
|
|
|
|
'y':(((y-box.y)/box.height)*selectedBBox.height + selectedBBox.y)
|
2009-08-26 05:08:34 +00:00
|
|
|
};
|
|
|
|
};
|
2009-09-21 00:03:47 +00:00
|
|
|
var scalew = function(w) {return (w*selectedBBox.width/box.width);}
|
|
|
|
var scaleh = function(h) {return (h*selectedBBox.height/box.height);}
|
2009-07-08 00:03:08 +00:00
|
|
|
|
2009-09-06 03:51:16 +00:00
|
|
|
var batchCmd = new BatchCommand("Transform");
|
|
|
|
|
2009-08-16 14:56:22 +00:00
|
|
|
// if there was a rotation transform, re-set it, otherwise empty out the transform attribute
|
|
|
|
var angle = canvas.getRotationAngle(selected);
|
2009-08-18 16:59:01 +00:00
|
|
|
var pointGripContainer = document.getElementById("polypointgrip_container");
|
2009-08-16 19:18:22 +00:00
|
|
|
if (angle) {
|
2009-08-26 05:08:34 +00:00
|
|
|
// this is our old center upon which we have rotated the shape
|
2009-09-17 14:47:54 +00:00
|
|
|
var tr_x = round(box.x + box.width/2),
|
|
|
|
tr_y = round(box.y + box.height/2);
|
2009-08-26 05:08:34 +00:00
|
|
|
var cx = null, cy = null;
|
2009-08-27 04:01:19 +00:00
|
|
|
|
|
|
|
var bFoundScale = false;
|
|
|
|
var tlist = selected.transform.baseVal;
|
|
|
|
var t = tlist.numberOfItems;
|
|
|
|
while (t--) {
|
|
|
|
var xform = tlist.getItem(t);
|
|
|
|
if (xform.type == 3) {
|
|
|
|
bFoundScale = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-26 05:08:34 +00:00
|
|
|
// if this was a resize, find the new cx,cy
|
2009-08-27 04:01:19 +00:00
|
|
|
if (bFoundScale) {
|
2009-08-26 05:08:34 +00:00
|
|
|
var alpha = angle * Math.PI / 180.0;
|
|
|
|
|
|
|
|
// rotate new opposite corners of bbox by angle at old center
|
|
|
|
var dx = selectedBBox.x - tr_x,
|
|
|
|
dy = selectedBBox.y - tr_y,
|
|
|
|
r = Math.sqrt(dx*dx + dy*dy),
|
|
|
|
theta = Math.atan2(dy,dx) + alpha;
|
|
|
|
var left = r * Math.cos(theta) + tr_x,
|
|
|
|
top = r * Math.sin(theta) + tr_y;
|
|
|
|
|
|
|
|
dx += selectedBBox.width;
|
|
|
|
dy += selectedBBox.height;
|
|
|
|
r = Math.sqrt(dx*dx + dy*dy);
|
|
|
|
theta = Math.atan2(dy,dx) + alpha;
|
|
|
|
var right = r * Math.cos(theta) + tr_x,
|
|
|
|
bottom = r * Math.sin(theta) + tr_y;
|
|
|
|
|
|
|
|
// now find mid-point of line between top-left and bottom-right to find new center
|
2009-09-17 14:47:54 +00:00
|
|
|
cx = round(left + (right-left)/2);
|
|
|
|
cy = round(top + (bottom-top)/2);
|
2009-08-26 05:08:34 +00:00
|
|
|
|
|
|
|
// now that we know the center and the axis-aligned width/height, calculate the x,y
|
2009-09-17 14:47:54 +00:00
|
|
|
selectedBBox.x = round(cx - selectedBBox.width/2),
|
|
|
|
selectedBBox.y = round(cy - selectedBBox.height/2);
|
2009-08-25 12:40:58 +00:00
|
|
|
}
|
2009-08-26 05:08:34 +00:00
|
|
|
// if it was not a resize, then it was a translation only
|
|
|
|
else {
|
|
|
|
var tx = selectedBBox.x - box.x,
|
|
|
|
ty = selectedBBox.y - box.y;
|
|
|
|
cx = tr_x + tx;
|
|
|
|
cy = tr_y + ty;
|
2009-08-25 12:40:58 +00:00
|
|
|
}
|
|
|
|
|
2009-08-18 16:59:01 +00:00
|
|
|
var rotate = ["rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
|
|
|
selected.setAttribute("transform", rotate);
|
2009-09-06 03:51:16 +00:00
|
|
|
// if we were rotated, store just the old rotation (not other transforms) on the
|
|
|
|
// undo stack
|
|
|
|
var changes = {};
|
|
|
|
changes["transform"] = ["rotate(", angle, " ", tr_x, ",", tr_y, ")"].join('');
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(selected, changes));
|
2009-08-18 19:05:42 +00:00
|
|
|
if(pointGripContainer) {
|
|
|
|
pointGripContainer.setAttribute("transform", rotate);
|
|
|
|
}
|
2009-08-16 14:56:22 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-08-28 23:12:46 +00:00
|
|
|
// This fixes Firefox 2- behavior - which does not reset values when the attribute has
|
|
|
|
// been removed, see https://bugzilla.mozilla.org/show_bug.cgi?id=320622
|
2009-08-16 14:56:22 +00:00
|
|
|
selected.setAttribute("transform", "");
|
|
|
|
selected.removeAttribute("transform");
|
2009-08-18 19:05:42 +00:00
|
|
|
if(pointGripContainer) {
|
|
|
|
pointGripContainer.setAttribute("transform", "");
|
|
|
|
pointGripContainer.removeAttribute("transform");
|
|
|
|
}
|
2009-08-16 14:56:22 +00:00
|
|
|
}
|
2009-09-06 01:59:26 +00:00
|
|
|
|
|
|
|
// if it's a group, transfer the transform attribute to each child element
|
|
|
|
// and recursively call recalculateDimensions()
|
|
|
|
if (selected.tagName == "g") {
|
|
|
|
var children = selected.childNodes;
|
|
|
|
var i = children.length;
|
|
|
|
while (i--) {
|
|
|
|
var child = children.item(i);
|
|
|
|
if (child.nodeType == 1) {
|
2009-09-23 15:29:55 +00:00
|
|
|
try {
|
|
|
|
var childBox = child.getBBox();
|
2009-09-09 15:23:44 +00:00
|
|
|
// TODO: to fix the rotation problem, we must account for the
|
|
|
|
// child's rotation in the bbox adjustment
|
|
|
|
|
|
|
|
// If the child is rotated at all, we should figure out the rotated
|
|
|
|
// bbox before the group's transform, remap all four corners of the bbox
|
|
|
|
// via the group's transform, then determine the new angle and the new center
|
|
|
|
/*
|
|
|
|
var childAngle = canvas.getRotationAngle(child) * Math.PI / 180.0;
|
|
|
|
var left = childBox.x - gcx,
|
|
|
|
top = childBox.y - gcy,
|
|
|
|
right = childBox.x + childBox.width - gcx,
|
|
|
|
bottom = childBox.y + childBox.height - gcy;
|
|
|
|
|
|
|
|
var ptTopLeft = remap(left,top),
|
|
|
|
ptTopRight = remap(right,top),
|
|
|
|
ptBottomLeft = remap(left,bottom),
|
|
|
|
ptBottomRight = remap(right,bottom);
|
|
|
|
*/
|
2009-09-06 01:59:26 +00:00
|
|
|
var pt = remap(childBox.x,childBox.y),
|
|
|
|
w = scalew(childBox.width),
|
|
|
|
h = scaleh(childBox.height);
|
|
|
|
childBox.x = pt.x; childBox.y = pt.y;
|
|
|
|
childBox.width = w; childBox.height = h;
|
|
|
|
batchCmd.addSubCommand(recalculateDimensions(child, childBox));
|
2009-09-23 15:29:55 +00:00
|
|
|
} catch(e) {}
|
2009-09-06 01:59:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return batchCmd;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-06 03:51:16 +00:00
|
|
|
var changes = {};
|
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
switch (selected.tagName)
|
|
|
|
{
|
2009-09-06 01:59:26 +00:00
|
|
|
case "g":
|
|
|
|
// do work here :P
|
|
|
|
break;
|
2009-08-20 05:33:34 +00:00
|
|
|
// NOTE: at the moment, there's no way to create an actual polygon element except by
|
|
|
|
// editing source or importing from somewhere else but we'll cover it here anyway
|
|
|
|
// polygon is handled just like polyline
|
2009-08-15 04:37:08 +00:00
|
|
|
case "polygon":
|
2009-08-07 17:19:32 +00:00
|
|
|
case "polyline":
|
2009-08-20 05:33:34 +00:00
|
|
|
// extract the points from the polygon/polyline, adjust it and write back the new points
|
|
|
|
// but first, save the old points
|
2009-08-07 17:19:32 +00:00
|
|
|
changes["points"] = selected.getAttribute("points");
|
|
|
|
var list = selected.points;
|
|
|
|
var len = list.numberOfItems;
|
|
|
|
var newpoints = "";
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var pt = list.getItem(i);
|
2009-08-26 05:08:34 +00:00
|
|
|
pt = remap(pt.x,pt.y);
|
|
|
|
newpoints += pt.x + "," + pt.y + " ";
|
2009-08-07 17:19:32 +00:00
|
|
|
}
|
|
|
|
selected.setAttributeNS(null, "points", newpoints);
|
|
|
|
break;
|
2009-07-08 00:03:08 +00:00
|
|
|
case "path":
|
|
|
|
// extract the x,y from the path, adjust it and write back the new path
|
|
|
|
// but first, save the old path
|
|
|
|
changes["d"] = selected.getAttribute("d");
|
|
|
|
var M = selected.pathSegList.getItem(0);
|
|
|
|
var curx = M.x, cury = M.y;
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt = remap(curx,cury);
|
|
|
|
var newd = "M" + pt.x + "," + pt.y;
|
2009-07-08 00:03:08 +00:00
|
|
|
var segList = selected.pathSegList;
|
|
|
|
var len = segList.numberOfItems;
|
2009-08-15 04:40:33 +00:00
|
|
|
// for all path segments in the path, we first turn them into relative path segments,
|
|
|
|
// then we remap the coordinates from the resize
|
2009-07-08 00:03:08 +00:00
|
|
|
for (var i = 1; i < len; ++i) {
|
2009-08-15 04:37:08 +00:00
|
|
|
var seg = segList.getItem(i);
|
|
|
|
// if these properties are not in the segment, set them to zero
|
|
|
|
var x = seg.x || 0,
|
|
|
|
y = seg.y || 0,
|
|
|
|
x1 = seg.x1 || 0,
|
|
|
|
y1 = seg.y1 || 0,
|
|
|
|
x2 = seg.x2 || 0,
|
|
|
|
y2 = seg.y2 || 0;
|
|
|
|
|
|
|
|
var type = seg.pathSegType;
|
|
|
|
switch (type) {
|
|
|
|
case 1: // z,Z closepath (Z/z)
|
|
|
|
newd += "z";
|
|
|
|
continue;
|
2009-08-16 06:02:26 +00:00
|
|
|
// turn this into a relative segment then fall through
|
2009-08-15 04:37:08 +00:00
|
|
|
case 2: // absolute move (M)
|
|
|
|
case 4: // absolute line (L)
|
|
|
|
case 12: // absolute horizontal line (H)
|
|
|
|
case 14: // absolute vertical line (V)
|
|
|
|
case 18: // absolute smooth quad (T)
|
|
|
|
x -= curx;
|
|
|
|
y -= cury;
|
|
|
|
case 3: // relative move (m)
|
|
|
|
case 5: // relative line (l)
|
|
|
|
case 13: // relative horizontal line (h)
|
|
|
|
case 15: // relative vertical line (v)
|
|
|
|
case 19: // relative smooth quad (t)
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
2009-08-15 10:53:56 +00:00
|
|
|
newd += [" ", pathMap[type], scalew(x), ",", scaleh(y)].join('');
|
2009-08-15 04:37:08 +00:00
|
|
|
break;
|
|
|
|
case 6: // absolute cubic (C)
|
|
|
|
x -= curx; x1 -= curx; x2 -= curx;
|
|
|
|
y -= cury; y1 -= cury; y2 -= cury;
|
|
|
|
case 7: // relative cubic (c)
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
newd += [" c", scalew(x1), ",", scaleh(y1), " ", scalew(x2), ",", scaleh(y2),
|
2009-08-15 10:53:56 +00:00
|
|
|
" ", scalew(x), ",", scaleh(y)].join('');
|
2009-08-15 04:37:08 +00:00
|
|
|
break;
|
|
|
|
case 8: // absolute quad (Q)
|
|
|
|
x -= curx; x1 -= curx;
|
|
|
|
y -= cury; y1 -= cury;
|
|
|
|
case 9: // relative quad (q)
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
2009-08-15 10:53:56 +00:00
|
|
|
newd += [" q", scalew(x1), ",", scaleh(y1), " ", scalew(x), ",", scaleh(y)].join('');
|
2009-08-15 04:37:08 +00:00
|
|
|
break;
|
|
|
|
case 10: // absolute elliptical arc (A)
|
|
|
|
x -= curx;
|
|
|
|
y -= cury;
|
|
|
|
case 11: // relative elliptical arc (a)
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
newd += [ "a", scalew(seg.r1), ",", scaleh(seg.r2), " ", seg.angle, " ",
|
|
|
|
(seg.largeArcFlag ? 1 : 0), " ", (seg.sweepFlag ? 1 : 0), " ",
|
2009-08-15 10:53:56 +00:00
|
|
|
scalew(x), ",", scaleh(y) ].join('')
|
2009-08-15 04:37:08 +00:00
|
|
|
break;
|
|
|
|
case 16: // absolute smooth cubic (S)
|
|
|
|
x -= curx; x2 -= curx;
|
|
|
|
y -= cury; y2 -= cury;
|
|
|
|
case 17: // relative smooth cubic (s)
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
2009-08-15 10:53:56 +00:00
|
|
|
newd += [" s", scalew(x2), ",", scaleh(y2), " ", scalew(x), ",", scaleh(y)].join('');
|
2009-08-15 04:37:08 +00:00
|
|
|
break;
|
|
|
|
} // switch on path segment type
|
|
|
|
} // for each segment
|
2009-07-08 00:03:08 +00:00
|
|
|
selected.setAttributeNS(null, "d", newd);
|
|
|
|
break;
|
|
|
|
case "line":
|
2009-08-16 06:02:26 +00:00
|
|
|
changes["x1"] = selected.getAttribute("x1");
|
|
|
|
changes["y1"] = selected.getAttribute("y1");
|
|
|
|
changes["x2"] = selected.getAttribute("x2");
|
|
|
|
changes["y2"] = selected.getAttribute("y2");
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt1 = remap(changes["x1"],changes["y1"]),
|
|
|
|
pt2 = remap(changes["x2"],changes["y2"]);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(selected, {
|
2009-08-26 05:08:34 +00:00
|
|
|
'x1': pt1.x,
|
|
|
|
'y1': pt1.y,
|
|
|
|
'x2': pt2.x,
|
|
|
|
'y2': pt2.y,
|
2009-08-24 20:00:48 +00:00
|
|
|
}, 1000);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
|
|
|
case "circle":
|
2009-08-16 06:02:26 +00:00
|
|
|
changes["cx"] = selected.getAttribute("cx");
|
|
|
|
changes["cy"] = selected.getAttribute("cy");
|
|
|
|
changes["r"] = selected.getAttribute("r");
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt = remap(changes["cx"], changes["cy"]);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(selected, {
|
2009-08-26 05:08:34 +00:00
|
|
|
'cx': pt.x,
|
|
|
|
'cy': pt.y,
|
2009-08-24 20:00:48 +00:00
|
|
|
|
|
|
|
// take the minimum of the new selected box's dimensions for the new circle radius
|
2009-09-17 14:47:54 +00:00
|
|
|
'r': round(Math.min(selectedBBox.width/2,selectedBBox.height/2))
|
2009-08-24 20:00:48 +00:00
|
|
|
}, 1000);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
|
|
|
case "ellipse":
|
2009-08-16 06:02:26 +00:00
|
|
|
changes["cx"] = selected.getAttribute("cx");
|
|
|
|
changes["cy"] = selected.getAttribute("cy");
|
|
|
|
changes["rx"] = selected.getAttribute("rx");
|
|
|
|
changes["ry"] = selected.getAttribute("ry");
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt = remap(changes["cx"], changes["cy"]);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(selected, {
|
2009-08-26 05:08:34 +00:00
|
|
|
'cx': pt.x,
|
|
|
|
'cy': pt.y,
|
2009-08-24 20:00:48 +00:00
|
|
|
'rx': scalew(changes["rx"]),
|
|
|
|
'ry': scaleh(changes["ry"])
|
|
|
|
}, 1000);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
|
|
|
case "text":
|
|
|
|
changes["x"] = selected.getAttribute("x");
|
|
|
|
changes["y"] = selected.getAttribute("y");
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt = remap(changes["x"], changes["y"]);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(selected, {
|
2009-08-26 05:08:34 +00:00
|
|
|
'x': pt.x,
|
|
|
|
'y': pt.y
|
2009-08-24 20:00:48 +00:00
|
|
|
}, 1000);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
2009-09-04 22:22:40 +00:00
|
|
|
|
2009-09-05 11:59:25 +00:00
|
|
|
case "image":
|
|
|
|
changes["x"] = selected.getAttribute("x");
|
2009-09-04 22:22:40 +00:00
|
|
|
changes["y"] = selected.getAttribute("y");
|
|
|
|
changes["width"] = selected.getAttribute("width");
|
|
|
|
changes["height"] = selected.getAttribute("height");
|
|
|
|
var pt = remap(changes["x"], changes["y"]);
|
|
|
|
assignAttributes(selected, {
|
|
|
|
'x': pt.x,
|
|
|
|
'y': pt.y,
|
|
|
|
'width': scalew(changes["width"]),
|
|
|
|
'height': scaleh(changes["height"])
|
|
|
|
}, 1000);
|
|
|
|
break;
|
2009-07-08 00:03:08 +00:00
|
|
|
case "rect":
|
2009-08-16 06:02:26 +00:00
|
|
|
changes["x"] = selected.getAttribute("x");
|
|
|
|
changes["y"] = selected.getAttribute("y");
|
|
|
|
changes["width"] = selected.getAttribute("width");
|
|
|
|
changes["height"] = selected.getAttribute("height");
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt = remap(changes["x"], changes["y"]);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(selected, {
|
2009-08-26 05:08:34 +00:00
|
|
|
'x': pt.x,
|
|
|
|
'y': pt.y,
|
2009-08-24 20:00:48 +00:00
|
|
|
'width': scalew(changes["width"]),
|
|
|
|
'height': scaleh(changes["height"])
|
|
|
|
}, 1000);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
|
|
|
default: // rect
|
|
|
|
console.log("Unknown shape type: " + selected.tagName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (changes) {
|
2009-09-06 03:51:16 +00:00
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(selected, changes));
|
2009-06-23 07:48:15 +00:00
|
|
|
}
|
2009-09-06 03:51:16 +00:00
|
|
|
return batchCmd;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 05:17:53 +00:00
|
|
|
|
2009-06-02 12:52:44 +00:00
|
|
|
// public events
|
2009-07-01 15:34:15 +00:00
|
|
|
|
|
|
|
this.clearSelection = function() {
|
2009-09-22 18:29:15 +00:00
|
|
|
if (selectedElements[0] != null) {
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var elem = selectedElements[i];
|
|
|
|
if (elem == null) break;
|
|
|
|
selectorManager.releaseSelector(elem);
|
|
|
|
selectedElements[i] = null;
|
|
|
|
selectedBBoxes[i] = null;
|
|
|
|
}
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
2009-07-01 19:28:08 +00:00
|
|
|
call("selected", selectedElements);
|
2009-07-01 15:34:15 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-10 14:27:34 +00:00
|
|
|
// TODO: do we need to worry about selectedBBoxes here?
|
2009-08-21 13:39:23 +00:00
|
|
|
this.addToSelection = function(elemsToAdd, showGrips) {
|
2009-07-01 15:34:15 +00:00
|
|
|
if (elemsToAdd.length == 0) { return; }
|
|
|
|
// find the first null in our selectedElements array
|
|
|
|
var j = 0;
|
|
|
|
while (j < selectedElements.length) {
|
|
|
|
if (selectedElements[j] == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
2009-08-28 19:20:54 +00:00
|
|
|
|
2009-07-01 15:34:15 +00:00
|
|
|
// now add each element consecutively
|
2009-07-03 02:49:42 +00:00
|
|
|
var i = elemsToAdd.length;
|
|
|
|
while (i--) {
|
2009-07-01 15:34:15 +00:00
|
|
|
var elem = elemsToAdd[i];
|
2009-07-01 19:28:08 +00:00
|
|
|
// we ignore any selectors
|
|
|
|
if (elem.id.substr(0,13) == "selectorGrip_") continue;
|
2009-07-01 15:34:15 +00:00
|
|
|
// if it's not already there, add it
|
|
|
|
if (selectedElements.indexOf(elem) == -1) {
|
2009-07-01 19:28:08 +00:00
|
|
|
selectedElements[j] = elem;
|
2009-08-16 15:07:00 +00:00
|
|
|
selectedBBoxes[j++] = this.getBBox(elem);
|
2009-09-06 23:25:12 +00:00
|
|
|
var sel = selectorManager.requestSelector(elem);
|
|
|
|
if (selectedElements.length > 1) {
|
|
|
|
sel.showGrips(false);
|
|
|
|
}
|
2009-07-01 19:28:08 +00:00
|
|
|
call("selected", selectedElements);
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-06 23:25:12 +00:00
|
|
|
|
2009-08-21 13:39:23 +00:00
|
|
|
if(showGrips) {
|
2009-08-27 14:44:23 +00:00
|
|
|
selectorManager.requestSelector(selectedElements[0]).showGrips(true);
|
2009-08-21 13:39:23 +00:00
|
|
|
}
|
2009-09-06 23:25:12 +00:00
|
|
|
else if (selectedElements.length > 1) {
|
|
|
|
selectorManager.requestSelector(selectedElements[0]).showGrips(false);
|
|
|
|
}
|
2009-07-01 15:34:15 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-10 14:27:34 +00:00
|
|
|
// updates the canvas arrays selectedElements and selectedBBoxes
|
|
|
|
// TODO: could use slice here to make this faster?
|
2009-07-01 15:34:15 +00:00
|
|
|
this.removeFromSelection = function(elemsToRemove) {
|
|
|
|
if (selectedElements[0] == null) { return; }
|
|
|
|
if (elemsToRemove.length == 0) { return; }
|
|
|
|
|
2009-08-17 06:56:55 +00:00
|
|
|
// find every element and remove it from our array copy
|
2009-07-01 15:34:15 +00:00
|
|
|
var newSelectedItems = new Array(selectedElements.length);
|
2009-09-10 14:27:34 +00:00
|
|
|
var newSelectedBBoxes = new Array(selectedBBoxes.length);
|
2009-07-01 15:34:15 +00:00
|
|
|
var j = 0;
|
2009-07-03 02:49:42 +00:00
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-07-01 15:34:15 +00:00
|
|
|
var elem = selectedElements[i];
|
|
|
|
if (elem) {
|
|
|
|
// keep the item
|
|
|
|
if (elemsToRemove.indexOf(elem) == -1) {
|
2009-09-10 14:27:34 +00:00
|
|
|
newSelectedBBoxes[j] = selectedBBoxes[i];
|
2009-07-01 15:34:15 +00:00
|
|
|
newSelectedItems[j++] = elem;
|
|
|
|
}
|
|
|
|
else { // remove the item and its selector
|
|
|
|
selectorManager.releaseSelector(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// the copy becomes the master now
|
|
|
|
selectedElements = newSelectedItems;
|
2009-09-10 14:27:34 +00:00
|
|
|
selectedBBoxes = newSelectedBBoxes;
|
2009-07-01 15:34:15 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
// in mouseDown :
|
|
|
|
// - when we are in a create mode, the element is added to the canvas
|
2009-07-01 19:28:08 +00:00
|
|
|
// but the action is not recorded until mousing up
|
2009-06-29 14:38:30 +00:00
|
|
|
// - when we are in select mode, select the element, remember the position
|
2009-06-25 02:54:07 +00:00
|
|
|
// and do nothing else
|
2009-06-07 00:17:21 +00:00
|
|
|
var mouseDown = function(evt)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-09-28 20:14:18 +00:00
|
|
|
console.dir(container);
|
|
|
|
var mouse_x = evt.pageX - container.parentNode.offsetLeft + container.parentNode.scrollLeft - container.offsetLeft;
|
|
|
|
var mouse_y = evt.pageY - container.parentNode.offsetTop + container.parentNode.scrollTop - container.offsetTop;
|
2009-08-19 17:08:05 +00:00
|
|
|
|
2009-09-05 23:09:46 +00:00
|
|
|
evt.preventDefault();
|
2009-09-04 22:22:40 +00:00
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
if($.inArray(current_mode, ['select', 'resize']) == -1) {
|
|
|
|
addGradient();
|
|
|
|
}
|
2009-09-09 14:18:24 +00:00
|
|
|
|
2009-09-11 13:32:56 +00:00
|
|
|
x = mouse_x / current_zoom;
|
|
|
|
y = mouse_y / current_zoom;
|
2009-09-09 14:18:24 +00:00
|
|
|
|
2009-08-24 17:40:41 +00:00
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-08-19 17:08:05 +00:00
|
|
|
|
2009-06-03 08:41:25 +00:00
|
|
|
switch (current_mode) {
|
|
|
|
case "select":
|
2009-06-08 02:17:07 +00:00
|
|
|
started = true;
|
2009-06-25 02:54:07 +00:00
|
|
|
current_resize_mode = "none";
|
2009-06-07 05:07:23 +00:00
|
|
|
var t = evt.target;
|
2009-09-21 02:05:58 +00:00
|
|
|
// if this element is in a group, go up until we reach the top-level group
|
|
|
|
// just below the layer groups
|
2009-09-05 23:09:46 +00:00
|
|
|
// TODO: once we implement links, we also would have to check for <a> elements
|
2009-09-21 02:05:58 +00:00
|
|
|
while (t.parentNode.parentNode.tagName == "g") {
|
2009-09-05 22:20:36 +00:00
|
|
|
t = t.parentNode;
|
|
|
|
}
|
2009-06-18 04:20:03 +00:00
|
|
|
// WebKit returns <div> when the canvas is clicked, Firefox/Opera return <svg>
|
2009-07-01 21:59:11 +00:00
|
|
|
var nodeName = t.nodeName.toLowerCase();
|
|
|
|
if (nodeName != "div" && nodeName != "svg") {
|
2009-07-01 19:54:51 +00:00
|
|
|
// if this element is not yet selected, clear selection and select it
|
|
|
|
if (selectedElements.indexOf(t) == -1) {
|
2009-09-06 23:25:12 +00:00
|
|
|
// only clear selection if shift is not pressed (otherwise, add
|
|
|
|
// element to selection)
|
|
|
|
if (!evt.shiftKey) {
|
|
|
|
canvas.clearSelection();
|
|
|
|
}
|
2009-07-01 19:54:51 +00:00
|
|
|
canvas.addToSelection([t]);
|
2009-09-06 23:25:12 +00:00
|
|
|
justSelected = t;
|
2009-08-08 14:09:30 +00:00
|
|
|
current_poly = null;
|
2009-07-01 19:54:51 +00:00
|
|
|
}
|
2009-08-08 14:03:14 +00:00
|
|
|
// else if it's a poly, go into polyedit mode in mouseup
|
2009-06-30 06:24:41 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-07-01 19:28:08 +00:00
|
|
|
canvas.clearSelection();
|
2009-06-30 06:24:41 +00:00
|
|
|
current_mode = "multiselect";
|
2009-07-01 19:28:08 +00:00
|
|
|
if (rubberBox == null) {
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
}
|
2009-09-10 19:20:26 +00:00
|
|
|
start_x *= current_zoom;
|
|
|
|
start_y *= current_zoom;
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': start_x,
|
|
|
|
'y': start_y,
|
|
|
|
'width': 0,
|
|
|
|
'height': 0,
|
|
|
|
'display': 'inline'
|
|
|
|
}, 100);
|
2009-06-07 05:07:23 +00:00
|
|
|
}
|
2009-06-03 08:41:25 +00:00
|
|
|
break;
|
2009-09-16 15:45:50 +00:00
|
|
|
case "zoom":
|
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-09-16 17:18:11 +00:00
|
|
|
if (rubberBox == null) {
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
}
|
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': start_x * current_zoom,
|
|
|
|
'y': start_y * current_zoom,
|
|
|
|
'width': 0,
|
|
|
|
'height': 0,
|
|
|
|
'display': 'inline'
|
|
|
|
}, 100);
|
2009-09-16 15:45:50 +00:00
|
|
|
break;
|
2009-06-23 05:17:53 +00:00
|
|
|
case "resize":
|
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
break;
|
2009-06-03 08:41:25 +00:00
|
|
|
case "fhellipse":
|
|
|
|
case "fhrect":
|
|
|
|
case "path":
|
|
|
|
started = true;
|
2009-06-08 02:17:07 +00:00
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-08-07 17:19:32 +00:00
|
|
|
d_attr = x + "," + y + " ";
|
2009-09-01 19:52:34 +00:00
|
|
|
var stroke_w = cur_shape.stroke_width == 0?1:cur_shape.stroke_width;
|
2009-06-03 08:41:25 +00:00
|
|
|
addSvgElementFromJson({
|
2009-08-07 17:19:32 +00:00
|
|
|
"element": "polyline",
|
2009-06-03 08:41:25 +00:00
|
|
|
"attr": {
|
2009-08-07 17:19:32 +00:00
|
|
|
"points": d_attr,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-06-03 08:41:25 +00:00
|
|
|
"fill": "none",
|
2009-08-31 14:54:59 +00:00
|
|
|
"stroke": cur_shape.stroke,
|
2009-09-01 19:52:34 +00:00
|
|
|
"stroke-width": stroke_w,
|
2009-08-31 14:54:59 +00:00
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
2009-08-16 06:02:26 +00:00
|
|
|
"stroke-linecap": "round",
|
|
|
|
"stroke-linejoin": "round",
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
|
|
|
"style": "pointer-events:all"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
freehand_min_x = x;
|
|
|
|
freehand_max_x = x;
|
|
|
|
freehand_min_y = y;
|
|
|
|
freehand_max_y = y;
|
|
|
|
break;
|
2009-09-04 22:22:40 +00:00
|
|
|
case "image":
|
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
var newImage = addSvgElementFromJson({
|
|
|
|
"element": "image",
|
|
|
|
"attr": {
|
|
|
|
"x": x,
|
|
|
|
"y": y,
|
|
|
|
"width": 0,
|
|
|
|
"height": 0,
|
|
|
|
"id": getNextId(),
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
|
|
|
"style": "pointer-events:all"
|
2009-09-04 22:22:40 +00:00
|
|
|
}
|
|
|
|
});
|
2009-09-05 11:59:25 +00:00
|
|
|
newImage.setAttributeNS(xlinkns, "href", "images/logo.png");
|
2009-09-04 22:22:40 +00:00
|
|
|
break;
|
2009-06-03 08:41:25 +00:00
|
|
|
case "square":
|
2009-06-25 02:54:07 +00:00
|
|
|
// FIXME: once we create the rect, we lose information that this was a square
|
2009-08-21 12:21:43 +00:00
|
|
|
// (for resizing purposes this could be important)
|
2009-06-03 08:41:25 +00:00
|
|
|
case "rect":
|
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"x": x,
|
|
|
|
"y": y,
|
|
|
|
"width": 0,
|
|
|
|
"height": 0,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
|
|
|
"style": "pointer-events:all"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "line":
|
|
|
|
started = true;
|
2009-09-01 19:52:34 +00:00
|
|
|
var stroke_w = cur_shape.stroke_width == 0?1:cur_shape.stroke_width;
|
2009-06-03 08:41:25 +00:00
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "line",
|
|
|
|
"attr": {
|
|
|
|
"x1": x,
|
|
|
|
"y1": y,
|
|
|
|
"x2": x,
|
|
|
|
"y2": y,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"stroke": cur_shape.stroke,
|
2009-09-01 19:52:34 +00:00
|
|
|
"stroke-width": stroke_w,
|
2009-08-31 14:54:59 +00:00
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
2009-06-17 17:36:00 +00:00
|
|
|
"fill": "none",
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
|
|
|
"style": "pointer-events:all"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "circle":
|
2009-06-03 15:00:48 +00:00
|
|
|
started = true;
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "circle",
|
|
|
|
"attr": {
|
|
|
|
"cx": x,
|
|
|
|
"cy": y,
|
|
|
|
"r": 0,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
|
|
|
"style": "pointer-events:all"
|
2009-06-03 15:00:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2009-06-03 08:41:25 +00:00
|
|
|
case "ellipse":
|
|
|
|
started = true;
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "ellipse",
|
|
|
|
"attr": {
|
|
|
|
"cx": x,
|
|
|
|
"cy": y,
|
|
|
|
"rx": 0,
|
|
|
|
"ry": 0,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
|
|
|
"style": "pointer-events:all"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
2009-06-03 15:00:48 +00:00
|
|
|
break;
|
2009-06-10 03:12:19 +00:00
|
|
|
case "text":
|
|
|
|
started = true;
|
|
|
|
var newText = addSvgElementFromJson({
|
|
|
|
"element": "text",
|
|
|
|
"attr": {
|
|
|
|
"x": x,
|
|
|
|
"y": y,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_text.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_text.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-06-11 15:18:46 +00:00
|
|
|
// fix for bug where text elements were always 50% opacity
|
2009-08-31 14:54:59 +00:00
|
|
|
"opacity": cur_shape.opacity,
|
|
|
|
"font-size": cur_text.font_size,
|
|
|
|
"font-family": cur_text.font_family,
|
2009-09-21 22:16:44 +00:00
|
|
|
"text-anchor": "middle",
|
|
|
|
"style": "pointer-events:all"
|
2009-06-10 03:12:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
newText.textContent = "text";
|
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
case "poly":
|
|
|
|
started = true;
|
|
|
|
break;
|
2009-08-08 14:03:14 +00:00
|
|
|
case "polyedit":
|
|
|
|
started = true;
|
2009-08-29 22:10:52 +00:00
|
|
|
current_poly_oldd = current_poly.getAttribute("d");
|
2009-08-08 22:17:59 +00:00
|
|
|
var id = evt.target.id;
|
|
|
|
if (id.substr(0,14) == "polypointgrip_") {
|
|
|
|
current_poly_pt_drag = parseInt(id.substr(14));
|
2009-09-25 17:59:32 +00:00
|
|
|
} else if(id.indexOf("ctrlpointgrip_") == 0) {
|
|
|
|
current_ctrl_pt_drag = id.split('_')[1];
|
2009-08-08 22:17:59 +00:00
|
|
|
}
|
2009-08-24 17:40:41 +00:00
|
|
|
|
2009-09-25 17:59:32 +00:00
|
|
|
if(current_poly_pt_drag == -1 && current_ctrl_pt_drag == -1) {
|
2009-08-24 17:40:41 +00:00
|
|
|
canvas.clearSelection();
|
|
|
|
canvas.setMode("multiselect");
|
|
|
|
if (rubberBox == null) {
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
}
|
2009-08-24 18:39:22 +00:00
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': start_x,
|
|
|
|
'y': start_y,
|
|
|
|
'width': 0,
|
|
|
|
'height': 0,
|
|
|
|
'display': 'inline'
|
2009-08-24 19:19:27 +00:00
|
|
|
}, 100);
|
2009-08-24 17:40:41 +00:00
|
|
|
}
|
|
|
|
|
2009-08-26 19:45:28 +00:00
|
|
|
break;
|
|
|
|
case "rotate":
|
|
|
|
started = true;
|
2009-09-04 05:10:48 +00:00
|
|
|
// we are starting an undoable change (a drag-rotation)
|
|
|
|
canvas.beginUndoableChange("transform", selectedElements);
|
2009-08-08 14:03:14 +00:00
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
default:
|
|
|
|
console.log("Unknown mode in mousedown: " + current_mode);
|
|
|
|
break;
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-08-17 03:11:30 +00:00
|
|
|
// in this function we do not record any state changes yet (but we do update
|
2009-06-25 02:54:07 +00:00
|
|
|
// any elements that are still being created, moved or resized on the canvas)
|
2009-07-10 05:04:06 +00:00
|
|
|
// TODO: svgcanvas should just retain a reference to the image being dragged instead
|
|
|
|
// of the getId() and getElementById() funkiness - this will help us customize the ids
|
|
|
|
// a little bit for squares and polys
|
2009-06-07 00:17:21 +00:00
|
|
|
var mouseMove = function(evt)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-06-02 16:40:33 +00:00
|
|
|
if (!started) return;
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-09-28 20:14:18 +00:00
|
|
|
var mouse_x = evt.pageX - container.parentNode.offsetLeft + container.parentNode.scrollLeft - container.offsetLeft;
|
|
|
|
var mouse_y = evt.pageY - container.parentNode.offsetTop + container.parentNode.scrollTop - container.offsetTop;
|
2009-06-05 12:33:02 +00:00
|
|
|
var shape = svgdoc.getElementById(getId());
|
2009-09-04 22:22:40 +00:00
|
|
|
|
2009-09-11 13:32:56 +00:00
|
|
|
x = mouse_x / current_zoom;
|
|
|
|
y = mouse_y / current_zoom;
|
2009-09-09 14:18:24 +00:00
|
|
|
|
2009-09-06 23:25:12 +00:00
|
|
|
evt.preventDefault();
|
2009-09-04 22:22:40 +00:00
|
|
|
|
2009-06-02 16:40:33 +00:00
|
|
|
switch (current_mode)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-06-08 02:17:07 +00:00
|
|
|
case "select":
|
2009-08-30 02:37:16 +00:00
|
|
|
// we temporarily use a translate on the element(s) being dragged
|
2009-07-01 19:28:08 +00:00
|
|
|
// this transform is removed upon mousing up and the element is
|
|
|
|
// relocated to the new location
|
2009-07-01 19:54:51 +00:00
|
|
|
if (selectedElements[0] != null) {
|
2009-06-08 02:17:07 +00:00
|
|
|
var dx = x - start_x;
|
|
|
|
var dy = y - start_y;
|
2009-08-08 14:03:14 +00:00
|
|
|
if (dx != 0 || dy != 0) {
|
|
|
|
var ts = ["translate(",dx,",",dy,")"].join('');
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected == null) break;
|
2009-08-16 15:07:00 +00:00
|
|
|
var box = canvas.getBBox(selected);
|
2009-08-21 15:53:36 +00:00
|
|
|
selectedBBoxes[i].x = box.x + dx;
|
|
|
|
selectedBBoxes[i].y = box.y + dy;
|
2009-08-16 14:56:22 +00:00
|
|
|
var angle = canvas.getRotationAngle(selected);
|
2009-08-16 19:18:22 +00:00
|
|
|
if (angle) {
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(box.x + box.width/2),
|
|
|
|
cy = round(box.y + box.height/2);
|
2009-08-30 02:37:16 +00:00
|
|
|
var xform = ts + [" rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
2009-08-21 15:53:36 +00:00
|
|
|
var r = Math.sqrt( dx*dx + dy*dy );
|
|
|
|
var theta = Math.atan2(dy,dx) - angle * Math.PI / 180.0;
|
2009-08-30 02:37:16 +00:00
|
|
|
selected.setAttribute("transform", xform);
|
|
|
|
box.x += r * Math.cos(theta); box.y += r * Math.sin(theta);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
selected.setAttribute("transform", ts);
|
|
|
|
box.x += dx; box.y += dy;
|
2009-08-16 14:56:22 +00:00
|
|
|
}
|
2009-08-21 15:53:36 +00:00
|
|
|
// update our internal bbox that we're tracking while dragging
|
2009-08-08 14:03:14 +00:00
|
|
|
selectorManager.requestSelector(selected).resize(box);
|
|
|
|
}
|
2009-07-01 19:54:51 +00:00
|
|
|
}
|
2009-06-08 02:17:07 +00:00
|
|
|
}
|
2009-06-10 03:12:19 +00:00
|
|
|
break;
|
2009-06-30 06:24:41 +00:00
|
|
|
case "multiselect":
|
2009-09-10 19:20:26 +00:00
|
|
|
x *= current_zoom;
|
|
|
|
y *= current_zoom;
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': Math.min(start_x,x),
|
2009-08-24 19:20:52 +00:00
|
|
|
'y': Math.min(start_y,y),
|
2009-08-24 19:19:27 +00:00
|
|
|
'width': Math.abs(x-start_x),
|
|
|
|
'height': Math.abs(y-start_y)
|
|
|
|
},100);
|
2009-07-02 04:50:24 +00:00
|
|
|
|
|
|
|
// this code will probably be faster than using getIntersectionList(), but
|
|
|
|
// not as accurate (only grabs an element if the mouse happens to pass over
|
|
|
|
// its bbox and elements would never be released from selection)
|
|
|
|
// var nodeName = evt.target.nodeName.toLowerCase();
|
|
|
|
// if (nodeName != "div" && nodeName != "svg") {
|
|
|
|
// canvas.addToSelection([evt.target]);
|
|
|
|
// }
|
2009-07-03 02:49:42 +00:00
|
|
|
|
|
|
|
// clear out selection and set it to the new list
|
2009-07-02 04:50:24 +00:00
|
|
|
canvas.clearSelection();
|
|
|
|
canvas.addToSelection(getIntersectionList());
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-03 02:49:42 +00:00
|
|
|
/*
|
|
|
|
// for each selected:
|
|
|
|
// - if newList contains selected, do nothing
|
|
|
|
// - if newList doesn't contain selected, remove it from selected
|
|
|
|
// - for any newList that was not in selectedElements, add it to selected
|
|
|
|
var elemsToRemove = [];
|
|
|
|
var newList = getIntersectionList();
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var ind = newList.indexOf(selectedElements[i]);
|
|
|
|
if (ind == -1) {
|
|
|
|
elemsToRemove.push(selectedElements[i]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newList[ind] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (elemsToRemove.length > 0)
|
|
|
|
canvas.removeFromSelection(elemsToRemove);
|
|
|
|
*/
|
2009-06-30 06:24:41 +00:00
|
|
|
break;
|
2009-06-23 05:17:53 +00:00
|
|
|
case "resize":
|
|
|
|
// we track the resize bounding box and translate/scale the selected element
|
|
|
|
// while the mouse is down, when mouse goes up, we use this to recalculate
|
|
|
|
// the shape's coordinates
|
2009-08-16 15:07:00 +00:00
|
|
|
var box=canvas.getBBox(selected), left=box.x, top=box.y, width=box.width,
|
2009-06-23 05:17:53 +00:00
|
|
|
height=box.height, dx=(x-start_x), dy=(y-start_y);
|
2009-08-22 19:08:01 +00:00
|
|
|
|
2009-08-21 15:53:36 +00:00
|
|
|
// if rotated, adjust the dx,dy values
|
2009-08-22 19:08:01 +00:00
|
|
|
var angle = canvas.getRotationAngle(selected);
|
2009-08-21 15:53:36 +00:00
|
|
|
if (angle) {
|
|
|
|
var r = Math.sqrt( dx*dx + dy*dy );
|
2009-08-22 19:08:01 +00:00
|
|
|
var theta = Math.atan2(dy,dx) - angle * Math.PI / 180.0;
|
2009-08-21 15:53:36 +00:00
|
|
|
dx = r * Math.cos(theta);
|
|
|
|
dy = r * Math.sin(theta);
|
|
|
|
}
|
|
|
|
|
2009-08-22 19:08:01 +00:00
|
|
|
// if not stretching in y direction, set dy to 0
|
|
|
|
// if not stretching in x direction, set dx to 0
|
|
|
|
if(current_resize_mode.indexOf("n")==-1 && current_resize_mode.indexOf("s")==-1) {
|
|
|
|
dy = 0;
|
|
|
|
}
|
|
|
|
if(current_resize_mode.indexOf("e")==-1 && current_resize_mode.indexOf("w")==-1) {
|
|
|
|
dx = 0;
|
|
|
|
}
|
|
|
|
|
2009-06-23 05:17:53 +00:00
|
|
|
var ts = null;
|
2009-08-22 19:08:01 +00:00
|
|
|
var tx = 0, ty = 0;
|
2009-09-29 03:10:29 +00:00
|
|
|
var sy = height ? (height+dy)/height : 1,
|
|
|
|
sx = width ? (width+dx)/width : 1;
|
2009-08-27 22:26:47 +00:00
|
|
|
// if we are dragging on the north side, then adjust the scale factor and ty
|
2009-06-23 05:17:53 +00:00
|
|
|
if(current_resize_mode.indexOf("n") != -1) {
|
2009-09-29 03:10:29 +00:00
|
|
|
sy = height ? (height-dy)/height : 1;
|
2009-08-22 19:08:01 +00:00
|
|
|
ty = height;
|
2009-06-23 05:17:53 +00:00
|
|
|
}
|
2009-08-27 22:26:47 +00:00
|
|
|
|
|
|
|
// if we dragging on the east side, then adjust the scale factor and tx
|
2009-08-22 19:08:01 +00:00
|
|
|
if(current_resize_mode.indexOf("w") != -1) {
|
2009-09-29 03:10:29 +00:00
|
|
|
sx = width ? (width-dx)/width : 1;
|
2009-08-22 19:08:01 +00:00
|
|
|
tx = width;
|
2009-06-23 05:17:53 +00:00
|
|
|
}
|
2009-08-22 19:08:01 +00:00
|
|
|
|
|
|
|
// find the rotation transform and prepend it
|
|
|
|
var ts = [" translate(", (left+tx), ",", (top+ty), ") scale(", sx, ",", sy,
|
|
|
|
") translate(", -(left+tx), ",", -(top+ty), ")"].join('');
|
|
|
|
if (angle) {
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(left+width/2),
|
|
|
|
cy = round(top+height/2);
|
2009-08-22 19:08:01 +00:00
|
|
|
ts = ["rotate(", angle, " ", cx, ",", cy, ")", ts].join('')
|
|
|
|
}
|
|
|
|
selected.setAttribute("transform", ts);
|
2009-08-27 22:26:47 +00:00
|
|
|
|
|
|
|
var selectedBBox = selectedBBoxes[0];
|
|
|
|
|
|
|
|
// reset selected bbox top-left position
|
|
|
|
selectedBBox.x = left;
|
|
|
|
selectedBBox.y = top;
|
2009-08-22 19:08:01 +00:00
|
|
|
|
2009-08-27 22:26:47 +00:00
|
|
|
// if this is a translate, adjust the box position
|
2009-08-22 19:08:01 +00:00
|
|
|
if (tx) {
|
2009-08-27 22:26:47 +00:00
|
|
|
selectedBBox.x += dx;
|
2009-08-22 19:08:01 +00:00
|
|
|
}
|
|
|
|
if (ty) {
|
2009-08-27 22:26:47 +00:00
|
|
|
selectedBBox.y += dy;
|
2009-08-22 19:08:01 +00:00
|
|
|
}
|
2009-08-27 22:26:47 +00:00
|
|
|
|
|
|
|
// update box width/height
|
2009-09-17 14:47:54 +00:00
|
|
|
selectedBBox.width = round(width*sx);
|
|
|
|
selectedBBox.height = round(height*sy);
|
2009-08-27 22:26:47 +00:00
|
|
|
|
2009-06-23 05:46:47 +00:00
|
|
|
// normalize selectedBBox
|
|
|
|
if (selectedBBox.width < 0) {
|
2009-08-27 22:26:47 +00:00
|
|
|
selectedBBox.width *= -1;
|
|
|
|
// if we are dragging on the east side and scaled negatively
|
|
|
|
if(current_resize_mode.indexOf("e") != -1 && sx < 0) {
|
|
|
|
selectedBBox.x = box.x - selectedBBox.width;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
selectedBBox.x -= selectedBBox.width;
|
|
|
|
}
|
2009-06-23 05:46:47 +00:00
|
|
|
}
|
|
|
|
if (selectedBBox.height < 0) {
|
2009-08-27 22:26:47 +00:00
|
|
|
selectedBBox.height *= -1;
|
|
|
|
// if we are dragging on the south side and scaled negatively
|
|
|
|
if(current_resize_mode.indexOf("s") != -1 && sy < 0) {
|
|
|
|
selectedBBox.y = box.y - selectedBBox.height;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
selectedBBox.y -= selectedBBox.height;
|
|
|
|
}
|
2009-08-16 14:56:22 +00:00
|
|
|
}
|
2009-08-27 22:26:47 +00:00
|
|
|
|
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
selectorManager.requestSelector(selected).resize(selectedBBox);
|
2009-06-23 07:48:15 +00:00
|
|
|
break;
|
2009-09-16 17:18:11 +00:00
|
|
|
case "zoom":
|
|
|
|
x *= current_zoom;
|
|
|
|
y *= current_zoom;
|
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': Math.min(start_x*current_zoom,x),
|
|
|
|
'y': Math.min(start_y*current_zoom,y),
|
|
|
|
'width': Math.abs(x-start_x*current_zoom),
|
|
|
|
'height': Math.abs(y-start_y*current_zoom)
|
|
|
|
},100);
|
|
|
|
break;
|
2009-06-10 03:12:19 +00:00
|
|
|
case "text":
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(shape,{
|
|
|
|
'x': x,
|
|
|
|
'y': y
|
|
|
|
},1000);
|
2009-06-10 03:12:19 +00:00
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "line":
|
2009-07-30 22:45:59 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-06-02 16:40:33 +00:00
|
|
|
shape.setAttributeNS(null, "x2", x);
|
|
|
|
shape.setAttributeNS(null, "y2", y);
|
2009-08-17 06:56:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "square":
|
|
|
|
var size = Math.max( Math.abs(x - start_x), Math.abs(y - start_y) );
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(shape,{
|
|
|
|
'width': size,
|
|
|
|
'height': size,
|
|
|
|
'x': start_x < x ? start_x : start_x - size,
|
|
|
|
'y': start_y < y ? start_y : start_y - size
|
|
|
|
},1000);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "rect":
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(shape,{
|
|
|
|
'width': Math.abs(x-start_x),
|
|
|
|
'height': Math.abs(y-start_y),
|
|
|
|
'x': Math.min(start_x,x),
|
|
|
|
'y': Math.min(start_y,y)
|
|
|
|
},1000);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-09-04 22:22:40 +00:00
|
|
|
case "image":
|
|
|
|
assignAttributes(shape,{
|
|
|
|
'width': Math.abs(x-start_x),
|
|
|
|
'height': Math.abs(y-start_y),
|
|
|
|
'x': Math.min(start_x,x),
|
|
|
|
'y': Math.min(start_y,y)
|
|
|
|
},1000);
|
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "circle":
|
|
|
|
var cx = shape.getAttributeNS(null, "cx");
|
|
|
|
var cy = shape.getAttributeNS(null, "cy");
|
|
|
|
var rad = Math.sqrt( (x-cx)*(x-cx) + (y-cy)*(y-cy) );
|
2009-06-03 15:00:48 +00:00
|
|
|
shape.setAttributeNS(null, "r", rad);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "ellipse":
|
|
|
|
var cx = shape.getAttributeNS(null, "cx");
|
|
|
|
var cy = shape.getAttributeNS(null, "cy");
|
2009-07-30 22:45:59 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-06-02 16:40:33 +00:00
|
|
|
shape.setAttributeNS(null, "rx", Math.abs(x - cx) );
|
|
|
|
shape.setAttributeNS(null, "ry", Math.abs(y - cy) );
|
2009-08-17 06:56:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "fhellipse":
|
|
|
|
case "fhrect":
|
|
|
|
freehand_min_x = Math.min(x, freehand_min_x);
|
|
|
|
freehand_max_x = Math.max(x, freehand_max_x);
|
|
|
|
freehand_min_y = Math.min(y, freehand_min_y);
|
|
|
|
freehand_max_y = Math.max(y, freehand_max_y);
|
2009-06-03 08:41:25 +00:00
|
|
|
// break; missing on purpose
|
2009-06-02 16:40:33 +00:00
|
|
|
case "path":
|
2009-06-08 02:17:07 +00:00
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-08-07 17:19:32 +00:00
|
|
|
d_attr += + x + "," + y + " ";
|
|
|
|
shape.setAttributeNS(null, "points", d_attr);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-08-07 17:19:32 +00:00
|
|
|
// update poly stretch line coordinates
|
2009-08-07 05:44:55 +00:00
|
|
|
case "poly":
|
|
|
|
var line = document.getElementById("poly_stretch_line");
|
|
|
|
if (line) {
|
2009-09-10 20:34:20 +00:00
|
|
|
line.setAttribute("x2", x *= current_zoom);
|
|
|
|
line.setAttribute("y2", y *= current_zoom);
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-08-08 22:17:59 +00:00
|
|
|
case "polyedit":
|
|
|
|
// if we are dragging a point, let's move it
|
|
|
|
if (current_poly_pt_drag != -1 && current_poly) {
|
|
|
|
var i = current_poly_pt_drag * 2;
|
2009-08-19 03:42:27 +00:00
|
|
|
|
|
|
|
// if the image is rotated, then we must modify the x,y mouse coordinates
|
|
|
|
// and rotate them into the shape's rotated coordinate system
|
2009-08-21 12:21:43 +00:00
|
|
|
var angle = canvas.getRotationAngle(current_poly) * Math.PI / 180.0;
|
2009-08-19 03:42:27 +00:00
|
|
|
if (angle) {
|
2009-08-27 22:26:47 +00:00
|
|
|
// calculate the shape's old center that was used for rotation
|
2009-08-27 04:43:05 +00:00
|
|
|
var box = selectedBBoxes[0];
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(box.x + box.width/2) * current_zoom,
|
|
|
|
cy = round(box.y + box.height/2) * current_zoom;
|
2009-09-11 16:49:57 +00:00
|
|
|
var dx = mouse_x - cx, dy = mouse_y - cy;
|
2009-08-19 03:42:27 +00:00
|
|
|
var r = Math.sqrt( dx*dx + dy*dy );
|
|
|
|
var theta = Math.atan2(dy,dx) - angle;
|
2009-09-12 04:49:50 +00:00
|
|
|
current_poly_pts[i] = mouse_x = cx + r * Math.cos(theta);
|
|
|
|
current_poly_pts[i+1] = mouse_y = cy + r * Math.sin(theta);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
current_poly_pts[i] = x * current_zoom;
|
|
|
|
current_poly_pts[i+1] = y * current_zoom;
|
2009-08-19 03:42:27 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-08 22:17:59 +00:00
|
|
|
// reset the path's d attribute using current_poly_pts
|
|
|
|
var oldd = current_poly.getAttribute("d");
|
|
|
|
var closedPath = (oldd[oldd.length-1] == 'z' || oldd[oldd.length-1] == 'Z');
|
|
|
|
var len = current_poly_pts.length/2;
|
|
|
|
var arr = new Array(len+1);
|
2009-09-12 04:49:50 +00:00
|
|
|
var curx = current_poly_pts[0]/current_zoom,
|
|
|
|
cury = current_poly_pts[1]/current_zoom;
|
2009-08-08 22:17:59 +00:00
|
|
|
arr[0] = ["M", curx, ",", cury].join('');
|
|
|
|
for (var j = 1; j < len; ++j) {
|
2009-09-11 16:49:57 +00:00
|
|
|
var px = current_poly_pts[j*2]/current_zoom, py = current_poly_pts[j*2+1]/current_zoom;
|
2009-09-17 14:47:54 +00:00
|
|
|
arr[j] = ["l", round(px-curx), ",", round(py-cury)].join('');
|
2009-08-08 22:17:59 +00:00
|
|
|
curx = px;
|
|
|
|
cury = py;
|
|
|
|
}
|
|
|
|
if (closedPath) {
|
|
|
|
arr[len] = "z";
|
|
|
|
}
|
2009-08-29 18:38:34 +00:00
|
|
|
// we don't want to undo this, we are in the middle of a drag
|
2009-08-08 22:17:59 +00:00
|
|
|
current_poly.setAttribute("d", arr.join(' '));
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-08 22:17:59 +00:00
|
|
|
// move the point grip
|
|
|
|
var grip = document.getElementById("polypointgrip_" + current_poly_pt_drag);
|
|
|
|
if (grip) {
|
2009-09-11 13:32:56 +00:00
|
|
|
grip.setAttribute("cx", mouse_x);
|
|
|
|
grip.setAttribute("cy", mouse_y);
|
2009-08-08 22:17:59 +00:00
|
|
|
}
|
2009-09-25 17:59:32 +00:00
|
|
|
} else if (current_ctrl_pt_drag != -1 && current_poly) {
|
|
|
|
// Moving the control point. Since only one segment is altered,
|
|
|
|
// we only need to do a pathSegList replace.
|
|
|
|
var data = current_ctrl_pt_drag.split('c');
|
|
|
|
var index = data[0]-0;
|
|
|
|
var ctrl_num = data[1]-0;
|
|
|
|
var c_item = current_poly.pathSegList.getItem(index+1);
|
|
|
|
c_item['x' + ctrl_num] = x;
|
|
|
|
c_item['y' + ctrl_num] = y;
|
|
|
|
var newCurve = current_poly.createSVGPathSegCurvetoCubicAbs(c_item.x,c_item.y, c_item.x1,c_item.y1, c_item.x2,c_item.y2);
|
|
|
|
current_poly.pathSegList.replaceItem(newCurve, index+1);
|
|
|
|
|
|
|
|
var grip = document.getElementById("ctrlpointgrip_" + current_ctrl_pt_drag);
|
|
|
|
if(grip) {
|
|
|
|
grip.setAttribute("cx", mouse_x);
|
|
|
|
grip.setAttribute("cy", mouse_y);
|
|
|
|
|
|
|
|
var line = document.getElementById("ctrlLine_"+current_ctrl_pt_drag);
|
|
|
|
line.setAttribute("x2", mouse_x);
|
|
|
|
line.setAttribute("y2", mouse_y);
|
|
|
|
}
|
2009-08-08 22:17:59 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-08-26 19:45:28 +00:00
|
|
|
case "rotate":
|
2009-09-04 16:48:20 +00:00
|
|
|
var box = canvas.getBBox(selected),
|
2009-09-17 14:47:54 +00:00
|
|
|
cx = round(box.x + box.width/2),
|
|
|
|
cy = round(box.y + box.height/2);
|
|
|
|
var angle = round(((Math.atan2(cy-y,cx-x) * (180/Math.PI))-90) % 360);
|
2009-09-04 05:10:48 +00:00
|
|
|
canvas.setRotationAngle(angle<-180?(360+angle):angle, true);
|
2009-08-26 19:45:28 +00:00
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-08-08 14:03:14 +00:00
|
|
|
var removeAllPointGripsFromPoly = function() {
|
|
|
|
// loop through and hide all pointgrips
|
|
|
|
var i = current_poly_pts.length/2;
|
|
|
|
while(i--) {
|
|
|
|
document.getElementById("polypointgrip_"+i).setAttribute("display", "none");
|
|
|
|
}
|
2009-09-25 17:59:32 +00:00
|
|
|
|
2009-08-30 02:24:20 +00:00
|
|
|
var line = document.getElementById("poly_stretch_line");
|
|
|
|
if (line) line.setAttribute("display", "none");
|
2009-09-25 17:59:32 +00:00
|
|
|
|
|
|
|
// Should this be only for individual control grips + lines?
|
|
|
|
var ctrlContainer = document.getElementById("ctrlpointgrip_container");
|
|
|
|
if(ctrlContainer) ctrlContainer.setAttribute("display", "none");
|
2009-08-08 14:03:14 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-08 03:36:01 +00:00
|
|
|
var addAllPointGripsToPoly = function() {
|
2009-09-12 04:49:50 +00:00
|
|
|
// loop through and show all pointgrips
|
2009-08-31 18:02:15 +00:00
|
|
|
var len = current_poly_pts.length;
|
|
|
|
for (var i = 0; i < len; i += 2) {
|
2009-08-08 14:03:14 +00:00
|
|
|
var grip = document.getElementById("polypointgrip_"+i/2);
|
2009-08-31 18:02:15 +00:00
|
|
|
if (grip) {
|
|
|
|
assignAttributes(grip, {
|
|
|
|
'cx': current_poly_pts[i],
|
|
|
|
'cy': current_poly_pts[i+1],
|
|
|
|
'display': 'inline'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
addPointGripToPoly(current_poly_pts[i], current_poly_pts[i+1],i/2);
|
|
|
|
}
|
2009-09-25 20:03:20 +00:00
|
|
|
|
|
|
|
var index = i/2;
|
|
|
|
var item = current_poly.pathSegList.getItem(index);
|
|
|
|
if(item.pathSegType == 6) {
|
|
|
|
index -= 1;
|
|
|
|
// Same code as when making a curve, needs to be in own function
|
|
|
|
var pt_index = index*2;
|
|
|
|
var cur_x = current_poly_pts[pt_index];
|
|
|
|
var cur_y = current_poly_pts[pt_index+1];
|
|
|
|
if(pt_index + 2 >= current_poly_pts.length) {
|
|
|
|
var next_x = current_poly_pts[0];
|
|
|
|
var next_y = current_poly_pts[1];
|
|
|
|
} else {
|
|
|
|
var next_x = current_poly_pts[pt_index+2];
|
|
|
|
var next_y = current_poly_pts[pt_index+3];
|
|
|
|
}
|
|
|
|
addControlPointGrip(item.x1,item.y1, cur_x,cur_y, index+'c1');
|
|
|
|
addControlPointGrip(item.x2,item.y2, next_x,next_y, index+'c2');
|
|
|
|
} else if(item.pathSegType == 7) {
|
|
|
|
// TODO: Make relative curve handles appear here
|
|
|
|
}
|
2009-08-08 14:03:14 +00:00
|
|
|
}
|
2009-08-31 19:24:45 +00:00
|
|
|
var pointGripContainer = document.getElementById("polypointgrip_container");
|
2009-09-12 04:49:50 +00:00
|
|
|
// FIXME: we cannot just use the same transform as the poly because we might be
|
|
|
|
// at a different zoom level
|
|
|
|
var angle = canvas.getRotationAngle(current_poly);
|
|
|
|
if (angle) {
|
|
|
|
var bbox = canvas.getBBox(current_poly);
|
|
|
|
var cx = (bbox.x + bbox.width/2) * current_zoom,
|
|
|
|
cy = (bbox.y + bbox.height/2) * current_zoom;
|
|
|
|
var xform = ["rotate(", angle, " ", cx, ",", cy, ")"].join("");
|
|
|
|
pointGripContainer.setAttribute("transform", xform);
|
|
|
|
}
|
2009-08-08 03:36:01 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-31 18:02:15 +00:00
|
|
|
var addPointGripToPoly = function(x,y,index) {
|
2009-08-08 03:36:01 +00:00
|
|
|
// create the container of all the point grips
|
|
|
|
var pointGripContainer = document.getElementById("polypointgrip_container");
|
|
|
|
if (!pointGripContainer) {
|
|
|
|
var parent = document.getElementById("selectorParentGroup");
|
|
|
|
pointGripContainer = parent.appendChild(document.createElementNS(svgns, "g"));
|
|
|
|
pointGripContainer.id = "polypointgrip_container";
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-08 03:36:01 +00:00
|
|
|
var pointGrip = document.getElementById("polypointgrip_"+index);
|
|
|
|
// create it
|
|
|
|
if (!pointGrip) {
|
|
|
|
pointGrip = document.createElementNS(svgns, "circle");
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(pointGrip, {
|
|
|
|
'id': "polypointgrip_" + index,
|
|
|
|
'display': "none",
|
|
|
|
'r': 4,
|
|
|
|
'fill': "#0F0",
|
|
|
|
'stroke': "#00F",
|
|
|
|
'stroke-width': 2,
|
|
|
|
'cursor': 'move',
|
2009-09-21 22:16:44 +00:00
|
|
|
'style': 'pointer-events:all'
|
2009-08-24 20:00:48 +00:00
|
|
|
});
|
2009-08-08 03:36:01 +00:00
|
|
|
pointGrip = pointGripContainer.appendChild(pointGrip);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-08 03:36:01 +00:00
|
|
|
var grip = $('#polypointgrip_'+index);
|
2009-08-08 14:03:14 +00:00
|
|
|
grip.mouseover( function() { this.setAttribute("stroke", "#F00"); } );
|
2009-08-08 03:36:01 +00:00
|
|
|
grip.mouseout( function() {this.setAttribute("stroke", "#00F"); } );
|
2009-09-25 17:59:32 +00:00
|
|
|
grip.dblclick( function() {
|
|
|
|
// Toggle segment to curve/straight line
|
|
|
|
var type = current_poly.pathSegList.getItem(index+1).pathSegType;
|
|
|
|
var pt_index = index*2;
|
|
|
|
var cur_x = current_poly_pts[pt_index];
|
|
|
|
var cur_y = current_poly_pts[pt_index+1];
|
|
|
|
if(pt_index + 2 >= current_poly_pts.length) {
|
|
|
|
var next_x = current_poly_pts[0];
|
|
|
|
var next_y = current_poly_pts[1];
|
|
|
|
} else {
|
|
|
|
var next_x = current_poly_pts[pt_index+2];
|
|
|
|
var next_y = current_poly_pts[pt_index+3];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(type != 6) {
|
|
|
|
// Change to CubicAbs curve
|
|
|
|
|
|
|
|
// Get points in between to set as default control points
|
|
|
|
var ct1_x = (next_y/-2 - cur_y/-2) + cur_x;
|
|
|
|
var ct1_y = (next_x/-2 - cur_x/-2) + cur_y;
|
|
|
|
var ct2_x = (next_y/-2 - cur_y/-2) + next_x;
|
|
|
|
var ct2_y = (next_x/-2 - cur_x/-2) + next_y;
|
|
|
|
|
|
|
|
var newCurve = current_poly.createSVGPathSegCurvetoCubicAbs(next_x,next_y, ct1_x,ct1_y, ct2_x,ct2_y);
|
|
|
|
current_poly.pathSegList.replaceItem(newCurve, index+1);
|
|
|
|
|
|
|
|
// Add the control points + lines
|
|
|
|
addControlPointGrip(ct1_x,ct1_y, cur_x,cur_y, index+'c1');
|
|
|
|
addControlPointGrip(ct2_x,ct2_y, next_x,next_y, index+'c2');
|
|
|
|
} else {
|
|
|
|
// Revert to absolute line (should probably be relative)
|
|
|
|
var newStraight = current_poly.createSVGPathSegLinetoAbs(next_x, next_y);
|
|
|
|
current_poly.pathSegList.replaceItem(newStraight, index+1);
|
|
|
|
removeControlPointGrips(index);
|
|
|
|
}
|
|
|
|
});
|
2009-08-08 03:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set up the point grip element and display it
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(pointGrip, {
|
|
|
|
'cx': x,
|
|
|
|
'cy': y,
|
|
|
|
'display': "inline",
|
|
|
|
});
|
2009-08-08 03:36:01 +00:00
|
|
|
};
|
2009-09-25 17:59:32 +00:00
|
|
|
|
|
|
|
var addControlPointGrip = function(x, y, source_x, source_y, id) {
|
|
|
|
// create the container of all the control point grips
|
|
|
|
var ctrlPointGripContainer = document.getElementById("ctrlpointgrip_container");
|
|
|
|
if (!ctrlPointGripContainer) {
|
|
|
|
var parent = document.getElementById("selectorParentGroup");
|
|
|
|
ctrlPointGripContainer = parent.appendChild(document.createElementNS(svgns, "g"));
|
|
|
|
ctrlPointGripContainer.id = "ctrlpointgrip_container";
|
|
|
|
}
|
2009-09-25 20:03:20 +00:00
|
|
|
ctrlPointGripContainer.setAttribute("display", "inline");
|
2009-09-25 17:59:32 +00:00
|
|
|
|
|
|
|
var ctrlLine = document.getElementById("ctrlLine_"+id);
|
|
|
|
if (!ctrlLine) {
|
|
|
|
ctrlLine = document.createElementNS(svgns, "line");
|
|
|
|
assignAttributes(ctrlLine, {
|
|
|
|
'id': "ctrlLine_"+id,
|
|
|
|
'stroke': "#555",
|
|
|
|
'stroke-width': 1,
|
|
|
|
"style": "pointer-events:none"
|
|
|
|
});
|
|
|
|
ctrlLine = ctrlPointGripContainer.appendChild(ctrlLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
assignAttributes(ctrlLine, {
|
|
|
|
'x1': source_x,
|
|
|
|
'y1': source_y,
|
|
|
|
'x2': x,
|
|
|
|
'y2': y,
|
|
|
|
'display': "inline"
|
|
|
|
});
|
|
|
|
|
|
|
|
var pointGrip = document.getElementById("ctrlpointgrip_"+id);
|
|
|
|
// create it
|
|
|
|
if (!pointGrip) {
|
|
|
|
pointGrip = document.createElementNS(svgns, "circle");
|
|
|
|
assignAttributes(pointGrip, {
|
|
|
|
'id': "ctrlpointgrip_" + id,
|
|
|
|
'display': "none",
|
|
|
|
'r': 5,
|
|
|
|
'fill': "#AAA",
|
|
|
|
'stroke': "#55F",
|
|
|
|
'stroke-width': 1,
|
|
|
|
'cursor': 'move',
|
|
|
|
'style': 'pointer-events:all'
|
|
|
|
});
|
|
|
|
pointGrip = ctrlPointGripContainer.appendChild(pointGrip);
|
|
|
|
}
|
|
|
|
|
|
|
|
assignAttributes(pointGrip, {
|
|
|
|
'cx': x,
|
|
|
|
'cy': y,
|
|
|
|
'display': "inline"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var removeControlPointGrips = function(index) {
|
|
|
|
for(var i=1; i <= 2; i++) {
|
|
|
|
document.getElementById("ctrlpointgrip_" + index + "c" + i).setAttribute("display", "none");
|
|
|
|
document.getElementById("ctrlLine_" + index + "c" + i).setAttribute("display", "none");
|
|
|
|
}
|
|
|
|
}
|
2009-08-08 03:36:01 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
// - in create mode, the element's opacity is set properly, we create an InsertElementCommand
|
|
|
|
// and store it on the Undo stack
|
|
|
|
// - in move/resize mode, the element's attributes which were affected by the move/resize are
|
|
|
|
// identified, a ChangeElementCommand is created and stored on the stack for those attrs
|
2009-07-08 00:03:08 +00:00
|
|
|
// this is done in when we recalculate the selected dimensions()
|
2009-06-08 02:17:07 +00:00
|
|
|
var mouseUp = function(evt)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-09-06 23:25:12 +00:00
|
|
|
var tempJustSelected = justSelected;
|
|
|
|
justSelected = null;
|
2009-06-02 16:40:33 +00:00
|
|
|
if (!started) return;
|
|
|
|
|
2009-09-28 20:14:18 +00:00
|
|
|
var mouse_x = evt.pageX - container.parentNode.offsetLeft + container.parentNode.scrollLeft - container.offsetLeft;
|
|
|
|
var mouse_y = evt.pageY - container.parentNode.offsetTop + container.parentNode.scrollTop - container.offsetTop;
|
2009-09-11 13:32:56 +00:00
|
|
|
var x = mouse_x / current_zoom;
|
|
|
|
var y = mouse_y / current_zoom;
|
|
|
|
|
2009-06-02 16:40:33 +00:00
|
|
|
started = false;
|
2009-06-05 12:33:02 +00:00
|
|
|
var element = svgdoc.getElementById(getId());
|
2009-06-06 17:53:38 +00:00
|
|
|
var keep = false;
|
2009-06-02 16:40:33 +00:00
|
|
|
switch (current_mode)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-07-01 19:28:08 +00:00
|
|
|
// intentionally fall-through to select here
|
2009-06-23 05:17:53 +00:00
|
|
|
case "resize":
|
2009-07-01 19:28:08 +00:00
|
|
|
case "multiselect":
|
|
|
|
if (rubberBox != null) {
|
|
|
|
rubberBox.setAttribute("display", "none");
|
2009-08-26 19:59:52 +00:00
|
|
|
curBBoxes = [];
|
2009-07-01 19:28:08 +00:00
|
|
|
}
|
2009-06-23 05:17:53 +00:00
|
|
|
current_mode = "select";
|
2009-06-02 16:40:33 +00:00
|
|
|
case "select":
|
2009-07-01 19:28:08 +00:00
|
|
|
if (selectedElements[0] != null) {
|
|
|
|
// if we only have one selected element
|
|
|
|
if (selectedElements[1] == null) {
|
|
|
|
// set our current stroke/fill properties to the element's
|
|
|
|
var selected = selectedElements[0];
|
2009-09-13 15:50:43 +00:00
|
|
|
if (selected.tagName != "g") {
|
|
|
|
cur_shape.fill = selected.getAttribute("fill");
|
|
|
|
cur_shape.fill_opacity = selected.getAttribute("fill-opacity");
|
|
|
|
cur_shape.stroke = selected.getAttribute("stroke");
|
|
|
|
cur_shape.stroke_opacity = selected.getAttribute("stroke-opacity");
|
|
|
|
cur_shape.stroke_width = selected.getAttribute("stroke-width");
|
|
|
|
cur_shape.stroke_style = selected.getAttribute("stroke-dasharray");
|
|
|
|
}
|
2009-07-01 19:28:08 +00:00
|
|
|
if (selected.tagName == "text") {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_text.font_size = selected.getAttribute("font-size");
|
|
|
|
cur_text.font_family = selected.getAttribute("font-family");
|
2009-07-01 19:28:08 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-27 14:44:23 +00:00
|
|
|
selectorManager.requestSelector(selected).showGrips(true);
|
2009-07-01 19:28:08 +00:00
|
|
|
}
|
2009-08-08 14:03:14 +00:00
|
|
|
// if it was being dragged/resized
|
|
|
|
if (x != start_x || y != start_y) {
|
|
|
|
recalculateAllSelectedDimensions();
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) break;
|
|
|
|
selectorManager.requestSelector(selectedElements[i]).resize(selectedBBoxes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// no change in position/size, so maybe we should move to polyedit
|
|
|
|
else {
|
2009-09-06 23:25:12 +00:00
|
|
|
var t = evt.target;
|
2009-08-08 14:03:14 +00:00
|
|
|
// TODO: this causes a poly that was just going to be selected to go straight to polyedit
|
|
|
|
if (selectedElements[0].nodeName == "path" && selectedElements[1] == null) {
|
|
|
|
if (current_poly == t) {
|
|
|
|
current_mode = "polyedit";
|
|
|
|
|
|
|
|
// recalculate current_poly_pts
|
|
|
|
current_poly_pts = [];
|
|
|
|
var segList = t.pathSegList;
|
|
|
|
var curx = segList.getItem(0).x, cury = segList.getItem(0).y;
|
2009-09-11 13:32:56 +00:00
|
|
|
current_poly_pts.push(curx * current_zoom);
|
|
|
|
current_poly_pts.push(cury * current_zoom);
|
2009-08-08 14:03:14 +00:00
|
|
|
var len = segList.numberOfItems;
|
|
|
|
for (var i = 1; i < len; ++i) {
|
|
|
|
var l = segList.getItem(i);
|
|
|
|
var x = l.x, y = l.y;
|
|
|
|
// polys can now be closed, skip Z segments
|
|
|
|
if (l.pathSegType == 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var type = l.pathSegType;
|
|
|
|
// current_poly_pts just holds the absolute coords
|
|
|
|
if (type == 4) {
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
} // type 4 (abs line)
|
|
|
|
else if (type == 5) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} // type 5 (rel line)
|
2009-09-25 20:03:20 +00:00
|
|
|
else if (type == 6) {
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
} // type 6 (abs curve)
|
|
|
|
else if (type == 7) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} // type 7 (rel curve)
|
2009-09-11 13:32:56 +00:00
|
|
|
current_poly_pts.push(curx * current_zoom);
|
|
|
|
current_poly_pts.push(cury * current_zoom);
|
2009-08-08 14:03:14 +00:00
|
|
|
} // for each segment
|
|
|
|
canvas.clearSelection();
|
2009-08-27 04:43:05 +00:00
|
|
|
// save the poly's bbox
|
|
|
|
selectedBBoxes[0] = canvas.getBBox(current_poly);
|
2009-08-08 14:03:14 +00:00
|
|
|
addAllPointGripsToPoly();
|
|
|
|
} // going into polyedit mode
|
|
|
|
else {
|
|
|
|
current_poly = t;
|
|
|
|
}
|
2009-09-06 23:25:12 +00:00
|
|
|
} // if it was a path
|
|
|
|
// else, if it was selected and this is a shift-click, remove it from selection
|
|
|
|
else if (evt.shiftKey && tempJustSelected != t) {
|
|
|
|
canvas.removeFromSelection([t]);
|
|
|
|
}
|
|
|
|
} // no change in mouse position
|
2009-08-17 06:56:55 +00:00
|
|
|
}
|
2009-07-13 04:12:25 +00:00
|
|
|
// we return immediately from select so that the obj_num is not incremented
|
|
|
|
return;
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-09-16 15:45:50 +00:00
|
|
|
case "zoom":
|
2009-09-16 17:18:11 +00:00
|
|
|
if (rubberBox != null) {
|
|
|
|
rubberBox.setAttribute("display", "none");
|
|
|
|
}
|
2009-09-16 15:45:50 +00:00
|
|
|
call("zoomed", {
|
|
|
|
'x': Math.min(start_x,x),
|
|
|
|
'y': Math.min(start_y,y),
|
|
|
|
'width': Math.abs(x-start_x),
|
|
|
|
'height': Math.abs(y-start_y)
|
|
|
|
});
|
|
|
|
return;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "path":
|
2009-09-24 16:05:23 +00:00
|
|
|
var coordnum = element.getAttribute('points').split(',').length - 1;
|
|
|
|
keep = coordnum > 1;
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "line":
|
2009-06-06 17:53:38 +00:00
|
|
|
keep = (element.getAttribute('x1') != element.getAttribute('x2') ||
|
2009-08-28 20:18:27 +00:00
|
|
|
element.getAttribute('y1') != element.getAttribute('y2'));
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "square":
|
|
|
|
case "rect":
|
2009-06-06 17:53:38 +00:00
|
|
|
keep = (element.getAttribute('width') != 0 ||
|
2009-06-07 10:27:55 +00:00
|
|
|
element.getAttribute('height') != 0);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-09-04 22:22:40 +00:00
|
|
|
case "image":
|
|
|
|
keep = (element.getAttribute('width') != 0 ||
|
|
|
|
element.getAttribute('height') != 0);
|
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "circle":
|
2009-06-06 17:53:38 +00:00
|
|
|
keep = (element.getAttribute('r') != 0);
|
2009-06-03 15:00:48 +00:00
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "ellipse":
|
2009-06-06 17:53:38 +00:00
|
|
|
keep = (element.getAttribute('rx') != 0 ||
|
2009-06-07 10:27:55 +00:00
|
|
|
element.getAttribute('ry') != 0);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "fhellipse":
|
2009-06-03 08:41:25 +00:00
|
|
|
if ((freehand_max_x - freehand_min_x) > 0 &&
|
|
|
|
(freehand_max_y - freehand_min_y) > 0) {
|
2009-06-28 04:10:03 +00:00
|
|
|
element = addSvgElementFromJson({
|
2009-06-03 08:41:25 +00:00
|
|
|
"element": "ellipse",
|
|
|
|
"attr": {
|
|
|
|
"cx": (freehand_min_x + freehand_max_x) / 2,
|
|
|
|
"cy": (freehand_min_y + freehand_max_y) / 2,
|
|
|
|
"rx": (freehand_max_x - freehand_min_x) / 2,
|
|
|
|
"ry": (freehand_max_y - freehand_min_y) / 2,
|
2009-06-05 12:33:02 +00:00
|
|
|
"id": getId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"opacity": cur_shape.opacity,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
|
|
|
"style": "pointer-events:all"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-28 04:10:03 +00:00
|
|
|
});
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed",[element]);
|
2009-06-16 02:39:12 +00:00
|
|
|
keep = true;
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "fhrect":
|
2009-06-03 08:41:25 +00:00
|
|
|
if ((freehand_max_x - freehand_min_x) > 0 &&
|
|
|
|
(freehand_max_y - freehand_min_y) > 0) {
|
2009-06-28 04:10:03 +00:00
|
|
|
element = addSvgElementFromJson({
|
2009-06-03 08:41:25 +00:00
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"x": freehand_min_x,
|
|
|
|
"y": freehand_min_y,
|
|
|
|
"width": (freehand_max_x - freehand_min_x),
|
|
|
|
"height": (freehand_max_y - freehand_min_y),
|
2009-06-05 12:33:02 +00:00
|
|
|
"id": getId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"opacity": cur_shape.opacity,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
|
|
|
"style": "pointer-events:all"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-28 04:10:03 +00:00
|
|
|
});
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed",[element]);
|
2009-06-16 02:39:12 +00:00
|
|
|
keep = true;
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-06-10 03:12:19 +00:00
|
|
|
case "text":
|
|
|
|
keep = true;
|
2009-07-01 19:28:08 +00:00
|
|
|
canvas.clearSelection();
|
2009-06-10 03:12:19 +00:00
|
|
|
break;
|
2009-08-17 06:56:55 +00:00
|
|
|
case "poly":
|
2009-08-07 05:44:55 +00:00
|
|
|
// set element to null here so that it is not removed nor finalized
|
|
|
|
element = null;
|
|
|
|
// continue to be set to true so that mouseMove happens
|
|
|
|
started = true;
|
|
|
|
var stretchy = document.getElementById("poly_stretch_line");
|
|
|
|
if (!stretchy) {
|
|
|
|
stretchy = document.createElementNS(svgns, "line");
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(stretchy, {
|
|
|
|
'id': "poly_stretch_line",
|
|
|
|
'stroke': "blue",
|
|
|
|
'stroke-width': "0.5"
|
|
|
|
});
|
2009-08-07 05:44:55 +00:00
|
|
|
stretchy = document.getElementById("selectorParentGroup").appendChild(stretchy);
|
|
|
|
}
|
|
|
|
stretchy.setAttribute("display", "inline");
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// if pts array is empty, create path element with M at current point
|
|
|
|
if (current_poly_pts.length == 0) {
|
|
|
|
current_poly_pts.push(x);
|
|
|
|
current_poly_pts.push(y);
|
|
|
|
d_attr = "M" + x + "," + y + " ";
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "path",
|
|
|
|
"attr": {
|
|
|
|
"d": d_attr,
|
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
|
|
|
"style": "pointer-events:all"
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
// set stretchy line to first point
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(stretchy, {
|
2009-09-11 13:32:56 +00:00
|
|
|
'x1': mouse_x,
|
|
|
|
'y1': mouse_y,
|
|
|
|
'x2': mouse_x,
|
|
|
|
'y2': mouse_y
|
2009-08-24 20:00:48 +00:00
|
|
|
});
|
2009-09-11 13:32:56 +00:00
|
|
|
addPointGripToPoly(mouse_x,mouse_y,0);
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// determine if we clicked on an existing point
|
|
|
|
var i = current_poly_pts.length;
|
2009-09-15 20:50:38 +00:00
|
|
|
var FUZZ = 6/current_zoom;
|
2009-08-07 05:44:55 +00:00
|
|
|
var clickOnPoint = false;
|
|
|
|
while(i) {
|
|
|
|
i -= 2;
|
|
|
|
var px = current_poly_pts[i], py = current_poly_pts[i+1];
|
|
|
|
// found a matching point
|
|
|
|
if ( x >= (px-FUZZ) && x <= (px+FUZZ) && y >= (py-FUZZ) && y <= (py+FUZZ) ) {
|
|
|
|
clickOnPoint = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-09-10 20:34:20 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// get poly element that we are in the process of creating
|
|
|
|
var poly = svgdoc.getElementById(getId());
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// if we clicked on an existing point, then we are done this poly, commit it
|
|
|
|
// (i,i+1) are the x,y that were clicked on
|
|
|
|
if (clickOnPoint) {
|
|
|
|
// if clicked on any other point but the first OR
|
|
|
|
// the first point was clicked on and there are less than 3 points
|
|
|
|
// then leave the poly open
|
|
|
|
// otherwise, close the poly
|
|
|
|
if (i == 0 && current_poly_pts.length >= 6) {
|
|
|
|
poly.setAttribute("d", d_attr + "z");
|
2009-09-24 16:05:23 +00:00
|
|
|
} else if(current_poly_pts.length < 3) {
|
|
|
|
keep = false;
|
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
2009-08-08 14:03:14 +00:00
|
|
|
removeAllPointGripsFromPoly();
|
2009-08-07 05:44:55 +00:00
|
|
|
// this will signal to commit the poly
|
|
|
|
element = poly;
|
|
|
|
current_poly_pts = [];
|
|
|
|
started = false;
|
|
|
|
}
|
|
|
|
// else, create a new point, append to pts array, update path element
|
|
|
|
else {
|
|
|
|
var len = current_poly_pts.length;
|
|
|
|
var lastx = current_poly_pts[len-2], lasty = current_poly_pts[len-1];
|
|
|
|
// we store absolute values in our poly points array for easy checking above
|
|
|
|
current_poly_pts.push(x);
|
|
|
|
current_poly_pts.push(y);
|
|
|
|
// but we store relative coordinates in the d string of the poly for easy
|
|
|
|
// translation around the canvas in move mode
|
2009-09-17 14:47:54 +00:00
|
|
|
d_attr += "l" + round(x-lastx) + "," + round(y-lasty) + " ";
|
2009-08-07 05:44:55 +00:00
|
|
|
poly.setAttribute("d", d_attr);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// set stretchy line to latest point
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(stretchy, {
|
2009-09-11 13:32:56 +00:00
|
|
|
'x1': mouse_x,
|
|
|
|
'y1': mouse_y,
|
|
|
|
'x2': mouse_x,
|
|
|
|
'y2': mouse_y
|
2009-08-24 20:00:48 +00:00
|
|
|
});
|
2009-09-11 13:32:56 +00:00
|
|
|
addPointGripToPoly(mouse_x,mouse_y,(current_poly_pts.length/2 - 1));
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
|
|
|
keep = true;
|
|
|
|
}
|
|
|
|
break;
|
2009-08-08 14:03:14 +00:00
|
|
|
case "polyedit":
|
|
|
|
keep = true;
|
|
|
|
element = null;
|
2009-08-08 22:17:59 +00:00
|
|
|
// if we were dragging a poly point, stop it now
|
|
|
|
if (current_poly_pt_drag != -1) {
|
|
|
|
current_poly_pt_drag = -1;
|
2009-08-28 23:12:46 +00:00
|
|
|
|
2009-08-29 22:10:52 +00:00
|
|
|
var batchCmd = new BatchCommand("Edit Poly");
|
|
|
|
// the attribute changes we want to undo
|
|
|
|
var oldvalues = {};
|
|
|
|
oldvalues["d"] = current_poly_oldd;
|
|
|
|
|
2009-08-28 23:12:46 +00:00
|
|
|
// If the poly was rotated, we must now pay the piper:
|
|
|
|
// Every poly point must be rotated into the rotated coordinate system of
|
|
|
|
// its old center, then determine the new center, then rotate it back
|
2009-09-12 04:49:50 +00:00
|
|
|
// This is because we want the poly to remember its rotation
|
2009-08-28 23:12:46 +00:00
|
|
|
var angle = canvas.getRotationAngle(current_poly) * Math.PI / 180.0;
|
|
|
|
if (angle) {
|
|
|
|
var box = canvas.getBBox(current_poly);
|
|
|
|
var oldbox = selectedBBoxes[0];
|
2009-09-17 14:47:54 +00:00
|
|
|
var oldcx = round(oldbox.x + oldbox.width/2),
|
|
|
|
oldcy = round(oldbox.y + oldbox.height/2),
|
|
|
|
newcx = round(box.x + box.width/2),
|
|
|
|
newcy = round(box.y + box.height/2);
|
2009-08-28 23:12:46 +00:00
|
|
|
|
2009-08-29 18:38:34 +00:00
|
|
|
// un-rotate the new center to the proper position
|
|
|
|
var dx = newcx - oldcx,
|
|
|
|
dy = newcy - oldcy;
|
|
|
|
var r = Math.sqrt(dx*dx + dy*dy);
|
|
|
|
var theta = Math.atan2(dy,dx) + angle;
|
2009-09-17 14:47:54 +00:00
|
|
|
newcx = round(r * Math.cos(theta) + oldcx);
|
|
|
|
newcy = round(r * Math.sin(theta) + oldcy);
|
2009-08-29 18:38:34 +00:00
|
|
|
|
2009-08-28 23:12:46 +00:00
|
|
|
var i = current_poly_pts.length;
|
|
|
|
while (i) {
|
|
|
|
i -= 2;
|
2009-08-29 18:38:34 +00:00
|
|
|
dx = current_poly_pts[i] - oldcx;
|
|
|
|
dy = current_poly_pts[i+1] - oldcy;
|
2009-08-28 23:12:46 +00:00
|
|
|
|
|
|
|
// rotate the point around the old center
|
2009-08-29 18:38:34 +00:00
|
|
|
r = Math.sqrt(dx*dx + dy*dy);
|
|
|
|
theta = Math.atan2(dy,dx) + angle;
|
|
|
|
current_poly_pts[i] = dx = r * Math.cos(theta) + oldcx;
|
|
|
|
current_poly_pts[i+1] = dy = r * Math.sin(theta) + oldcy;
|
2009-08-28 23:12:46 +00:00
|
|
|
|
|
|
|
// dx,dy should now hold the actual coordinates of each
|
|
|
|
// point after being rotated
|
2009-08-29 18:38:34 +00:00
|
|
|
|
2009-08-28 23:12:46 +00:00
|
|
|
// now we want to rotate them around the new center in the reverse direction
|
|
|
|
dx -= newcx;
|
|
|
|
dy -= newcy;
|
|
|
|
|
|
|
|
r = Math.sqrt(dx*dx + dy*dy);
|
|
|
|
theta = Math.atan2(dy,dx) - angle;
|
|
|
|
|
2009-09-17 14:47:54 +00:00
|
|
|
current_poly_pts[i] = round(r * Math.cos(theta) + newcx);
|
|
|
|
current_poly_pts[i+1] = round(r * Math.sin(theta) + newcy);
|
2009-08-29 18:38:34 +00:00
|
|
|
} // loop for each point
|
2009-08-28 23:12:46 +00:00
|
|
|
|
|
|
|
// now set the d attribute to the new value of current_poly_pts
|
|
|
|
var oldd = current_poly.getAttribute("d");
|
|
|
|
var closedPath = (oldd[oldd.length-1] == 'z' || oldd[oldd.length-1] == 'Z');
|
|
|
|
var len = current_poly_pts.length/2;
|
|
|
|
var arr = new Array(len+1);
|
2009-09-12 04:49:50 +00:00
|
|
|
var curx = current_poly_pts[0]/current_zoom,
|
|
|
|
cury = current_poly_pts[1]/current_zoom;
|
2009-08-28 23:12:46 +00:00
|
|
|
arr[0] = ["M", curx, ",", cury].join('');
|
2009-08-29 18:38:34 +00:00
|
|
|
assignAttributes(document.getElementById("polypointgrip_0"),
|
|
|
|
{"cx":curx,"cy":cury}, 100);
|
2009-08-28 23:12:46 +00:00
|
|
|
for (var j = 1; j < len; ++j) {
|
2009-09-12 04:49:50 +00:00
|
|
|
var px = current_poly_pts[j*2]/current_zoom,
|
|
|
|
py = current_poly_pts[j*2+1]/current_zoom;
|
2009-09-17 14:47:54 +00:00
|
|
|
arr[j] = ["l", round(px-curx), ",", round(py-cury)].join('');
|
2009-08-28 23:12:46 +00:00
|
|
|
curx = px;
|
|
|
|
cury = py;
|
2009-08-29 18:38:34 +00:00
|
|
|
assignAttributes(document.getElementById("polypointgrip_"+j),
|
|
|
|
{"cx":px,"cy":py}, 100);
|
2009-08-28 23:12:46 +00:00
|
|
|
}
|
|
|
|
if (closedPath) {
|
|
|
|
arr[len] = "z";
|
|
|
|
}
|
|
|
|
current_poly.setAttribute("d", arr.join(' '));
|
2009-08-29 18:38:34 +00:00
|
|
|
|
|
|
|
box = canvas.getBBox(current_poly);
|
|
|
|
selectedBBoxes[0].x = box.x; selectedBBoxes[0].y = box.y;
|
|
|
|
selectedBBoxes[0].width = box.width; selectedBBoxes[0].height = box.height;
|
2009-08-28 23:12:46 +00:00
|
|
|
|
|
|
|
// now we must set the new transform to be rotated around the new center
|
2009-08-29 18:38:34 +00:00
|
|
|
var rotate = "rotate(" + (angle * 180.0 / Math.PI) + " " + newcx + "," + newcy + ")";
|
2009-08-29 22:10:52 +00:00
|
|
|
oldvalues["transform"] = current_poly.getAttribute("rotate");
|
2009-08-29 18:38:34 +00:00
|
|
|
current_poly.setAttribute("transform", rotate);
|
2009-08-28 23:12:46 +00:00
|
|
|
|
2009-08-29 18:38:34 +00:00
|
|
|
var pointGripContainer = document.getElementById("polypointgrip_container");
|
|
|
|
if(pointGripContainer) {
|
2009-09-12 04:49:50 +00:00
|
|
|
var pcx = newcx * current_zoom,
|
|
|
|
pcy = newcy * current_zoom;
|
|
|
|
var xform = ["rotate(", (angle*180.0/Math.PI), " ", pcx, ",", pcy, ")"].join("");
|
|
|
|
pointGripContainer.setAttribute("transform", xform);
|
2009-08-29 18:38:34 +00:00
|
|
|
}
|
|
|
|
} // if rotated
|
2009-08-29 22:10:52 +00:00
|
|
|
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(current_poly, oldvalues, "poly points"));
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
call("changed", [current_poly]);
|
|
|
|
|
|
|
|
// make these changes undo-able
|
2009-08-29 18:38:34 +00:00
|
|
|
} // if (current_poly_pt_drag != -1)
|
2009-08-08 22:17:59 +00:00
|
|
|
// else, move back to select mode
|
2009-09-25 17:59:32 +00:00
|
|
|
else if(current_ctrl_pt_drag != -1) {
|
|
|
|
current_ctrl_pt_drag = -1;
|
|
|
|
}
|
2009-08-08 22:17:59 +00:00
|
|
|
else {
|
|
|
|
current_mode = "select";
|
|
|
|
removeAllPointGripsFromPoly();
|
2009-08-21 12:21:43 +00:00
|
|
|
canvas.clearSelection();
|
2009-08-08 22:17:59 +00:00
|
|
|
canvas.addToSelection([evt.target]);
|
|
|
|
}
|
2009-08-08 14:03:14 +00:00
|
|
|
break;
|
2009-08-26 19:45:28 +00:00
|
|
|
case "rotate":
|
|
|
|
keep = true;
|
|
|
|
element = null;
|
|
|
|
current_mode = "select";
|
2009-09-04 05:10:48 +00:00
|
|
|
var batchCmd = canvas.finishUndoableChange();
|
|
|
|
if (!batchCmd.isEmpty()) {
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
}
|
2009-08-26 19:45:28 +00:00
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
default:
|
|
|
|
console.log("Unknown mode in mouseup: " + current_mode);
|
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
}
|
2009-06-18 05:08:47 +00:00
|
|
|
if (!keep && element != null) {
|
2009-06-10 03:12:19 +00:00
|
|
|
element.parentNode.removeChild(element);
|
|
|
|
element = null;
|
|
|
|
} else if (element != null) {
|
2009-08-21 13:39:23 +00:00
|
|
|
canvas.addedNew = true;
|
2009-08-31 14:54:59 +00:00
|
|
|
element.setAttribute("opacity", cur_shape.opacity);
|
2009-06-10 03:12:19 +00:00
|
|
|
cleanupElement(element);
|
2009-06-30 17:50:08 +00:00
|
|
|
selectorManager.update();
|
2009-08-21 17:02:51 +00:00
|
|
|
canvas.addToSelection([element], true);
|
2009-06-25 02:54:07 +00:00
|
|
|
// we create the insert command that is stored on the stack
|
|
|
|
// undo means to call cmd.unapply(), redo means to call cmd.apply()
|
|
|
|
addCommandToHistory(new InsertElementCommand(element));
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed",[element]);
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
|
|
|
// public functions
|
|
|
|
|
2009-08-25 16:35:57 +00:00
|
|
|
this.open = function(str) {
|
|
|
|
// Nothing by default, handled by optional widget/extention
|
|
|
|
call("opened", str);
|
2009-08-19 03:42:27 +00:00
|
|
|
};
|
|
|
|
|
2009-06-06 23:54:53 +00:00
|
|
|
this.save = function() {
|
2009-06-07 05:07:23 +00:00
|
|
|
// remove the selected outline before serializing
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-08-19 17:08:05 +00:00
|
|
|
|
2009-06-16 02:52:36 +00:00
|
|
|
var str = "<?xml version=\"1.0\" standalone=\"no\"?>\n";
|
2009-07-08 00:03:08 +00:00
|
|
|
// no need for doctype, see http://jwatt.org/svg/authoring/#doctype-declaration
|
2009-09-09 14:18:24 +00:00
|
|
|
str += svgCanvasToString();
|
2009-08-17 21:55:47 +00:00
|
|
|
call("saved", str);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-21 22:16:44 +00:00
|
|
|
var walkTree = function(elem, cbFn){
|
|
|
|
if (elem && elem.nodeType == 1) {
|
|
|
|
cbFn(elem);
|
|
|
|
var i = elem.childNodes.length;
|
|
|
|
while (i--) {
|
|
|
|
walkTree(elem.childNodes.item(i), cbFn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
this.getSvgString = function() {
|
2009-09-09 14:18:24 +00:00
|
|
|
return svgCanvasToString();
|
2009-07-10 05:04:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// this function returns false if the set was unsuccessful, true otherwise
|
2009-08-16 02:48:00 +00:00
|
|
|
// TODO: after parsing in the new text, do we need to synchronize getId()?
|
2009-07-10 05:04:06 +00:00
|
|
|
this.setSvgString = function(xmlString) {
|
|
|
|
try {
|
|
|
|
// convert string into XML document
|
|
|
|
var newDoc = Utils.text2xml(xmlString);
|
|
|
|
|
|
|
|
// run it through our sanitizer to remove anything we do not support
|
|
|
|
sanitizeSvg(newDoc.documentElement);
|
|
|
|
|
|
|
|
var batchCmd = new BatchCommand("Change Source");
|
|
|
|
|
2009-09-09 16:43:09 +00:00
|
|
|
// remove old svg document
|
|
|
|
var oldzoom = svgroot.removeChild(svgzoom);
|
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(oldzoom, svgroot));
|
2009-07-10 05:04:06 +00:00
|
|
|
|
2009-09-09 16:43:09 +00:00
|
|
|
// set new svg document
|
|
|
|
svgzoom = svgroot.appendChild(svgdoc.importNode(newDoc.documentElement, true));
|
|
|
|
svgzoom.setAttribute('id', 'svgzoom');
|
2009-09-09 17:41:28 +00:00
|
|
|
// determine proper size
|
2009-09-09 18:15:28 +00:00
|
|
|
var w, h;
|
|
|
|
if (svgzoom.getAttribute("viewBox")) {
|
|
|
|
var vb = svgzoom.getAttribute("viewBox").split(' ');
|
|
|
|
w = vb[2];
|
|
|
|
h = vb[3];
|
|
|
|
}
|
|
|
|
// handle old content that doesn't have a viewBox
|
|
|
|
else {
|
|
|
|
w = svgzoom.getAttribute("width");
|
|
|
|
h = svgzoom.getAttribute("height");
|
|
|
|
svgzoom.setAttribute("viewBox", ["0", "0", w, h].join(" "));
|
|
|
|
}
|
2009-09-09 17:41:28 +00:00
|
|
|
// just to be safe, remove any width/height from text so that they are 100%/100%
|
|
|
|
svgzoom.removeAttribute('width');
|
|
|
|
svgzoom.removeAttribute('height');
|
2009-09-09 16:43:09 +00:00
|
|
|
batchCmd.addSubCommand(new InsertElementCommand(svgzoom));
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-09 17:41:28 +00:00
|
|
|
// update root to the correct size
|
|
|
|
var changes = {};
|
|
|
|
changes['width'] = svgroot.getAttribute('width');
|
|
|
|
changes['height'] = svgroot.getAttribute('height');
|
|
|
|
svgroot.setAttribute('width', w);
|
|
|
|
svgroot.setAttribute('height', h);
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(svgroot, changes));
|
|
|
|
|
|
|
|
// reset zoom
|
|
|
|
current_zoom = 1;
|
2009-09-21 02:05:58 +00:00
|
|
|
|
|
|
|
// identify layers
|
2009-09-22 01:00:24 +00:00
|
|
|
canvas.identifyLayers();
|
2009-09-21 22:16:44 +00:00
|
|
|
|
2009-09-09 17:41:28 +00:00
|
|
|
selectorManager.update();
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-09-11 15:49:50 +00:00
|
|
|
call("changed", [svgzoom]);
|
2009-07-10 05:04:06 +00:00
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
return true;
|
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-09-22 02:42:31 +00:00
|
|
|
// Layer API Functions
|
|
|
|
|
2009-09-22 01:00:24 +00:00
|
|
|
this.identifyLayers = function() {
|
|
|
|
all_layers = [];
|
|
|
|
var numchildren = svgzoom.childNodes.length;
|
|
|
|
// loop through all children of svgzoom
|
2009-09-24 03:13:25 +00:00
|
|
|
var orphans = [], layernames = [];
|
2009-09-22 01:00:24 +00:00
|
|
|
for (var i = 0; i < numchildren; ++i) {
|
|
|
|
var child = svgzoom.childNodes.item(i);
|
|
|
|
// for each g, find its layer name
|
2009-09-24 03:14:43 +00:00
|
|
|
if (child && child.nodeType == 1) {
|
2009-09-24 03:13:25 +00:00
|
|
|
if (child.tagName == "g") {
|
|
|
|
var name = getLayerName(child);
|
|
|
|
// store layer and name in global variable
|
|
|
|
if (name) {
|
|
|
|
layernames.push(name);
|
|
|
|
all_layers.push( [name,child] );
|
|
|
|
current_layer = child;
|
|
|
|
walkTree(child, function(e){e.setAttribute("style", "pointer-events:none");});
|
|
|
|
}
|
|
|
|
}
|
2009-09-24 03:14:43 +00:00
|
|
|
else {
|
2009-09-24 03:13:25 +00:00
|
|
|
orphans.push(child);
|
2009-09-22 01:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-24 03:13:25 +00:00
|
|
|
// create a new layer and add all the orphans to it
|
|
|
|
if (orphans.length > 0) {
|
|
|
|
var i = 1;
|
|
|
|
while ($.inArray(("Layer " + i), layernames) != -1) { i++; }
|
|
|
|
var newname = "Layer " + i;
|
|
|
|
current_layer = svgdoc.createElementNS(svgns, "g");
|
|
|
|
var layer_title = svgdoc.createElementNS(svgns, "title");
|
|
|
|
layer_title.textContent = newname;
|
|
|
|
current_layer.appendChild(layer_title);
|
|
|
|
for (var j = 0; j < orphans.length; ++j) {
|
|
|
|
walkTree(orphans[j], function(e){e.setAttribute("style", "pointer-events:none");});
|
|
|
|
current_layer.appendChild(orphans[j]);
|
|
|
|
}
|
|
|
|
current_layer = svgzoom.appendChild(current_layer);
|
|
|
|
all_layers.push( [newname, current_layer] );
|
|
|
|
}
|
2009-09-22 01:00:24 +00:00
|
|
|
walkTree(current_layer, function(e){e.setAttribute("style","pointer-events:all");});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.createLayer = function(name) {
|
|
|
|
var batchCmd = new BatchCommand("Create Layer");
|
2009-09-22 18:10:54 +00:00
|
|
|
var new_layer = svgdoc.createElementNS(svgns, "g");
|
2009-09-22 01:00:24 +00:00
|
|
|
var layer_title = svgdoc.createElementNS(svgns, "title");
|
2009-09-22 18:10:54 +00:00
|
|
|
layer_title.textContent = name;
|
|
|
|
new_layer.appendChild(layer_title);
|
|
|
|
new_layer = svgzoom.appendChild(new_layer);
|
|
|
|
batchCmd.addSubCommand(new InsertElementCommand(new_layer));
|
2009-09-22 01:00:24 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-09-22 02:42:31 +00:00
|
|
|
canvas.clearSelection();
|
2009-09-22 18:10:54 +00:00
|
|
|
canvas.identifyLayers();
|
|
|
|
canvas.setCurrentLayer(name);
|
|
|
|
call("changed", [new_layer]);
|
2009-09-22 01:00:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.deleteCurrentLayer = function() {
|
|
|
|
if (current_layer && all_layers.length > 1) {
|
|
|
|
var batchCmd = new BatchCommand("Delete Layer");
|
2009-09-22 18:10:54 +00:00
|
|
|
// actually delete from the DOM and store in our Undo History
|
|
|
|
var parent = current_layer.parentNode;
|
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(current_layer, parent));
|
|
|
|
parent.removeChild(current_layer);
|
2009-09-22 01:00:24 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-09-22 02:42:31 +00:00
|
|
|
canvas.clearSelection();
|
2009-09-22 18:10:54 +00:00
|
|
|
canvas.identifyLayers();
|
|
|
|
canvas.setCurrentLayer(all_layers[all_layers.length-1][0]);
|
2009-09-22 02:42:31 +00:00
|
|
|
call("changed", [svgzoom]);
|
2009-09-22 01:00:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2009-09-21 22:16:44 +00:00
|
|
|
this.getNumLayers = function() {
|
|
|
|
return all_layers.length;
|
|
|
|
};
|
|
|
|
this.getLayer = function(i) {
|
|
|
|
if (i >= 0 && i < canvas.getNumLayers()) {
|
|
|
|
return all_layers[i][0];
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
};
|
2009-09-22 04:44:56 +00:00
|
|
|
|
2009-09-21 22:16:44 +00:00
|
|
|
this.getCurrentLayer = function() {
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][1] == current_layer) {
|
|
|
|
return all_layers[i][0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
};
|
2009-09-22 04:44:56 +00:00
|
|
|
|
2009-09-21 22:16:44 +00:00
|
|
|
this.setCurrentLayer = function(name) {
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (name == all_layers[i][0]) {
|
|
|
|
if (current_layer != all_layers[i][1]) {
|
2009-09-22 01:00:24 +00:00
|
|
|
canvas.clearSelection();
|
2009-09-21 22:16:44 +00:00
|
|
|
walkTree(current_layer,function(e){e.setAttribute("style","pointer-events:none");});
|
|
|
|
current_layer = all_layers[i][1];
|
|
|
|
walkTree(current_layer,function(e){e.setAttribute("style","pointer-events:all");});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-09-22 04:44:56 +00:00
|
|
|
|
2009-09-23 03:05:27 +00:00
|
|
|
this.renameCurrentLayer = function(newname) {
|
|
|
|
if (current_layer) {
|
2009-09-21 22:16:44 +00:00
|
|
|
var oldLayer = current_layer;
|
|
|
|
// setCurrentLayer will return false if the name doesn't already exists
|
|
|
|
if (!canvas.setCurrentLayer(newname)) {
|
2009-09-22 02:42:31 +00:00
|
|
|
var batchCmd = new BatchCommand("Rename Layer");
|
2009-09-21 22:16:44 +00:00
|
|
|
// find the index of the layer
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][1] == oldLayer) break;
|
|
|
|
}
|
2009-09-23 03:05:27 +00:00
|
|
|
var oldname = all_layers[i][0];
|
2009-09-21 22:16:44 +00:00
|
|
|
all_layers[i][0] = newname;
|
|
|
|
|
|
|
|
// now change the underlying title element contents
|
|
|
|
var len = oldLayer.childNodes.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var child = oldLayer.childNodes.item(i);
|
|
|
|
// found the <title> element, now append all the
|
|
|
|
if (child && child.tagName == "title") {
|
|
|
|
// wipe out old name
|
|
|
|
while (child.firstChild) { child.removeChild(child.firstChild); }
|
2009-09-22 02:42:31 +00:00
|
|
|
child.textContent = newname;
|
|
|
|
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(child, {"#text":oldname}));
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
call("changed", [oldLayer]);
|
2009-09-21 22:16:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-23 03:05:27 +00:00
|
|
|
current_layer = oldLayer;
|
2009-09-21 22:16:44 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-09-22 04:44:56 +00:00
|
|
|
|
|
|
|
this.setCurrentLayerPosition = function(newpos) {
|
|
|
|
if (current_layer && newpos >= 0 && newpos < all_layers.length) {
|
|
|
|
for (var oldpos = 0; oldpos < all_layers.length; ++oldpos) {
|
|
|
|
if (all_layers[oldpos][1] == current_layer) break;
|
|
|
|
}
|
|
|
|
// some unknown error condition (current_layer not in all_layers)
|
|
|
|
if (oldpos == all_layers.length) { return false; }
|
|
|
|
|
|
|
|
if (oldpos != newpos) {
|
|
|
|
// if our new position is below us, we need to insert before the node after newpos
|
|
|
|
var refLayer = null;
|
|
|
|
var oldNextSibling = current_layer.nextSibling;
|
|
|
|
if (newpos > oldpos ) {
|
|
|
|
if (newpos < all_layers.length-1) {
|
|
|
|
refLayer = all_layers[newpos+1][1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if our new position is above us, we need to insert before the node at newpos
|
|
|
|
else {
|
|
|
|
refLayer = all_layers[newpos][1];
|
|
|
|
}
|
|
|
|
svgzoom.insertBefore(current_layer, refLayer);
|
|
|
|
addCommandToHistory(new MoveElementCommand(current_layer, oldNextSibling, svgzoom));
|
|
|
|
|
|
|
|
canvas.identifyLayers();
|
2009-09-22 18:10:54 +00:00
|
|
|
canvas.setCurrentLayer(all_layers[newpos][0]);
|
2009-09-22 04:44:56 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: if i differs, then MoveElementCommand
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2009-09-24 04:48:40 +00:00
|
|
|
this.getLayerVisibility = function(layername) {
|
|
|
|
// find the layer
|
|
|
|
var layer = null;
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][0] == layername) {
|
|
|
|
layer = all_layers[i][1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!layer) return false;
|
|
|
|
return (layer.getAttribute("display") != "none");
|
|
|
|
};
|
|
|
|
|
|
|
|
this.setLayerVisibility = function(layername, bVisible) {
|
|
|
|
// find the layer
|
|
|
|
var layer = null;
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][0] == layername) {
|
|
|
|
layer = all_layers[i][1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!layer) return false;
|
|
|
|
|
|
|
|
var oldDisplay = layer.getAttribute("display");
|
|
|
|
if (!oldDisplay) oldDisplay = "inline";
|
|
|
|
layer.setAttribute("display", bVisible ? "inline" : "none");
|
|
|
|
addCommandToHistory(new ChangeElementCommand(layer, {"display":oldDisplay}, "Layer Visibility"));
|
|
|
|
|
|
|
|
if (layer == current_layer) {
|
|
|
|
canvas.clearSelection();
|
|
|
|
}
|
|
|
|
// call("changed", [selected]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2009-09-21 22:16:44 +00:00
|
|
|
// used internally
|
|
|
|
var getLayerName = function(g) {
|
|
|
|
var name = "";
|
|
|
|
if (g && g.tagName == "g") {
|
|
|
|
var len = g.childNodes.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var child = g.childNodes.item(i);
|
|
|
|
// found the <title> element, now append all the
|
|
|
|
if (child && child.tagName == "title") {
|
2009-09-22 02:42:31 +00:00
|
|
|
name = child.textContent;
|
2009-09-21 22:16:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
};
|
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
this.clear = function() {
|
2009-09-21 02:05:58 +00:00
|
|
|
current_poly_pts = [];
|
|
|
|
|
|
|
|
// clear the svgzoom node
|
2009-09-13 12:55:15 +00:00
|
|
|
var nodes = svgzoom.childNodes;
|
|
|
|
var len = svgzoom.childNodes.length;
|
2009-06-01 21:24:30 +00:00
|
|
|
var i = 0;
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-06-01 21:24:30 +00:00
|
|
|
for(var rep = 0; rep < len; rep++){
|
2009-06-02 16:40:33 +00:00
|
|
|
if (nodes[i].nodeType == 1) { // element node
|
2009-09-21 02:05:58 +00:00
|
|
|
svgzoom.removeChild(nodes[i]);
|
2009-06-01 21:24:30 +00:00
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2009-09-21 02:05:58 +00:00
|
|
|
// create empty first layer
|
2009-09-21 22:16:44 +00:00
|
|
|
all_layers = [];
|
2009-09-22 01:00:24 +00:00
|
|
|
canvas.createLayer("Layer 1");
|
2009-09-21 02:05:58 +00:00
|
|
|
|
2009-07-02 03:54:11 +00:00
|
|
|
// clear the undo stack
|
|
|
|
resetUndoStack();
|
2009-09-04 06:36:37 +00:00
|
|
|
// reset the selector manager
|
|
|
|
selectorManager.initGroup();
|
2009-09-04 17:23:56 +00:00
|
|
|
// reset the rubber band box
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
|
2009-06-12 12:20:48 +00:00
|
|
|
call("cleared");
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-09-04 16:26:48 +00:00
|
|
|
|
2009-09-25 17:59:32 +00:00
|
|
|
this.clearPoly = function(remove) {
|
|
|
|
if(remove && current_poly_pts.length > 0) {
|
2009-09-24 16:05:23 +00:00
|
|
|
var elem = svgdoc.getElementById(getId());
|
|
|
|
if(elem) elem.parentNode.removeChild(elem);
|
|
|
|
}
|
2009-09-04 16:26:48 +00:00
|
|
|
removeAllPointGripsFromPoly();
|
|
|
|
current_poly = null;
|
|
|
|
current_poly_pts = [];
|
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
this.getResolution = function() {
|
2009-09-09 14:18:24 +00:00
|
|
|
// return [svgroot.getAttribute("width"), svgroot.getAttribute("height")];
|
|
|
|
var vb = svgzoom.getAttribute("viewBox").split(' ');
|
|
|
|
return {'w':vb[2], 'h':vb[3], 'zoom': current_zoom};
|
2009-07-10 05:04:06 +00:00
|
|
|
};
|
2009-09-22 17:22:28 +00:00
|
|
|
|
2009-06-25 08:22:30 +00:00
|
|
|
this.setResolution = function(x, y) {
|
2009-09-11 15:49:50 +00:00
|
|
|
var res = canvas.getResolution();
|
|
|
|
var w = res.w, h = res.h;
|
2009-07-30 22:45:59 +00:00
|
|
|
|
2009-09-22 17:22:28 +00:00
|
|
|
if(x == 'fit') {
|
2009-08-20 15:59:29 +00:00
|
|
|
canvas.clearSelection();
|
|
|
|
|
|
|
|
// Get bounding box
|
2009-09-22 14:35:05 +00:00
|
|
|
var bbox = canvas.getStrokedBBox();
|
2009-08-20 15:59:29 +00:00
|
|
|
|
|
|
|
if(bbox) {
|
2009-09-22 14:35:05 +00:00
|
|
|
canvas.addToSelection(canvas.getVisibleElements());
|
|
|
|
$.each(selectedElements, function(i, item) {
|
|
|
|
var sel_bb = item.getBBox();
|
|
|
|
recalculateDimensions(item, {
|
|
|
|
x: sel_bb.x - bbox.x,
|
|
|
|
y: sel_bb.y - bbox.y,
|
|
|
|
width: sel_bb.width,
|
|
|
|
height: sel_bb.height
|
|
|
|
});
|
|
|
|
});
|
|
|
|
canvas.clearSelection();
|
2009-09-22 17:22:28 +00:00
|
|
|
x = Math.round(bbox.width);
|
|
|
|
y = Math.round(bbox.height);
|
2009-08-20 15:59:29 +00:00
|
|
|
} else {
|
2009-09-22 17:22:28 +00:00
|
|
|
return false;
|
2009-08-20 15:59:29 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-14 00:04:53 +00:00
|
|
|
x *= current_zoom;
|
|
|
|
y *= current_zoom;
|
|
|
|
if (x != w || y != h) {
|
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
|
|
|
var batchCmd = new BatchCommand("Change Image Dimensions");
|
|
|
|
svgroot.setAttribute('width', x);
|
|
|
|
svgroot.setAttribute('height', y);
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(svgroot, {"width":w, "height":h}));
|
|
|
|
|
2009-09-22 17:46:44 +00:00
|
|
|
svgzoom.setAttribute("viewBox", ["0 0", x/current_zoom, y/current_zoom].join(' '));
|
2009-09-14 00:04:53 +00:00
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(svgzoom, {"viewBox": ["0 0", w, h].join(' ')}));
|
2009-09-11 15:49:50 +00:00
|
|
|
|
2009-09-14 00:04:53 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
svgroot.unsuspendRedraw(handle);
|
|
|
|
call("changed", [svgzoom]);
|
|
|
|
}
|
2009-09-22 17:22:28 +00:00
|
|
|
return true;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-25 08:22:30 +00:00
|
|
|
|
2009-09-16 15:45:50 +00:00
|
|
|
this.setBBoxZoom = function(val, editor_w, editor_h) {
|
2009-09-16 14:12:51 +00:00
|
|
|
var spacer = .85;
|
2009-09-23 15:29:55 +00:00
|
|
|
var bb;
|
|
|
|
var calcZoom = function(bb) {
|
|
|
|
var w_zoom = Math.round((editor_w / bb.width)*100 * spacer)/100;
|
|
|
|
var h_zoom = Math.round((editor_h / bb.height)*100 * spacer)/100;
|
|
|
|
var zoomlevel = Math.min(w_zoom,h_zoom);
|
|
|
|
canvas.setZoom(zoomlevel);
|
|
|
|
return {'zoom': zoomlevel, 'bbox': bb};
|
|
|
|
}
|
2009-09-16 14:12:51 +00:00
|
|
|
|
2009-09-16 15:45:50 +00:00
|
|
|
if(typeof val == 'object') {
|
2009-09-23 15:29:55 +00:00
|
|
|
bb = val;
|
2009-09-16 15:45:50 +00:00
|
|
|
if(bb.width == 0 || bb.height == 0) {
|
|
|
|
canvas.setZoom(current_zoom * 2);
|
|
|
|
return {'zoom': current_zoom, 'bbox': bb};
|
|
|
|
}
|
2009-09-23 15:29:55 +00:00
|
|
|
return calcZoom(bb);
|
2009-09-16 15:45:50 +00:00
|
|
|
}
|
2009-09-23 15:29:55 +00:00
|
|
|
|
2009-09-16 15:45:50 +00:00
|
|
|
switch (val) {
|
2009-09-16 14:12:51 +00:00
|
|
|
case 'selection':
|
|
|
|
if(!selectedElements[0]) return;
|
2009-09-22 14:35:05 +00:00
|
|
|
var sel_elems = $.map(selectedElements, function(n){ if(n) return n; });
|
2009-09-23 15:29:55 +00:00
|
|
|
bb = canvas.getStrokedBBox(sel_elems);
|
|
|
|
break;
|
2009-09-16 14:12:51 +00:00
|
|
|
case 'canvas':
|
|
|
|
var res = canvas.getResolution();
|
2009-09-23 15:29:55 +00:00
|
|
|
spacer = .95;
|
|
|
|
bb = {width:res.w, height:res.h ,x:0, y:0};
|
|
|
|
break;
|
2009-09-16 14:12:51 +00:00
|
|
|
case 'content':
|
2009-09-23 15:29:55 +00:00
|
|
|
bb = canvas.getStrokedBBox();
|
|
|
|
break;
|
|
|
|
case 'layer':
|
|
|
|
bb = canvas.getStrokedBBox(canvas.getVisibleElements(current_layer));
|
|
|
|
break;
|
2009-09-16 14:12:51 +00:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2009-09-23 15:29:55 +00:00
|
|
|
return calcZoom(bb);
|
2009-09-16 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
2009-09-09 14:18:24 +00:00
|
|
|
this.setZoom = function(zoomlevel) {
|
|
|
|
var res = canvas.getResolution();
|
|
|
|
svgroot.setAttribute("width", res.w * zoomlevel);
|
|
|
|
svgroot.setAttribute("height", res.h * zoomlevel);
|
|
|
|
current_zoom = zoomlevel;
|
2009-09-16 14:12:51 +00:00
|
|
|
$.each(selectedElements, function(i, elem) {
|
|
|
|
if(!elem) return;
|
|
|
|
selectorManager.requestSelector(elem).resize();
|
|
|
|
});
|
2009-09-09 14:18:24 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getMode = function() {
|
|
|
|
return current_mode;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
this.setMode = function(name) {
|
2009-08-07 05:44:55 +00:00
|
|
|
// toss out half-drawn poly
|
|
|
|
if (current_mode == "poly" && current_poly_pts.length > 0) {
|
2009-08-14 15:50:11 +00:00
|
|
|
var elem = svgdoc.getElementById(getId());
|
|
|
|
elem.parentNode.removeChild(elem);
|
2009-09-04 16:26:48 +00:00
|
|
|
canvas.clearPoly();
|
2009-08-14 15:50:11 +00:00
|
|
|
canvas.clearSelection();
|
|
|
|
started = false;
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
2009-08-17 03:57:43 +00:00
|
|
|
else if (current_mode == "polyedit") {
|
2009-09-04 16:26:48 +00:00
|
|
|
canvas.clearPoly();
|
2009-08-17 03:57:43 +00:00
|
|
|
}
|
2009-08-31 14:54:59 +00:00
|
|
|
|
|
|
|
cur_properties = (selectedElements[0] && selectedElements[0].nodeName == 'text') ? cur_text : cur_shape;
|
2009-08-14 15:50:11 +00:00
|
|
|
current_mode = name;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeColor = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_properties.stroke;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-09-04 06:11:04 +00:00
|
|
|
this.setStrokeColor = function(val,preventUndo) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_shape.stroke = val;
|
2009-08-31 15:59:42 +00:00
|
|
|
cur_properties.stroke_paint = {type:"solidColor"};
|
2009-09-04 06:11:04 +00:00
|
|
|
if (!preventUndo)
|
|
|
|
this.changeSelectedAttribute("stroke", val);
|
2009-09-04 06:30:18 +00:00
|
|
|
else
|
|
|
|
this.changeSelectedAttributeNoUndo("stroke", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getFillColor = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_properties.fill;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-09-04 06:11:04 +00:00
|
|
|
this.setFillColor = function(val,preventUndo) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_properties.fill = val;
|
2009-08-31 15:59:42 +00:00
|
|
|
cur_properties.fill_paint = {type:"solidColor"};
|
2009-07-08 04:40:45 +00:00
|
|
|
// take out any path/line elements when setting fill
|
|
|
|
var elems = [];
|
|
|
|
var i = selectedElements.length;
|
|
|
|
while(i--) {
|
|
|
|
var elem = selectedElements[i];
|
2009-08-07 17:19:32 +00:00
|
|
|
if (elem && elem.tagName != "polyline" && elem.tagName != "line") {
|
2009-07-08 04:40:45 +00:00
|
|
|
elems.push(elem);
|
|
|
|
}
|
|
|
|
}
|
2009-09-04 06:30:18 +00:00
|
|
|
if (elems.length > 0) {
|
|
|
|
if (!preventUndo)
|
|
|
|
this.changeSelectedAttribute("fill", val, elems);
|
|
|
|
else
|
|
|
|
this.changeSelectedAttributeNoUndo("fill", val, elems);
|
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
var findDefs = function() {
|
2009-09-09 17:41:28 +00:00
|
|
|
var defs = svgzoom.getElementsByTagNameNS(svgns, "defs");
|
2009-07-30 22:45:59 +00:00
|
|
|
if (defs.length > 0) {
|
|
|
|
defs = defs[0];
|
|
|
|
}
|
|
|
|
else {
|
2009-08-28 14:39:53 +00:00
|
|
|
// first child is a comment, so call nextSibling
|
2009-09-09 17:41:28 +00:00
|
|
|
defs = svgzoom.insertBefore( svgdoc.createElementNS(svgns, "defs" ), svgzoom.firstChild.nextSibling);
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
|
|
|
return defs;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
var addGradient = function() {
|
|
|
|
$.each(['stroke','fill'],function(i,type) {
|
|
|
|
|
2009-08-31 14:54:59 +00:00
|
|
|
if(!cur_properties[type + '_paint'] || cur_properties[type + '_paint'].type == "solidColor") return;
|
2009-08-19 17:08:05 +00:00
|
|
|
var grad = canvas[type + 'Grad'];
|
|
|
|
// find out if there is a duplicate gradient already in the defs
|
|
|
|
var duplicate_grad = findDuplicateGradient(grad);
|
|
|
|
var defs = findDefs();
|
|
|
|
// no duplicate found, so import gradient into defs
|
|
|
|
if (!duplicate_grad) {
|
|
|
|
grad = defs.appendChild( svgdoc.importNode(grad, true) );
|
|
|
|
// get next id and set it on the grad
|
|
|
|
grad.id = getNextId();
|
|
|
|
}
|
|
|
|
else { // use existing gradient
|
|
|
|
grad = duplicate_grad;
|
|
|
|
}
|
|
|
|
var functype = type=='fill'?'Fill':'Stroke';
|
|
|
|
canvas['set'+ functype +'Color']("url(#" + grad.id + ")");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
var findDuplicateGradient = function(grad) {
|
|
|
|
var defs = findDefs();
|
|
|
|
var existing_grads = defs.getElementsByTagNameNS(svgns, "linearGradient");
|
|
|
|
var i = existing_grads.length;
|
|
|
|
while (i--) {
|
|
|
|
var og = existing_grads.item(i);
|
|
|
|
if (grad.getAttribute('x1') != og.getAttribute('x1') ||
|
|
|
|
grad.getAttribute('y1') != og.getAttribute('y1') ||
|
|
|
|
grad.getAttribute('x2') != og.getAttribute('x2') ||
|
|
|
|
grad.getAttribute('y2') != og.getAttribute('y2'))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
// else could be a duplicate, iterate through stops
|
|
|
|
var stops = grad.getElementsByTagNameNS(svgns, "stop");
|
|
|
|
var ostops = og.getElementsByTagNameNS(svgns, "stop");
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
if (stops.length != ostops.length) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
var j = stops.length;
|
|
|
|
while(j--) {
|
|
|
|
var stop = stops.item(j);
|
|
|
|
var ostop = ostops.item(j);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
if (stop.getAttribute('offset') != ostop.getAttribute('offset') ||
|
|
|
|
stop.getAttribute('stop-opacity') != ostop.getAttribute('stop-opacity') ||
|
|
|
|
stop.getAttribute('stop-color') != ostop.getAttribute('stop-color'))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
if (j == -1) {
|
|
|
|
return og;
|
|
|
|
}
|
|
|
|
} // for each gradient in defs
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
return null;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
this.setStrokePaint = function(p, addGrad) {
|
2009-09-04 06:11:04 +00:00
|
|
|
// make a copy
|
|
|
|
var p = new $.jGraduate.Paint(p);
|
2009-09-04 06:30:18 +00:00
|
|
|
this.setStrokeOpacity(p.alpha/100);
|
|
|
|
|
|
|
|
// now set the current paint object
|
|
|
|
cur_properties.stroke_paint = p;
|
2009-09-04 06:11:04 +00:00
|
|
|
if (p.type == "solidColor") {
|
|
|
|
this.setStrokeColor("#"+p.solidColor);
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
2009-09-04 06:11:04 +00:00
|
|
|
else if(p.type == "linearGradient") {
|
|
|
|
canvas.strokeGrad = p.linearGradient;
|
2009-08-19 17:08:05 +00:00
|
|
|
if(addGrad) addGradient();
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// console.log("none!");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
this.setFillPaint = function(p, addGrad) {
|
2009-09-04 06:11:04 +00:00
|
|
|
// make a copy
|
|
|
|
var p = new $.jGraduate.Paint(p);
|
2009-09-04 06:30:18 +00:00
|
|
|
this.setFillOpacity(p.alpha/100, true);
|
|
|
|
|
|
|
|
// now set the current paint object
|
|
|
|
cur_properties.fill_paint = p;
|
2009-09-04 06:11:04 +00:00
|
|
|
if (p.type == "solidColor") {
|
|
|
|
this.setFillColor("#"+p.solidColor);
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
2009-09-04 06:11:04 +00:00
|
|
|
else if(p.type == "linearGradient") {
|
|
|
|
canvas.fillGrad = p.linearGradient;
|
2009-08-19 17:08:05 +00:00
|
|
|
if(addGrad) addGradient();
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// console.log("none!");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeWidth = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_properties.stroke_width;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-01 22:01:07 +00:00
|
|
|
this.setStrokeWidth = function(val) {
|
2009-09-01 19:52:34 +00:00
|
|
|
if(val == 0 && $.inArray(current_mode, ['line', 'path']) == -1) {
|
|
|
|
canvas.setStrokeWidth(1);
|
|
|
|
}
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_properties.stroke_width = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("stroke-width", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeStyle = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_shape.stroke_style;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-01 22:01:07 +00:00
|
|
|
this.setStrokeStyle = function(val) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_shape.stroke_style = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("stroke-dasharray", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 22:01:07 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getOpacity = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_shape.opacity;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-04 13:43:58 +00:00
|
|
|
this.setOpacity = function(val) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_shape.opacity = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("opacity", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-04 13:43:58 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getFillOpacity = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_shape.fill_opacity;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-09-04 06:11:04 +00:00
|
|
|
this.setFillOpacity = function(val, preventUndo) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_shape.fill_opacity = val;
|
2009-09-04 06:11:04 +00:00
|
|
|
if (!preventUndo)
|
|
|
|
this.changeSelectedAttribute("fill-opacity", val);
|
2009-09-04 06:30:18 +00:00
|
|
|
else
|
|
|
|
this.changeSelectedAttributeNoUndo("fill-opacity", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-04 13:43:58 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeOpacity = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_shape.stroke_opacity;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-09-04 06:11:04 +00:00
|
|
|
this.setStrokeOpacity = function(val, preventUndo) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_shape.stroke_opacity = val;
|
2009-09-04 06:11:04 +00:00
|
|
|
if (!preventUndo)
|
|
|
|
this.changeSelectedAttribute("stroke-opacity", val);
|
2009-09-04 06:30:18 +00:00
|
|
|
else
|
|
|
|
this.changeSelectedAttributeNoUndo("stroke-opacity", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-16 15:07:00 +00:00
|
|
|
this.getBBox = function(elem) {
|
|
|
|
var selected = elem || selectedElements[0];
|
2009-08-23 16:39:45 +00:00
|
|
|
|
2009-08-24 12:55:22 +00:00
|
|
|
if(elem.nodeName == 'text' && selected.textContent == '') {
|
2009-08-21 20:12:01 +00:00
|
|
|
selected.textContent = 'a'; // Some character needed for the selector to use.
|
|
|
|
var ret = selected.getBBox();
|
|
|
|
selected.textContent = '';
|
|
|
|
} else {
|
|
|
|
var ret = selected.getBBox();
|
|
|
|
}
|
|
|
|
|
2009-08-19 04:49:50 +00:00
|
|
|
// get the bounding box from the DOM (which is in that element's coordinate system)
|
2009-08-21 20:12:01 +00:00
|
|
|
return ret;
|
2009-08-16 15:07:00 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-16 14:56:22 +00:00
|
|
|
this.getRotationAngle = function(elem) {
|
|
|
|
var selected = elem || selectedElements[0];
|
2009-09-11 15:49:50 +00:00
|
|
|
if(!elem.transform) return null;
|
2009-08-16 14:56:22 +00:00
|
|
|
// find the rotation transform (if any) and set it
|
|
|
|
var tlist = selected.transform.baseVal;
|
|
|
|
var t = tlist.numberOfItems;
|
|
|
|
var foundRot = false;
|
|
|
|
while (t--) {
|
|
|
|
var xform = tlist.getItem(t);
|
|
|
|
if (xform.type == 4) {
|
|
|
|
return xform.angle;
|
|
|
|
}
|
|
|
|
}
|
2009-08-16 19:18:22 +00:00
|
|
|
return 0;
|
2009-08-16 14:56:22 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-04 05:10:48 +00:00
|
|
|
this.setRotationAngle = function(val,preventUndo) {
|
2009-08-16 06:02:26 +00:00
|
|
|
var elem = selectedElements[0];
|
2009-08-20 05:33:34 +00:00
|
|
|
// we use the actual element's bbox (not the calculated one) since the
|
|
|
|
// calculated bbox's center can change depending on the angle
|
2009-09-04 05:10:48 +00:00
|
|
|
var bbox = elem.getBBox();
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(bbox.x+bbox.width/2), cy = round(bbox.y+bbox.height/2);
|
2009-09-04 05:10:48 +00:00
|
|
|
var rotate = "rotate(" + val + " " + cx + "," + cy + ")";
|
|
|
|
if (preventUndo) {
|
|
|
|
this.changeSelectedAttributeNoUndo("transform", rotate, selectedElements);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.changeSelectedAttribute("transform",rotate,selectedElements);
|
|
|
|
}
|
2009-08-18 16:59:01 +00:00
|
|
|
var pointGripContainer = document.getElementById("polypointgrip_container");
|
2009-08-31 19:19:07 +00:00
|
|
|
if(elem.nodeName == "path" && pointGripContainer) {
|
2009-08-18 19:05:42 +00:00
|
|
|
pointGripContainer.setAttribute("transform", rotate);
|
|
|
|
}
|
2009-08-26 13:59:25 +00:00
|
|
|
selectorManager.requestSelector(selectedElements[0]).updateGripCursors(val);
|
2009-08-16 02:48:00 +00:00
|
|
|
};
|
2009-06-04 13:43:58 +00:00
|
|
|
|
2009-06-09 22:13:13 +00:00
|
|
|
this.each = function(cb) {
|
|
|
|
$(svgroot).children().each(cb);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-09 22:13:13 +00:00
|
|
|
|
2009-06-06 18:34:42 +00:00
|
|
|
this.bind = function(event, f) {
|
2009-09-02 16:51:11 +00:00
|
|
|
var old = events[event];
|
2009-06-06 18:34:42 +00:00
|
|
|
events[event] = f;
|
2009-09-02 16:51:11 +00:00
|
|
|
return old;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 18:34:42 +00:00
|
|
|
|
2009-06-12 06:45:37 +00:00
|
|
|
this.setIdPrefix = function(p) {
|
|
|
|
idprefix = p;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.getBold = function() {
|
|
|
|
// should only have one element selected
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
|
|
|
return (selected.getAttribute("font-weight") == "bold");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.setBold = function(b) {
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
2009-07-06 16:21:12 +00:00
|
|
|
this.changeSelectedAttribute("font-weight", b ? "bold" : "normal");
|
2009-07-06 14:57:13 +00:00
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.getItalic = function() {
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
|
|
|
return (selected.getAttribute("font-style") == "italic");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.setItalic = function(i) {
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
2009-07-06 15:03:56 +00:00
|
|
|
this.changeSelectedAttribute("font-style", i ? "italic" : "normal");
|
2009-07-06 14:57:13 +00:00
|
|
|
}
|
|
|
|
};
|
2009-06-12 06:45:37 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.getFontFamily = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_text.font_family;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.setFontFamily = function(val) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_text.font_family = val;
|
2009-06-29 14:38:30 +00:00
|
|
|
this.changeSelectedAttribute("font-family", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-10 03:12:19 +00:00
|
|
|
|
|
|
|
this.getFontSize = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_text.font_size;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.setFontSize = function(val) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_text.font_size = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("font-size", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.getText = function() {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-10 03:12:19 +00:00
|
|
|
if (selected == null) { return ""; }
|
|
|
|
return selected.textContent;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.setTextContent = function(val) {
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("#text", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-09-04 22:22:40 +00:00
|
|
|
this.setImageURL = function(val) {
|
|
|
|
this.changeSelectedAttribute("#href", val);
|
|
|
|
};
|
|
|
|
|
2009-06-11 15:18:46 +00:00
|
|
|
this.setRectRadius = function(val) {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-11 15:18:46 +00:00
|
|
|
if (selected != null && selected.tagName == "rect") {
|
2009-06-25 02:54:07 +00:00
|
|
|
var r = selected.getAttribute("rx");
|
|
|
|
if (r != val) {
|
|
|
|
selected.setAttribute("rx", val);
|
|
|
|
selected.setAttribute("ry", val);
|
|
|
|
addCommandToHistory(new ChangeElementCommand(selected, {"rx":r, "ry":r}, "Radius"));
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed", [selected]);
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-06-11 15:18:46 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-28 12:37:52 +00:00
|
|
|
|
|
|
|
this.quickClone = function(elem) {
|
|
|
|
// Hack for Firefox bugs where text element features aren't updated
|
2009-09-03 17:03:07 +00:00
|
|
|
if(navigator.userAgent.indexOf('Gecko/') == -1) return elem;
|
2009-08-28 12:37:52 +00:00
|
|
|
var clone = elem.cloneNode(true)
|
|
|
|
elem.parentNode.insertBefore(clone, elem);
|
|
|
|
elem.parentNode.removeChild(elem);
|
|
|
|
canvas.clearSelection();
|
|
|
|
canvas.addToSelection([clone],true);
|
|
|
|
return clone;
|
|
|
|
}
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-09-04 05:10:48 +00:00
|
|
|
// New functions for refactoring of Undo/Redo
|
|
|
|
|
|
|
|
// this is the stack that stores the original values, the elements and
|
|
|
|
// the attribute name for begin/finish
|
|
|
|
var undoChangeStackPointer = -1;
|
|
|
|
var undoableChangeStack = [];
|
|
|
|
|
|
|
|
// This function tells the canvas to remember the old values of the
|
|
|
|
// attrName attribute for each element sent in. The elements and values
|
|
|
|
// are stored on a stack, so the next call to finishUndoableChange() will
|
|
|
|
// pop the elements and old values off the stack, gets the current values
|
|
|
|
// from the DOM and uses all of these to construct the undo-able command.
|
|
|
|
this.beginUndoableChange = function(attrName, elems) {
|
|
|
|
var p = ++undoChangeStackPointer;
|
2009-07-08 04:40:45 +00:00
|
|
|
var i = elems.length;
|
2009-09-04 05:10:48 +00:00
|
|
|
var oldValues = new Array(i), elements = new Array(i);
|
|
|
|
while (i--) {
|
|
|
|
var elem = elems[i];
|
|
|
|
if (elem == null) continue;
|
|
|
|
elements[i] = elem;
|
|
|
|
oldValues[i] = elem.getAttribute(attrName);
|
|
|
|
}
|
|
|
|
undoableChangeStack[p] = {'attrName': attrName,
|
|
|
|
'oldValues': oldValues,
|
|
|
|
'elements': elements};
|
|
|
|
};
|
|
|
|
|
|
|
|
// This function makes the changes to the elements and then
|
|
|
|
// fires the 'changed' event
|
|
|
|
this.changeSelectedAttributeNoUndo = function(attr, newValue, elems) {
|
2009-07-30 22:45:59 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-09-04 06:30:18 +00:00
|
|
|
var elems = elems || selectedElements;
|
2009-09-04 05:10:48 +00:00
|
|
|
var i = elems.length;
|
|
|
|
while (i--) {
|
2009-07-08 04:40:45 +00:00
|
|
|
var elem = elems[i];
|
|
|
|
if (elem == null) continue;
|
2009-09-05 22:09:42 +00:00
|
|
|
// only allow the transform attribute to change on <g> elements, slightly hacky
|
|
|
|
if (elem.tagName == "g" && attr != "transform") continue;
|
2009-09-04 05:10:48 +00:00
|
|
|
var oldval = attr == "#text" ? elem.textContent : elem.getAttribute(attr);
|
|
|
|
if (oldval == null) oldval = "";
|
|
|
|
if (oldval != newValue) {
|
2009-08-28 12:37:52 +00:00
|
|
|
if (attr == "#text") {
|
2009-09-04 05:10:48 +00:00
|
|
|
elem.textContent = newValue;
|
2009-08-28 12:37:52 +00:00
|
|
|
elem = canvas.quickClone(elem);
|
2009-09-04 22:22:40 +00:00
|
|
|
} else if (attr == "#href") {
|
2009-09-05 11:19:42 +00:00
|
|
|
elem.setAttributeNS(xlinkns, "href", newValue);
|
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
else elem.setAttribute(attr, newValue);
|
2009-08-16 15:07:00 +00:00
|
|
|
selectedBBoxes[i] = this.getBBox(elem);
|
2009-09-03 17:03:07 +00:00
|
|
|
// Use the Firefox quickClone hack for text elements with gradients or
|
|
|
|
// where other text attributes are changed.
|
2009-08-28 13:19:47 +00:00
|
|
|
if(elem.nodeName == 'text') {
|
2009-09-04 13:42:44 +00:00
|
|
|
if((newValue+'').indexOf('url') == 0 || $.inArray(attr, ['font-size','font-family','x','y']) != -1) {
|
2009-08-28 13:19:47 +00:00
|
|
|
elem = canvas.quickClone(elem);
|
|
|
|
}
|
2009-08-27 18:42:46 +00:00
|
|
|
}
|
2009-08-13 15:42:41 +00:00
|
|
|
// Timeout needed for Opera & Firefox
|
|
|
|
setTimeout(function() {
|
|
|
|
selectorManager.requestSelector(elem).resize(selectedBBoxes[i]);
|
|
|
|
},0);
|
2009-08-23 03:18:59 +00:00
|
|
|
// if this element was rotated, and we changed the position of this element
|
2009-09-04 05:10:48 +00:00
|
|
|
// we need to update the rotational transform attribute
|
2009-08-23 03:18:59 +00:00
|
|
|
var angle = canvas.getRotationAngle(elem);
|
2009-09-02 22:17:04 +00:00
|
|
|
if (angle && attr != "transform") {
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(selectedBBoxes[i].x + selectedBBoxes[i].width/2),
|
|
|
|
cy = round(selectedBBoxes[i].y + selectedBBoxes[i].height/2);
|
2009-08-23 03:18:59 +00:00
|
|
|
var rotate = ["rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
|
|
|
if (rotate != elem.getAttribute("transform")) {
|
|
|
|
elem.setAttribute("transform", rotate);
|
|
|
|
}
|
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
} // if oldValue != newValue
|
|
|
|
} // for each elem
|
|
|
|
svgroot.unsuspendRedraw(handle);
|
|
|
|
call("changed", elems);
|
|
|
|
};
|
|
|
|
|
|
|
|
// This function returns a BatchCommand object which summarizes the
|
|
|
|
// change since beginUndoableChange was called. The command can then
|
|
|
|
// be added to the command history
|
|
|
|
this.finishUndoableChange = function() {
|
|
|
|
var p = undoChangeStackPointer--;
|
|
|
|
var changeset = undoableChangeStack[p];
|
|
|
|
var i = changeset['elements'].length;
|
|
|
|
var attrName = changeset['attrName'];
|
|
|
|
var batchCmd = new BatchCommand("Change " + attrName);
|
|
|
|
while (i--) {
|
|
|
|
var elem = changeset['elements'][i];
|
|
|
|
if (elem == null) continue;
|
|
|
|
var changes = {};
|
|
|
|
changes[attrName] = changeset['oldValues'][i];
|
|
|
|
if (changes[attrName] != elem.getAttribute(attrName)) {
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(elem, changes, attrName));
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-06-24 04:33:09 +00:00
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
undoableChangeStack[p] = null;
|
|
|
|
return batchCmd;
|
|
|
|
};
|
|
|
|
|
|
|
|
// If you want to change all selectedElements, ignore the elems argument.
|
|
|
|
// If you want to change only a subset of selectedElements, then send the
|
|
|
|
// subset to this function in the elems argument.
|
|
|
|
this.changeSelectedAttribute = function(attr, val, elems) {
|
|
|
|
var elems = elems || selectedElements;
|
|
|
|
canvas.beginUndoableChange(attr, elems);
|
|
|
|
var i = elems.length;
|
|
|
|
|
|
|
|
canvas.changeSelectedAttributeNoUndo(attr, val, elems);
|
|
|
|
|
|
|
|
var batchCmd = canvas.finishUndoableChange();
|
2009-07-08 04:40:45 +00:00
|
|
|
if (!batchCmd.isEmpty()) {
|
2009-09-04 05:10:48 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-07-08 04:40:45 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-07 00:17:21 +00:00
|
|
|
$(container).mouseup(mouseUp);
|
|
|
|
$(container).mousedown(mouseDown);
|
|
|
|
$(container).mousemove(mouseMove);
|
2009-06-01 17:35:22 +00:00
|
|
|
|
2009-07-01 20:22:29 +00:00
|
|
|
this.deleteSelectedElements = function() {
|
2009-07-01 21:28:22 +00:00
|
|
|
var batchCmd = new BatchCommand("Delete Elements");
|
2009-07-03 02:49:42 +00:00
|
|
|
var len = selectedElements.length;
|
2009-09-05 22:09:42 +00:00
|
|
|
var selectedCopy = []; //selectedElements is being deleted
|
2009-07-03 02:49:42 +00:00
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-07-01 20:22:29 +00:00
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected == null) break;
|
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
var parent = selected.parentNode;
|
2009-06-10 04:44:59 +00:00
|
|
|
var t = selected;
|
2009-07-01 20:22:29 +00:00
|
|
|
// this will unselect the element and remove the selectedOutline
|
|
|
|
selectorManager.releaseSelector(t);
|
2009-06-25 02:54:07 +00:00
|
|
|
var elem = parent.removeChild(t);
|
2009-09-02 18:22:35 +00:00
|
|
|
selectedCopy.push(selected) //for the copy
|
2009-07-01 20:22:29 +00:00
|
|
|
selectedElements[i] = null;
|
2009-07-01 21:28:22 +00:00
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(elem, parent));
|
2009-06-10 04:44:59 +00:00
|
|
|
}
|
2009-07-01 21:28:22 +00:00
|
|
|
if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
|
2009-09-22 18:29:15 +00:00
|
|
|
call("changed", selectedCopy);
|
|
|
|
canvas.clearSelection();
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-09-05 22:09:42 +00:00
|
|
|
|
|
|
|
this.groupSelectedElements = function() {
|
|
|
|
var batchCmd = new BatchCommand("Group Elements");
|
|
|
|
|
|
|
|
// create and insert the group element
|
|
|
|
var g = addSvgElementFromJson({
|
|
|
|
"element": "g",
|
|
|
|
"attr": {
|
|
|
|
"id": getNextId()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
batchCmd.addSubCommand(new InsertElementCommand(g));
|
|
|
|
|
|
|
|
// now move all children into the group
|
|
|
|
var i = selectedElements.length;
|
|
|
|
while (i--) {
|
|
|
|
var elem = selectedElements[i];
|
2009-09-06 23:25:12 +00:00
|
|
|
if (elem == null) continue;
|
2009-09-05 22:09:42 +00:00
|
|
|
var oldNextSibling = elem.nextSibling;
|
|
|
|
var oldParent = elem.parentNode;
|
|
|
|
g.appendChild(elem);
|
|
|
|
batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldParent));
|
|
|
|
}
|
|
|
|
if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
|
|
|
|
|
|
|
|
// ensure selectors are at bottom and update selection
|
|
|
|
selectorManager.update();
|
|
|
|
canvas.clearSelection();
|
|
|
|
canvas.addToSelection([g]);
|
|
|
|
};
|
|
|
|
|
2009-09-06 13:00:05 +00:00
|
|
|
// TODO: when transferring group's rotational transform to the children, must deal
|
2009-09-23 16:18:04 +00:00
|
|
|
// with children who are already rotated within the group (Issue 204)
|
2009-09-05 22:09:42 +00:00
|
|
|
this.ungroupSelectedElement = function() {
|
|
|
|
var g = selectedElements[0];
|
|
|
|
if (g.tagName == "g") {
|
|
|
|
var batchCmd = new BatchCommand("Ungroup Elements");
|
|
|
|
var parent = g.parentNode;
|
|
|
|
var anchor = g.previousSibling;
|
|
|
|
var children = new Array(g.childNodes.length);
|
2009-09-06 03:51:16 +00:00
|
|
|
var xform = g.getAttribute("transform");
|
2009-09-05 22:09:42 +00:00
|
|
|
var i = 0;
|
2009-09-06 13:00:05 +00:00
|
|
|
var gbox = g.getBBox(),
|
|
|
|
gx = gbox.x + gbox.width/2,
|
|
|
|
gy = gbox.y + gbox.height/2;
|
2009-09-05 22:09:42 +00:00
|
|
|
while (g.firstChild) {
|
|
|
|
var elem = g.firstChild;
|
|
|
|
var oldNextSibling = elem.nextSibling;
|
|
|
|
var oldParent = elem.parentNode;
|
|
|
|
children[i++] = elem = parent.insertBefore(elem, anchor);
|
2009-09-06 03:51:16 +00:00
|
|
|
batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldParent));
|
|
|
|
if (xform) {
|
2009-09-06 13:00:05 +00:00
|
|
|
var angle = canvas.getRotationAngle(g) * Math.PI / 180.0;
|
|
|
|
var childBox = elem.getBBox();
|
|
|
|
var cx = childBox.x + childBox.width/2,
|
|
|
|
cy = childBox.y + childBox.height/2,
|
|
|
|
dx = cx - gx,
|
|
|
|
dy = cy - gy,
|
|
|
|
r = Math.sqrt(dx*dx + dy*dy);
|
|
|
|
angle += Math.atan2(dy,dx);
|
|
|
|
var newcx = r * Math.cos(angle) + gx,
|
|
|
|
newcy = r * Math.sin(angle) + gy;
|
|
|
|
childBox.x += (newcx - cx);
|
|
|
|
childBox.y += (newcy - cy);
|
2009-09-06 03:51:16 +00:00
|
|
|
elem.setAttribute("transform", xform);
|
2009-09-06 13:00:05 +00:00
|
|
|
batchCmd.addSubCommand(recalculateDimensions(elem, childBox));
|
2009-09-06 03:51:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove transform and make it undo-able
|
|
|
|
if (xform) {
|
|
|
|
var changes = {};
|
2009-09-06 13:00:05 +00:00
|
|
|
changes["transform"] = xform;
|
|
|
|
g.setAttribute("transform", "");
|
|
|
|
g.removeAttribute("transform");
|
2009-09-06 03:51:16 +00:00
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(g, changes));
|
2009-09-05 22:09:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove the group from the selection
|
|
|
|
canvas.clearSelection();
|
|
|
|
|
|
|
|
// delete the group element (but make undo-able)
|
|
|
|
g = parent.removeChild(g);
|
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(g, parent));
|
|
|
|
|
|
|
|
if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
|
|
|
|
|
|
|
|
// ensure selectors are at bottom and update selection
|
|
|
|
selectorManager.update();
|
|
|
|
canvas.addToSelection(children);
|
|
|
|
}
|
|
|
|
};
|
2009-06-10 04:44:59 +00:00
|
|
|
|
2009-06-17 20:08:53 +00:00
|
|
|
this.moveToTopSelectedElement = function() {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-17 20:08:53 +00:00
|
|
|
if (selected != null) {
|
|
|
|
var t = selected;
|
2009-06-25 02:54:07 +00:00
|
|
|
var oldParent = t.parentNode;
|
|
|
|
var oldNextSibling = t.nextSibling;
|
2009-06-29 13:20:39 +00:00
|
|
|
if (oldNextSibling == selectorManager.selectorParentGroup) oldNextSibling = null;
|
2009-06-25 02:54:07 +00:00
|
|
|
t = t.parentNode.appendChild(t);
|
|
|
|
addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, "top"));
|
2009-06-17 20:08:53 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-17 20:08:53 +00:00
|
|
|
|
|
|
|
this.moveToBottomSelectedElement = function() {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-17 20:08:53 +00:00
|
|
|
if (selected != null) {
|
|
|
|
var t = selected;
|
2009-06-25 02:54:07 +00:00
|
|
|
var oldParent = t.parentNode;
|
|
|
|
var oldNextSibling = t.nextSibling;
|
2009-06-29 13:20:39 +00:00
|
|
|
if (oldNextSibling == selectorManager.selectorParentGroup) oldNextSibling = null;
|
2009-08-28 14:39:53 +00:00
|
|
|
// first child is a comment, so call nextSibling
|
|
|
|
var firstChild = t.parentNode.firstChild.nextSibling;
|
2009-07-30 22:45:59 +00:00
|
|
|
if (firstChild.tagName == 'defs') {
|
|
|
|
firstChild = firstChild.nextSibling;
|
|
|
|
}
|
|
|
|
t = t.parentNode.insertBefore(t, firstChild);
|
2009-06-25 02:54:07 +00:00
|
|
|
addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, "bottom"));
|
2009-06-17 20:08:53 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-07-13 13:51:00 +00:00
|
|
|
this.moveSelectedElements = function(dx,dy,undoable) {
|
|
|
|
// if undoable is not sent, default to true
|
2009-09-13 04:09:33 +00:00
|
|
|
// if single values, scale them to the zoom
|
|
|
|
if (dx.constructor != Array) {
|
|
|
|
dx *= current_zoom;
|
|
|
|
dy *= current_zoom;
|
|
|
|
}
|
2009-08-08 03:36:01 +00:00
|
|
|
var undoable = undoable || true;
|
2009-07-08 00:03:08 +00:00
|
|
|
var batchCmd = new BatchCommand("position");
|
|
|
|
var i = selectedElements.length;
|
|
|
|
while (i--) {
|
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected != null) {
|
2009-08-16 15:07:00 +00:00
|
|
|
selectedBBoxes[i] = this.getBBox(selected);
|
2009-08-23 17:16:27 +00:00
|
|
|
// dx and dy could be arrays
|
|
|
|
if (dx.constructor == Array) {
|
2009-09-13 04:09:33 +00:00
|
|
|
selectedBBoxes[i].x += dx[i] * current_zoom;
|
2009-08-23 17:16:27 +00:00
|
|
|
} else {
|
|
|
|
selectedBBoxes[i].x += dx;
|
|
|
|
}
|
|
|
|
if (dy.constructor == Array) {
|
2009-09-13 04:09:33 +00:00
|
|
|
selectedBBoxes[i].y += dy[i] * current_zoom;
|
2009-08-23 17:16:27 +00:00
|
|
|
} else {
|
|
|
|
selectedBBoxes[i].y += dy;
|
|
|
|
}
|
2009-09-05 23:09:46 +00:00
|
|
|
var cmd = recalculateDimensions(selected,selectedBBoxes[i]);
|
2009-07-08 00:03:08 +00:00
|
|
|
if (cmd) {
|
|
|
|
batchCmd.addSubCommand(cmd);
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
selectorManager.requestSelector(selected).resize(selectedBBoxes[i]);
|
2009-07-08 00:03:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!batchCmd.isEmpty()) {
|
2009-07-13 13:51:00 +00:00
|
|
|
if (undoable)
|
|
|
|
addCommandToHistory(batchCmd);
|
2009-07-08 00:03:08 +00:00
|
|
|
call("changed", selectedElements);
|
2009-06-23 21:18:23 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-17 20:08:53 +00:00
|
|
|
|
2009-09-22 14:35:05 +00:00
|
|
|
this.getStrokedBBox = function(elems) {
|
|
|
|
// TODO: Get correct BBoxes for rotated elements
|
|
|
|
if(!elems) elems = canvas.getVisibleElements();
|
2009-09-22 17:22:28 +00:00
|
|
|
if(!elems.length) return false;
|
2009-09-22 14:35:05 +00:00
|
|
|
var full_bb = elems[0].getBBox();
|
|
|
|
var max_x = full_bb.x + full_bb.width;
|
|
|
|
var max_y = full_bb.y + full_bb.height;
|
|
|
|
var min_x = full_bb.x;
|
|
|
|
var min_y = full_bb.y;
|
|
|
|
var getOffset = function(elem) {
|
|
|
|
var sw = round(elem.getAttribute("stroke-width"));
|
|
|
|
var offset = 0;
|
|
|
|
if (elem.getAttribute("stroke") != "none" && !isNaN(sw)) {
|
|
|
|
offset += sw/2;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.each(elems, function(i, elem) {
|
|
|
|
var cur_bb = elem.getBBox();
|
|
|
|
var offset = getOffset(elem);
|
|
|
|
min_x = Math.min(min_x, cur_bb.x - offset);
|
|
|
|
min_y = Math.min(min_y, cur_bb.y - offset);
|
|
|
|
});
|
|
|
|
|
|
|
|
full_bb.x = min_x;
|
|
|
|
full_bb.y = min_y;
|
|
|
|
|
|
|
|
$.each(elems, function(i, elem) {
|
|
|
|
var cur_bb = elem.getBBox();
|
|
|
|
var offset = getOffset(elem);
|
|
|
|
max_x = Math.max(max_x, cur_bb.x + cur_bb.width + offset);
|
|
|
|
max_y = Math.max(max_y, cur_bb.y + cur_bb.height + offset);
|
|
|
|
});
|
|
|
|
|
|
|
|
full_bb.width = max_x - min_x;
|
|
|
|
full_bb.height = max_y - min_y;
|
|
|
|
return full_bb;
|
|
|
|
}
|
|
|
|
|
2009-09-23 15:29:55 +00:00
|
|
|
this.getVisibleElements = function(parent, includeBBox) {
|
|
|
|
if(!parent) parent = svgzoom;
|
|
|
|
var nodes = parent.childNodes;
|
2009-08-28 19:20:54 +00:00
|
|
|
var i = nodes.length;
|
|
|
|
var contentElems = [];
|
|
|
|
|
|
|
|
while (i--) {
|
|
|
|
var elem = nodes[i];
|
|
|
|
try {
|
|
|
|
var box = canvas.getBBox(elem);
|
2009-09-09 14:18:24 +00:00
|
|
|
if (box) {
|
2009-08-28 19:20:54 +00:00
|
|
|
var item = includeBBox?{'elem':elem, 'bbox':box}:elem;
|
|
|
|
contentElems.push(item);
|
|
|
|
}
|
|
|
|
} catch(e) {}
|
|
|
|
}
|
|
|
|
return contentElems;
|
|
|
|
}
|
|
|
|
|
2009-08-21 13:39:23 +00:00
|
|
|
this.cycleElement = function(next) {
|
|
|
|
var cur_elem = selectedElements[0];
|
|
|
|
var elem = false;
|
2009-08-28 19:20:54 +00:00
|
|
|
var all_elems = this.getVisibleElements();
|
2009-08-21 13:39:23 +00:00
|
|
|
if (cur_elem == null) {
|
2009-08-28 19:20:54 +00:00
|
|
|
var num = next?all_elems.length-1:0;
|
|
|
|
elem = all_elems[num];
|
2009-08-21 13:39:23 +00:00
|
|
|
} else {
|
2009-08-28 19:20:54 +00:00
|
|
|
var i = all_elems.length;
|
|
|
|
while(i--) {
|
|
|
|
if(all_elems[i] == cur_elem) {
|
|
|
|
var num = next?i-1:i+1;
|
2009-08-31 17:03:33 +00:00
|
|
|
if(num >= all_elems.length) {
|
|
|
|
num = 0;
|
|
|
|
} else if(num < 0) {
|
|
|
|
num = all_elems.length-1;
|
|
|
|
}
|
|
|
|
elem = all_elems[num];
|
|
|
|
break;
|
2009-08-28 19:20:54 +00:00
|
|
|
}
|
2009-08-21 13:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas.clearSelection();
|
|
|
|
canvas.addToSelection([elem], true);
|
|
|
|
call("selected", selectedElements);
|
|
|
|
}
|
|
|
|
|
2009-07-02 03:54:11 +00:00
|
|
|
var resetUndoStack = function() {
|
|
|
|
undoStack = [];
|
|
|
|
undoStackPointer = 0;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
|
|
|
this.getUndoStackSize = function() { return undoStackPointer; };
|
2009-07-06 18:54:38 +00:00
|
|
|
this.getRedoStackSize = function() { return undoStack.length - undoStackPointer; };
|
|
|
|
|
|
|
|
this.getNextUndoCommandText = function() {
|
|
|
|
if (undoStackPointer > 0)
|
|
|
|
return undoStack[undoStackPointer-1].text;
|
|
|
|
return "";
|
|
|
|
};
|
|
|
|
this.getNextRedoCommandText = function() {
|
|
|
|
if (undoStackPointer < undoStack.length)
|
|
|
|
return undoStack[undoStackPointer].text;
|
|
|
|
return "";
|
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.undo = function() {
|
2009-06-29 14:38:30 +00:00
|
|
|
if (undoStackPointer > 0) {
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-08-29 22:10:52 +00:00
|
|
|
removeAllPointGripsFromPoly();
|
2009-06-25 15:35:28 +00:00
|
|
|
var cmd = undoStack[--undoStackPointer];
|
|
|
|
cmd.unapply();
|
2009-07-01 21:28:22 +00:00
|
|
|
call("changed", cmd.elements());
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-07-06 18:54:38 +00:00
|
|
|
};
|
2009-06-25 02:54:07 +00:00
|
|
|
this.redo = function() {
|
|
|
|
if (undoStackPointer < undoStack.length && undoStack.length > 0) {
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-06-25 15:35:28 +00:00
|
|
|
var cmd = undoStack[undoStackPointer++];
|
|
|
|
cmd.apply();
|
2009-07-01 21:28:22 +00:00
|
|
|
call("changed", cmd.elements());
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-13 13:51:00 +00:00
|
|
|
// this creates deep DOM copies (clones) of all selected elements
|
2009-08-06 11:43:19 +00:00
|
|
|
this.cloneSelectedElements = function() {
|
2009-09-23 13:43:13 +00:00
|
|
|
var copyElem = function(el) {
|
|
|
|
// Manual clone function for Opera/Win/non-EN.
|
|
|
|
// Needed because cloneNode changes "." to "," on float values
|
|
|
|
if(!window.opera) return el.cloneNode(true);
|
|
|
|
var new_el = document.createElementNS(svgns, el.nodeName);
|
|
|
|
$.each(el.attributes, function(i, attr) {
|
|
|
|
var ns = attr.nodeName == 'href'?xlinkns:null;
|
|
|
|
new_el.setAttributeNS(ns, attr.nodeName, attr.nodeValue);
|
|
|
|
});
|
|
|
|
|
|
|
|
$.each(el.childNodes, function(i, child) {
|
|
|
|
switch(child.nodeType) {
|
|
|
|
case 1: // element node
|
|
|
|
new_el.appendChild(copyElem(child));
|
|
|
|
break;
|
|
|
|
case 3: // text node
|
|
|
|
new_el.textContent = child.nodeValue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return new_el;
|
|
|
|
}
|
|
|
|
|
2009-08-06 11:43:19 +00:00
|
|
|
var batchCmd = new BatchCommand("Clone Elements");
|
2009-09-01 19:47:25 +00:00
|
|
|
// find all the elements selected (stop at first null)
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-08-29 18:38:34 +00:00
|
|
|
var elem = selectedElements[i];
|
|
|
|
if (elem == null) break;
|
2009-07-13 13:51:00 +00:00
|
|
|
}
|
2009-09-01 19:47:25 +00:00
|
|
|
// use slice to quickly get the subset of elements we need
|
|
|
|
var copiedElements = selectedElements.slice(0,i);
|
2009-07-13 13:51:00 +00:00
|
|
|
this.clearSelection();
|
2009-09-01 19:47:25 +00:00
|
|
|
// note that we loop in the reverse way because of the way elements are added
|
|
|
|
// to the selectedElements array (top-first)
|
|
|
|
var i = copiedElements.length;
|
|
|
|
while (i--) {
|
|
|
|
// clone each element and replace it within copiedElements
|
2009-09-23 13:43:13 +00:00
|
|
|
var elem = copiedElements[i] = copyElem(copiedElements[i]);
|
2009-08-25 12:40:58 +00:00
|
|
|
elem.removeAttribute("id");
|
2009-07-13 13:51:00 +00:00
|
|
|
elem.id = getNextId();
|
2009-09-22 18:58:52 +00:00
|
|
|
current_layer.appendChild(elem);
|
2009-07-13 13:51:00 +00:00
|
|
|
batchCmd.addSubCommand(new InsertElementCommand(elem));
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-13 13:51:00 +00:00
|
|
|
if (!batchCmd.isEmpty()) {
|
|
|
|
this.addToSelection(copiedElements);
|
|
|
|
this.moveSelectedElements(20,20,false);
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
call("selected", selectedElements);
|
2009-08-17 06:56:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// aligns selected elements (type is a char - see switch below for explanation)
|
2009-08-25 07:04:19 +00:00
|
|
|
// relative_to can be "selected", "largest", "smallest", "page"
|
|
|
|
this.alignSelectedElements = function(type, relative_to) {
|
2009-08-24 15:30:11 +00:00
|
|
|
var bboxes = [], angles = [];
|
2009-08-23 17:16:27 +00:00
|
|
|
var minx = Number.MAX_VALUE, maxx = Number.MIN_VALUE, miny = Number.MAX_VALUE, maxy = Number.MIN_VALUE;
|
2009-08-25 07:04:19 +00:00
|
|
|
var curwidth = Number.MIN_VALUE, curheight = Number.MIN_VALUE;
|
2009-08-17 06:56:55 +00:00
|
|
|
var len = selectedElements.length;
|
|
|
|
if (!len) return;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) break;
|
|
|
|
var elem = selectedElements[i];
|
2009-08-24 15:30:11 +00:00
|
|
|
bboxes[i] = this.getBBox(elem);
|
|
|
|
// TODO: could make the following code block as part of getBBox() and add a parameter
|
|
|
|
// to that function
|
|
|
|
// if element is rotated, get angle and rotate the 4 corners of the bbox and get
|
|
|
|
// the new axis-aligned bbox
|
|
|
|
angles[i] = this.getRotationAngle(elem) * Math.PI / 180.0;
|
|
|
|
if (angles[i]) {
|
|
|
|
var rminx = Number.MAX_VALUE, rminy = Number.MAX_VALUE,
|
|
|
|
rmaxx = Number.MIN_VALUE, rmaxy = Number.MIN_VALUE;
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(bboxes[i].x + bboxes[i].width/2),
|
|
|
|
cy = round(bboxes[i].y + bboxes[i].height/2);
|
2009-08-24 15:30:11 +00:00
|
|
|
var pts = [ [bboxes[i].x - cx, bboxes[i].y - cy],
|
|
|
|
[bboxes[i].x + bboxes[i].width - cx, bboxes[i].y - cy],
|
2009-08-25 07:04:19 +00:00
|
|
|
[bboxes[i].x + bboxes[i].width - cx, bboxes[i].y + bboxes[i].height - cy],
|
2009-08-24 15:30:11 +00:00
|
|
|
[bboxes[i].x - cx, bboxes[i].y + bboxes[i].height - cy] ];
|
|
|
|
var j = 4;
|
|
|
|
while (j--) {
|
2009-08-25 07:04:19 +00:00
|
|
|
var x = pts[j][0],
|
2009-08-24 15:30:11 +00:00
|
|
|
y = pts[j][1],
|
|
|
|
r = Math.sqrt( x*x + y*y );
|
|
|
|
var theta = Math.atan2(y,x) + angles[i];
|
2009-09-17 14:47:54 +00:00
|
|
|
x = round(r * Math.cos(theta) + cx);
|
|
|
|
y = round(r * Math.sin(theta) + cy);
|
2009-08-24 15:30:11 +00:00
|
|
|
|
|
|
|
// now set the bbox for the shape after it's been rotated
|
|
|
|
if (x < rminx) rminx = x;
|
|
|
|
if (y < rminy) rminy = y;
|
|
|
|
if (x > rmaxx) rmaxx = x;
|
|
|
|
if (y > rmaxy) rmaxy = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
bboxes[i].x = rminx;
|
|
|
|
bboxes[i].y = rminy;
|
|
|
|
bboxes[i].width = rmaxx - rminx;
|
|
|
|
bboxes[i].height = rmaxy - rminy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now bbox is axis-aligned and handles rotation
|
2009-08-25 07:04:19 +00:00
|
|
|
switch (relative_to) {
|
|
|
|
case 'smallest':
|
|
|
|
if ( (type == 'l' || type == 'c' || type == 'r') && (curwidth == Number.MIN_VALUE || curwidth > bboxes[i].width) ||
|
|
|
|
(type == 't' || type == 'm' || type == 'b') && (curheight == Number.MIN_VALUE || curheight > bboxes[i].height) ) {
|
|
|
|
minx = bboxes[i].x;
|
|
|
|
miny = bboxes[i].y;
|
|
|
|
maxx = bboxes[i].x + bboxes[i].width;
|
|
|
|
maxy = bboxes[i].y + bboxes[i].height;
|
|
|
|
curwidth = bboxes[i].width;
|
|
|
|
curheight = bboxes[i].height;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'largest':
|
|
|
|
if ( (type == 'l' || type == 'c' || type == 'r') && (curwidth == Number.MIN_VALUE || curwidth < bboxes[i].width) ||
|
|
|
|
(type == 't' || type == 'm' || type == 'b') && (curheight == Number.MIN_VALUE || curheight < bboxes[i].height) ) {
|
|
|
|
minx = bboxes[i].x;
|
|
|
|
miny = bboxes[i].y;
|
|
|
|
maxx = bboxes[i].x + bboxes[i].width;
|
|
|
|
maxy = bboxes[i].y + bboxes[i].height;
|
|
|
|
curwidth = bboxes[i].width;
|
|
|
|
curheight = bboxes[i].height;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: // 'selected'
|
|
|
|
if (bboxes[i].x < minx) minx = bboxes[i].x;
|
|
|
|
if (bboxes[i].y < miny) miny = bboxes[i].y;
|
|
|
|
if (bboxes[i].x + bboxes[i].width > maxx) maxx = bboxes[i].x + bboxes[i].width;
|
|
|
|
if (bboxes[i].y + bboxes[i].height > maxy) maxy = bboxes[i].y + bboxes[i].height;
|
|
|
|
break;
|
|
|
|
}
|
2009-08-24 15:30:11 +00:00
|
|
|
} // loop for each element to find the bbox and adjust min/max
|
2009-08-25 07:04:19 +00:00
|
|
|
|
|
|
|
if (relative_to == 'page') {
|
|
|
|
minx = 0;
|
|
|
|
miny = 0;
|
|
|
|
maxx = svgroot.getAttribute('width');
|
|
|
|
maxy = svgroot.getAttribute('height');
|
|
|
|
}
|
|
|
|
|
2009-08-23 17:16:27 +00:00
|
|
|
var dx = new Array(len);
|
|
|
|
var dy = new Array(len);
|
2009-08-17 06:56:55 +00:00
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) break;
|
|
|
|
var elem = selectedElements[i];
|
2009-08-24 15:30:11 +00:00
|
|
|
var bbox = bboxes[i];
|
2009-08-23 17:16:27 +00:00
|
|
|
dx[i] = 0;
|
|
|
|
dy[i] = 0;
|
|
|
|
switch (type) {
|
|
|
|
case 'l': // left (horizontal)
|
|
|
|
dx[i] = minx - bbox.x;
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'c': // center (horizontal)
|
|
|
|
dx[i] = (minx+maxx)/2 - (bbox.x + bbox.width/2);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'r': // right (horizontal)
|
|
|
|
dx[i] = maxx - (bbox.x + bbox.width);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 't': // top (vertical)
|
|
|
|
dy[i] = miny - bbox.y;
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'm': // middle (vertical)
|
|
|
|
dy[i] = (miny+maxy)/2 - (bbox.y + bbox.height/2);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'b': // bottom (vertical)
|
|
|
|
dy[i] = maxy - (bbox.y + bbox.height);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-08-23 17:16:27 +00:00
|
|
|
this.moveSelectedElements(dx,dy);
|
2009-07-13 13:51:00 +00:00
|
|
|
};
|
2009-09-17 14:47:54 +00:00
|
|
|
this.getZoom = function(){return current_zoom;};
|
2009-09-21 02:05:58 +00:00
|
|
|
|
|
|
|
this.clear();
|
|
|
|
};
|
2009-06-08 08:09:15 +00:00
|
|
|
|
|
|
|
// Static class for various utility functions
|
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
var Utils = {
|
2009-06-08 08:09:15 +00:00
|
|
|
|
|
|
|
// This code was written by Tyler Akins and has been placed in the
|
|
|
|
// public domain. It would be nice if you left this header intact.
|
|
|
|
// Base64 code from Tyler Akins -- http://rumkin.com
|
|
|
|
|
2009-06-23 07:48:15 +00:00
|
|
|
// schiller: Removed string concatenation in favour of Array.join() optimization,
|
2009-06-17 03:20:42 +00:00
|
|
|
// also precalculate the size of the array needed.
|
2009-06-16 18:51:35 +00:00
|
|
|
|
2009-06-12 13:27:42 +00:00
|
|
|
"_keyStr" : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-12 13:27:42 +00:00
|
|
|
"encode64" : function(input) {
|
2009-06-17 03:20:42 +00:00
|
|
|
// base64 strings are 4/3 larger than the original string
|
2009-06-17 19:00:23 +00:00
|
|
|
var output = new Array( Math.floor( (input.length + 2) / 3 ) * 4 );
|
2009-06-08 09:08:45 +00:00
|
|
|
var chr1, chr2, chr3;
|
|
|
|
var enc1, enc2, enc3, enc4;
|
2009-06-17 03:20:42 +00:00
|
|
|
var i = 0, p = 0;
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
do {
|
|
|
|
chr1 = input.charCodeAt(i++);
|
|
|
|
chr2 = input.charCodeAt(i++);
|
|
|
|
chr3 = input.charCodeAt(i++);
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
enc1 = chr1 >> 2;
|
|
|
|
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
|
|
|
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
|
|
|
enc4 = chr3 & 63;
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
if (isNaN(chr2)) {
|
|
|
|
enc3 = enc4 = 64;
|
|
|
|
} else if (isNaN(chr3)) {
|
|
|
|
enc4 = 64;
|
|
|
|
}
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-17 03:20:42 +00:00
|
|
|
output[p++] = this._keyStr.charAt(enc1);
|
|
|
|
output[p++] = this._keyStr.charAt(enc2);
|
|
|
|
output[p++] = this._keyStr.charAt(enc3);
|
|
|
|
output[p++] = this._keyStr.charAt(enc4);
|
2009-06-08 09:08:45 +00:00
|
|
|
} while (i < input.length);
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-16 18:51:35 +00:00
|
|
|
return output.join('');
|
2009-07-02 04:50:24 +00:00
|
|
|
},
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-02 04:50:24 +00:00
|
|
|
"rectsIntersect": function(r1, r2) {
|
|
|
|
return r2.x < (r1.x+r1.width) &&
|
|
|
|
(r2.x+r2.width) > r1.x &&
|
|
|
|
r2.y < (r1.y+r1.height) &&
|
|
|
|
(r2.y+r2.height) > r1.y;
|
2009-07-10 05:04:06 +00:00
|
|
|
},
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// found this function http://groups.google.com/group/jquery-dev/browse_thread/thread/c6d11387c580a77f
|
|
|
|
"text2xml": function(sXML) {
|
2009-08-17 06:56:55 +00:00
|
|
|
// NOTE: I'd like to use jQuery for this, but jQuery makes all tags uppercase
|
|
|
|
//return $(xml)[0];
|
|
|
|
var out;
|
|
|
|
try{
|
|
|
|
var dXML = ($.browser.msie)?new ActiveXObject("Microsoft.XMLDOM"):new DOMParser();
|
|
|
|
dXML.async = false;
|
|
|
|
} catch(e){
|
|
|
|
throw new Error("XML Parser could not be instantiated");
|
|
|
|
};
|
|
|
|
try{
|
|
|
|
if($.browser.msie) out = (dXML.loadXML(sXML))?dXML:false;
|
|
|
|
else out = dXML.parseFromString(sXML, "text/xml");
|
|
|
|
}
|
|
|
|
catch(e){ throw new Error("Error parsing XML string"); };
|
|
|
|
return out;
|
|
|
|
}
|
2009-07-02 04:50:24 +00:00
|
|
|
};
|