import h2d.Drawable;
import h2d.Flow;
import h2d.Font;
import h2d.Graphics;
import h2d.Object;
import h2d.Text.Align;
// Use both text_res and res folders.
//PARAM=-D resourcesPath=../../text_res;../../res
class HtmlTextWidget extends Object
{
public var align: Align;
public var textField: h2d.HtmlText;
public var back: Graphics;
public function new(parent:h2d.Scene, font: Font, str:String, align:h2d.Text.Align){
super(parent);
this.align = align;
back = new Graphics(this);
var tf = new h2d.HtmlText(font, this);
tf.textColor = 0xffffff;
tf.textAlign = align;
tf.text = str;
textField = tf;
refreshBounds();
}
public function refreshBounds() {
back.clear();
var bounds = textField.getBounds(this);
var size = textField.getSize();
back.beginFill(0x5050ff, 0.5);
back.drawRect(bounds.x, 0, size.width, size.height);
back.endFill();
back.lineStyle(1, 0x50ff50);
back.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);
back.lineStyle(1, 0xff5050);
back.moveTo(bounds.x, 0);
back.lineTo(bounds.x + textField.textWidth, 0);
back.moveTo(bounds.x, 0);
back.lineTo(bounds.x, textField.textHeight);
}
public function setMaxWidth(w:Int) {
textField.maxWidth = w;
refreshBounds();
}
}
class HtmlText extends hxd.App {
var textWidgets:Array <p> tag: Forces line breaks before and after. It also supports `align` property.
Lorem ipsum dolor";
var singleText = "Hello simple text";
var xpos = 0;
var yoffset = 10.0;
function createWidget(str:String, align:h2d.Text.Align) {
var w = new HtmlTextWidget(s2d, font, str, align);
w.x = xpos;
w.y = yoffset;
textWidgets.push(w);
return w;
}
// Static single and multiline widgets
xpos += 450;
for (a in [Align.Left, Align.Center, Align.Right, Align.MultilineCenter, Align.MultilineRight]) {
var w = createWidget("", a);
var label = new h2d.HtmlText(font, w);
label.text = Std.string(a);
label.x = 5;
label.alpha = 0.5;
yoffset += w.textField.textHeight + 10;
var w = createWidget(singleText, a);
yoffset += w.textField.textHeight + 10;
var w = createWidget(multilineText, a);
yoffset += w.textField.textHeight + 10;
}
// Resized widgets
xpos += 200;
yoffset = 10;
var longText = "Long text long text. Icons like this one are flowed separately, but they should stick to the text when they appear
before or after
. We support different font faces";
for (a in [Align.Left, Align.Center, Align.Right, Align.MultilineCenter, Align.MultilineRight]) {
var w = createWidget(longText, a);
w.setMaxWidth(200);
resizeWidgets.push(w);
yoffset += 160;
}
// Flows
function createText(parent:Object, str : String, align:Align) {
var tf = new h2d.HtmlText(font, parent);
tf.textColor = 0xffffff;
tf.textAlign = align;
tf.text = str;
tf.maxWidth = 150;
return tf;
}
function createFlow(parent:Object) {
var flow = new Flow(parent);
flow.debug = true;
flow.horizontalSpacing = 5;
flow.verticalSpacing = 5;
flow.padding = 5;
return flow;
}
yoffset = 0;
var flow = createFlow(s2d);
flow.verticalAlign = FlowAlign.Middle;
createText(flow, singleText, Align.Left);
createText(flow, multilineText, Align.Left);
yoffset += flow.getBounds().height + 10;
var flow = createFlow(s2d);
flow.y = yoffset;
flow.multiline = false;
flow.verticalAlign = FlowAlign.Middle;
createText(flow, multilineText, Align.Center);
createText(flow, multilineText, Align.Right);
yoffset += flow.getBounds().height + 10;
var flow = createFlow(s2d);
flow.y = yoffset;
flow.verticalAlign = FlowAlign.Middle;
flow.maxWidth = 150;
createText(flow, singleText, Align.Left);
createText(flow, multilineText, Align.Center);
yoffset += flow.getBounds().height + 10;
var flow = createFlow(s2d);
flow.y = yoffset;
flow.horizontalAlign = FlowAlign.Middle;
flow.maxWidth = 150;
flow.layout = Vertical;
createText(flow, singleText, Align.Left);
createText(flow, multilineText, Align.Right);
yoffset += flow.getBounds().height + 10;
{
var flow = createFlow(s2d);
flow.y = yoffset;
flow.horizontalAlign = FlowAlign.Left;
flow.maxWidth = 360;
flow.horizontalSpacing = 8;
flow.layout = Horizontal;
var f = createText(flow, "short text", Align.Right);
createText(flow, singleText, Align.Left);
yoffset += flow.getBounds().height + 10;
}
var flow = createFlow(s2d);
flow.y = yoffset;
flow.x = 100;
flow.horizontalAlign = FlowAlign.Middle;
flow.layout = Vertical;
{
var f1 = createFlow(flow);
createText(f1, multilineText, Align.Left);
var f2 = createFlow(flow);
createText(f2, multilineText, Align.Left);
}
yoffset += flow.getBounds().height + 10;
var tagShowcase = "HtmlText supports next tags:" +
"
<br/> tag: Makes a
line break
<font> tag: Allows to control color, opacity and face properties.
" + "<img src='...'/> tag: Allows to insert a Tile into html text