Browse Source

ISurface and NESBank updated to handle undo/redo history. Fixed a base64 issue in NESBank.

dev
Bryan Miller 5 years ago
parent
commit
c4efdb74f0
2 changed files with 70 additions and 0 deletions
  1. +13
    -0
      app/js/ifaces/ISurface.js
  2. +57
    -0
      app/js/models/NESBank.js

+ 13
- 0
app/js/ifaces/ISurface.js View File

@@ -4,11 +4,15 @@ import {EventCaller} from "/app/js/common/EventCaller.js"
export default class ISurface extends EventCaller{
constructor(){
super();
this.__historyLength = 10;
}

get width(){return 0;}
get height(){return 0;}
get length(){return 0;}
get historyLength(){return this.__historyLength;}
get undos(){return 0;}
get redos(){return 0;}

get coloridx(){
return new Proxy(this, {
@@ -39,6 +43,15 @@ export default class ISurface extends EventCaller{
copy(b){return this;}
clone(){return new ISurface();}

snapshot(){return this;}
undo(){return this;}
redo(){return this;}
clearUndos(){return this;}
clearRedos(){return this;}
clearHistory(){
return this.clearUndos().clearRedos();
}

getColor(x, y){
return this.__default_pi[4];
}

+ 57
- 0
app/js/models/NESBank.js View File

@@ -87,7 +87,11 @@ export default class NESBank extends ISurface{
this.__AccessMode = NESBank.ACCESSMODE_8K;
this.__AccessOffset = 0;

this.__undos = []; // Holds Base64 snapshots of the bank.
this.__redos = []; // Holds Base64 snapshots of work undone.

this.__emitsEnabled = true;
this.snapshot = Utils.debounce(this.snapshot.bind(this), 250);

var handle_datachanged = Utils.debounce((function(side, idx){
var sendEmit = false;
@@ -272,6 +276,9 @@ export default class NESBank extends ISurface{
if (b.length !== 8192){
throw new Error("Base64 string contains invalid byte count.");
}
b = new Uint8Array(b.split("").map(function(c){
return c.charCodeAt(0);
}));
this.chr = b;
}

@@ -296,6 +303,10 @@ export default class NESBank extends ISurface{
}
get length(){return this.width * this.height;}

get undos(){return this.__undos.length;}
get redos(){return this.__redos.length;}


get coloridx(){
return new Proxy(this, {
get:function(obj, prop){
@@ -538,6 +549,52 @@ export default class NESBank extends ISurface{
}
return this;
}


snapshot(){
var snap = this.base64;
if (this.__undos.length === this.__historyLength){
this.__undos.pop();
}
this.__undos.splice(0,0,snap);
return this;
}

undo(){
if (this.__undos.length > 0){
var usnap = this.__undos.splice(0, 1)[0];
var rsnap = this.base64;
this.base64 = usnap;
if (this.__redos.length === this.__historyLength){
this.__redos.pop();
}
this.__redos.splice(0,0,rsnap);
}
return this;
}

redo(){
if (this.__redos.length > 0){
var rsnap = this.__redos.splice(0,1)[0];
var usnap = this.base64;
this.base64 = rsnap;
if (this.__undos.length === this.__historyLength){
this.__undos.pop();
}
this.__undos.splice(0,0,usnap);
}
return this;
}

clearUndos(){
this.__undos = [];
return this;
}

clearRedos(){
this.__redos = [];
return this;
}
}



Loading…
Cancel
Save