Réduction de l'espace des TMR

This commit is contained in:
2023-11-15 22:14:00 +01:00
parent da3091dc4b
commit bfd3b0d74a
49 changed files with 1288 additions and 463 deletions

View File

@ -1,35 +1,61 @@
import { SYSTEM_RDD } from "../constants.js";
import { Misc } from "../misc.js";
import { RdDTMRDialog } from "../rdd-tmr-dialog.js";
import { tmrConstants, tmrTokenZIndex } from "../tmr-constants.js";
import { TMRConstants, tmrTokenZIndex } from "../tmr-constants.js";
import { TMRUtility } from "../tmr-utility.js";
import { EffetsDraconiques } from "./effets-draconiques.js";
export const tooltipStyle = new PIXI.TextStyle({
fontFamily: 'CaslonAntique',
fontSize: 18,
fill: '#FFFFFF',
stroke: '#000000',
strokeThickness: 3
});
export class PixiTMR {
static textures = []
constructor(tmrDialog) {
static getImgFromCode(code) {
return PixiTMR.textures[code]
}
static register(name, img) {
PixiTMR.textures[name] = img;
}
static async init() {
await Promise.all(
Object.values(PixiTMR.textures)
.filter(img => img != undefined)
.map(async img => PIXI.Sprite.from(await PIXI.Assets.load(img)))
)
}
constructor(tmrDialog, displaySize) {
this.tmrDialog = tmrDialog;
this.callbacksOnAnimate = [];
this.pixiApp = new PIXI.Application({ width: 720, height: 860 });
this.sizes = new TMRConstants({ size: displaySize })
this.pixiApp = new PIXI.Application(PixiTMR.computeTMRSize(this.sizes));
this.pixiApp.eventMode = 'static';
this.pixiApp.stage.sortableChildren = true;
this.tooltip = new PIXI.Text('', tooltipStyle);
this.tooltipStyle = new PIXI.TextStyle({
fontFamily: 'CaslonAntique',
fontSize: 16,
fill: '#FFFFFF',
stroke: '#000000',
strokeThickness: 4
});
this.tooltip = new PIXI.Text('', this.tooltipStyle);
this.tooltip.zIndex = 1000
this.pixiApp.stage.addChild(this.tooltip);
}
close() {
this.pixiApp.ticker.stop();
}
static computeTMRSize(sizeConstants) {
return { width: sizeConstants.cellw * 13 + sizeConstants.marginx, height: sizeConstants.cellh / 2 + sizeConstants.cellh * 15 + sizeConstants.marginy }
}
resizeTMR(displaySize) {
this.sizes = new TMRConstants({ size: displaySize })
const appSize = PixiTMR.computeTMRSize(this.sizes)
this.pixiApp.renderer.resize(appSize.width, appSize.height)
this.tooltipStyle.fontSize = Math.max(this.sizes.size / 4, 16)
}
get view() {
@ -48,24 +74,11 @@ export class PixiTMR {
.on('pointerout', event => this.onHideTooltip(event));
}
async load(onLoad = (loader, resources) => { }) {
// WIP - Deprecated since v7 : let loader = new PIXI.Loader();
for (const [name, img] of Object.entries(PixiTMR.textures)) {
const texture = await PIXI.Assets.load(img);
let image = PIXI.Sprite.from(texture);
}
onLoad();
async loadAnimations() {
for (let onAnimate of this.callbacksOnAnimate) {
onAnimate();
}
}
static getImgFromCode(code) {
return PixiTMR.textures[code]
}
static register(name, img) {
PixiTMR.textures[name] = img;
this.pixiApp.ticker.start();
}
animate(animation = pixiApp => { }) {
@ -90,33 +103,48 @@ export class PixiTMR {
sprite(code, options = {}) {
let img = PixiTMR.getImgFromCode(code)
const texture = PIXI.utils.TextureCache[img];
const texture = PIXI.utils.TextureCache[img]
if (!texture) {
console.error("Texture manquante", code, PIXI.utils.TextureCache)
return;
}
let sprite = new PIXI.Sprite(texture);
sprite.width = options.taille ?? tmrConstants.half;
sprite.height = options.taille ?? tmrConstants.half;
sprite.anchor.set(0.5);
if (options.color) {
sprite.tint = options.color;
sprite.taille = options.taille ?? (() => this.sizes.half)
sprite.width = sprite.taille()
sprite.height = sprite.taille()
sprite.anchor.set(0.5)
if (options.tint) {
sprite.tint = options.tint
}
sprite.zIndex = options.zIndex ?? tmrTokenZIndex.casehumide + 1;
sprite.alpha = options.alpha ?? 0.75;
sprite.decallage = options.decallage ?? tmrConstants.center;
this.pixiApp.stage.addChild(sprite);
return sprite;
sprite.zIndex = options.zIndex ?? tmrTokenZIndex.casehumide + 1
sprite.alpha = options.alpha ?? 1
sprite.decallage = options.decallage ?? this.sizes.center
this.pixiApp.stage.addChild(sprite)
return sprite
}
circle(code, options = {}) {
let sprite = new PIXI.Graphics()
sprite.taille = options.taille ?? (() => this.sizes.half)
sprite.decallage = options.decallage ?? this.sizes.topLeft
sprite.beginFill(options.tint, options.opacity)
sprite.drawCircle(0, 0, sprite.taille())
sprite.endFill()
this.pixiApp.stage.addChild(sprite)
return sprite
}
square(code, 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;
sprite.taille = options.taille ?? (() => this.sizes.half)
sprite.decallage = options.decallage ?? this.sizes.topLeft
sprite.beginFill(options.tint, options.opacity)
const size = sprite.taille();
sprite.drawRect(0, 0, size, size)
sprite.endFill()
this.pixiApp.stage.addChild(sprite)
return sprite
}
onClickBackground(event) {
@ -131,7 +159,7 @@ export class PixiTMR {
this.tooltip.text = this.computeTooltip(event);
}
}
onShowTooltip(event) {
if (!this.carteTMR.isOver) {
this.setTooltipPosition(event);
@ -149,25 +177,27 @@ export class PixiTMR {
}
computeTooltip(event) {
const oddq = TMRUtility.computeEventOddq(event);
const oddq = this.sizes.computeEventOddq(event);
const coordTMR = TMRUtility.oddqToCoordTMR(oddq);
const tmr = TMRUtility.getTMR(coordTMR)
if (tmr) {
const labelTMR = TMRUtility.getTMRLabel(coordTMR);
const tmrTooltip = `${coordTMR}: ${TMRUtility.getTMRLabel(coordTMR)}`;
const tokenTooltips = this.tmrDialog.allTokens
.filter(token => token.coordTMR() == coordTMR)
.map(token => token.tooltip);
const tmrTooltip = `${coordTMR}: ${labelTMR}`;
return [tmrTooltip, ...tokenTooltips].reduce(Misc.joining('\n'))
}
}
setTooltipPosition(event) {
var { x, y } = TMRUtility.computeEventPosition(event);
const oddq = TMRUtility.computeOddq(x, y);
computeEventOddq(event) {
return this.sizes.computeEventOddq(event)
}
this.tooltip.x = x + (oddq.col > 8 ? -3 * tmrConstants.full : tmrConstants.half);
this.tooltip.y = y + (oddq.row > 10 ? -tmrConstants.half : tmrConstants.half);
setTooltipPosition(event) {
const oddq = this.sizes.computeEventOddq(event);
this.tooltip.x = oddq.x + (oddq.col > 7 ? -3 * this.sizes.full : this.sizes.quarter);
this.tooltip.y = oddq.y + (oddq.row > 10 ? -this.sizes.size : 0);
}
positionToken(token) {
@ -175,11 +205,11 @@ export class PixiTMR {
const sprite = token.sprite;
const oddq = TMRUtility.coordTMRToOddq(token.coordTMR());
const decallagePairImpair = (oddq.col % 2 == 0) ? tmrConstants.col1_y : tmrConstants.col2_y;
const dx = (sprite.decallage == undefined) ? 0 : sprite.decallage.x;
const dy = (sprite.decallage == undefined) ? 0 : sprite.decallage.y;
sprite.x = tmrConstants.gridx + (oddq.col * tmrConstants.cellw) + dx;
sprite.y = tmrConstants.gridy + (oddq.row * tmrConstants.cellh) + dy + decallagePairImpair;
const decallagePairImpair = (oddq.col % 2 == 0) ? this.sizes.col1_y : this.sizes.col2_y;
const dx = sprite.decallage?.x ?? 0
const dy = sprite.decallage?.y ?? 0
sprite.x = this.sizes.gridx + (oddq.col * this.sizes.cellw) + dx;
sprite.y = this.sizes.gridy + (oddq.row * this.sizes.cellh) + dy + decallagePairImpair;
}
}
@ -190,10 +220,9 @@ export class PixiTMR {
}
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;
return { x: x, y: y, w: tmrConstants.cellw, h: tmrConstants.cellh };
const decallagePairImpair = (oddq.col % 2 == 0) ? this.sizes.col1_y : this.sizes.col2_y;
const x = this.sizes.gridx + (oddq.col * this.sizes.cellw) - (this.sizes.cellw / 2);
const y = this.sizes.gridy + (oddq.row * this.sizes.cellh) - (this.sizes.cellh / 2) + decallagePairImpair;
return { x, y, w: this.sizes.cellw, h: this.sizes.cellh };
}
}