Text.hx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import h2d.Drawable;
  2. import h2d.Flow;
  3. import h2d.Font;
  4. import h2d.Graphics;
  5. import h2d.Object;
  6. import h2d.Text.Align;
  7. // Use both text_res and res folders.
  8. //PARAM=-D resourcesPath=../../text_res;../../res
  9. class TextWidget extends Object
  10. {
  11. public var align: Align;
  12. public var textField: h2d.Text;
  13. public var back: Graphics;
  14. public function new(parent:h2d.Scene, font: Font, str:String, align:h2d.Text.Align){
  15. super(parent);
  16. this.align = align;
  17. back = new Graphics(this);
  18. var tf = new h2d.Text(font, this);
  19. tf.textColor = 0xffffff;
  20. tf.textAlign = align;
  21. tf.text = str;
  22. textField = tf;
  23. refreshBounds();
  24. }
  25. public function refreshBounds() {
  26. back.clear();
  27. var bounds = textField.getBounds(this);
  28. var size = textField.getSize();
  29. back.beginFill(0x5050ff, 0.5);
  30. back.drawRect(bounds.x, 0, size.width, size.height);
  31. back.endFill();
  32. back.lineStyle(1, 0x50ff50);
  33. back.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);
  34. back.lineStyle(1, 0xff5050);
  35. back.moveTo(bounds.x, 0);
  36. back.lineTo(bounds.x + textField.textWidth, 0);
  37. back.moveTo(bounds.x, 0);
  38. back.lineTo(bounds.x, textField.textHeight);
  39. }
  40. public function setMaxWidth(w:Int) {
  41. textField.maxWidth = w;
  42. refreshBounds();
  43. }
  44. }
  45. class Text extends hxd.App {
  46. var textWidgets:Array<TextWidget> = [];
  47. var resizeWidgets: Array<TextWidget> = [];
  48. var sdfText:h2d.Text;
  49. override function init() {
  50. // Enable global scaling
  51. // s2d.scale(1.25);
  52. var font = hxd.res.DefaultFont.get();
  53. // var font = hxd.Res.customFont.toFont();
  54. var multilineText = "This is a multiline text.\nLorem ipsum dolor";
  55. var singleText = "Hello simple text";
  56. var xpos = 0;
  57. var yoffset = 10.0;
  58. function createWidget(str:String, align:h2d.Text.Align) {
  59. var w = new TextWidget(s2d, font, str, align);
  60. w.x = xpos;
  61. w.y = yoffset;
  62. textWidgets.push(w);
  63. return w;
  64. }
  65. // Static single and multiline widgets
  66. xpos += 450;
  67. for (a in [Align.Left, Align.Center, Align.Right, Align.MultilineCenter, Align.MultilineRight]) {
  68. var w = createWidget("", a);
  69. var label = new h2d.Text(font, w);
  70. label.text = Std.string(a);
  71. label.x = 5;
  72. label.alpha = 0.5;
  73. yoffset += w.textField.textHeight + 10;
  74. var w = createWidget(singleText, a);
  75. yoffset += w.textField.textHeight + 10;
  76. var w = createWidget(multilineText, a);
  77. yoffset += w.textField.textHeight + 10;
  78. }
  79. // Resized widgets
  80. xpos += 200;
  81. yoffset = 10;
  82. var longText = "Lorem ipsum dolor sit amet, fabulas repudiare accommodare nec ut. Ut nec facete maiestatis, partem debitis eos id, perfecto ocurreret repudiandae cum no.";
  83. for (a in [Align.Left, Align.Center, Align.Right, Align.MultilineCenter, Align.MultilineRight]) {
  84. var w = createWidget(longText, a);
  85. w.setMaxWidth(200);
  86. resizeWidgets.push(w);
  87. yoffset += 100;
  88. }
  89. // Flows
  90. function createText(parent:Object, str : String, align:Align, ?forceFont:h2d.Font) {
  91. var tf = new h2d.Text(forceFont != null ? forceFont : font, parent);
  92. tf.textColor = 0xffffff;
  93. tf.textAlign = align;
  94. tf.text = str;
  95. tf.maxWidth = 150;
  96. return tf;
  97. }
  98. function createFlow(parent:Object) {
  99. var flow = new Flow(parent);
  100. flow.debug = true;
  101. flow.horizontalSpacing = 5;
  102. flow.verticalSpacing = 5;
  103. flow.padding = 5;
  104. return flow;
  105. }
  106. yoffset = 0;
  107. var flow = createFlow(s2d);
  108. flow.verticalAlign = FlowAlign.Middle;
  109. createText(flow, singleText, Align.Left);
  110. createText(flow, multilineText, Align.Left);
  111. yoffset += flow.getBounds().height + 10;
  112. var flow = createFlow(s2d);
  113. flow.y = yoffset;
  114. flow.multiline = false;
  115. flow.verticalAlign = FlowAlign.Middle;
  116. createText(flow, multilineText, Align.Center);
  117. createText(flow, multilineText, Align.Right);
  118. yoffset += flow.getBounds().height + 10;
  119. var flow = createFlow(s2d);
  120. flow.y = yoffset;
  121. flow.verticalAlign = FlowAlign.Middle;
  122. flow.maxWidth = 150;
  123. createText(flow, singleText, Align.Left);
  124. createText(flow, multilineText, Align.Center);
  125. yoffset += flow.getBounds().height + 10;
  126. var flow = createFlow(s2d);
  127. flow.y = yoffset;
  128. flow.horizontalAlign = FlowAlign.Middle;
  129. flow.maxWidth = 150;
  130. flow.layout = Vertical;
  131. createText(flow, singleText, Align.Left);
  132. createText(flow, multilineText, Align.Right);
  133. yoffset += flow.getBounds().height + 10;
  134. {
  135. var flow = createFlow(s2d);
  136. flow.y = yoffset;
  137. flow.horizontalAlign = FlowAlign.Left;
  138. flow.maxWidth = 360;
  139. flow.horizontalSpacing = 8;
  140. var f = createText(flow, "short text", Align.Right);
  141. createText(flow, singleText, Align.Left);
  142. yoffset += flow.getBounds().height + 10;
  143. }
  144. var flow = createFlow(s2d);
  145. flow.y = yoffset;
  146. flow.x = 100;
  147. flow.horizontalAlign = FlowAlign.Middle;
  148. flow.layout = Vertical;
  149. {
  150. var f1 = createFlow(flow);
  151. createText(f1, multilineText, Align.Left);
  152. var f2 = createFlow(flow);
  153. createText(f2, multilineText, Align.Left);
  154. }
  155. yoffset += flow.getBounds().height + 10;
  156. // Showcases all supported font formats.
  157. var flow = createFlow(s2d);
  158. flow.debug = false;
  159. flow.y = yoffset;
  160. flow.x = 10;
  161. flow.horizontalAlign = FlowAlign.Left;
  162. flow.layout = Vertical;
  163. {
  164. var tf = createText(flow, "BMFont XML format (Littera export)", Align.Left, hxd.Res.littera_xml.toFont());
  165. tf.maxWidth = 400;
  166. tf = createText(flow, "BMFont XML format (BMFont export)", Align.Left, hxd.Res.bmfont_xml.toFont());
  167. tf.maxWidth = 400;
  168. tf = createText(flow, "BMFont text format (Littera export)", Align.Left, hxd.Res.littera_text.toFont());
  169. tf.maxWidth = 400;
  170. tf = createText(flow, "BMFont text format (BMFont export)", Align.Left, hxd.Res.bmfont_text.toFont());
  171. tf.maxWidth = 400;
  172. tf = createText(flow, "BMFont Binary format", Align.Left, hxd.Res.bmfont_binary.toFont());
  173. tf.maxWidth = 400;
  174. tf = createText(flow, "FontBuilder Divo format", Align.Left, hxd.Res.customFont.toFont());
  175. tf.maxWidth = 400;
  176. // Signed Distance Field textures can be another way to do scalable fonts apart from rasterizing every single size used.
  177. // They also look nice when rotated.
  178. // See here for details: https://github.com/libgdx/libgdx/wiki/Distance-field-fonts
  179. sdfText = createText(flow, "Signed Distance Field texture", Align.Left, hxd.Res.sdf_font.toSdfFont(null, 3));
  180. sdfText.smooth = true; // Smoothing is mandatory when scaling SDF textures.
  181. sdfText.maxWidth = 400;
  182. }
  183. yoffset += flow.getBounds().height + 35;
  184. {
  185. var flow = createFlow(s2d);
  186. flow.y = yoffset;
  187. flow.horizontalAlign = FlowAlign.Left;
  188. flow.horizontalSpacing = 15;
  189. flow.layout = Horizontal;
  190. createText(flow, "LEFT: It is a text with a new line added after THAT\nto test the alignment", Align.Left);
  191. createText(flow, "CENTER: It is a text with a new line added after THAT\nto test the alignment", Align.Center);
  192. createText(flow, "RIGHT: It is a text with a new line added after THAT\nto test the alignment", Align.Right);
  193. createText(flow, "MULTICENTER: It is a text with a new line added after THAT\nto test the alignment", Align.MultilineCenter);
  194. createText(flow, "MULTIRIGHT: It is a text with a new line added after THAT\nto test the alignment", Align.MultilineRight);
  195. }
  196. onResize();
  197. }
  198. override function update(dt:Float) {
  199. for (w in resizeWidgets) {
  200. w.setMaxWidth(Std.int(300 + Math.sin(haxe.Timer.stamp() * 0.5) * 100.0));
  201. }
  202. sdfText.setScale(0.5 + (Math.cos(haxe.Timer.stamp() * 0.5) + 1) * .5);
  203. }
  204. static function main() {
  205. hxd.Res.initEmbed();
  206. new Text();
  207. }
  208. }