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> /// <summary>
/// Scale of of the X axis. /// Scale of of the X axis.
/// </summary> /// </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> /// <summary>
/// Scale of the Y axis. /// Scale of the Y axis.
/// </summary> /// </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> /// <summary>
/// Sets scale of X and Y. /// Sets scale of X and Y.
@ -42,7 +42,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
public Image(Texture2D texture) public Image(Texture2D texture)
{ {
this.texture = texture ?? throw new ArgumentException("Image requires a texture."); this.texture = texture ?? throw new ArgumentException("Image requires a texture.");
bounds = texture.Bounds; situation = texture.Bounds;
} }
/// <summary> /// <summary>
@ -51,7 +51,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
/// <param name="batch">The batch to use.</param> /// <param name="batch">The batch to use.</param>
public override void Draw(SpriteBatch batch) 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); base.Draw(batch);
} }
@ -68,7 +68,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
this.color = color; this.color = color;
this.rotation = rotation; this.rotation = rotation;
this.origin = origin; this.origin = origin;
bounds = destination; situation = destination;
Draw(spriteBatch); Draw(spriteBatch);
} }
} }

View File

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

View File

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

View File

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

View File

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