1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

Fixed the scroll bar of an empty panel being positioned incorrectly

This commit is contained in:
Ell 2022-09-24 11:04:23 +02:00
parent 74b3c426b8
commit d6a51776e5
2 changed files with 21 additions and 17 deletions

View file

@ -46,6 +46,7 @@ Fixes
- Fixed UiMarkdownParser not parsing formatting in headings and blockquotes - Fixed UiMarkdownParser not parsing formatting in headings and blockquotes
- Fixed Element.OnChildAdded and Element.OnChildRemoved being called for grandchildren when a child is added - Fixed Element.OnChildAdded and Element.OnChildRemoved being called for grandchildren when a child is added
- Fixed an exception when trying to force-update the area of an element without a ui system - Fixed an exception when trying to force-update the area of an element without a ui system
- Fixed the scroll bar of an empty panel being positioned incorrectly
### MLEM.Data ### MLEM.Data
Additions Additions

View file

@ -84,7 +84,8 @@ namespace MLEM.Ui.Elements {
return; return;
if (e == null || !e.GetParentTree().Contains(this)) if (e == null || !e.GetParentTree().Contains(this))
return; return;
var firstChild = this.Children.First(c => c != this.ScrollBar); var firstChild = this.Children.FirstOrDefault(c => c != this.ScrollBar);
if (firstChild != null)
this.ScrollBar.CurrentValue = (e.Area.Center.Y - this.Area.Height / 2 - firstChild.Area.Top) / e.Scale + this.ChildPadding.Value.Height / 2; this.ScrollBar.CurrentValue = (e.Area.Center.Y - this.Area.Height / 2 - firstChild.Area.Top) / e.Scale + this.ChildPadding.Value.Height / 2;
}; };
this.AddChild(this.ScrollBar); this.AddChild(this.ScrollBar);
@ -231,20 +232,23 @@ namespace MLEM.Ui.Elements {
protected virtual void ScrollSetup() { protected virtual void ScrollSetup() {
if (!this.scrollOverflow || this.IsHidden) if (!this.scrollOverflow || this.IsHidden)
return; return;
// if there is only one child, then we have just the scroll bar
if (this.Children.Count == 1)
return;
// the "real" first child is the scroll bar, which we want to ignore float childrenHeight;
var firstChild = this.Children.First(c => c != this.ScrollBar); if (this.Children.Count > 1) {
var firstChild = this.Children.FirstOrDefault(c => c != this.ScrollBar);
var lowestChild = this.GetLowestChild(c => c != this.ScrollBar && !c.IsHidden); var lowestChild = this.GetLowestChild(c => c != this.ScrollBar && !c.IsHidden);
var childrenHeight = lowestChild.Area.Bottom - firstChild.Area.Top; childrenHeight = lowestChild.Area.Bottom - firstChild.Area.Top;
} else {
// if we only have one child (the scroll bar), then the children take up no visual height
childrenHeight = 0;
}
// the max value of the scroll bar is the amount of non-scaled pixels taken up by overflowing components // the max value of the scroll bar is the amount of non-scaled pixels taken up by overflowing components
var scrollBarMax = (childrenHeight - this.ChildPaddedArea.Height) / this.Scale; var scrollBarMax = (childrenHeight - this.ChildPaddedArea.Height) / this.Scale;
if (!this.ScrollBar.MaxValue.Equals(scrollBarMax, Element.Epsilon)) { if (!this.ScrollBar.MaxValue.Equals(scrollBarMax, Element.Epsilon)) {
this.ScrollBar.MaxValue = scrollBarMax; this.ScrollBar.MaxValue = scrollBarMax;
this.relevantChildrenDirty = true; this.relevantChildrenDirty = true;
}
// update child padding based on whether the scroll bar is visible // update child padding based on whether the scroll bar is visible
var childOffset = this.ScrollBar.IsHidden ? 0 : this.ScrollerSize.Value.X + this.ScrollBarOffset; var childOffset = this.ScrollBar.IsHidden ? 0 : this.ScrollerSize.Value.X + this.ScrollBarOffset;
@ -253,7 +257,6 @@ namespace MLEM.Ui.Elements {
this.scrollBarChildOffset = childOffset; this.scrollBarChildOffset = childOffset;
this.SetAreaDirty(); this.SetAreaDirty();
} }
}
// the scroller height has the same relation to the scroll bar height as the visible area has to the total height of the panel's content // the scroller height has the same relation to the scroll bar height as the visible area has to the total height of the panel's content
var scrollerHeight = Math.Min(this.ChildPaddedArea.Height / childrenHeight / this.Scale, 1) * this.ScrollBar.Area.Height; var scrollerHeight = Math.Min(this.ChildPaddedArea.Height / childrenHeight / this.Scale, 1) * this.ScrollBar.Area.Height;