using System.Linq;
using Microsoft.Xna.Framework;
using MLEM.Misc;
namespace MLEM.Ui.Elements {
///
/// A dropdown component to use inside of a .
/// A dropdown is a component that contains a hidden panel which is displayed upon pressing the dropdown button.
///
public class Dropdown : Button {
///
/// The panel that this dropdown contains. It will be displayed upon pressing the dropdown button.
///
public readonly Panel Panel;
///
/// This property stores whether the dropdown is currently opened or not
///
public bool IsOpen {
get => !this.Panel.IsHidden;
set {
this.Panel.IsHidden = !value;
this.OnOpenedOrClosed?.Invoke(this);
}
}
///
/// An event that is invoked when changes
///
public GenericCallback OnOpenedOrClosed;
///
/// Creates a new dropdown with the given settings
///
/// The dropdown's anchor
/// The dropdown button's size
/// The text displayed on the dropdown button
/// The text displayed as a tooltip when hovering over the dropdown button
/// The width of the dropdown button's tooltip
public Dropdown(Anchor anchor, Vector2 size, string text = null, string tooltipText = null, float tooltipWidth = 50) : base(anchor, size, text, tooltipText, tooltipWidth) {
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);
this.OnOpenedOrClosed += e => this.Priority = this.IsOpen ? 10000 : 0;
this.OnPressed += e => this.IsOpen = !this.IsOpen;
}
///
/// Adds an element to this dropdown's
///
/// The element to add
public void AddElement(Element element) {
this.Panel.AddChild(element);
// Since the dropdown causes elements to be over each other,
// usual gamepad code doesn't apply
element.GetGamepadNextElement = (dir, usualNext) => {
if (dir == Direction2.Up) {
var prev = element.GetOlderSibling();
return prev ?? this;
} else if (dir == Direction2.Down) {
return element.GetSiblings(e => e.GetOlderSibling() == element).FirstOrDefault();
}
return usualNext;
};
}
///
/// Adds a pressable element to this dropdown's
///
/// The text to display
/// The resulting paragraph's event
public void AddElement(string text, GenericCallback pressed = null) {
this.AddElement(p => text, pressed);
}
///
/// Adds a pressable element to this dropdown's .
/// By default, the paragraph's text color will change from to when hovering over it.
///
/// The text to display
/// The resulting paragraph's event
public void AddElement(Paragraph.TextCallback text, GenericCallback pressed = null) {
var paragraph = new Paragraph(Anchor.AutoLeft, 1, text) {
CanBeMoused = true,
CanBeSelected = true,
PositionOffset = new Vector2(0, 1)
};
if (pressed != null)
paragraph.OnPressed += pressed;
paragraph.OnMouseEnter += e => paragraph.TextColor = Color.LightGray;
paragraph.OnMouseExit += e => paragraph.TextColor = Color.White;
this.AddElement(paragraph);
}
}
}