pointvec/desktop/sources/scripts/tool.js

367 lines
11 KiB
JavaScript
Raw Normal View History

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