Skip to content

Commit

Permalink
refactor(bitmaptext): added ability to extend class to use BitmapText
Browse files Browse the repository at this point in the history
TaggedText is now a generic class and some methods are extendable so that it could be made to work
with bitmapText objects. It shouldn't affect existing code.

re #393
  • Loading branch information
mimshwright committed Jan 29, 2024
1 parent e156b18 commit 67985b3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Tommy Leunen
Copyright (c) 2024 Mims Wright, JT Smith, Tommy Leunen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
37 changes: 23 additions & 14 deletions src/TaggedText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
isSpriteSource,
isTextureSource,
DEFAULT_KEY,
PixiTextTypes,
} from "./types";

import { parseTagsNew, removeTags, EMOJI_TAG } from "./tags";
Expand Down Expand Up @@ -66,7 +67,9 @@ const DEFAULT_DESTROY_OPTIONS: PIXI.IDestroyOptions = {
texture: true,
};

export default class TaggedText extends PIXI.Sprite {
export default class TaggedText<
TextType extends PixiTextTypes = PIXI.Text,
> extends PIXI.Sprite {
public static get defaultStyles(): TextStyleSet {
return DEFAULT_STYLE_SET;
}
Expand Down Expand Up @@ -259,8 +262,8 @@ export default class TaggedText extends PIXI.Sprite {
}

// References to internal elements.
private _textFields: PIXI.Text[] = [];
public get textFields(): PIXI.Text[] {
private _textFields: TextType[] = [];
public get textFields(): TextType[] {
return this._textFields;
}
private _sprites: PIXI.Sprite[] = [];
Expand Down Expand Up @@ -385,7 +388,7 @@ export default class TaggedText extends PIXI.Sprite {
* Removes all PIXI children from this component's containers.
* Deletes references to sprites and text fields.
*/
private resetChildren() {
protected resetChildren() {
if (this._textContainer) {
this._textContainer.removeChildren();
this.removeChild(this._textContainer);
Expand Down Expand Up @@ -424,7 +427,7 @@ export default class TaggedText extends PIXI.Sprite {
* image Sprite objects which are included in the text.
* @param imgMap
*/
private createSpriteTemplatesFromSourceMap(imgMap: ImageSourceMap) {
protected createSpriteTemplatesFromSourceMap(imgMap: ImageSourceMap) {
this._spriteTemplates = {};

Object.entries(imgMap).forEach(([key, spriteSource]) => {
Expand Down Expand Up @@ -489,8 +492,7 @@ export default class TaggedText extends PIXI.Sprite {
});
}

private onImageTextureUpdate(baseTexture: PIXI.BaseTexture): void {
baseTexture;
private onImageTextureUpdate(_baseTexture: PIXI.BaseTexture): void {
this._needsUpdate = true;
this._needsDraw = true;
this.updateIfShould();
Expand Down Expand Up @@ -619,12 +621,12 @@ export default class TaggedText extends PIXI.Sprite {
if (isTextToken(t)) {
displayObject = this.createTextFieldForToken(t as TextSegmentToken);
textContainer.addChild(displayObject);
this.textFields.push(displayObject as PIXI.Text);
this.textFields.push(displayObject as TextType);

if (t.textDecorations && t.textDecorations.length > 0) {
for (const d of t.textDecorations) {
const drawing = this.createDrawingForTextDecoration(d);
(displayObject as PIXI.Text).addChild(drawing);
(displayObject as TextType).addChild(drawing);
this._decorations.push(drawing);
}
drewDecorations = true;
Expand Down Expand Up @@ -655,7 +657,7 @@ export default class TaggedText extends PIXI.Sprite {
this._needsDraw = false;
}

private createDrawingForTextDecoration(
protected createDrawingForTextDecoration(
textDecoration: TextDecorationMetrics
): PIXI.Graphics {
const { overdrawDecorations: overdraw = 0 } = this.options;
Expand Down Expand Up @@ -689,7 +691,11 @@ export default class TaggedText extends PIXI.Sprite {
return drawing;
}

private createTextFieldForToken(token: TextSegmentToken): PIXI.Text {
protected createTextField(text: string, style: TextStyleExtended): TextType {
return new PIXI.Text(text, style as Partial<PIXI.ITextStyle>) as TextType;
}

protected createTextFieldForToken(token: TextSegmentToken): TextType {
const { textTransform = "" } = token.style;

let text = token.content;
Expand All @@ -707,9 +713,12 @@ export default class TaggedText extends PIXI.Sprite {
}

const alignClassic = convertUnsupportedAlignment(token.style.align);
const sanitizedStyle = { ...token.style, align: alignClassic };
const sanitizedStyle = {
...token.style,
align: alignClassic,
} as TextStyleExtended;

const textField = new PIXI.Text(text, sanitizedStyle);
const textField = this.createTextField(text, sanitizedStyle) as PIXI.Text;

let { fontScaleWidth = 1.0, fontScaleHeight = 1.0 } = token.style;
fontScaleWidth =
Expand Down Expand Up @@ -739,7 +748,7 @@ export default class TaggedText extends PIXI.Sprite {
}

textField.scale.set(finalScaleWidth, finalScaleHeight);
return textField;
return textField as TextType;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/errorMessaging.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import TaggedText from "./TaggedText";
import * as PIXI from "pixi.js";
import { ErrorHandler, ErrorMessage, ErrorMessageType } from "./types";

const log =
(type: ErrorMessageType) =>
(handler?: ErrorHandler, supressConsole = false, target?: TaggedText) =>
(handler?: ErrorHandler, supressConsole = false, target?: PIXI.Sprite) =>
(code: string, message: string): void => {
if (supressConsole !== true) {
const method = type === "warning" ? console.warn : console.error;
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export type Bounds = Rectangle;

export type Nested<T> = T | Array<Nested<T>>;

export type PixiTextTypes = PIXI.Text | PIXI.BitmapText;

///// OPTIONS

export type SpriteSource =
Expand Down Expand Up @@ -201,6 +203,7 @@ export interface LineBreakStyles {

export interface TextStyleExtended
extends Record<string, unknown>,
Partial<Omit<Omit<PIXI.IBitmapTextStyle, "align">, "fontSize">>,
Partial<Omit<PIXI.ITextStyle, "align">>,
ImageStyles,
TextDecorationStyles,
Expand Down

0 comments on commit 67985b3

Please sign in to comment.