Add font and case options to Label and Textbox
This commit is contained in:
@@ -1,9 +1,31 @@
|
|||||||
// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
|
// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
|
||||||
namespace FlaxEngine.GUI
|
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>
|
/// <summary>
|
||||||
/// The basic GUI label control.
|
/// The basic GUI label control.
|
||||||
/// </summary>
|
/// </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>
|
/// <summary>
|
||||||
/// Gets or sets the color of the text.
|
/// Gets or sets the color of the text.
|
||||||
/// </summary>
|
/// </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)
|
if (ClipText)
|
||||||
Render2D.PopClip();
|
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 />
|
/// <inheritdoc />
|
||||||
protected override void PerformLayoutBeforeChildren()
|
protected override void PerformLayoutBeforeChildren()
|
||||||
{
|
{
|
||||||
if (_autoWidth || _autoHeight || _autoFitText)
|
if (_autoWidth || _autoHeight || _autoFitText)
|
||||||
{
|
{
|
||||||
var font = _font.GetFont();
|
Font font = GetFont();
|
||||||
|
var text = ConvertedText();
|
||||||
if (font)
|
if (font)
|
||||||
{
|
{
|
||||||
// Calculate text size
|
// Calculate text size
|
||||||
@@ -255,7 +328,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 = font.MeasureText(_text, ref layout);
|
_textSize = font.MeasureText(text, ref layout);
|
||||||
_textSize.Y *= BaseLinesGapScale;
|
_textSize.Y *= BaseLinesGapScale;
|
||||||
|
|
||||||
// Check if size is controlled via text
|
// Check if size is controlled via text
|
||||||
|
|||||||
@@ -24,6 +24,24 @@ namespace FlaxEngine.GUI
|
|||||||
get => _watermarkText;
|
get => _watermarkText;
|
||||||
set => _watermarkText = value;
|
set => _watermarkText = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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>
|
/// <summary>
|
||||||
/// Gets or sets the text wrapping within the control bounds.
|
/// Gets or sets the text wrapping within the control bounds.
|
||||||
@@ -98,19 +116,48 @@ namespace FlaxEngine.GUI
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override Float2 GetTextSize()
|
public override Float2 GetTextSize()
|
||||||
{
|
{
|
||||||
var font = Font.GetFont();
|
var font = GetFont();
|
||||||
if (font == null)
|
if (font == null)
|
||||||
{
|
{
|
||||||
return Float2.Zero;
|
return Float2.Zero;
|
||||||
}
|
}
|
||||||
|
|
||||||
return font.MeasureText(_text, ref _layout);
|
return font.MeasureText(ConvertedText(), ref _layout);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
string ConvertedText()
|
||||||
|
{
|
||||||
|
string text = _text;
|
||||||
|
switch (CaseOption)
|
||||||
|
{
|
||||||
|
case TextCaseOptions.None: break;
|
||||||
|
case TextCaseOptions.UpperCase:
|
||||||
|
text = text.ToUpper();
|
||||||
|
break;
|
||||||
|
case TextCaseOptions.LowerCase:
|
||||||
|
text = text.ToLower();
|
||||||
|
break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override Float2 GetCharPosition(int index, out float height)
|
public override Float2 GetCharPosition(int index, out float height)
|
||||||
{
|
{
|
||||||
var font = Font.GetFont();
|
var font = GetFont();
|
||||||
if (font == null)
|
if (font == null)
|
||||||
{
|
{
|
||||||
height = Height;
|
height = Height;
|
||||||
@@ -118,19 +165,19 @@ namespace FlaxEngine.GUI
|
|||||||
}
|
}
|
||||||
|
|
||||||
height = font.Height / DpiScale;
|
height = font.Height / DpiScale;
|
||||||
return font.GetCharPosition(_text, index, ref _layout);
|
return font.GetCharPosition(ConvertedText(), index, ref _layout);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override int HitTestText(Float2 location)
|
public override int HitTestText(Float2 location)
|
||||||
{
|
{
|
||||||
var font = Font.GetFont();
|
var font = GetFont();
|
||||||
if (font == null)
|
if (font == null)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return font.HitTestText(_text, location, ref _layout);
|
return font.HitTestText(ConvertedText(), location, ref _layout);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@@ -147,7 +194,7 @@ namespace FlaxEngine.GUI
|
|||||||
// Cache data
|
// Cache data
|
||||||
var rect = new Rectangle(Float2.Zero, Size);
|
var rect = new Rectangle(Float2.Zero, Size);
|
||||||
bool enabled = EnabledInHierarchy;
|
bool enabled = EnabledInHierarchy;
|
||||||
var font = Font.GetFont();
|
var font = GetFont();
|
||||||
if (!font)
|
if (!font)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -166,11 +213,13 @@ namespace FlaxEngine.GUI
|
|||||||
if (useViewOffset)
|
if (useViewOffset)
|
||||||
Render2D.PushTransform(Matrix3x3.Translation2D(-_viewOffset));
|
Render2D.PushTransform(Matrix3x3.Translation2D(-_viewOffset));
|
||||||
|
|
||||||
|
var text = ConvertedText();
|
||||||
|
|
||||||
// Check if sth is selected to draw selection
|
// Check if sth is selected to draw selection
|
||||||
if (HasSelection)
|
if (HasSelection)
|
||||||
{
|
{
|
||||||
var leftEdge = font.GetCharPosition(_text, SelectionLeft, ref _layout);
|
var leftEdge = font.GetCharPosition(text, SelectionLeft, ref _layout);
|
||||||
var rightEdge = font.GetCharPosition(_text, SelectionRight, ref _layout);
|
var rightEdge = font.GetCharPosition(text, SelectionRight, ref _layout);
|
||||||
var fontHeight = font.Height;
|
var fontHeight = font.Height;
|
||||||
var textHeight = fontHeight / DpiScale;
|
var textHeight = fontHeight / DpiScale;
|
||||||
|
|
||||||
@@ -207,12 +256,12 @@ namespace FlaxEngine.GUI
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Text or watermark
|
// Text or watermark
|
||||||
if (_text.Length > 0)
|
if (text.Length > 0)
|
||||||
{
|
{
|
||||||
var color = TextColor;
|
var color = TextColor;
|
||||||
if (!enabled)
|
if (!enabled)
|
||||||
color *= 0.6f;
|
color *= 0.6f;
|
||||||
Render2D.DrawText(font, _text, color, ref _layout, TextMaterial);
|
Render2D.DrawText(font, text, color, ref _layout, TextMaterial);
|
||||||
}
|
}
|
||||||
else if (!string.IsNullOrEmpty(_watermarkText))
|
else if (!string.IsNullOrEmpty(_watermarkText))
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user