Hand scrolling moves scroll bars appropriately.

This commit is contained in:
Harrison Deng 2019-04-26 00:09:31 -05:00
parent 53000e6a6c
commit 1211cda508
2 changed files with 80 additions and 7 deletions

View File

@ -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));
}
}
}

View File

@ -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
/// <param name="gameTime">Provides a snapshot of timing values.</param>
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);