Guest User

Untitled

a guest
Jan 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. /**
  2. * Clunky Typescript class to wrap bitmap text,
  3. * use like this:
  4. * new TextWrapper(THE_TEXT_YOU_WANT_TO_SET, CURRENT SCENE, BITMAP TEXT FIELD, DESIRED FONT SIZE, MAX WIDTH, OPT MAX HEIGHT).wrapText();
  5. */
  6. export class TextWrapper {
  7.  
  8. private targetScene:Phaser.Scene;
  9.  
  10. private targetTextField:Phaser.GameObjects.BitmapText;
  11. private fullText:string;
  12. private wrapWidth:number;
  13. private maxHeight:number = -1;
  14. private fontSizeStep:number = 10;
  15.  
  16. private fontSize:number = 0;
  17. private font:string;
  18. private textLines:Array<string>;
  19.  
  20. private testText:Phaser.GameObjects.BitmapText;
  21.  
  22. /**
  23. * Takes some text and attempts to figure how to wrap for bitmap text field
  24. *
  25. * @param fullText text to wrap
  26. * @param scene the scene the bitmap field will exist in
  27. * @param textField the bitmap text which text will end up in
  28. * @param wrapWidth width to wrap at
  29. * @param maxHeight optional: max height allowed, if we go over we start shrinking the font, -1 means dont do this
  30. * @param fontSizeStepToFit optional: amount to reduce font size for each fit check
  31. */
  32. constructor(fullText:string, scene:Phaser.Scene, textField:Phaser.GameObjects.BitmapText, fontSize:number, wrapWidth:number, maxHeight:number=-1, fontSizeStepToFit:number=5) {
  33. this.targetTextField = textField;
  34. this.targetScene = scene;
  35. this.fullText = fullText;
  36. this.wrapWidth = wrapWidth;
  37. this.maxHeight = maxHeight;
  38. this.fontSizeStep = fontSizeStepToFit;
  39. this.fontSize = fontSize;
  40. this.font = this.targetTextField.font;
  41. }
  42.  
  43. /**
  44. * Note! If maxHeight set this may resize the font size of your text field!
  45. */
  46. public async wrapText():Promise<string> {
  47. this.testText = await this.targetScene.add.bitmapText(-1000, -1000, this.font, '', this.fontSize);
  48. await this.wrapTextByLine();
  49. while ( this.maxHeight > 0 && this.fontSize > 0 && this.testText.getTextBounds().local.height > this.maxHeight) {
  50. this.fontSize -= this.fontSizeStep;
  51. await this.wrapTextByLine();
  52. }
  53. let finalText:string = this.textLines.join("\n");
  54. this.targetTextField.setFontSize(this.fontSize);
  55. this.targetTextField.setText( finalText);
  56. if (this.testText != null) {
  57. this.testText.destroy();
  58. }
  59. return finalText;
  60. }
  61.  
  62. private async wrapTextByLine():Promise<Array<string>> {
  63. let words:Array<string> = this.fullText.split(" ");
  64. //you might wonder why this is necessary, but if each line isn't tested alone bounds sometimes returns different values
  65. this.textLines = new Array<string>();
  66. let currentLine:number = 0;
  67.  
  68. if (words.length == 0) {
  69. console.log("no spaces, I can't wrap!");
  70. return new Array(this.fullText);
  71. }
  72.  
  73. this.textLines[currentLine] = words[0] + " ";
  74. this.testText.fontSize = this.fontSize;
  75. for (let i=1; i < words.length; i++) {
  76. this.testText.setText(this.textLines[currentLine] + words[i]);
  77. if (this.testText.getTextBounds().local.width > this.wrapWidth) {
  78. currentLine++;
  79. this.textLines[currentLine] = "";
  80. }
  81. this.textLines[currentLine] += words[i] + " ";
  82. }
  83.  
  84. this.testText.setText(this.textLines.join("\n"));
  85. return this.textLines;
  86. }
  87. }
Add Comment
Please, Sign In to add comment