2020-03-16 19:13:33 +01:00
|
|
|
using System.Linq;
|
2019-09-12 12:39:18 +02:00
|
|
|
using Microsoft.Xna.Framework;
|
2020-03-16 19:13:33 +01:00
|
|
|
using MLEM.Misc;
|
2019-09-12 12:39:18 +02:00
|
|
|
|
|
|
|
namespace MLEM.Ui.Elements {
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// A dropdown component to use inside of a <see cref="UiSystem"/>.
|
|
|
|
/// A dropdown is a component that contains a hidden panel which is displayed upon pressing the dropdown button.
|
|
|
|
/// </summary>
|
2019-09-12 12:39:18 +02:00
|
|
|
public class Dropdown : Button {
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The panel that this dropdown contains. It will be displayed upon pressing the dropdown button.
|
|
|
|
/// </summary>
|
2019-09-12 12:39:18 +02:00
|
|
|
public readonly Panel Panel;
|
2021-12-28 14:56:11 +01:00
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// This property stores whether the dropdown is currently opened or not
|
|
|
|
/// </summary>
|
2019-09-12 12:39:18 +02:00
|
|
|
public bool IsOpen {
|
|
|
|
get => !this.Panel.IsHidden;
|
2019-11-18 22:36:55 +01:00
|
|
|
set {
|
|
|
|
this.Panel.IsHidden = !value;
|
|
|
|
this.OnOpenedOrClosed?.Invoke(this);
|
|
|
|
}
|
2019-09-12 12:39:18 +02:00
|
|
|
}
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// An event that is invoked when <see cref="IsOpen"/> changes
|
|
|
|
/// </summary>
|
2019-11-18 22:36:55 +01:00
|
|
|
public GenericCallback OnOpenedOrClosed;
|
2019-09-12 12:39:18 +02:00
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new dropdown with the given settings
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="anchor">The dropdown's anchor</param>
|
|
|
|
/// <param name="size">The dropdown button's size</param>
|
|
|
|
/// <param name="text">The text displayed on the dropdown button</param>
|
|
|
|
/// <param name="tooltipText">The text displayed as a tooltip when hovering over the dropdown button</param>
|
2021-10-29 23:33:15 +02:00
|
|
|
public Dropdown(Anchor anchor, Vector2 size, string text = null, string tooltipText = null) : base(anchor, size, text, tooltipText) {
|
2019-09-12 12:39:18 +02:00
|
|
|
this.Panel = this.AddChild(new Panel(Anchor.TopCenter, size, Vector2.Zero, true) {
|
|
|
|
IsHidden = true
|
|
|
|
});
|
|
|
|
this.OnAreaUpdated += e => this.Panel.PositionOffset = new Vector2(0, e.Area.Height / this.Scale);
|
2019-11-18 22:36:55 +01:00
|
|
|
this.OnOpenedOrClosed += e => this.Priority = this.IsOpen ? 10000 : 0;
|
2019-09-12 12:39:18 +02:00
|
|
|
this.OnPressed += e => this.IsOpen = !this.IsOpen;
|
2022-03-10 15:04:52 +01:00
|
|
|
this.GetGamepadNextElement = (dir, usualNext) => {
|
|
|
|
// Force navigate down to our first child if we're open
|
|
|
|
if (this.IsOpen && dir == Direction2.Down)
|
|
|
|
return this.Panel.GetChildren().FirstOrDefault(c => c.CanBeSelected) ?? usualNext;
|
|
|
|
return usualNext;
|
|
|
|
};
|
2019-09-12 12:39:18 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Adds an element to this dropdown's <see cref="Panel"/>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="element">The element to add</param>
|
2022-05-03 19:07:53 +02:00
|
|
|
/// <param name="index">The index to add the child at, or -1 to add it to the end of the <see cref="Element.Children"/> list</param>
|
|
|
|
public void AddElement(Element element, int index = -1) {
|
|
|
|
this.Panel.AddChild(element, index);
|
2020-03-16 19:13:33 +01:00
|
|
|
// Since the dropdown causes elements to be over each other,
|
|
|
|
// usual gamepad code doesn't apply
|
|
|
|
element.GetGamepadNextElement = (dir, usualNext) => {
|
2022-03-10 15:04:52 +01:00
|
|
|
if (dir == Direction2.Left || dir == Direction2.Right)
|
|
|
|
return null;
|
2020-03-16 19:13:33 +01:00
|
|
|
if (dir == Direction2.Up) {
|
2022-03-17 20:46:49 +01:00
|
|
|
var prev = element.GetOlderSibling(e => e.CanBeSelected);
|
2020-03-16 19:22:08 +01:00
|
|
|
return prev ?? this;
|
2020-03-16 19:13:33 +01:00
|
|
|
} else if (dir == Direction2.Down) {
|
2022-03-17 20:46:49 +01:00
|
|
|
return element.GetSiblings(e => e.CanBeSelected && e.GetOlderSibling(s => s.CanBeSelected) == element).FirstOrDefault();
|
2020-03-16 19:13:33 +01:00
|
|
|
}
|
|
|
|
return usualNext;
|
|
|
|
};
|
2019-09-12 12:39:18 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Adds a pressable <see cref="Paragraph"/> element to this dropdown's <see cref="Panel"/>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="text">The text to display</param>
|
|
|
|
/// <param name="pressed">The resulting paragraph's <see cref="Element.OnPressed"/> event</param>
|
2022-05-03 19:07:53 +02:00
|
|
|
/// <param name="index">The index to add the child at, or -1 to add it to the end of the <see cref="Element.Children"/> list</param>
|
|
|
|
public Element AddElement(string text, GenericCallback pressed = null, int index = -1) {
|
|
|
|
return this.AddElement(p => text, pressed, index);
|
2019-09-12 12:39:18 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Adds a pressable <see cref="Paragraph"/> element to this dropdown's <see cref="Panel"/>.
|
|
|
|
/// By default, the paragraph's text color will change from <see cref="Color.White"/> to <see cref="Color.LightGray"/> when hovering over it.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="text">The text to display</param>
|
|
|
|
/// <param name="pressed">The resulting paragraph's <see cref="Element.OnPressed"/> event</param>
|
2022-05-03 19:07:53 +02:00
|
|
|
/// <param name="index">The index to add the child at, or -1 to add it to the end of the <see cref="Element.Children"/> list</param>
|
|
|
|
public Element AddElement(Paragraph.TextCallback text, GenericCallback pressed = null, int index = -1) {
|
2019-09-12 12:39:18 +02:00
|
|
|
var paragraph = new Paragraph(Anchor.AutoLeft, 1, text) {
|
|
|
|
CanBeMoused = true,
|
|
|
|
CanBeSelected = true,
|
|
|
|
PositionOffset = new Vector2(0, 1)
|
|
|
|
};
|
|
|
|
if (pressed != null)
|
|
|
|
paragraph.OnPressed += pressed;
|
2019-11-05 13:28:41 +01:00
|
|
|
paragraph.OnMouseEnter += e => paragraph.TextColor = Color.LightGray;
|
|
|
|
paragraph.OnMouseExit += e => paragraph.TextColor = Color.White;
|
2022-05-03 19:07:53 +02:00
|
|
|
this.AddElement(paragraph, index);
|
2020-11-08 18:16:12 +01:00
|
|
|
return paragraph;
|
2019-09-12 12:39:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|