From 1211cda508054020de03c711563c00123e5edd84 Mon Sep 17 00:00:00 2001 From: Recrown Date: Fri, 26 Apr 2019 00:09:31 -0500 Subject: [PATCH] Hand scrolling moves scroll bars appropriately. --- .../UI/Modular/Modules/UIScrollable.cs | 82 +++++++++++++++++-- TestGame/TestGame.cs | 5 +- 2 files changed, 80 insertions(+), 7 deletions(-) diff --git a/RecrownedAthenaeum/UI/Modular/Modules/UIScrollable.cs b/RecrownedAthenaeum/UI/Modular/Modules/UIScrollable.cs index c283b20..9fef5ce 100644 --- a/RecrownedAthenaeum/UI/Modular/Modules/UIScrollable.cs +++ b/RecrownedAthenaeum/UI/Modular/Modules/UIScrollable.cs @@ -20,10 +20,13 @@ namespace RecrownedAthenaeum.UI.Modular.Modules Color scrollBarColor; float opacityOfBar = 1; bool showingBars; - int shiftX, shiftY; + private bool mouseWasPressed; + private bool horizontalSelected; + private Vector2 mouseRelativePosition; + float shiftX, shiftY; int furthestX; int furthestY; - UIModule furthestXMod, furthestYMod; + UIModule furthestXModule, furthestYMod; bool horScrollAvailable, vertScrollAvailable; @@ -154,7 +157,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules { if (hideScrollBars) { - if (!showingBars) + if (!showingBars && !mouseWasPressed) { if (opacityOfBar > 0f) { @@ -165,6 +168,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules else { opacityOfBar = 1f; + scrollBarColor = color * opacityOfBar; } } @@ -209,7 +213,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules int mFurthestY = m.Boundaries.Y + m.Boundaries.Height; if (mFurthestX > furthestX) { - furthestXMod = m; + furthestXModule = m; furthestX = mFurthestX; } if (mFurthestY > furthestY) @@ -227,7 +231,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules { group.RemoveModule(module); - if (module == furthestXMod) + if (module == furthestXModule) { furthestX = 0; UIModule[] modules = group.GetCopyOfModules(); @@ -237,7 +241,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules int mFurthestX = m.Boundaries.X + m.Boundaries.Width; if (mFurthestX > furthestX) { - furthestXMod = m; + furthestXModule = m; furthestX = mFurthestX; } } @@ -331,6 +335,17 @@ namespace RecrownedAthenaeum.UI.Modular.Modules } } + + public override bool KeyboardStateChanged(KeyboardState state) + { + if (state.IsKeyDown(Keys.Right)) + { + shiftX -= 20; + } + + return base.KeyboardStateChanged(state); + } + public override bool MouseStateChanged(MouseState state) { if (InputUtilities.MouseWithinBoundries(Boundaries)) @@ -341,7 +356,62 @@ namespace RecrownedAthenaeum.UI.Modular.Modules { showingBars = false; } + + if (InputUtilities.MouseWithinBoundries(horizontalScrollBarBounds)) + { + if (state.LeftButton == ButtonState.Pressed) + { + mouseWasPressed = true; + horizontalSelected = true; + } + if (!mouseWasPressed) + { + mouseRelativePosition.X = state.X - horizontalScrollBarBounds.X; + } + } + + if (InputUtilities.MouseWithinBoundries(verticalScrollBarBounds)) + { + if (state.LeftButton == ButtonState.Pressed) + { + mouseWasPressed = true; + horizontalSelected = false; + } + if (!mouseWasPressed) + { + mouseRelativePosition.Y = state.Y - verticalScrollBarBounds.Y; + } + } + + if (mouseWasPressed) + { + if (horizontalSelected) + { + float latestPosition = state.X - mouseRelativePosition.X; + shiftX = -(latestPosition / (Width - horizontalScrollBarBounds.Width)) * furthestX; + } + else + { + float latestPosition = state.Y - mouseRelativePosition.Y; + shiftY = -(latestPosition / (Height - verticalScrollBarBounds.Height)) * furthestY; + } + + if (state.LeftButton == ButtonState.Released) + { + mouseWasPressed = false; + } + } 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)); + } } } diff --git a/TestGame/TestGame.cs b/TestGame/TestGame.cs index 55c8892..7b76ad3 100644 --- a/TestGame/TestGame.cs +++ b/TestGame/TestGame.cs @@ -1,6 +1,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; +using RecrownedAthenaeum.Input; using RecrownedAthenaeum.Render; using RecrownedAthenaeum.ScreenSystem; using RecrownedAthenaeum.SpecialTypes; @@ -23,7 +24,7 @@ namespace TestGame Texture2D logo; Image logoImage; Skin skin; - + public TestGame() { graphics = new GraphicsDeviceManager(this); @@ -67,6 +68,7 @@ namespace TestGame logoImage = new Image(logo); uIScrollable.AddModules(logoImage); + InputUtilities.InputListeners.Add(uIScrollable); // TODO: use this.Content to load your game content here } @@ -86,6 +88,7 @@ namespace TestGame /// Provides a snapshot of timing values. protected override void Update(GameTime gameTime) { + InputUtilities.Update(); if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); uIScrollable.Update(gameTime);