Modules made more useable by improving or fixing some of the drawing position logic. Variable names refactored as well.

This commit is contained in:
Harrison Deng 2019-03-09 00:57:29 -06:00
parent 782ca31f15
commit ed11d31100
6 changed files with 54 additions and 50 deletions

View File

@ -23,12 +23,12 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
/// <summary>
/// Scale of of the X axis.
/// </summary>
public float ScaleX { get { return (float)bounds.Width / texture.Width; } set { bounds.Width = (int)(texture.Width * value); } }
public float ScaleX { get { return (float)situation.Width / texture.Width; } set { situation.Width = (int)(texture.Width * value); } }
/// <summary>
/// Scale of the Y axis.
/// </summary>
public float ScaleY { get { return (float)bounds.Height / texture.Height; } set { bounds.Height = (int)(texture.Height * value); } }
public float ScaleY { get { return (float)situation.Height / texture.Height; } set { situation.Height = (int)(texture.Height * value); } }
/// <summary>
/// Sets scale of X and Y.
@ -42,7 +42,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
public Image(Texture2D texture)
{
this.texture = texture ?? throw new ArgumentException("Image requires a texture.");
bounds = texture.Bounds;
situation = texture.Bounds;
}
/// <summary>
@ -51,7 +51,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
/// <param name="batch">The batch to use.</param>
public override void Draw(SpriteBatch batch)
{
batch.Draw(texture, bounds, null, color, rotation, origin, SpriteEffects.None, 0f);
batch.Draw(texture, situation, null, color, rotation, origin, SpriteEffects.None, 0f);
base.Draw(batch);
}
@ -68,7 +68,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
this.color = color;
this.rotation = rotation;
this.origin = origin;
bounds = destination;
situation = destination;
Draw(spriteBatch);
}
}

View File

@ -69,12 +69,12 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
/// </summary>
/// <param name="skin">The skin the definition is defined in.</param>
/// <param name="skinDefinition">The definition itself.</param>
public Button(ISkin skin, ButtonSkinDefinition skinDefinition) :
this(skin.GetTextureAtlasRegion(skinDefinition.downRegion, true),
skin.GetTextureAtlasRegion(skinDefinition.upRegion, true),
skin.GetTextureAtlasRegion(skinDefinition.disabledRegion),
public Button(ISkin skin, ButtonSkinDefinition skinDefinition) :
this(skin.GetTextureAtlasRegion(skinDefinition.downRegion, true),
skin.GetTextureAtlasRegion(skinDefinition.upRegion, true),
skin.GetTextureAtlasRegion(skinDefinition.disabledRegion),
skin.GetTextureAtlasRegion(skinDefinition.selectedRegion))
{}
{ }
/// <summary>
/// Draws the button.
@ -84,17 +84,21 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
{
if (disabled)
{
disabledTexture?.Draw(batch, bounds, color);
disabledTexture?.Draw(batch, Boundaries, color);
}
else
{
if (pressed)
{
downTexture.Draw(batch, bounds, color);
downTexture.Draw(batch, Boundaries, color);
}
else if (Highlighted)
{
highlightedTexture?.Draw(batch, Boundaries, color);
}
else
{
upTexture.Draw(batch, bounds, color);
upTexture.Draw(batch, Boundaries, color);
}
}
@ -108,7 +112,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
/// <returns>Whether or not to continue calling the next mouse change listener.</returns>
public sealed override bool MouseStateChanged(MouseState state)
{
if (InputUtilities.MouseWithinBoundries(bounds))
if (InputUtilities.MouseWithinBoundries(Boundaries))
{
if (state.LeftButton == ButtonState.Pressed)
{

View File

@ -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.bounds = bounds;
text.situation = Boundaries;
text.Update(gameTime);
base.Update(gameTime);
}

View File

@ -67,8 +67,8 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
/// <param name="gameTime">The game time.</param>
public override void Update(GameTime gameTime)
{
position.X = bounds.X;
position.Y = bounds.Y;
position.X = situation.X;
position.Y = situation.Y;
if (useEllipses) AttemptToApplyEllipsis();
if (autoWrap) AttemptToWrapText();
@ -84,7 +84,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
/// <param name="batch">Batch to use.</param>
public override void Draw(SpriteBatch batch)
{
batch.DrawString(font, Content, position, color, 0f, origin, scale, SpriteEffects.None, 0f);
batch.DrawString(font, Content, position, color, 0f, default(Vector2), scale, SpriteEffects.None, 0f);
base.Draw(batch);
}
@ -93,7 +93,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
/// </summary>
public void AttemptToApplyEllipsis()
{
if (modifiedTextSize.X * scale > bounds.Width && ModifiedText.Length > ellipsis.Length + 1)
if (modifiedTextSize.X * scale > situation.Width && ModifiedText.Length > ellipsis.Length + 1)
{
RemoveLineBreaks();
StringBuilder stringBuilder = new StringBuilder(ModifiedText);
@ -102,7 +102,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
stringBuilder.Remove(stringBuilder.Length, ellipsis.Length - 1);
stringBuilder.Insert(stringBuilder.Length, ellipsis);
}
while (font.MeasureString(stringBuilder).X * scale > bounds.Width);
while (font.MeasureString(stringBuilder).X * scale > situation.Width);
ModifiedText = stringBuilder.ToString();
}
@ -114,18 +114,18 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
public void AttemptToScaleFont()
{
if (bounds.Width < bounds.Height)
if (situation.Width < situation.Height)
{
if (Math.Round(modifiedTextSize.X * scale ) != bounds.Width)
if (Math.Round(modifiedTextSize.X * scale ) != situation.Width)
{
scale = bounds.Width / modifiedTextSize.X;
scale = situation.Width / modifiedTextSize.X;
}
}
else
{
if (Math.Round(modifiedTextSize.Y * scale ) != bounds.Height)
if (Math.Round(modifiedTextSize.Y * scale ) != situation.Height)
{
scale = bounds.Height / (modifiedTextSize.Y);
scale = situation.Height / (modifiedTextSize.Y);
}
}
}
@ -153,7 +153,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
public void AttemptToWrapText(bool unwrap = false)
{
if (unwrap) RemoveLineBreaks();
if (modifiedTextSize.X * scale > bounds.Width)
if (modifiedTextSize.X * scale > situation.Width)
{
ModifiedText = ModifiedText.Replace("\n", " ");
string[] words = ModifiedText.Split(' ');
@ -165,7 +165,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
string word = words[w];
float scaledWidth = font.MeasureString(word).X * scale;
if (currentScaledLineWidth + scaledWidth <= bounds.Width)
if (currentScaledLineWidth + scaledWidth <= situation.Width)
{
stringBuilder.Append(word);
currentScaledLineWidth += scaledWidth;
@ -184,18 +184,18 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
{
Vector2 textSize = new Vector2(modifiedTextSize.X * scale, modifiedTextSize.Y * scale);
if (textSize.X <= bounds.Width)
if (textSize.X <= situation.Width)
{
position.X = bounds.X + (bounds.Width - textSize.X) / 2f;
position.X = situation.X + (situation.Width - textSize.X) / 2f;
}
else
{
return false;
}
if (textSize.Y <= bounds.Height)
if (textSize.Y <= situation.Height)
{
position.Y = bounds.Y + (bounds.Height - textSize.Y) / 2f;
position.Y = situation.Y + (situation.Height - textSize.Y) / 2f;
}
else
{

View File

@ -16,16 +16,16 @@ namespace RecrownedAthenaeum.UI.Modular
/// <summary>
/// The bounds before factoring in the origin.
/// </summary>
public Rectangle bounds;
public Rectangle situation;
/// <summary>
/// Bounds of this module (after factoring in the origin).
/// </summary>
public Rectangle FinalBounds
public Rectangle Boundaries
{
get
{
return new Rectangle((int)(bounds.X - origin.X), (int)(bounds.Y - origin.Y), bounds.Width, bounds.Height);
return new Rectangle((int)(situation.X - origin.X), (int)(situation.Y - origin.Y), situation.Width, situation.Height);
}
}
@ -120,8 +120,8 @@ namespace RecrownedAthenaeum.UI.Modular
/// </summary>
public void CenterOrigin()
{
origin.X = bounds.Width / 2f;
origin.Y = bounds.Height / 2f;
origin.X = situation.Width / 2f;
origin.Y = situation.Height / 2f;
}
/// <summary>
@ -131,9 +131,9 @@ namespace RecrownedAthenaeum.UI.Modular
/// <returns>True if possible and false if not.</returns>
public bool CenterHorizontally(Rectangle rectangle)
{
if (rectangle.Width >= bounds.Width)
if (rectangle.Width >= Boundaries.Width)
{
bounds.X = rectangle.Width / 2 + rectangle.X;
situation.X = rectangle.Width / 2 + situation.X;
return true;
}
return false;
@ -146,9 +146,9 @@ namespace RecrownedAthenaeum.UI.Modular
/// <returns>True if possible.</returns>
public bool CenterVertically(Rectangle rectangle)
{
if (rectangle.Height >= bounds.Height)
if (rectangle.Height >= Boundaries.Height)
{
bounds.Y = rectangle.Height / 2 + rectangle.Y;
situation.Y = rectangle.Height / 2 + situation.Y;
return true;
}
return false;

View File

@ -49,10 +49,10 @@ namespace RecrownedAthenaeum.UI.Modular
{
batch.End();
batch.Begin(effect: camera?.BasicEffect);
scissorBounds.Width = bounds.Width;
scissorBounds.Height = bounds.Height;
scissorBounds.X = bounds.X;
scissorBounds.Y = bounds.Y;
scissorBounds.Width = situation.Width;
scissorBounds.Height = situation.Height;
scissorBounds.X = situation.X;
scissorBounds.Y = situation.Y;
Rectangle scissor = scissorBounds;
scissorBounds = batch.GraphicsDevice.ScissorRectangle;
batch.GraphicsDevice.ScissorRectangle = scissor;
@ -60,13 +60,13 @@ namespace RecrownedAthenaeum.UI.Modular
foreach (UIModule module in modules)
{
int offsetX = module.bounds.X;
int offsetY = module.bounds.Y;
module.bounds.X = bounds.X + offsetX;
module.bounds.Y = bounds.Y + offsetY;
int offsetX = module.situation.X;
int offsetY = module.situation.Y;
module.situation.X = situation.X + offsetX;
module.situation.Y = situation.Y + offsetY;
module.Draw(batch);
module.bounds.X = offsetX;
module.bounds.Y = offsetY;
module.situation.X = offsetX;
module.situation.Y = offsetY;
}
if (scissorBounds != null)