All files / core panel.ts

90.91% Statements 50/55
66.67% Branches 12/18
94.44% Functions 17/18
90.91% Lines 50/55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139    3x 3x                                       3x 13x 13x         13x 13x 13x   13x 3x       13x       179258x       181810x       1x       1x       2x       3x 3x 2x 1x 1x 1x         2x 1x             3x 3x 2x 1x             2x 2x 1x 1x 1x         2x 2x 2x 2x 2x         20x   20x 20x 2689x 2689x     20x       20x 20x 20x         2689x       2521x       168x    
import Board from './board';
import { Sequence } from '../types';
import { Exception } from '../utils/exception';
import { Event } from '../utils/event';
import { Scroller } from './scrollers/scroller';
 
export interface PanelParameters  {
  /** The board for which the panel operates on */
  board: Board,
  /** Increment at each frame */
  increment: number,
  /** The width of the panel in bits displayed */
  width: number,
  /** Whether the panel animation should be reverse */
  reverse: boolean,
  scroller: Scroller
}
 
/**
 * The panel deals with the displaying logic. 
 * You can see it as a viewport moving through a board. 
 * It has control over starting, stopping, pausing, resuming, seeking, ticking.
 */
export class Panel {
  readonly CLASS_NAME = Panel.name;
  private _initiated: boolean = false;
 
  private _params: PanelParameters;
 
  constructor(params: PanelParameters) {
    this._params =  params;
    this._initiated = true;
    this.updateCurrentSequence();
 
    this._params.board.PropertyChange.on(() => {
      this.updateCurrentSequence();
    })
  }
 
  protected readonly onNewSequence = new Event<{sequence: Sequence}>();
  public get NewSequence() { return this.onNewSequence.expose(); }
 
  public get width() {
    return this._params.width;
  }
 
  public get board() {
    return this._params.board;
  }
 
  public get increment() {
    return this._params.increment;
  }
 
  public get reverse() {
    return this._params.reverse;
  }
 
  public get scroller() {
    return this._params.scroller;
  }
  
  public set width(value: number) {
    const widthDescription = Exception.getDescriptionForProperty(this.CLASS_NAME, 'width');
    Exception.throwIfNull(value, widthDescription);
    Exception.throwIfNegative(value, widthDescription);
    Eif (this._params.width != value) {
      this._params.width = value;
      this.updateCurrentSequence();
    }
  }
 
  public set board(value: Board) {
    Exception.throwIfNull(value, Exception.getDescriptionForProperty(this.CLASS_NAME, 'board'));
    Iif (this._params.board != value) {
      this._params.board = value;
      this.updateCurrentSequence(); 
    }
  }
 
  public set increment(value: number) {
    const fpsDescription = Exception.getDescriptionForProperty(this.CLASS_NAME, 'fps');
    Exception.throwIfNull(value, fpsDescription);
    Exception.throwIfNegative(value, fpsDescription);
    Iif (this._params.increment != value) {
      this._params.increment = value;
      this.updateCurrentSequence();
    }
  }
 
  public set reverse(value: boolean) {
    const reverseDescription = Exception.getDescriptionForProperty(this.CLASS_NAME, 'reverse');
    Exception.throwIfNull(value, reverseDescription);
    Eif (value != this._params.reverse) {
      this._params.reverse = value;
      this.updateCurrentSequence();
    }
  }
 
  public set scroller(value: Scroller) {
    const reverseDescription = Exception.getDescriptionForProperty(this.CLASS_NAME, 'reverse');
    Exception.throwIfNull(value, reverseDescription);
    Eif (value != this._params.scroller) {
      this._params.scroller = value;
      this.updateCurrentSequence();
    }
  }
 
  public GetCurrentSequence(): Sequence {
    let sequence: Sequence = [];
 
    let panelIndex = 0;
    for (let i = 0; i <= this._params.scroller.loopEndIndex(this); i++) {
      panelIndex = this._tickPanelIndex(panelIndex);
      sequence.push(this._params.scroller.generatePanelFrameAtIndex(panelIndex, this));
    }
 
    return sequence;
  }
 
  public updateCurrentSequence() {
    Eif (this._initiated) {
      const sequence = this.GetCurrentSequence();
      this.onNewSequence.trigger({ sequence });
    }
  }
 
  private _tickPanelIndex(index: number): number {
    return this._params.reverse ? this._decrementIndex(index) : this._incrementIndex(index);
  }
 
  private _incrementIndex(index: number): number {
    return index >= this._params.scroller.loopEndIndex(this) ? 0 : index + this._params.increment;
  }
 
  private _decrementIndex(index: number): number {
    return index === 0 ? this._params.scroller.loopEndIndex(this) : index - this._params.increment;
  }
};