1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-24 01:09:23 +01:00

made auto-anchored elements look for the lowest older sibling

This commit is contained in:
Ellpeck 2019-08-24 15:12:11 +02:00
parent 21c17066fc
commit d231386f86
4 changed files with 17 additions and 16 deletions

View file

@ -160,7 +160,7 @@ 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, Anchor.AutoLeft, new Vector2(1), 3);
var cols = ElementHelper.MakeColumns(root, 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"));

View file

@ -263,7 +263,7 @@ namespace MLEM.Ui.Elements {
}
if (this.Anchor >= Anchor.AutoLeft) {
var previousChild = this.GetPreviousChild(false, false);
var previousChild = this.GetLowestOlderSibling(false, false);
if (previousChild != null) {
var prevArea = previousChild.GetAreaForAutoAnchors();
switch (this.Anchor) {
@ -296,7 +296,7 @@ namespace MLEM.Ui.Elements {
child.ForceUpdateArea();
if (this.SetHeightBasedOnChildren && this.Children.Count > 0) {
var lowest = this.GetLowestReachingChild(false, true);
var lowest = this.GetLowestChild(false, true);
var newHeight = (lowest.area.Bottom - pos.Y + this.ScaledChildPadding.Y) / this.Scale;
if (newHeight != this.size.Y) {
this.size.Y = newHeight;
@ -316,7 +316,7 @@ namespace MLEM.Ui.Elements {
return this.Area;
}
public Element GetLowestReachingChild(bool hiddenAlso, bool unattachableAlso) {
public Element GetLowestChild(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--) {
@ -331,19 +331,22 @@ namespace MLEM.Ui.Elements {
return lowest;
}
public Element GetPreviousChild(bool hiddenAlso, bool unattachableAlso) {
public Element GetLowestOlderSibling(bool hiddenAlso, bool unattachableAlso) {
if (this.Parent == null)
return null;
Element lastChild = null;
Element lowest = null;
foreach (var child in this.Parent.Children) {
if (!hiddenAlso && child.IsHidden || !unattachableAlso && !child.CanAutoAnchorsAttach)
continue;
if (child == this)
break;
lastChild = child;
if (!hiddenAlso && child.IsHidden || !unattachableAlso && !child.CanAutoAnchorsAttach)
continue;
if (child.Anchor > Anchor.TopRight && child.Anchor < Anchor.AutoLeft)
continue;
if (lowest == null || child.Area.Bottom > lowest.Area.Bottom)
lowest = child;
}
return lastChild;
return lowest;
}
public IEnumerable<Element> GetSiblings(bool hiddenAlso) {

View file

@ -18,15 +18,13 @@ namespace MLEM.Ui.Elements {
return box;
}
public static Group[] MakeColumns(Element parent, Anchor anchor, Vector2 totalSize, int amount, bool setHeightBasedOnChildren = true) {
var group = new Group(anchor, totalSize, setHeightBasedOnChildren);
public static Group[] MakeColumns(Element parent, Vector2 totalSize, int amount, bool setHeightBasedOnChildren = true) {
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);
group.AddChild(cols[i]);
if (parent != null)
parent.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(false, true);
var lowestChild = this.GetLowestChild(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;