1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

added column helper and fixed some more bugs

This commit is contained in:
Ellpeck 2019-08-24 15:00:08 +02:00
parent d69033a315
commit 21c17066fc
5 changed files with 39 additions and 17 deletions

View file

@ -160,11 +160,19 @@ namespace Demos {
root.AddChild(ElementHelper.NumberField(Anchor.AutoLeft, new Vector2(1, 10))).PositionOffset = new Vector2(0, 1);
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "There is an easy helper method to make any amount of same-sized columns:") {PositionOffset = new Vector2(0, 1)});
var cols = ElementHelper.MakeColumns(root, new Vector2(1), 3);
var cols = ElementHelper.MakeColumns(root, Anchor.AutoLeft, new Vector2(1), 3);
cols[0].AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is the first column"));
cols[1].AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is the second column"));
cols[2].AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is the third column"));
root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Show Info Box") {
OnClicked = (element, button) => {
if (button == MouseButton.Left)
ElementHelper.ShowInfoBox(this.UiSystem, Anchor.Center, 100, "This is an easy info box that you can open with just one line of code! It automatically closes when you press the button below as well.");
},
PositionOffset = new Vector2(0, 1)
});
// Below are some querying examples that help you find certain elements easily
var children = root.GetChildren();

View file

@ -296,7 +296,7 @@ namespace MLEM.Ui.Elements {
child.ForceUpdateArea();
if (this.SetHeightBasedOnChildren && this.Children.Count > 0) {
var lowest = this.GetLowestReachingChild();
var lowest = this.GetLowestReachingChild(false, true);
var newHeight = (lowest.area.Bottom - pos.Y + this.ScaledChildPadding.Y) / this.Scale;
if (newHeight != this.size.Y) {
this.size.Y = newHeight;
@ -316,12 +316,12 @@ namespace MLEM.Ui.Elements {
return this.Area;
}
public Element GetLowestReachingChild() {
public Element GetLowestReachingChild(bool hiddenAlso, bool unattachableAlso) {
Element lowest = null;
// the lowest child is expected to be towards the back, so search is usually faster if done backwards
for (var i = this.Children.Count - 1; i >= 0; i--) {
var child = this.Children[i];
if (child.isHidden)
if (!hiddenAlso && child.IsHidden || !unattachableAlso && !child.CanAutoAnchorsAttach)
continue;
if (child.Anchor > Anchor.TopRight && child.Anchor < Anchor.AutoLeft)
continue;
@ -337,9 +337,7 @@ namespace MLEM.Ui.Elements {
Element lastChild = null;
foreach (var child in this.Parent.Children) {
if (!hiddenAlso && child.IsHidden)
continue;
if (!unattachableAlso && !child.CanAutoAnchorsAttach)
if (!hiddenAlso && child.IsHidden || !unattachableAlso && !child.CanAutoAnchorsAttach)
continue;
if (child == this)
break;

View file

@ -4,13 +4,29 @@ using MLEM.Input;
namespace MLEM.Ui.Elements {
public static class ElementHelper {
public static Group[] MakeColumns(Element parent, Vector2 totalSize, int amount, bool setHeightBasedOnChildren = true) {
public static Panel ShowInfoBox(UiSystem system, Anchor anchor, float width, string text, float buttonHeight = 10, string okText = "Okay") {
var box = new Panel(anchor, new Vector2(width, 1), Vector2.Zero, true);
box.AddChild(new Paragraph(Anchor.AutoLeft, 1, text));
box.AddChild(new Button(Anchor.AutoCenter, new Vector2(0.5F, buttonHeight), okText) {
OnClicked = (element, button) => {
if (button == MouseButton.Left)
system.Remove("InfoBox");
},
PositionOffset = new Vector2(0, 1)
});
system.Add("InfoBox", box);
return box;
}
public static Group[] MakeColumns(Element parent, Anchor anchor, Vector2 totalSize, int amount, bool setHeightBasedOnChildren = true) {
var group = new Group(anchor, totalSize, setHeightBasedOnChildren);
var cols = new Group[amount];
for (var i = 0; i < amount; i++) {
cols[i] = new Group(Anchor.AutoInline, new Vector2(totalSize.X / amount, totalSize.Y), setHeightBasedOnChildren);
if (parent != null)
parent.AddChild(cols[i]);
group.AddChild(cols[i]);
}
if (parent != null)
parent.AddChild(group);
return cols;
}

View file

@ -65,7 +65,7 @@ namespace MLEM.Ui.Elements {
return;
// the "real" first child is the scroll bar, which we want to ignore
var firstChild = this.Children[1];
var lowestChild = this.GetLowestReachingChild();
var lowestChild = this.GetLowestReachingChild(false, true);
// the max value of the scrollbar is the amount of non-scaled pixels taken up by overflowing components
var childrenHeight = lowestChild.Area.Bottom - firstChild.Area.Top;
this.ScrollBar.MaxValue = (childrenHeight - this.Area.Height) / this.Scale + this.ChildPadding.Y * 2;

View file

@ -120,18 +120,18 @@ namespace MLEM.Ui {
public RootElement Add(string name, Element element) {
var root = new RootElement(name, element, this);
this.Add(root);
return root;
return !this.Add(root) ? null : root;
}
internal void Add(RootElement root, int index = -1) {
internal bool Add(RootElement root, int index = -1) {
if (this.IndexOf(root.Name) >= 0)
throw new ArgumentException($"There is already a root element with name {root.Name}");
return false;
if (index < 0 || index > this.rootElements.Count)
index = this.rootElements.Count;
this.rootElements.Insert(index, root);
root.Element.PropagateRoot(root);
root.Element.PropagateUiSystem(this);
return true;
}
public void Remove(string name) {
@ -153,8 +153,8 @@ namespace MLEM.Ui {
}
private Element GetMousedElement() {
foreach (var root in this.rootElements) {
var moused = root.Element.GetMousedElement();
for (var i = this.rootElements.Count - 1; i >= 0; i--) {
var moused = this.rootElements[i].Element.GetMousedElement();
if (moused != null)
return moused;
}