✨ Further changes to handling of parts and copying their data
This commit is contained in:
parent
415a98f68e
commit
fd80450d77
6 changed files with 30 additions and 44 deletions
|
@ -18,6 +18,13 @@ attributes.prototype.add = function(name, value) {
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Sets an attribute, overwriting existing value */
|
||||||
|
attributes.prototype.set = function(name, value) {
|
||||||
|
this.list[name] = [value];
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
/** Retrieves an attribute */
|
/** Retrieves an attribute */
|
||||||
attributes.prototype.get = function(name) {
|
attributes.prototype.get = function(name) {
|
||||||
if (typeof this.list[name] === "undefined") return false;
|
if (typeof this.list[name] === "undefined") return false;
|
||||||
|
|
53
src/part.js
53
src/part.js
|
@ -6,18 +6,16 @@ import attributes from "./attributes";
|
||||||
import * as hooklib from "hooks";
|
import * as hooklib from "hooks";
|
||||||
import { round, units } from "./utils";
|
import { round, units } from "./utils";
|
||||||
|
|
||||||
function part(id) {
|
function part() {
|
||||||
this.attributes = new attributes();
|
this.attributes = new attributes();
|
||||||
this.points = {};
|
this.points = {};
|
||||||
this.paths = {};
|
this.paths = {};
|
||||||
this.snippets = {};
|
this.snippets = {};
|
||||||
//this.id = id;
|
|
||||||
this.freeId = 0;
|
this.freeId = 0;
|
||||||
this.topLeft = false;
|
this.topLeft = false;
|
||||||
this.bottomRight = false;
|
this.bottomRight = false;
|
||||||
this.width = false;
|
this.width = false;
|
||||||
this.height = false;
|
this.height = false;
|
||||||
//this.render = id.substr(0, 1) === "_" ? false : true;
|
|
||||||
this.render = true;
|
this.render = true;
|
||||||
this.points.origin = new point(0, 0);
|
this.points.origin = new point(0, 0);
|
||||||
for (let k in hooklib) this[k] = hooklib[k];
|
for (let k in hooklib) this[k] = hooklib[k];
|
||||||
|
@ -99,45 +97,24 @@ part.prototype.stack = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Adds an attribute. This is here to make this call chainable in assignment */
|
/** Adds an attribute. This is here to make this call chainable in assignment */
|
||||||
part.prototype.attr = function(name, value) {
|
part.prototype.attr = function(name, value, overwrite = false) {
|
||||||
this.attributes.add(name, value);
|
if (overwrite) this.attributes.set(name, value);
|
||||||
|
else this.attributes.add(name, value);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Returns a (deep) clone of this part object */
|
/** Copies point/path/snippet data from part orig into this */
|
||||||
part.prototype.clone = function() {};
|
part.prototype.copy = function(orig) {
|
||||||
|
for (let type of ["points", "paths", "snippets"]) {
|
||||||
|
for (let i in orig[type]) {
|
||||||
|
if (typeof this[type][i] === "undefined") {
|
||||||
|
this[type][i] = orig[type][i].clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns a deep copy of this */
|
return this;
|
||||||
part.prototype.clone = function(id = false) {
|
|
||||||
let clone = new part(id);
|
|
||||||
clone.freeId = 0;
|
|
||||||
clone.width = this.width;
|
|
||||||
clone.height = this.height;
|
|
||||||
clone.attributes = this.attributes.clone();
|
|
||||||
clone.render = this.render;
|
|
||||||
|
|
||||||
if (!id) clone.id = this.id;
|
|
||||||
if (this.topLeft) clone.topLeft = this.topLeft.clone();
|
|
||||||
else clone.topLeft = false;
|
|
||||||
if (this.bottomRight) clone.bottomRight = this.bottomRight.clone();
|
|
||||||
else clone.bottomRight = false;
|
|
||||||
|
|
||||||
clone.points = {};
|
|
||||||
clone.paths = {};
|
|
||||||
clone.snippets = {};
|
|
||||||
for (let i in this.points) clone.points[i] = this.points[i].clone();
|
|
||||||
for (let i in this.paths) clone.paths[i] = this.paths[i].clone();
|
|
||||||
for (let i in this.snippets) clone.snippets[i] = this.snippets[i].clone();
|
|
||||||
for (let k in hooklib) clone[k] = hooklib[k];
|
|
||||||
|
|
||||||
clone.point = point;
|
|
||||||
clone.path = path;
|
|
||||||
clone.snippet = snippet;
|
|
||||||
clone.round = round;
|
|
||||||
|
|
||||||
clone.context = this.context;
|
|
||||||
|
|
||||||
return clone;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default part;
|
export default part;
|
||||||
|
|
|
@ -40,8 +40,9 @@ path.prototype.close = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Adds an attribute. This is here to make this call chainable in assignment */
|
/** Adds an attribute. This is here to make this call chainable in assignment */
|
||||||
path.prototype.attr = function(name, value) {
|
path.prototype.attr = function(name, value, overwrite = false) {
|
||||||
this.attributes.add(name, value);
|
if (overwrite) this.attributes.set(name, value);
|
||||||
|
else this.attributes.add(name, value);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
|
@ -115,7 +115,7 @@ pattern.prototype.pack = function() {
|
||||||
if (part.render) {
|
if (part.render) {
|
||||||
part.stack();
|
part.stack();
|
||||||
bins.push({
|
bins.push({
|
||||||
id: part.id,
|
id: key,
|
||||||
width: part.bottomRight.x - part.topLeft.x,
|
width: part.bottomRight.x - part.topLeft.x,
|
||||||
height: part.bottomRight.y - part.topLeft.y
|
height: part.bottomRight.y - part.topLeft.y
|
||||||
});
|
});
|
||||||
|
|
|
@ -18,8 +18,9 @@ point.prototype.deg2rad = function(degrees) {
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Adds an attribute. This is here to make this call chainable in assignment */
|
/** Adds an attribute. This is here to make this call chainable in assignment */
|
||||||
point.prototype.attr = function(name, value) {
|
point.prototype.attr = function(name, value, overwrite = false) {
|
||||||
this.attributes.add(name, value);
|
if (overwrite) this.attributes.set(name, value);
|
||||||
|
else this.attributes.add(name, value);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
|
@ -54,7 +54,7 @@ svg.prototype.render = function(pattern) {
|
||||||
for (let partId in pattern.parts) {
|
for (let partId in pattern.parts) {
|
||||||
let part = pattern.parts[partId];
|
let part = pattern.parts[partId];
|
||||||
if (part.render) {
|
if (part.render) {
|
||||||
this.svg += this.openGroup(part.id, part.attributes);
|
this.svg += this.openGroup(this.getUid(), part.attributes);
|
||||||
this.svg += this.renderPart(part);
|
this.svg += this.renderPart(part);
|
||||||
this.svg += this.closeGroup();
|
this.svg += this.closeGroup();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue