HtmlText.hx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 HtmlTextWidget extends Object
  10. {
  11. public var align: Align;
  12. public var textField: h2d.HtmlText;
  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.HtmlText(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 HtmlText extends hxd.App {
  46. var textWidgets:Array<HtmlTextWidget> = [];
  47. var resizeWidgets: Array<HtmlTextWidget> = [];
  48. override function init() {
  49. // Enable global scaling
  50. // s2d.scale(1.25);
  51. var font = hxd.res.DefaultFont.get();
  52. // var font = hxd.Res.customFont.toFont();
  53. h2d.HtmlText.defaultLoadFont = function( face : String ) : h2d.Font {
  54. if ( face == 'myFontFace' ) {
  55. var font = hxd.res.DefaultFont.get().clone();
  56. font.resizeTo(font.size * 2);
  57. return font;
  58. }
  59. return null;
  60. }
  61. h2d.HtmlText.defaultLoadImage = function( src : String ) : h2d.Tile {
  62. if ( src == "logo" ) {
  63. var t = hxd.Res.hxlogo.toTile();
  64. t.scaleToSize(16, 16);
  65. return t;
  66. }
  67. return null;
  68. }
  69. var multilineText = "This is a multiline <font color=\"#FF00FF\">text.<br/>Lorem</font> ipsum dolor";
  70. var singleText = "Hello simple text";
  71. var xpos = 0;
  72. var yoffset = 10.0;
  73. function createWidget(str:String, align:h2d.Text.Align) {
  74. var w = new HtmlTextWidget(s2d, font, str, align);
  75. w.x = xpos;
  76. w.y = yoffset;
  77. textWidgets.push(w);
  78. return w;
  79. }
  80. // Static single and multiline widgets
  81. xpos += 450;
  82. for (a in [Align.Left, Align.Center, Align.Right, Align.MultilineCenter, Align.MultilineRight]) {
  83. var w = createWidget("", a);
  84. var label = new h2d.HtmlText(font, w);
  85. label.text = Std.string(a);
  86. label.x = 5;
  87. label.alpha = 0.5;
  88. yoffset += w.textField.textHeight + 10;
  89. var w = createWidget(singleText, a);
  90. yoffset += w.textField.textHeight + 10;
  91. var w = createWidget(multilineText, a);
  92. yoffset += w.textField.textHeight + 10;
  93. }
  94. // Resized widgets
  95. xpos += 200;
  96. yoffset = 10;
  97. var longText = "Long text long text. Icons like this one <img src='logo'/> are flowed separately, but they should <font color=\"#FF00FF\">stick</font> to the text when they appear <img src='logo'/>before or after<img src='logo'/>. We support different <font face='myFontFace'>font faces</font>";
  98. for (a in [Align.Left, Align.Center, Align.Right, Align.MultilineCenter, Align.MultilineRight]) {
  99. var w = createWidget(longText, a);
  100. w.setMaxWidth(200);
  101. resizeWidgets.push(w);
  102. yoffset += 160;
  103. }
  104. // Flows
  105. function createText(parent:Object, str : String, align:Align) {
  106. var tf = new h2d.HtmlText(font, parent);
  107. tf.textColor = 0xffffff;
  108. tf.textAlign = align;
  109. tf.text = str;
  110. tf.maxWidth = 150;
  111. return tf;
  112. }
  113. function createFlow(parent:Object) {
  114. var flow = new Flow(parent);
  115. flow.debug = true;
  116. flow.horizontalSpacing = 5;
  117. flow.verticalSpacing = 5;
  118. flow.padding = 5;
  119. return flow;
  120. }
  121. yoffset = 0;
  122. var flow = createFlow(s2d);
  123. flow.verticalAlign = FlowAlign.Middle;
  124. createText(flow, singleText, Align.Left);
  125. createText(flow, multilineText, Align.Left);
  126. yoffset += flow.getBounds().height + 10;
  127. var flow = createFlow(s2d);
  128. flow.y = yoffset;
  129. flow.multiline = false;
  130. flow.verticalAlign = FlowAlign.Middle;
  131. createText(flow, multilineText, Align.Center);
  132. createText(flow, multilineText, Align.Right);
  133. yoffset += flow.getBounds().height + 10;
  134. var flow = createFlow(s2d);
  135. flow.y = yoffset;
  136. flow.verticalAlign = FlowAlign.Middle;
  137. flow.maxWidth = 150;
  138. createText(flow, singleText, Align.Left);
  139. createText(flow, multilineText, Align.Center);
  140. yoffset += flow.getBounds().height + 10;
  141. var flow = createFlow(s2d);
  142. flow.y = yoffset;
  143. flow.horizontalAlign = FlowAlign.Middle;
  144. flow.maxWidth = 150;
  145. flow.layout = Vertical;
  146. createText(flow, singleText, Align.Left);
  147. createText(flow, multilineText, Align.Right);
  148. yoffset += flow.getBounds().height + 10;
  149. {
  150. var flow = createFlow(s2d);
  151. flow.y = yoffset;
  152. flow.horizontalAlign = FlowAlign.Left;
  153. flow.maxWidth = 360;
  154. flow.horizontalSpacing = 8;
  155. flow.layout = Horizontal;
  156. var f = createText(flow, "short text", Align.Right);
  157. createText(flow, singleText, Align.Left);
  158. yoffset += flow.getBounds().height + 10;
  159. }
  160. var flow = createFlow(s2d);
  161. flow.y = yoffset;
  162. flow.x = 100;
  163. flow.horizontalAlign = FlowAlign.Middle;
  164. flow.layout = Vertical;
  165. {
  166. var f1 = createFlow(flow);
  167. createText(f1, multilineText, Align.Left);
  168. var f2 = createFlow(flow);
  169. createText(f2, multilineText, Align.Left);
  170. }
  171. yoffset += flow.getBounds().height + 10;
  172. var tagShowcase = "HtmlText supports next tags:" +
  173. "<p>&lt;p&gt; tag:<p align='center'>Forces line breaks before and after.</p><p align='right'>It also supports `align` property.</p></p>" +
  174. "<p>&lt;br/&gt; tag: Makes a<br/>line break</p>" +
  175. "<p>&lt;font&gt; tag: Allows to control <font color='#ff00ff'>color</font>, <font opacity='0.8'>op</font><font opacity='0.6'>ac</font><font opacity='0.4'>it</font><font opacity='0.2'>y</font> and <font face='myFontFace'>face</font> properties.</p>" +
  176. "<p>&lt;img src='...'/&gt; tag: Allows to insert a Tile <img src='logo'/> into html text</p>";
  177. var flow = createFlow(s2d);
  178. flow.y = yoffset;
  179. flow.maxWidth = 360;
  180. var text = createText(flow, tagShowcase, Align.Left);
  181. text.maxWidth = 360;
  182. onResize();
  183. }
  184. override function update(dt:Float) {
  185. for (w in resizeWidgets) {
  186. w.setMaxWidth(Std.int(300 + Math.sin(haxe.Timer.stamp() * 0.2) * 100.0));
  187. }
  188. }
  189. static function main() {
  190. hxd.Res.initEmbed();
  191. new HtmlText();
  192. }
  193. }