From d85536e72331ef064dffc71a078b674323cb14a5 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Fri, 16 Nov 2018 19:37:48 +1200 Subject: Improved save/load --- cli/terminal.js | 19 ++--- desktop/core/lib/c.js | 2 +- desktop/core/pico.js | 4 +- desktop/sources/index.html | 8 +-- desktop/sources/scripts/cursor.js | 3 +- desktop/sources/scripts/events.js | 14 +++- desktop/sources/scripts/source.js | 64 +++++++++++++++++ desktop/sources/scripts/terminal.js | 134 +++++++++++++++--------------------- 8 files changed, 147 insertions(+), 101 deletions(-) create mode 100644 desktop/sources/scripts/source.js diff --git a/cli/terminal.js b/cli/terminal.js index 69a604e..5ea0682 100644 --- a/cli/terminal.js +++ b/cli/terminal.js @@ -82,18 +82,13 @@ function Terminal (pico) { this.log(this.isPaused ? "Paused" : "Unpaused") } - this.load = function (path) { - const terminal = this - var fs = require('fs') - fs.readFile(path, 'utf8', function (err, data) { - if (err) throw err - const w = data.split('\n')[0].length - const h = data.split('\n').length - terminal.log(`Loaded: ${path} ${w}x${h}`) - pico.load(w, h, data) - terminal._grid.width = w - terminal.update() - }) + this.load = function (data) { + const w = data.split('\n')[0].length + const h = data.split('\n').length + terminal.log(`Loaded: ${path} ${w}x${h}`) + pico.load(w, h, data) + terminal._grid.width = w + terminal.update() } this.log = function (msg) { diff --git a/desktop/core/lib/c.js b/desktop/core/lib/c.js index 25e8232..f438530 100644 --- a/desktop/core/lib/c.js +++ b/desktop/core/lib/c.js @@ -6,7 +6,7 @@ function FnC (pico, x, y, passive) { FnBase.call(this, pico, x, y, 'c', passive) this.name = 'clock' - this.info = 'A sync value.' + this.info = 'Adds a constant value southward.' this.ports.push({ x: 0, y: 1, output: true }) this.ports.push({ x: 1, y: 0, input: true }) this.ports.push({ x: -1, y: 0, input: true }) diff --git a/desktop/core/pico.js b/desktop/core/pico.js index c63e3b4..1c7d392 100644 --- a/desktop/core/pico.js +++ b/desktop/core/pico.js @@ -199,15 +199,15 @@ function Pico (w, h) { // Tools this.lines = function () { - const origin = this.s.replace(/[^0-9a-z]/gi, '.') const a = [] for (let y = 0; y < this.h; y++) { const from = y * this.w const to = this.w - a.push(origin.substr(from, to)) + a.push(this.s.substr(from, to)) } return a } + this.output = function () { return this.lines().reduce((acc, val) => { return `${acc}${val}\n` diff --git a/desktop/sources/index.html b/desktop/sources/index.html index e1f1662..90f527a 100644 --- a/desktop/sources/index.html +++ b/desktop/sources/index.html @@ -2,7 +2,6 @@ - @@ -33,9 +32,10 @@ terminal.controller.add("default","*","Reset",() => { terminal.reset(); },"CmdOrCtrl+Backspace") terminal.controller.add("default","*","Quit",() => { app.exit(); },"CmdOrCtrl+Q") - terminal.controller.add("default","File","New",() => { terminal.new(); },"CmdOrCtrl+N") - terminal.controller.add("default","File","Save",() => { terminal.save(); },"CmdOrCtrl+S") - terminal.controller.add("default","File","Open",() => { terminal.open(); },"CmdOrCtrl+O") + terminal.controller.add("default","File","New",() => { terminal.source.new(); },"CmdOrCtrl+N") + terminal.controller.add("default","File","Save",() => { terminal.source.save(); },"CmdOrCtrl+S") + terminal.controller.add("default","File","Open",() => { terminal.source.open(); },"CmdOrCtrl+O") + terminal.controller.add("default","File","Revert",() => { terminal.source.revert(); },"CmdOrCtrl+W") terminal.controller.add("default","Program","Play/Pause",() => { terminal.pause(); },"Space") terminal.controller.add("default","Program","Incr. Speed",() => { terminal.modSpeed(10); },">") diff --git a/desktop/sources/scripts/cursor.js b/desktop/sources/scripts/cursor.js index 0584c4f..7f05566 100644 --- a/desktop/sources/scripts/cursor.js +++ b/desktop/sources/scripts/cursor.js @@ -64,8 +64,7 @@ function Cursor (terminal) { this.inspect = function () { if (this.w > 1 || this.h > 1) { return 'multi' } - - const g = pico.glyphAt(this.x, this.y) + const g = pico.glyphAt(this.x, this.y).toLowerCase() return pico.docs[g] ? pico.docs[g] : 'empty' } diff --git a/desktop/sources/scripts/events.js b/desktop/sources/scripts/events.js index 482768f..f203f06 100644 --- a/desktop/sources/scripts/events.js +++ b/desktop/sources/scripts/events.js @@ -1,3 +1,5 @@ +'use strict' + window.addEventListener('dragover', function (e) { e.stopPropagation() e.preventDefault() @@ -9,9 +11,15 @@ window.addEventListener('drop', function (e) { e.stopPropagation() const file = e.dataTransfer.files[0] - const name = file.path ? file.path : file.name + const path = file.path ? file.path : file.name - if (!name || name.indexOf('.pico') < 0) { console.log('Pico', 'Not a pico file'); return } + if (!path || path.indexOf('.pico') < 0) { console.log('Pico', 'Not a pico file'); return } - terminal.load(name) + terminal.source.path = path + terminal.source.read(path) }) + +window.onresize = (event) => { + const marginTop = (window.innerHeight - (terminal.size.height * terminal.size.ratio)) / 2 + terminal.el.style.marginTop = (marginTop - 20) + 'px' +} diff --git a/desktop/sources/scripts/source.js b/desktop/sources/scripts/source.js new file mode 100644 index 0000000..8ff0689 --- /dev/null +++ b/desktop/sources/scripts/source.js @@ -0,0 +1,64 @@ +'use strict' + +function Source (pico, terminal) { + this.path = null + + this.new = function () { + pico.clear() + terminal.log('New') + this.path = null + } + + this.open = function () { + terminal.log('Open') + + let paths = dialog.showOpenDialog(app.win, { properties: ['openFile'] }) + if (!paths) { console.log('Nothing to load') } + if (!terminal.source.validate(paths[0])) { console.log('Invalid file') } + this.path = paths[0] + this.read(paths[0]) + } + + this.save = function () { + terminal.log('Save') + + if (this.path) { + this.write(this.path) + } else { + dialog.showSaveDialog((path) => { + if (path === undefined) { return } + if (path.indexOf('.') < 0) { path += '.pico' } + terminal.source.write(path) + terminal.source.path = path + }) + } + } + + this.revert = function () { + terminal.log('Revert') + this.read(this.path) + } + + // I/O + + this.write = function (path) { + fs.writeFile(path, `${pico}`, (err) => { + if (err) { alert('An error ocurred updating the file' + err.message); console.log(err) } + }) + } + + this.read = function (path) { + fs.readFile(path, 'utf8', function (err, data) { + const fs = require('fs') + if (err) throw err + if (!terminal.source.validate(data)) { console.warn('Invalid File'); return } + terminal.load(data.trim()) + }) + } + + this.validate = function (data) { + return true + } +} + +module.exports = Source diff --git a/desktop/sources/scripts/terminal.js b/desktop/sources/scripts/terminal.js index 13e1477..00cfbdb 100644 --- a/desktop/sources/scripts/terminal.js +++ b/desktop/sources/scripts/terminal.js @@ -2,10 +2,13 @@ function Terminal (pico) { const Cursor = require('./cursor') + const Source = require('./source') const QQQ = require('./qqq') this.qqq = new QQQ(this) this.cursor = new Cursor(this) + this.source = new Source(pico, this) + this.controller = new Controller() this.theme = new Theme({ background: '#111111', f_high: '#ffffff', f_med: '#777777', f_low: '#333333', f_inv: '#000000', b_high: '#ffb545', b_med: '#72dec2', b_low: '#444444', b_inv: '#ffffff' }) @@ -13,6 +16,7 @@ function Terminal (pico) { this.el = document.createElement('canvas') this.isPaused = false this.tile = { w: 20, h: 30 } + this.size = { width: this.tile.w * pico.w, height: this.tile.h * pico.h + (this.tile.h * 3), ratio: 0.5 } this.debug = 'hello.' this.timer = null @@ -34,18 +38,6 @@ function Terminal (pico) { this.setSpeed(120) } - this.setSpeed = function (bpm) { - this.bpm = clamp(bpm, 60, 300) - this.log(`Changed speed to ${this.bpm}.`) - const ms = (60000 / bpm) / 4 - clearInterval(this.timer) - this.timer = setInterval(() => { this.run() }, ms) - } - - this.modSpeed = function (mod = 0) { - this.setSpeed(this.bpm + mod) - } - this.run = function () { if (this.isPaused) { return } @@ -62,33 +54,27 @@ function Terminal (pico) { this.log(this.isPaused ? 'Paused' : 'Unpaused') } - this.open = function () { - console.log('Open[TODO]') - let paths = dialog.showOpenDialog(app.win, { properties: ['openFile'] }) - if (!paths) { console.log('Nothing to load') } + this.load = function (data) { + const w = data.split('\n')[0].length + const h = data.split('\n').length + this.log(`Loading ${w}x${h}`) + pico.load(w, h, data) + this.resize() + this.update() } - this.save = function () { - dialog.showSaveDialog((fileName) => { - if (fileName === undefined) { return } - fs.writeFile(fileName + '.pico', `${pico}`, (err) => { - if (err) { alert('An error ocurred updating the file' + err.message); console.log(err) } - }) - }) + // + + this.setSpeed = function (bpm) { + this.bpm = clamp(bpm, 60, 300) + this.log(`Changed speed to ${this.bpm}.`) + const ms = (60000 / bpm) / 4 + clearInterval(this.timer) + this.timer = setInterval(() => { this.run() }, ms) } - this.load = function (path) { - const terminal = this - var fs = require('fs') - fs.readFile(path, 'utf8', function (err, data) { - if (err) throw err - const w = data.split('\n')[0].length - const h = data.split('\n').length - terminal.log(`Loaded: ${path} ${w}x${h}`) - pico.load(w, h, data) - terminal.resize() - terminal.update() - }) + this.modSpeed = function (mod = 0) { + this.setSpeed(this.bpm + mod) } // @@ -103,8 +89,42 @@ function Terminal (pico) { this.drawInterface() } - this.new = function () { - pico.clear() + this.reset = function () { + this.theme.reset() + } + + // + + this.isCursor = function (x, y) { + return x === this.cursor.x && y === this.cursor.y + } + + this.isSelection = function (x, y) { + if (x >= this.cursor.x && x < this.cursor.x + this.cursor.w && y >= this.cursor.y && y < this.cursor.y + this.cursor.h) { + return true + } + return false + } + + this.findPorts = function () { + const h = {} + const fns = pico.findFns() + for (const id in fns) { + const g = fns[id] + if (pico.isLocked(g.x, g.y)) { continue } + for (const id in g.ports) { + const port = g.ports[id] + h[`${g.x + port.x}:${g.y + port.y}`] = port.output ? 2 : port.bang ? 1 : 3 + } + } + + return h + } + + // + + this.context = function () { + return this.el.getContext('2d') } this.drawProgram = function () { @@ -152,36 +172,6 @@ function Terminal (pico) { } } - this.isCursor = function (x, y) { - return x === this.cursor.x && y === this.cursor.y - } - - this.isSelection = function (x, y) { - if (x >= this.cursor.x && x < this.cursor.x + this.cursor.w && y >= this.cursor.y && y < this.cursor.y + this.cursor.h) { - return true - } - return false - } - - this.findPorts = function () { - const h = {} - const fns = pico.findFns() - for (const id in fns) { - const g = fns[id] - if (pico.isLocked(g.x, g.y)) { continue } - for (const id in g.ports) { - const port = g.ports[id] - h[`${g.x + port.x}:${g.y + port.y}`] = port.output ? 2 : port.bang ? 1 : 3 - } - } - - return h - } - - this.context = function () { - return this.el.getContext('2d') - } - this.clear = function () { const ctx = this.context() ctx.clearRect(0, 0, this.size.width, this.size.height) @@ -222,12 +212,7 @@ function Terminal (pico) { ctx.fillText(styles.isCursor && g === '.' ? (!this.isPaused ? '@' : '~') : g, (x + 0.5) * this.tile.w, (y + 1) * this.tile.h) } - this.reset = function () { - this.theme.reset() - } - this.resize = function () { - this.size = { width: this.tile.w * pico.w, height: this.tile.h * pico.h + (this.tile.h * 3), ratio: 0.5 } this.el.width = this.size.width this.el.height = this.size.height + this.tile.h this.el.style.width = (this.size.width * this.size.ratio) + 'px' @@ -242,11 +227,6 @@ function Terminal (pico) { win.setSize(width, height, true) } - window.onresize = (event) => { - const marginTop = (window.innerHeight - (this.size.height * this.size.ratio)) / 2 - this.el.style.marginTop = (marginTop - 20) + 'px' - } - function clamp (v, min, max) { return v < min ? min : v > max ? max : v } } -- cgit v1.2.3