2018-10-03 19:27:40 -04:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
function Dotgrid (width, height, grid_x, grid_y, block_x, block_y) {
|
|
|
|
this.controller = null
|
2018-12-21 17:07:46 -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' }
|
|
|
|
|
|
|
|
this.theme = new Theme(defaultTheme)
|
2018-10-03 19:27:40 -04:00
|
|
|
this.history = new History()
|
|
|
|
|
|
|
|
this.grid_x = grid_x
|
|
|
|
this.grid_y = grid_y
|
|
|
|
this.block_x = block_x
|
|
|
|
this.block_y = block_y
|
2016-12-31 16:35:14 -05:00
|
|
|
|
2018-09-11 23:27:01 -04:00
|
|
|
// ISU
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.install = function (host) {
|
2018-10-04 19:52:17 -04:00
|
|
|
this.guide = new this.Guide()
|
|
|
|
this.tool = new this.Tool()
|
|
|
|
this.interface = new this.Interface()
|
|
|
|
this.renderer = new this.Renderer()
|
|
|
|
this.picker = new this.Picker()
|
|
|
|
this.cursor = new this.Cursor()
|
2018-10-03 19:27:40 -04:00
|
|
|
host.appendChild(this.guide.el)
|
2017-11-04 20:52:05 -04:00
|
|
|
|
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()
|
|
|
|
this.guide.start()
|
|
|
|
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()
|
|
|
|
DOTGRID.interface.update()
|
|
|
|
DOTGRID.guide.update()
|
2016-12-31 12:18:01 -05:00
|
|
|
}
|
|
|
|
|
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 () {
|
2018-07-17 20:52:23 -04:00
|
|
|
this.set_zoom(1.0)
|
2019-01-08 21:36:26 -05:00
|
|
|
this.setSize({ width: 300, height: 300 })
|
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()))
|
|
|
|
this.guide.update()
|
|
|
|
})
|
2018-05-10 17:57:14 -04:00
|
|
|
}
|
|
|
|
|
2018-10-10 16:09:00 -04:00
|
|
|
function grab (base64, name) {
|
|
|
|
const link = document.createElement('a')
|
|
|
|
link.setAttribute('href', base64)
|
|
|
|
link.setAttribute('download', name)
|
2018-10-10 16:23:22 -04:00
|
|
|
link.dispatchEvent(new MouseEvent(`click`, { bubbles: true, cancelable: true, view: window }))
|
2018-01-12 03:09:26 -05:00
|
|
|
}
|
|
|
|
|
2018-10-10 16:09:00 -04:00
|
|
|
this.save = function () {
|
|
|
|
if (DOTGRID.tool.length() < 1) { console.warn('Nothing to save'); return }
|
2018-01-12 03:09:26 -05:00
|
|
|
|
2019-01-08 21:36:26 -05:00
|
|
|
this.renderer.toGRID(grab)
|
2018-08-17 17:34:24 -04:00
|
|
|
}
|
|
|
|
|
2018-10-10 16:09:00 -04:00
|
|
|
this.export = function () {
|
2018-10-04 19:52:17 -04:00
|
|
|
if (DOTGRID.tool.length() < 1) { console.warn('Nothing to export'); return }
|
2018-08-03 23:55:08 -04:00
|
|
|
|
2019-01-08 21:36:26 -05:00
|
|
|
this.renderer.toSVG(grab)
|
2018-01-12 03:09:26 -05:00
|
|
|
}
|
|
|
|
|
2018-10-10 16:09:00 -04:00
|
|
|
this.render = function () {
|
|
|
|
if (DOTGRID.tool.length() < 1) { console.warn('Nothing to render'); return }
|
|
|
|
|
|
|
|
const size = { width: DOTGRID.tool.settings.size.width * 2, height: DOTGRID.tool.settings.size.height * 2 }
|
2019-01-08 21:36:26 -05:00
|
|
|
this.renderer.toPNG(size, grab)
|
2018-08-17 17:34:24 -04:00
|
|
|
}
|
|
|
|
|
2018-05-07 01:03:35 -04:00
|
|
|
// Basics
|
2018-07-07 20:15:35 -04:00
|
|
|
|
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()
|
|
|
|
win.setSize((size.width + 100) * scale, (size.height + 100 + (ui ? 10 : 0)) * scale, true)
|
|
|
|
} catch (err) {
|
|
|
|
console.log('No window')
|
2018-08-17 15:58:01 -04:00
|
|
|
}
|
2018-05-07 00:24:01 -04:00
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.grid_x = size.width / 15
|
|
|
|
this.grid_y = size.height / 15
|
|
|
|
|
|
|
|
this.grid_width = this.tool.settings.size.width / this.grid_x
|
|
|
|
this.grid_height = this.tool.settings.size.height / this.grid_y
|
2018-05-07 00:51:58 -04:00
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.guide.resize(size)
|
2018-05-10 17:13:01 -04:00
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.interface.update()
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.guide.update()
|
2017-11-04 19:59:11 -04:00
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.set_zoom = function (scale) {
|
2019-01-08 21:36:26 -05:00
|
|
|
this.setSize({ width: this.tool.settings.size.width, height: this.tool.settings.size.height }, true, scale)
|
2018-08-17 15:58:01 -04:00
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
try {
|
|
|
|
webFrame.setZoomFactor(scale)
|
|
|
|
} catch (err) {
|
|
|
|
console.log('Cannot zoom')
|
2018-08-17 15:58:01 -04:00
|
|
|
}
|
2018-05-12 17:50:19 -04:00
|
|
|
}
|
|
|
|
|
2016-12-31 12:18:01 -05:00
|
|
|
// Draw
|
2018-07-07 20:15:35 -04:00
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.reset = function () {
|
|
|
|
this.tool.clear()
|
|
|
|
this.update()
|
2016-12-31 12:18:01 -05:00
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.clear = function () {
|
|
|
|
this.history.clear()
|
|
|
|
this.tool.reset()
|
|
|
|
this.reset()
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.guide.update()
|
|
|
|
DOTGRID.interface.update(true)
|
2017-11-07 16:40:13 -05: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
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
if (size.width == DOTGRID.tool.settings.size.width && size.height == DOTGRID.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
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.tool.settings.size.width = size.width
|
|
|
|
DOTGRID.tool.settings.size.height = size.height
|
2018-08-17 15:58:01 -04:00
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.grid_x = size.width / 15
|
|
|
|
DOTGRID.grid_y = size.height / 15
|
2018-08-17 15:58:01 -04:00
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.grid_width = DOTGRID.tool.settings.size.width / DOTGRID.grid_x
|
|
|
|
DOTGRID.grid_height = DOTGRID.tool.settings.size.height / DOTGRID.grid_y
|
2018-08-17 15:58:01 -04:00
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.guide.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
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.drag = function (e) {
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
2018-07-07 20:15:35 -04:00
|
|
|
|
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}`))
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.guide.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) {
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.guide.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())
|
2018-10-10 16:09:00 -04:00
|
|
|
e.clipboardData.setData('text/html', DOTGRID.renderer.svg_el.outerHTML)
|
|
|
|
e.clipboardData.setData('text/svg+xml', DOTGRID.renderer.svg_el.outerHTML)
|
2018-10-03 19:27:40 -04:00
|
|
|
e.preventDefault()
|
2018-07-26 02:56:06 -04:00
|
|
|
}
|
2018-02-07 01:44:18 -05:00
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.guide.update()
|
2018-02-07 01:44:18 -05:00
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.cut = function (e) {
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.guide.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()))
|
2018-10-10 16:09:00 -04:00
|
|
|
e.clipboardData.setData('text/html', DOTGRID.renderer.svg_el.outerHTML)
|
|
|
|
e.clipboardData.setData('text/svg+xml', DOTGRID.renderer.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-07-26 02:56:06 -04:00
|
|
|
}
|
2018-02-07 01:44:18 -05:00
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.guide.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-07-26 02:56:06 -04:00
|
|
|
}
|
2018-10-03 19:27:40 -04:00
|
|
|
e.preventDefault()
|
2018-05-07 21:32:06 -04:00
|
|
|
}
|
2018-07-07 20:15:35 -04:00
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.guide.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
|
|
|
|
2018-10-10 16:09:00 -04:00
|
|
|
const DOTGRID = new Dotgrid(300, 300, 20, 20, 4, 4)
|