From efbaad9c1b593600272b363d0a5429200d61e287 Mon Sep 17 00:00:00 2001 From: Recrown Date: Tue, 20 Nov 2018 18:41:24 -0600 Subject: [PATCH] improved text label (still untested, changed from "Text" to "TextLabel"). --- RhythmBullet/RhythmBullet.csproj | 2 +- .../Utilities/UI/Modular/Modules/Text.cs | 112 -------------- .../Utilities/UI/Modular/Modules/TextLabel.cs | 146 ++++++++++++++++++ 3 files changed, 147 insertions(+), 113 deletions(-) delete mode 100644 RhythmBullet/Zer01HD/Utilities/UI/Modular/Modules/Text.cs create mode 100644 RhythmBullet/Zer01HD/Utilities/UI/Modular/Modules/TextLabel.cs diff --git a/RhythmBullet/RhythmBullet.csproj b/RhythmBullet/RhythmBullet.csproj index e325f64..15c2cfe 100644 --- a/RhythmBullet/RhythmBullet.csproj +++ b/RhythmBullet/RhythmBullet.csproj @@ -73,7 +73,7 @@ - + diff --git a/RhythmBullet/Zer01HD/Utilities/UI/Modular/Modules/Text.cs b/RhythmBullet/Zer01HD/Utilities/UI/Modular/Modules/Text.cs deleted file mode 100644 index 9d1c6bd..0000000 --- a/RhythmBullet/Zer01HD/Utilities/UI/Modular/Modules/Text.cs +++ /dev/null @@ -1,112 +0,0 @@ -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; -using RhythmBullet.Zer01HD.Utilities.Camera; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RhythmBullet.Zer01HD.UI.Modular.Modules -{ - public class Text : UIModule - { - private SpriteFont font; - private float scale; - private Vector2 position; - private string text; - private Vector2 textSize; - public bool autoWrap; - public bool autoScale; - public string DisplayedText - { - get - { - return text; - } - set - { - textSize = font.MeasureString(value); - text = value; - } - } - public Text(string displayedText, SpriteFont font, int height) - { - bounds.Height = height; - this.font = font; - } - - public override void Update(GameTime gameTime) - { - position.X = bounds.X; - position.Y = bounds.Y; - - if (autoWrap) - { - AttemptToWrapText(); - } - - if (autoScale) - { - AttemptToScaleFont(); - } - - base.Update(gameTime); - } - - public override void Draw(SpriteBatch batch) - { - batch.DrawString(font, DisplayedText, position, color, 0f, origin, scale, SpriteEffects.None, 0f); - base.Draw(batch); - } - - public void AttemptToScaleFont() - { - if (textSize.X * scale > bounds.Width || textSize.X * scale < bounds.Width) - { - scale = bounds.Width / textSize.X; - } - - if (textSize.Y * scale > bounds.Height || textSize.Y *scale > bounds.Height) - { - scale = bounds.Height / textSize.Y; - } - } - - public void RemoveLineBreaks() - { - DisplayedText = DisplayedText.Replace("\n", " "); - - } - - public void AttemptToWrapText(bool unwrap = false) - { - if (unwrap) RemoveLineBreaks(); - if (textSize.X * scale > bounds.Width) - { - text = text.Replace("\n", " "); - string[] words = text.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; - } - } - DisplayedText = stringBuilder.ToString(); - } - } - } -} diff --git a/RhythmBullet/Zer01HD/Utilities/UI/Modular/Modules/TextLabel.cs b/RhythmBullet/Zer01HD/Utilities/UI/Modular/Modules/TextLabel.cs new file mode 100644 index 0000000..5543a27 --- /dev/null +++ b/RhythmBullet/Zer01HD/Utilities/UI/Modular/Modules/TextLabel.cs @@ -0,0 +1,146 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using RhythmBullet.Zer01HD.Utilities.Camera; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RhythmBullet.Zer01HD.UI.Modular.Modules +{ + 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; + } + + set + { + displayedText = value; + modifiedTextSize = font.MeasureString(value); + } + } + public string Text + { + get + { + return originalText; + } + set + { + modifiedTextSize = font.MeasureString(value); + originalText = value; + displayedText = value; + } + } + + public TextLabel(string text, SpriteFont font, int height) + { + Text = text; + bounds.Height = height; + 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); + } + while (font.MeasureString(stringBuilder).X *scale > bounds.Width); + + ModifiedText = stringBuilder.ToString(); + } + } + + public void AttemptToScaleFont() + { + if (modifiedTextSize.X * scale > bounds.Width || modifiedTextSize.X * scale < bounds.Width) + { + scale = bounds.Width / modifiedTextSize.X; + } + + if (modifiedTextSize.Y * scale > bounds.Height || modifiedTextSize.Y * scale > bounds.Height) + { + 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(); + } + } + } +}