2018-11-21 00:41:24 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2018-11-21 00:56:41 +00:00
|
|
|
|
using RhythmBullet.Utilities.Camera;
|
2018-11-21 00:41:24 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2018-11-21 00:56:41 +00:00
|
|
|
|
namespace RhythmBullet.UI.Modular.Modules
|
2018-11-21 00:41:24 +00:00
|
|
|
|
{
|
|
|
|
|
public class TextLabel : UIModule
|
|
|
|
|
{
|
|
|
|
|
private SpriteFont font;
|
|
|
|
|
private float scale;
|
|
|
|
|
private Vector2 position;
|
|
|
|
|
private string originalText;
|
|
|
|
|
private string displayedText;
|
|
|
|
|
private Vector2 modifiedTextSize;
|
|
|
|
|
public bool autoWrap;
|
|
|
|
|
public bool autoScale;
|
|
|
|
|
public bool useEllipses;
|
|
|
|
|
public string ellipsis = "...";
|
|
|
|
|
private string ModifiedText
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return displayedText;
|
|
|
|
|
}
|
2018-11-21 01:52:51 +00:00
|
|
|
|
|
2018-11-21 00:41:24 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
displayedText = value;
|
|
|
|
|
modifiedTextSize = font.MeasureString(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public string Text
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return originalText;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2018-11-21 01:52:51 +00:00
|
|
|
|
if (value == null) value = "...";
|
2018-11-21 00:41:24 +00:00
|
|
|
|
modifiedTextSize = font.MeasureString(value);
|
|
|
|
|
originalText = value;
|
|
|
|
|
displayedText = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 01:52:51 +00:00
|
|
|
|
public TextLabel(SpriteFont font, string text = null)
|
2018-11-21 00:41:24 +00:00
|
|
|
|
{
|
|
|
|
|
Text = text;
|
|
|
|
|
this.font = font;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Draw(SpriteBatch batch)
|
|
|
|
|
{
|
|
|
|
|
batch.DrawString(font, Text, position, color, 0f, origin, scale, SpriteEffects.None, 0f);
|
|
|
|
|
base.Draw(batch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2018-11-21 01:52:51 +00:00
|
|
|
|
while (font.MeasureString(stringBuilder).X * scale > bounds.Width);
|
2018-11-21 00:41:24 +00:00
|
|
|
|
|
|
|
|
|
ModifiedText = stringBuilder.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AttemptToScaleFont()
|
|
|
|
|
{
|
2018-11-21 01:52:51 +00:00
|
|
|
|
if (modifiedTextSize.X * scale > bounds.Width || modifiedTextSize.X * scale < bounds.Width - 5)
|
2018-11-21 00:41:24 +00:00
|
|
|
|
{
|
|
|
|
|
scale = bounds.Width / modifiedTextSize.X;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 01:52:51 +00:00
|
|
|
|
if (modifiedTextSize.Y * scale > bounds.Height || modifiedTextSize.Y * scale < bounds.Height - 5)
|
2018-11-21 00:41:24 +00:00
|
|
|
|
{
|
|
|
|
|
scale = bounds.Height / modifiedTextSize.Y;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveLineBreaks()
|
|
|
|
|
{
|
|
|
|
|
ModifiedText = ModifiedText.Replace("\n", " ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResetToOriginalText()
|
|
|
|
|
{
|
|
|
|
|
ModifiedText = originalText;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|