NoticeDialog.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. using Client.MirControls;
  2. using Client.MirGraphics;
  3. using Client.MirSounds;
  4. using System.Text.RegularExpressions;
  5. using System.Globalization;
  6. using System.Diagnostics;
  7. namespace Client.MirScenes.Dialogs
  8. {
  9. public sealed class NoticeDialog : MirImageControl
  10. {
  11. public static Regex C = new Regex(@"{((.*?)\/(.*?))}");
  12. public static Regex L = new Regex(@"\(((.*?)\/(.*?))\)");
  13. public MirButton CloseButton, UpButton, DownButton, PositionBar, OkButton;
  14. public MirLabel[] TextLabel;
  15. public List<MirLabel> TextButtons;
  16. public MirImageControl TitleLabel;
  17. public MirLabel NameLabel;
  18. public Notice Notice = new Notice();
  19. Font font = new Font(Settings.FontName, 10F);
  20. public List<string> CurrentLines = new List<string>();
  21. private int _index = 0;
  22. public int MaximumLines = 19;
  23. public NoticeDialog()
  24. {
  25. Index = 961;
  26. Library = Libraries.Prguse;
  27. Location = new Point((Settings.ScreenWidth - Size.Width) / 2, (Settings.ScreenHeight - Size.Height) / 3);
  28. TextLabel = new MirLabel[40];
  29. TextButtons = new List<MirLabel>();
  30. MouseWheel += LoginNoticeDialog_MouseWheel;
  31. Sort = true;
  32. NameLabel = new MirLabel
  33. {
  34. Text = "",
  35. Parent = this,
  36. Font = new Font(Settings.FontName, 10F, FontStyle.Bold),
  37. ForeColour = Color.BurlyWood,
  38. Location = new Point(30, 6),
  39. AutoSize = true
  40. };
  41. CloseButton = new MirButton
  42. {
  43. Index = 360,
  44. HoverIndex = 361,
  45. PressedIndex = 362,
  46. Library = Libraries.Prguse2,
  47. Parent = this,
  48. Location = new Point(289, 3),
  49. Sound = SoundList.ButtonA,
  50. };
  51. CloseButton.Click += (o, e) => Hide();
  52. OkButton = new MirButton
  53. {
  54. Enabled = true,
  55. HoverIndex = 194,
  56. Index = 193,
  57. Library = Libraries.Title,
  58. Location = new Point(120, 436),
  59. Parent = this,
  60. PressedIndex = 195,
  61. };
  62. OkButton.Click += (o, e) => Hide();
  63. UpButton = new MirButton
  64. {
  65. Index = 470,
  66. HoverIndex = 471,
  67. PressedIndex = 472,
  68. Library = Libraries.Prguse2,
  69. Parent = this,
  70. Size = new Size(16, 14),
  71. Location = new Point(293, 33),
  72. Sound = SoundList.ButtonA,
  73. Visible = false
  74. };
  75. UpButton.Click += (o, e) =>
  76. {
  77. if (_index <= 0) return;
  78. _index--;
  79. NewText(CurrentLines, false);
  80. UpdatePositionBar();
  81. };
  82. DownButton = new MirButton
  83. {
  84. Index = 473,
  85. HoverIndex = 474,
  86. Library = Libraries.Prguse2,
  87. PressedIndex = 475,
  88. Parent = this,
  89. Size = new Size(16, 14),
  90. Location = new Point(293, 418),
  91. Sound = SoundList.ButtonA,
  92. Visible = true
  93. };
  94. DownButton.Click += (o, e) =>
  95. {
  96. if (_index + MaximumLines >= CurrentLines.Count) return;
  97. _index++;
  98. NewText(CurrentLines, false);
  99. UpdatePositionBar();
  100. };
  101. PositionBar = new MirButton
  102. {
  103. Index = 205,
  104. HoverIndex = 206,
  105. PressedIndex = 206,
  106. Library = Libraries.Prguse2,
  107. Location = new Point(293, 46),
  108. Parent = this,
  109. Movable = true,
  110. Sound = SoundList.None,
  111. Visible = true
  112. };
  113. PositionBar.OnMoving += PositionBar_OnMoving;
  114. }
  115. private void PositionBar_OnMoving(object sender, MouseEventArgs e)
  116. {
  117. int x = 293;
  118. int y = PositionBar.Location.Y;
  119. if (y >= 399) y = 399;
  120. if (y <= 46) y = 46;
  121. int location = y - 46;
  122. int interval = 400 / (CurrentLines.Count - MaximumLines);
  123. double yPoint = location / interval;
  124. _index = Convert.ToInt16(Math.Floor(yPoint));
  125. NewText(CurrentLines, false);
  126. PositionBar.Location = new Point(x, y);
  127. }
  128. private void LoginNoticeDialog_MouseWheel(object sender, MouseEventArgs e)
  129. {
  130. int count = e.Delta / SystemInformation.MouseWheelScrollDelta;
  131. if (_index == 0 && count >= 0) return;
  132. if (_index == CurrentLines.Count - 1 && count <= 0) return;
  133. if (CurrentLines.Count <= MaximumLines) return;
  134. _index -= count;
  135. if (_index < 0) _index = 0;
  136. if (_index + MaximumLines > CurrentLines.Count - 1) _index = CurrentLines.Count - MaximumLines;
  137. NewText(CurrentLines, false);
  138. UpdatePositionBar();
  139. }
  140. private void UpdatePositionBar()
  141. {
  142. if (CurrentLines.Count <= MaximumLines) return;
  143. int interval = 400 / (CurrentLines.Count - MaximumLines);
  144. int x = 293;
  145. int y = 46 + (_index * interval);
  146. if (y >= 399) y = 399;
  147. if (y <= 46) y = 46;
  148. PositionBar.Location = new Point(x, y);
  149. }
  150. public void Update(Notice notice)
  151. {
  152. this.Notice = notice;
  153. List<string> temp = new List<string>();
  154. if (string.IsNullOrWhiteSpace(notice.Message))
  155. {
  156. return;
  157. }
  158. string[] lines = notice.Message.Split(new string[] { "\r\n" }, StringSplitOptions.None);
  159. foreach (string line in lines)
  160. {
  161. temp.Add(line);
  162. }
  163. NewText(temp);
  164. Visible = true;
  165. }
  166. public void NewText(List<string> lines, bool resetIndex = true)
  167. {
  168. if (resetIndex)
  169. {
  170. _index = 0;
  171. CurrentLines = lines;
  172. UpdatePositionBar();
  173. }
  174. if (resetIndex)
  175. {
  176. if (Index != -1)
  177. {
  178. MaximumLines = 19;
  179. if (lines.Count > MaximumLines)
  180. {
  181. UpButton.Visible = true;
  182. DownButton.Visible = true;
  183. PositionBar.Visible = true;
  184. }
  185. else
  186. {
  187. UpButton.Visible = false;
  188. DownButton.Visible = false;
  189. PositionBar.Visible = false;
  190. }
  191. }
  192. else
  193. {
  194. MaximumLines = 19;
  195. UpButton.Visible = false;
  196. DownButton.Visible = false;
  197. PositionBar.Visible = false;
  198. }
  199. }
  200. for (int i = 0; i < TextButtons.Count; i++)
  201. TextButtons[i].Dispose();
  202. for (int i = 0; i < TextLabel.Length; i++)
  203. {
  204. if (TextLabel[i] != null) TextLabel[i].Text = "";
  205. }
  206. TextButtons.Clear();
  207. if (!string.IsNullOrEmpty(Notice.Title))
  208. {
  209. NameLabel.Text = Notice.Title;
  210. }
  211. int lastLine = lines.Count > MaximumLines ? ((MaximumLines + _index) > lines.Count ? lines.Count : (MaximumLines + _index)) : lines.Count;
  212. for (int i = _index; i < lastLine; i++)
  213. {
  214. TextLabel[i] = new MirLabel
  215. {
  216. Font = font,
  217. DrawFormat = TextFormatFlags.WordBreak,
  218. Visible = true,
  219. Parent = this,
  220. Size = new Size(420, 20),
  221. Location = new Point(25, 50 + (i - _index) * 20),
  222. NotControl = true
  223. };
  224. if (i >= lines.Count)
  225. {
  226. TextLabel[i].Text = string.Empty;
  227. continue;
  228. }
  229. string currentLine = lines[i];
  230. List<Match> matchList = C.Matches(currentLine).Cast<Match>().ToList();
  231. matchList.AddRange(L.Matches(currentLine).Cast<Match>());
  232. int oldLength = currentLine.Length;
  233. foreach (Match match in matchList.OrderBy(o => o.Index).ToList())
  234. {
  235. int offSet = oldLength - currentLine.Length;
  236. Capture capture = match.Groups[1].Captures[0];
  237. string txt = match.Groups[2].Captures[0].Value;
  238. string action = match.Groups[3].Captures[0].Value;
  239. currentLine = currentLine.Remove(capture.Index - 1 - offSet, capture.Length + 2).Insert(capture.Index - 1 - offSet, txt);
  240. string text = currentLine.Substring(0, capture.Index - 1 - offSet) + " ";
  241. Size size = TextRenderer.MeasureText(CMain.Graphics, text, TextLabel[i].Font, TextLabel[i].Size, TextFormatFlags.TextBoxControl);
  242. if (L.Match(match.Value).Success)
  243. {
  244. NewLink(txt, action, TextLabel[i].Location.Add(new Point(size.Width - 11, 0)));
  245. }
  246. if (C.Match(match.Value).Success)
  247. {
  248. NewColour(txt, action, TextLabel[i].Location.Add(new Point(size.Width - 11, 0)));
  249. }
  250. }
  251. TextLabel[i].Text = currentLine;
  252. TextLabel[i].MouseWheel += LoginNoticeDialog_MouseWheel;
  253. }
  254. OkButton.BringToFront();
  255. }
  256. private void NewLink(string text, string link, Point p)
  257. {
  258. MirLabel temp = new MirLabel
  259. {
  260. AutoSize = true,
  261. Visible = true,
  262. Parent = this,
  263. Location = p,
  264. Text = text,
  265. ForeColour = Color.Yellow,
  266. Sound = SoundList.ButtonC,
  267. Font = font
  268. };
  269. temp.MouseEnter += (o, e) => temp.ForeColour = Color.Red;
  270. temp.MouseLeave += (o, e) => temp.ForeColour = Color.Yellow;
  271. temp.MouseDown += (o, e) => temp.ForeColour = Color.Yellow;
  272. temp.MouseUp += (o, e) => temp.ForeColour = Color.Red;
  273. temp.Click += (o, e) =>
  274. {
  275. if (link.StartsWith("http://", true, CultureInfo.InvariantCulture))
  276. {
  277. System.Diagnostics.Process.Start(new ProcessStartInfo
  278. {
  279. FileName = link,
  280. UseShellExecute = true
  281. });
  282. }
  283. };
  284. temp.MouseWheel += LoginNoticeDialog_MouseWheel;
  285. TextButtons.Add(temp);
  286. }
  287. private void NewColour(string text, string colour, Point p)
  288. {
  289. Color textColour = Color.FromName(colour);
  290. MirLabel temp = new MirLabel
  291. {
  292. AutoSize = true,
  293. Visible = true,
  294. Parent = this,
  295. Location = p,
  296. Text = text,
  297. ForeColour = textColour,
  298. Font = font
  299. };
  300. temp.MouseWheel += LoginNoticeDialog_MouseWheel;
  301. TextButtons.Add(temp);
  302. }
  303. }
  304. }