Skip to content

Commit

Permalink
Adding basic text alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
MattTuttle committed Nov 12, 2015
1 parent a8b1a6a commit d45c1b7
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 18 deletions.
87 changes: 72 additions & 15 deletions haxepunk/graphics/Text.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,35 @@ import lime.text.TextLayout;

using StringTools;

enum TextHorizontalAlign
{
Left;
Center;
Right;
}

enum TextVerticalAlign
{
Top;
Middle;
Bottom;
}

typedef TextLineChar = {
x:Float,
y:Float,
u:Float,
v:Float,
w:Float,
h:Float,
c:Null<Color>
};

typedef TextLine = {
width:Float,
chars:Array<TextLineChar>
};

typedef GlyphImages = Map<lime.text.Glyph, lime.graphics.Image>;

class Font
Expand Down Expand Up @@ -102,6 +131,12 @@ class Text extends Graphic
*/
public var tabWidth:Int = 4;

public var textWidth(default, null):Float;
public var textHeight(default, null):Float;

public var align:TextHorizontalAlign = Left;
public var verticalAlign:TextVerticalAlign = Top;

/**
* The font size of the Text
*/
Expand Down Expand Up @@ -179,39 +214,47 @@ class Text extends Graphic
// hoisted variables
var x:Float, y:Float, line:String, image,
idx:Int = 0;
_chars = [];
// TODO: handle carriage return!!
var lines = _textLayout.text.split("\n");
var positions = _textLayout.positions;
_lines = [];
for (i in 0...lines.length)
{
line = lines[i];
// TODO: remove magic number (lineHeight * 0.8)
y = lineHeight * i + lineHeight * 0.8;
// text aligns at bottom of characters
y = lineHeight * (i + 1);
x = 0.0;
var chars = new Array<TextLineChar>();
for (j in 0...positions.length)
{
var p = positions[j];
image = _images.get(p.glyph);
if (image != null)
{
_chars[idx++] = {
chars[idx++] = {
x: x + p.offset.x + image.x,
y: y + p.offset.y - image.y,
u: image.offsetX,
v: image.offsetY,
w: image.width,
h: image.height,
c: color
c: null
};
}

x += p.advance.x;
y -= p.advance.y;
}
if (x > width) width = x;
_lines[i] = {
width: x,
chars: chars
};
if (x > textWidth) textWidth = x;
}
height = lineHeight * lines.length;
textHeight = lineHeight * lines.length;
// assign if not already
if (width == 0) width = textWidth;
if (height == 0) height = textHeight;
}

/**
Expand All @@ -220,20 +263,34 @@ class Text extends Graphic
*/
override public function draw(batch:SpriteBatch, offset:Vector3):Void
{
var r,
var r, l,
x = offset.x - origin.x,
y = offset.y - origin.y;
for (i in 0..._chars.length)
var oy = switch (verticalAlign) {
case Top: 0;
case Middle: (height - textHeight) / 2;
case Bottom: (height - textHeight);
};
for (i in 0..._lines.length)
{
r = _chars[i];
batch.draw(material,
x + r.x, y + r.y, r.w, r.h, // position
r.u, r.v, r.w, r.h, // texture coords
false, false, origin.x, origin.y, scale.x, scale.y, 0, r.c);
l = _lines[i];
var ox = switch (align) {
case Left: 0;
case Center: (width - l.width) / 2;
case Right: width - l.width;
}
for (j in 0...l.chars.length)
{
r = l.chars[j];
batch.draw(material,
ox + x + r.x, oy + y + r.y, r.w, r.h, // position
r.u, r.v, r.w, r.h, // texture coords
false, false, origin.x, origin.y, scale.x, scale.y, 0, r.c == null ? color : r.c);
}
}
}

private var _chars:Array<{x:Float, y:Float, u:Float, v:Float, w:Float, h:Float, c:Color}>;
private var _lines:Array<TextLine>;
private var _textLayout:TextLayout;
private var _font:Font;
private var _texture:Texture;
Expand Down
25 changes: 22 additions & 3 deletions haxepunk/mint/Label.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,39 @@ class Label extends BaseRender
super(render, control);
var opt = control.options.options;

color = def(opt.color, new Color().fromInt(0x373737));
color = def(opt.color, new Color().fromInt(0xffffff));
color_hover = def(opt.color_hover, new Color().fromInt(0x9dca63));

visual = new Text(control.text);
switch (control.options.align)
{
case center: visual.align = Center;
case right: visual.align = Right;
default: visual.align = Left;
}
switch (control.options.align_vertical)
{
case center: visual.verticalAlign = Middle;
case bottom: visual.verticalAlign = Bottom;
default: visual.verticalAlign = Top;
}
onbounds();
visual.color = color;
entity.addGraphic(visual);

control.onchange.listen(function(value:String) {
visual.text = value;
});

control.onmouseenter.listen(function(e,c){ visual.color = color_hover; });
control.onmouseleave.listen(function(e,c){ visual.color = color; });
}

function ontext(value:String)
override function onbounds()
{
visual.text = value;
super.onbounds();
visual.width = control.w;
visual.height = control.h;
}

private var visual:Text;
Expand Down

0 comments on commit d45c1b7

Please sign in to comment.