From b66d50ae1b500875fbd17e31c1421a675eda2889 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Mon, 13 May 2024 16:34:23 -0500 Subject: [PATCH 1/4] Add font and case options to Label and Textbox --- Source/Engine/UI/GUI/Common/Label.cs | 79 +++++++++++++++++++++++++- Source/Engine/UI/GUI/Common/TextBox.cs | 71 +++++++++++++++++++---- 2 files changed, 136 insertions(+), 14 deletions(-) diff --git a/Source/Engine/UI/GUI/Common/Label.cs b/Source/Engine/UI/GUI/Common/Label.cs index 61282e7a5..f5c383765 100644 --- a/Source/Engine/UI/GUI/Common/Label.cs +++ b/Source/Engine/UI/GUI/Common/Label.cs @@ -1,9 +1,31 @@ // Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. +using System; using System.ComponentModel; namespace FlaxEngine.GUI { + /// + /// Options for text case + /// + public enum TextCaseOptions + { + /// + /// No text case. + /// + None, + + /// + /// Uppercase. + /// + UpperCase, + + /// + /// Lowercase + /// + LowerCase + } + /// /// The basic GUI label control. /// @@ -45,6 +67,24 @@ namespace FlaxEngine.GUI } } + /// + /// The text case. + /// + [EditorDisplay("Text Style"), EditorOrder(2000), Tooltip("The case of the text.")] + public TextCaseOptions CaseOption { get; set; } = TextCaseOptions.None; + + /// + /// Whether to bold the text. + /// + [EditorDisplay("Text Style"), EditorOrder(2001), Tooltip("Bold the text.")] + public bool Bold { get; set; } = false; + + /// + /// Whether to italicize the text. + /// + [EditorDisplay("Text Style"), EditorOrder(2002), Tooltip("Italicize the text.")] + public bool Italic { get; set; } = false; + /// /// Gets or sets the color of the text. /// @@ -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; + } + /// 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 diff --git a/Source/Engine/UI/GUI/Common/TextBox.cs b/Source/Engine/UI/GUI/Common/TextBox.cs index 3d0581c4e..035de2267 100644 --- a/Source/Engine/UI/GUI/Common/TextBox.cs +++ b/Source/Engine/UI/GUI/Common/TextBox.cs @@ -24,6 +24,24 @@ namespace FlaxEngine.GUI get => _watermarkText; set => _watermarkText = value; } + + /// + /// The text case. + /// + [EditorDisplay("Text Style"), EditorOrder(2000), Tooltip("The case of the text.")] + public TextCaseOptions CaseOption { get; set; } = TextCaseOptions.None; + + /// + /// Whether to bold the text. + /// + [EditorDisplay("Text Style"), EditorOrder(2001), Tooltip("Bold the text.")] + public bool Bold { get; set; } = false; + + /// + /// Whether to italicize the text. + /// + [EditorDisplay("Text Style"), EditorOrder(2002), Tooltip("Italicize the text.")] + public bool Italic { get; set; } = false; /// /// Gets or sets the text wrapping within the control bounds. @@ -98,19 +116,48 @@ namespace FlaxEngine.GUI /// public override Float2 GetTextSize() { - var font = Font.GetFont(); + var font = GetFont(); if (font == null) { 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; } /// public override Float2 GetCharPosition(int index, out float height) { - var font = Font.GetFont(); + var font = GetFont(); if (font == null) { height = Height; @@ -118,19 +165,19 @@ namespace FlaxEngine.GUI } height = font.Height / DpiScale; - return font.GetCharPosition(_text, index, ref _layout); + return font.GetCharPosition(ConvertedText(), index, ref _layout); } /// public override int HitTestText(Float2 location) { - var font = Font.GetFont(); + var font = GetFont(); if (font == null) { return 0; } - return font.HitTestText(_text, location, ref _layout); + return font.HitTestText(ConvertedText(), location, ref _layout); } /// @@ -147,7 +194,7 @@ namespace FlaxEngine.GUI // Cache data var rect = new Rectangle(Float2.Zero, Size); bool enabled = EnabledInHierarchy; - var font = Font.GetFont(); + var font = GetFont(); if (!font) return; @@ -166,11 +213,13 @@ namespace FlaxEngine.GUI if (useViewOffset) Render2D.PushTransform(Matrix3x3.Translation2D(-_viewOffset)); + var text = ConvertedText(); + // Check if sth is selected to draw selection if (HasSelection) { - var leftEdge = font.GetCharPosition(_text, SelectionLeft, ref _layout); - var rightEdge = font.GetCharPosition(_text, SelectionRight, ref _layout); + var leftEdge = font.GetCharPosition(text, SelectionLeft, ref _layout); + var rightEdge = font.GetCharPosition(text, SelectionRight, ref _layout); var fontHeight = font.Height; var textHeight = fontHeight / DpiScale; @@ -207,12 +256,12 @@ namespace FlaxEngine.GUI } // Text or watermark - if (_text.Length > 0) + if (text.Length > 0) { var color = TextColor; if (!enabled) color *= 0.6f; - Render2D.DrawText(font, _text, color, ref _layout, TextMaterial); + Render2D.DrawText(font, text, color, ref _layout, TextMaterial); } else if (!string.IsNullOrEmpty(_watermarkText)) { From 6962ed6730dbd1c5fd0bbf03f75064d89b9c37d4 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Mon, 13 May 2024 16:38:10 -0500 Subject: [PATCH 2/4] Fix case spelling --- Source/Engine/UI/GUI/Common/Label.cs | 8 ++++---- Source/Engine/UI/GUI/Common/TextBox.cs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/Engine/UI/GUI/Common/Label.cs b/Source/Engine/UI/GUI/Common/Label.cs index f5c383765..cb357773f 100644 --- a/Source/Engine/UI/GUI/Common/Label.cs +++ b/Source/Engine/UI/GUI/Common/Label.cs @@ -18,12 +18,12 @@ namespace FlaxEngine.GUI /// /// Uppercase. /// - UpperCase, + Uppercase, /// /// Lowercase /// - LowerCase + Lowercase } /// @@ -301,10 +301,10 @@ namespace FlaxEngine.GUI switch (CaseOption) { case TextCaseOptions.None: break; - case TextCaseOptions.UpperCase: + case TextCaseOptions.Uppercase: text = text.ToString().ToUpper(); break; - case TextCaseOptions.LowerCase: + case TextCaseOptions.Lowercase: text = text.ToString().ToLower(); break; default: break; diff --git a/Source/Engine/UI/GUI/Common/TextBox.cs b/Source/Engine/UI/GUI/Common/TextBox.cs index 035de2267..488fa594c 100644 --- a/Source/Engine/UI/GUI/Common/TextBox.cs +++ b/Source/Engine/UI/GUI/Common/TextBox.cs @@ -143,10 +143,10 @@ namespace FlaxEngine.GUI switch (CaseOption) { case TextCaseOptions.None: break; - case TextCaseOptions.UpperCase: + case TextCaseOptions.Uppercase: text = text.ToUpper(); break; - case TextCaseOptions.LowerCase: + case TextCaseOptions.Lowercase: text = text.ToLower(); break; default: break; From e028d263f1613536901c50a16aa79d15ba6fb08f Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Mon, 13 May 2024 16:38:38 -0500 Subject: [PATCH 3/4] Remove unused include. --- Source/Engine/UI/GUI/Common/Label.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/Engine/UI/GUI/Common/Label.cs b/Source/Engine/UI/GUI/Common/Label.cs index cb357773f..5ef323f23 100644 --- a/Source/Engine/UI/GUI/Common/Label.cs +++ b/Source/Engine/UI/GUI/Common/Label.cs @@ -1,6 +1,5 @@ // Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. -using System; using System.ComponentModel; namespace FlaxEngine.GUI From 17de6388ca988d1236ce0d4216b71c5caa21cc3d Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Mon, 13 May 2024 17:42:37 -0500 Subject: [PATCH 4/4] Code fix --- Source/Engine/UI/GUI/Common/Label.cs | 4 ++-- Source/Engine/UI/GUI/Common/TextBox.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/Engine/UI/GUI/Common/Label.cs b/Source/Engine/UI/GUI/Common/Label.cs index 5ef323f23..9aae957c0 100644 --- a/Source/Engine/UI/GUI/Common/Label.cs +++ b/Source/Engine/UI/GUI/Common/Label.cs @@ -282,7 +282,7 @@ namespace FlaxEngine.GUI Render2D.PopClip(); } - Font GetFont() + private Font GetFont() { Font font; if (Bold) @@ -294,7 +294,7 @@ namespace FlaxEngine.GUI return font; } - LocalizedString ConvertedText() + private LocalizedString ConvertedText() { LocalizedString text = _text; switch (CaseOption) diff --git a/Source/Engine/UI/GUI/Common/TextBox.cs b/Source/Engine/UI/GUI/Common/TextBox.cs index 488fa594c..0343e033a 100644 --- a/Source/Engine/UI/GUI/Common/TextBox.cs +++ b/Source/Engine/UI/GUI/Common/TextBox.cs @@ -125,7 +125,7 @@ namespace FlaxEngine.GUI return font.MeasureText(ConvertedText(), ref _layout); } - Font GetFont() + private Font GetFont() { Font font; if (Bold) @@ -137,7 +137,7 @@ namespace FlaxEngine.GUI return font; } - string ConvertedText() + private string ConvertedText() { string text = _text; switch (CaseOption)