pointvec/desktop/sources/scripts/tool.js

378 lines
11 KiB
JavaScript
Raw Normal View History

2018-10-03 19:27:40 -04:00
'use strict'
2018-08-28 00:34:17 -04:00
2019-01-08 22:49:34 -05:00
function Tool (dotgrid) {
2018-10-03 19:27:40 -04:00
this.index = 0
2019-04-21 20:28:31 -04:00
this.settings = { size: { width: 600, height: 300 }, crest: false }
2018-10-03 19:27:40 -04:00
this.layers = [[], [], []]
2018-03-06 17:37:01 -05:00
this.styles = [
2019-01-08 22:12:18 -05:00
{ thickness: 10, strokeLinecap: 'round', strokeLinejoin: 'round', color: '#f00', fill: 'none', mirror_style: 0, transform: 'rotate(45)' },
{ thickness: 10, strokeLinecap: 'round', strokeLinejoin: 'round', color: '#0f0', fill: 'none', mirror_style: 0, transform: 'rotate(45)' },
{ thickness: 10, strokeLinecap: 'round', strokeLinejoin: 'round', color: '#00f', fill: 'none', mirror_style: 0, transform: 'rotate(45)' }
2018-10-03 19:27:40 -04:00
]
this.vertices = []
2018-11-08 17:41:41 -05:00
this.reqs = { line: 2, arc_c: 2, arc_r: 2, arc_c_full: 2, arc_r_full: 2, bezier: 3, close: 0 }
2018-10-03 19:27:40 -04:00
this.start = function () {
2019-01-08 22:49:34 -05:00
this.styles[0].color = dotgrid.theme.active.f_high
this.styles[1].color = dotgrid.theme.active.f_med
this.styles[2].color = dotgrid.theme.active.f_low
2018-03-06 17:57:52 -05:00
}
2019-01-07 17:34:57 -05:00
this.erase = function () {
this.layers = [[], [], []]
}
2018-10-03 19:27:40 -04:00
this.reset = function () {
2019-01-10 00:14:01 -05:00
this.settings.crest = false
2018-12-14 15:01:16 -05:00
this.styles[0].mirror_style = 0
this.styles[1].mirror_style = 0
this.styles[2].mirror_style = 0
this.styles[0].fill = 'none'
this.styles[1].fill = 'none'
this.styles[2].fill = 'none'
2019-01-07 17:34:57 -05:00
this.erase()
2018-10-03 19:27:40 -04:00
this.vertices = []
this.index = 0
2018-02-06 14:16:01 -05:00
}
2018-10-03 19:27:40 -04:00
this.clear = function () {
this.vertices = []
2019-01-08 22:49:34 -05:00
dotgrid.renderer.update()
dotgrid.interface.update(true)
2018-03-06 17:26:18 -05:00
}
2018-10-03 19:27:40 -04:00
this.undo = function () {
2019-01-08 22:49:34 -05:00
this.layers = dotgrid.history.prev()
dotgrid.renderer.update()
dotgrid.interface.update(true)
2018-03-06 22:08:34 -05:00
}
2018-10-03 19:27:40 -04:00
this.redo = function () {
2019-01-08 22:49:34 -05:00
this.layers = dotgrid.history.next()
dotgrid.renderer.update()
dotgrid.interface.update(true)
2018-03-06 22:08:34 -05:00
}
2018-10-03 19:27:40 -04:00
this.length = function () {
2018-08-03 23:55:08 -04:00
return this.layers[0].length + this.layers[1].length + this.layers[2].length
}
2018-03-06 22:08:34 -05:00
// I/O
2018-10-03 19:27:40 -04:00
this.export = function (target = { settings: this.settings, layers: this.layers, styles: this.styles }) {
return JSON.stringify(copy(target), null, 2)
2018-03-06 22:08:34 -05:00
}
2018-10-03 19:27:40 -04:00
this.import = function (layer) {
2018-03-06 22:08:34 -05:00
this.layers[this.index] = this.layers[this.index].concat(layer)
2019-01-08 22:49:34 -05:00
dotgrid.history.push(this.layers)
2018-10-03 19:27:40 -04:00
this.clear()
2019-01-08 22:49:34 -05:00
dotgrid.renderer.update()
dotgrid.interface.update(true)
2018-03-06 22:08:34 -05:00
}
2018-05-18 00:55:19 -04:00
2018-10-03 19:27:40 -04:00
this.replace = function (dot) {
2019-04-21 19:36:12 -04:00
if (!dot.layers || dot.layers.length !== 3) { console.warn('Incompatible version'); return }
2018-10-03 19:27:40 -04:00
if (dot.settings.width && dot.settings.height) {
dot.settings.size = { width: dot.settings.width, height: dot.settings.height }
2018-05-18 00:55:19 -04:00
}
2018-05-07 00:51:58 -04:00
2018-10-03 19:27:40 -04:00
this.layers = dot.layers
this.styles = dot.styles
this.settings = dot.settings
2018-05-07 00:51:58 -04:00
2018-10-03 19:27:40 -04:00
this.clear()
2019-04-21 20:28:31 -04:00
dotgrid.fitSize()
2019-01-08 22:49:34 -05:00
dotgrid.renderer.update()
dotgrid.interface.update(true)
dotgrid.history.push(this.layers)
2018-02-05 20:14:23 -05:00
}
2018-03-06 22:08:34 -05:00
// EDIT
2018-10-03 19:27:40 -04:00
2019-01-08 22:49:34 -05:00
this.removeSegment = function () {
2018-10-03 19:27:40 -04:00
if (this.vertices.length > 0) { this.clear(); return }
this.layer().pop()
this.clear()
2019-01-08 22:49:34 -05:00
dotgrid.renderer.update()
dotgrid.interface.update(true)
}
2019-01-08 22:49:34 -05:00
this.removeSegmentsAt = function (pos) {
for (const segmentId in this.layer()) {
let segment = this.layer()[segmentId]
for (const vertexId in segment.vertices) {
let vertex = segment.vertices[vertexId]
2019-04-21 19:36:12 -04:00
if (Math.abs(pos.x) === Math.abs(vertex.x) && Math.abs(pos.y) === Math.abs(vertex.y)) {
2019-01-08 22:49:34 -05:00
segment.vertices.splice(vertexId, 1)
2018-02-05 23:35:25 -05:00
}
}
2018-10-03 19:27:40 -04:00
if (segment.vertices.length < 2) {
2019-01-08 22:49:34 -05:00
this.layers[this.index].splice(segmentId, 1)
2018-02-16 20:42:52 -05:00
}
2018-02-05 23:35:25 -05:00
}
2018-10-03 19:27:40 -04:00
this.clear()
2019-01-08 22:49:34 -05:00
dotgrid.renderer.update()
dotgrid.interface.update(true)
2018-02-05 23:19:34 -05:00
}
2019-01-08 22:49:34 -05:00
this.selectSegmentAt = function (pos, source = this.layer()) {
2018-11-21 18:14:57 -05:00
let target_segment = null
2019-01-08 22:49:34 -05:00
for (const segmentId in source) {
let segment = source[segmentId]
for (const vertexId in segment.vertices) {
let vertex = segment.vertices[vertexId]
2019-04-21 19:36:12 -04:00
if (vertex.x === Math.abs(pos.x) && vertex.y === Math.abs(pos.y)) {
2018-11-21 18:14:57 -05:00
return segment
}
}
}
return null
}
2019-01-08 22:49:34 -05:00
this.addVertex = function (pos) {
2018-10-03 19:27:40 -04:00
pos = { x: Math.abs(pos.x), y: Math.abs(pos.y) }
this.vertices.push(pos)
2019-01-08 22:49:34 -05:00
dotgrid.interface.update(true)
}
2019-01-08 22:49:34 -05:00
this.vertexAt = function (pos) {
for (const segmentId in this.layer()) {
let segment = this.layer()[segmentId]
for (const vertexId in segment.vertices) {
let vertex = segment.vertices[vertexId]
2019-04-21 19:36:12 -04:00
if (vertex.x === Math.abs(pos.x) && vertex.y === Math.abs(pos.y)) {
2018-10-03 19:27:40 -04:00
return vertex
2018-02-06 13:42:34 -05:00
}
}
}
2018-10-03 19:27:40 -04:00
return null
2018-02-06 13:42:34 -05:00
}
2019-01-12 19:32:03 -05:00
this.addSegment = function (type, vertices, index = this.index) {
2019-01-13 21:39:07 -05:00
let append_target = this.canAppend({ type: type, vertices: vertices }, index)
2018-11-22 16:03:33 -05:00
if (append_target) {
2019-01-12 19:32:03 -05:00
this.layer(index)[append_target].vertices = this.layer(index)[append_target].vertices.concat(vertices)
2018-11-22 16:03:33 -05:00
} else {
2019-01-12 19:32:03 -05:00
this.layer(index).push({ type: type, vertices: vertices })
2018-11-22 16:03:33 -05:00
}
}
2018-10-03 19:27:40 -04:00
this.cast = function (type) {
if (!this.layer()) { this.layers[this.index] = [] }
2019-01-08 01:22:14 -05:00
if (!this.canCast(type)) { console.warn('Cannot cast'); return }
2019-01-08 22:49:34 -05:00
this.addSegment(type, this.vertices.slice())
2018-05-10 17:13:01 -04:00
2019-01-08 22:49:34 -05:00
dotgrid.history.push(this.layers)
2018-05-10 17:47:09 -04:00
2018-10-03 19:27:40 -04:00
this.clear()
2019-01-08 22:49:34 -05:00
dotgrid.renderer.update()
dotgrid.interface.update(true)
2018-10-03 19:27:40 -04:00
console.log(`Casted ${type} -> ${this.layer().length} elements`)
}
2018-10-03 19:27:40 -04:00
this.i = { linecap: 0, linejoin: 0, thickness: 5 }
this.toggle = function (type, mod = 1) {
2019-04-21 19:36:12 -04:00
if (type === 'linecap') {
2018-10-03 19:27:40 -04:00
let a = ['butt', 'square', 'round']
this.i.linecap += mod
this.style().strokeLinecap = a[this.i.linecap % a.length]
2019-04-21 19:36:12 -04:00
} else if (type === 'linejoin') {
2018-10-03 19:27:40 -04:00
let a = ['miter', 'round', 'bevel']
this.i.linejoin += mod
this.style().strokeLinejoin = a[this.i.linejoin % a.length]
2019-04-21 19:36:12 -04:00
} else if (type === 'fill') {
this.style().fill = this.style().fill === 'none' ? this.style().color : 'none'
} else if (type === 'thickness') {
2018-10-03 19:27:40 -04:00
this.style().thickness = clamp(this.style().thickness + mod, 1, 100)
2019-04-21 19:36:12 -04:00
} else if (type === 'mirror') {
2018-11-21 16:21:34 -05:00
this.style().mirror_style = this.style().mirror_style > 2 ? 0 : this.style().mirror_style + 1
2018-10-03 19:27:40 -04:00
} else {
console.warn('Unknown', type)
2018-08-02 20:57:36 -04:00
}
2019-01-08 22:49:34 -05:00
dotgrid.interface.update(true)
dotgrid.renderer.update()
2018-08-02 20:53:26 -04:00
}
2019-01-09 17:43:17 -05:00
this.toggleCrest = function () {
this.settings.crest = this.settings.crest !== true
dotgrid.interface.update(true)
dotgrid.renderer.update()
}
2018-10-03 19:27:40 -04:00
this.misc = function (type) {
2019-01-08 22:49:34 -05:00
dotgrid.picker.start()
2018-08-02 20:57:36 -04:00
}
2018-10-03 19:27:40 -04:00
this.source = function (type) {
2019-04-21 19:36:12 -04:00
if (type === 'grid') { dotgrid.renderer.toggle() }
if (type === 'screen') { app.toggleFullscreen() }
2018-08-03 23:55:08 -04:00
2019-04-21 21:25:31 -04:00
if (type === 'open') { dotgrid.source.open() }
if (type === 'save') { dotgrid.source.save() }
if (type === 'render') { dotgrid.source.render() }
if (type === 'export') { dotgrid.source.export() }
2018-08-03 23:55:08 -04:00
}
2019-01-13 21:39:07 -05:00
this.canAppend = function (content, index = this.index) {
for (const id in this.layer(index)) {
let stroke = this.layer(index)[id]
2019-04-21 19:36:12 -04:00
if (stroke.type !== content.type) { continue }
2018-10-03 19:27:40 -04:00
if (!stroke.vertices) { continue }
if (!stroke.vertices[stroke.vertices.length - 1]) { continue }
2019-04-21 19:36:12 -04:00
if (stroke.vertices[stroke.vertices.length - 1].x !== content.vertices[0].x) { continue }
if (stroke.vertices[stroke.vertices.length - 1].y !== content.vertices[0].y) { continue }
2018-10-03 19:27:40 -04:00
return id
2018-02-10 23:00:22 -05:00
}
2018-10-03 19:27:40 -04:00
return false
2018-02-10 23:00:22 -05:00
}
2019-01-08 01:22:14 -05:00
this.canCast = function (type) {
2018-10-03 19:27:40 -04:00
if (!type) { return false }
2018-02-05 22:47:25 -05:00
// Cannot cast close twice
2019-04-21 19:36:12 -04:00
if (type === 'close') {
2018-10-03 19:27:40 -04:00
let prev = this.layer()[this.layer().length - 1]
2019-04-21 19:36:12 -04:00
if (!prev || prev.type === 'close') {
2018-10-03 19:27:40 -04:00
return false
2018-02-05 22:47:25 -05:00
}
}
2019-04-21 19:36:12 -04:00
if (type === 'bezier') {
if (this.vertices.length !== 3 && this.vertices.length !== 5 && this.vertices.length !== 7 && this.vertices.length !== 9) {
2018-10-03 19:27:40 -04:00
return false
2018-05-08 17:33:17 -04:00
}
}
2018-10-03 19:27:40 -04:00
return this.vertices.length >= this.reqs[type]
2018-02-05 22:27:48 -05:00
}
2018-10-03 19:27:40 -04:00
this.paths = function () {
2019-01-08 22:49:34 -05:00
let l1 = new Generator(dotgrid.tool.layers[0], dotgrid.tool.styles[0]).toString({ x: 0, y: 0 }, 1)
let l2 = new Generator(dotgrid.tool.layers[1], dotgrid.tool.styles[1]).toString({ x: 0, y: 0 }, 1)
let l3 = new Generator(dotgrid.tool.layers[2], dotgrid.tool.styles[2]).toString({ x: 0, y: 0 }, 1)
2018-02-06 13:42:34 -05:00
2018-10-03 19:27:40 -04:00
return [l1, l2, l3]
}
2018-10-03 19:27:40 -04:00
this.path = function () {
2019-01-08 22:49:34 -05:00
return new Generator(dotgrid.tool.layer(), dotgrid.tool.style()).toString({ x: 0, y: 0 }, 1)
2018-04-15 22:32:54 -04:00
}
2018-10-03 19:27:40 -04:00
this.translate = function (a, b) {
2019-01-08 22:49:34 -05:00
for (const segmentId in this.layer()) {
let segment = this.layer()[segmentId]
for (const vertexId in segment.vertices) {
let vertex = segment.vertices[vertexId]
2019-04-21 19:36:12 -04:00
if (vertex.x === Math.abs(a.x) && vertex.y === Math.abs(a.y)) {
2019-01-08 22:49:34 -05:00
segment.vertices[vertexId] = { x: Math.abs(b.x), y: Math.abs(b.y) }
2018-02-05 20:02:13 -05:00
}
}
}
2019-01-08 22:49:34 -05:00
dotgrid.history.push(this.layers)
2018-10-03 19:27:40 -04:00
this.clear()
2019-01-08 22:49:34 -05:00
dotgrid.renderer.update()
2018-02-05 20:02:13 -05:00
}
2019-01-08 22:49:34 -05:00
this.translateMulti = function (a, b) {
2018-11-21 18:14:57 -05:00
const offset = { x: a.x - b.x, y: a.y - b.y }
2019-01-08 22:49:34 -05:00
const segment = this.selectSegmentAt(a)
2018-03-21 03:35:28 -04:00
2018-11-21 18:14:57 -05:00
if (!segment) { return }
2019-01-08 22:49:34 -05:00
for (const vertexId in segment.vertices) {
let vertex = segment.vertices[vertexId]
segment.vertices[vertexId] = { x: vertex.x - offset.x, y: vertex.y - offset.y }
2018-11-21 18:14:57 -05:00
}
2019-01-08 22:49:34 -05:00
dotgrid.history.push(this.layers)
2018-11-21 18:14:57 -05:00
this.clear()
2019-01-08 22:49:34 -05:00
dotgrid.renderer.update()
2018-11-21 18:14:57 -05:00
}
2019-01-08 22:49:34 -05:00
this.translateLayer = function (a, b) {
2018-12-16 18:01:53 -05:00
const offset = { x: a.x - b.x, y: a.y - b.y }
2019-01-08 22:49:34 -05:00
for (const segmentId in this.layer()) {
let segment = this.layer()[segmentId]
for (const vertexId in segment.vertices) {
let vertex = segment.vertices[vertexId]
segment.vertices[vertexId] = { x: vertex.x - offset.x, y: vertex.y - offset.y }
2018-12-16 18:01:53 -05:00
}
}
2019-01-08 22:49:34 -05:00
dotgrid.history.push(this.layers)
2018-12-16 18:01:53 -05:00
this.clear()
2019-01-08 22:49:34 -05:00
dotgrid.renderer.update()
2018-12-16 18:01:53 -05:00
}
2019-01-08 22:49:34 -05:00
this.translateCopy = function (a, b) {
2018-11-21 18:14:57 -05:00
const offset = { x: a.x - b.x, y: a.y - b.y }
2019-01-08 22:49:34 -05:00
const segment = this.selectSegmentAt(a, copy(this.layer()))
2018-11-21 18:14:57 -05:00
if (!segment) { return }
2019-01-08 22:49:34 -05:00
for (const vertexId in segment.vertices) {
let vertex = segment.vertices[vertexId]
segment.vertices[vertexId] = { x: vertex.x - offset.x, y: vertex.y - offset.y }
2018-03-21 03:35:28 -04:00
}
2018-11-21 18:14:57 -05:00
this.layer().push(segment)
2019-01-08 22:49:34 -05:00
dotgrid.history.push(this.layers)
2018-10-03 19:27:40 -04:00
this.clear()
2019-01-08 22:49:34 -05:00
dotgrid.renderer.update()
2018-03-21 03:35:28 -04:00
}
2019-01-07 17:34:57 -05:00
this.merge = function () {
const merged = [].concat(this.layers[0]).concat(this.layers[1]).concat(this.layers[2])
this.erase()
this.layers[this.index] = merged
2019-01-08 22:49:34 -05:00
dotgrid.history.push(this.layers)
2019-01-07 17:34:57 -05:00
this.clear()
2019-01-08 22:49:34 -05:00
dotgrid.renderer.update()
2019-01-07 17:34:57 -05:00
}
2018-03-06 22:08:34 -05:00
// Style
2018-02-06 01:34:45 -05:00
2018-10-03 19:27:40 -04:00
this.style = function () {
if (!this.styles[this.index]) {
this.styles[this.index] = []
2018-03-06 22:08:34 -05:00
}
2018-10-03 19:27:40 -04:00
return this.styles[this.index]
2018-02-06 01:34:45 -05:00
}
2018-03-06 22:08:34 -05:00
// Layers
2018-02-07 15:34:17 -05:00
2019-01-12 19:32:03 -05:00
this.layer = function (index = this.index) {
if (!this.layers[index]) {
this.layers[index] = []
2018-03-06 22:08:34 -05:00
}
2019-01-12 19:32:03 -05:00
return this.layers[index]
2018-02-06 01:34:45 -05:00
}
2018-02-06 13:42:34 -05:00
2019-01-08 22:49:34 -05:00
this.selectLayer = function (id) {
2018-10-03 19:27:40 -04:00
this.index = clamp(id, 0, 2)
2019-01-11 22:02:29 -05:00
2019-01-10 00:14:01 -05:00
if (this.index !== 0) { this.settings.crest = false }
2018-10-03 19:27:40 -04:00
this.clear()
2019-01-08 22:49:34 -05:00
dotgrid.renderer.update()
dotgrid.interface.update(true)
2018-02-06 13:42:34 -05:00
console.log(`layer:${this.index}`)
}
2019-01-08 22:49:34 -05:00
this.selectNextLayer = function () {
2018-10-01 15:38:14 -04:00
this.index = this.index >= 2 ? 0 : this.index++
2019-01-08 22:49:34 -05:00
this.selectLayer(this.index)
2018-10-01 15:38:14 -04:00
}
2019-01-08 22:49:34 -05:00
this.selectPrevLayer = function () {
2018-10-01 15:38:14 -04:00
this.index = this.index >= 0 ? 2 : this.index--
2019-01-08 22:49:34 -05:00
this.selectLayer(this.index)
2018-02-06 13:42:34 -05:00
}
2018-02-07 01:44:18 -05:00
2018-10-03 19:27:40 -04:00
function copy (data) { return data ? JSON.parse(JSON.stringify(data)) : [] }
function clamp (v, min, max) { return v < min ? min : v > max ? max : v }
2018-05-09 03:19:24 -04:00
}