瀏覽代碼

NESTile and ISurface are now event emitters. NESTile and by extension NESBank will emit 'data_changed' when there's a change to the data. CTRLPainter updated to utilize the new events.

dev-bank
Bryan Miller 5 年之前
父節點
當前提交
16df3f71d1
共有 4 個文件被更改,包括 70 次插入13 次删除
  1. +17
    -9
      app/js/ctrls/CTRLPainter.js
  2. +5
    -3
      app/js/ifaces/ISurface.js
  3. +25
    -0
      app/js/models/NESBank.js
  4. +23
    -1
      app/js/models/NESTile.js

+ 17
- 9
app/js/ctrls/CTRLPainter.js 查看文件

@@ -4,8 +4,6 @@ import GlobalEvents from "/app/js/common/EventCaller.js";
import Input from "/app/js/ui/Input.js";

import NESPalette from "/app/js/models/NESPalette.js";
//import NESTile from "/app/js/models/NESTile.js";
//import NESBank from "/app/js/models/NESBank.js";
import ISurface from "/app/js/ifaces/ISurface.js";

const EL_CANVAS_ID = "painter";
@@ -182,18 +180,28 @@ class CTRLPainter {
}).bind(this);
GlobalEvents.listen("set_app_palette", handle_setapppalette);

var handle_surface_data_changed = (function(){
RenderD();
}).bind(this);

var handle_change_surface = (function(surf){
if (!(surf instanceof ISurface)){
console.log("WARNING: Attempted to set painter to non-surface instance.");
return;
}
this.__surface = surf;
if (this.__palette === null && this.__surface.palette !== null){
this.__palette = this.__surface.palette;
} else if (this.__palette !== null && this.__surface.palette !== this.__palette){
this.__surface.palette = this.__palette;
if (surf !== this.__surface){
if (this.__surface !== null){
this.__surface.unlisten("data_changed", handle_surface_data_changed);
}
this.__surface = surf;
this.__surface.listen("data_changed", handle_surface_data_changed);
if (this.__palette === null && this.__surface.palette !== null){
this.__palette = this.__surface.palette;
} else if (this.__palette !== null && this.__surface.palette !== this.__palette){
this.__surface.palette = this.__palette;
}
this.center_surface();
}
this.center_surface();
RenderD();
}).bind(this);
GlobalEvents.listen("change_surface", handle_change_surface);
@@ -229,7 +237,7 @@ class CTRLPainter {
var sy = (e.isCombo) ? Math.floor((this.__brushLastPos[1] - this.__offset[1]) * (1.0 / this.__scale)) : y;
if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
LineToSurface(sx, sy, x, y, this.__brushColor, this.__brushPalette);
RenderD();
//RenderD();
}
}
}

+ 5
- 3
app/js/ifaces/ISurface.js 查看文件

@@ -1,8 +1,10 @@
import Utils from "/app/js/common/Utils.js"
import {EventCaller} from "/app/js/common/EventCaller.js"


export default class ISurface{
constructor(){}
export default class ISurface extends EventCaller{
constructor(){
super();
}

get width(){return 0;}
get height(){return 0;}

+ 25
- 0
app/js/models/NESBank.js 查看文件

@@ -38,9 +38,18 @@ export default class NESBank extends ISurface{
this.__RP = []; // Right Patterns (Backgrounds)
this.__AccessMode = 2; // 0 = Sprites only | 1 = BG only | 2 = Sprites and BG

var handle_datachanged = function(side){
if ((side == 0 && (this.__AccessMode == 0 || this.__AccessMode == 2)) ||
(side == 1 && (this.__AccessMode == 1 || this.__AccessMode == 2))){
this.emit("data_changed");
}
}

for (var i=0; i < 256; i++){
this.__LP.push(new NESTile());
this.__LP[i].listen("data_changed", handle_datachanged.bind(this, 0));
this.__RP.push(new NESTile());
this.__RP[i].listen("data_changed", handle_datachanged.bind(this, 1));
}

this.__palette = null;
@@ -81,6 +90,22 @@ export default class NESBank extends ISurface{
return buff;
}

set chr(buff){
if (!(buff instanceof Uint8Array))
throw new TypeError("Expected Uint8Array buffer.");
if (buff.length !== 8192)
throw new RangeError("Data buffer has invalid byte length.");
var offset = 0;
this.__LP.forEach((i) => {
i.chr = buff.slice(offset, offset+15);
offset += 16;
});
this.__RP.forEach((i) => {
i.chr = buff.slice(offset, offset+15);
offset += 16;
});
}

get palette(){return this.__palette;}
set palette(p){
if (p !== null && !(p instanceof NESPalette))

+ 23
- 1
app/js/models/NESTile.js 查看文件

@@ -1,4 +1,5 @@
import Utils from "/app/js/common/Utils.js";
import {EventCaller} from "/app/js/common/EventCaller.js";
import NESPalette from "/app/js/models/NESPalette.js";


@@ -23,9 +24,13 @@ function BitMask(offset, inv){
}


var BLOCK_CHANGE_EMIT = false; // This will block the "data_changed" event when class is processing
// lots of changes.

export default class NESTile{

export default class NESTile extends EventCaller{
constructor(){
super();
this.__paletteIndex = 0;
this.__data = new Uint8Array(16);
}
@@ -72,6 +77,8 @@ export default class NESTile{
} else {
obj.__data[8+dindex] &= BitMask(bitoffset, true);
}
if (!BLOCK_CHANGE_EMIT)
obj.emit("data_changed");
return true;
}
});
@@ -91,6 +98,15 @@ export default class NESTile{
return new Uint8Array(this.__data);
}

set chr(buff){
if (!(buff instanceof Uint8Array))
throw new TypeError("Expected Uint8Array buffer");
if (buff.length !== 16)
throw new RangeError("Buffer contains invalid byte length.");
this.__data = new Uint8Array(buff);
this.emit("data_changed");
}

get base64(){
var b = ""
for (var i = 0; i < this.__data.length; i++) {
@@ -111,6 +127,7 @@ export default class NESTile{
}
this.__data = bytes;
this.__paletteIndex = b.charCodeAt(b.length-1);
this.emit("data_changed");
}


@@ -122,6 +139,7 @@ export default class NESTile{
throw new RangeError("Palette index out of bounds.");
}
this.__paletteIndex = pi;
this.emit("data_changed");
}

setPixelIndex(x, y, ci){
@@ -146,6 +164,7 @@ export default class NESTile{
if (flag >= 1 && flag <= 3){
var oldData = this.__data;
var newData = new Uint8Array(16);
BLOCK_CHANGE_EMIT = true;
for (var x = 0; x < 8; x++){
for (var y = 0; y < 8; y++){
this.__data = oldData;
@@ -158,6 +177,8 @@ export default class NESTile{
);
}
}
BLOCK_CHANGE_EMIT = false;
this.emit("data_changed");
}
return this;
}
@@ -171,6 +192,7 @@ export default class NESTile{
if (!(t instanceof NESTile))
throw new TypeError("Expected NESTile object.");
this.__data.set(t.__data);
this.emit("data_changed");
return this;
}


Loading…
取消
儲存