2019-08-24 12:40:20 +02:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using MLEM.Input;
|
2019-08-27 21:44:02 +02:00
|
|
|
using MLEM.Textures;
|
2019-08-24 12:40:20 +02:00
|
|
|
|
|
|
|
namespace MLEM.Ui.Elements {
|
|
|
|
public static class ElementHelper {
|
|
|
|
|
2019-09-26 22:16:21 +02:00
|
|
|
public static Button ImageButton(Anchor anchor, Vector2 size, TextureRegion texture, string text = null, string tooltipText = null, float tooltipWidth = 50, float imagePadding = 2) {
|
2019-08-27 21:44:02 +02:00
|
|
|
var button = new Button(anchor, size, text, tooltipText, tooltipWidth);
|
2019-09-26 22:16:21 +02:00
|
|
|
var image = new Image(Anchor.CenterLeft, Vector2.One, texture) {Padding = new Vector2(imagePadding)};
|
2019-08-27 21:44:02 +02:00
|
|
|
button.OnAreaUpdated += e => image.Size = new Vector2(e.Area.Height, e.Area.Height) / e.Scale;
|
|
|
|
button.AddChild(image, 0);
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
2019-08-24 15:00:08 +02:00
|
|
|
public static Panel ShowInfoBox(UiSystem system, Anchor anchor, float width, string text, float buttonHeight = 10, string okText = "Okay") {
|
2019-12-25 19:16:07 +01:00
|
|
|
var group = new Group(Anchor.TopLeft, Vector2.One, false);
|
|
|
|
var box = group.AddChild(new Panel(anchor, new Vector2(width, 1), Vector2.Zero, true));
|
2019-08-24 15:00:08 +02:00
|
|
|
box.AddChild(new Paragraph(Anchor.AutoLeft, 1, text));
|
2019-09-09 17:12:36 +02:00
|
|
|
var button = box.AddChild(new Button(Anchor.AutoCenter, new Vector2(0.5F, buttonHeight), okText) {
|
2019-08-25 21:49:27 +02:00
|
|
|
OnPressed = element => system.Remove("InfoBox"),
|
2019-08-24 15:00:08 +02:00
|
|
|
PositionOffset = new Vector2(0, 1)
|
|
|
|
});
|
2019-12-25 19:16:07 +01:00
|
|
|
var root = system.Add("InfoBox", group);
|
2019-09-09 17:12:36 +02:00
|
|
|
root.SelectElement(button);
|
2019-08-24 15:00:08 +02:00
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
2019-08-24 15:12:11 +02:00
|
|
|
public static Group[] MakeColumns(Element parent, Vector2 totalSize, int amount, bool setHeightBasedOnChildren = true) {
|
2019-08-24 14:34:08 +02:00
|
|
|
var cols = new Group[amount];
|
|
|
|
for (var i = 0; i < amount; i++) {
|
2019-09-11 15:03:10 +02:00
|
|
|
var anchor = i == amount - 1 ? Anchor.AutoInlineIgnoreOverflow : Anchor.AutoInline;
|
|
|
|
cols[i] = new Group(anchor, new Vector2(totalSize.X / amount, totalSize.Y), setHeightBasedOnChildren);
|
2019-08-24 15:12:11 +02:00
|
|
|
if (parent != null)
|
|
|
|
parent.AddChild(cols[i]);
|
2019-08-24 14:34:08 +02:00
|
|
|
}
|
|
|
|
return cols;
|
|
|
|
}
|
|
|
|
|
2019-08-24 12:40:20 +02:00
|
|
|
public static Group NumberField(Anchor anchor, Vector2 size, int defaultValue = 0, int stepPerClick = 1, TextField.Rule rule = null, TextField.TextChanged onTextChange = null) {
|
|
|
|
var group = new Group(anchor, size, false);
|
|
|
|
|
|
|
|
var field = new TextField(Anchor.TopLeft, Vector2.One, rule ?? TextField.OnlyNumbers);
|
|
|
|
field.OnTextChange = onTextChange;
|
2019-09-05 12:51:40 +02:00
|
|
|
field.SetText(defaultValue.ToString());
|
2019-08-24 12:40:20 +02:00
|
|
|
group.AddChild(field);
|
|
|
|
group.OnAreaUpdated += e => field.Size = new Vector2((e.Area.Width - e.Area.Height / 2) / e.Scale, 1);
|
|
|
|
|
|
|
|
var upButton = new Button(Anchor.TopRight, Vector2.One, "+") {
|
2019-08-25 21:49:27 +02:00
|
|
|
OnPressed = element => {
|
|
|
|
var text = field.Text.ToString();
|
|
|
|
if (int.TryParse(text, out var val))
|
|
|
|
field.SetText(val + stepPerClick);
|
2019-08-24 12:40:20 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
group.AddChild(upButton);
|
|
|
|
group.OnAreaUpdated += e => upButton.Size = new Vector2(e.Area.Height / 2 / e.Scale);
|
|
|
|
|
|
|
|
var downButton = new Button(Anchor.BottomRight, Vector2.One, "-") {
|
2019-08-25 21:49:27 +02:00
|
|
|
OnPressed = element => {
|
|
|
|
var text = field.Text.ToString();
|
|
|
|
if (int.TryParse(text, out var val))
|
|
|
|
field.SetText(val - stepPerClick);
|
2019-08-24 12:40:20 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
group.AddChild(downButton);
|
|
|
|
group.OnAreaUpdated += e => downButton.Size = new Vector2(e.Area.Height / 2 / e.Scale);
|
|
|
|
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|