partey_workadventure/front/src/Phaser/Components/TextUtils.ts

60 lines
2.0 KiB
TypeScript
Raw Normal View History

import type { ITiledMapObject } from "../Map/ITiledMap";
import type { GameScene } from "../Game/GameScene";
import { type } from "os";
2021-11-02 10:51:02 +01:00
import { GameMapProperties } from "../Game/GameMapProperties";
export class TextUtils {
public static createTextFromITiledMapObject(scene: GameScene, object: ITiledMapObject): void {
if (object.text === undefined) {
throw new Error("This object has not textual representation.");
}
2021-04-17 18:55:39 +02:00
const options: {
fontStyle?: string;
fontSize?: string;
fontFamily?: string;
color?: string;
align?: string;
2021-04-17 18:55:39 +02:00
wordWrap?: {
width: number;
useAdvancedWrap?: boolean;
};
2021-04-17 18:55:39 +02:00
} = {};
if (object.text.italic) {
options.fontStyle = "italic";
}
// Note: there is no support for "strikeout" and "underline"
let fontSize: number = 16;
if (object.text.pixelsize) {
fontSize = object.text.pixelsize;
}
options.fontSize = fontSize + "px";
if (object.text.fontfamily) {
options.fontFamily = '"' + object.text.fontfamily + '"';
}
if (object.properties !== undefined) {
for (const property of object.properties) {
2021-11-02 10:51:02 +01:00
if (property.name === GameMapProperties.FONT_FAMILY && typeof property.value === "string") {
options.fontFamily = property.value;
}
}
}
let color = "#000000";
if (object.text.color !== undefined) {
color = object.text.color;
}
2021-04-17 18:55:39 +02:00
options.color = color;
if (object.text.wrap === true) {
options.wordWrap = {
width: object.width,
//useAdvancedWrap: true
};
}
if (object.text.halign !== undefined) {
2021-04-17 18:55:39 +02:00
options.align = object.text.halign;
}
2021-04-17 18:55:39 +02:00
const textElem = scene.add.text(object.x, object.y, object.text.text, options);
textElem.setAngle(object.rotation);
}
}