MirScrollingLabel.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Text.RegularExpressions;
  2. namespace Client.MirControls
  3. {
  4. public class MirScrollingLabel : MirControl
  5. {
  6. private static readonly Regex R = new Regex(@"<(.*?/\@.*?)>");
  7. private static readonly Regex C = new Regex(@"{(.*?/.*?)}");
  8. private readonly MirLabel[] _textLabel;
  9. private readonly List<MirLabel> _textButtons;
  10. public int Index;
  11. public Font Font = new Font(Settings.FontName, 8F);
  12. public List<string> CurrentLines = new List<string>();
  13. public int VisibleLines = 8;
  14. public MirScrollingLabel()
  15. {
  16. _textLabel = new MirLabel[12];
  17. _textButtons = new List<MirLabel>();
  18. }
  19. public void NewText(List<string> lines, bool resetIndex = true)
  20. {
  21. if (resetIndex)
  22. {
  23. Index = 0;
  24. CurrentLines = lines;
  25. }
  26. foreach (MirLabel t in _textButtons)
  27. t.Dispose();
  28. foreach (MirLabel t in _textLabel.Where(t => t != null))
  29. t.Text = "";
  30. _textButtons.Clear();
  31. int lastLine = lines.Count > VisibleLines ? ((VisibleLines + Index) > lines.Count ? lines.Count : (VisibleLines + Index)) : lines.Count;
  32. for (int i = Index; i < lastLine; i++)
  33. {
  34. _textLabel[i - Index] = new MirLabel
  35. {
  36. Font = Font,
  37. DrawFormat = TextFormatFlags.WordBreak,
  38. Visible = true,
  39. Parent = this,
  40. Size = new Size(Size.Width, 20),
  41. Location = new Point(0, 0 + (i - Index) * 15),
  42. NotControl = true
  43. };
  44. if (i >= lines.Count)
  45. {
  46. _textLabel[i - Index].Text = string.Empty;
  47. continue;
  48. }
  49. string currentLine = lines[i];
  50. List<Match> matchList = R.Matches(currentLine).Cast<Match>().ToList();
  51. matchList.AddRange(C.Matches(currentLine).Cast<Match>());
  52. int oldLength = currentLine.Length;
  53. foreach (Match match in matchList.OrderBy(o => o.Index).ToList())
  54. {
  55. int offSet = oldLength - currentLine.Length;
  56. Capture capture = match.Groups[1].Captures[0];
  57. string[] values = capture.Value.Split('/');
  58. currentLine = currentLine.Remove(capture.Index - 1 - offSet, capture.Length + 2).Insert(capture.Index - 1 - offSet, values[0]);
  59. string text = currentLine.Substring(0, capture.Index - 1 - offSet) + " ";
  60. Size size = TextRenderer.MeasureText(CMain.Graphics, text, _textLabel[i - Index].Font, _textLabel[i - Index].Size, TextFormatFlags.TextBoxControl);
  61. //if (R.Match(match.Value).Success)
  62. // NewButton(values[0], values[1], TextLabel[i].Location.Add(new Point(size.Width - 10, 0)));
  63. if (C.Match(match.Value).Success)
  64. NewColour(values[0], values[1], _textLabel[i - Index].Location.Add(new Point(size.Width - 10, 0)));
  65. }
  66. _textLabel[i - Index].Text = currentLine;
  67. //TextLabel[i].MouseWheel += NPCDialog_MouseWheel;
  68. }
  69. }
  70. private void NewColour(string text, string colour, Point p)
  71. {
  72. Color textColour = Color.FromName(colour);
  73. MirLabel temp = new MirLabel
  74. {
  75. AutoSize = true,
  76. Visible = true,
  77. Parent = this,
  78. Location = p,
  79. Text = text,
  80. ForeColour = textColour,
  81. Font = Font
  82. };
  83. //temp.MouseWheel += NPCDialog_MouseWheel;
  84. _textButtons.Add(temp);
  85. }
  86. }
  87. }