Added UI automatic centering.

This commit is contained in:
Harrison Deng 2019-03-08 10:14:51 -06:00
parent ebe22fa9dc
commit 5ffdc3a9d6
2 changed files with 45 additions and 12 deletions

View File

@ -18,6 +18,12 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
private string originalText;
private string displayedText;
private Vector2 modifiedTextSize;
/// <summary>
/// Centers the text int bounds.
/// </summary>
public bool center;
/// <summary>
/// Whether or not to try and wrap text automatically. Meaning will check and attempt to wrap every update.
/// </summary>
@ -67,6 +73,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
if (useEllipses) AttemptToApplyEllipsis();
if (autoWrap) AttemptToWrapText();
if (autoScale) AttemptToScaleFont();
if (center) Center();
base.Update(gameTime);
}
@ -173,27 +180,22 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
}
}
/// <summary>
/// Centers this text in the given rectangle if possible.
/// </summary>
/// <param name="rectangle"></param>
/// <returns>True if succeeded and false if there isn't enough space.</returns>
public bool CenterIn(Rectangle rectangle)
private bool Center()
{
Vector2 textSize = new Vector2(modifiedTextSize.X * scale, modifiedTextSize.Y * scale);
if (textSize.X <= rectangle.Width)
if (textSize.X <= bounds.Width)
{
position.X = rectangle.X + (rectangle.Width - textSize.X) / 2f;
position.X = bounds.X + (bounds.Width - textSize.X) / 2f;
}
else
{
return false;
}
if (textSize.Y <= rectangle.Height)
if (textSize.Y <= bounds.Height)
{
position.Y = rectangle.Y + (rectangle.Height - textSize.Y) / 2f;
position.Y = bounds.Y + (bounds.Height - textSize.Y) / 2f;
}
else
{

View File

@ -79,7 +79,8 @@ namespace RecrownedAthenaeum.UI.Modular
int tX = rectangle.X + parentHitbox.X;
int tY = rectangle.Y + parentHitbox.Y;
return new Rectangle(tX, tY, rectangle.Width, rectangle.Height);
} else
}
else
{
return rectangle;
}
@ -122,5 +123,35 @@ namespace RecrownedAthenaeum.UI.Modular
origin.X = bounds.Width / 2f;
origin.Y = bounds.Height / 2f;
}
/// <summary>
/// Centers this module's origin on the horizontal axis relative to the given rectangle.
/// </summary>
/// <param name="rectangle">The rectangle to center it in.</param>
/// <returns>True if possible and false if not.</returns>
public bool CenterHorizontally(Rectangle rectangle)
{
if (rectangle.Width >= bounds.Width)
{
bounds.X = rectangle.Width / 2 + rectangle.X;
return true;
}
return false;
}
/// <summary>
/// Center's this module's origin on the vertical axis relative to the given rectangle.
/// </summary>
/// <param name="rectangle">The rectangle to center in.</param>
/// <returns>True if possible.</returns>
public bool CenterVertically(Rectangle rectangle)
{
if (rectangle.Height >= bounds.Height)
{
bounds.Y = rectangle.Height / 2 + rectangle.Y;
return true;
}
return false;
}
}
}