Move fallback rendering to new class

This commit is contained in:
ExMatics HydrogenC
2023-11-30 23:22:11 +08:00
parent cdbe59a3fb
commit 623f478b44
30 changed files with 165 additions and 217 deletions

View File

@@ -43,7 +43,7 @@ namespace FlaxEditor.CustomEditors.Dedicated
// Add script button // Add script button
var buttonText = "Add script"; var buttonText = "Add script";
var textSize = Render2D.MeasureText(Style.Current.FontMedium, buttonText); var textSize = FallbackTextUtils.MeasureText(Style.Current.FontMedium, buttonText);
float addScriptButtonWidth = (textSize.X < 60.0f) ? 60.0f : textSize.X + 4; float addScriptButtonWidth = (textSize.X < 60.0f) ? 60.0f : textSize.X + 4;
var buttonHeight = (textSize.Y < 18) ? 18 : textSize.Y + 4; var buttonHeight = (textSize.Y < 18) ? 18 : textSize.Y + 4;
_addScriptsButton = new Button _addScriptsButton = new Button

View File

@@ -423,7 +423,7 @@ namespace FlaxEditor.CustomEditors.Dedicated
// Set control type button // Set control type button
var space = layout.Space(20); var space = layout.Space(20);
var buttonText = "Set Type"; var buttonText = "Set Type";
var textSize = Render2D.MeasureText(FlaxEngine.GUI.Style.Current.FontMedium, buttonText); var textSize = FallbackTextUtils.MeasureText(FlaxEngine.GUI.Style.Current.FontMedium, buttonText);
float setTypeButtonWidth = (textSize.X < 60.0f) ? 60.0f : textSize.X + 4; float setTypeButtonWidth = (textSize.X < 60.0f) ? 60.0f : textSize.X + 4;
var setTypeButton = new Button var setTypeButton = new Button
{ {

View File

@@ -101,7 +101,7 @@ namespace FlaxEditor.CustomEditors.Editors
_linkButton.Clicked += ToggleLink; _linkButton.Clicked += ToggleLink;
ToggleEnabled(); ToggleEnabled();
SetLinkStyle(); SetLinkStyle();
var textSize = Render2D.MeasureText(FlaxEngine.GUI.Style.Current.FontMedium, LinkedLabel.Text.Value); var textSize = FallbackTextUtils.MeasureText(FlaxEngine.GUI.Style.Current.FontMedium, LinkedLabel.Text.Value);
_linkButton.LocalX += textSize.X + 10; _linkButton.LocalX += textSize.X + 10;
LinkedLabel.SetupContextMenu += (label, menu, editor) => LinkedLabel.SetupContextMenu += (label, menu, editor) =>
{ {

View File

@@ -631,7 +631,7 @@ namespace FlaxEditor.CustomEditors.Editors
TooltipText = "Edit...", TooltipText = "Edit...",
Parent = _label, Parent = _label,
}; };
var textSize = Render2D.MeasureText(FlaxEngine.GUI.Style.Current.FontMedium, buttonText); var textSize = FallbackTextUtils.MeasureText(FlaxEngine.GUI.Style.Current.FontMedium, buttonText);
if (textSize.Y > button.Width) if (textSize.Y > button.Width)
button.Width = textSize.Y + 2; button.Width = textSize.Y + 2;

View File

@@ -236,9 +236,9 @@ namespace FlaxEditor.GUI.ContextMenu
float width = 20; float width = 20;
if (style.FontMedium) if (style.FontMedium)
{ {
width += Render2D.MeasureText(style.FontMedium, Text).X; width += FallbackTextUtils.MeasureText(style.FontMedium, Text).X;
if (!string.IsNullOrEmpty(ShortKeys)) if (!string.IsNullOrEmpty(ShortKeys))
width += 40 + Render2D.MeasureText(style.FontMedium, ShortKeys).X; width += 40 + FallbackTextUtils.MeasureText(style.FontMedium, ShortKeys).X;
} }
return Mathf.Max(width, base.MinimumWidth); return Mathf.Max(width, base.MinimumWidth);

View File

@@ -489,7 +489,7 @@ namespace FlaxEditor.GUI.Docking
{ {
var style = Style.Current; var style = Style.Current;
if (style?.FontMedium != null) if (style?.FontMedium != null)
_titleSize = Render2D.MeasureText(style.FontMedium, _title); _titleSize = FallbackTextUtils.MeasureText(style.FontMedium, _title);
} }
base.PerformLayoutBeforeChildren(); base.PerformLayoutBeforeChildren();

View File

@@ -103,7 +103,7 @@ namespace FlaxEditor.GUI
float width = 18; float width = 18;
if (style.FontMedium) if (style.FontMedium)
width += Render2D.MeasureText(style.FontMedium, Text).X; width += FallbackTextUtils.MeasureText(style.FontMedium, Text).X;
Width = width; Width = width;
} }

View File

@@ -68,7 +68,7 @@ namespace FlaxEditor.GUI
if (style.FontMedium) if (style.FontMedium)
{ {
Width = Render2D.MeasureText(style.FontMedium, Text).X + 2 * DefaultMargin; Width = FallbackTextUtils.MeasureText(style.FontMedium, Text).X + 2 * DefaultMargin;
} }
} }
} }

View File

@@ -345,7 +345,7 @@ namespace FlaxEditor.GUI.Timeline.Tracks
if (_previewValue != null) if (_previewValue != null)
{ {
// Based on Track.Draw for track text placement // Based on Track.Draw for track text placement
var left = _xOffset + 16 + Render2D.MeasureText(Style.Current.FontSmall, Title ?? Name).X; var left = _xOffset + 16 + FallbackTextUtils.MeasureText(Style.Current.FontSmall, Title ?? Name).X;
if (Icon.IsValid) if (Icon.IsValid)
left += 18; left += 18;
if (IsExpanded) if (IsExpanded)

View File

@@ -152,7 +152,7 @@ namespace FlaxEditor.GUI
if (hasSprite) if (hasSprite)
width += iconSize; width += iconSize;
if (!string.IsNullOrEmpty(_text) && style.FontMedium) if (!string.IsNullOrEmpty(_text) && style.FontMedium)
width += Render2D.MeasureText(style.FontMedium, _text).X + (hasSprite ? DefaultMargin : 0); width += FallbackTextUtils.MeasureText(style.FontMedium, _text).X + (hasSprite ? DefaultMargin : 0);
Width = width; Width = width;
} }

View File

@@ -576,7 +576,7 @@ namespace FlaxEditor.GUI.Tree
var font = TextFont.GetFont(); var font = TextFont.GetFont();
if (font) if (font)
{ {
_textWidth = Render2D.MeasureText(font, _text).X; _textWidth = FallbackTextUtils.MeasureText(font, _text).X;
_textChanged = false; _textChanged = false;
} }
} }

View File

@@ -337,7 +337,7 @@ namespace FlaxEditor.Surface.Archetypes
_textRect = new Rectangle(Float2.Zero, Size); _textRect = new Rectangle(Float2.Zero, Size);
var style = Style.Current; var style = Style.Current;
var titleSize = Render2D.MeasureText(style.FontLarge, Title); var titleSize = FallbackTextUtils.MeasureText(style.FontLarge, Title);
var width = Mathf.Max(100, titleSize.X + 50); var width = Mathf.Max(100, titleSize.X + 50);
Resize(width, 0); Resize(width, 0);
titleSize.X += 8.0f; titleSize.X += 8.0f;
@@ -1402,7 +1402,7 @@ namespace FlaxEditor.Surface.Archetypes
{ {
Title = StateTitle; Title = StateTitle;
var style = Style.Current; var style = Style.Current;
var titleSize = Render2D.MeasureText(style.FontLarge, Title); var titleSize = FallbackTextUtils.MeasureText(style.FontLarge, Title);
var width = Mathf.Max(100, titleSize.X + 50); var width = Mathf.Max(100, titleSize.X + 50);
Resize(width, 0); Resize(width, 0);
titleSize.X += 8.0f; titleSize.X += 8.0f;

View File

@@ -77,7 +77,7 @@ namespace FlaxEditor.Surface.Archetypes
Title = asset?.ShortName ?? "Animation"; Title = asset?.ShortName ?? "Animation";
var style = Style.Current; var style = Style.Current;
Resize(Mathf.Max(230, Render2D.MeasureText(style.FontLarge, Title).X + 30), 160); Resize(Mathf.Max(230, FallbackTextUtils.MeasureText(style.FontLarge, Title).X + 30), 160);
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@@ -101,7 +101,7 @@ namespace FlaxEditor.Surface.Archetypes
_debugRelevant = Behavior.GetNodeDebugRelevancy(instance, behavior); _debugRelevant = Behavior.GetNodeDebugRelevancy(instance, behavior);
_debugInfo = Behavior.GetNodeDebugInfo(instance, behavior); _debugInfo = Behavior.GetNodeDebugInfo(instance, behavior);
if (!string.IsNullOrEmpty(_debugInfo)) if (!string.IsNullOrEmpty(_debugInfo))
_debugInfoSize = Render2D.MeasureText(Style.Current.FontSmall, _debugInfo); _debugInfoSize = FallbackTextUtils.MeasureText(Style.Current.FontSmall, _debugInfo);
} }
} }
@@ -488,7 +488,7 @@ namespace FlaxEditor.Surface.Archetypes
var height = 0.0f; var height = 0.0f;
var titleLabelFont = Style.Current.FontLarge; var titleLabelFont = Style.Current.FontLarge;
width = Mathf.Max(width, 100.0f); width = Mathf.Max(width, 100.0f);
width = Mathf.Max(width, Render2D.MeasureText(titleLabelFont, Title).X + 30); width = Mathf.Max(width, FallbackTextUtils.MeasureText(titleLabelFont, Title).X + 30);
if (_debugInfoSize.X > 0) if (_debugInfoSize.X > 0)
{ {
width = Mathf.Max(width, _debugInfoSize.X + 8.0f); width = Mathf.Max(width, _debugInfoSize.X + 8.0f);

View File

@@ -286,7 +286,7 @@ namespace FlaxEditor.Surface.ContextMenu
Render2D.DrawText(style.FontSmall, _archetype.Title, textRect, Enabled ? style.Foreground : style.ForegroundDisabled, TextAlignment.Near, TextAlignment.Center); Render2D.DrawText(style.FontSmall, _archetype.Title, textRect, Enabled ? style.Foreground : style.ForegroundDisabled, TextAlignment.Near, TextAlignment.Center);
if (_archetype.SubTitle != null) if (_archetype.SubTitle != null)
{ {
var titleLength = Render2D.MeasureText(style.FontSmall, _archetype.Title).X; var titleLength = FallbackTextUtils.MeasureText(style.FontSmall, _archetype.Title).X;
var subTitleRect = new Rectangle(textRect.X + titleLength, textRect.Y, textRect.Width - titleLength, textRect.Height); var subTitleRect = new Rectangle(textRect.X + titleLength, textRect.Y, textRect.Width - titleLength, textRect.Height);
Render2D.DrawText(style.FontSmall, _archetype.SubTitle, subTitleRect, style.ForegroundDisabled, TextAlignment.Near, TextAlignment.Center); Render2D.DrawText(style.FontSmall, _archetype.SubTitle, subTitleRect, style.ForegroundDisabled, TextAlignment.Near, TextAlignment.Center);
} }

View File

@@ -1428,7 +1428,7 @@ namespace FlaxEditor.Surface.Elements
if (_defaultValueEditor != null) if (_defaultValueEditor != null)
{ {
_defaultValueEditor.Location = new Float2(X + Width + 8 + Render2D.MeasureText(Style.Current.FontSmall, Text).X, Y); _defaultValueEditor.Location = new Float2(X + Width + 8 + FallbackTextUtils.MeasureText(Style.Current.FontSmall, Text).X, Y);
} }
} }
@@ -1635,7 +1635,7 @@ namespace FlaxEditor.Surface.Elements
{ {
if (DefaultValueEditors[i].CanUse(this, ref _currentType)) if (DefaultValueEditors[i].CanUse(this, ref _currentType))
{ {
var bounds = new Rectangle(X + Width + 8 + Render2D.MeasureText(Style.Current.FontSmall, Text).X, Y, 90, Height); var bounds = new Rectangle(X + Width + 8 + FallbackTextUtils.MeasureText(Style.Current.FontSmall, Text).X, Y, 90, Height);
_editor = DefaultValueEditors[i]; _editor = DefaultValueEditors[i];
try try
{ {

View File

@@ -200,7 +200,7 @@ namespace FlaxEditor.Surface
continue; continue;
if (child is InputBox inputBox) if (child is InputBox inputBox)
{ {
var boxWidth = Render2D.MeasureText(boxLabelFont, inputBox.Text).X + 20; var boxWidth = FallbackTextUtils.MeasureText(boxLabelFont, inputBox.Text).X + 20;
if (inputBox.DefaultValueEditor != null) if (inputBox.DefaultValueEditor != null)
boxWidth += inputBox.DefaultValueEditor.Width + 4; boxWidth += inputBox.DefaultValueEditor.Width + 4;
leftWidth = Mathf.Max(leftWidth, boxWidth); leftWidth = Mathf.Max(leftWidth, boxWidth);
@@ -208,7 +208,7 @@ namespace FlaxEditor.Surface
} }
else if (child is OutputBox outputBox) else if (child is OutputBox outputBox)
{ {
rightWidth = Mathf.Max(rightWidth, Render2D.MeasureText(boxLabelFont, outputBox.Text).X + 20); rightWidth = Mathf.Max(rightWidth, FallbackTextUtils.MeasureText(boxLabelFont, outputBox.Text).X + 20);
rightHeight = Mathf.Max(rightHeight, outputBox.Archetype.Position.Y - Constants.NodeMarginY - Constants.NodeHeaderSize + 20.0f); rightHeight = Mathf.Max(rightHeight, outputBox.Archetype.Position.Y - Constants.NodeMarginY - Constants.NodeHeaderSize + 20.0f);
} }
else if (child is Control control) else if (child is Control control)
@@ -226,7 +226,7 @@ namespace FlaxEditor.Surface
} }
} }
width = Mathf.Max(width, leftWidth + rightWidth + 10); width = Mathf.Max(width, leftWidth + rightWidth + 10);
width = Mathf.Max(width, Render2D.MeasureText(titleLabelFont, Title).X + 30); width = Mathf.Max(width, FallbackTextUtils.MeasureText(titleLabelFont, Title).X + 30);
height = Mathf.Max(height, Mathf.Max(leftHeight, rightHeight)); height = Mathf.Max(height, Mathf.Max(leftHeight, rightHeight));
Resize(width, height); Resize(width, height);
} }

View File

@@ -148,7 +148,7 @@ namespace FlaxEditor.Tools.Foliage
Parent = _noFoliagePanel, Parent = _noFoliagePanel,
Enabled = false Enabled = false
}; };
var textSize = Render2D.MeasureText(Style.Current.FontMedium, buttonText); var textSize = FallbackTextUtils.MeasureText(Style.Current.FontMedium, buttonText);
if (_createNewFoliage.Width < textSize.X) if (_createNewFoliage.Width < textSize.X)
{ {
_createNewFoliage.LocalX -= (textSize.X - _createNewFoliage.Width) / 2; _createNewFoliage.LocalX -= (textSize.X - _createNewFoliage.Width) / 2;

View File

@@ -106,7 +106,7 @@ namespace FlaxEditor.Tools.Terrain
Parent = _noTerrainPanel, Parent = _noTerrainPanel,
Enabled = false Enabled = false
}; };
var textSize = Render2D.MeasureText(Style.Current.FontMedium, buttonText); var textSize = FallbackTextUtils.MeasureText(Style.Current.FontMedium, buttonText);
if (_createTerrainButton.Width < textSize.X) if (_createTerrainButton.Width < textSize.X)
{ {
_createTerrainButton.LocalX -= (textSize.X - _createTerrainButton.Width) / 2; _createTerrainButton.LocalX -= (textSize.X - _createTerrainButton.Width) / 2;

View File

@@ -1,108 +0,0 @@
using FlaxEngine.GUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlaxEngine
{
public static class TextRenderUtils
{
/// <summary>
/// Draws a text using the global fallback defined in styles.
/// </summary>
/// <param name="font">The font to use.</param>
/// <param name="text">The text to render.</param>
/// <param name="layoutRect">The size and position of the area in which the text is drawn.</param>
/// <param name="color">The text color.</param>
/// <param name="horizontalAlignment">The horizontal alignment of the text in a layout rectangle.</param>
/// <param name="verticalAlignment">The vertical alignment of the text in a layout rectangle.</param>
/// <param name="textWrapping">Describes how wrap text inside a layout rectangle.</param>
/// <param name="baseLinesGapScale">The scale for distance one baseline from another. Default is 1.</param>
/// <param name="scale">The text drawing scale. Default is 1.</param>
public static void DrawTextWithFallback(Font font, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f)
{
var layout = new TextLayoutOptions
{
Bounds = layoutRect,
HorizontalAlignment = horizontalAlignment,
VerticalAlignment = verticalAlignment,
TextWrapping = textWrapping,
Scale = scale,
BaseLinesGapScale = baseLinesGapScale,
};
Render2D.DrawText(font, Style.Current.Fallbacks, text, color, ref layout);
}
/// <summary>
/// Draws a text using the global fallback defined in styles. Given material must have GUI domain and a public parameter named Font (texture parameter used for a font atlas sampling).
/// </summary>
/// <param name="font">The font to use.</param>
/// <param name="customMaterial">Custom material for font characters rendering. It must contain texture parameter named Font used to sample font texture.</param>
/// <param name="text">The text to render.</param>
/// <param name="layoutRect">The size and position of the area in which the text is drawn.</param>
/// <param name="color">The text color.</param>
/// <param name="horizontalAlignment">The horizontal alignment of the text in a layout rectangle.</param>
/// <param name="verticalAlignment">The vertical alignment of the text in a layout rectangle.</param>
/// <param name="textWrapping">Describes how wrap text inside a layout rectangle.</param>
/// <param name="baseLinesGapScale">The scale for distance one baseline from another. Default is 1.</param>
/// <param name="scale">The text drawing scale. Default is 1.</param>
public static void DrawTextWithFallback(Font font, MaterialBase customMaterial, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f)
{
var layout = new TextLayoutOptions
{
Bounds = layoutRect,
HorizontalAlignment = horizontalAlignment,
VerticalAlignment = verticalAlignment,
TextWrapping = textWrapping,
Scale = scale,
BaseLinesGapScale = baseLinesGapScale,
};
Render2D.DrawText(font, Style.Current.Fallbacks, text, color, ref layout, customMaterial);
}
public static Float2 MeasureTextWithFallback(Font font, string text)
{
return font.MeasureText(Style.Current.Fallbacks, text);
}
public static Float2 MeasureTextWithFallback(Font font, string text, ref TextRange textRange)
{
return font.MeasureText(Style.Current.Fallbacks, text, ref textRange);
}
public static Float2 MeasureTextWithFallback(Font font, string text, ref TextLayoutOptions layout)
{
return font.MeasureText(Style.Current.Fallbacks, text, ref layout);
}
public static Float2 MeasureTextWithFallback(Font font, string text, ref TextRange textRange, ref TextLayoutOptions layout)
{
return font.MeasureText(Style.Current.Fallbacks, text, ref textRange, ref layout);
}
public static Float2 GetCharPositionWithFallback(Font font, string text, int index)
{
return font.GetCharPosition(Style.Current.Fallbacks, text, index);
}
public static Float2 GetCharPositionWithFallback(Font font, string text, ref TextRange textRange, int index)
{
return font.GetCharPosition(Style.Current.Fallbacks, text, ref textRange, index);
}
public static Float2 GetCharPositionWithFallback(Font font, string text, int index, ref TextLayoutOptions layout)
{
return font.GetCharPosition(Style.Current.Fallbacks, text, index, ref layout);
}
public static Float2 GetCharPositionWithFallback(Font font, string text, ref TextRange textRange, int index, ref TextLayoutOptions layout)
{
return font.GetCharPosition(Style.Current.Fallbacks, text, ref textRange, index, ref layout);
}
}
}

View File

@@ -548,9 +548,9 @@ namespace FlaxEditor.Viewport
#region Camera settings widget #region Camera settings widget
var largestText = "Relative Panning"; var largestText = "Relative Panning";
var textSize = Render2D.MeasureText(Style.Current.FontMedium, largestText); var textSize = FallbackTextUtils.MeasureText(Style.Current.FontMedium, largestText);
var xLocationForExtras = textSize.X + 5; var xLocationForExtras = textSize.X + 5;
var cameraSpeedTextWidth = Render2D.MeasureText(Style.Current.FontMedium, "0.00").X; var cameraSpeedTextWidth = FallbackTextUtils.MeasureText(Style.Current.FontMedium, "0.00").X;
// Camera Settings Widget // Camera Settings Widget
_cameraWidget = new ViewportWidgetsContainer(ViewportWidgetLocation.UpperRight); _cameraWidget = new ViewportWidgetsContainer(ViewportWidgetLocation.UpperRight);
@@ -801,7 +801,7 @@ namespace FlaxEditor.Viewport
#region View mode widget #region View mode widget
largestText = "Brightness"; largestText = "Brightness";
textSize = Render2D.MeasureText(Style.Current.FontMedium, largestText); textSize = FallbackTextUtils.MeasureText(Style.Current.FontMedium, largestText);
xLocationForExtras = textSize.X + 5; xLocationForExtras = textSize.X + 5;
var viewMode = new ViewportWidgetsContainer(ViewportWidgetLocation.UpperLeft); var viewMode = new ViewportWidgetsContainer(ViewportWidgetLocation.UpperLeft);

View File

@@ -164,7 +164,7 @@ namespace FlaxEditor.Viewport.Widgets
var style = Style.Current; var style = Style.Current;
if (style != null && style.FontMedium) if (style != null && style.FontMedium)
Width = CalculateButtonWidth(_forcedTextWidth > 0.0f ? _forcedTextWidth : Render2D.MeasureText(style.FontMedium, _text).X, Icon.IsValid); Width = CalculateButtonWidth(_forcedTextWidth > 0.0f ? _forcedTextWidth : FallbackTextUtils.MeasureText(style.FontMedium, _text).X, Icon.IsValid);
} }
} }
} }

View File

@@ -54,7 +54,7 @@ namespace FlaxEditor.Windows
Parent = this Parent = this
}; };
var buttonText = "Copy version info"; var buttonText = "Copy version info";
var fontSize = Render2D.MeasureText(Style.Current.FontMedium, buttonText); var fontSize = FallbackTextUtils.MeasureText(Style.Current.FontMedium, buttonText);
var copyVersionButton = new Button(Width - fontSize.X - 8, 6, fontSize.X + 4, 20) var copyVersionButton = new Button(Width - fontSize.X - 8, 6, fontSize.X + 4, 20)
{ {
Text = buttonText, Text = buttonText,

View File

@@ -122,7 +122,7 @@ namespace FlaxEditor.Windows
url = desc.RepositoryUrl; url = desc.RepositoryUrl;
versionLabel.Font.Font.WaitForLoaded(); versionLabel.Font.Font.WaitForLoaded();
var font = versionLabel.Font.GetFont(); var font = versionLabel.Font.GetFont();
var authorWidth = Render2D.MeasureText(font, desc.Author).X + 8; var authorWidth = FallbackTextUtils.MeasureText(font, desc.Author).X + 8;
var authorLabel = new ClickableLabel var authorLabel = new ClickableLabel
{ {
HorizontalAlignment = TextAlignment.Far, HorizontalAlignment = TextAlignment.Far,

View File

@@ -86,7 +86,7 @@ namespace FlaxEditor.Windows.Profiler
Render2D.DrawRectangle(bounds, color * 0.5f); Render2D.DrawRectangle(bounds, color * 0.5f);
if (_nameLength < 0 && style.FontMedium) if (_nameLength < 0 && style.FontMedium)
_nameLength = Render2D.MeasureText(style.FontMedium, _name).X; _nameLength = FallbackTextUtils.MeasureText(style.FontMedium, _name).X;
if (_nameLength < bounds.Width + 4) if (_nameLength < bounds.Width + 4)
{ {

View File

@@ -0,0 +1,126 @@
namespace FlaxEngine
{
/// <summary>
/// A collection of functions to handle text rendering with fallback font
/// </summary>
public static class FallbackTextUtils
{
public static FallbackFonts Fallbacks
{
get; set;
} = null;
public static void DrawText(Font font, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f)
{
var layout = new TextLayoutOptions
{
Bounds = layoutRect,
HorizontalAlignment = horizontalAlignment,
VerticalAlignment = verticalAlignment,
TextWrapping = textWrapping,
Scale = scale,
BaseLinesGapScale = baseLinesGapScale,
};
if (Fallbacks != null)
{
Render2D.DrawText(font, Fallbacks, text, color, ref layout);
}
else
{
Render2D.DrawText(font, text, color, ref layout);
}
}
public static void DrawText(Font font, MaterialBase customMaterial, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f)
{
var layout = new TextLayoutOptions
{
Bounds = layoutRect,
HorizontalAlignment = horizontalAlignment,
VerticalAlignment = verticalAlignment,
TextWrapping = textWrapping,
Scale = scale,
BaseLinesGapScale = baseLinesGapScale,
};
if (Fallbacks != null)
{
Render2D.DrawText(font, Fallbacks, text, color, ref layout, customMaterial);
}
else
{
Render2D.DrawText(font, text, color, ref layout, customMaterial);
}
}
public static Float2 MeasureText(Font font, string text)
{
if (Fallbacks != null)
{
return font.MeasureText(Fallbacks, text);
}
else
{
return font.MeasureText(text);
}
}
public static Float2 MeasureText(Font font, string text, ref TextRange textRange)
{
if (Fallbacks != null)
{
return font.MeasureText(Fallbacks, text, ref textRange);
}
else
{
return font.MeasureText(text, ref textRange);
}
}
public static Float2 MeasureText(Font font, string text, ref TextLayoutOptions layout)
{
if (Fallbacks != null)
{
return font.MeasureText(Fallbacks, text, ref layout);
}
else
{
return font.MeasureText(text, ref layout);
}
}
public static Float2 MeasureText(Font font, string text, ref TextRange textRange, ref TextLayoutOptions layout)
{
if (Fallbacks != null)
{
return font.MeasureText(Fallbacks, text, ref textRange, ref layout);
}
else
{
return font.MeasureText(text, ref textRange, ref layout);
}
}
public static Float2 GetCharPosition(Font font, string text, int index)
{
return font.GetCharPosition(Style.Current.Fallbacks, text, index);
}
public static Float2 GetCharPosition(Font font, string text, ref TextRange textRange, int index)
{
return font.GetCharPosition(Style.Current.Fallbacks, text, ref textRange, index);
}
public static Float2 GetCharPosition(Font font, string text, int index, ref TextLayoutOptions layout)
{
return font.GetCharPosition(Style.Current.Fallbacks, text, index, ref layout);
}
public static Float2 GetCharPosition(Font font, string text, ref TextRange textRange, int index, ref TextLayoutOptions layout)
{
return font.GetCharPosition(Style.Current.Fallbacks, text, ref textRange, index, ref layout);
}
}
}

View File

@@ -7,10 +7,6 @@ namespace FlaxEngine
{ {
partial class Render2D partial class Render2D
{ {
public static FallbackFonts Fallbacks
{
get; set;
} = null;
/// <summary> /// <summary>
/// Pushes transformation layer. /// Pushes transformation layer.
@@ -117,8 +113,7 @@ namespace FlaxEngine
/// <param name="textWrapping">Describes how wrap text inside a layout rectangle.</param> /// <param name="textWrapping">Describes how wrap text inside a layout rectangle.</param>
/// <param name="baseLinesGapScale">The scale for distance one baseline from another. Default is 1.</param> /// <param name="baseLinesGapScale">The scale for distance one baseline from another. Default is 1.</param>
/// <param name="scale">The text drawing scale. Default is 1.</param> /// <param name="scale">The text drawing scale. Default is 1.</param>
/// <param name="useFallback">Whether to use fallback fonts for chars not renderable by the font.</param> public static void DrawText(Font font, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f)
public static void DrawText(Font font, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f, bool useFallback = true)
{ {
var layout = new TextLayoutOptions var layout = new TextLayoutOptions
{ {
@@ -129,15 +124,7 @@ namespace FlaxEngine
Scale = scale, Scale = scale,
BaseLinesGapScale = baseLinesGapScale, BaseLinesGapScale = baseLinesGapScale,
}; };
DrawText(font, text, color, ref layout);
if (useFallback && Fallbacks != null)
{
DrawText(font, Fallbacks, text, color, ref layout);
}
else
{
DrawText(font, text, color, ref layout);
}
} }
/// <summary> /// <summary>
@@ -153,8 +140,7 @@ namespace FlaxEngine
/// <param name="textWrapping">Describes how wrap text inside a layout rectangle.</param> /// <param name="textWrapping">Describes how wrap text inside a layout rectangle.</param>
/// <param name="baseLinesGapScale">The scale for distance one baseline from another. Default is 1.</param> /// <param name="baseLinesGapScale">The scale for distance one baseline from another. Default is 1.</param>
/// <param name="scale">The text drawing scale. Default is 1.</param> /// <param name="scale">The text drawing scale. Default is 1.</param>
/// <param name="useFallback">Whether to use fallback fonts for chars not renderable by the font.</param> public static void DrawText(Font font, MaterialBase customMaterial, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f)
public static void DrawText(Font font, MaterialBase customMaterial, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f, bool useFallback = true)
{ {
var layout = new TextLayoutOptions var layout = new TextLayoutOptions
{ {
@@ -165,63 +151,7 @@ namespace FlaxEngine
Scale = scale, Scale = scale,
BaseLinesGapScale = baseLinesGapScale, BaseLinesGapScale = baseLinesGapScale,
}; };
DrawText(font, text, color, ref layout, customMaterial);
if (useFallback && Fallbacks != null)
{
DrawText(font, Fallbacks, text, color, ref layout, customMaterial);
}
else
{
DrawText(font, text, color, ref layout, customMaterial);
}
}
public static Float2 MeasureText(Font font, string text, bool useFallback = true)
{
if (useFallback && Fallbacks != null)
{
return font.MeasureText(Style.Current.Fallbacks, text);
}
else
{
return font.MeasureText(text);
}
}
public static Float2 MeasureText(Font font, string text, ref TextRange textRange, bool useFallback = true)
{
if (useFallback && Fallbacks != null)
{
return font.MeasureText(Style.Current.Fallbacks, text, ref textRange);
}
else
{
return font.MeasureText(text, ref textRange);
}
}
public static Float2 MeasureText(Font font, string text, ref TextLayoutOptions layout, bool useFallback = true)
{
if (useFallback && Fallbacks != null)
{
return font.MeasureText(Style.Current.Fallbacks, text, ref layout);
}
else
{
return font.MeasureText(text, ref layout);
}
}
public static Float2 MeasureText(Font font, string text, ref TextRange textRange, ref TextLayoutOptions layout, bool useFallback = true)
{
if (useFallback && Fallbacks != null)
{
return font.MeasureText(Style.Current.Fallbacks, text, ref textRange, ref layout);
}
else
{
return font.MeasureText(text, ref textRange, ref layout);
}
} }
/// <summary> /// <summary>

View File

@@ -476,7 +476,7 @@ namespace FlaxEngine.GUI
var font = Font.GetFont(); var font = Font.GetFont();
for (int i = 0; i < _items.Count; i++) for (int i = 0; i < _items.Count; i++)
{ {
itemsWidth = Mathf.Max(itemsWidth, itemsMargin + 4 + Render2D.MeasureText(font, _items[i]).X); itemsWidth = Mathf.Max(itemsWidth, itemsMargin + 4 + FallbackTextUtils.MeasureText(font, _items[i]).X);
} }
*/ */
var itemsWidth = Width; var itemsWidth = Width;

View File

@@ -256,7 +256,7 @@ namespace FlaxEngine.GUI
layout.Bounds.Size.X = Width - Margin.Width; layout.Bounds.Size.X = Width - Margin.Width;
else if (_autoWidth && !_autoHeight) else if (_autoWidth && !_autoHeight)
layout.Bounds.Size.Y = Height - Margin.Height; layout.Bounds.Size.Y = Height - Margin.Height;
_textSize = Render2D.MeasureText(font, _text, ref layout); _textSize = FallbackTextUtils.MeasureText(font, _text, ref layout);
_textSize.Y *= BaseLinesGapScale; _textSize.Y *= BaseLinesGapScale;
// Check if size is controlled via text // Check if size is controlled via text

View File

@@ -106,7 +106,7 @@ namespace FlaxEngine.GUI
return Float2.Zero; return Float2.Zero;
} }
return Render2D.MeasureText(font, _text, ref _layout); return FallbackTextUtils.MeasureText(font, _text, ref _layout);
} }
/// <inheritdoc /> /// <inheritdoc />