Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fieldHeight prop for FlxText to control textfield height #2789

Merged
merged 9 commits into from
Jun 5, 2023
58 changes: 52 additions & 6 deletions flixel/text/FlxText.hx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ import openfl.utils.AssetType;
/**
* Extends FlxSprite to support rendering text. Can tint, fade, rotate and scale just like a sprite. Doesn't really animate
* though. Also does nice pixel-perfect centering on pixel fonts as long as they are only one-liners.
*
* ## Autosizing
*
* By default `FlxText` is autosized to fit it's text.
* To set a fixed size, * use the `fieldWidth`, `fieldHeight` and `autoSize` fields.
*/
class FlxText extends FlxSprite
{
/**
* 2px gutter on both top and bottom
* 4px gutter at the bottom when the field has automatic height
*/
static inline var VERTICAL_GUTTER:Int = 4;

Expand Down Expand Up @@ -122,14 +127,27 @@ class FlxText extends FlxSprite
/**
* The width of the `TextField` object used for bitmap generation for this `FlxText` object.
* Use it when you want to change the visible width of text. Enables `autoSize` if `<= 0`.
*
* **NOTE:** auto width always implies auto height
*/
public var fieldWidth(get, set):Float;

/**
* Whether the `fieldWidth` should be determined automatically. Requires `wordWrap` to be `false`.
* The height of `TextField` object used for bitmap generation for this `FlxText` object.
* Use it when you want to change the visible height of the text. Enables "auto height" if `<= 0`.
*
* **NOTE:** Fixed height has no effect if `autoSize = true`.
*/
public var fieldHeight(get, set):Float;

/**
* Whether the `fieldWidth` and `fieldHeight` should be determined automatically.
* Requires `wordWrap` to be `false`.
*/
public var autoSize(get, set):Bool;

var _autoHeight:Bool = true;

/**
* Offset that is applied to the shadow border style, if active.
* `x` and `y` are multiplied by `borderSize`. Default is `(1, 1)`, or lower-right corner.
Expand Down Expand Up @@ -527,6 +545,8 @@ class FlxText extends FlxSprite
{
wordWrap = false;
autoSize = true;
// auto width always implies auto height
_autoHeight = true;
}
else
{
Expand All @@ -544,6 +564,29 @@ class FlxText extends FlxSprite
return (textField != null) ? textField.width : 0;
}

function get_fieldHeight():Float
{
return (textField != null) ? textField.height : 0;
}

function set_fieldHeight(value:Float):Float
{
if (textField == null)
return value;

if (value <= 0)
{
_autoHeight = true;
}
else
{
_autoHeight = false;
textField.height = value;
}
_regen = true;
return value;
}

function set_autoSize(value:Bool):Bool
{
if (textField != null)
Expand Down Expand Up @@ -786,8 +829,10 @@ class FlxText extends FlxSprite
}

var newWidth:Int = Math.ceil(textField.width);
var textfieldHeight = _autoHeight ? textField.textHeight : textField.height;
var vertGutter = _autoHeight ? VERTICAL_GUTTER : 0;
// Account for gutter
var newHeight:Int = Math.ceil(textField.textHeight) + VERTICAL_GUTTER;
var newHeight:Int = Math.ceil(textfieldHeight) + vertGutter;

// prevent text height from shrinking on flash if text == ""
if (textField.textHeight == 0)
Expand All @@ -798,14 +843,15 @@ class FlxText extends FlxSprite
if (oldWidth != newWidth || oldHeight != newHeight)
{
// Need to generate a new buffer to store the text graphic
height = newHeight;
var key:String = FlxG.bitmap.getUniqueKey("text");
makeGraphic(newWidth, newHeight, FlxColor.TRANSPARENT, false, key);

if (_hasBorderAlpha)
_borderPixels = graphic.bitmap.clone();
frameHeight = newHeight;
textField.height = height * 1.2;

if (_autoHeight)
textField.height = newHeight;

_flashRect.x = 0;
_flashRect.y = 0;
_flashRect.width = newWidth;
Expand Down