Skip to content

Commit 3064087

Browse files
authored
FlxBitmapText: add autoBounds (#3396)
* add autoBounds * doc * add unit test
1 parent 48833dc commit 3064087

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

flixel/text/FlxBitmapText.hx

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package flixel.text;
22

3-
import openfl.display.BitmapData;
43
import flixel.FlxBasic;
54
import flixel.FlxG;
65
import flixel.FlxSprite;
@@ -12,6 +11,7 @@ import flixel.text.FlxText.FlxTextAlign;
1211
import flixel.text.FlxText.FlxTextBorderStyle;
1312
import flixel.util.FlxColor;
1413
import flixel.util.FlxDestroyUtil;
14+
import openfl.display.BitmapData;
1515
import openfl.geom.ColorTransform;
1616

1717
using flixel.util.FlxColorTransformUtil;
@@ -94,6 +94,13 @@ class FlxBitmapText extends FlxSprite
9494
* Default value if true.
9595
*/
9696
public var autoSize(default, set):Bool = true;
97+
98+
/**
99+
* Whether to autmatically adjust the `width`, `height`, `offset` and
100+
* `origin` whenever the size of the text is changed.
101+
* @since 6.1.0
102+
*/
103+
public var autoBounds(default, set):Bool = true;
97104

98105
/**
99106
* Number of pixels between text and text field border
@@ -1278,11 +1285,8 @@ class FlxBitmapText extends FlxSprite
12781285
borderDrawData.splice(0, borderDrawData.length);
12791286
}
12801287

1281-
// use local var to avoid get_width and recursion
1282-
final newWidth = width = Math.abs(scale.x) * frameWidth;
1283-
final newHeight = height = Math.abs(scale.y) * frameHeight;
1284-
offset.set(-0.5 * (newWidth - frameWidth), -0.5 * (newHeight - frameHeight));
1285-
centerOrigin();
1288+
if (autoBounds)
1289+
autoAdjustBounds();
12861290
}
12871291

12881292
if (!useTiles)
@@ -1397,6 +1401,15 @@ class FlxBitmapText extends FlxSprite
13971401
if (pendingPixelsChange)
13981402
throw "pendingPixelsChange was changed to true while processing changed pixels";
13991403
}
1404+
1405+
function autoAdjustBounds()
1406+
{
1407+
// use local var to avoid get_width and recursion
1408+
final newWidth = width = Math.abs(scale.x) * frameWidth;
1409+
final newHeight = height = Math.abs(scale.y) * frameHeight;
1410+
offset.set(-0.5 * (newWidth - frameWidth), -0.5 * (newHeight - frameHeight));
1411+
centerOrigin();
1412+
}
14001413

14011414
function drawText(posX:Int, posY:Int, isFront:Bool = true, ?bitmap:BitmapData, useTiles:Bool = false):Void
14021415
{
@@ -1610,6 +1623,14 @@ class FlxBitmapText extends FlxSprite
16101623

16111624
return autoSize = value;
16121625
}
1626+
1627+
function set_autoBounds(value:Bool):Bool
1628+
{
1629+
if (autoBounds != value)
1630+
pendingTextChange = true;
1631+
1632+
return this.autoBounds = value;
1633+
}
16131634

16141635
function set_padding(value:Int):Int
16151636
{

tests/unit/src/flixel/text/FlxBitmapTextTest.hx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ class FlxBitmapTextTest extends FlxTest
3232
Assert.areEqual(text1.font, text2.font);
3333
}
3434

35+
@Test // #1526 and #2750
36+
function testAutoBounds()
37+
{
38+
final text = new FlxBitmapText("test");
39+
text.autoBounds = false;
40+
text.offset.x = 100;
41+
text.text = "text.text.text";
42+
@:privateAccess
43+
text.checkPendingChanges(true);
44+
Assert.areEqual(100, text.offset.x);
45+
}
46+
3547
@Test
3648
function testWrapMono()
3749
{

0 commit comments

Comments
 (0)