Add font and case options to Label and Textbox

This commit is contained in:
Chandler Cox
2024-05-13 16:34:23 -05:00
parent 44006dd533
commit b66d50ae1b
2 changed files with 136 additions and 14 deletions

View File

@@ -1,9 +1,31 @@
// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
using System;
using System.ComponentModel;
namespace FlaxEngine.GUI
{
/// <summary>
/// Options for text case
/// </summary>
public enum TextCaseOptions
{
/// <summary>
/// No text case.
/// </summary>
None,
/// <summary>
/// Uppercase.
/// </summary>
UpperCase,
/// <summary>
/// Lowercase
/// </summary>
LowerCase
}
/// <summary>
/// The basic GUI label control.
/// </summary>
@@ -45,6 +67,24 @@ namespace FlaxEngine.GUI
}
}
/// <summary>
/// The text case.
/// </summary>
[EditorDisplay("Text Style"), EditorOrder(2000), Tooltip("The case of the text.")]
public TextCaseOptions CaseOption { get; set; } = TextCaseOptions.None;
/// <summary>
/// Whether to bold the text.
/// </summary>
[EditorDisplay("Text Style"), EditorOrder(2001), Tooltip("Bold the text.")]
public bool Bold { get; set; } = false;
/// <summary>
/// Whether to italicize the text.
/// </summary>
[EditorDisplay("Text Style"), EditorOrder(2002), Tooltip("Italicize the text.")]
public bool Italic { get; set; } = false;
/// <summary>
/// Gets or sets the color of the text.
/// </summary>
@@ -234,18 +274,51 @@ namespace FlaxEngine.GUI
}
}
Render2D.DrawText(_font.GetFont(), Material, _text, rect, color, hAlignment, wAlignment, Wrapping, BaseLinesGapScale, scale);
Font font = GetFont();
var text = ConvertedText();
Render2D.DrawText(font, Material, text, rect, color, hAlignment, wAlignment, Wrapping, BaseLinesGapScale, scale);
if (ClipText)
Render2D.PopClip();
}
Font GetFont()
{
Font font;
if (Bold)
font = Italic ? _font.GetBold().GetItalic().GetFont() : _font.GetBold().GetFont();
else if (Italic)
font = _font.GetItalic().GetFont();
else
font = _font.GetFont();
return font;
}
LocalizedString ConvertedText()
{
LocalizedString text = _text;
switch (CaseOption)
{
case TextCaseOptions.None: break;
case TextCaseOptions.UpperCase:
text = text.ToString().ToUpper();
break;
case TextCaseOptions.LowerCase:
text = text.ToString().ToLower();
break;
default: break;
}
return text;
}
/// <inheritdoc />
protected override void PerformLayoutBeforeChildren()
{
if (_autoWidth || _autoHeight || _autoFitText)
{
var font = _font.GetFont();
Font font = GetFont();
var text = ConvertedText();
if (font)
{
// Calculate text size
@@ -255,7 +328,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 = font.MeasureText(text, ref layout);
_textSize.Y *= BaseLinesGapScale;
// Check if size is controlled via text