public GUIMessage(string text, Color color, Vector2 worldPosition, Vector2 velocity, float lifeTime, Alignment textAlignment = Alignment.Center, ScalableFont font = null) { coloredText = new ColoredText(text, color, false); WorldSpace = true; pos = worldPosition; Timer = lifeTime; Velocity = velocity; this.lifeTime = lifeTime; Font = font; size = font.MeasureString(text); Origin = new Vector2((int)(0.5f * size.X), (int)(0.5f * size.Y)); if (textAlignment.HasFlag(Alignment.Left)) { Origin.X -= size.X * 0.5f; } if (textAlignment.HasFlag(Alignment.Right)) { Origin.X += size.X * 0.5f; } if (textAlignment.HasFlag(Alignment.Top)) { Origin.Y -= size.Y * 0.5f; } if (textAlignment.HasFlag(Alignment.Bottom)) { Origin.Y += size.Y * 0.5f; } }
public static bool DrawButton(SpriteBatch sb, Rectangle rect, string text, Color color, bool isHoldable = false) { bool clicked = false; if (rect.Contains(PlayerInput.MousePosition)) { clicked = PlayerInput.LeftButtonHeld(); color = clicked ? new Color((int)(color.R * 0.8f), (int)(color.G * 0.8f), (int)(color.B * 0.8f), color.A) : new Color((int)(color.R * 1.2f), (int)(color.G * 1.2f), (int)(color.B * 1.2f), color.A); if (!isHoldable) { clicked = PlayerInput.LeftButtonClicked(); } } DrawRectangle(sb, rect, color, true); Vector2 origin; try { origin = Font.MeasureString(text) / 2; } catch { origin = Vector2.Zero; } Font.DrawString(sb, text, new Vector2(rect.Center.X, rect.Center.Y), Color.White, 0.0f, origin, 1.0f, SpriteEffects.None, 0.0f); return(clicked); }
private Vector2 InflateSize(ref Point size, string label, ScalableFont font) { Vector2 textSize = font.MeasureString(label); size.X = Math.Max((int)Math.Ceiling(textSize.X), size.X); size.Y += (int)Math.Ceiling(textSize.Y); return(textSize); }
public GUIMessage(string text, Color color, float lifeTime, ScalableFont font = null) { coloredText = new ColoredText(text, color, false, false); this.lifeTime = lifeTime; Timer = lifeTime; size = font.MeasureString(text); Origin = new Vector2(0, size.Y * 0.5f); Font = font; }
public static string LimitString(string str, ScalableFont font, int maxWidth) { if (maxWidth <= 0 || string.IsNullOrWhiteSpace(str)) { return(""); } float currWidth = font.MeasureString("...").X; for (int i = 0; i < str.Length; i++) { currWidth += font.MeasureString(str[i].ToString()).X; if (currWidth > maxWidth) { return(str.Substring(0, Math.Max(i - 2, 1)) + "..."); } } return(str); }
public static void DrawString(SpriteBatch sb, Vector2 pos, string text, Color color, Color?backgroundColor = null, int backgroundPadding = 0, ScalableFont font = null) { if (font == null) { font = Font; } if (backgroundColor != null) { Vector2 textSize = font.MeasureString(text); DrawRectangle(sb, pos - Vector2.One * backgroundPadding, textSize + Vector2.One * 2.0f * backgroundPadding, (Color)backgroundColor, true); } font.DrawString(sb, text, pos, color); }
public static string WrapText(string text, float lineLength, ScalableFont font, float textScale = 1.0f) //TODO: could integrate this into the ScalableFont class directly { Vector2 textSize = font.MeasureString(text); if (textSize.X < lineLength) { return(text); } text = text.Replace("\n", " \n "); List <string> words = new List <string>(); string currWord = ""; for (int i = 0; i < text.Length; i++) { if (TextManager.IsCJK(text[i].ToString())) { if (currWord.Length > 0) { words.Add(currWord); currWord = ""; } words.Add(text[i].ToString()); } else if (text[i] == ' ') { if (currWord.Length > 0) { words.Add(currWord); currWord = ""; } } else { currWord += text[i]; } } if (currWord.Length > 0) { words.Add(currWord); currWord = ""; } StringBuilder wrappedText = new StringBuilder(); float linePos = 0f; Vector2 spaceSize = font.MeasureString(" ") * textScale; for (int i = 0; i < words.Count; ++i) { if (words[i].Length == 0) { //space } else if (string.IsNullOrWhiteSpace(words[i]) && words[i] != "\n") { continue; } Vector2 size = words[i].Length == 0 ? spaceSize : font.MeasureString(words[i]) * textScale; if (size.X > lineLength) { if (linePos == 0.0f) { wrappedText.AppendLine(words[i]); } else { do { if (words[i].Length == 0) { break; } wrappedText.Append(words[i][0]); words[i] = words[i].Remove(0, 1); linePos += size.X; } while (words[i].Length > 0 && (size = font.MeasureString((words[i][0]).ToString()) * textScale).X + linePos < lineLength); wrappedText.Append("\n"); linePos = 0.0f; i--; } continue; } if (linePos + size.X < lineLength) { wrappedText.Append(words[i]); if (words[i] == "\n") { linePos = 0.0f; } else { linePos += size.X + spaceSize.X; } } else { wrappedText.Append("\n"); wrappedText.Append(words[i]); linePos = size.X + spaceSize.X; } if (i < words.Count - 1 && !TextManager.IsCJK(words[i]) && !TextManager.IsCJK(words[i + 1])) { wrappedText.Append(" "); } } return(wrappedText.ToString().Replace(" \n ", "\n")); }
public static string WrapText(string text, float lineLength, ScalableFont font, float textScale = 1.0f, bool playerInput = false) //TODO: could integrate this into the ScalableFont class directly { Vector2 textSize = font.MeasureString(text); if (textSize.X <= lineLength) { return(text); } if (!playerInput) { text = text.Replace("\n", " \n "); } List <string> words = new List <string>(); string currWord = ""; for (int i = 0; i < text.Length; i++) { if (TextManager.IsCJK(text[i].ToString())) { if (currWord.Length > 0) { words.Add(currWord); currWord = ""; } words.Add(text[i].ToString()); } else if (text[i] == ' ') { if (currWord.Length > 0) { words.Add(currWord); currWord = ""; } words.Add(string.Empty); } else { currWord += text[i]; } } if (currWord.Length > 0) { words.Add(currWord); currWord = ""; } StringBuilder wrappedText = new StringBuilder(); float linePos = 0f; Vector2 spaceSize = font.MeasureString(" ") * textScale; for (int i = 0; i < words.Count; ++i) { string currentWord = words[i]; if (currentWord.Length == 0) { // space currentWord = " "; } else if (string.IsNullOrWhiteSpace(currentWord) && currentWord != "\n") { continue; } Vector2 size = words[i].Length == 0 ? spaceSize : font.MeasureString(currentWord) * textScale; if (size.X > lineLength) { float splitSize = 0.0f; List <string> splitWord = new List <string>() { string.Empty }; int k = 0; for (int j = 0; j < currentWord.Length; j++) { splitWord[k] += currentWord[j]; splitSize += (font.MeasureString(currentWord[j].ToString()) * textScale).X; if (splitSize + linePos > lineLength) { linePos = splitSize = 0.0f; splitWord[k] = splitWord[k].Remove(splitWord[k].Length - 1) + "\n"; j--; splitWord.Add(string.Empty); k++; } } for (int j = 0; j < splitWord.Count; j++) { wrappedText.Append(splitWord[j]); } linePos = splitSize; } else { if (linePos + size.X < lineLength) { wrappedText.Append(currentWord); if (currentWord == "\n") { linePos = 0.0f; } else { linePos += size.X; } } else { wrappedText.Append("\n"); wrappedText.Append(currentWord); linePos = size.X; } } } if (!playerInput) { return(wrappedText.ToString().Replace(" \n ", "\n")); } else { return(wrappedText.ToString()); } }
public static string WrapText(string text, float lineLength, ScalableFont font, float textScale = 1.0f) //TODO: could integrate this into the ScalableFont class directly { if (font.MeasureString(text).X < lineLength) { return(text); } text = text.Replace("\n", " \n "); string[] words = text.Split(' '); StringBuilder wrappedText = new StringBuilder(); float linePos = 0f; float spaceWidth = font.MeasureString(" ").X *textScale; for (int i = 0; i < words.Length; ++i) { if (string.IsNullOrWhiteSpace(words[i]) && words[i] != "\n") { continue; } Vector2 size = font.MeasureString(words[i]) * textScale; if (size.X > lineLength) { if (linePos == 0.0f) { wrappedText.AppendLine(words[i]); } else { do { if (words[i].Length == 0) { break; } wrappedText.Append(words[i][0]); words[i] = words[i].Remove(0, 1); linePos += size.X; } while (words[i].Length > 0 && (size = font.MeasureString((words[i][0]).ToString()) * textScale).X + linePos < lineLength); wrappedText.Append("\n"); linePos = 0.0f; i--; } continue; } if (linePos + size.X < lineLength) { wrappedText.Append(words[i]); if (words[i] == "\n") { linePos = 0.0f; } else { linePos += size.X + spaceWidth; } } else { wrappedText.Append("\n"); wrappedText.Append(words[i]); linePos = size.X + spaceWidth; } if (i < words.Length - 1) { wrappedText.Append(" "); } } return(wrappedText.ToString().Replace(" \n ", "\n")); }