Implemented undo/redo
This commit is contained in:
@@ -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"){
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
40
sources/scripts/lib/history.js
Normal file
40
sources/scripts/lib/history.js
Normal file
@@ -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; }
|
||||
}
|
||||
Reference in New Issue
Block a user