1
0
Fork 0
freesewing/packages/core/src/part.js

192 lines
5.6 KiB
JavaScript
Raw Normal View History

2019-08-03 15:03:33 +02:00
import * as utils from './utils'
import Point from './point'
import Path from './path'
import Snippet from './snippet'
import Attributes from './attributes'
import Hooks from './hooks'
2018-07-23 11:12:06 +00:00
function Part() {
2019-08-03 15:03:33 +02:00
this.attributes = new Attributes()
this.points = {}
this.paths = {}
this.snippets = {}
this.freeId = 0
this.topLeft = false
this.bottomRight = false
this.width = false
this.height = false
this.render = true
this.utils = utils
2018-07-23 11:12:06 +00:00
// Constructors so macros can create objects
2019-08-03 15:03:33 +02:00
this.Point = Point
this.Path = Path
this.Snippet = Snippet
2019-08-03 15:03:33 +02:00
this.hooks = new Hooks() // Hooks container
2019-08-03 15:03:33 +02:00
return this
2018-07-23 20:14:32 +02:00
}
2018-07-23 11:12:06 +00:00
Part.prototype.macroClosure = function (args) {
2019-08-03 15:03:33 +02:00
let self = this
let method = function (key, args) {
2019-08-03 15:03:33 +02:00
let macro = utils.macroName(key)
if (typeof self[macro] === 'function') self[macro](args)
2019-08-03 15:03:33 +02:00
}
2019-08-03 15:03:33 +02:00
return method
}
2018-07-23 11:12:06 +00:00
Part.prototype.runHooks = function (hookName, data = false) {
2019-08-03 15:03:33 +02:00
if (data === false) data = this
let hooks = this.hooks[hookName]
if (hooks && hooks.length > 0) {
for (let hook of hooks) {
2019-08-03 15:03:33 +02:00
hook.method(data, hook.data)
}
}
2019-08-03 15:03:33 +02:00
}
/** Returns an unused ID */
Part.prototype.getId = function () {
2019-08-03 15:03:33 +02:00
this.freeId += 1
2019-08-03 15:03:33 +02:00
return '' + this.freeId
}
2018-08-01 18:18:29 +02:00
/** Returns a value formatted for units provided in settings */
Part.prototype.unitsClosure = function (value) {
2019-08-03 15:03:33 +02:00
let self = this
let method = function (value) {
2019-08-03 15:03:33 +02:00
return utils.units(value, self.context.settings.units)
}
2019-08-03 15:03:33 +02:00
return method
}
2018-08-01 18:18:29 +02:00
/** Calculates the part's bounding box and sets it */
Part.prototype.boundary = function () {
2019-08-03 15:03:33 +02:00
if (this.topLeft) return this // Cached
2018-08-01 18:18:29 +02:00
2019-08-03 15:03:33 +02:00
let topLeft = new Point(Infinity, Infinity)
let bottomRight = new Point(-Infinity, -Infinity)
2018-08-01 18:18:29 +02:00
for (let key in this.paths) {
2019-08-03 15:03:33 +02:00
let path = this.paths[key].boundary()
2018-08-01 18:18:29 +02:00
if (path.render) {
2019-08-03 15:03:33 +02:00
if (path.topLeft.x < topLeft.x) topLeft.x = path.topLeft.x
if (path.topLeft.y < topLeft.y) topLeft.y = path.topLeft.y
if (path.bottomRight.x > bottomRight.x) bottomRight.x = path.bottomRight.x
if (path.bottomRight.y > bottomRight.y) bottomRight.y = path.bottomRight.y
2018-08-01 18:18:29 +02:00
}
}
for (let key in this.points) {
2019-08-03 15:03:33 +02:00
let point = this.points[key]
let radius = point.attributes.get('data-circle')
if (radius) {
2019-08-03 15:03:33 +02:00
radius = parseFloat(radius)
if (point.x - radius < topLeft.x) topLeft.x = point.x - radius
if (point.y - radius < topLeft.y) topLeft.y = point.y - radius
if (point.x + radius > bottomRight.x) bottomRight.x = point.x + radius
if (point.y + radius > bottomRight.y) bottomRight.y = point.y + radius
}
}
// Fix infinity if part has no paths
2019-08-03 15:03:33 +02:00
if (topLeft.x === Infinity) topLeft.x = 0
if (topLeft.y === Infinity) topLeft.y = 0
if (bottomRight.x === -Infinity) bottomRight.x = 0
if (bottomRight.y === -Infinity) bottomRight.y = 0
// Add margin
2019-08-03 15:03:33 +02:00
let margin = this.context.settings.margin
if (this.context.settings.paperless && margin < 10) margin = 10
this.topLeft = new Point(topLeft.x - margin, topLeft.y - margin)
this.bottomRight = new Point(bottomRight.x + margin, bottomRight.y + margin)
this.width = this.bottomRight.x - this.topLeft.x
this.height = this.bottomRight.y - this.topLeft.y
return this
}
2018-08-01 18:18:29 +02:00
/** Stacks part so that its top left corner is in (0,0) */
Part.prototype.stack = function () {
2019-08-03 15:03:33 +02:00
if (this.topLeft !== false) return this
else this.boundary()
if (this.topLeft.x == 0 && this.topLeft.y == 0) return this
else this.attr('transform', `translate(${this.topLeft.x * -1}, ${this.topLeft.y * -1})`)
return this
}
2018-08-01 18:18:29 +02:00
/** Adds an attribute. This is here to make this call chainable in assignment */
Part.prototype.attr = function (name, value, overwrite = false) {
2019-08-03 15:03:33 +02:00
if (overwrite) this.attributes.set(name, value)
else this.attributes.add(name, value)
2018-08-01 18:18:29 +02:00
2019-08-03 15:03:33 +02:00
return this
}
2018-08-01 18:18:29 +02:00
/** Copies point/path/snippet data from part orig into this */
Part.prototype.inject = function (orig) {
const findBasePoint = (p) => {
for (let i in orig.points) {
2019-08-03 15:03:33 +02:00
if (orig.points[i] === p) return i
}
2019-08-03 15:03:33 +02:00
return false
}
2019-08-03 15:03:33 +02:00
for (let i in orig.points) this.points[i] = orig.points[i].clone()
for (let i in orig.paths) {
2019-08-03 15:03:33 +02:00
this.paths[i] = orig.paths[i].clone()
// Keep link between points and path ops where possible
for (let j in orig.paths[i].ops) {
2019-08-03 15:03:33 +02:00
let op = orig.paths[i].ops[j]
if (op.type !== 'close') {
let toPoint = findBasePoint(op.to)
if (toPoint) this.paths[i].ops[j].to = this.points[toPoint]
}
2019-08-03 15:03:33 +02:00
if (op.type === 'curve') {
let cp1Point = findBasePoint(op.cp1)
if (cp1Point) this.paths[i].ops[j].cp1 = this.points[cp1Point]
let cp2Point = findBasePoint(op.cp2)
if (cp2Point) this.paths[i].ops[j].cp2 = this.points[cp2Point]
}
}
}
for (let i in orig.snippets) {
2019-08-03 15:03:33 +02:00
this.snippets[i] = orig.snippets[i].clone()
}
2019-08-03 15:03:33 +02:00
return this
}
Part.prototype.units = function (input) {
2019-08-03 15:03:33 +02:00
return utils.units(input, this.context.settings.units)
}
/** Returns an object with shorthand access for pattern design */
Part.prototype.shorthand = function () {
2019-08-03 15:03:33 +02:00
let complete = this.context.settings.complete ? true : false
let paperless = this.context.settings.paperless === true ? true : false
let sa = this.context.settings.complete ? this.context.settings.sa || 0 : 0
return {
sa,
measurements: this.context.settings.measurements || {},
options: this.context.settings.options || {},
store: this.context.store,
points: this.points || {},
paths: this.paths || {},
snippets: this.snippets || {},
macro: this.macroClosure(),
units: this.unitsClosure(),
utils: utils,
Point: this.Point,
Path: this.Path,
Snippet: this.Snippet,
complete,
paperless
2019-08-03 15:03:33 +02:00
}
}
2019-08-03 15:03:33 +02:00
export default Part