TextboxLine.hx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package textbox;
  2. import flixel.group.FlxGroup;
  3. import flixel.text.FlxText;
  4. class TextBoxLine {
  5. public var characters:FlxTypedGroup<Text>;
  6. public var text_width(default, null):Float;
  7. var inner_text:FlxText;
  8. public function new()
  9. {
  10. characters = new FlxTypedGroup<Text>();
  11. text_width = 0;
  12. inner_text = new FlxText();
  13. inner_text.text = "";
  14. }
  15. // Creates a tempoary FlxText to calculate the future length of the current string with a suffix.
  16. public function projectWidth(string_to_append:String):Float
  17. {
  18. var test_string:FlxText = new FlxText();
  19. test_string.font = inner_text.font;
  20. test_string.size = inner_text.size;
  21. test_string.text = inner_text.text + string_to_append;
  22. return test_string.textField.width;
  23. }
  24. // Accepts a new character and updates its logic values like width.
  25. public function push(character:Text):Void
  26. {
  27. // Regerenate the FlxText.
  28. if(characters.length == 0)
  29. {
  30. inner_text.text = "";
  31. inner_text.font = character.font;
  32. inner_text.size = character.size;
  33. }
  34. characters.add(character);
  35. inner_text.text += character.text;
  36. #if js
  37. // Legnth calculation wouldn't work properly if I haven't done this.
  38. if(character.text == " ")
  39. // TODO : pass this magic cookie as a setting
  40. text_width += character.width+2;
  41. else
  42. text_width = inner_text.textField.textWidth;
  43. #else
  44. text_width = inner_text.textField.textWidth;
  45. #end
  46. }
  47. // Releases its characters to pass along or put them back into pool.
  48. public function dispose():FlxTypedGroup<Text>
  49. {
  50. text_width = 0;
  51. var c = characters;
  52. characters = new FlxTypedGroup<Text>();
  53. inner_text.text = "";
  54. return c;
  55. }
  56. // Takes ownership of the characters and recalculates its metrics.
  57. public function take(characters:FlxTypedGroup<Text>):Void
  58. {
  59. this.characters = characters;
  60. inner_text.text = "";
  61. for(character in characters)
  62. {
  63. inner_text.text += character.text;
  64. }
  65. text_width = inner_text.width;
  66. }
  67. }