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

made scrolling a lot less intensive

This commit is contained in:
Ellpeck 2019-08-24 22:27:47 +02:00
parent cc66c453c5
commit 6766d769f4
2 changed files with 19 additions and 16 deletions

View file

@ -66,7 +66,7 @@ namespace MLEM.Ui.Elements {
} }
public Rectangle ChildPaddedArea { public Rectangle ChildPaddedArea {
get { get {
var padded = this.Area; var padded = this.UnscrolledArea;
padded.Location += this.ScaledChildPadding; padded.Location += this.ScaledChildPadding;
padded.Width -= this.ScaledChildPadding.X * 2; padded.Width -= this.ScaledChildPadding.X * 2;
padded.Height -= this.ScaledChildPadding.Y * 2; padded.Height -= this.ScaledChildPadding.Y * 2;
@ -120,12 +120,15 @@ namespace MLEM.Ui.Elements {
public bool CanAutoAnchorsAttach = true; public bool CanAutoAnchorsAttach = true;
private Rectangle area; private Rectangle area;
public Rectangle Area { public Rectangle UnscrolledArea {
get { get {
this.UpdateAreaIfDirty(); this.UpdateAreaIfDirty();
return this.area; return this.area;
} }
} }
public Rectangle Area => this.UnscrolledArea.OffsetCopy(this.ScaledScrollOffset);
public Point ScrollOffset;
public Point ScaledScrollOffset => this.ScrollOffset.Multiply(this.Scale);
public Rectangle DisplayArea { public Rectangle DisplayArea {
get { get {
var padded = this.Area; var padded = this.Area;
@ -302,7 +305,7 @@ namespace MLEM.Ui.Elements {
if (this.SetHeightBasedOnChildren && this.Children.Count > 0) { if (this.SetHeightBasedOnChildren && this.Children.Count > 0) {
var lowest = this.GetLowestChild(false, true); var lowest = this.GetLowestChild(false, true);
var newHeight = (lowest.area.Bottom - pos.Y + this.ScaledChildPadding.Y) / this.Scale; var newHeight = (lowest.UnscrolledArea.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;
if (this.Anchor > Anchor.TopRight) if (this.Anchor > Anchor.TopRight)
@ -318,7 +321,7 @@ namespace MLEM.Ui.Elements {
} }
protected virtual Rectangle GetAreaForAutoAnchors() { protected virtual Rectangle GetAreaForAutoAnchors() {
return this.Area; return this.UnscrolledArea;
} }
public Element GetLowestChild(bool hiddenAlso, bool unattachableAlso) { public Element GetLowestChild(bool hiddenAlso, bool unattachableAlso) {
@ -330,7 +333,7 @@ namespace MLEM.Ui.Elements {
continue; continue;
if (child.Anchor > Anchor.TopRight && child.Anchor < Anchor.AutoLeft) if (child.Anchor > Anchor.TopRight && child.Anchor < Anchor.AutoLeft)
continue; continue;
if (lowest == null || child.Area.Bottom > lowest.Area.Bottom) if (lowest == null || child.UnscrolledArea.Bottom > lowest.UnscrolledArea.Bottom)
lowest = child; lowest = child;
} }
return lowest; return lowest;
@ -345,7 +348,7 @@ namespace MLEM.Ui.Elements {
break; break;
if (!hiddenAlso && child.IsHidden || !unattachableAlso && !child.CanAutoAnchorsAttach) if (!hiddenAlso && child.IsHidden || !unattachableAlso && !child.CanAutoAnchorsAttach)
continue; continue;
if (lowest == null || child.Area.Bottom >= lowest.Area.Bottom) if (lowest == null || child.UnscrolledArea.Bottom >= lowest.UnscrolledArea.Bottom)
lowest = child; lowest = child;
} }
return lowest; return lowest;

View file

@ -24,16 +24,7 @@ namespace MLEM.Ui.Elements {
var scrollSize = scrollerSize ?? Point.Zero; var scrollSize = scrollerSize ?? Point.Zero;
this.ScrollBar = new ScrollBar(Anchor.TopRight, new Vector2(scrollSize.X, 1), scrollSize.Y, 0) { this.ScrollBar = new ScrollBar(Anchor.TopRight, new Vector2(scrollSize.X, 1), scrollSize.Y, 0) {
StepPerScroll = 10, StepPerScroll = 10,
OnValueChanged = (element, value) => { OnValueChanged = (element, value) => this.ForceChildrenScroll(),
// 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
var firstChild = this.Children[1];
// as all children have to be auto-aligned, moving the first one up will move all others
firstChild.PositionOffset = new Vector2(firstChild.PositionOffset.X, -value.Floor());
this.ForceUpdateArea();
},
CanAutoAnchorsAttach = false CanAutoAnchorsAttach = false
}; };
this.AddChild(this.ScrollBar); this.AddChild(this.ScrollBar);
@ -58,6 +49,7 @@ namespace MLEM.Ui.Elements {
} }
base.ForceUpdateArea(); base.ForceUpdateArea();
this.ForceChildrenScroll();
if (this.scrollOverflow) { if (this.scrollOverflow) {
// if there is only one child, then we have just the scroll bar // if there is only one child, then we have just the scroll bar
@ -80,6 +72,14 @@ namespace MLEM.Ui.Elements {
} }
} }
private void ForceChildrenScroll() {
if (!this.scrollOverflow)
return;
var offset = -this.ScrollBar.CurrentValue.Floor();
foreach (var child in this.GetChildren(c => c != this.ScrollBar, true))
child.ScrollOffset = new Point(0, offset);
}
public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) { public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) {
batch.Draw(this.Texture, this.DisplayArea.OffsetCopy(offset), Color.White * alpha, this.Scale); batch.Draw(this.Texture, this.DisplayArea.OffsetCopy(offset), Color.White * alpha, this.Scale);
// if we handle overflow, draw using the render target in DrawUnbound // if we handle overflow, draw using the render target in DrawUnbound