Added centering function.
This commit is contained in:
		| @@ -13,7 +13,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules | |||||||
|     public class Text : UIModule |     public class Text : UIModule | ||||||
|     { |     { | ||||||
|         private SpriteFont font; |         private SpriteFont font; | ||||||
|         private float scale; |         private float scale = 1f; | ||||||
|         private Vector2 position; |         private Vector2 position; | ||||||
|         private string originalText; |         private string originalText; | ||||||
|         private string displayedText; |         private string displayedText; | ||||||
| @@ -106,14 +106,20 @@ namespace RecrownedAthenaeum.UI.Modular.Modules | |||||||
|         /// </summary> |         /// </summary> | ||||||
|         public void AttemptToScaleFont() |         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(); |                 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; | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user