From cfc49dc435fa910565545c585e6742c273f78a10 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 1 Feb 2018 09:21:59 +1300 Subject: [PATCH] Implemented undo/redo --- LAYOUT.md | 1 + README.md | 3 ++- main.js | 2 -- sources/index.html | 2 +- sources/scripts/dotgrid.js | 34 ++++++++++++++--------------- sources/scripts/history.js | 40 ---------------------------------- sources/scripts/lib/history.js | 40 ++++++++++++++++++++++++++++++++++ 7 files changed, 60 insertions(+), 62 deletions(-) delete mode 100644 sources/scripts/history.js create mode 100644 sources/scripts/lib/history.js diff --git a/LAYOUT.md b/LAYOUT.md index 3cb774b..2d6d110 100644 --- a/LAYOUT.md +++ b/LAYOUT.md @@ -10,6 +10,7 @@ - Copy: `CmdOrCtrl+C` - Paste: `CmdOrCtrl+V` - Undo: `CmdOrCtrl+Z` +- Redo: `CmdOrCtrl+Shift+Z` - Delete: `Backspace` - Move Up: `Up` - Move Down: `Down` diff --git a/README.md b/README.md index 37f369c..b22041f 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Clicking on the canvas will insert control points, up to 3CPs. CPs can be moved - Copy: `CmdOrCtrl+C` - Paste: `CmdOrCtrl+V` - Undo: `CmdOrCtrl+Z` +- Redo: `CmdOrCtrl+Shift+Z` - Delete: `Backspace` - Move Up: `Up` - Move Down: `Down` @@ -51,7 +52,7 @@ Clicking on the canvas will insert control points, up to 3CPs. CPs can be moved - Control Points: `J` - Expert Mode: `:` - + ## Extras diff --git a/main.js b/main.js index 8be41ae..d5e2c66 100644 --- a/main.js +++ b/main.js @@ -32,8 +32,6 @@ app.on('ready', () => app.win = new BrowserWindow({width: 400, height: 420, minWidth: 400, minHeight: 420, backgroundColor:"#000", frame:false, autoHideMenuBar: true, icon: __dirname + '/icon.ico'}) app.win.loadURL(`file://${__dirname}/sources/index.html`); - - app.win.toggleDevTools(); app.win.on('closed', () => { win = null diff --git a/sources/index.html b/sources/index.html index 8dfff92..a30a6b4 100644 --- a/sources/index.html +++ b/sources/index.html @@ -2,6 +2,7 @@ + @@ -13,7 +14,6 @@ - diff --git a/sources/scripts/dotgrid.js b/sources/scripts/dotgrid.js index ef73b9e..662acc2 100644 --- a/sources/scripts/dotgrid.js +++ b/sources/scripts/dotgrid.js @@ -241,14 +241,12 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,lineca } this.segments = this.history.prev(); - console.log(this.history.a) this.draw(); } this.redo = function() { this.segments = this.history.next(); - console.log(this.history.a) this.draw(); } @@ -281,6 +279,22 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,lineca this.draw(); } + this.translate = function(t) + { + for(id in dotgrid.segments){ + var segment = dotgrid.segments[id]; + if(segment.from && segment.from.is_equal(dotgrid.translation.from)){ segment.from = new Pos(-dotgrid.translation.to.x,dotgrid.translation.to.y)} + if(segment.to && segment.to.is_equal(dotgrid.translation.from)){ segment.to = new Pos(-dotgrid.translation.to.x,dotgrid.translation.to.y)} + if(segment.end && segment.end.is_equal(dotgrid.translation.from)){ segment.end = new Pos(-dotgrid.translation.to.x,dotgrid.translation.to.y)} + } + + dotgrid.history.push(dotgrid.segments); + dotgrid.translation = null; + dotgrid.reset(); + + dotgrid.draw(); + } + // STROKE this.draw_line = function() @@ -449,22 +463,6 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,lineca return false; } - this.translate = function(t) - { - for(id in dotgrid.segments){ - var segment = dotgrid.segments[id]; - if(segment.from && segment.from.is_equal(dotgrid.translation.from)){ segment.from = new Pos(-dotgrid.translation.to.x,dotgrid.translation.to.y)} - if(segment.to && segment.to.is_equal(dotgrid.translation.from)){ segment.to = new Pos(-dotgrid.translation.to.x,dotgrid.translation.to.y)} - if(segment.end && segment.end.is_equal(dotgrid.translation.from)){ segment.end = new Pos(-dotgrid.translation.to.x,dotgrid.translation.to.y)} - } - - dotgrid.translation = null; - dotgrid.reset(); - dotgrid.history.push(dotgrid.segments); - - dotgrid.draw(); - } - this.preview = function(operation) { if(from && to && operation == "line"){ diff --git a/sources/scripts/history.js b/sources/scripts/history.js deleted file mode 100644 index 91bc1a1..0000000 --- a/sources/scripts/history.js +++ /dev/null @@ -1,40 +0,0 @@ -function History() -{ - this.index = 0; - this.a = []; - - this.push = function(data) - { - var d = data.slice(0); - if(this.index != this.a.length){ - this.a = this.a.slice(0,this.index); - } - this.a.push(d); - this.index = this.a.length; - console.log(`history: ${this.index}/${this.a.length}`) - } - - this.pop = function() - { - console.log(`history: ${this.index}/${this.a.length}`) - return this.a.pop(); - } - - this.prev = function() - { - this.index = clamp(this.index-1,0,this.a.length-1); - console.log(`history: ${this.index}/${this.a.length}`) - return this.a[this.index].slice(0); - } - - this.next = function() - { - this.index = clamp(this.index+1,0,this.a.length-1); - console.log(`history: ${this.index}/${this.a.length}`) - return this.a[this.index].slice(0); - } - - function copy(data){ return JSON.parse(JSON.stringify(data)); } - - function clamp(v, min, max) { return v < min ? min : v > max ? max : v; } -} \ No newline at end of file diff --git a/sources/scripts/lib/history.js b/sources/scripts/lib/history.js new file mode 100644 index 0000000..100b1fe --- /dev/null +++ b/sources/scripts/lib/history.js @@ -0,0 +1,40 @@ +function History() +{ + this.index = 0; + this.a = []; + + this.push = function(data) + { + if(this.index < this.a.length-1){ + this.fork(); + } + this.index = this.a.length; + this.a = this.a.slice(0,this.index); + this.a.push(copy(data)); + } + + this.fork = function() + { + this.a = this.a.slice(0,this.index+1); + } + + this.pop = function() + { + return this.a.pop(); + } + + this.prev = function() + { + this.index = clamp(this.index-1,0,this.a.length-1); + return copy(this.a[this.index]); + } + + this.next = function() + { + this.index = clamp(this.index+1,0,this.a.length-1); + return copy(this.a[this.index]); + } + + function copy(data){ return data.slice(0); } + function clamp(v, min, max) { return v < min ? min : v > max ? max : v; } +} \ No newline at end of file