2019-12-28 21:41:06 +00:00
|
|
|
|
using OpenTK;
|
2019-12-28 20:35:01 +00:00
|
|
|
|
using RecrownedAthenaeum.Graphics.Render;
|
2019-01-30 13:46:58 +00:00
|
|
|
|
using System;
|
2018-11-30 02:41:06 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
2019-12-28 21:41:06 +00:00
|
|
|
|
namespace RecrownedAthenaeum.Graphics.UI.Modular.Modules
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
2019-01-14 07:26:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents text for the UI.
|
|
|
|
|
/// </summary>
|
2018-12-11 07:12:25 +00:00
|
|
|
|
public class Text : UIModule
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
|
|
|
|
private SpriteFont font;
|
2019-03-06 03:41:31 +00:00
|
|
|
|
private float scale = 1f;
|
2018-11-30 02:41:06 +00:00
|
|
|
|
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>
|
2019-03-08 16:17:37 +00:00
|
|
|
|
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>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
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>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
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>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
public bool useEllipses;
|
2019-01-14 07:26:46 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The text to use for the ellipsis.
|
|
|
|
|
/// </summary>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
public string ellipsis = "...";
|
2018-12-11 07:12:25 +00:00
|
|
|
|
private string ModifiedText { get { return displayedText; } set { displayedText = value; modifiedTextSize = font.MeasureString(value); } }
|
2018-12-14 06:43:38 +00:00
|
|
|
|
|
2019-01-14 07:26:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The string to be displayed.
|
|
|
|
|
/// </summary>
|
2019-01-30 13:46:58 +00:00
|
|
|
|
public string Content { get { return originalText; } set { originalText = value; if (value == null) value = ellipsis; modifiedTextSize = font.MeasureString(value); displayedText = value; } }
|
2018-11-30 02:41:06 +00:00
|
|
|
|
|
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>
|
2018-12-11 07:12:25 +00:00
|
|
|
|
public Text(SpriteFont font, string content = null)
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
2019-01-30 13:46:58 +00:00
|
|
|
|
this.font = font ?? throw new ArgumentNullException("Font cannot be null.");
|
2018-12-11 07:12:25 +00:00
|
|
|
|
Content = content;
|
2018-11-30 02:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
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>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
public override void Update(GameTime gameTime)
|
|
|
|
|
{
|
2019-04-09 04:58:27 +00:00
|
|
|
|
position.X = X;
|
|
|
|
|
position.Y = Y;
|
2018-11-30 02:41:06 +00:00
|
|
|
|
|
|
|
|
|
if (useEllipses) AttemptToApplyEllipsis();
|
|
|
|
|
if (autoWrap) AttemptToWrapText();
|
|
|
|
|
if (autoScale) AttemptToScaleFont();
|
2019-03-08 16:17:37 +00:00
|
|
|
|
if (centered) Center();
|
2018-11-30 02:41:06 +00:00
|
|
|
|
|
|
|
|
|
base.Update(gameTime);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 07:26:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Draws the text.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="batch">Batch to use.</param>
|
2019-03-24 00:04:43 +00:00
|
|
|
|
public override void Draw(ConsistentSpriteBatch batch)
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
2019-03-09 06:57:29 +00:00
|
|
|
|
batch.DrawString(font, Content, position, color, 0f, default(Vector2), scale, SpriteEffects.None, 0f);
|
2018-11-30 02:41:06 +00:00
|
|
|
|
base.Draw(batch);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 07:26:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Attempts to apply ellipsis. Checks of nessecary.
|
|
|
|
|
/// </summary>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
public void AttemptToApplyEllipsis()
|
|
|
|
|
{
|
2019-04-09 04:58:27 +00:00
|
|
|
|
if (modifiedTextSize.X * scale > Width && ModifiedText.Length > ellipsis.Length + 1)
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
|
|
|
|
RemoveLineBreaks();
|
|
|
|
|
StringBuilder stringBuilder = new StringBuilder(ModifiedText);
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
stringBuilder.Remove(stringBuilder.Length, ellipsis.Length - 1);
|
|
|
|
|
stringBuilder.Insert(stringBuilder.Length, ellipsis);
|
|
|
|
|
}
|
2019-04-09 04:58:27 +00:00
|
|
|
|
while (font.MeasureString(stringBuilder).X * scale > Width);
|
2018-11-30 02:41:06 +00:00
|
|
|
|
|
|
|
|
|
ModifiedText = stringBuilder.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 07:26:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Attempts to scale the font. Checks if nessecary.
|
|
|
|
|
/// </summary>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
public void AttemptToScaleFont()
|
|
|
|
|
{
|
2019-03-06 03:41:31 +00:00
|
|
|
|
|
2019-04-09 04:58:27 +00:00
|
|
|
|
if (Width < Height)
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
2019-04-09 04:58:27 +00:00
|
|
|
|
if (Math.Round(modifiedTextSize.X * scale ) != Width)
|
2019-03-06 03:41:31 +00:00
|
|
|
|
{
|
2019-04-09 04:58:27 +00:00
|
|
|
|
scale = Width / modifiedTextSize.X;
|
2019-03-06 03:41:31 +00:00
|
|
|
|
}
|
2018-11-30 02:41:06 +00:00
|
|
|
|
}
|
2019-03-06 03:41:31 +00:00
|
|
|
|
else
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
2019-04-09 04:58:27 +00:00
|
|
|
|
if (Math.Round(modifiedTextSize.Y * scale ) != Height)
|
2019-03-06 03:41:31 +00:00
|
|
|
|
{
|
2019-04-09 04:58:27 +00:00
|
|
|
|
scale = Height / (modifiedTextSize.Y);
|
2019-03-06 03:41:31 +00:00
|
|
|
|
}
|
2018-11-30 02:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 07:26:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes line breaks.
|
|
|
|
|
/// </summary>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
public void RemoveLineBreaks()
|
|
|
|
|
{
|
|
|
|
|
ModifiedText = ModifiedText.Replace("\n", " ");
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 07:26:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resets to original text.
|
|
|
|
|
/// </summary>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
public void ResetToOriginalText()
|
|
|
|
|
{
|
2019-01-30 13:46:58 +00:00
|
|
|
|
Content = originalText;
|
2018-11-30 02:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
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>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
public void AttemptToWrapText(bool unwrap = false)
|
|
|
|
|
{
|
|
|
|
|
if (unwrap) RemoveLineBreaks();
|
2019-04-09 04:58:27 +00:00
|
|
|
|
if (modifiedTextSize.X * scale > Width)
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
2019-04-09 04:58:27 +00:00
|
|
|
|
if (currentScaledLineWidth + scaledWidth <= Width)
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
2019-04-09 04:58:27 +00:00
|
|
|
|
if (textSize.X <= Width)
|
2019-03-06 03:41:31 +00:00
|
|
|
|
{
|
2019-04-09 04:58:27 +00:00
|
|
|
|
position.X = X + (Width - textSize.X) / 2f;
|
2019-03-06 03:41:31 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 04:58:27 +00:00
|
|
|
|
if (textSize.Y <= Height)
|
2019-03-06 03:41:31 +00:00
|
|
|
|
{
|
2019-04-09 04:58:27 +00:00
|
|
|
|
position.Y = Y + (Height - textSize.Y) / 2f;
|
2019-03-06 03:41:31 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-11-30 02:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|