diff --git a/Source/Editor/CustomEditors/Dedicated/LocalizationSettingsEditor.cs b/Source/Editor/CustomEditors/Dedicated/LocalizationSettingsEditor.cs index ea56c7630..521b844ec 100644 --- a/Source/Editor/CustomEditors/Dedicated/LocalizationSettingsEditor.cs +++ b/Source/Editor/CustomEditors/Dedicated/LocalizationSettingsEditor.cs @@ -353,7 +353,7 @@ namespace FlaxEditor.CustomEditors.Dedicated } } - private static void FindNewKeysJson(string file, Dictionary newKeys, HashSet allKeys, JToken token) + private static void FindNewKeysJson(Dictionary newKeys, HashSet allKeys, JToken token) { if (token is JObject o) { @@ -377,14 +377,14 @@ namespace FlaxEditor.CustomEditors.Dedicated } } } - FindNewKeysJson(file, newKeys, allKeys, p.Value); + FindNewKeysJson(newKeys, allKeys, p.Value); } } else if (token is JArray a) { foreach (var p in a) { - FindNewKeysJson(file, newKeys, allKeys, p); + FindNewKeysJson(newKeys, allKeys, p); } } } @@ -395,7 +395,7 @@ namespace FlaxEditor.CustomEditors.Dedicated using (var jsonReader = new JsonTextReader(reader)) { var token = JToken.ReadFrom(jsonReader); - FindNewKeysJson(file, newKeys, allKeys, token); + FindNewKeysJson(newKeys, allKeys, token); } } diff --git a/Source/Editor/CustomEditors/Editors/LocalizedStringEditor.cs b/Source/Editor/CustomEditors/Editors/LocalizedStringEditor.cs index 0f93df70b..82503d9bc 100644 --- a/Source/Editor/CustomEditors/Editors/LocalizedStringEditor.cs +++ b/Source/Editor/CustomEditors/Editors/LocalizedStringEditor.cs @@ -30,9 +30,9 @@ namespace FlaxEditor.CustomEditors.Editors { base.Initialize(layout); - if (layout.Children.Count != 1) + if (layout.Children.Count == 0) return; - var propList = layout.Children[0] as PropertiesListElement; + var propList = layout.Children[layout.Children.Count - 1] as PropertiesListElement; if (propList == null || propList.Children.Count != 2) return; var idElement = propList.Children[0] as TextBoxElement; @@ -66,6 +66,7 @@ namespace FlaxEditor.CustomEditors.Editors Text = "+", TooltipText = "Add new localized text to Localization Settings (all used locales)", Parent = _valueElement.TextBox, + Enabled = IsSingleObject, }; addString.SetAnchorPreset(AnchorPresets.MiddleRight, false, true); addString.ButtonClicked += OnAddStringClicked; @@ -175,6 +176,7 @@ namespace FlaxEditor.CustomEditors.Editors allKeys.Add(e.Key); } } + _valueElement.TextBox.SetTextAsUser(null); string newKey = null; if (string.IsNullOrEmpty(_idElement.Text)) { diff --git a/Source/Engine/Localization/LocalizedString.cs b/Source/Engine/Localization/LocalizedString.cs index 9d7dc7720..2d6535336 100644 --- a/Source/Engine/Localization/LocalizedString.cs +++ b/Source/Engine/Localization/LocalizedString.cs @@ -51,7 +51,9 @@ namespace FlaxEngine /// The string. 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; } /// @@ -61,9 +63,35 @@ namespace FlaxEngine /// The localized string. public static implicit operator LocalizedString(string str) { + if (str == null) + return null; return new LocalizedString(str); } + /// + /// Compares two localized strings. + /// + /// The lft string. + /// The right string. + /// True if both values are equal, otherwise false. + public static bool operator ==(LocalizedString left, LocalizedString right) + { + return left?.Equals(right) ?? (object)right == null; + } + + /// + /// Compares two localized strings. + /// + /// The lft string. + /// The right string. + /// True if both values are not equal, otherwise false. + public static bool operator !=(LocalizedString left, LocalizedString right) + { + if ((object)left == null) + return (object)right != null; + return !left.Equals(right); + } + /// public int CompareTo(object obj) { @@ -77,7 +105,7 @@ namespace FlaxEngine /// public bool Equals(LocalizedString other) { - return Id == other.Id && Value == other.Value; + return (object)other != null && Id == other.Id && Value == other.Value; } /// @@ -101,7 +129,7 @@ namespace FlaxEngine /// 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); } /// diff --git a/Source/Engine/UI/GUI/Common/Button.cs b/Source/Engine/UI/GUI/Common/Button.cs index 639552ec3..51d115780 100644 --- a/Source/Engine/UI/GUI/Common/Button.cs +++ b/Source/Engine/UI/GUI/Common/Button.cs @@ -24,11 +24,20 @@ namespace FlaxEngine.GUI /// protected FontReference _font; + /// + /// The text. + /// + protected LocalizedString _text; + /// /// Button text property. /// [EditorOrder(10), Tooltip("The button label text.")] - public string Text { get; set; } + public LocalizedString Text + { + get => _text; + set => _text = value; + } /// /// 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); } /// diff --git a/Source/Engine/UI/GUI/Common/Dropdown.cs b/Source/Engine/UI/GUI/Common/Dropdown.cs index 3bb00990f..ea2debb4a 100644 --- a/Source/Engine/UI/GUI/Common/Dropdown.cs +++ b/Source/Engine/UI/GUI/Common/Dropdown.cs @@ -77,7 +77,7 @@ namespace FlaxEngine.GUI /// /// The items. /// - protected List _items = new List(); + protected List _items = new List(); /// /// 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. /// [EditorOrder(1), Tooltip("The items collection.")] - public List Items + public List 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); + } + + /// + /// Gets or sets the selected item (returns if no item is being selected). + /// + [HideInEditor, NoSerialize] + public LocalizedString SelectedItemLocalized + { + get => _selectedIndex != -1 ? _items[_selectedIndex] : LocalizedString.Empty; set => SelectedIndex = _items.IndexOf(value); } @@ -265,7 +275,8 @@ namespace FlaxEngine.GUI /// The items. public void AddItems(IEnumerable items) { - _items.AddRange(items); + foreach (var item in items) + _items.Add(item); } /// @@ -276,7 +287,8 @@ namespace FlaxEngine.GUI { SelectedIndex = -1; _items.Clear(); - _items.AddRange(items); + foreach (var item in items) + _items.Add(item); } /// diff --git a/Source/Engine/UI/GUI/Common/Label.cs b/Source/Engine/UI/GUI/Common/Label.cs index 196bbb2b6..e935cd0eb 100644 --- a/Source/Engine/UI/GUI/Common/Label.cs +++ b/Source/Engine/UI/GUI/Common/Label.cs @@ -10,7 +10,11 @@ namespace FlaxEngine.GUI /// public class Label : ContainerControl { - private string _text; + /// + /// The text. + /// + protected LocalizedString _text; + private bool _autoWidth; private bool _autoHeight; private bool _autoFitText; @@ -26,7 +30,7 @@ namespace FlaxEngine.GUI /// Gets or sets the text. /// [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(); diff --git a/Source/Engine/UI/GUI/Common/TextBox.cs b/Source/Engine/UI/GUI/Common/TextBox.cs index 0ed5c731c..20548eed8 100644 --- a/Source/Engine/UI/GUI/Common/TextBox.cs +++ b/Source/Engine/UI/GUI/Common/TextBox.cs @@ -9,11 +9,20 @@ namespace FlaxEngine.GUI { private TextLayoutOptions _layout; + /// + /// The watermark text. + /// + protected LocalizedString _watermarkText; + /// /// Gets or sets the watermark text to show grayed when textbox is empty. /// [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; + } /// /// 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