2019-09-12 12:39:18 +02:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
|
|
|
namespace MLEM.Ui.Elements {
|
|
|
|
public class Dropdown : Button {
|
|
|
|
|
|
|
|
public readonly Panel Panel;
|
|
|
|
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
|
|
|
}
|
2019-11-18 22:36:55 +01:00
|
|
|
public GenericCallback OnOpenedOrClosed;
|
2019-09-12 12:39:18 +02:00
|
|
|
|
|
|
|
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);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-11-05 13:28:41 +01:00
|
|
|
paragraph.OnMouseEnter += e => paragraph.TextColor = Color.LightGray;
|
|
|
|
paragraph.OnMouseExit += e => paragraph.TextColor = Color.White;
|
2019-09-12 12:39:18 +02:00
|
|
|
this.AddElement(paragraph);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|