1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-10 16:49:09 +01:00
MLEM/MLEM.Ui/Elements/Dropdown.cs

47 lines
No EOL
1.8 KiB
C#

using Microsoft.Xna.Framework;
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);
}
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;
paragraph.OnMouseEnter += e => paragraph.TextColor = Color.LightGray;
paragraph.OnMouseExit += e => paragraph.TextColor = Color.White;
this.AddElement(paragraph);
}
}
}