mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-12-24 01:09:23 +01:00
fixed some bugs and added column helper
This commit is contained in:
parent
2698153491
commit
d69033a315
4 changed files with 35 additions and 9 deletions
|
@ -159,6 +159,12 @@ namespace Demos {
|
|||
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "There are also some additional \"components\" which are created as combinations of other components. You can find all of them in the ElementHelper class. Here are some examples:"));
|
||||
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);
|
||||
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"));
|
||||
|
||||
// Below are some querying examples that help you find certain elements easily
|
||||
|
||||
var children = root.GetChildren();
|
||||
|
|
|
@ -296,13 +296,8 @@ namespace MLEM.Ui.Elements {
|
|||
child.ForceUpdateArea();
|
||||
|
||||
if (this.SetHeightBasedOnChildren && this.Children.Count > 0) {
|
||||
var height = 0;
|
||||
foreach (var child in this.Children) {
|
||||
if (!child.isHidden && (child.Anchor <= Anchor.TopRight || child.Anchor >= Anchor.AutoLeft) && child.area.Bottom > height)
|
||||
height = child.area.Bottom;
|
||||
}
|
||||
|
||||
var newHeight = (height - pos.Y + this.ScaledChildPadding.Y) / this.Scale;
|
||||
var lowest = this.GetLowestReachingChild();
|
||||
var newHeight = (lowest.area.Bottom - pos.Y + this.ScaledChildPadding.Y) / this.Scale;
|
||||
if (newHeight != this.size.Y) {
|
||||
this.size.Y = newHeight;
|
||||
if (this.Anchor > Anchor.TopRight)
|
||||
|
@ -321,6 +316,21 @@ namespace MLEM.Ui.Elements {
|
|||
return this.Area;
|
||||
}
|
||||
|
||||
public Element GetLowestReachingChild() {
|
||||
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)
|
||||
continue;
|
||||
if (child.Anchor > Anchor.TopRight && child.Anchor < Anchor.AutoLeft)
|
||||
continue;
|
||||
if (lowest == null || child.Area.Bottom > lowest.Area.Bottom)
|
||||
lowest = child;
|
||||
}
|
||||
return lowest;
|
||||
}
|
||||
|
||||
public Element GetPreviousChild(bool hiddenAlso, bool unattachableAlso) {
|
||||
if (this.Parent == null)
|
||||
return null;
|
||||
|
|
|
@ -4,6 +4,16 @@ using MLEM.Input;
|
|||
namespace MLEM.Ui.Elements {
|
||||
public static class ElementHelper {
|
||||
|
||||
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);
|
||||
if (parent != null)
|
||||
parent.AddChild(cols[i]);
|
||||
}
|
||||
return cols;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
|
|
|
@ -65,9 +65,9 @@ namespace MLEM.Ui.Elements {
|
|||
return;
|
||||
// the "real" first child is the scroll bar, which we want to ignore
|
||||
var firstChild = this.Children[1];
|
||||
var lastChild = this.Children[this.Children.Count - 1];
|
||||
var lowestChild = this.GetLowestReachingChild();
|
||||
// the max value of the scrollbar is the amount of non-scaled pixels taken up by overflowing components
|
||||
var childrenHeight = lastChild.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;
|
||||
|
||||
// update the render target
|
||||
|
|
Loading…
Reference in a new issue