Added centering function.

This commit is contained in:
Harrison Deng 2019-03-05 21:41:31 -06:00
parent 7b130f39ea
commit d346f7c729

View File

@ -13,7 +13,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
public class Text : UIModule
{
private SpriteFont font;
private float scale;
private float scale = 1f;
private Vector2 position;
private string originalText;
private string displayedText;
@ -106,14 +106,20 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
/// </summary>
public void AttemptToScaleFont()
{
if (modifiedTextSize.X * scale > bounds.Width || modifiedTextSize.X * scale < bounds.Width - 5)
{
scale = bounds.Width / modifiedTextSize.X;
}
if (modifiedTextSize.Y * scale > bounds.Height || modifiedTextSize.Y * scale < bounds.Height - 5)
if (bounds.Width < bounds.Height)
{
scale = bounds.Height / modifiedTextSize.Y;
if (Math.Round(modifiedTextSize.X * scale ) != bounds.Width)
{
scale = bounds.Width / modifiedTextSize.X;
}
}
else
{
if (Math.Round(modifiedTextSize.Y * scale ) != bounds.Height)
{
scale = bounds.Height / (modifiedTextSize.Y);
}
}
}
@ -166,5 +172,35 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
ModifiedText = stringBuilder.ToString();
}
}
/// <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)
{
Vector2 textSize = new Vector2(modifiedTextSize.X * scale, modifiedTextSize.Y * scale);
if (textSize.X <= rectangle.Width)
{
position.X = rectangle.X + (rectangle.Width - textSize.X) / 2f;
}
else
{
return false;
}
if (textSize.Y <= rectangle.Height)
{
position.Y = rectangle.Y + (rectangle.Height - textSize.Y) / 2f;
}
else
{
return false;
}
return true;
}
}
}