Add LocalizedString support for UI

This commit is contained in:
Wojtek Figat
2021-04-27 14:28:42 +02:00
parent 5fc2f12139
commit 17467c81a4
7 changed files with 86 additions and 33 deletions

View File

@@ -51,7 +51,9 @@ namespace FlaxEngine
/// <returns>The string.</returns>
public static implicit operator string(LocalizedString str)
{
return str.ToString();
if ((object)str == null)
return null;
return string.IsNullOrEmpty(str.Value) ? Localization.GetString(str.Id) : str.Value;
}
/// <summary>
@@ -61,9 +63,35 @@ namespace FlaxEngine
/// <returns>The localized string.</returns>
public static implicit operator LocalizedString(string str)
{
if (str == null)
return null;
return new LocalizedString(str);
}
/// <summary>
/// Compares two localized strings.
/// </summary>
/// <param name="left">The lft string.</param>
/// <param name="right">The right string.</param>
/// <returns>True if both values are equal, otherwise false.</returns>
public static bool operator ==(LocalizedString left, LocalizedString right)
{
return left?.Equals(right) ?? (object)right == null;
}
/// <summary>
/// Compares two localized strings.
/// </summary>
/// <param name="left">The lft string.</param>
/// <param name="right">The right string.</param>
/// <returns>True if both values are not equal, otherwise false.</returns>
public static bool operator !=(LocalizedString left, LocalizedString right)
{
if ((object)left == null)
return (object)right != null;
return !left.Equals(right);
}
/// <inheritdoc />
public int CompareTo(object obj)
{
@@ -77,7 +105,7 @@ namespace FlaxEngine
/// <inheritdoc />
public bool Equals(LocalizedString other)
{
return Id == other.Id && Value == other.Value;
return (object)other != null && Id == other.Id && Value == other.Value;
}
/// <inheritdoc />
@@ -101,7 +129,7 @@ namespace FlaxEngine
/// <inheritdoc />
public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is LocalizedString other && Equals(other);
return (object)this == (object)obj || obj is LocalizedString other && Equals(other);
}
/// <inheritdoc />

View File

@@ -24,11 +24,20 @@ namespace FlaxEngine.GUI
/// </summary>
protected FontReference _font;
/// <summary>
/// The text.
/// </summary>
protected LocalizedString _text;
/// <summary>
/// Button text property.
/// </summary>
[EditorOrder(10), Tooltip("The button label text.")]
public string Text { get; set; }
public LocalizedString Text
{
get => _text;
set => _text = value;
}
/// <summary>
/// Gets or sets the font used to draw button text.
@@ -201,7 +210,7 @@ namespace FlaxEngine.GUI
Render2D.DrawRectangle(clientRect, borderColor);
// Draw text
Render2D.DrawText(_font.GetFont(), TextMaterial, Text, clientRect, textColor, TextAlignment.Center, TextAlignment.Center);
Render2D.DrawText(_font.GetFont(), TextMaterial, _text, clientRect, textColor, TextAlignment.Center, TextAlignment.Center);
}
/// <inheritdoc />

View File

@@ -77,7 +77,7 @@ namespace FlaxEngine.GUI
/// <summary>
/// The items.
/// </summary>
protected List<string> _items = new List<string>();
protected List<LocalizedString> _items = new List<LocalizedString>();
/// <summary>
/// The popup menu. May be null if has not been used yet.
@@ -95,7 +95,7 @@ namespace FlaxEngine.GUI
/// Gets or sets the items collection.
/// </summary>
[EditorOrder(1), Tooltip("The items collection.")]
public List<string> Items
public List<LocalizedString> Items
{
get => _items;
set => _items = value;
@@ -107,7 +107,17 @@ namespace FlaxEngine.GUI
[HideInEditor, NoSerialize]
public string SelectedItem
{
get => _selectedIndex != -1 ? _items[_selectedIndex] : string.Empty;
get => _selectedIndex != -1 ? _items[_selectedIndex].ToString() : string.Empty;
set => SelectedIndex = _items.IndexOf(value);
}
/// <summary>
/// Gets or sets the selected item (returns <see cref="LocalizedString.Empty"/> if no item is being selected).
/// </summary>
[HideInEditor, NoSerialize]
public LocalizedString SelectedItemLocalized
{
get => _selectedIndex != -1 ? _items[_selectedIndex] : LocalizedString.Empty;
set => SelectedIndex = _items.IndexOf(value);
}
@@ -265,7 +275,8 @@ namespace FlaxEngine.GUI
/// <param name="items">The items.</param>
public void AddItems(IEnumerable<string> items)
{
_items.AddRange(items);
foreach (var item in items)
_items.Add(item);
}
/// <summary>
@@ -276,7 +287,8 @@ namespace FlaxEngine.GUI
{
SelectedIndex = -1;
_items.Clear();
_items.AddRange(items);
foreach (var item in items)
_items.Add(item);
}
/// <summary>

View File

@@ -10,7 +10,11 @@ namespace FlaxEngine.GUI
/// <seealso cref="FlaxEngine.GUI.ContainerControl" />
public class Label : ContainerControl
{
private string _text;
/// <summary>
/// The text.
/// </summary>
protected LocalizedString _text;
private bool _autoWidth;
private bool _autoHeight;
private bool _autoFitText;
@@ -26,7 +30,7 @@ namespace FlaxEngine.GUI
/// Gets or sets the text.
/// </summary>
[EditorOrder(10), MultilineText, Tooltip("The label text.")]
public string Text
public LocalizedString Text
{
get => _text;
set
@@ -225,18 +229,7 @@ namespace FlaxEngine.GUI
}
}
Render2D.DrawText(
_font.GetFont(),
Material,
Text,
rect,
color,
hAlignment,
wAlignment,
Wrapping,
1.0f,
scale
);
Render2D.DrawText(_font.GetFont(), Material, _text, rect, color, hAlignment, wAlignment, Wrapping, 1.0f, scale);
if (ClipText)
Render2D.PopClip();

View File

@@ -9,11 +9,20 @@ namespace FlaxEngine.GUI
{
private TextLayoutOptions _layout;
/// <summary>
/// The watermark text.
/// </summary>
protected LocalizedString _watermarkText;
/// <summary>
/// Gets or sets the watermark text to show grayed when textbox is empty.
/// </summary>
[EditorOrder(20), Tooltip("The watermark text to show grayed when textbox is empty.")]
public string WatermarkText { get; set; }
public LocalizedString WatermarkText
{
get => _watermarkText;
set => _watermarkText = value;
}
/// <summary>
/// Gets or sets the text wrapping within the control bounds.
@@ -201,9 +210,9 @@ namespace FlaxEngine.GUI
color *= 0.6f;
Render2D.DrawText(font, _text, color, ref _layout, TextMaterial);
}
else if (!string.IsNullOrEmpty(WatermarkText) && !IsFocused)
else if (!string.IsNullOrEmpty(_watermarkText) && !IsFocused)
{
Render2D.DrawText(font, WatermarkText, WatermarkTextColor, ref _layout, TextMaterial);
Render2D.DrawText(font, _watermarkText, WatermarkTextColor, ref _layout, TextMaterial);
}
// Caret