pointvec/desktop/sources/scripts/dotgrid.js

268 lines
9.8 KiB
JavaScript
Raw Normal View History

2018-10-03 19:27:40 -04:00
'use strict'
2019-11-03 13:36:58 -05:00
/* global Acels */
/* global Theme */
/* global Source */
/* global History */
/* global Manager */
/* global Renderer */
/* global Tool */
/* global Interface */
/* global Picker */
/* global Cursor */
/* global FileReader */
2018-12-21 17:07:46 -05:00
2019-11-03 13:36:58 -05:00
function Dotgrid () {
2018-10-03 19:27:40 -04:00
this.install = function (host) {
2019-04-21 20:28:31 -04:00
console.info('Dotgrid', 'Installing..')
2019-11-03 13:36:58 -05:00
2019-11-08 11:19:27 -05:00
this.acels = new Acels(this)
this.theme = new Theme(this)
this.history = new History(this)
this.source = new Source(this)
2019-04-21 19:49:47 -04:00
2019-01-08 22:09:20 -05:00
this.manager = new Manager(this)
2019-01-08 22:12:18 -05:00
this.renderer = new Renderer(this)
2019-01-08 22:49:34 -05:00
this.tool = new Tool(this)
2019-01-08 22:51:57 -05:00
this.interface = new Interface(this)
this.picker = new Picker(this)
this.cursor = new Cursor(this)
2019-01-08 22:12:18 -05:00
host.appendChild(this.renderer.el)
2017-11-04 20:52:05 -04:00
2019-11-03 13:36:58 -05:00
document.addEventListener('mousedown', (e) => { this.cursor.down(e) }, false)
document.addEventListener('mousemove', (e) => { this.cursor.move(e) }, false)
document.addEventListener('contextmenu', (e) => { this.cursor.alt(e) }, false)
document.addEventListener('mouseup', (e) => { this.cursor.up(e) }, false)
document.addEventListener('copy', (e) => { this.copy(e) }, false)
document.addEventListener('cut', (e) => { this.cut(e) }, false)
document.addEventListener('paste', (e) => { this.paste(e) }, false)
window.addEventListener('resize', (e) => { this.onResize() }, false)
window.addEventListener('dragover', (e) => { e.stopPropagation(); e.preventDefault(); e.dataTransfer.dropEffect = 'copy' })
2019-11-03 20:44:31 -05:00
window.addEventListener('drop', this.onDrop)
2019-11-03 13:36:58 -05:00
this.acels.set('File', 'New', 'CmdOrCtrl+N', () => { this.source.new() })
this.acels.set('File', 'Open', 'CmdOrCtrl+O', () => { this.source.open('grid', this.whenOpen) })
2019-11-08 11:19:27 -05:00
this.acels.set('File', 'Save', 'CmdOrCtrl+S', () => { this.source.write('dotgrid', 'grid', this.tool.export(), 'text/plain') })
this.acels.set('File', 'Export Vector', 'CmdOrCtrl+E', () => { this.source.write('dotgrid', 'svg', this.manager.toString(), 'image/svg+xml') })
this.acels.set('File', 'Export Image', 'CmdOrCtrl+Shift+E', () => { this.manager.toPNG(this.tool.settings.size, (dataUrl) => { this.source.download('dotgrid', 'png', dataUrl, 'image/png') }) })
2019-11-03 13:36:58 -05:00
this.acels.set('File', 'Revert', 'CmdOrCtrl+W', () => { this.source.revert() })
this.acels.set('History', 'Undo', 'CmdOrCtrl+Z', () => { this.history.undo() })
this.acels.set('History', 'Redo', 'CmdOrCtrl+Shift+Z', () => { this.history.redo() })
this.acels.set('Stroke', 'Line', 'A', () => { this.tool.cast('line') })
this.acels.set('Stroke', 'Arc', 'S', () => { this.tool.cast('arc_c') })
this.acels.set('Stroke', 'Arc Rev', 'D', () => { this.tool.cast('arc_r') })
this.acels.set('Stroke', 'Bezier', 'F', () => { this.tool.cast('bezier') })
this.acels.set('Stroke', 'Close', 'Z', () => { this.tool.cast('close') })
this.acels.set('Stroke', 'Arc(full)', 'T', () => { this.tool.cast('arc_c_full') })
this.acels.set('Stroke', 'Arc Rev(full)', 'Y', () => { this.tool.cast('arc_r_full') })
this.acels.set('Stroke', 'Clear Selection', 'Escape', () => { this.tool.clear() })
this.acels.set('Effect', 'Linecap', 'Q', () => { this.tool.toggle('linecap') })
this.acels.set('Effect', 'Linejoin', 'W', () => { this.tool.toggle('linejoin') })
this.acels.set('Effect', 'Mirror', 'E', () => { this.tool.toggle('mirror') })
this.acels.set('Effect', 'Fill', 'R', () => { this.tool.toggle('fill') })
this.acels.set('Effect', 'Thicker', '}', () => { this.tool.toggle('thickness', 1) })
this.acels.set('Effect', 'Thinner', '{', () => { this.tool.toggle('thickness', -1) })
this.acels.set('Effect', 'Thicker +5', ']', () => { this.tool.toggle('thickness', 5) })
this.acels.set('Effect', 'Thinner -5', '[', () => { this.tool.toggle('thickness', -5) })
this.acels.set('Manual', 'Add Point', 'Enter', () => { this.tool.addVertex(this.cursor.pos); this.renderer.update() })
this.acels.set('Manual', 'Move Up', 'Up', () => { this.cursor.pos.y -= 15; this.renderer.update() })
this.acels.set('Manual', 'Move Right', 'Right', () => { this.cursor.pos.x += 15; this.renderer.update() })
this.acels.set('Manual', 'Move Down', 'Down', () => { this.cursor.pos.y += 15; this.renderer.update() })
this.acels.set('Manual', 'Move Left', 'Left', () => { this.cursor.pos.x -= 15; this.renderer.update() })
this.acels.set('Manual', 'Remove Point', 'Shift+Backspace', () => { this.tool.removeSegmentsAt(this.cursor.pos) })
this.acels.set('Manual', 'Remove Segment', 'Backspace', () => { this.tool.removeSegment() })
this.acels.set('Layers', 'Foreground', 'CmdOrCtrl+1', () => { this.tool.selectLayer(0) })
this.acels.set('Layers', 'Middleground', 'CmdOrCtrl+2', () => { this.tool.selectLayer(1) })
this.acels.set('Layers', 'Background', 'CmdOrCtrl+3', () => { this.tool.selectLayer(2) })
this.acels.set('Layers', 'Merge Layers', 'CmdOrCtrl+M', () => { this.tool.merge() })
this.acels.set('View', 'Color Picker', 'G', () => { this.picker.start() })
this.acels.set('View', 'Toggle Grid', 'H', () => { this.renderer.toggle() })
this.acels.install(window)
2019-11-08 11:19:27 -05:00
this.acels.pipe(this)
2019-11-03 13:36:58 -05:00
2019-01-08 22:09:20 -05:00
this.manager.install()
2018-10-03 19:27:40 -04:00
this.interface.install(host)
2019-04-21 20:28:31 -04:00
this.theme.install(host, () => { this.update() })
2018-09-11 21:20:31 -04:00
}
2019-11-03 13:36:58 -05:00
this.start = () => {
2019-11-03 20:44:31 -05:00
console.log('Dotgrid', 'Starting..')
2019-11-03 13:36:58 -05:00
console.info(`${this.acels}`)
2018-10-03 19:27:40 -04:00
this.theme.start()
this.tool.start()
2019-01-08 22:12:18 -05:00
this.renderer.start()
2018-10-03 19:27:40 -04:00
this.interface.start()
2019-04-21 19:49:47 -04:00
this.source.new()
2019-04-21 21:25:31 -04:00
this.onResize()
2018-10-03 19:27:40 -04:00
setTimeout(() => { document.body.className += ' ready' }, 250)
2018-09-11 23:27:01 -04:00
}
2019-11-03 13:36:58 -05:00
this.update = () => {
2019-04-21 19:49:47 -04:00
this.manager.update()
this.interface.update()
this.renderer.update()
2016-12-31 12:18:01 -05:00
}
2019-11-03 13:36:58 -05:00
this.clear = () => {
2019-01-09 16:00:52 -05:00
this.history.clear()
this.tool.reset()
this.reset()
this.renderer.update()
this.interface.update(true)
}
2019-11-03 13:36:58 -05:00
this.reset = () => {
2019-01-09 16:00:52 -05:00
this.tool.clear()
this.update()
}
2019-11-08 11:19:27 -05:00
this.whenOpen = (file, data) => {
2019-11-03 20:44:31 -05:00
this.tool.replace(JSON.parse(data))
2019-11-08 11:19:27 -05:00
this.onResize()
2017-11-04 19:59:11 -04:00
}
2019-04-21 20:45:21 -04:00
// Resize Tools
2019-11-03 13:36:58 -05:00
this.fitSize = () => {
2019-04-21 20:28:31 -04:00
if (this.requireResize() === false) { return }
console.log('Dotgrid', `Will resize to: ${printSize(this.getRequiredSize())}`)
2019-04-21 20:45:21 -04:00
this.update()
}
2019-11-03 13:36:58 -05:00
this.getPadding = () => {
2019-04-21 21:09:42 -04:00
return { x: 60, y: 90 }
2019-04-21 20:28:31 -04:00
}
2018-09-11 23:27:01 -04:00
2019-11-03 13:36:58 -05:00
this.getWindowSize = () => {
2019-04-21 20:28:31 -04:00
return { width: window.innerWidth, height: window.innerHeight }
}
2018-10-03 19:27:40 -04:00
2019-11-03 13:36:58 -05:00
this.getProjectSize = () => {
2019-04-21 20:28:31 -04:00
return this.tool.settings.size
}
2018-08-17 15:58:01 -04:00
2019-11-03 13:36:58 -05:00
this.getPaddedSize = () => {
2019-04-21 20:34:48 -04:00
const rect = this.getWindowSize()
const pad = this.getPadding()
2019-04-21 20:45:21 -04:00
return { width: step(rect.width - pad.x, 15), height: step(rect.height - pad.y, 15) }
2019-04-21 20:28:31 -04:00
}
2018-08-17 15:58:01 -04:00
2019-11-03 13:36:58 -05:00
this.getRequiredSize = () => {
2019-04-21 20:34:48 -04:00
const rect = this.getProjectSize()
const pad = this.getPadding()
return { width: step(rect.width, 15) + pad.x, height: step(rect.height, 15) + pad.y }
2019-04-21 20:28:31 -04:00
}
2019-11-03 13:36:58 -05:00
this.requireResize = () => {
2019-04-21 20:34:48 -04:00
const _window = this.getWindowSize()
2019-04-21 20:28:31 -04:00
const _required = this.getRequiredSize()
2019-04-21 21:00:32 -04:00
const offset = sizeOffset(_window, _required)
2019-04-21 20:28:31 -04:00
if (offset.width !== 0 || offset.height !== 0) {
2019-11-03 13:36:58 -05:00
console.log('Dotgrid', `Require ${printSize(_required)}, but window is ${printSize(_window)}(${printSize(offset)})`)
2019-04-21 20:28:31 -04:00
return true
}
return false
2018-08-17 15:58:01 -04:00
}
2019-11-03 13:36:58 -05:00
this.onResize = () => {
2019-04-21 20:45:21 -04:00
const _project = this.getProjectSize()
const _padded = this.getPaddedSize()
2019-04-21 21:00:32 -04:00
const offset = sizeOffset(_padded, _project)
2019-04-21 20:45:21 -04:00
if (offset.width !== 0 || offset.height !== 0) {
2019-11-03 13:36:58 -05:00
console.log('Dotgrid', `Resize project to ${printSize(_padded)}`)
2019-04-21 20:45:21 -04:00
this.tool.settings.size = _padded
2019-01-09 17:43:17 -05:00
}
2019-04-21 20:45:21 -04:00
this.update()
2019-01-09 17:43:17 -05:00
}
2019-01-09 16:00:52 -05:00
// Events
2018-10-03 19:27:40 -04:00
this.drag = function (e) {
e.preventDefault()
e.stopPropagation()
2018-10-03 19:27:40 -04:00
const file = e.dataTransfer.files[0]
2019-01-07 17:03:38 -05:00
const filename = file.path ? file.path : file.name ? file.name : ''
2018-01-11 22:22:50 -05:00
2019-01-07 17:03:38 -05:00
if (filename.indexOf('.grid') < 0) { console.warn('Dotgrid', 'Not a .grid file'); return }
2018-01-11 22:22:50 -05:00
2018-10-03 19:27:40 -04:00
const reader = new FileReader()
2019-01-07 17:03:38 -05:00
2018-10-03 19:27:40 -04:00
reader.onload = function (e) {
2019-01-07 17:03:38 -05:00
const data = e.target && e.target.result ? e.target.result : ''
2019-11-03 13:36:58 -05:00
this.source.load(filename, data)
this.fitSize()
2018-10-03 19:27:40 -04:00
}
reader.readAsText(file)
2018-01-11 22:22:50 -05:00
}
2019-11-03 20:44:31 -05:00
this.onDrop = (e) => {
e.preventDefault()
e.stopPropagation()
const file = e.dataTransfer.files[0]
if (file.name.indexOf('.grid') > -1) {
2019-11-08 11:19:27 -05:00
this.source.read(e.dataTransfer.files[0], this.whenOpen)
2019-11-03 20:44:31 -05:00
}
}
2018-10-03 19:27:40 -04:00
this.copy = function (e) {
2019-11-03 13:36:58 -05:00
this.renderer.update()
2018-01-13 15:11:57 -05:00
2018-10-03 19:27:40 -04:00
if (e.target !== this.picker.input) {
2019-11-03 13:36:58 -05:00
e.clipboardData.setData('text/source', this.tool.export(this.tool.layer()))
e.clipboardData.setData('text/plain', this.tool.path())
e.clipboardData.setData('text/html', this.manager.el.outerHTML)
e.clipboardData.setData('text/svg+xml', this.manager.el.outerHTML)
2018-10-03 19:27:40 -04:00
e.preventDefault()
}
2018-02-07 01:44:18 -05:00
2019-11-03 13:36:58 -05:00
this.renderer.update()
2018-02-07 01:44:18 -05:00
}
2018-10-03 19:27:40 -04:00
this.cut = function (e) {
2019-11-03 13:36:58 -05:00
this.renderer.update()
2018-02-07 01:44:18 -05:00
2018-10-03 19:27:40 -04:00
if (e.target !== this.picker.input) {
2019-11-03 13:36:58 -05:00
e.clipboardData.setData('text/source', this.tool.export(this.tool.layer()))
e.clipboardData.setData('text/plain', this.tool.export(this.tool.layer()))
e.clipboardData.setData('text/html', this.manager.el.outerHTML)
e.clipboardData.setData('text/svg+xml', this.manager.el.outerHTML)
this.tool.layers[this.tool.index] = []
2018-10-03 19:27:40 -04:00
e.preventDefault()
}
2018-02-07 01:44:18 -05:00
2019-11-03 13:36:58 -05:00
this.renderer.update()
2017-11-21 16:24:25 -05:00
}
2018-10-03 19:27:40 -04:00
this.paste = function (e) {
if (e.target !== this.picker.el) {
2018-10-03 19:38:53 -04:00
let data = e.clipboardData.getData('text/source')
2019-01-07 17:03:38 -05:00
if (isJson(data)) {
2018-10-03 19:27:40 -04:00
data = JSON.parse(data.trim())
2019-11-03 13:36:58 -05:00
this.tool.import(data)
}
2018-10-03 19:27:40 -04:00
e.preventDefault()
2018-05-07 21:32:06 -04:00
}
2019-11-03 13:36:58 -05:00
this.renderer.update()
2017-11-21 16:24:25 -05:00
}
2019-11-08 11:19:27 -05:00
this.onKeyDown = (e) => {
}
this.onKeyUp = (e) => {
}
2019-11-03 20:44:31 -05:00
function sizeOffset (a, b) { return { width: a.width - b.width, height: a.height - b.height } }
function printSize (size) { return `${size.width}x${size.height}` }
function isJson (text) { try { JSON.parse(text); return true } catch (error) { return false } }
function step (v, s) { return Math.round(v / s) * s }
2017-11-05 23:35:29 -05:00
}