Scrolling now moves children.

This commit is contained in:
Harrison Deng 2019-04-27 23:22:33 -05:00
parent 1211cda508
commit 72188194e5

View File

@ -15,6 +15,8 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
{ {
public class UIScrollable : UIModule public class UIScrollable : UIModule
{ {
BasicScissor basicScissor;
Rectangle viewport;
UIModuleGroup group; UIModuleGroup group;
Color scrollBarColor; Color scrollBarColor;
@ -24,8 +26,34 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
private bool horizontalSelected; private bool horizontalSelected;
private Vector2 mouseRelativePosition; private Vector2 mouseRelativePosition;
float shiftX, shiftY; 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; UIModule furthestXModule, furthestYMod;
bool horScrollAvailable, vertScrollAvailable; bool horScrollAvailable, vertScrollAvailable;
@ -139,7 +167,8 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
this.verticalBarTrack = verticalBarTrack; this.verticalBarTrack = verticalBarTrack;
this.background = background; this.background = background;
group = new UIModuleGroup(new BasicScissor()); basicScissor = new BasicScissor();
group = new UIModuleGroup();
HorizontalBarThickness = 12; HorizontalBarThickness = 12;
VerticalBarThickness = 12; VerticalBarThickness = 12;
} }
@ -174,23 +203,26 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
if (horScrollAvailable) 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) 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); base.Update(gameTime);
} }
public override void Draw(ConsistentSpriteBatch spriteBatch) public override void Draw(ConsistentSpriteBatch spriteBatch)
{ {
background?.Draw(spriteBatch, Boundaries, color, origin: origin); background?.Draw(spriteBatch, Boundaries, color, origin: origin);
spriteBatch.End();
basicScissor.Begin(viewport, spriteBatch);
group.Draw(spriteBatch); group.Draw(spriteBatch);
basicScissor.End();
spriteBatch.Begin();
if (horScrollAvailable) if (horScrollAvailable)
{ {
horizontalScrollBar.Draw(spriteBatch, horizontalScrollBarBounds, scrollBarColor); horizontalScrollBar.Draw(spriteBatch, horizontalScrollBarBounds, scrollBarColor);
@ -211,15 +243,15 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
UIModule m = addModules[i]; UIModule m = addModules[i];
int mFurthestX = m.Boundaries.X + m.Boundaries.Width; int mFurthestX = m.Boundaries.X + m.Boundaries.Width;
int mFurthestY = m.Boundaries.Y + m.Boundaries.Height; int mFurthestY = m.Boundaries.Y + m.Boundaries.Height;
if (mFurthestX > furthestX) if (mFurthestX > group.Width)
{ {
furthestXModule = m; furthestXModule = m;
furthestX = mFurthestX; group.Width = mFurthestX;
} }
if (mFurthestY > furthestY) if (mFurthestY > group.Height)
{ {
furthestYMod = m; furthestYMod = m;
furthestY = mFurthestY; group.Height = mFurthestY;
} }
} }
@ -233,33 +265,33 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
if (module == furthestXModule) if (module == furthestXModule)
{ {
furthestX = 0; group.Width = 0;
UIModule[] modules = group.GetCopyOfModules(); UIModule[] modules = group.GetCopyOfModules();
for (int i = 0; i < modules.Length; i++) for (int i = 0; i < modules.Length; i++)
{ {
UIModule m = modules[i]; UIModule m = modules[i];
int mFurthestX = m.Boundaries.X + m.Boundaries.Width; int mFurthestX = m.Boundaries.X + m.Boundaries.Width;
if (mFurthestX > furthestX) if (mFurthestX > group.Width)
{ {
furthestXModule = m; furthestXModule = m;
furthestX = mFurthestX; group.Width = mFurthestX;
} }
} }
} }
if (module == furthestYMod) if (module == furthestYMod)
{ {
furthestY = 0; group.Height = 0;
UIModule[] modules = group.GetCopyOfModules(); UIModule[] modules = group.GetCopyOfModules();
for (int i = 0; i < modules.Length; i++) for (int i = 0; i < modules.Length; i++)
{ {
UIModule m = modules[i]; UIModule m = modules[i];
int mFurthestY = m.Boundaries.Y + m.Boundaries.Height; int mFurthestY = m.Boundaries.Y + m.Boundaries.Height;
if (mFurthestY > furthestY) if (mFurthestY > group.Height)
{ {
furthestYMod = m; furthestYMod = m;
furthestY = mFurthestY; group.Height = mFurthestY;
} }
} }
} }
@ -267,14 +299,15 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
private void WidthOrXChange(bool onlyXChange = false) private void WidthOrXChange(bool onlyXChange = false)
{ {
group.X = X + leftPadding; group.X = X + leftPadding + (int)shiftX;
if (!onlyXChange) if (!onlyXChange)
{ {
group.Width = Width - rightPadding - leftPadding; viewport.X = X + leftPadding;
if (Width < furthestX) viewport.Width = Width - rightPadding - leftPadding;
if (Width < group.Width)
{ {
horScrollAvailable = true; horScrollAvailable = true;
horizontalScrollBarBounds.Width = (int)(Width * ((float)Width / furthestX)); horizontalScrollBarBounds.Width = (int)(Width * ((float)Width / group.Width));
horizontalScrollBarBounds.Width = Math.Max(horizontalScrollBarBounds.Width, minimumBarLength); horizontalScrollBarBounds.Width = Math.Max(horizontalScrollBarBounds.Width, minimumBarLength);
} }
else { horScrollAvailable = false; } else { horScrollAvailable = false; }
@ -282,17 +315,17 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
verticalScrollBarBounds.X = X; verticalScrollBarBounds.X = X;
if (!hideScrollBars) if (!hideScrollBars)
{ {
group.Width -= VerticalBarThickness; viewport.Width -= VerticalBarThickness;
if (!verticalBarLeftPosition) if (!verticalBarLeftPosition)
{ {
verticalScrollBarBounds.X += group.Width; verticalScrollBarBounds.X += viewport.Width;
} }
} }
else else
{ {
if (!verticalBarLeftPosition) 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) private void HeightOrYChange(bool onlyYChange = false)
{ {
group.Y = Y + bottomPadding; group.Y = Y + bottomPadding + (int)shiftY;
if (!onlyYChange) if (!onlyYChange)
{ {
group.Height = Height - bottomPadding - topPadding; viewport.Y = Y + bottomPadding;
if (Height < furthestY) viewport.Height = Height - bottomPadding - topPadding;
if (Height < group.Height)
{ {
vertScrollAvailable = true; vertScrollAvailable = true;
verticalScrollBarBounds.Height = (int)(Height * ((float)Height / furthestY)); verticalScrollBarBounds.Height = (int)(Height * ((float)Height / group.Height));
verticalScrollBarBounds.Height = Math.Max(verticalScrollBarBounds.Height, minimumBarLength); verticalScrollBarBounds.Height = Math.Max(verticalScrollBarBounds.Height, minimumBarLength);
} }
else { vertScrollAvailable = false; } else { vertScrollAvailable = false; }
@ -315,32 +349,31 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
horizontalScrollBarBounds.Y = Y; horizontalScrollBarBounds.Y = Y;
if (!hideScrollBars) if (!hideScrollBars)
{ {
group.Height -= HorizontalBarThickness; viewport.Height -= HorizontalBarThickness;
if (!horizontalBarTopPosition) if (!horizontalBarTopPosition)
{ {
horizontalScrollBarBounds.Y += group.Height; horizontalScrollBarBounds.Y += viewport.Height;
} }
else else
{ {
group.Y += horizontalScrollBarBounds.Height; viewport.Y += horizontalScrollBarBounds.Height;
} }
} }
else else
{ {
if (!horizontalBarTopPosition) if (!horizontalBarTopPosition)
{ {
horizontalScrollBarBounds.Y += group.Height - horizontalScrollBarBounds.Height; horizontalScrollBarBounds.Y += viewport.Height - horizontalScrollBarBounds.Height;
} }
} }
} }
} }
public override bool KeyboardStateChanged(KeyboardState state) public override bool KeyboardStateChanged(KeyboardState state)
{ {
if (state.IsKeyDown(Keys.Right)) if (state.IsKeyDown(Keys.Right))
{ {
shiftX -= 20; XScrollPosition += 20;
} }
return base.KeyboardStateChanged(state); return base.KeyboardStateChanged(state);
@ -388,12 +421,12 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
if (horizontalSelected) if (horizontalSelected)
{ {
float latestPosition = state.X - mouseRelativePosition.X; float latestPosition = state.X - mouseRelativePosition.X;
shiftX = -(latestPosition / (Width - horizontalScrollBarBounds.Width)) * furthestX; XScrollPosition = (latestPosition / (Width - horizontalScrollBarBounds.Width)) * group.Width;
} }
else else
{ {
float latestPosition = state.Y - mouseRelativePosition.Y; float latestPosition = state.Y - mouseRelativePosition.Y;
shiftY = -(latestPosition / (Height - verticalScrollBarBounds.Height)) * furthestY; YScrollPosition = -(latestPosition / (Height - verticalScrollBarBounds.Height)) * group.Height;
} }
if (state.LeftButton == ButtonState.Released) if (state.LeftButton == ButtonState.Released)
@ -403,15 +436,5 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
} }
return base.MouseStateChanged(state); 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));
}
} }
} }