2023-10-20 22:18:37 +02:00
|
|
|
import { RdDTMRDialog } from "../rdd-tmr-dialog.js";
|
2021-12-12 17:36:22 +01:00
|
|
|
import { tmrConstants, tmrTokenZIndex } from "../tmr-constants.js";
|
2023-10-20 22:18:37 +02:00
|
|
|
import { TMRUtility } from "../tmr-utility.js";
|
2021-02-11 02:48:27 +01:00
|
|
|
|
2023-10-20 22:18:37 +02:00
|
|
|
export const tooltipStyle = new PIXI.TextStyle({
|
2021-02-11 02:48:27 +01:00
|
|
|
fontFamily: 'CaslonAntique',
|
|
|
|
fontSize: 18,
|
|
|
|
fill: '#FFFFFF',
|
|
|
|
stroke: '#000000',
|
|
|
|
strokeThickness: 3
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
export class PixiTMR {
|
|
|
|
|
|
|
|
static textures = []
|
|
|
|
|
|
|
|
constructor(tmrObject, pixiApp) {
|
|
|
|
this.tmrObject = tmrObject;
|
|
|
|
this.pixiApp = pixiApp ?? tmrObject.pixiApp;
|
2021-02-12 15:01:10 +01:00
|
|
|
this.pixiApp.stage.sortableChildren = true;
|
2021-02-11 02:48:27 +01:00
|
|
|
this.callbacksOnAnimate = [];
|
|
|
|
}
|
|
|
|
|
2023-10-20 22:18:37 +02:00
|
|
|
async load(onLoad = (loader, resources) => { }) {
|
2023-05-29 08:38:22 +02:00
|
|
|
// WIP - Deprecated since v7 : let loader = new PIXI.Loader();
|
2021-02-11 02:48:27 +01:00
|
|
|
for (const [name, img] of Object.entries(PixiTMR.textures)) {
|
2023-05-25 20:40:59 +02:00
|
|
|
const texture = await PIXI.Assets.load(img);
|
|
|
|
let image = PIXI.Sprite.from(texture);
|
2021-02-11 02:48:27 +01:00
|
|
|
}
|
2023-05-29 08:38:22 +02:00
|
|
|
onLoad();
|
|
|
|
for (let onAnimate of this.callbacksOnAnimate) {
|
|
|
|
onAnimate();
|
2023-05-25 20:40:59 +02:00
|
|
|
}
|
2023-05-29 08:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static getImgFromCode(code) {
|
|
|
|
return PixiTMR.textures[code]
|
2021-02-11 02:48:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static register(name, img) {
|
|
|
|
PixiTMR.textures[name] = img;
|
|
|
|
}
|
|
|
|
|
2023-10-20 22:18:37 +02:00
|
|
|
animate(animation = pixiApp => { }) {
|
2021-02-11 02:48:27 +01:00
|
|
|
this.callbacksOnAnimate.push(() => animation(this.pixiApp));
|
|
|
|
}
|
|
|
|
|
|
|
|
sprite(code, options = {}) {
|
2023-05-29 08:38:22 +02:00
|
|
|
let img = PixiTMR.getImgFromCode(code)
|
|
|
|
const texture = PIXI.utils.TextureCache[img];
|
2021-02-11 02:48:27 +01:00
|
|
|
if (!texture) {
|
2023-05-29 08:38:22 +02:00
|
|
|
console.error("Texture manquante", code, PIXI.utils.TextureCache)
|
2021-02-11 02:48:27 +01:00
|
|
|
return;
|
2023-10-20 22:18:37 +02:00
|
|
|
}
|
2021-02-11 02:48:27 +01:00
|
|
|
let sprite = new PIXI.Sprite(texture);
|
|
|
|
sprite.width = options.taille ?? tmrConstants.half;
|
|
|
|
sprite.height = options.taille ?? tmrConstants.half;
|
|
|
|
sprite.anchor.set(0.5);
|
2021-02-12 12:50:17 +01:00
|
|
|
if (options.color) {
|
|
|
|
sprite.tint = options.color;
|
|
|
|
}
|
2023-10-20 22:18:37 +02:00
|
|
|
sprite.zIndex = options.zIndex ?? tmrTokenZIndex.casehumide + 1;
|
2021-02-11 02:48:27 +01:00
|
|
|
sprite.alpha = options.alpha ?? 0.75;
|
|
|
|
sprite.decallage = options.decallage ?? tmrConstants.center;
|
|
|
|
this.pixiApp.stage.addChild(sprite);
|
|
|
|
return sprite;
|
2023-10-20 22:18:37 +02:00
|
|
|
}
|
|
|
|
|
2021-02-11 02:48:27 +01:00
|
|
|
circle(name, options = {}) {
|
|
|
|
let sprite = new PIXI.Graphics();
|
|
|
|
sprite.beginFill(options.color, options.opacity);
|
|
|
|
sprite.drawCircle(0, 0, (options.taille ?? 12) / 2);
|
|
|
|
sprite.endFill();
|
|
|
|
sprite.decallage = options.decallage ?? tmrConstants.topLeft;
|
|
|
|
this.pixiApp.stage.addChild(sprite);
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
2023-10-20 22:18:37 +02:00
|
|
|
addTooltip(sprite, computeTooltip) {
|
|
|
|
sprite.tooltip = new PIXI.Text('', tooltipStyle);
|
|
|
|
sprite.tooltip.zIndex = tmrTokenZIndex.tooltip;
|
|
|
|
sprite.isOver = false;
|
2023-10-24 22:44:51 +02:00
|
|
|
sprite.eventMode = 'static';
|
2023-10-20 22:18:37 +02:00
|
|
|
sprite
|
|
|
|
.on('pointermove', event => this.onPointerMove(event, sprite, computeTooltip))
|
|
|
|
.on('pointerdown', event => this.onClickBackground(event))
|
|
|
|
.on('pointerover', event => this.onShowTooltip(event, sprite))
|
|
|
|
.on('pointerout', event => this.onHideTooltip(event, sprite));
|
2021-02-11 02:48:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onClickBackground(event) {
|
2023-10-20 22:18:37 +02:00
|
|
|
if (!this.viewOnly) {
|
|
|
|
this.tmrObject.onClickTMR(event)
|
|
|
|
}
|
2021-02-11 02:48:27 +01:00
|
|
|
}
|
|
|
|
|
2023-10-20 22:18:37 +02:00
|
|
|
onPointerMove(event, sprite, computeTooltip) {
|
|
|
|
if (sprite.isOver && sprite.tooltip) {
|
|
|
|
var { x, y } = TMRUtility.computeEventPosition(event);
|
|
|
|
const oddq = TMRUtility.computeOddq(x, y);
|
2021-02-11 02:48:27 +01:00
|
|
|
|
2023-10-20 22:18:37 +02:00
|
|
|
sprite.tooltip.x = x + (oddq.col > 8 ? - 3 * tmrConstants.full : tmrConstants.half)
|
|
|
|
sprite.tooltip.y = y + (oddq.row > 10 ? - tmrConstants.half : tmrConstants.half)
|
|
|
|
sprite.tooltip.text = computeTooltip(event, sprite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onShowTooltip(event, sprite) {
|
|
|
|
if (sprite.tooltip) {
|
2021-02-11 02:48:27 +01:00
|
|
|
if (!sprite.isOver) {
|
|
|
|
sprite.tooltip.x = sprite.x;
|
|
|
|
sprite.tooltip.y = sprite.y;
|
|
|
|
this.pixiApp.stage.addChild(sprite.tooltip);
|
|
|
|
}
|
|
|
|
sprite.isOver = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-20 22:18:37 +02:00
|
|
|
onHideTooltip(event, sprite) {
|
2021-02-11 02:48:27 +01:00
|
|
|
if (sprite.tooltip) {
|
|
|
|
if (sprite.isOver) {
|
|
|
|
this.pixiApp.stage.removeChild(sprite.tooltip);
|
|
|
|
}
|
|
|
|
sprite.isOver = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-20 22:18:37 +02:00
|
|
|
setPosition(sprite, oddq) {
|
2021-12-12 17:36:22 +01:00
|
|
|
let decallagePairImpair = (oddq.col % 2 == 0) ? tmrConstants.col1_y : tmrConstants.col2_y;
|
2021-02-11 02:48:27 +01:00
|
|
|
let dx = (sprite.decallage == undefined) ? 0 : sprite.decallage.x;
|
|
|
|
let dy = (sprite.decallage == undefined) ? 0 : sprite.decallage.y;
|
2021-12-12 17:36:22 +01:00
|
|
|
sprite.x = tmrConstants.gridx + (oddq.col * tmrConstants.cellw) + dx;
|
|
|
|
sprite.y = tmrConstants.gridy + (oddq.row * tmrConstants.cellh) + dy + decallagePairImpair;
|
2021-02-11 02:48:27 +01:00
|
|
|
}
|
|
|
|
|
2021-12-12 17:36:22 +01:00
|
|
|
getCaseRectangle(oddq) {
|
|
|
|
let decallagePairImpair = (oddq.col % 2 == 0) ? tmrConstants.col1_y : tmrConstants.col2_y;
|
|
|
|
let x = tmrConstants.gridx + (oddq.col * tmrConstants.cellw) - (tmrConstants.cellw / 2);
|
|
|
|
let y = tmrConstants.gridy + (oddq.row * tmrConstants.cellh) - (tmrConstants.cellh / 2) + decallagePairImpair;
|
2021-02-11 02:48:27 +01:00
|
|
|
return { x: x, y: y, w: tmrConstants.cellw, h: tmrConstants.cellh };
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|