led-matrix-ts
Version:
Highly customizable led matrix for the browser
106 lines • 3.93 kB
JavaScript
import { Exception } from '../utils/exception';
import { Event } from '../utils/event';
export class Panel {
constructor(params) {
this.CLASS_NAME = Panel.name;
this._initiated = false;
this._latestCurrentSequenceUuid = 0;
this.onNewSequence = new Event();
this._params = params;
this._initiated = true;
this.updateCurrentSequence();
this._params.board.PropertyChange.on(() => {
this.updateCurrentSequence();
});
}
get NewSequence() { return this.onNewSequence.expose(); }
get width() {
return this._params.width;
}
get board() {
return this._params.board;
}
get increment() {
return this._params.increment;
}
get reverse() {
return this._params.reverse;
}
get scroller() {
return this._params.scroller;
}
set width(value) {
const widthDescription = Exception.getDescriptionForProperty(this.CLASS_NAME, 'width');
Exception.throwIfNull(value, widthDescription);
Exception.throwIfNegative(value, widthDescription);
if (this._params.width != value) {
this._params.width = value;
this.updateCurrentSequence();
}
}
set board(value) {
Exception.throwIfNull(value, Exception.getDescriptionForProperty(this.CLASS_NAME, 'board'));
if (this._params.board != value) {
this._params.board = value;
this.updateCurrentSequence();
}
}
set increment(value) {
const fpsDescription = Exception.getDescriptionForProperty(this.CLASS_NAME, 'fps');
Exception.throwIfNull(value, fpsDescription);
Exception.throwIfNegative(value, fpsDescription);
if (this._params.increment != value) {
this._params.increment = value;
this.updateCurrentSequence();
}
}
set reverse(value) {
const reverseDescription = Exception.getDescriptionForProperty(this.CLASS_NAME, 'reverse');
Exception.throwIfNull(value, reverseDescription);
if (value != this._params.reverse) {
this._params.reverse = value;
this.updateCurrentSequence();
}
}
set scroller(value) {
const reverseDescription = Exception.getDescriptionForProperty(this.CLASS_NAME, 'reverse');
Exception.throwIfNull(value, reverseDescription);
if (value != this._params.scroller) {
this._params.scroller = value;
this.updateCurrentSequence();
}
}
GetCurrentSequence() {
return new Promise((resolve) => {
let sequence = [];
let panelIndex = 0;
for (let i = 0; i <= (this.increment == 0 ? 0 : this._params.scroller.loopEndIndex(this) / this.increment); i++) {
sequence.push(this._params.scroller.generatePanelFrameAtIndex(panelIndex, this));
panelIndex = this._tickPanelIndex(panelIndex);
}
resolve(sequence);
});
}
updateCurrentSequence() {
if (this._initiated) {
this._latestCurrentSequenceUuid += 1;
const currentUuid = this._latestCurrentSequenceUuid;
this.GetCurrentSequence().then((sequence) => {
if (currentUuid == this._latestCurrentSequenceUuid && sequence[0].length > 0) {
this.onNewSequence.trigger(sequence);
}
});
}
}
_tickPanelIndex(index) {
return this._params.reverse ? this._decrementIndex(index) : this._incrementIndex(index);
}
_incrementIndex(index) {
return index >= this._params.scroller.loopEndIndex(this) ? 0 : index + this._params.increment;
}
_decrementIndex(index) {
return index === 0 ? this._params.scroller.loopEndIndex(this) : index - this._params.increment;
}
}
;
//# sourceMappingURL=panel.js.map