pointvec/desktop/sources/scripts/tool.js

301 lines
9.1 KiB
JavaScript
Raw Normal View History

2018-10-03 23:27:40 +00:00
'use strict'
2018-08-28 04:34:17 +00:00
2018-10-10 20:09:00 +00:00
DOTGRID.Tool = function () {
2018-10-03 23:27:40 +00:00
this.index = 0
this.settings = { size: { width: 300, height: 300 } }
this.layers = [[], [], []]
2018-03-06 22:37:01 +00:00
this.styles = [
2018-10-03 23:27:40 +00:00
{ thickness: 10, strokeLinecap: 'round', strokeLinejoin: 'round', color: '#f00', fill: 'none', mirror_style: 0 },
{ thickness: 10, strokeLinecap: 'round', strokeLinejoin: 'round', color: '#0f0', fill: 'none', mirror_style: 0 },
{ thickness: 10, strokeLinecap: 'round', strokeLinejoin: 'round', color: '#00f', fill: 'none', mirror_style: 0 }
]
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 () {
2018-10-04 23:52:17 +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
}
2018-10-03 23:27:40 +00:00
this.reset = function () {
this.layers = [[], [], []]
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 = []
2018-10-04 23:52:17 +00:00
DOTGRID.guide.update()
DOTGRID.interface.update(true)
2018-03-06 22:26:18 +00:00
}
2018-10-03 23:27:40 +00:00
this.undo = function () {
2018-10-04 23:52:17 +00:00
this.layers = DOTGRID.history.prev()
DOTGRID.guide.update()
DOTGRID.interface.update(true)
2018-03-07 03:08:34 +00:00
}
2018-10-03 23:27:40 +00:00
this.redo = function () {
2018-10-04 23:52:17 +00:00
this.layers = DOTGRID.history.next()
DOTGRID.guide.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)
2018-10-04 23:52:17 +00:00
DOTGRID.history.push(this.layers)
2018-10-03 23:27:40 +00:00
this.clear()
2018-10-04 23:52:17 +00:00
DOTGRID.guide.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) {
if (!dot.layers || dot.layers.length != 3) { console.warn('Incompatible version'); return }
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-10-03 23:27:40 +00:00
if (this.settings && (this.settings.size.width != dot.settings.size.width || this.settings.size.height != dot.settings.size.height)) {
2018-10-04 23:52:17 +00:00
DOTGRID.set_size({ width: dot.settings.size.width, height: dot.settings.size.height })
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()
2018-10-04 23:52:17 +00:00
DOTGRID.guide.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
this.remove_segment = function () {
if (this.vertices.length > 0) { this.clear(); return }
this.layer().pop()
this.clear()
2018-10-04 23:52:17 +00:00
DOTGRID.guide.update()
DOTGRID.interface.update(true)
}
2018-10-03 23:27:40 +00:00
this.remove_segments_at = function (pos) {
for (const segment_id in this.layer()) {
let segment = this.layer()[segment_id]
for (const vertex_id in segment.vertices) {
let vertex = segment.vertices[vertex_id]
if (Math.abs(pos.x) == Math.abs(vertex.x) && Math.abs(pos.y) == Math.abs(vertex.y)) {
segment.vertices.splice(vertex_id, 1)
2018-02-06 04:35:25 +00:00
}
}
2018-10-03 23:27:40 +00:00
if (segment.vertices.length < 2) {
this.layers[this.index].splice(segment_id, 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()
2018-10-04 23:52:17 +00:00
DOTGRID.guide.update()
DOTGRID.interface.update(true)
2018-02-06 04:19:34 +00:00
}
2018-10-03 23:27:40 +00:00
this.add_vertex = function (pos) {
pos = { x: Math.abs(pos.x), y: Math.abs(pos.y) }
this.vertices.push(pos)
2018-10-04 23:52:17 +00:00
DOTGRID.interface.update(true)
}
2018-10-03 23:27:40 +00:00
this.vertex_at = function (pos) {
for (const segment_id in this.layer()) {
let segment = this.layer()[segment_id]
for (const vertex_id in segment.vertices) {
let vertex = segment.vertices[vertex_id]
if (vertex.x == Math.abs(pos.x) && vertex.y == Math.abs(pos.y)) {
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
}
2018-10-03 23:27:40 +00:00
this.cast = function (type) {
if (!this.layer()) { this.layers[this.index] = [] }
if (!this.can_cast(type)) { console.warn('Cannot cast'); return }
2018-10-03 23:27:40 +00:00
let append_target = this.can_append({ type: type, vertices: this.vertices.slice() })
2018-02-11 04:00:22 +00:00
2018-10-03 23:27:40 +00:00
if (append_target) {
2018-05-09 07:19:24 +00:00
this.layers[this.index][append_target].vertices = this.layers[this.index][append_target].vertices.concat(this.vertices.slice())
2018-10-03 23:27:40 +00:00
} else {
this.layer().push({ type: type, vertices: this.vertices.slice() })
2018-02-11 04:00:22 +00:00
}
2018-05-10 21:13:01 +00:00
2018-10-04 23:52:17 +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()
2018-10-04 23:52:17 +00:00
DOTGRID.guide.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) {
if (type == 'linecap') {
let a = ['butt', 'square', 'round']
this.i.linecap += mod
this.style().strokeLinecap = a[this.i.linecap % a.length]
} else if (type == 'linejoin') {
let a = ['miter', 'round', 'bevel']
this.i.linejoin += mod
this.style().strokeLinejoin = a[this.i.linejoin % a.length]
} else if (type == 'fill') {
this.style().fill = this.style().fill == 'none' ? this.style().color : 'none'
} else if (type == 'thickness') {
this.style().thickness = clamp(this.style().thickness + mod, 1, 100)
} else if (type == 'mirror') {
this.style().mirror_style = this.style().mirror_style > 3 ? 0 : this.style().mirror_style + 1
} else {
console.warn('Unknown', type)
2018-08-03 00:57:36 +00:00
}
2018-10-04 23:52:17 +00:00
DOTGRID.interface.update(true)
DOTGRID.guide.update()
2018-08-03 00:53:26 +00:00
}
2018-10-03 23:27:40 +00:00
this.misc = function (type) {
2018-10-04 23:52:17 +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) {
2018-10-04 23:52:17 +00:00
if (type == 'grid') { DOTGRID.guide.toggle() }
2018-10-03 23:27:40 +00:00
if (type == 'screen') { app.toggle_fullscreen() }
2018-08-04 03:55:08 +00:00
2018-10-04 23:52:17 +00:00
if (type == 'open') { DOTGRID.open() }
if (type == 'save') { DOTGRID.save() }
if (type == 'render') { DOTGRID.render() }
if (type == 'export') { DOTGRID.export() }
2018-08-04 03:55:08 +00:00
}
2018-10-03 23:27:40 +00:00
this.can_append = function (content) {
for (const id in this.layer()) {
let stroke = this.layer()[id]
if (stroke.type != content.type) { continue }
if (!stroke.vertices) { continue }
if (!stroke.vertices[stroke.vertices.length - 1]) { continue }
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 }
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
}
2018-10-03 23:27:40 +00:00
this.can_cast = function (type) {
if (!type) { return false }
2018-02-06 03:47:25 +00:00
// Cannot cast close twice
2018-10-03 23:27:40 +00:00
if (type == 'close') {
let prev = this.layer()[this.layer().length - 1]
if (!prev || prev.type == 'close') {
return false
2018-02-06 03:47:25 +00:00
}
}
2018-10-03 23:27:40 +00:00
if (type == 'bezier') {
if (this.vertices.length != 3 && this.vertices.length != 5 && this.vertices.length != 7 && this.vertices.length != 9) {
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 () {
2018-10-04 23:52:17 +00:00
let l1 = new Generator(DOTGRID.tool.layers[0], DOTGRID.tool.styles[0]).toString({ x: -10, y: -10 }, 1)
let l2 = new Generator(DOTGRID.tool.layers[1], DOTGRID.tool.styles[1]).toString({ x: -10, y: -10 }, 1)
let l3 = new Generator(DOTGRID.tool.layers[2], DOTGRID.tool.styles[2]).toString({ x: -10, y: -10 }, 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 () {
2018-10-04 23:52:17 +00:00
return new Generator(DOTGRID.tool.layer(), DOTGRID.tool.style()).toString({ x: -10, y: -10 }, 1)
2018-04-16 02:32:54 +00:00
}
2018-10-03 23:27:40 +00:00
this.translate = function (a, b) {
for (const segment_id in this.layer()) {
let segment = this.layer()[segment_id]
for (const vertex_id in segment.vertices) {
let vertex = segment.vertices[vertex_id]
if (vertex.x == Math.abs(a.x) && vertex.y == Math.abs(a.y)) {
segment.vertices[vertex_id] = { x: Math.abs(b.x), y: Math.abs(b.y) }
2018-02-06 01:02:13 +00:00
}
}
}
2018-10-04 23:52:17 +00:00
DOTGRID.history.push(this.layers)
2018-10-03 23:27:40 +00:00
this.clear()
2018-10-04 23:52:17 +00:00
DOTGRID.guide.update()
2018-02-06 01:02:13 +00:00
}
2018-10-03 23:27:40 +00:00
this.translate_multi = function (a, b) {
let offset = { x: a.x - b.x, y: a.y - b.y }
2018-03-21 07:35:28 +00:00
2018-10-03 23:27:40 +00:00
for (const segment_id in this.layer()) {
let segment = this.layer()[segment_id]
for (const vertex_id in segment.vertices) {
let vertex = segment.vertices[vertex_id]
segment.vertices[vertex_id] = { x: vertex.x - offset.x, y: vertex.y - offset.y }
2018-03-21 07:35:28 +00:00
}
}
2018-10-04 23:52:17 +00:00
DOTGRID.history.push(this.layers)
2018-10-03 23:27:40 +00:00
this.clear()
2018-10-04 23:52:17 +00:00
DOTGRID.guide.update()
2018-03-21 07:35:28 +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
2018-10-03 23:27:40 +00:00
this.layer = function () {
if (!this.layers[this.index]) {
this.layers[this.index] = []
2018-03-07 03:08:34 +00:00
}
2018-10-03 23:27:40 +00:00
return this.layers[this.index]
2018-02-06 06:34:45 +00:00
}
2018-02-06 18:42:34 +00:00
2018-10-03 23:27:40 +00:00
this.select_layer = function (id) {
this.index = clamp(id, 0, 2)
this.clear()
2018-10-04 23:52:17 +00:00
DOTGRID.guide.update()
DOTGRID.interface.update(true)
2018-02-06 18:42:34 +00:00
console.log(`layer:${this.index}`)
}
2018-10-03 23:27:40 +00:00
this.select_next_layer = function () {
2018-10-01 19:38:14 +00:00
this.index = this.index >= 2 ? 0 : this.index++
2018-10-03 23:27:40 +00:00
this.select_layer(this.index)
2018-10-01 19:38:14 +00:00
}
2018-10-03 23:27:40 +00:00
this.select_prev_layer = function () {
2018-10-01 19:38:14 +00:00
this.index = this.index >= 0 ? 2 : this.index--
2018-10-03 23:27:40 +00:00
this.select_layer(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
}