Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Clunky Typescript class to wrap bitmap text,
- * use like this:
- * new TextWrapper(THE_TEXT_YOU_WANT_TO_SET, CURRENT SCENE, BITMAP TEXT FIELD, DESIRED FONT SIZE, MAX WIDTH, OPT MAX HEIGHT).wrapText();
- */
- export class TextWrapper {
- private targetScene:Phaser.Scene;
- private targetTextField:Phaser.GameObjects.BitmapText;
- private fullText:string;
- private wrapWidth:number;
- private maxHeight:number = -1;
- private fontSizeStep:number = 10;
- private fontSize:number = 0;
- private font:string;
- private textLines:Array<string>;
- private testText:Phaser.GameObjects.BitmapText;
- /**
- * Takes some text and attempts to figure how to wrap for bitmap text field
- *
- * @param fullText text to wrap
- * @param scene the scene the bitmap field will exist in
- * @param textField the bitmap text which text will end up in
- * @param wrapWidth width to wrap at
- * @param maxHeight optional: max height allowed, if we go over we start shrinking the font, -1 means dont do this
- * @param fontSizeStepToFit optional: amount to reduce font size for each fit check
- */
- constructor(fullText:string, scene:Phaser.Scene, textField:Phaser.GameObjects.BitmapText, fontSize:number, wrapWidth:number, maxHeight:number=-1, fontSizeStepToFit:number=5) {
- this.targetTextField = textField;
- this.targetScene = scene;
- this.fullText = fullText;
- this.wrapWidth = wrapWidth;
- this.maxHeight = maxHeight;
- this.fontSizeStep = fontSizeStepToFit;
- this.fontSize = fontSize;
- this.font = this.targetTextField.font;
- }
- /**
- * Note! If maxHeight set this may resize the font size of your text field!
- */
- public async wrapText():Promise<string> {
- this.testText = await this.targetScene.add.bitmapText(-1000, -1000, this.font, '', this.fontSize);
- await this.wrapTextByLine();
- while ( this.maxHeight > 0 && this.fontSize > 0 && this.testText.getTextBounds().local.height > this.maxHeight) {
- this.fontSize -= this.fontSizeStep;
- await this.wrapTextByLine();
- }
- let finalText:string = this.textLines.join("\n");
- this.targetTextField.setFontSize(this.fontSize);
- this.targetTextField.setText( finalText);
- if (this.testText != null) {
- this.testText.destroy();
- }
- return finalText;
- }
- private async wrapTextByLine():Promise<Array<string>> {
- let words:Array<string> = this.fullText.split(" ");
- //you might wonder why this is necessary, but if each line isn't tested alone bounds sometimes returns different values
- this.textLines = new Array<string>();
- let currentLine:number = 0;
- if (words.length == 0) {
- console.log("no spaces, I can't wrap!");
- return new Array(this.fullText);
- }
- this.textLines[currentLine] = words[0] + " ";
- this.testText.fontSize = this.fontSize;
- for (let i=1; i < words.length; i++) {
- this.testText.setText(this.textLines[currentLine] + words[i]);
- if (this.testText.getTextBounds().local.width > this.wrapWidth) {
- currentLine++;
- this.textLines[currentLine] = "";
- }
- this.textLines[currentLine] += words[i] + " ";
- }
- this.testText.setText(this.textLines.join("\n"));
- return this.textLines;
- }
- }
Add Comment
Please, Sign In to add comment