From 72188194e5b0bec9549f54f400652e8fe91aad15 Mon Sep 17 00:00:00 2001 From: Recrown Date: Sat, 27 Apr 2019 23:22:33 -0500 Subject: [PATCH] Scrolling now moves children. --- .../UI/Modular/Modules/UIScrollable.cs | 115 +++++++++++------- 1 file changed, 69 insertions(+), 46 deletions(-) diff --git a/RecrownedAthenaeum/UI/Modular/Modules/UIScrollable.cs b/RecrownedAthenaeum/UI/Modular/Modules/UIScrollable.cs index 9fef5ce..417182e 100644 --- a/RecrownedAthenaeum/UI/Modular/Modules/UIScrollable.cs +++ b/RecrownedAthenaeum/UI/Modular/Modules/UIScrollable.cs @@ -15,6 +15,8 @@ namespace RecrownedAthenaeum.UI.Modular.Modules { public class UIScrollable : UIModule { + BasicScissor basicScissor; + Rectangle viewport; UIModuleGroup group; Color scrollBarColor; @@ -24,8 +26,34 @@ namespace RecrownedAthenaeum.UI.Modular.Modules private bool horizontalSelected; private Vector2 mouseRelativePosition; float shiftX, shiftY; - int furthestX; - int furthestY; + + public float XScrollPosition + { + get + { + return -shiftX; + } + + set + { + shiftX = -value; + WidthOrXChange(true); + } + } + public float YScrollPosition + { + get + { + return shiftY; + } + + set + { + shiftY = value; + HeightOrYChange(true); + } + } + UIModule furthestXModule, furthestYMod; bool horScrollAvailable, vertScrollAvailable; @@ -139,7 +167,8 @@ namespace RecrownedAthenaeum.UI.Modular.Modules this.verticalBarTrack = verticalBarTrack; this.background = background; - group = new UIModuleGroup(new BasicScissor()); + basicScissor = new BasicScissor(); + group = new UIModuleGroup(); HorizontalBarThickness = 12; VerticalBarThickness = 12; } @@ -174,23 +203,26 @@ namespace RecrownedAthenaeum.UI.Modular.Modules if (horScrollAvailable) { - horizontalScrollBarBounds.X = (int)(((float)-shiftX / furthestX) * (group.Width - horizontalScrollBarBounds.Width)); + horizontalScrollBarBounds.X = (int)(((float)-shiftX / group.Width) * (viewport.Width - horizontalScrollBarBounds.Width)); } if (vertScrollAvailable) { - verticalScrollBarBounds.Y = (int)(((float)-shiftY / furthestY) * (group.Height - verticalScrollBarBounds.Height)); + verticalScrollBarBounds.Y = (int)(((float)-shiftY / group.Height) * (viewport.Height - verticalScrollBarBounds.Height)); } + base.Update(gameTime); } public override void Draw(ConsistentSpriteBatch spriteBatch) { background?.Draw(spriteBatch, Boundaries, color, origin: origin); - + spriteBatch.End(); + basicScissor.Begin(viewport, spriteBatch); group.Draw(spriteBatch); - + basicScissor.End(); + spriteBatch.Begin(); if (horScrollAvailable) { horizontalScrollBar.Draw(spriteBatch, horizontalScrollBarBounds, scrollBarColor); @@ -211,15 +243,15 @@ namespace RecrownedAthenaeum.UI.Modular.Modules UIModule m = addModules[i]; int mFurthestX = m.Boundaries.X + m.Boundaries.Width; int mFurthestY = m.Boundaries.Y + m.Boundaries.Height; - if (mFurthestX > furthestX) + if (mFurthestX > group.Width) { furthestXModule = m; - furthestX = mFurthestX; + group.Width = mFurthestX; } - if (mFurthestY > furthestY) + if (mFurthestY > group.Height) { furthestYMod = m; - furthestY = mFurthestY; + group.Height = mFurthestY; } } @@ -233,33 +265,33 @@ namespace RecrownedAthenaeum.UI.Modular.Modules if (module == furthestXModule) { - furthestX = 0; + group.Width = 0; UIModule[] modules = group.GetCopyOfModules(); for (int i = 0; i < modules.Length; i++) { UIModule m = modules[i]; int mFurthestX = m.Boundaries.X + m.Boundaries.Width; - if (mFurthestX > furthestX) + if (mFurthestX > group.Width) { furthestXModule = m; - furthestX = mFurthestX; + group.Width = mFurthestX; } } } if (module == furthestYMod) { - furthestY = 0; + group.Height = 0; UIModule[] modules = group.GetCopyOfModules(); for (int i = 0; i < modules.Length; i++) { UIModule m = modules[i]; int mFurthestY = m.Boundaries.Y + m.Boundaries.Height; - if (mFurthestY > furthestY) + if (mFurthestY > group.Height) { furthestYMod = m; - furthestY = mFurthestY; + group.Height = mFurthestY; } } } @@ -267,14 +299,15 @@ namespace RecrownedAthenaeum.UI.Modular.Modules private void WidthOrXChange(bool onlyXChange = false) { - group.X = X + leftPadding; + group.X = X + leftPadding + (int)shiftX; if (!onlyXChange) { - group.Width = Width - rightPadding - leftPadding; - if (Width < furthestX) + viewport.X = X + leftPadding; + viewport.Width = Width - rightPadding - leftPadding; + if (Width < group.Width) { horScrollAvailable = true; - horizontalScrollBarBounds.Width = (int)(Width * ((float)Width / furthestX)); + horizontalScrollBarBounds.Width = (int)(Width * ((float)Width / group.Width)); horizontalScrollBarBounds.Width = Math.Max(horizontalScrollBarBounds.Width, minimumBarLength); } else { horScrollAvailable = false; } @@ -282,17 +315,17 @@ namespace RecrownedAthenaeum.UI.Modular.Modules verticalScrollBarBounds.X = X; if (!hideScrollBars) { - group.Width -= VerticalBarThickness; + viewport.Width -= VerticalBarThickness; if (!verticalBarLeftPosition) { - verticalScrollBarBounds.X += group.Width; + verticalScrollBarBounds.X += viewport.Width; } } else { if (!verticalBarLeftPosition) { - verticalScrollBarBounds.X += group.Width - verticalScrollBarBounds.Width; + verticalScrollBarBounds.X += viewport.Width - verticalScrollBarBounds.Width; } } } @@ -300,14 +333,15 @@ namespace RecrownedAthenaeum.UI.Modular.Modules private void HeightOrYChange(bool onlyYChange = false) { - group.Y = Y + bottomPadding; + group.Y = Y + bottomPadding + (int)shiftY; if (!onlyYChange) { - group.Height = Height - bottomPadding - topPadding; - if (Height < furthestY) + viewport.Y = Y + bottomPadding; + viewport.Height = Height - bottomPadding - topPadding; + if (Height < group.Height) { vertScrollAvailable = true; - verticalScrollBarBounds.Height = (int)(Height * ((float)Height / furthestY)); + verticalScrollBarBounds.Height = (int)(Height * ((float)Height / group.Height)); verticalScrollBarBounds.Height = Math.Max(verticalScrollBarBounds.Height, minimumBarLength); } else { vertScrollAvailable = false; } @@ -315,32 +349,31 @@ namespace RecrownedAthenaeum.UI.Modular.Modules horizontalScrollBarBounds.Y = Y; if (!hideScrollBars) { - group.Height -= HorizontalBarThickness; + viewport.Height -= HorizontalBarThickness; if (!horizontalBarTopPosition) { - horizontalScrollBarBounds.Y += group.Height; + horizontalScrollBarBounds.Y += viewport.Height; } else { - group.Y += horizontalScrollBarBounds.Height; + viewport.Y += horizontalScrollBarBounds.Height; } } else { if (!horizontalBarTopPosition) { - horizontalScrollBarBounds.Y += group.Height - horizontalScrollBarBounds.Height; + horizontalScrollBarBounds.Y += viewport.Height - horizontalScrollBarBounds.Height; } } } } - public override bool KeyboardStateChanged(KeyboardState state) { if (state.IsKeyDown(Keys.Right)) { - shiftX -= 20; + XScrollPosition += 20; } return base.KeyboardStateChanged(state); @@ -388,12 +421,12 @@ namespace RecrownedAthenaeum.UI.Modular.Modules if (horizontalSelected) { float latestPosition = state.X - mouseRelativePosition.X; - shiftX = -(latestPosition / (Width - horizontalScrollBarBounds.Width)) * furthestX; + XScrollPosition = (latestPosition / (Width - horizontalScrollBarBounds.Width)) * group.Width; } else { float latestPosition = state.Y - mouseRelativePosition.Y; - shiftY = -(latestPosition / (Height - verticalScrollBarBounds.Height)) * furthestY; + YScrollPosition = -(latestPosition / (Height - verticalScrollBarBounds.Height)) * group.Height; } if (state.LeftButton == ButtonState.Released) @@ -403,15 +436,5 @@ namespace RecrownedAthenaeum.UI.Modular.Modules } return base.MouseStateChanged(state); } - - private void MoveHorizontalScrollBar(int position) - { - shiftX = -(int)(furthestX * ((float)position / Width)); - } - - private void MoveVerticalScrollBar(int position) - { - shiftY = (int)(furthestY * ((float)position / Height)); - } } }