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

feat: added outline support for TextBlock #2582

Merged
merged 4 commits into from
Dec 26, 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
16 changes: 16 additions & 0 deletions sources/engine/Stride.UI/Controls/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ public float TextSize
[Display(category: AppearanceCategory)]
public Color TextColor { get; set; } = Color.FromAbgr(0xF0F0F0FF);

/// <summary>
/// Gets or sets the Text outline color.
/// </summary>
/// <userdoc>The outline color of the text.</userdoc>
[DataMember]
[Display(category: AppearanceCategory)]
public Color OutlineColor { get; set; } = Color.Black;

/// <summary>
/// Gets or sets the Text outline thickness.
/// </summary>
/// <userdoc>The outline thickness of the text.</userdoc>
[DataMember]
[Display(category: AppearanceCategory)]
public float OutlineThickness { get; set; } = 0.0f;

/// <summary>
/// Gets or sets the alignment of the text to display.
/// </summary>
Expand Down
26 changes: 26 additions & 0 deletions sources/engine/Stride.UI/Renderers/DefaultTextBlockRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ public override void RenderColor(UIElement element, UIRenderingContext context)
Batch.BeginCustom(context.GraphicsContext, 1);
}

if (textBlock.OutlineColor.A != 0 && textBlock.OutlineThickness > 0)
{
var borderThickness = textBlock.OutlineThickness;
var borderColor = textBlock.RenderOpacity * textBlock.OutlineColor;


for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
if (x == 0 && y == 0) continue;

var offsetX = x * borderThickness;
var offsetY = y * borderThickness;

var borderDrawCommand = drawCommand;
borderDrawCommand.Color = borderColor;
borderDrawCommand.Matrix = drawCommand.Matrix * Matrix.Translation(offsetX, offsetY, 0);

Batch.DrawString(textBlock.Font, textBlock.TextToDisplay, ref borderDrawCommand);
}
}

drawCommand.DepthBias += 1;
}

Batch.DrawString(textBlock.Font, textBlock.TextToDisplay, ref drawCommand);

if (textBlock.Font.FontType == SpriteFontType.SDF)
Expand Down