pointvec/desktop/sources/scripts/dotgrid.js

259 lines
7.4 KiB
JavaScript
Raw Normal View History

2018-10-03 19:27:40 -04:00
'use strict'
2019-01-08 23:19:50 -05:00
function Dotgrid (width, height) {
2019-01-09 16:00:52 -05:00
const defaultTheme = {
background: '#eee',
f_high: '#000',
f_med: '#999',
f_low: '#ccc',
f_inv: '#000',
b_high: '#000',
b_med: '#888',
b_low: '#aaa',
b_inv: '#ffb545'
}
2018-12-21 17:07:46 -05:00
2018-09-11 23:27:01 -04:00
// ISU
2018-10-03 19:27:40 -04:00
this.install = function (host) {
2019-01-08 23:44:24 -05:00
this.theme = new Theme(defaultTheme)
this.history = new History()
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-01-08 22:09:20 -05:00
this.manager.install()
2018-10-03 19:27:40 -04:00
this.interface.install(host)
this.theme.install(host, this.update)
2018-09-11 21:20:31 -04:00
}
2018-10-03 19:27:40 -04:00
this.start = function () {
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()
2018-10-04 19:52:17 -04:00
document.addEventListener('mousedown', function (e) { DOTGRID.cursor.down(e) }, false)
document.addEventListener('mousemove', function (e) { DOTGRID.cursor.move(e) }, false)
document.addEventListener('contextmenu', function (e) { DOTGRID.cursor.alt(e) }, false)
document.addEventListener('mouseup', function (e) { DOTGRID.cursor.up(e) }, false)
document.addEventListener('copy', function (e) { DOTGRID.copy(e) }, false)
document.addEventListener('cut', function (e) { DOTGRID.cut(e) }, false)
document.addEventListener('paste', function (e) { DOTGRID.paste(e) }, false)
window.addEventListener('drop', DOTGRID.drag)
2018-10-03 19:27:40 -04:00
this.new()
setTimeout(() => { document.body.className += ' ready' }, 250)
2018-09-11 23:27:01 -04:00
}
2018-10-03 19:27:40 -04:00
this.update = function () {
2018-10-04 19:52:17 -04:00
DOTGRID.resize()
2019-01-08 22:09:20 -05:00
DOTGRID.manager.update()
2018-10-04 19:52:17 -04:00
DOTGRID.interface.update()
2019-01-08 22:12:18 -05:00
DOTGRID.renderer.update()
2016-12-31 12:18:01 -05:00
}
2019-01-09 16:00:52 -05:00
this.clear = function () {
this.history.clear()
this.tool.reset()
this.reset()
this.renderer.update()
this.interface.update(true)
}
this.reset = function () {
this.tool.clear()
this.update()
}
2018-05-07 01:03:35 -04:00
// File
2018-01-12 03:46:09 -05:00
2018-10-03 19:27:40 -04:00
this.new = function () {
2019-01-09 15:41:41 -05:00
this.setZoom(1.0)
2019-01-09 22:28:04 -05:00
this.setSize({ width: 300, height: 325 })
2018-10-03 19:27:40 -04:00
this.history.push(this.tool.layers)
this.clear()
2018-01-12 03:09:26 -05:00
}
2018-10-03 19:27:40 -04:00
this.open = function () {
if (!dialog) { return }
2018-08-26 15:39:15 -04:00
2018-10-03 19:27:40 -04:00
const paths = dialog.showOpenDialog({ properties: ['openFile'], filters: [{ name: 'Dotgrid Image', extensions: ['dot', 'grid'] }] })
2018-05-10 17:57:14 -04:00
2018-10-03 19:27:40 -04:00
if (!paths) { console.warn('Nothing to load'); return }
2018-05-10 17:57:14 -04:00
fs.readFile(paths[0], 'utf-8', (err, data) => {
2018-10-03 19:27:40 -04:00
if (err) { alert('An error ocurred reading the file :' + err.message); return }
this.tool.replace(JSON.parse(data.toString().trim()))
2019-01-08 22:12:18 -05:00
this.renderer.update()
2018-10-03 19:27:40 -04:00
})
2018-05-10 17:57:14 -04:00
}
2019-01-09 15:45:50 -05:00
this.save = function () {
if (DOTGRID.tool.length() < 1) { console.warn('Nothing to save'); return }
this.manager.toGRID(grab)
}
this.export = function () {
if (DOTGRID.tool.length() < 1) { console.warn('Nothing to export'); return }
this.manager.toSVG(grab)
}
this.render = function () {
if (DOTGRID.tool.length() < 1) { console.warn('Nothing to render'); return }
this.manager.toPNG({ width: DOTGRID.tool.settings.size.width * 2, height: DOTGRID.tool.settings.size.height * 2 }, grab)
}
function grab (base64, name) {
const link = document.createElement('a')
link.setAttribute('href', base64)
link.setAttribute('download', name)
link.dispatchEvent(new MouseEvent(`click`, { bubbles: true, cancelable: true, view: window }))
}
2018-05-07 01:03:35 -04:00
// Basics
2019-01-09 15:35:10 -05:00
this.getSize = function () {
return { markers: {
w: parseInt(this.tool.settings.size.width / 15),
h: parseInt(this.tool.settings.size.height / 15) }
}
}
2019-01-08 21:36:26 -05:00
this.setSize = function (size = { width: 300, height: 300 }, ui = true, scale = 1) {
2018-10-03 19:27:40 -04:00
size = { width: clamp(step(size.width, 15), 105, 1080), height: clamp(step(size.height, 15), 120, 1080) }
2018-05-06 19:15:23 -04:00
2018-05-07 18:47:09 -04:00
this.tool.settings.size.width = size.width
this.tool.settings.size.height = size.height
2018-05-10 04:01:31 -04:00
2018-10-03 19:27:40 -04:00
try {
const win = require('electron').remote.getCurrentWindow()
2019-01-08 21:45:20 -05:00
win.setSize((size.width + 100) * scale, (size.height + 100) * scale, true)
2018-10-03 19:27:40 -04:00
} catch (err) {
console.log('No window')
2018-08-17 15:58:01 -04:00
}
2018-05-07 00:24:01 -04:00
2019-01-08 23:27:59 -05:00
this.renderer.resize(size)
2018-10-03 19:27:40 -04:00
this.interface.update()
2019-01-08 23:27:59 -05:00
this.renderer.update()
2017-11-04 19:59:11 -04:00
}
2018-10-03 19:27:40 -04:00
this.resize = function () {
const size = { width: step(window.innerWidth - 90, 15), height: step(window.innerHeight - 120, 15) }
2018-08-17 15:58:01 -04:00
2019-01-08 23:27:59 -05:00
if (size.width == this.tool.settings.size.width && size.height == this.tool.settings.size.height) {
2018-10-03 19:27:40 -04:00
return
2018-09-11 23:27:01 -04:00
}
console.log(`Resized: ${size.width}x${size.height}`)
2018-10-03 19:27:40 -04:00
2019-01-08 23:27:59 -05:00
this.tool.settings.size.width = size.width
this.tool.settings.size.height = size.height
2018-08-17 15:58:01 -04:00
2019-01-08 23:27:59 -05:00
this.renderer.resize(size)
2018-08-17 15:58:01 -04:00
2018-08-17 17:34:24 -04:00
document.title = `Dotgrid — ${size.width}x${size.height}`
2018-08-17 15:58:01 -04:00
}
2019-01-09 17:43:17 -05:00
this.setZoom = function (scale) {
this.setSize({ width: this.tool.settings.size.width, height: this.tool.settings.size.height }, true, scale)
try {
webFrame.setZoomFactor(scale)
} catch (err) {
console.log('Cannot zoom')
}
}
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 : ''
if (data && !isJson(data)) { return }
DOTGRID.tool.replace(JSON.parse(`${data}`))
2019-01-08 22:12:18 -05:00
DOTGRID.renderer.update()
2018-10-03 19:27:40 -04:00
}
reader.readAsText(file)
2018-01-11 22:22:50 -05:00
}
2018-10-03 19:27:40 -04:00
this.copy = function (e) {
2019-01-08 22:12:18 -05:00
DOTGRID.renderer.update()
2018-01-13 15:11:57 -05:00
2018-10-03 19:27:40 -04:00
if (e.target !== this.picker.input) {
2018-10-04 19:52:17 -04:00
e.clipboardData.setData('text/source', DOTGRID.tool.export(DOTGRID.tool.layer()))
e.clipboardData.setData('text/plain', DOTGRID.tool.path())
2019-01-08 22:09:20 -05:00
e.clipboardData.setData('text/html', DOTGRID.manager.svg_el.outerHTML)
e.clipboardData.setData('text/svg+xml', DOTGRID.manager.svg_el.outerHTML)
2018-10-03 19:27:40 -04:00
e.preventDefault()
}
2018-02-07 01:44:18 -05:00
2019-01-08 22:12:18 -05:00
DOTGRID.renderer.update()
2018-02-07 01:44:18 -05:00
}
2018-10-03 19:27:40 -04:00
this.cut = function (e) {
2019-01-08 22:12:18 -05:00
DOTGRID.renderer.update()
2018-02-07 01:44:18 -05:00
2018-10-03 19:27:40 -04:00
if (e.target !== this.picker.input) {
2018-11-21 03:11:27 -05:00
e.clipboardData.setData('text/source', DOTGRID.tool.export(DOTGRID.tool.layer()))
2018-10-04 19:52:17 -04:00
e.clipboardData.setData('text/plain', DOTGRID.tool.export(DOTGRID.tool.layer()))
2019-01-08 22:09:20 -05:00
e.clipboardData.setData('text/html', DOTGRID.manager.svg_el.outerHTML)
e.clipboardData.setData('text/svg+xml', DOTGRID.manager.svg_el.outerHTML)
2018-10-04 19:52:17 -04:00
DOTGRID.tool.layers[DOTGRID.tool.index] = []
2018-10-03 19:27:40 -04:00
e.preventDefault()
}
2018-02-07 01:44:18 -05:00
2019-01-08 22:12:18 -05:00
DOTGRID.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())
2018-10-04 19:52:17 -04:00
DOTGRID.tool.import(data)
}
2018-10-03 19:27:40 -04:00
e.preventDefault()
2018-05-07 21:32:06 -04:00
}
2019-01-08 22:12:18 -05:00
DOTGRID.renderer.update()
2017-11-21 16:24:25 -05:00
}
2017-11-05 23:35:29 -05:00
}
2017-11-06 21:10:09 -05:00
2018-10-03 19:27:40 -04:00
window.addEventListener('resize', function (e) {
2018-10-04 19:52:17 -04:00
DOTGRID.update()
2018-10-03 19:27:40 -04:00
}, false)
window.addEventListener('dragover', function (e) {
e.stopPropagation()
e.preventDefault()
e.dataTransfer.dropEffect = 'copy'
})
String.prototype.capitalize = function () {
return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase()
2018-05-09 03:22:00 -04:00
}
2018-07-17 20:52:23 -04:00
2019-01-07 17:03:38 -05:00
function isJson (text) { try { JSON.parse(text); return true } catch (error) { return false } }
function isEqual (a, b) { return a && b && a.x == b.x && a.y == b.y }
2018-10-03 19:27:40 -04:00
function clamp (v, min, max) { return v < min ? min : v > max ? max : v }
function step (v, s) { return Math.round(v / s) * s }
2018-10-04 19:52:17 -04:00
2019-01-08 23:19:50 -05:00
const DOTGRID = new Dotgrid(300, 300)