182 lines
6.4 KiB
C#
182 lines
6.4 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using RecrownedAthenaeum.UI.Skin.Definitions;
|
|
using System.Text;
|
|
|
|
namespace RecrownedAthenaeum.UI.Modular.Modules
|
|
{
|
|
/// <summary>
|
|
/// Represents text for the UI.
|
|
/// </summary>
|
|
public class Text : UIModule
|
|
{
|
|
private TextSkinDefinition skinDefinition;
|
|
private SpriteFont font;
|
|
private float scale;
|
|
private Vector2 position;
|
|
private string originalText;
|
|
private string displayedText;
|
|
private Vector2 modifiedTextSize;
|
|
/// <summary>
|
|
/// Whether or not to try and wrap text automatically. Meaning will check and attempt to wrap every update.
|
|
/// </summary>
|
|
public bool autoWrap;
|
|
|
|
/// <summary>
|
|
/// Whether or not to automatically scale the text every update. Happens after auto wrap if enabled.
|
|
/// </summary>
|
|
public bool autoScale;
|
|
|
|
/// <summary>
|
|
/// Should this use ellipses? Will perform this operation before auto wrapping or auto scalling.
|
|
/// </summary>
|
|
public bool useEllipses;
|
|
|
|
/// <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); } }
|
|
|
|
/// <summary>
|
|
/// The string to be displayed.
|
|
/// </summary>
|
|
public string Content { get { return originalText; } set { if (value == null) value = ellipsis; modifiedTextSize = font.MeasureString(value); originalText = value; displayedText = value; } }
|
|
|
|
/// <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)
|
|
{
|
|
Content = content;
|
|
this.font = font;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a UI text object
|
|
/// </summary>
|
|
/// <param name="skin"></param>
|
|
/// <param name="skinDefinitionName"></param>
|
|
/// <param name="content"></param>
|
|
public Text(Skin.Skin skin, string skinDefinitionName = null, string content = null) : this(skin.fonts[skin.ObtainDefinition<TextSkinDefinition>(skinDefinitionName, typeof(Text)).font])
|
|
{
|
|
skinDefinition = skin.ObtainDefinition<TextSkinDefinition>(skinDefinitionName, GetType());
|
|
color = skin.colors[skinDefinition.color];
|
|
}
|
|
|
|
/// <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 = bounds.X;
|
|
position.Y = bounds.Y;
|
|
|
|
if (useEllipses) AttemptToApplyEllipsis();
|
|
if (autoWrap) AttemptToWrapText();
|
|
if (autoScale) AttemptToScaleFont();
|
|
|
|
base.Update(gameTime);
|
|
}
|
|
|
|
/// <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, origin, scale, SpriteEffects.None, 0f);
|
|
base.Draw(batch);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Attempts to apply ellipsis. Checks of nessecary.
|
|
/// </summary>
|
|
public void AttemptToApplyEllipsis()
|
|
{
|
|
if (modifiedTextSize.X * scale > bounds.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 > bounds.Width);
|
|
|
|
ModifiedText = stringBuilder.ToString();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Attempts to scale the font. Checks if nessecary.
|
|
/// </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)
|
|
{
|
|
scale = bounds.Height / modifiedTextSize.Y;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes line breaks.
|
|
/// </summary>
|
|
public void RemoveLineBreaks()
|
|
{
|
|
ModifiedText = ModifiedText.Replace("\n", " ");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resets to original text.
|
|
/// </summary>
|
|
public void ResetToOriginalText()
|
|
{
|
|
ModifiedText = originalText;
|
|
}
|
|
|
|
/// <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 > bounds.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 <= bounds.Width)
|
|
{
|
|
stringBuilder.Append(word);
|
|
currentScaledLineWidth += scaledWidth;
|
|
}
|
|
else
|
|
{
|
|
stringBuilder.AppendLine();
|
|
currentScaledLineWidth = 0;
|
|
}
|
|
}
|
|
ModifiedText = stringBuilder.ToString();
|
|
}
|
|
}
|
|
}
|
|
}
|