mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
made auto-anchored elements look for the lowest older sibling
This commit is contained in:
parent
21c17066fc
commit
d231386f86
4 changed files with 17 additions and 16 deletions
|
@ -160,7 +160,7 @@ namespace Demos {
|
||||||
root.AddChild(ElementHelper.NumberField(Anchor.AutoLeft, new Vector2(1, 10))).PositionOffset = new Vector2(0, 1);
|
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)});
|
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[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[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"));
|
cols[2].AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is the third column"));
|
||||||
|
|
|
@ -263,7 +263,7 @@ namespace MLEM.Ui.Elements {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.Anchor >= Anchor.AutoLeft) {
|
if (this.Anchor >= Anchor.AutoLeft) {
|
||||||
var previousChild = this.GetPreviousChild(false, false);
|
var previousChild = this.GetLowestOlderSibling(false, false);
|
||||||
if (previousChild != null) {
|
if (previousChild != null) {
|
||||||
var prevArea = previousChild.GetAreaForAutoAnchors();
|
var prevArea = previousChild.GetAreaForAutoAnchors();
|
||||||
switch (this.Anchor) {
|
switch (this.Anchor) {
|
||||||
|
@ -296,7 +296,7 @@ namespace MLEM.Ui.Elements {
|
||||||
child.ForceUpdateArea();
|
child.ForceUpdateArea();
|
||||||
|
|
||||||
if (this.SetHeightBasedOnChildren && this.Children.Count > 0) {
|
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;
|
var newHeight = (lowest.area.Bottom - pos.Y + this.ScaledChildPadding.Y) / this.Scale;
|
||||||
if (newHeight != this.size.Y) {
|
if (newHeight != this.size.Y) {
|
||||||
this.size.Y = newHeight;
|
this.size.Y = newHeight;
|
||||||
|
@ -316,7 +316,7 @@ namespace MLEM.Ui.Elements {
|
||||||
return this.Area;
|
return this.Area;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Element GetLowestReachingChild(bool hiddenAlso, bool unattachableAlso) {
|
public Element GetLowestChild(bool hiddenAlso, bool unattachableAlso) {
|
||||||
Element lowest = null;
|
Element lowest = null;
|
||||||
// the lowest child is expected to be towards the back, so search is usually faster if done backwards
|
// 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--) {
|
for (var i = this.Children.Count - 1; i >= 0; i--) {
|
||||||
|
@ -331,19 +331,22 @@ namespace MLEM.Ui.Elements {
|
||||||
return lowest;
|
return lowest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Element GetPreviousChild(bool hiddenAlso, bool unattachableAlso) {
|
public Element GetLowestOlderSibling(bool hiddenAlso, bool unattachableAlso) {
|
||||||
if (this.Parent == null)
|
if (this.Parent == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
Element lastChild = null;
|
Element lowest = null;
|
||||||
foreach (var child in this.Parent.Children) {
|
foreach (var child in this.Parent.Children) {
|
||||||
if (!hiddenAlso && child.IsHidden || !unattachableAlso && !child.CanAutoAnchorsAttach)
|
|
||||||
continue;
|
|
||||||
if (child == this)
|
if (child == this)
|
||||||
break;
|
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) {
|
public IEnumerable<Element> GetSiblings(bool hiddenAlso) {
|
||||||
|
|
|
@ -18,15 +18,13 @@ namespace MLEM.Ui.Elements {
|
||||||
return box;
|
return box;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Group[] MakeColumns(Element parent, Anchor anchor, Vector2 totalSize, int amount, bool setHeightBasedOnChildren = true) {
|
public static Group[] MakeColumns(Element parent, Vector2 totalSize, int amount, bool setHeightBasedOnChildren = true) {
|
||||||
var group = new Group(anchor, totalSize, setHeightBasedOnChildren);
|
|
||||||
var cols = new Group[amount];
|
var cols = new Group[amount];
|
||||||
for (var i = 0; i < amount; i++) {
|
for (var i = 0; i < amount; i++) {
|
||||||
cols[i] = new Group(Anchor.AutoInline, new Vector2(totalSize.X / amount, totalSize.Y), setHeightBasedOnChildren);
|
cols[i] = new Group(Anchor.AutoInline, new Vector2(totalSize.X / amount, totalSize.Y), setHeightBasedOnChildren);
|
||||||
group.AddChild(cols[i]);
|
|
||||||
}
|
|
||||||
if (parent != null)
|
if (parent != null)
|
||||||
parent.AddChild(group);
|
parent.AddChild(cols[i]);
|
||||||
|
}
|
||||||
return cols;
|
return cols;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,7 @@ namespace MLEM.Ui.Elements {
|
||||||
return;
|
return;
|
||||||
// the "real" first child is the scroll bar, which we want to ignore
|
// the "real" first child is the scroll bar, which we want to ignore
|
||||||
var firstChild = this.Children[1];
|
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
|
// 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;
|
var childrenHeight = lowestChild.Area.Bottom - firstChild.Area.Top;
|
||||||
this.ScrollBar.MaxValue = (childrenHeight - this.Area.Height) / this.Scale + this.ChildPadding.Y * 2;
|
this.ScrollBar.MaxValue = (childrenHeight - this.Area.Height) / this.Scale + this.ChildPadding.Y * 2;
|
||||||
|
|
Loading…
Reference in a new issue