2021-05-12 09:13:25 +02:00
|
|
|
import type {ITiledMapObject} from "../Map/ITiledMap";
|
|
|
|
import type {GameScene} from "../Game/GameScene";
|
2021-04-16 21:29:05 +02:00
|
|
|
|
|
|
|
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,
|
|
|
|
wordWrap?: {
|
|
|
|
width: number,
|
|
|
|
useAdvancedWrap?: boolean
|
|
|
|
}
|
|
|
|
} = {};
|
2021-04-16 21:29:05 +02:00
|
|
|
if (object.text.italic) {
|
2021-04-17 18:55:39 +02:00
|
|
|
options.fontStyle = 'italic';
|
2021-04-16 21:29:05 +02:00
|
|
|
}
|
|
|
|
// Note: there is no support for "strikeout" and "underline"
|
|
|
|
let fontSize: number = 16;
|
|
|
|
if (object.text.pixelsize) {
|
|
|
|
fontSize = object.text.pixelsize;
|
|
|
|
}
|
2021-04-17 18:55:39 +02:00
|
|
|
options.fontSize = fontSize + 'px';
|
2021-04-16 21:29:05 +02:00
|
|
|
if (object.text.fontfamily) {
|
2021-04-17 18:55:39 +02:00
|
|
|
options.fontFamily = '"'+object.text.fontfamily+'"';
|
2021-04-16 21:29:05 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2021-04-16 21:29:05 +02:00
|
|
|
}
|
|
|
|
if (object.text.halign !== undefined) {
|
2021-04-17 18:55:39 +02:00
|
|
|
options.align = object.text.halign;
|
2021-04-16 21:29:05 +02:00
|
|
|
}
|
2021-04-17 18:55:39 +02:00
|
|
|
|
|
|
|
console.warn(options);
|
|
|
|
const textElem = scene.add.text(object.x, object.y, object.text.text, options);
|
|
|
|
textElem.setAngle(object.rotation);
|
2021-04-16 21:29:05 +02:00
|
|
|
}
|
|
|
|
}
|