All files / src/model numberLabel.ts

100% Statements 45/45
100% Branches 5/5
100% Functions 15/15
100% Lines 44/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199                                                                                                                                                                              22x 33x                 33x   33x 33x 33x 33x 33x 33x 33x                     3617x 3613x 3613x 3613x         5x 1x 1x 1x         13x       6x       3620x       4x       14x 12x 12x 12x 12x   2x   14x       8x       8x       3784x       3784x       7419x       3776x 3776x 7646x   3776x   3776x       3614x 3614x 3614x 129x 129x        
/**
 * {@link NumberLabel} の状態.
 *
 * 'enabled': 値の変更を許可します.
 *
 * 'disabled': 値を変更しようとしても無視します.
 */
export type NumberLabelStatus = 'enabled' | 'disabled'
 
/**
 * ラベル付き数値を制御、描画します.
 */
export interface NumberLabel {
  /**
   * 値が変化したとき発火されるトリガ
   */
  readonly onValue: g.Trigger<number>
  /**
   * 描画に使用するフォントを取得します.
   */
  readonly font: g.Font
 
  /**
   * 表示する数値の桁数を取得します.
   */
  readonly digit: number
 
  /**
   * 数値に前置するテキストを取得します
   */
  readonly prefix: string
 
  /**
   * 数値に後置するテキストを取得します
   */
  readonly suffix: string
 
  /**
   * 数値を取得します.
   */
  readonly value: number
 
  /**
   * 数値の変更を受け付けるか状態を取得します
   */
  readonly status: NumberLabelStatus
 
  /**
   * ラベル付き数値を描画するエンティティ. (このエンティティの子にラベルが登録されます.)
   */
  container?: g.E
 
  /**
   * 加算します.
   *
   * @param value 加算する値
   */
  add (value: number): void
 
  /**
   * 引数の値に設定します.
   *
   * @param value 設定する値
   */
  set (value: number): void
 
  /**
   * 値を設定できるようにします.
   */
  enable (): void
 
  /**
   * 値を設定しようとしても無視します.
   */
  disable (): void
}
 
export interface NumberLabelOptions {
  scene: g.Scene
  value: number
  font: g.Font
  digit: number
  prefix: string
  suffix: string
  textAlign: g.TextAlignString
}
 
export class NumberLabelImpl implements NumberLabel {
  readonly onValue = new g.Trigger<number>()
  private readonly scene: g.Scene
  private _container?: g.E
  private readonly _font: g.Font
  private readonly _digit: number
  private readonly _prefix: string
  private readonly _suffix: string
  private readonly _view: g.Label
  private _value: number
  private _status: NumberLabelStatus = 'disabled'
  constructor ({ scene, value, font, digit, prefix, suffix, textAlign }: NumberLabelOptions) {
    this.scene = scene
    this._font = font
    this._digit = digit
    this._prefix = prefix
    this._suffix = suffix
    this._value = value
    this._view = new g.Label({
      scene,
      font,
      text: this.format(),
      textAlign,
      widthAutoAdjust: false,
      anchorY: 0.5
    })
  }
 
  add (value: number): void {
    if (this._status === 'enabled') {
      this._value += value
      this.updateTextIf()
      this.onValue.fire(value)
    }
  }
 
  set (value: number): void {
    if (this._status === 'enabled') {
      this._value = value
      this.updateTextIf()
      this.onValue.fire(value)
    }
  }
 
  enable (): void {
    this._status = 'enabled'
  }
 
  disable () {
    this._status = 'disabled'
  }
 
  get status (): NumberLabelStatus {
    return this._status
  }
 
  get container (): g.E | undefined {
    return this._container
  }
 
  set container (container: g.E | undefined) {
    if (container) {
      container.append(this._view)
      this._view.width = container.width
      this._view.y = container.height / 2
      this._view.modified()
    } else {
      this._view.remove()
    }
    this._container = container
  }
 
  get font (): g.Font {
    return this._font
  }
 
  get digit (): number {
    return this._digit
  }
 
  get prefix (): string {
    return this._prefix
  }
 
  get suffix (): string {
    return this._suffix
  }
 
  get value (): number {
    return this._value
  }
 
  private format (): string {
    let payload = ''
    for (let i = 0; i < this._digit; i++) {
      payload += ' '
    }
    payload += this.value.toString()
 
    return `${this.prefix}${payload.slice(-this._digit)}${this.suffix}`
  }
 
  private updateTextIf () {
    const oldText = this._view.text
    const newText = this.format()
    if (oldText !== newText) {
      this._view.text = this.format()
      this._view.invalidate()
    }
  }
}