|
| 1 | +import { FUPressureGauge } from './pressure-gauge.mjs'; |
| 2 | + |
| 3 | +export class FUModernPressureGauge extends FUPressureGauge { |
| 4 | + static get name() { |
| 5 | + return 'FU.CombatHudModern'; |
| 6 | + } |
| 7 | + |
| 8 | + static get combatHudTheme() { |
| 9 | + return 'fu-modern'; |
| 10 | + } |
| 11 | + |
| 12 | + bgColor = '#656565'; |
| 13 | + borderColor = '#c7c7c7'; |
| 14 | + barHeight = 12; |
| 15 | + |
| 16 | + clipSize = 0.1; |
| 17 | + |
| 18 | + _createMaskTexture(width, height) { |
| 19 | + const canvas = document.createElement('canvas'); |
| 20 | + canvas.width = width; |
| 21 | + canvas.height = height; |
| 22 | + |
| 23 | + const ctx = canvas.getContext('2d'); |
| 24 | + |
| 25 | + ctx.beginPath(); |
| 26 | + ctx.moveTo(width * this.clipSize, 0); |
| 27 | + ctx.lineTo(width, 0); |
| 28 | + |
| 29 | + ctx.lineTo(width - width * this.clipSize, height); |
| 30 | + ctx.lineTo(0, height); |
| 31 | + |
| 32 | + ctx.closePath(); |
| 33 | + |
| 34 | + ctx.fillStyle = 'white'; |
| 35 | + ctx.fill(); |
| 36 | + |
| 37 | + const texture = globalThis.PIXI.Texture.from(canvas); |
| 38 | + return texture; |
| 39 | + } |
| 40 | + |
| 41 | + _createFGMaskTexture(width, height) { |
| 42 | + const canvas = document.createElement('canvas'); |
| 43 | + canvas.width = width; |
| 44 | + canvas.height = height; |
| 45 | + const ctx = canvas.getContext('2d'); |
| 46 | + |
| 47 | + ctx.beginPath(); |
| 48 | + ctx.moveTo(0, 0); |
| 49 | + ctx.lineTo(width, 0); |
| 50 | + ctx.lineTo(width - width * this.clipSize, height); |
| 51 | + ctx.lineTo(0, height); |
| 52 | + |
| 53 | + ctx.closePath(); |
| 54 | + ctx.fillStyle = 'white'; |
| 55 | + ctx.fill(); |
| 56 | + |
| 57 | + return globalThis.PIXI.Texture.from(canvas); |
| 58 | + } |
| 59 | + |
| 60 | + _getBorderThickness() { |
| 61 | + return canvas.stage.scale.y; |
| 62 | + } |
| 63 | + _getShadowThickness() { |
| 64 | + return 2 * canvas.stage.scale.y; |
| 65 | + } |
| 66 | + |
| 67 | + _drawShadow(width, height, color = this.shadowColor) { |
| 68 | + const shadow = this._getChildElement('Shadow'); |
| 69 | + if (!shadow) return; |
| 70 | + |
| 71 | + shadow.clear(); |
| 72 | + const thickness = this._getShadowThickness(); |
| 73 | + shadow.lineStyle(thickness, color, 0.25, 0); |
| 74 | + shadow.moveTo(0, thickness); |
| 75 | + shadow.lineTo(width, thickness); |
| 76 | + |
| 77 | + shadow.moveTo(0, thickness); |
| 78 | + shadow.lineTo(0, height); |
| 79 | + } |
| 80 | + |
| 81 | + _drawBorder(width, height, color = this.border) { |
| 82 | + const border = this._getChildElement('Border'); |
| 83 | + if (!border) return; |
| 84 | + |
| 85 | + border.clear(); |
| 86 | + border.lineStyle(this._getBorderThickness(), color, 1, 0); |
| 87 | + border.drawRect(0, 0, width, height); |
| 88 | + } |
| 89 | + |
| 90 | + async _setFGPosition(suppressAnimation = false) { |
| 91 | + const fg = this._getChildElement('FG'); |
| 92 | + if (!fg?.mask) return; |
| 93 | + |
| 94 | + const { current = 0, max = 0 } = this.clock ?? {}; |
| 95 | + const perc = current / max; |
| 96 | + const x = -(fg.mask.width - fg.mask.width * perc); |
| 97 | + |
| 98 | + if (!this.token.isPreview && !suppressAnimation) { |
| 99 | + globalThis.gsap.killTweensOf(fg); |
| 100 | + await globalThis.gsap.to(fg.mask, { x, duration: this.animationDuration / 1000 }); |
| 101 | + } else { |
| 102 | + fg.mask.x = x; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + onCanvasScale() { |
| 107 | + if (!(this.lastCanvasScale.x === canvas.stage.scale.y && this.lastCanvasScale.y === canvas.stage.scale.y)) { |
| 108 | + super.onCanvasScale(); |
| 109 | + const { width, height } = this._getScaledSize(); |
| 110 | + const fg = this._getChildElement('FG'); |
| 111 | + if (fg?.mask) fg.mask.texture = this._createFGMaskTexture(width, height); |
| 112 | + |
| 113 | + if (this.mask) this.mask.texture = this._createMaskTexture(width, height); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** @inheritdoc */ |
| 118 | + _createElements(width = 100, height = this.barHeight) { |
| 119 | + const { current = 0, max = 0 } = this.clock ?? {}; |
| 120 | + const bg = new globalThis.PIXI.Sprite(globalThis.PIXI.Texture.WHITE); |
| 121 | + bg.name = `${this.token.id}.Pressure.BG`; |
| 122 | + this.addChild(bg); |
| 123 | + |
| 124 | + const fg = new globalThis.PIXI.Sprite(this._createProgressTexture(width, height)); |
| 125 | + fg.name = `${this.token.id}.Pressure.FG`; |
| 126 | + this.addChild(fg); |
| 127 | + |
| 128 | + const fgmask = new globalThis.PIXI.Sprite(this._createFGMaskTexture(width, height, current / max)); |
| 129 | + fgmask.name = `${this.token.id}.Pressure.FGMask`; |
| 130 | + this.addChild(fgmask); |
| 131 | + fg.mask = fgmask; |
| 132 | + |
| 133 | + const shadow = new globalThis.PIXI.Graphics(); |
| 134 | + shadow.name = `${this.token.id}.Pressure.Shadow`; |
| 135 | + this.addChild(shadow); |
| 136 | + this._drawShadow(width, height, this.shadowColor); |
| 137 | + |
| 138 | + const border = new globalThis.PIXI.Graphics(); |
| 139 | + border.name = `${this.token.id}.Pressure.Border`; |
| 140 | + this.addChild(border); |
| 141 | + this._drawBorder(width, height, this.borderColor); |
| 142 | + |
| 143 | + const mask = new globalThis.PIXI.Sprite(this._createMaskTexture(width, height)); |
| 144 | + mask.name = `${this.token.id}.Pressure.Mask`; |
| 145 | + this.addChild(mask); |
| 146 | + this.mask = mask; |
| 147 | + |
| 148 | + this._setFGPosition(true); |
| 149 | + } |
| 150 | + |
| 151 | + /** @inheritdoc */ |
| 152 | + refresh(force = false) { |
| 153 | + try { |
| 154 | + const { width, height } = this._getScaledSize(); |
| 155 | + |
| 156 | + if (force) { |
| 157 | + this._destroyChildren(); |
| 158 | + const { width, height } = this._getScaledSize(); |
| 159 | + this._createElements(width, height); |
| 160 | + } |
| 161 | + |
| 162 | + if (!this.shouldDrawPressureGauge) { |
| 163 | + this.visible = false; |
| 164 | + return; |
| 165 | + } else { |
| 166 | + this.visible = true; |
| 167 | + } |
| 168 | + |
| 169 | + const elems = ['BG', 'FG', 'Border', 'Mask'].map((name) => this._getChildElement(name)); |
| 170 | + if (elems.some((elem) => !elem)) throw new Error(`Pressure gauge PIXI elements not created for ${this.id}!`); |
| 171 | + |
| 172 | + const [bg, fg, border, mask] = elems; |
| 173 | + const { current } = this.clock; |
| 174 | + |
| 175 | + this._drawBorder(width, height, this.borderColor); |
| 176 | + this._drawShadow(width, height, this.shadowColor); |
| 177 | + |
| 178 | + bg.tint = this.bgColor; |
| 179 | + bg.width = fg.width = border.width; |
| 180 | + bg.height = fg.height = border.height; |
| 181 | + |
| 182 | + bg.x = bg.y = fg.y = border.x = border.y = mask.x = mask.y = 0; |
| 183 | + |
| 184 | + // fg.texture = this._createProgressTexture(width, this.barHeight); |
| 185 | + this._setFGPosition(current === this.lastValue); |
| 186 | + this.lastValue = current; |
| 187 | + this._positionGauge(); |
| 188 | + } catch (err) { |
| 189 | + console.error(err); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments