Hello, I am trying to create a text button using the Flow
class.
My problem is that when I put a Text
in my Flow
, it is not properly centered vertically due to the way getBoundsRec
is computed for Text
, and it ends up looking better if I just stop using Flow
and center the Text
by hand instead:
Here is the code using Flow
:
class Button extends Flow {
public function new(text:String, width:Int, height:Int, parent:Scene) {
super(parent);
this.layout = FlowLayout.Stack;
this.minWidth = width;
this.minHeight = height;
this.borderWidth = 5;
this.borderHeight = 5;
this.verticalAlign = FlowAlign.Middle;
this.horizontalAlign = FlowAlign.Middle;
this.backgroundTile = Res.sprites.ui.toTile();
var font = Res.fonts.atlantis_text_bold_32.toFont();
var label = new Text(font, this);
label.text = text;
}
}
And here is the code computing the position of Text
by hand:
class Button {
var background:ScaleGrid;
var font:Font;
var label:Text;
var width:Float;
var height:Float;
public function new(text:String, width:Float, height:Float, parent:Scene) {
this.width = width;
this.height = height;
background = new ScaleGrid(Res.sprites.ui.toTile(), 5, 5, parent);
background.width = width;
background.height = height;
font = Res.fonts.atlantis_text_bold_32.toFont();
label = new Text(font, background);
label.text = text;
label.x = width * 0.5 - label.textWidth * 0.5;
label.y = height * 0.5 - label.textHeight * 0.5;
}
}
In the exemple I use a custom font which kind of exacerbates the problem, but it is still present using the default font instead.
So should I just abandon the idea of using a Flow
like this or is there something I’m missing ?
Thanks in advance !