recrownedathenaeum/RecrownedAthenaeum/UI/Modular/Modules/Text.cs

209 lines
6.8 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
2019-01-28 00:14:10 +00:00
using RecrownedAthenaeum.UI.SkinSystem;
2019-01-27 23:39:18 +00:00
using RecrownedAthenaeum.UI.SkinSystem.Definitions;
using System;
using System.Text;
namespace RecrownedAthenaeum.UI.Modular.Modules
{
2019-01-14 07:26:46 +00:00
/// <summary>
/// Represents text for the UI.
/// </summary>
public class Text : UIModule
{
private SpriteFont font;
2019-03-06 03:41:31 +00:00
private float scale = 1f;
private Vector2 position;
private string originalText;
private string displayedText;
private Vector2 modifiedTextSize;
2019-03-08 16:14:51 +00:00
/// <summary>
/// Centers the text int bounds.
/// </summary>
public bool centered;
2019-03-08 16:14:51 +00:00
2019-01-14 07:26:46 +00:00
/// <summary>
/// Whether or not to try and wrap text automatically. Meaning will check and attempt to wrap every update.
/// </summary>
public bool autoWrap;
2019-01-14 07:26:46 +00:00
/// <summary>
/// Whether or not to automatically scale the text every update. Happens after auto wrap if enabled.
/// </summary>
public bool autoScale;
2019-01-14 07:26:46 +00:00
/// <summary>
/// Should this use ellipses? Will perform this operation before auto wrapping or auto scalling.
/// </summary>
public bool useEllipses;
2019-01-14 07:26:46 +00:00
/// <summary>
/// The text to use for the ellipsis.
/// </summary>
public string ellipsis = "...";
private string ModifiedText { get { return displayedText; } set { displayedText = value; modifiedTextSize = font.MeasureString(value); } }
2019-01-14 07:26:46 +00:00
/// <summary>
/// The string to be displayed.
/// </summary>
public string Content { get { return originalText; } set { originalText = value; if (value == null) value = ellipsis; modifiedTextSize = font.MeasureString(value); displayedText = value; } }
2019-01-14 07:26:46 +00:00
/// <summary>
/// Creates a UI text object.
/// </summary>
/// <param name="font">The font to use.</param>
/// <param name="content">The string for the text.</param>
public Text(SpriteFont font, string content = null)
{
this.font = font ?? throw new ArgumentNullException("Font cannot be null.");
Content = content;
}
2019-01-14 07:26:46 +00:00
/// <summary>
/// Updates the positioning and attempts to perform any operations that were marked automatic.
/// </summary>
/// <param name="gameTime">The game time.</param>
public override void Update(GameTime gameTime)
{
position.X = situation.X;
position.Y = situation.Y;
if (useEllipses) AttemptToApplyEllipsis();
if (autoWrap) AttemptToWrapText();
if (autoScale) AttemptToScaleFont();
if (centered) Center();
base.Update(gameTime);
}
2019-01-14 07:26:46 +00:00
/// <summary>
/// Draws the text.
/// </summary>
/// <param name="batch">Batch to use.</param>
public override void Draw(SpriteBatch batch)
{
batch.DrawString(font, Content, position, color, 0f, default(Vector2), scale, SpriteEffects.None, 0f);
base.Draw(batch);
}
2019-01-14 07:26:46 +00:00
/// <summary>
/// Attempts to apply ellipsis. Checks of nessecary.
/// </summary>
public void AttemptToApplyEllipsis()
{
if (modifiedTextSize.X * scale > situation.Width && ModifiedText.Length > ellipsis.Length + 1)
{
RemoveLineBreaks();
StringBuilder stringBuilder = new StringBuilder(ModifiedText);
do
{
stringBuilder.Remove(stringBuilder.Length, ellipsis.Length - 1);
stringBuilder.Insert(stringBuilder.Length, ellipsis);
}
while (font.MeasureString(stringBuilder).X * scale > situation.Width);
ModifiedText = stringBuilder.ToString();
}
}
2019-01-14 07:26:46 +00:00
/// <summary>
/// Attempts to scale the font. Checks if nessecary.
/// </summary>
public void AttemptToScaleFont()
{
2019-03-06 03:41:31 +00:00
if (situation.Width < situation.Height)
{
if (Math.Round(modifiedTextSize.X * scale ) != situation.Width)
2019-03-06 03:41:31 +00:00
{
scale = situation.Width / modifiedTextSize.X;
2019-03-06 03:41:31 +00:00
}
}
2019-03-06 03:41:31 +00:00
else
{
if (Math.Round(modifiedTextSize.Y * scale ) != situation.Height)
2019-03-06 03:41:31 +00:00
{
scale = situation.Height / (modifiedTextSize.Y);
2019-03-06 03:41:31 +00:00
}
}
}
2019-01-14 07:26:46 +00:00
/// <summary>
/// Removes line breaks.
/// </summary>
public void RemoveLineBreaks()
{
ModifiedText = ModifiedText.Replace("\n", " ");
}
2019-01-14 07:26:46 +00:00
/// <summary>
/// Resets to original text.
/// </summary>
public void ResetToOriginalText()
{
Content = originalText;
}
2019-01-14 07:26:46 +00:00
/// <summary>
/// Attempts to wrap text. Checks if nessecary.
/// </summary>
/// <param name="unwrap">If true, will first unwrap text, and the wrap again. This occurs before nessecity check.</param>
public void AttemptToWrapText(bool unwrap = false)
{
if (unwrap) RemoveLineBreaks();
if (modifiedTextSize.X * scale > situation.Width)
{
ModifiedText = ModifiedText.Replace("\n", " ");
string[] words = ModifiedText.Split(' ');
StringBuilder stringBuilder = new StringBuilder();
float currentScaledLineWidth = 0f;
for (int w = 0; w < words.Length; w++)
{
string word = words[w];
float scaledWidth = font.MeasureString(word).X * scale;
if (currentScaledLineWidth + scaledWidth <= situation.Width)
{
stringBuilder.Append(word);
currentScaledLineWidth += scaledWidth;
}
else
{
stringBuilder.AppendLine();
currentScaledLineWidth = 0;
}
}
ModifiedText = stringBuilder.ToString();
}
}
2019-03-06 03:41:31 +00:00
2019-03-08 16:14:51 +00:00
private bool Center()
2019-03-06 03:41:31 +00:00
{
Vector2 textSize = new Vector2(modifiedTextSize.X * scale, modifiedTextSize.Y * scale);
if (textSize.X <= situation.Width)
2019-03-06 03:41:31 +00:00
{
position.X = situation.X + (situation.Width - textSize.X) / 2f;
2019-03-06 03:41:31 +00:00
}
else
{
return false;
}
if (textSize.Y <= situation.Height)
2019-03-06 03:41:31 +00:00
{
position.Y = situation.Y + (situation.Height - textSize.Y) / 2f;
2019-03-06 03:41:31 +00:00
}
else
{
return false;
}
return true;
}
}
}