From d346f7c7291925e843244adda03fa5f6b0df9a21 Mon Sep 17 00:00:00 2001 From: Recrown Date: Tue, 5 Mar 2019 21:41:31 -0600 Subject: [PATCH] Added centering function. --- RecrownedAthenaeum/UI/Modular/Modules/Text.cs | 50 ++++++++++++++++--- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/RecrownedAthenaeum/UI/Modular/Modules/Text.cs b/RecrownedAthenaeum/UI/Modular/Modules/Text.cs index 55bf217..dfbaed8 100644 --- a/RecrownedAthenaeum/UI/Modular/Modules/Text.cs +++ b/RecrownedAthenaeum/UI/Modular/Modules/Text.cs @@ -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 /// 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(); } } + + /// + /// Centers this text in the given rectangle if possible. + /// + /// + /// True if succeeded and false if there isn't enough space. + 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; + } } }