Many refactors and minor changes. idk.
This commit is contained in:
@@ -24,12 +24,12 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
|
||||
/// <summary>
|
||||
/// Scale of of the X axis.
|
||||
/// </summary>
|
||||
public float ScaleX { get { return (float)situation.Width / texture.Width; } set { situation.Width = (int)(texture.Width * value); } }
|
||||
public float ScaleX { get { return (float)Width / texture.Width; } set { Width = (int)(texture.Width * value); } }
|
||||
|
||||
/// <summary>
|
||||
/// Scale of the Y axis.
|
||||
/// </summary>
|
||||
public float ScaleY { get { return (float)situation.Height / texture.Height; } set { situation.Height = (int)(texture.Height * value); } }
|
||||
public float ScaleY { get { return (float)Height / texture.Height; } set { Height = (int)(texture.Height * value); } }
|
||||
|
||||
/// <summary>
|
||||
/// Sets scale of X and Y.
|
||||
@@ -43,7 +43,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
|
||||
public Image(Texture2D texture)
|
||||
{
|
||||
this.texture = texture ?? throw new ArgumentException("Image requires a texture.");
|
||||
situation = texture.Bounds;
|
||||
SetPositionAndDimensions(texture.Bounds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -52,7 +52,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
|
||||
/// <param name="batch">The batch to use.</param>
|
||||
public override void Draw(ConsistentSpriteBatch batch)
|
||||
{
|
||||
batch.Draw(texture, situation, null, color, rotation, origin, SpriteEffects.None, 0f);
|
||||
batch.Draw(texture, new Rectangle(X, Y, Width, Height), null, color, rotation, origin, SpriteEffects.None, 0f);
|
||||
base.Draw(batch);
|
||||
}
|
||||
|
||||
@@ -69,8 +69,17 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
|
||||
this.color = color;
|
||||
this.rotation = rotation;
|
||||
this.origin = origin;
|
||||
situation = destination;
|
||||
SetPositionAndDimensions(destination);
|
||||
Draw(spriteBatch);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Center's the origin to the middle of the dimensions of the texture.
|
||||
/// </summary>
|
||||
public override void CenterOrigin()
|
||||
{
|
||||
origin.X = texture.Bounds.Width / 2f;
|
||||
origin.Y = texture.Bounds.Height / 2f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -76,7 +76,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
|
||||
/// <param name="gameTime">Snapshot of information about time for game.</param>
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
text.situation = Boundaries;
|
||||
text.SetPositionAndDimensions(Boundaries);
|
||||
text.Update(gameTime);
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
@@ -66,8 +66,8 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
|
||||
/// <param name="gameTime">The game time.</param>
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
position.X = situation.X;
|
||||
position.Y = situation.Y;
|
||||
position.X = X;
|
||||
position.Y = Y;
|
||||
|
||||
if (useEllipses) AttemptToApplyEllipsis();
|
||||
if (autoWrap) AttemptToWrapText();
|
||||
@@ -92,7 +92,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
|
||||
/// </summary>
|
||||
public void AttemptToApplyEllipsis()
|
||||
{
|
||||
if (modifiedTextSize.X * scale > situation.Width && ModifiedText.Length > ellipsis.Length + 1)
|
||||
if (modifiedTextSize.X * scale > Width && ModifiedText.Length > ellipsis.Length + 1)
|
||||
{
|
||||
RemoveLineBreaks();
|
||||
StringBuilder stringBuilder = new StringBuilder(ModifiedText);
|
||||
@@ -101,7 +101,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
|
||||
stringBuilder.Remove(stringBuilder.Length, ellipsis.Length - 1);
|
||||
stringBuilder.Insert(stringBuilder.Length, ellipsis);
|
||||
}
|
||||
while (font.MeasureString(stringBuilder).X * scale > situation.Width);
|
||||
while (font.MeasureString(stringBuilder).X * scale > Width);
|
||||
|
||||
ModifiedText = stringBuilder.ToString();
|
||||
}
|
||||
@@ -113,18 +113,18 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
|
||||
public void AttemptToScaleFont()
|
||||
{
|
||||
|
||||
if (situation.Width < situation.Height)
|
||||
if (Width < Height)
|
||||
{
|
||||
if (Math.Round(modifiedTextSize.X * scale ) != situation.Width)
|
||||
if (Math.Round(modifiedTextSize.X * scale ) != Width)
|
||||
{
|
||||
scale = situation.Width / modifiedTextSize.X;
|
||||
scale = Width / modifiedTextSize.X;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Math.Round(modifiedTextSize.Y * scale ) != situation.Height)
|
||||
if (Math.Round(modifiedTextSize.Y * scale ) != Height)
|
||||
{
|
||||
scale = situation.Height / (modifiedTextSize.Y);
|
||||
scale = Height / (modifiedTextSize.Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -152,7 +152,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
|
||||
public void AttemptToWrapText(bool unwrap = false)
|
||||
{
|
||||
if (unwrap) RemoveLineBreaks();
|
||||
if (modifiedTextSize.X * scale > situation.Width)
|
||||
if (modifiedTextSize.X * scale > Width)
|
||||
{
|
||||
ModifiedText = ModifiedText.Replace("\n", " ");
|
||||
string[] words = ModifiedText.Split(' ');
|
||||
@@ -164,7 +164,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
|
||||
string word = words[w];
|
||||
float scaledWidth = font.MeasureString(word).X * scale;
|
||||
|
||||
if (currentScaledLineWidth + scaledWidth <= situation.Width)
|
||||
if (currentScaledLineWidth + scaledWidth <= Width)
|
||||
{
|
||||
stringBuilder.Append(word);
|
||||
currentScaledLineWidth += scaledWidth;
|
||||
@@ -183,18 +183,18 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
|
||||
{
|
||||
Vector2 textSize = new Vector2(modifiedTextSize.X * scale, modifiedTextSize.Y * scale);
|
||||
|
||||
if (textSize.X <= situation.Width)
|
||||
if (textSize.X <= Width)
|
||||
{
|
||||
position.X = situation.X + (situation.Width - textSize.X) / 2f;
|
||||
position.X = X + (Width - textSize.X) / 2f;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (textSize.Y <= situation.Height)
|
||||
if (textSize.Y <= Height)
|
||||
{
|
||||
position.Y = situation.Y + (situation.Height - textSize.Y) / 2f;
|
||||
position.Y = Y + (Height - textSize.Y) / 2f;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
319
RecrownedAthenaeum/UI/Modular/Modules/UIScrollable.cs
Normal file
319
RecrownedAthenaeum/UI/Modular/Modules/UIScrollable.cs
Normal file
@@ -0,0 +1,319 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using RecrownedAthenaeum.Input;
|
||||
using RecrownedAthenaeum.Render;
|
||||
using RecrownedAthenaeum.SpecialTypes;
|
||||
using RecrownedAthenaeum.UI.SkinSystem;
|
||||
using RecrownedAthenaeum.UI.SkinSystem.Definitions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.Modular.Modules
|
||||
{
|
||||
public class UIScrollable : UIModule
|
||||
{
|
||||
UIModuleGroup group;
|
||||
|
||||
float opacityOfBar;
|
||||
bool showingBars;
|
||||
int shiftX, shiftY;
|
||||
int furthestX;
|
||||
int furthestY;
|
||||
UIModule furthestXMod, furthestYMod;
|
||||
|
||||
bool horScrollAvailable, vertScrollAvailable;
|
||||
|
||||
Rectangle horizontalScrollBarBounds, verticalScrollBarBounds;
|
||||
/// <summary>
|
||||
/// How fast the bars fade away in opacity (0 to 254) per second.
|
||||
/// </summary>
|
||||
public float barFadeSpeed;
|
||||
|
||||
ISpecialDrawable horizontalScrollBar, verticalScrollBar;
|
||||
ISpecialDrawable background, horizontalBarTrack, verticalBarTrack;
|
||||
|
||||
private int topPadding, bottomPadding, leftPadding, rightPadding;
|
||||
public int PadTop { get { return topPadding; } set { topPadding = value; HeightOrYChange(); } }
|
||||
public int PadBottom { get { return bottomPadding; } set { bottomPadding = value; HeightOrYChange(); } }
|
||||
public int PadLeft { get { return leftPadding; } set { leftPadding = value; WidthOrXChange(); } }
|
||||
public int PadRight { get { return rightPadding; } set { rightPadding = value; WidthOrXChange(); } }
|
||||
|
||||
/// <summary>
|
||||
/// The minimum bar length for the scroll bar.
|
||||
/// </summary>
|
||||
public int minimumBarLength = 16;
|
||||
/// <summary>
|
||||
/// The thickness of the scroll bars.
|
||||
/// </summary>
|
||||
int horizontalBarThickness, verticalBarThickness;
|
||||
public int HorizontalBarThickness { get { return horizontalBarThickness; } set { horizontalBarThickness = value; HeightOrYChange(); } }
|
||||
public int VerticalBarThickness { get { return verticalBarThickness; } set { verticalBarThickness = value; WidthOrXChange(); } }
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not to hide scroll bars.
|
||||
/// </summary>
|
||||
public bool HideScrollBars
|
||||
{
|
||||
get { return hideScrollBars; }
|
||||
set
|
||||
{
|
||||
hideScrollBars = value;
|
||||
WidthOrXChange();
|
||||
HeightOrYChange();
|
||||
}
|
||||
}
|
||||
|
||||
bool hideScrollBars = true;
|
||||
|
||||
/// <summary>
|
||||
/// Set to true to change from the normal position to the new position.
|
||||
/// </summary>
|
||||
public bool verticalBarLeftPosition, horizontalBarTopPosition;
|
||||
|
||||
|
||||
public override int Width
|
||||
{
|
||||
get
|
||||
{
|
||||
return base.Width;
|
||||
}
|
||||
set
|
||||
{
|
||||
base.Width = value;
|
||||
WidthOrXChange();
|
||||
}
|
||||
}
|
||||
|
||||
public override int Height
|
||||
{
|
||||
get
|
||||
{
|
||||
return base.Height;
|
||||
}
|
||||
set
|
||||
{
|
||||
base.Height = value;
|
||||
HeightOrYChange();
|
||||
}
|
||||
}
|
||||
|
||||
public override int X
|
||||
{
|
||||
get
|
||||
{
|
||||
return base.X;
|
||||
}
|
||||
set
|
||||
{
|
||||
WidthOrXChange(true);
|
||||
base.X = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override int Y
|
||||
{
|
||||
get
|
||||
{
|
||||
return base.Y;
|
||||
}
|
||||
set
|
||||
{
|
||||
HeightOrYChange(true);
|
||||
base.Y = value;
|
||||
}
|
||||
}
|
||||
|
||||
public UIScrollable(ISpecialDrawable horizontalScrollBar, ISpecialDrawable verticalScrollBar, ISpecialDrawable horizontalBarTrack = null, ISpecialDrawable verticalBarTrack = null, ISpecialDrawable background = null)
|
||||
{
|
||||
this.horizontalScrollBar = horizontalScrollBar;
|
||||
this.verticalScrollBar = verticalScrollBar;
|
||||
this.horizontalBarTrack = horizontalBarTrack;
|
||||
this.verticalBarTrack = verticalBarTrack;
|
||||
this.background = background;
|
||||
|
||||
group = new UIModuleGroup(new BasicScissor());
|
||||
}
|
||||
|
||||
public UIScrollable(ISkin skin, string definition) :
|
||||
this(skin.GetTextureAtlasRegion(skin.ObtainDefinition<UIScrollableSkinDefinition>().horizontalBar, true),
|
||||
skin.GetTextureAtlasRegion(skin.ObtainDefinition<UIScrollableSkinDefinition>().verticalBar, true),
|
||||
skin.GetTextureAtlasRegion(skin.ObtainDefinition<UIScrollableSkinDefinition>().horizontalBarTrack),
|
||||
skin.GetTextureAtlasRegion(skin.ObtainDefinition<UIScrollableSkinDefinition>().verticalBarTrack),
|
||||
skin.GetTextureAtlasRegion(skin.ObtainDefinition<UIScrollableSkinDefinition>().background))
|
||||
{
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
if (hideScrollBars)
|
||||
{
|
||||
if (!showingBars)
|
||||
{
|
||||
if (opacityOfBar <= 0)
|
||||
{
|
||||
opacityOfBar -= (barFadeSpeed / 255f) * gameTime.ElapsedGameTime.Seconds;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
opacityOfBar = 1f;
|
||||
}
|
||||
}
|
||||
|
||||
if (horScrollAvailable)
|
||||
{
|
||||
horizontalScrollBarBounds.X = (int)(((float)-shiftX / furthestX) * (group.Width - horizontalScrollBarBounds.Width));
|
||||
}
|
||||
|
||||
if (vertScrollAvailable)
|
||||
{
|
||||
verticalScrollBarBounds.Y = (int)(((float)-shiftY / furthestY) * (group.Height - verticalScrollBarBounds.Height));
|
||||
}
|
||||
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
||||
public override void Draw(ConsistentSpriteBatch spriteBatch)
|
||||
{
|
||||
background.Draw(spriteBatch, Boundaries, color, origin: origin);
|
||||
if (horScrollAvailable)
|
||||
{
|
||||
horizontalScrollBar.Draw(spriteBatch, horizontalScrollBarBounds, color);
|
||||
}
|
||||
if (vertScrollAvailable)
|
||||
{
|
||||
horizontalScrollBar.Draw(spriteBatch, verticalScrollBarBounds, color);
|
||||
}
|
||||
base.Draw(spriteBatch);
|
||||
}
|
||||
|
||||
public void AddModules(params UIModule[] addModules)
|
||||
{
|
||||
group.AddModules(addModules);
|
||||
|
||||
for (int i = 0; i < addModules.Length; i++)
|
||||
{
|
||||
UIModule m = addModules[i];
|
||||
int mFurthestX = m.Boundaries.X + m.Boundaries.Width;
|
||||
int mFurthestY = m.Boundaries.Y + m.Boundaries.Height;
|
||||
if (mFurthestX > furthestX)
|
||||
{
|
||||
furthestXMod = m;
|
||||
furthestX = mFurthestX;
|
||||
}
|
||||
if (mFurthestY > furthestY)
|
||||
{
|
||||
furthestYMod = m;
|
||||
furthestY = mFurthestY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveModule(UIModule module)
|
||||
{
|
||||
group.RemoveModule(module);
|
||||
|
||||
if (module == furthestXMod)
|
||||
{
|
||||
furthestX = 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)
|
||||
{
|
||||
furthestXMod = m;
|
||||
furthestX = mFurthestX;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (module == furthestYMod)
|
||||
{
|
||||
furthestY = 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)
|
||||
{
|
||||
furthestYMod = m;
|
||||
furthestY = mFurthestY;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void WidthOrXChange(bool onlyXChange = false)
|
||||
{
|
||||
group.X = X + leftPadding;
|
||||
if (!onlyXChange)
|
||||
{
|
||||
group.Width = Width - rightPadding - leftPadding;
|
||||
if (Width < furthestX)
|
||||
{
|
||||
horScrollAvailable = true;
|
||||
horizontalScrollBarBounds.Width = (int)(Width * ((float)Width / furthestX));
|
||||
horizontalScrollBarBounds.Width = Math.Max(horizontalScrollBarBounds.Width, minimumBarLength);
|
||||
}
|
||||
else { horScrollAvailable = false; }
|
||||
|
||||
|
||||
if (!hideScrollBars)
|
||||
{
|
||||
if (verticalBarLeftPosition)
|
||||
{
|
||||
group.X += verticalBarThickness;
|
||||
group.Width -= verticalBarThickness;
|
||||
}
|
||||
group.Height -= verticalBarThickness;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void HeightOrYChange(bool onlyYChange = false)
|
||||
{
|
||||
group.Y = Y + bottomPadding;
|
||||
if (!onlyYChange)
|
||||
{
|
||||
group.Height = Height - bottomPadding - topPadding;
|
||||
if (Height < furthestY)
|
||||
{
|
||||
horScrollAvailable = true;
|
||||
verticalScrollBarBounds.Height = (int)(Height * ((float)Height / furthestY));
|
||||
verticalScrollBarBounds.Height = Math.Max(verticalScrollBarBounds.Height, minimumBarLength);
|
||||
}
|
||||
else { vertScrollAvailable = false; }
|
||||
|
||||
if (!hideScrollBars)
|
||||
{
|
||||
if (!horizontalBarTopPosition)
|
||||
{
|
||||
group.Y += horizontalBarThickness;
|
||||
group.Height -= horizontalBarThickness;
|
||||
}
|
||||
group.Height -= horizontalBarThickness;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override bool MouseStateChanged(MouseState state)
|
||||
{
|
||||
if (InputUtilities.MouseWithinBoundries(Boundaries))
|
||||
{
|
||||
showingBars = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
showingBars = false;
|
||||
}
|
||||
return base.MouseStateChanged(state);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user