chore: Further tweaks to pattern events
This commit is contained in:
parent
53e8274624
commit
4ac64e05cb
11 changed files with 2558 additions and 955 deletions
|
@ -75,12 +75,20 @@ Part.prototype.boundary = function () {
|
|||
let topLeft = new Point(Infinity, Infinity)
|
||||
let bottomRight = new Point(-Infinity, -Infinity)
|
||||
for (let key in this.paths) {
|
||||
let path = this.paths[key].boundary()
|
||||
if (path.render) {
|
||||
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
|
||||
try {
|
||||
let path = this.paths[key].boundary()
|
||||
if (path.render) {
|
||||
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
|
||||
}
|
||||
} catch (err) {
|
||||
this.context.raise.error(`Could not calculate boundary of \`paths.${key}\``)
|
||||
this.context.raise.debug(
|
||||
`Since \`paths.${key}\` has no boundary, neither does \`parts.${this.name}\`. Ejecting part`
|
||||
)
|
||||
return false
|
||||
}
|
||||
}
|
||||
for (let key in this.points) {
|
||||
|
@ -190,19 +198,19 @@ Part.prototype.shorthand = function () {
|
|||
|
||||
// Wrap the Point constructor so objects can raise events
|
||||
shorthand.Point = function (x, y) {
|
||||
Point.apply(this, [x, y, self.context.raise])
|
||||
Point.apply(this, [x, y, true])
|
||||
this.raise = self.context.raise
|
||||
}
|
||||
shorthand.Point.prototype = Object.create(Point.prototype)
|
||||
// Wrap the Path constructor so objects can raise events
|
||||
shorthand.Path = function () {
|
||||
Path.apply(this, [self.context.raise])
|
||||
Path.apply(this, [true])
|
||||
this.raise = self.context.raise
|
||||
}
|
||||
shorthand.Path.prototype = Object.create(Path.prototype)
|
||||
// Wrap the Snippet constructor so objects can raise events
|
||||
shorthand.Snippet = function (def, anchor) {
|
||||
Snippet.apply(this, [def, anchor, self.context.raise])
|
||||
Snippet.apply(this, [def, anchor, true])
|
||||
Snippet.apply(this, arguments)
|
||||
shorthand.raise = self.context.raise
|
||||
}
|
||||
|
@ -227,8 +235,12 @@ Part.prototype.shorthand = function () {
|
|||
self.context.raise.warning(
|
||||
`\`points.${name}\` was set with a \`y\` parameter that is not a \`number\``
|
||||
)
|
||||
value.name = name
|
||||
return (self.points[name] = value)
|
||||
try {
|
||||
value.name = name
|
||||
} catch (err) {
|
||||
self.context.raise.warning(`Could not set \`name\` property on \`points.${name}\``)
|
||||
}
|
||||
return (self.points[name] = value) || true
|
||||
}
|
||||
}
|
||||
shorthand.points = new Proxy(this.points || {}, pointsProxy)
|
||||
|
@ -243,8 +255,12 @@ Part.prototype.shorthand = function () {
|
|||
self.context.raise.warning(
|
||||
`\`paths.${name}\` was set with a value that is not a \`Path\` object`
|
||||
)
|
||||
value.name = name
|
||||
return (self.paths[name] = value)
|
||||
try {
|
||||
value.name = name
|
||||
} catch (err) {
|
||||
self.context.raise.warning(`Could not set \`name\` property on \`paths.${name}\``)
|
||||
}
|
||||
return (self.paths[name] = value) || true
|
||||
}
|
||||
}
|
||||
shorthand.paths = new Proxy(this.paths || {}, pathsProxy)
|
||||
|
@ -267,8 +283,12 @@ Part.prototype.shorthand = function () {
|
|||
self.context.raise.warning(
|
||||
`\`snippets.${name}\` was set with an \`anchor\` parameter that is not a \`Point\``
|
||||
)
|
||||
value.name = name
|
||||
return (self.snippets[name] = value)
|
||||
try {
|
||||
value.name = name
|
||||
} catch (err) {
|
||||
self.context.raise.warning(`Could not set \`name\` property on \`snippets.${name}\``)
|
||||
}
|
||||
return (self.snippets[name] = value) || true
|
||||
}
|
||||
}
|
||||
shorthand.snippets = new Proxy(this.snippets || {}, snippetsProxy)
|
||||
|
@ -282,7 +302,7 @@ Part.prototype.shorthand = function () {
|
|||
return Reflect.get(...arguments)
|
||||
},
|
||||
set: (measurements, name, value) => {
|
||||
return (self.measurements[name] = value)
|
||||
return (self.measurements[name] = value) || true
|
||||
}
|
||||
}
|
||||
shorthand.measurements = new Proxy(this.context.settings.measurements || {}, measurementsProxy)
|
||||
|
@ -294,7 +314,7 @@ Part.prototype.shorthand = function () {
|
|||
return Reflect.get(...arguments)
|
||||
},
|
||||
set: (options, name, value) => {
|
||||
return (self.options[name] = value)
|
||||
return (self.options[name] = value) || true
|
||||
}
|
||||
}
|
||||
shorthand.options = new Proxy(this.context.settings.options || {}, optionsProxy)
|
||||
|
|
|
@ -11,8 +11,8 @@ import {
|
|||
round
|
||||
} from './utils'
|
||||
|
||||
function Path(raise = false) {
|
||||
this.debug = raise ? true : false
|
||||
function Path(debug = false) {
|
||||
this.debug = debug
|
||||
this.render = true
|
||||
this.topLeft = false
|
||||
this.bottomRight = false
|
||||
|
@ -24,7 +24,7 @@ function Path(raise = false) {
|
|||
Path.prototype.setRender = function (render = true) {
|
||||
if (render) this.render = true
|
||||
else this.render = false
|
||||
if (this.debug) this.raise('Setting `Path.render` to ' + render ? '`true`' : '`false`')
|
||||
if (this.debug) this.raise.debug('Setting `Path.render` to ' + render ? '`true`' : '`false`')
|
||||
|
||||
return this
|
||||
}
|
||||
|
@ -387,7 +387,9 @@ function joinPaths(paths, closed = false) {
|
|||
// We're using sitsRoughlyOn here to avoid miniscule line segments
|
||||
if (current && !op.to.sitsRoughlyOn(current)) joint.line(op.to)
|
||||
} else {
|
||||
throw new Error('Cannot join a closed paths with another')
|
||||
let err = 'Cannot join a closed path with another'
|
||||
this.raise.error(err)
|
||||
throw new Error(err)
|
||||
}
|
||||
if (op.to) current = op.to
|
||||
}
|
||||
|
|
|
@ -31,7 +31,6 @@ export default function Pattern(config = { options: {} }) {
|
|||
},
|
||||
debug: function (data) {
|
||||
events.debug.push(data)
|
||||
console.log(data)
|
||||
}
|
||||
}
|
||||
this.raise.debug(
|
||||
|
@ -80,7 +79,11 @@ export default function Pattern(config = { options: {} }) {
|
|||
else if (typeof option.count !== 'undefined') this.settings.options[i] = option.count
|
||||
else if (typeof option.bool !== 'undefined') this.settings.options[i] = option.bool
|
||||
else if (typeof option.dflt !== 'undefined') this.settings.options[i] = option.dflt
|
||||
else throw new Error('Unknown option type: ' + JSON.stringify(option))
|
||||
else {
|
||||
let err = 'Unknown option type: ' + JSON.stringify(option)
|
||||
this.raise.error(err)
|
||||
throw new Error(err)
|
||||
}
|
||||
} else {
|
||||
this.settings.options[i] = option
|
||||
}
|
||||
|
@ -129,11 +132,7 @@ Pattern.prototype.apply = function (settings) {
|
|||
}
|
||||
} else this.settings[key] = settings[key]
|
||||
}
|
||||
if (this.settings.debug) this.raise.debug('Debug enabled')
|
||||
else {
|
||||
this.raise.debug('Debug disabled')
|
||||
this.debug = false
|
||||
}
|
||||
if (!this.settings.debug) this.debug = false
|
||||
|
||||
return this
|
||||
}
|
||||
|
@ -166,7 +165,14 @@ Pattern.prototype.draft = function () {
|
|||
this.raise.debug(
|
||||
`Injecting part \`${this.config.inject[partName]}\` into part \`${partName}\``
|
||||
)
|
||||
this.parts[partName].inject(this.parts[this.config.inject[partName]])
|
||||
try {
|
||||
this.parts[partName].inject(this.parts[this.config.inject[partName]])
|
||||
} catch (err) {
|
||||
this.raise.error([
|
||||
`Could not inject part \`${this.config.inject[partName]}\` into part \`${partName}\``,
|
||||
err
|
||||
])
|
||||
}
|
||||
}
|
||||
if (this.needs(partName)) {
|
||||
let method = 'draft' + capitalize(partName)
|
||||
|
@ -174,17 +180,22 @@ Pattern.prototype.draft = function () {
|
|||
this.raise.error(`Method \`pattern.${method}\` is callable`)
|
||||
throw new Error('Method "' + method + '" on pattern object is not callable')
|
||||
}
|
||||
this.parts[partName] = this[method](this.parts[partName])
|
||||
try {
|
||||
this.parts[partName] = this[method](this.parts[partName])
|
||||
} catch (err) {
|
||||
this.raise.error([`Unable to draft part \`${partName}\``, err])
|
||||
}
|
||||
if (typeof this.parts[partName] === 'undefined') {
|
||||
this.raise.error(
|
||||
`Result of \`pattern.${method}\` was \`undefined\`. Did you forget to return the \`Part\` object?`
|
||||
)
|
||||
throw new Error(
|
||||
'Result of ' + method + '() was undefined. Did you forget to return the Part object?'
|
||||
)
|
||||
}
|
||||
this.parts[partName].render =
|
||||
this.parts[partName].render === false ? false : this.wants(partName)
|
||||
try {
|
||||
this.parts[partName].render =
|
||||
this.parts[partName].render === false ? false : this.wants(partName)
|
||||
} catch (err) {
|
||||
this.raise.error([`Unable to set \`render\` property on part \`${partName}\``, err])
|
||||
}
|
||||
} else {
|
||||
if (this.debug)
|
||||
this.raise.debug(
|
||||
|
@ -323,7 +334,7 @@ Pattern.prototype.sampleMeasurement = function (measurementName) {
|
|||
let parts = this.sampleParts()
|
||||
let val = this.settings.measurements[measurementName]
|
||||
if (val === undefined)
|
||||
throw new Error('Cannot sample a measurement that is undefined: ' + measurementName)
|
||||
this.raise.error(`Cannot sample measurement \`${measurementName}\` because it's \`undefined\``)
|
||||
let step = val / 50
|
||||
val = val * 0.9
|
||||
for (let run = 1; run < 11; run++) {
|
||||
|
@ -410,6 +421,10 @@ Pattern.prototype.macro = function (key, method) {
|
|||
|
||||
/** Packs parts in a 2D space and sets pattern size */
|
||||
Pattern.prototype.pack = function () {
|
||||
if (this.events.error.length > 0) {
|
||||
this.raise.warning(`One or more errors occured. Not packing pattern parts`)
|
||||
return this
|
||||
}
|
||||
let bins = []
|
||||
for (let key in this.parts) {
|
||||
let part = this.parts[key]
|
||||
|
@ -531,7 +546,10 @@ Pattern.prototype.resolveDependencies = function (graph = this.config.dependenci
|
|||
else if (Array.isArray(this.config.dependencies[i])) {
|
||||
if (this.config.dependencies[i].indexOf(dependency) === -1)
|
||||
this.config.dependencies[i].push(dependency)
|
||||
} else throw new Error('Part dependencies should be a string or an array of strings')
|
||||
} else {
|
||||
this.raise.error('Part dependencies should be a string or an array of strings')
|
||||
throw new Error('Part dependencies should be a string or an array of strings')
|
||||
}
|
||||
}
|
||||
// Parts both in the parts and dependencies array trip up the dependency resolver
|
||||
if (Array.isArray(this.config.parts)) {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import Attributes from './attributes'
|
||||
import { round } from './utils'
|
||||
|
||||
function Point(x, y, raise = false) {
|
||||
this.debug = raise ? true : false
|
||||
function Point(x, y, debug = false) {
|
||||
this.debug = debug
|
||||
if (this.debug) {
|
||||
if (typeof x !== 'number') raise.warning('Called `new Point(x,y)` but `x` is not a number')
|
||||
if (typeof y !== 'number') raise.warning('Called `new Point(x,y)` but `y` is not a number')
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import Attributes from './attributes'
|
||||
import Point from './point'
|
||||
|
||||
function Snippet(def, anchor, raise = false) {
|
||||
this.debug = raise ? true : false
|
||||
function Snippet(def, anchor, debug = false) {
|
||||
this.debug = debug
|
||||
if (this.debug) {
|
||||
if (typeof def !== 'string')
|
||||
raise.warning('Called `new Snippet(def, anchor)` but `def` is not a string')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue