1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-29 23:58:34 +01:00
MLEM/MLEM.Ui/Elements/Dropdown.cs

60 lines
2.4 KiB
C#
Raw Normal View History

using System.Linq;
using Microsoft.Xna.Framework;
using MLEM.Misc;
namespace MLEM.Ui.Elements {
public class Dropdown : Button {
public readonly Panel Panel;
public bool IsOpen {
get => !this.Panel.IsHidden;
set {
this.Panel.IsHidden = !value;
this.OnOpenedOrClosed?.Invoke(this);
}
}
public GenericCallback OnOpenedOrClosed;
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;
}
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();
2020-03-16 19:22:08 +01:00
return prev ?? this;
} else if (dir == Direction2.Down) {
2020-03-16 19:22:08 +01:00
return element.GetSiblings(e => e.GetOlderSibling() == element).FirstOrDefault();
}
return usualNext;
};
}
public void AddElement(string text, GenericCallback pressed = null) {
this.AddElement(p => text, pressed);
}
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;
2019-11-05 13:28:41 +01:00
paragraph.OnMouseEnter += e => paragraph.TextColor = Color.LightGray;
paragraph.OnMouseExit += e => paragraph.TextColor = Color.White;
this.AddElement(paragraph);
}
}
}