/// <summary> /// this will take input like (color_code)01HELLO! and use it to write out colored text. /// </summary> /// <param name="s"></param> /// <param name="text_to_draw"></param> public void draw_text(SpriteBatch s, SpriteFont sf, string text_to_draw, int x_start, int y_start, int max_width, byte opacity) { string[] texts = text_to_draw.Split(Display.color_code[0]); int cur_width = 0; int cur_measure = 0; Color col = Color.White; col.A = opacity; int cur_col = 0; string combined = ""; for (int x = 0; x < texts.Length; x++) { combined = ""; if (texts[x].Length >= 2) { if ((byte)texts[x][0] >= 48 && (byte)texts[x][0] <= 57 && (byte)texts[x][1] >= 48 && (byte)texts[x][1] <= 57) { //we have a color code... remove it cur_col = int.Parse(texts[x].Substring(0, 2)); col = Acc.int_to_col(int.Parse(texts[x].Substring(0, 2))); col.A = opacity; texts[x] = texts[x].Substring(2); combined = texts[x]; } else { //not a color code, display the @ symbol combined = "@" + texts[x]; } } else if (x != 0) { combined = "@" + texts[x]; } if (combined != "") { cur_measure = (int)sf.MeasureString(combined).X; if (cur_width + cur_measure <= max_width) { s.DrawString(sf, combined, new Vector2(x_start + cur_width, y_start), Color.FromNonPremultiplied(col.R, col.G, col.B, opacity)); cur_width += cur_measure; } else if (cur_width < max_width) //adding the string is just too much! { int over = cur_width + cur_measure - max_width; string final = combined; int min_try = 0; int max_try = combined.Length; while (min_try + 1 < max_try) { final = combined.Substring(0, (min_try + max_try) / 2); cur_measure = (int)sf.MeasureString(final).X; over = cur_width + cur_measure - max_width; if (over > 0) { max_try = final.Length; } else { min_try = final.Length; } } s.DrawString(sf, final, new Vector2(x_start + cur_width, y_start), Color.FromNonPremultiplied(col.R, col.G, col.B, opacity)); if (drawing_messages > -1 && combined.Length > final.Length) { string precombined = combined.Substring(final.Length - 1); combined = "@" + cur_col.ToString().PadLeft(2, '0') + (combined.Substring(final.Length - 1).Trim()); while (x < texts.Length - 1) { x++; combined += "@" + texts[x]; precombined += "@" + texts[x]; } messages.Insert(drawing_messages, combined); messages[drawing_messages + 1] = messages[drawing_messages + 1].Replace(precombined, ""); start_show_messages = messages.Count - 1; } return; } else { return; } } } }
/// <summary> /// this will take input like (color_code)01HELLO! and use it to write out colored text. /// </summary> /// <param name="s"></param> /// <param name="text_to_draw"></param> public void draw_text(SpriteBatch s, string text_to_draw, int x_start, int y_start, int max_width, bool use_small_font) { if (!text_to_draw.Contains(Display.color_code[0]) || text_to_draw[0] != Display.color_code[0]) { text_to_draw = Display.color_code + "01" + text_to_draw; } string[] texts = text_to_draw.Split(Display.color_code[0]); int cur_width = 0; int cur_measure = 0; int col = 0; for (int x = 0; x < texts.GetLength(0); x++) { if (texts[x].Length > 2 && int.TryParse(texts[x].Substring(0, 2), out col)) { if (use_small_font) { cur_measure = (int)small_font.MeasureString(texts[x].Substring(2)).X; } else { cur_measure = (int)middle_font.MeasureString(texts[x].Substring(2)).X; } if (cur_width + cur_measure < max_width) { if (use_small_font) { s.DrawString(small_font, texts[x].Substring(2), new Vector2(x_start + cur_width, y_start), Acc.int_to_col(col)); } else { s.DrawString(middle_font, texts[x].Substring(2), new Vector2(x_start + cur_width, y_start), Acc.int_to_col(col)); } cur_width += cur_measure; } else if (cur_width < max_width) { int over = cur_width + cur_measure - max_width; over /= 12; //texts[x] = texts[x].Substring(0, texts[x].Length - (3 + over)) + "..."; if (use_small_font) { s.DrawString(small_font, texts[x].Substring(2), new Vector2(x_start + cur_width, y_start), Acc.int_to_col(col)); } else { s.DrawString(middle_font, texts[x].Substring(2), new Vector2(x_start + cur_width, y_start), Acc.int_to_col(col)); } cur_width += cur_measure; } } } }