using System; using System.Collections.Generic; using System.Linq; using System.Text; using VBXSE; namespace TalkingIsAFreeAction { public static class Textbox { public const float DEFAULT_TEXT_SPEED = 50; public const float QUICK_TEXT_SPEED = 400; public const int MAXIMUM_LENGTH = 184; public static Sprite textBox = null; public static GText gText = null; public static Sprite waitCursor = null; public static Sprite finalCursor = null; public static string printing = ""; public static float printed = 0; public static float textSpeed = 20; public static bool finishedPrinting = false; public static bool textIsFinal = false; public static Microsoft.Xna.Framework.Audio.SoundEffectInstance talkSound = null; public static void Initialize() { textBox = SpriteEngine.CreateSprite("textbox"); textBox.depth = -50; textBox.SetPosition(64, 24); textBox.positionLocked = true; textBox.enabled = false; gText = SpriteEngine.CreateGText("", 68, 28); gText.depth = -60; gText.color = Microsoft.Xna.Framework.Color.Black; gText.enabled = false; waitCursor = SpriteEngine.CreateMultiSprite(new string[] { "wait1", "wait2", "wait3", "wait4" }, (int)textBox.position.X + 180, (int)textBox.position.Y + 28, 1f); waitCursor.depth = -70; waitCursor.positionLocked = true; waitCursor.AddAnimation("waiting", new Animation("waiting", "wait1", 4, 200)); waitCursor.currentAnimation = "waiting"; waitCursor.enabled = false; finalCursor = SpriteEngine.CreateMultiSprite(new string[] { "final1", "final2", "final3", "final4" }, (int)textBox.position.X + 180, (int)textBox.position.Y + 28, 1f); finalCursor.depth = -71; //Just in case finalCursor.positionLocked = true; finalCursor.AddAnimation("finaling", new Animation("finaling", "final1", 4, 200)); finalCursor.currentAnimation = "finaling"; finalCursor.enabled = false; talkSound = Audio.PrepareSound("talk"); talkSound.IsLooped = true; } public static void PrintText(string text, bool final = false) { printing = text; printed = 0; textBox.enabled = true; gText.enabled = true; waitCursor.enabled = false; finalCursor.enabled = false; finishedPrinting = false; textSpeed = DEFAULT_TEXT_SPEED; textIsFinal = final; //Word wrapping! if (SpriteEngine.spriteFont.MeasureString(text).X > MAXIMUM_LENGTH) { string[] words = text.Split(new string[] { " " }, StringSplitOptions.None); int numWords = words.Length; int currentLength = MAXIMUM_LENGTH + 1; while (currentLength > MAXIMUM_LENGTH) { numWords--; StringBuilder toTry = new StringBuilder(); for (int i = 0; i < numWords; i++) { toTry.Append(words[i]); if(i < numWords - 1) toTry.Append(" "); } currentLength = Convert.ToInt32(SpriteEngine.spriteFont.MeasureString(toTry.ToString()).X); } StringBuilder finalText = new StringBuilder(); for (int i = 0; i < numWords; i++) { finalText.Append(words[i]); finalText.Append(" "); } finalText.Append("\n"); for (int i = numWords; i < words.Length; i++) { finalText.Append(words[i]); if (i < words.Length - 1) finalText.Append(" "); } printing = finalText.ToString(); } } public static void Poke() { if (!finishedPrinting) { textSpeed = QUICK_TEXT_SPEED; } else { Main.FinishText(); } } public static void Close() { printing = ""; printed = 0; textBox.enabled = false; gText.enabled = false; gText.text = ""; waitCursor.enabled = false; finalCursor.enabled = false; } public static void Update(float seconds) { if (printing == "") gText.text = ""; else { if (printed < printing.Length) { printed += textSpeed * seconds; if (talkSound.State != Microsoft.Xna.Framework.Audio.SoundState.Playing) { talkSound.Play(); } } if (printed >= printing.Length) { printed = printing.Length; if (textIsFinal) finalCursor.enabled = true; else waitCursor.enabled = true; finishedPrinting = true; if (talkSound.State == Microsoft.Xna.Framework.Audio.SoundState.Playing) { talkSound.Stop(true); } } gText.text = printing.Substring(0, (int)Math.Floor(printed)); } } } }