ergogen/src/points.js

170 lines
4.7 KiB
JavaScript
Raw Normal View History

2020-05-28 22:18:41 +02:00
const m = require('makerjs')
2020-05-31 20:33:23 +02:00
const u = require('./utils')
2020-06-26 21:00:10 +02:00
const Point = require('./point')
const extend_pair = exports._extend_pair = (to, from) => {
const to_type = u.type(to)
const from_type = u.type(from)
if (!from && ['array', 'object'].includes(to_type)) return to
if (to_type != from_type) return from
if (from_type == 'object') {
const res = {}
for (const key of Object.keys(from)) {
res[key] = extend_pair(to[key], from[key])
}
return res
} else if (from_type == 'array') {
const res = u.deepcopy(to)
for (const [i, val] of from.entries()) {
res[i] = extend_pair(res[i], val)
}
return res
} else return from
}
2020-05-31 20:33:23 +02:00
2020-06-26 21:00:10 +02:00
const extend = exports._extend = (...args) => {
let res = args[0]
for (const arg of args) {
if (res == arg) continue
res = extend_pair(res, arg)
}
return res
}
const push_rotation = exports._push_rotation = (list, angle, origin) => {
2020-05-29 22:35:49 +02:00
let candidate = origin
for (const r of list) {
candidate = m.point.rotate(candidate, r.angle, r.origin)
}
list.push({
angle: angle,
origin: candidate
})
}
2020-06-26 21:00:10 +02:00
const render_zone = exports._render_zone = (cols, rows, zone_wide_key, anchor) => {
2020-05-29 22:35:49 +02:00
2020-05-28 22:18:41 +02:00
const points = {}
2020-05-30 15:17:53 +02:00
const rotations = []
// transferring the anchor rotation to "real" rotations
rotations.push({
2020-05-29 22:35:49 +02:00
angle: anchor.r,
2020-05-30 15:17:53 +02:00
origin: anchor.p
})
2020-06-26 21:00:10 +02:00
for (const [colname, col] of Object.entries(cols)) {
2020-05-29 22:35:49 +02:00
2020-05-30 15:17:53 +02:00
anchor.y += col.stagger || 0
const col_anchor = anchor.clone()
// clear potential rotations, as they will get re-applied anyway
// and we don't want to apply them twice...
col_anchor.r = 0
2020-06-03 22:16:45 +02:00
// combine row data from zone-wide defs and col-specific defs
2020-06-26 21:00:10 +02:00
const col_specific_rows = col.rows || {}
const zone_wide_rows = rows || {}
const actual_rows = col_specific_rows || zone_wide_rows
// get key config through the 4-level extension
const keys = []
for (const row of Object.keys(actual_rows)) {
const key = extend(zone_wide_key, col.key || {}, zone_wide_rows[row] || {}, col_specific_rows[row] || {})
key.col = col
key.row = row
key.name = `${colname}_${row}`
keys.push(key)
2020-06-03 22:16:45 +02:00
}
2020-06-26 21:00:10 +02:00
// lay out keys
for (const key of keys) {
2020-05-30 15:17:53 +02:00
let point = col_anchor.clone()
for (const r of rotations) {
point.rotate(r.angle, r.origin)
}
2020-06-26 21:00:10 +02:00
if (key.rotate) {
point.rotate(key.rotate, point.add(key.origin || [0, 0]).p)
}
point.meta = key
points[key.name] = point
2020-05-29 22:35:49 +02:00
2020-06-26 21:00:10 +02:00
col_anchor.y += key.padding || 19
2020-05-29 22:35:49 +02:00
}
2020-06-26 21:00:10 +02:00
// apply col-level rotation for the next columns
2020-05-30 15:17:53 +02:00
if (col.rotate) {
push_rotation(
rotations,
col.rotate,
anchor.add(col.origin || [0, 0]).p
2020-05-29 22:35:49 +02:00
)
2020-05-28 22:18:41 +02:00
}
2020-05-29 22:35:49 +02:00
2020-06-26 21:00:10 +02:00
anchor.x += col.spread || 19
2020-05-30 15:17:53 +02:00
}
2020-05-29 22:35:49 +02:00
2020-05-30 15:17:53 +02:00
return points
}
2020-05-29 22:35:49 +02:00
2020-06-26 21:00:10 +02:00
const anchor = exports._anchor = (raw, points={}) => {
2020-05-30 15:17:53 +02:00
let a = new Point()
if (raw) {
if (raw.ref && points[raw.ref]) {
a = points[raw.ref].clone()
2020-05-28 22:18:41 +02:00
}
2020-05-30 15:17:53 +02:00
if (raw.shift) {
2020-06-26 21:00:10 +02:00
a.x += raw.shift[0] || 0
a.y += raw.shift[1] || 0
2020-05-30 15:17:53 +02:00
}
2020-06-26 21:00:10 +02:00
a.r += raw.rotate || 0
2020-05-28 22:18:41 +02:00
}
2020-05-30 15:17:53 +02:00
return a
2020-05-29 22:35:49 +02:00
}
2020-05-28 22:18:41 +02:00
2020-05-30 15:17:53 +02:00
exports.parse = (config) => {
2020-05-29 22:35:49 +02:00
2020-05-30 15:17:53 +02:00
let points = {}
2020-05-29 22:35:49 +02:00
2020-05-30 15:17:53 +02:00
for (const zone of Object.values(config.zones)) {
points = Object.assign(points, render_zone(
zone.columns || [],
zone.rows || [{name: 'default'}],
2020-06-26 21:00:10 +02:00
zone.key || {},
anchor(zone.anchor, points)
2020-05-30 15:17:53 +02:00
))
}
2020-05-29 22:35:49 +02:00
2020-06-26 21:00:10 +02:00
if (config.rotate) {
2020-05-30 15:17:53 +02:00
for (const p of Object.values(points)) {
2020-06-26 21:00:10 +02:00
p.rotate(config.rotate)
2020-05-28 22:18:41 +02:00
}
2020-05-29 22:35:49 +02:00
}
2020-05-28 22:18:41 +02:00
2020-05-30 15:17:53 +02:00
if (config.mirror) {
2020-06-26 21:00:10 +02:00
let axis = config.mirror.axis
if (!axis) {
axis = anchor(config.mirror, points).x
axis += (config.mirror.distance || 0) / 2
}
2020-05-30 15:17:53 +02:00
const mirrored_points = {}
for (const [name, p] of Object.entries(points)) {
2020-06-26 21:00:10 +02:00
if (p.meta.asym == 'left') continue
2020-06-03 22:16:45 +02:00
const mp = p.clone().mirror(axis)
2020-05-30 15:17:53 +02:00
mp.meta.mirrored = true
2020-05-31 20:33:23 +02:00
delete mp.meta.asym
mirrored_points[`mirror_${name}`] = mp
2020-06-26 21:00:10 +02:00
if (p.meta.asym == 'right') {
p.meta.skip = true
2020-05-31 20:33:23 +02:00
}
2020-05-30 15:17:53 +02:00
}
Object.assign(points, mirrored_points)
2020-05-28 22:18:41 +02:00
}
2020-05-31 20:33:23 +02:00
2020-05-30 15:17:53 +02:00
const filtered = {}
for (const [k, p] of Object.entries(points)) {
2020-06-26 21:00:10 +02:00
if (p.meta.skip) continue
2020-05-30 15:17:53 +02:00
filtered[k] = p
}
2020-05-28 22:18:41 +02:00
2020-05-30 15:17:53 +02:00
return filtered
2020-05-28 22:18:41 +02:00
}