Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 93 additions & 62 deletions flixel/text/FlxText.hx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class FlxText extends FlxSprite
* Reference to a `TextField` object used internally for rendering -
* be sure to know what you're doing if messing with its properties!
*/
public var textField(default, null):TextField;
public var textField(default, null):TextField = new TextField();

/**
* The width of the `TextField` object used for bitmap generation for this `FlxText` object.
Expand Down Expand Up @@ -175,7 +175,7 @@ class FlxText extends FlxSprite
var _graphicOffset:FlxPoint = FlxPoint.get(0, 0);

var _defaultFormat:TextFormat;
var _formatAdjusted:TextFormat;
var _formatAdjusted = new TextFormat();
var _formatRanges:Array<FlxTextFormatRange> = [];
var _font:String;

Expand Down Expand Up @@ -211,35 +211,33 @@ class FlxText extends FlxSprite
* @param Size The font size for this text object.
* @param EmbeddedFont Whether this text field uses embedded fonts or not.
*/
public function new(X:Float = 0, Y:Float = 0, FieldWidth:Float = 0, ?Text:String, Size:Int = 8, EmbeddedFont:Bool = true)
public function new(x = 0.0, y = 0.0, fieldWidth = 0.0, ?text:String, size = 8, embeddedFont = true)
{
super(X, Y);
textField.selectable = false;
textField.multiline = true;
textField.wordWrap = true;
_defaultFormat = new TextFormat(null, size, 0xffffff);

super(x, y);

if (Text == null || Text == "")
if (text == null || text == "")
{
// empty texts have a textHeight of 0, need to
// prevent initializing with "" before the first calcFrame() call
text = "";
Text = " ";
text = " ";
}
else
{
text = Text;
this.text = text;
}

textField = new TextField();
textField.selectable = false;
textField.multiline = true;
textField.wordWrap = true;
_defaultFormat = new TextFormat(null, Size, 0xffffff);

letterSpacing = 0;
font = FlxAssets.FONT_DEFAULT;
_formatAdjusted = new TextFormat();
textField.defaultTextFormat = _defaultFormat;
textField.text = Text;
fieldWidth = FieldWidth;
textField.embedFonts = EmbeddedFont;
textField.height = (Text.length <= 0) ? 1 : 10;
textField.text = text;
this.fieldWidth = fieldWidth;
textField.embedFonts = embeddedFont;
textField.height = (text.length <= 0) ? 1 : 10;

// call this just to set the textfield's properties
set_antialiasing(antialiasing);
Expand Down Expand Up @@ -571,19 +569,20 @@ class FlxText extends FlxSprite

if (value <= 0)
{
wordWrap = false;
autoSize = true;
wordWrap = false;
// auto width always implies auto height
_regen = _regen || !_autoHeight;
_autoHeight = true;
}
else
{
autoSize = false;
wordWrap = true;
_regen = _regen || textField.width != value;
textField.width = value;
}

_regen = true;
return value;
}

Expand All @@ -599,29 +598,38 @@ class FlxText extends FlxSprite

function set_fieldHeight(value:Float):Float
{
// TODO: Remove if and let it crash on 7.0.0
if (textField == null)
{
FlxG.log.error("Cannot set fieldHeight of destroyed FlxText");
return value;
}

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

function set_autoSize(value:Bool):Bool
{
// TODO: Remove if and let it crash on 7.0.0
if (textField != null)
{
textField.autoSize = value ? TextFieldAutoSize.LEFT : TextFieldAutoSize.NONE;
_regen = true;
final newValue = value ? TextFieldAutoSize.LEFT : TextFieldAutoSize.NONE;
_regen = _regen || textField.autoSize != newValue;
textField.autoSize = newValue;
}
else
FlxG.log.error("Cannot set autosize of destroyed FlxText");

return value;
}
Expand All @@ -631,40 +639,50 @@ class FlxText extends FlxSprite
return (textField != null) ? (textField.autoSize != TextFieldAutoSize.NONE) : false;
}

function set_text(Text:String):String
function set_text(value:String):String
{
text = Text;
// TODO: Remove if and let it crash on 7.0.0
if (textField != null)
{
var ot:String = textField.text;
textField.text = Text;
_regen = (textField.text != ot) || _regen;
_regen = _regen || (this.text != value);
textField.text = value;
}
return Text;
else
FlxG.log.error("Cannot set text of destroyed FlxText");

return this.text = value;
}

inline function get_size():Int
{
return Std.int(_defaultFormat.size);
}

function set_size(Size:Int):Int
function set_size(value:Int):Int
{
_defaultFormat.size = Size;
updateDefaultFormat();
return Size;
if (_defaultFormat.size != value)
{
_defaultFormat.size = value;
updateDefaultFormat();
}

return value;
}

inline function get_letterSpacing():Float
{
return _defaultFormat.letterSpacing;
}

function set_letterSpacing(LetterSpacing:Float):Float
function set_letterSpacing(value:Float):Float
{
_defaultFormat.letterSpacing = LetterSpacing;
updateDefaultFormat();
return LetterSpacing;
if (_defaultFormat.letterSpacing != value)
{
_defaultFormat.letterSpacing = value;
updateDefaultFormat();
}

return value;
}

override function setColorTransform(redMultiplier = 1.0, greenMultiplier = 1.0, blueMultiplier = 1.0, alphaMultiplier = 1.0, redOffset = 0.0, greenOffset = 0.0, blueOffset = 0.0, alphaOffset = 0.0)
Expand All @@ -691,27 +709,33 @@ class FlxText extends FlxSprite
return _font;
}

function set_font(Font:String):String
function set_font(value:String):String
{
final newFont = getFontHelper(value);

_regen = _regen || !textField.embedFonts;
textField.embedFonts = true;

if (Font != null)
if (_defaultFormat.font != newFont)
{
var newFontName:String = Font;
if (FlxG.assets.exists(Font, FONT))
{
newFontName = FlxG.assets.getFontUnsafe(Font).fontName;
}

_defaultFormat.font = newFontName;
_defaultFormat.font = newFont;
updateDefaultFormat();
}
else

return _font = newFont;
}

static function getFontHelper(font:String)
{
if (font != null)
{
_defaultFormat.font = FlxAssets.FONT_DEFAULT;
if (FlxG.assets.exists(font, FONT))
return FlxG.assets.getFontUnsafe(font).fontName;

return font;
}

updateDefaultFormat();
return _font = _defaultFormat.font;

return FlxAssets.FONT_DEFAULT;
}

inline function get_embedded():Bool
Expand All @@ -724,12 +748,18 @@ class FlxText extends FlxSprite
return _defaultFormat.font;
}

function set_systemFont(Font:String):String
function set_systemFont(value:String):String
{
_regen = _regen || textField.embedFonts;
textField.embedFonts = false;
_defaultFormat.font = Font;
updateDefaultFormat();
return Font;

if (_defaultFormat.font != value)
{
_defaultFormat.font = value;
updateDefaultFormat();
}

return value;
}

inline function get_bold():Bool
Expand Down Expand Up @@ -874,6 +904,8 @@ class FlxText extends FlxSprite
if (textField == null || !_regen)
return;

_regen = false;

final oldWidth:Int = graphic != null ? graphic.width : 0;
final oldHeight:Int = graphic != null ? graphic.height : VERTICAL_GUTTER;

Expand Down Expand Up @@ -955,8 +987,7 @@ class FlxText extends FlxSprite

drawTextFieldTo(graphic.bitmap);
}

_regen = false;

resetFrame();
}

Expand Down Expand Up @@ -1291,6 +1322,8 @@ class FlxText extends FlxSprite

override function set_antialiasing(value:Bool):Bool
{
_regen = _regen || this.antialiasing != value;

if (value)
{
textField.antiAliasType = NORMAL;
Expand All @@ -1301,9 +1334,7 @@ class FlxText extends FlxSprite
textField.antiAliasType = ADVANCED;
textField.sharpness = 400;
}

_regen = true;


return antialiasing = value;
}
}
Expand Down