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

FlxTextBorderStyle: Add SHADOW_XY, prevent border clipping #3236

Merged
merged 9 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
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
73 changes: 47 additions & 26 deletions flixel/text/FlxBitmapText.hx
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,18 @@ class FlxBitmapText extends FlxSprite
* NOTE: If the borderSize is 1, borderQuality of 0 or 1 will have the exact same effect (and performance).
*/
public var borderQuality(default, set):Float = 0;


/**
* Internal handler for deprecated `shadowOffset` field
*/
var _shadowOffset:FlxPoint = FlxPoint.get(1, 1);

/**
* 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.
* `x` and `y` are multiplied by `borderSize`. Default is `(1, 1)`, or lower-right corner.
*/
public var shadowOffset(default, null):FlxPoint;
@:deprecated("shadowOffset is deprecated, use setBorderStyle(SHADOW_XY(offsetX, offsetY)), instead") // 5.9.0
public var shadowOffset(get, never):FlxPoint;

/**
* Specifies whether the text should have a background. It is recommended to use a
Expand Down Expand Up @@ -220,8 +226,6 @@ class FlxBitmapText extends FlxSprite

this.font = (font == null) ? FlxBitmapFont.getDefaultFont() : font;

shadowOffset = FlxPoint.get(1, 1);

if (FlxG.renderBlit)
{
pixels = new BitmapData(1, 1, true, FlxColor.TRANSPARENT);
Expand All @@ -247,7 +251,7 @@ class FlxBitmapText extends FlxSprite
_lines = null;
_linesWidth = null;

shadowOffset = FlxDestroyUtil.put(shadowOffset);
_shadowOffset = FlxDestroyUtil.put(_shadowOffset);
textBitmap = FlxDestroyUtil.dispose(textBitmap);

_colorParams = null;
Expand Down Expand Up @@ -1293,34 +1297,46 @@ class FlxBitmapText extends FlxSprite

var delta:Int = Std.int(borderSize / iterations);

var iterationsX:Int = 1;
var iterationsY:Int = 1;
var deltaX:Int = 1;
var deltaY:Int = 1;

if (borderStyle == FlxTextBorderStyle.SHADOW)
{
iterationsX = Math.round(Math.abs(shadowOffset.x) * borderQuality);
iterationsX = (iterationsX <= 0) ? 1 : iterationsX;

iterationsY = Math.round(Math.abs(shadowOffset.y) * borderQuality);
iterationsY = (iterationsY <= 0) ? 1 : iterationsY;

deltaX = Math.round(shadowOffset.x / iterationsX);
deltaY = Math.round(shadowOffset.y / iterationsY);
}

// render border
switch (borderStyle)
{
case SHADOW:
case SHADOW if (_shadowOffset.x != 1 || _shadowOffset.y != 1):
var iterationsX = Math.round(Math.abs(_shadowOffset.x) * borderQuality);
iterationsX = (iterationsX <= 0) ? 1 : iterationsX;

var iterationsY = Math.round(Math.abs(_shadowOffset.y) * borderQuality);
iterationsY = (iterationsY <= 0) ? 1 : iterationsY;

final deltaX = Math.round(_shadowOffset.x / iterationsX);
final deltaY = Math.round(_shadowOffset.y / iterationsY);

for (iterY in 0...iterationsY)
{
for (iterX in 0...iterationsX)
{
drawText(deltaX * (iterX + 1), deltaY * (iterY + 1), isFront, bitmap, useTiles);
}
}

case SHADOW:
final iterations = borderQuality < 1 ? 1 : Std.int(Math.abs(borderSize) * borderQuality);
final delta = borderSize / iterations;
var i = iterations + 1;
while (i-- > 1)
{
drawText(Std.int(delta * i), Std.int(delta * i), isFront, bitmap, useTiles);
}

case SHADOW_XY(shadowX, shadowY):
// Size is max of both, so (4, 4) has 4 iterations, just like SHADOW
final size = Math.max(shadowX, shadowY);
final iterations = borderQuality < 1 ? 1 : Std.int(size * borderQuality);
var i = iterations + 1;
while (i-- > 1)
{
drawText(Std.int(shadowX / iterations * i), Std.int(shadowY / iterations * i), isFront, bitmap, useTiles);
}

case OUTLINE:
// Render an outline around the text
// (do 8 offset draw calls)
Expand Down Expand Up @@ -1482,7 +1498,7 @@ class FlxBitmapText extends FlxSprite
borderQuality = quality;
if (borderStyle == FlxTextBorderStyle.SHADOW)
{
shadowOffset.set(borderSize, borderSize);
_shadowOffset.set(borderSize, borderSize);
}
pendingTextBitmapChange = true;
}
Expand Down Expand Up @@ -1742,7 +1758,12 @@ class FlxBitmapText extends FlxSprite
checkPendingChanges(true);
return super.get_height();
}


inline function get_shadowOffset()
{
return _shadowOffset;
}

/**
* Checks if the specified code is one of the Unicode Combining Diacritical Marks
* @param Code The charactercode we want to check
Expand Down
Loading
Loading