Add TextFormat, SelectedItemChanged, and text alignment options to Dropdown

This commit is contained in:
Wojtek Figat
2024-08-07 17:46:30 +02:00
parent 1c02f3d8fe
commit 6e01cca9ad

View File

@@ -207,7 +207,7 @@ namespace FlaxEngine.GUI
/// <summary> /// <summary>
/// Gets or sets the items collection. /// Gets or sets the items collection.
/// </summary> /// </summary>
[EditorOrder(1), Tooltip("The items collection.")] [EditorOrder(1)]
public List<LocalizedString> Items public List<LocalizedString> Items
{ {
get => _items; get => _items;
@@ -220,7 +220,7 @@ namespace FlaxEngine.GUI
[HideInEditor, NoSerialize] [HideInEditor, NoSerialize]
public string SelectedItem public string SelectedItem
{ {
get => _selectedIndex != -1 ? _items[_selectedIndex].ToString() : string.Empty; get => _selectedIndex > -1 && _selectedIndex < _items.Count ? _items[_selectedIndex].ToString() : string.Empty;
set => SelectedIndex = _items.IndexOf(value); set => SelectedIndex = _items.IndexOf(value);
} }
@@ -230,7 +230,7 @@ namespace FlaxEngine.GUI
[HideInEditor, NoSerialize] [HideInEditor, NoSerialize]
public LocalizedString SelectedItemLocalized public LocalizedString SelectedItemLocalized
{ {
get => _selectedIndex != -1 ? _items[_selectedIndex] : LocalizedString.Empty; get => _selectedIndex > -1 && _selectedIndex < _items.Count ? _items[_selectedIndex] : LocalizedString.Empty;
set => SelectedIndex = _items.IndexOf(value); set => SelectedIndex = _items.IndexOf(value);
} }
@@ -253,17 +253,22 @@ namespace FlaxEngine.GUI
} }
/// <summary> /// <summary>
/// Gets or sets whether to show all of the items. /// Gets or sets whether to show all the items in the dropdown.
/// </summary> /// </summary>
[EditorOrder(3), Tooltip("Whether to show all of the items in the drop down.")] [EditorOrder(3)]
public bool ShowAllItems { get; set; } = true; public bool ShowAllItems { get; set; } = true;
/// <summary> /// <summary>
/// Gets or sets the maximum number of items to show at once. Only used if ShowAllItems is false. /// Gets or sets the maximum number of items to show at once. Only used if ShowAllItems is false.
/// </summary> /// </summary>
[EditorOrder(4), VisibleIf(nameof(ShowAllItems), true), Limit(1), Tooltip("The number of items to show in the drop down.")] [EditorOrder(4), VisibleIf(nameof(ShowAllItems), true), Limit(1)]
public int ShowMaxItemsCount { get; set; } = 5; public int ShowMaxItemsCount { get; set; } = 5;
/// <summary>
/// Event fired when selected item gets changed.
/// </summary>
public event Action SelectedItemChanged;
/// <summary> /// <summary>
/// Event fired when selected index gets changed. /// Event fired when selected index gets changed.
/// </summary> /// </summary>
@@ -277,21 +282,39 @@ namespace FlaxEngine.GUI
/// <summary> /// <summary>
/// Gets or sets the font used to draw text. /// Gets or sets the font used to draw text.
/// </summary> /// </summary>
[EditorDisplay("Text Style"), EditorOrder(2021)] [EditorDisplay("Text Style"), EditorOrder(2020)]
public FontReference Font { get; set; } public FontReference Font { get; set; }
/// <summary> /// <summary>
/// Gets or sets the custom material used to render the text. It must has domain set to GUI and have a public texture parameter named Font used to sample font atlas texture with font characters data. /// Gets or sets the custom material used to render the text. It must has domain set to GUI and have a public texture parameter named Font used to sample font atlas texture with font characters data.
/// </summary> /// </summary>
[EditorDisplay("Text Style"), EditorOrder(2022), Tooltip("Custom material used to render the text. It must has domain set to GUI and have a public texture parameter named Font used to sample font atlas texture with font characters data.")] [EditorDisplay("Text Style"), EditorOrder(2021)]
public MaterialBase FontMaterial { get; set; } public MaterialBase FontMaterial { get; set; }
/// <summary>
/// Gets or sets the custom text format for selected item displaying. Can be used to prefix or/and postfix actual selected value within the drowpdown control text where '{0}' is used to insert selected value text. Example: 'Selected: {0}'. Leave empty if unussed.
/// </summary>
[EditorDisplay("Text Style"), EditorOrder(2022)]
public LocalizedString TextFormat { get; set; }
/// <summary> /// <summary>
/// Gets or sets the color of the text. /// Gets or sets the color of the text.
/// </summary> /// </summary>
[EditorDisplay("Text Style"), EditorOrder(2020), ExpandGroups] [EditorDisplay("Text Style"), EditorOrder(2023), ExpandGroups]
public Color TextColor { get; set; } public Color TextColor { get; set; }
/// <summary>
/// Gets or sets the horizontal text alignment within the control bounds.
/// </summary>
[EditorDisplay("Text Style"), EditorOrder(2027)]
public TextAlignment HorizontalAlignment { get; set; } = TextAlignment.Near;
/// <summary>
/// Gets or sets the vertical text alignment within the control bounds.
/// </summary>
[EditorDisplay("Text Style"), EditorOrder(2028)]
public TextAlignment VerticalAlignment { get; set; } = TextAlignment.Center;
/// <summary> /// <summary>
/// Gets or sets the color of the border. /// Gets or sets the color of the border.
/// </summary> /// </summary>
@@ -319,7 +342,7 @@ namespace FlaxEngine.GUI
/// <summary> /// <summary>
/// Gets or sets the border color when dropdown is highlighted. /// Gets or sets the border color when dropdown is highlighted.
/// </summary> /// </summary>
[EditorDisplay("Border Style"), EditorOrder(2011)] [EditorDisplay("Border Style"), EditorOrder(2013)]
public Color BorderColorHighlighted { get; set; } public Color BorderColorHighlighted { get; set; }
/// <summary> /// <summary>
@@ -420,6 +443,7 @@ namespace FlaxEngine.GUI
protected virtual void OnSelectedIndexChanged() protected virtual void OnSelectedIndexChanged()
{ {
SelectedIndexChanged?.Invoke(this); SelectedIndexChanged?.Invoke(this);
SelectedItemChanged?.Invoke();
} }
/// <summary> /// <summary>
@@ -506,7 +530,8 @@ namespace FlaxEngine.GUI
Font = Font, Font = Font,
TextColor = Color.White * 0.9f, TextColor = Color.White * 0.9f,
TextColorHighlighted = Color.White, TextColorHighlighted = Color.White,
HorizontalAlignment = TextAlignment.Near, HorizontalAlignment = HorizontalAlignment,
VerticalAlignment = VerticalAlignment,
Text = _items[i], Text = _items[i],
Parent = item, Parent = item,
Tag = i, Tag = i,
@@ -681,7 +706,11 @@ namespace FlaxEngine.GUI
var textRect = new Rectangle(margin, 0, clientRect.Width - boxSize - 2.0f * margin, clientRect.Height); var textRect = new Rectangle(margin, 0, clientRect.Width - boxSize - 2.0f * margin, clientRect.Height);
Render2D.PushClip(textRect); Render2D.PushClip(textRect);
var textColor = TextColor; var textColor = TextColor;
Render2D.DrawText(Font.GetFont(), FontMaterial, _items[_selectedIndex], textRect, enabled ? textColor : textColor * 0.5f, TextAlignment.Near, TextAlignment.Center); string text = _items[_selectedIndex];
string format = TextFormat != null ? TextFormat : null;
if (!string.IsNullOrEmpty(format))
text = string.Format(format, text);
Render2D.DrawText(Font.GetFont(), FontMaterial, text, textRect, enabled ? textColor : textColor * 0.5f, HorizontalAlignment, VerticalAlignment);
Render2D.PopClip(); Render2D.PopClip();
} }