Add fallback settings to CSharp

This commit is contained in:
ExMatics HydrogenC
2023-11-30 23:12:56 +08:00
parent d3840bb1f3
commit cdbe59a3fb
54 changed files with 357 additions and 273 deletions

View File

@@ -39,7 +39,7 @@ public:
/// <summary>
/// Combine the primary fonts with the fallback fonts to get a font list
/// </summary>
API_PROPERTY() FORCE_INLINE Array<Font*>& GetFontList(float size) {
API_FUNCTION() FORCE_INLINE Array<Font*>& GetFontList(float size) {
Array<Font*>* result;
if (_cache.TryGet(size, result)) {
return *result;

View File

@@ -1,74 +0,0 @@
using System.Collections.Generic;
using System.Linq;
namespace FlaxEngine
{
/// <summary>
/// Reference to multiple font references
/// </summary>
public class MultiFontReference : List<FontReference>
{
public MultiFontReference()
{
_cachedFont = null;
}
public MultiFontReference(IEnumerable<FontReference> other)
{
AddRange(other);
_cachedFont = null;
}
public MultiFontReference(MultiFontReference other)
{
AddRange(other);
_cachedFont = other._cachedFont;
}
public MultiFontReference(MultiFontReference other, float size)
{
AddRange(other.Select(x => new FontReference(x) { Size = size }));
_cachedFont = null;
}
public MultiFontReference(MultiFont other)
{
AddRange(other.Fonts.Select(x => new FontReference(x)));
_cachedFont = other;
}
public MultiFontReference(FontAsset[] assets, float size)
{
AddRange(assets.Select(x => new FontReference(x, size)));
_cachedFont = null;
}
[EditorOrder(0), Tooltip("The font asset to use as characters source.")]
public MultiFont GetMultiFont()
{
if (_cachedFont)
return _cachedFont;
var fontList = this.Where(x => x.Font).Select(x => x.GetFont()).ToArray();
_cachedFont = MultiFont.Create(fontList);
return _cachedFont;
}
public bool Verify()
{
foreach (var i in this)
{
if (!i.Font)
{
return false;
}
}
return true;
}
[NoSerialize]
private MultiFont _cachedFont;
}
}

View File

@@ -1,11 +1,17 @@
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using FlaxEngine.GUI;
using System;
namespace FlaxEngine
{
partial class Render2D
{
public static FallbackFonts Fallbacks
{
get; set;
} = null;
/// <summary>
/// Pushes transformation layer.
/// </summary>
@@ -111,7 +117,8 @@ namespace FlaxEngine
/// <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 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)
/// <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, bool useFallback = true)
{
var layout = new TextLayoutOptions
{
@@ -122,7 +129,15 @@ namespace FlaxEngine
Scale = scale,
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>
@@ -138,7 +153,8 @@ namespace FlaxEngine
/// <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 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)
/// <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, bool useFallback = true)
{
var layout = new TextLayoutOptions
{
@@ -149,63 +165,63 @@ namespace FlaxEngine
Scale = scale,
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);
}
}
/// <summary>
/// Draws a text.
/// </summary>
/// <param name="fonts">The fonts to use, ordered by priority.</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 DrawText(MultiFont fonts, 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 Float2 MeasureText(Font font, string text, bool useFallback = true)
{
var layout = new TextLayoutOptions
if (useFallback && Fallbacks != null)
{
Bounds = layoutRect,
HorizontalAlignment = horizontalAlignment,
VerticalAlignment = verticalAlignment,
TextWrapping = textWrapping,
Scale = scale,
BaseLinesGapScale = baseLinesGapScale,
};
DrawText(fonts, text, color, ref layout);
return font.MeasureText(Style.Current.Fallbacks, text);
}
else
{
return font.MeasureText(text);
}
}
/// <summary>
/// Draws a text using a custom material shader. Given material must have GUI domain and a public parameter named Font (texture parameter used for a font atlas sampling).
/// </summary>
/// <param name="fonts">The fonts to use, ordered by priority.</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 DrawText(MultiFont fonts, 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 Float2 MeasureText(Font font, string text, ref TextRange textRange, bool useFallback = true)
{
var layout = new TextLayoutOptions
if (useFallback && Fallbacks != null)
{
Bounds = layoutRect,
HorizontalAlignment = horizontalAlignment,
VerticalAlignment = verticalAlignment,
TextWrapping = textWrapping,
Scale = scale,
BaseLinesGapScale = baseLinesGapScale,
};
return font.MeasureText(Style.Current.Fallbacks, text, ref textRange);
}
else
{
return font.MeasureText(text, ref textRange);
}
}
DrawText(fonts, text, color, ref layout, customMaterial);
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>

View File

@@ -294,12 +294,12 @@ namespace FlaxEngine
style.DragWindow = style.BackgroundSelected * 0.7f;
// Use optionally bundled default font (matches Editor)
FontAsset[] defaultFont = [Content.LoadAsyncInternal<FontAsset>("Editor/Fonts/Roboto-Regular"), Content.LoadAsyncInternal<FontAsset>("Editor/Fonts/NotoSansSC-Regular")];
FontAsset defaultFont = Content.LoadAsyncInternal<FontAsset>("Editor/Fonts/Roboto-Regular");
style.FontTitle = new MultiFontReference(defaultFont, 18).GetMultiFont();
style.FontLarge = new MultiFontReference(defaultFont, 14).GetMultiFont();
style.FontMedium = new MultiFontReference(defaultFont, 9).GetMultiFont();
style.FontSmall = new MultiFontReference(defaultFont, 9).GetMultiFont();
style.FontTitle = new FontReference(defaultFont, 18).GetFont();
style.FontLarge = new FontReference(defaultFont, 14).GetFont();
style.FontMedium = new FontReference(defaultFont, 9).GetFont();
style.FontSmall = new FontReference(defaultFont, 9).GetFont();
Style.Current = style;
}

View File

@@ -23,7 +23,7 @@ namespace FlaxEngine.GUI
/// <summary>
/// The font.
/// </summary>
protected MultiFontReference _font;
protected FontReference _font;
/// <summary>
/// The text.
@@ -44,7 +44,7 @@ namespace FlaxEngine.GUI
/// Gets or sets the font used to draw button text.
/// </summary>
[EditorDisplay("Text Style"), EditorOrder(2022), ExpandGroups]
public MultiFontReference Font
public FontReference Font
{
get => _font;
set => _font = value;
@@ -156,7 +156,7 @@ namespace FlaxEngine.GUI
var style = Style.Current;
if (style != null)
{
_font = new MultiFontReference(style.FontMedium);
_font = new FontReference(style.FontMedium);
TextColor = style.Foreground;
BackgroundColor = style.BackgroundNormal;
BorderColor = style.BorderNormal;
@@ -262,7 +262,7 @@ namespace FlaxEngine.GUI
Render2D.DrawRectangle(clientRect, borderColor, BorderThickness);
// Draw text
Render2D.DrawText(_font?.GetMultiFont(), TextMaterial, _text, clientRect, textColor, TextAlignment.Center, TextAlignment.Center);
Render2D.DrawText(_font?.GetFont(), TextMaterial, _text, clientRect, textColor, TextAlignment.Center, TextAlignment.Center);
}
/// <inheritdoc />

View File

@@ -278,7 +278,7 @@ namespace FlaxEngine.GUI
/// Gets or sets the font used to draw text.
/// </summary>
[EditorDisplay("Text Style"), EditorOrder(2021)]
public MultiFontReference Font { get; set; }
public FontReference Font { get; set; }
/// <summary>
/// Gets or sets the custom material used to render the text. It must has domain set to GUI and have a public texture parameter named Font used to sample font atlas texture with font characters data.
@@ -359,7 +359,7 @@ namespace FlaxEngine.GUI
: base(0, 0, 120, 18.0f)
{
var style = Style.Current;
Font = new MultiFontReference(style.FontMedium);
Font = new FontReference(style.FontMedium);
TextColor = style.Foreground;
BackgroundColor = style.BackgroundNormal;
BackgroundColorHighlighted = BackgroundColor;
@@ -476,7 +476,7 @@ namespace FlaxEngine.GUI
var font = Font.GetFont();
for (int i = 0; i < _items.Count; i++)
{
itemsWidth = Mathf.Max(itemsWidth, itemsMargin + 4 + font.MeasureText(_items[i]).X);
itemsWidth = Mathf.Max(itemsWidth, itemsMargin + 4 + Render2D.MeasureText(font, _items[i]).X);
}
*/
var itemsWidth = Width;
@@ -674,7 +674,7 @@ namespace FlaxEngine.GUI
var textRect = new Rectangle(margin, 0, clientRect.Width - boxSize - 2.0f * margin, clientRect.Height);
Render2D.PushClip(textRect);
var textColor = TextColor;
Render2D.DrawText(Font.GetMultiFont(), FontMaterial, _items[_selectedIndex], textRect, enabled ? textColor : textColor * 0.5f, TextAlignment.Near, TextAlignment.Center);
Render2D.DrawText(Font.GetFont(), FontMaterial, _items[_selectedIndex], textRect, enabled ? textColor : textColor * 0.5f, TextAlignment.Near, TextAlignment.Center);
Render2D.PopClip();
}

View File

@@ -26,7 +26,7 @@ namespace FlaxEngine.GUI
/// <summary>
/// The font.
/// </summary>
protected MultiFontReference _font;
protected FontReference _font;
/// <summary>
/// Gets or sets the text.
@@ -86,7 +86,7 @@ namespace FlaxEngine.GUI
/// Gets or sets the font.
/// </summary>
[EditorDisplay("Text Style"), EditorOrder(2024)]
public MultiFontReference Font
public FontReference Font
{
get => _font;
set
@@ -192,7 +192,7 @@ namespace FlaxEngine.GUI
{
AutoFocus = false;
var style = Style.Current;
Font = new MultiFontReference(style.FontMedium);
Font = new FontReference(style.FontMedium);
TextColor = style.Foreground;
TextColorHighlighted = style.Foreground;
}
@@ -203,7 +203,7 @@ namespace FlaxEngine.GUI
{
AutoFocus = false;
var style = Style.Current;
Font = new MultiFontReference(style.FontMedium);
Font = new FontReference(style.FontMedium);
TextColor = style.Foreground;
TextColorHighlighted = style.Foreground;
}
@@ -235,7 +235,7 @@ namespace FlaxEngine.GUI
}
}
Render2D.DrawText(_font.GetMultiFont(), Material, _text, rect, color, hAlignment, wAlignment, Wrapping, BaseLinesGapScale, scale);
Render2D.DrawText(_font.GetFont(), Material, _text, rect, color, hAlignment, wAlignment, Wrapping, BaseLinesGapScale, scale);
if (ClipText)
Render2D.PopClip();
@@ -246,7 +246,7 @@ namespace FlaxEngine.GUI
{
if (_autoWidth || _autoHeight || _autoFitText)
{
var font = _font.GetMultiFont();
var font = _font.GetFont();
if (font)
{
// Calculate text size
@@ -256,7 +256,7 @@ namespace FlaxEngine.GUI
layout.Bounds.Size.X = Width - Margin.Width;
else if (_autoWidth && !_autoHeight)
layout.Bounds.Size.Y = Height - Margin.Height;
_textSize = font.MeasureText(_text, ref layout);
_textSize = Render2D.MeasureText(font, _text, ref layout);
_textSize.Y *= BaseLinesGapScale;
// Check if size is controlled via text

View File

@@ -46,7 +46,7 @@ namespace FlaxEngine.GUI
var style = Style.Current;
_textStyle = new TextBlockStyle
{
Font = new FontReference(style.FontMedium.Fonts.First()),
Font = new FontReference(style.FontMedium),
Color = style.Foreground,
BackgroundSelectedBrush = new SolidColorBrush(style.BackgroundSelected),
};

View File

@@ -1,5 +1,6 @@
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System.Drawing;
using System.Linq;
namespace FlaxEngine.GUI
@@ -40,7 +41,7 @@ namespace FlaxEngine.GUI
/// Gets or sets the font.
/// </summary>
[EditorDisplay("Text Style"), EditorOrder(2024)]
public MultiFontReference Font { get; set; }
public FontReference Font { get; set; }
/// <summary>
/// Gets or sets the custom material used to render the text. It must has domain set to GUI and have a public texture parameter named Font used to sample font atlas texture with font characters data.
@@ -90,7 +91,7 @@ namespace FlaxEngine.GUI
_layout.Bounds = new Rectangle(DefaultMargin, 1, Width - 2 * DefaultMargin, Height - 2);
var style = Style.Current;
Font = new MultiFontReference(style.FontMedium);
Font = new FontReference(style.FontMedium);
TextColor = style.Foreground;
WatermarkTextColor = style.ForegroundDisabled;
SelectionColor = style.BackgroundSelected;
@@ -99,33 +100,33 @@ namespace FlaxEngine.GUI
/// <inheritdoc />
public override Float2 GetTextSize()
{
var font = Font.GetMultiFont();
var font = Font.GetFont();
if (font == null)
{
return Float2.Zero;
}
return font.MeasureText(_text, ref _layout);
return Render2D.MeasureText(font, _text, ref _layout);
}
/// <inheritdoc />
public override Float2 GetCharPosition(int index, out float height)
{
var font = Font.GetMultiFont();
var font = Font.GetFont();
if (font == null)
{
height = Height;
return Float2.Zero;
}
height = font.MaxHeight / DpiScale;
height = font.Height / DpiScale;
return font.GetCharPosition(_text, index, ref _layout);
}
/// <inheritdoc />
public override int HitTestText(Float2 location)
{
var font = Font.GetMultiFont();
var font = Font.GetFont();
if (font == null)
{
return 0;
@@ -148,7 +149,7 @@ namespace FlaxEngine.GUI
// Cache data
var rect = new Rectangle(Float2.Zero, Size);
bool enabled = EnabledInHierarchy;
var font = Font.GetMultiFont();
var font = Font.GetFont();
if (!font)
return;
@@ -172,7 +173,7 @@ namespace FlaxEngine.GUI
{
var leftEdge = font.GetCharPosition(_text, SelectionLeft, ref _layout);
var rightEdge = font.GetCharPosition(_text, SelectionRight, ref _layout);
float fontHeight = font.MaxHeight / DpiScale;
float fontHeight = font.Height / DpiScale;
// Draw selection background
float alpha = Mathf.Min(1.0f, Mathf.Cos(_animateTime * BackgroundSelectedFlashSpeed) * 0.5f + 1.3f);
@@ -212,11 +213,25 @@ namespace FlaxEngine.GUI
var color = TextColor;
if (!enabled)
color *= 0.6f;
Render2D.DrawText(font, _text, color, ref _layout, TextMaterial);
if (Render2D.Fallbacks != null)
{
Render2D.DrawText(font, Render2D.Fallbacks, _text, color, ref _layout, TextMaterial);
}
else
{
Render2D.DrawText(font, _text, color, ref _layout, TextMaterial);
}
}
else if (!string.IsNullOrEmpty(_watermarkText) && !IsFocused)
{
Render2D.DrawText(font, _watermarkText, WatermarkTextColor, ref _layout, TextMaterial);
if (Render2D.Fallbacks != null)
{
Render2D.DrawText(font, Render2D.Fallbacks, _watermarkText, WatermarkTextColor, ref _layout, TextMaterial);
}
else
{
Render2D.DrawText(font, _watermarkText, WatermarkTextColor, ref _layout, TextMaterial);
}
}
// Caret

View File

@@ -130,7 +130,7 @@ namespace FlaxEngine.GUI
/// Gets or sets the font used to render panel header text.
/// </summary>
[EditorDisplay("Header Text Style"), EditorOrder(2020), ExpandGroups]
public MultiFontReference HeaderTextFont { get; set; }
public FontReference HeaderTextFont { get; set; }
/// <summary>
/// Gets or sets the custom material used to render the text. It must has domain set to GUI and have a public texture parameter named Font used to sample font atlas texture with font characters data.
@@ -238,7 +238,7 @@ namespace FlaxEngine.GUI
var style = Style.Current;
HeaderColor = style.BackgroundNormal;
HeaderColorMouseOver = style.BackgroundHighlighted;
HeaderTextFont = new MultiFontReference(style.FontMedium);
HeaderTextFont = new FontReference(style.FontMedium);
HeaderTextColor = style.Foreground;
ArrowImageOpened = new SpriteBrush(style.ArrowDown);
ArrowImageClosed = new SpriteBrush(style.ArrowRight);
@@ -375,7 +375,7 @@ namespace FlaxEngine.GUI
textColor *= 0.6f;
}
Render2D.DrawText(HeaderTextFont.GetMultiFont(), HeaderTextMaterial, HeaderText, textRect, textColor, TextAlignment.Near, TextAlignment.Center);
Render2D.DrawText(HeaderTextFont.GetFont(), HeaderTextMaterial, HeaderText, textRect, textColor, TextAlignment.Near, TextAlignment.Center);
if (!_isClosed && EnableContainmentLines)
{

View File

@@ -15,61 +15,67 @@ namespace FlaxEngine.GUI
public static Style Current { get; set; }
[Serialize]
private MultiFontReference _fontTitle;
private FontReference _fontTitle;
/// <summary>
/// The font title.
/// </summary>
[NoSerialize]
[EditorOrder(10)]
public MultiFont FontTitle
public Font FontTitle
{
get => _fontTitle?.GetMultiFont();
set => _fontTitle = new MultiFontReference(value);
get => _fontTitle?.GetFont();
set => _fontTitle = new FontReference(value);
}
[Serialize]
private MultiFontReference _fontLarge;
private FontReference _fontLarge;
/// <summary>
/// The font large.
/// </summary>
[NoSerialize]
[EditorOrder(20)]
public MultiFont FontLarge
public Font FontLarge
{
get => _fontLarge?.GetMultiFont();
set => _fontLarge = new MultiFontReference(value);
get => _fontLarge?.GetFont();
set => _fontLarge = new FontReference(value);
}
[Serialize]
private MultiFontReference _fontMedium;
private FontReference _fontMedium;
/// <summary>
/// The font medium.
/// </summary>
[NoSerialize]
[EditorOrder(30)]
public MultiFont FontMedium
public Font FontMedium
{
get => _fontMedium?.GetMultiFont();
set => _fontMedium = new MultiFontReference(value);
get => _fontMedium?.GetFont();
set => _fontMedium = new FontReference(value);
}
[Serialize]
private MultiFontReference _fontSmall;
private FontReference _fontSmall;
/// <summary>
/// The font small.
/// </summary>
[NoSerialize]
[EditorOrder(40)]
public MultiFont FontSmall
public Font FontSmall
{
get => _fontSmall?.GetMultiFont();
set => _fontSmall = new MultiFontReference(value);
get => _fontSmall?.GetFont();
set => _fontSmall = new FontReference(value);
}
/// <summary>
/// The fallback fonts to use if the primary font can't render the char.
/// </summary>
[EditorOrder(50)]
public FallbackFonts Fallbacks;
/// <summary>
/// The background color.
/// </summary>