FriendDialog.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. using Client.MirControls;
  2. using Client.MirGraphics;
  3. using Client.MirNetwork;
  4. using Client.MirSounds;
  5. using C = ClientPackets;
  6. namespace Client.MirScenes.Dialogs
  7. {
  8. public sealed class FriendDialog : MirImageControl
  9. {
  10. public MirImageControl TitleLabel, FriendLabel, BlacklistLabel;
  11. public MirLabel PageNumberLabel;
  12. public MirButton CloseButton, PreviousButton, NextButton;
  13. public MirButton AddButton, RemoveButton, MemoButton, EmailButton, WhisperButton;
  14. public FriendRow[] Rows = new FriendRow[12];
  15. public List<ClientFriend> Friends = new List<ClientFriend>();
  16. private ClientFriend SelectedFriend = null;
  17. private bool _tempBlockedTab = false;
  18. private bool _blockedTab = false;
  19. public int SelectedIndex = 0;
  20. public int StartIndex = 0;
  21. public int Page = 0;
  22. public FriendDialog()
  23. {
  24. Index = 199;
  25. Library = Libraries.Title;
  26. Movable = true;
  27. Sort = true;
  28. Location = Center;
  29. TitleLabel = new MirImageControl
  30. {
  31. Index = 6,
  32. Library = Libraries.Title,
  33. Location = new Point(18, 8),
  34. Parent = this
  35. };
  36. FriendLabel = new MirImageControl
  37. {
  38. Index = 163,
  39. Library = Libraries.Title,
  40. Location = new Point(10, 34),
  41. Parent = this,
  42. Sound = SoundList.ButtonA,
  43. };
  44. FriendLabel.Click += (o, e) =>
  45. {
  46. _tempBlockedTab = false;
  47. UpdateDisplay();
  48. };
  49. BlacklistLabel = new MirImageControl
  50. {
  51. Index = 167,
  52. Library = Libraries.Title,
  53. Location = new Point(128, 34),
  54. Parent = this,
  55. Sound = SoundList.ButtonA,
  56. };
  57. BlacklistLabel.Click += (o, e) =>
  58. {
  59. _tempBlockedTab = true;
  60. UpdateDisplay();
  61. };
  62. PageNumberLabel = new MirLabel
  63. {
  64. Text = "",
  65. Parent = this,
  66. Size = new Size(83, 17),
  67. Location = new Point(87, 216),
  68. DrawFormat = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter
  69. };
  70. #region Buttons
  71. PreviousButton = new MirButton
  72. {
  73. Index = 240,
  74. HoverIndex = 241,
  75. PressedIndex = 242,
  76. Library = Libraries.Prguse2,
  77. Parent = this,
  78. Size = new Size(16, 16),
  79. Location = new Point(70, 218),
  80. Sound = SoundList.ButtonA,
  81. };
  82. PreviousButton.Click += (o, e) =>
  83. {
  84. Page--;
  85. if (Page < 0) Page = 0;
  86. StartIndex = Rows.Length * Page;
  87. Update();
  88. };
  89. NextButton = new MirButton
  90. {
  91. Index = 243,
  92. HoverIndex = 244,
  93. PressedIndex = 245,
  94. Library = Libraries.Prguse2,
  95. Parent = this,
  96. Size = new Size(16, 16),
  97. Location = new Point(171, 218),
  98. Sound = SoundList.ButtonA,
  99. };
  100. NextButton.Click += (o, e) =>
  101. {
  102. Page++;
  103. if (Page > Friends.Count() / Rows.Length) Page = Friends.Count() / Rows.Length;
  104. StartIndex = Rows.Length * Page;
  105. Update();
  106. };
  107. CloseButton = new MirButton
  108. {
  109. HoverIndex = 361,
  110. Index = 360,
  111. Location = new Point(237, 3),
  112. Library = Libraries.Prguse2,
  113. Parent = this,
  114. PressedIndex = 362,
  115. Sound = SoundList.ButtonA,
  116. };
  117. CloseButton.Click += (o, e) => Hide();
  118. AddButton = new MirButton
  119. {
  120. Index = 554,
  121. HoverIndex = 555,
  122. PressedIndex = 556,
  123. Library = Libraries.Prguse,
  124. Location = new Point(60, 241),
  125. Parent = this,
  126. Sound = SoundList.ButtonA,
  127. Hint = GameLanguage.ClientTextMap.GetLocalization(ClientTextKeys.AddFriend)
  128. };
  129. AddButton.Click += (o, e) =>
  130. {
  131. string message = GameLanguage.ClientTextMap.GetLocalization(ClientTextKeys.FriendEnterAddName);
  132. if (_blockedTab)
  133. {
  134. message = GameLanguage.ClientTextMap.GetLocalization(ClientTextKeys.FriendEnterBlockName);
  135. }
  136. MirInputBox inputBox = new MirInputBox(message);
  137. inputBox.OKButton.Click += (o1, e1) =>
  138. {
  139. Network.Enqueue(new C.AddFriend { Name = inputBox.InputTextBox.Text, Blocked = _blockedTab });
  140. inputBox.Dispose();
  141. };
  142. inputBox.Show();
  143. };
  144. RemoveButton = new MirButton
  145. {
  146. Index = 557,
  147. HoverIndex = 558,
  148. PressedIndex = 559,
  149. Library = Libraries.Prguse,
  150. Location = new Point(88, 241),
  151. Parent = this,
  152. Sound = SoundList.ButtonA,
  153. Hint = GameLanguage.ClientTextMap.GetLocalization(ClientTextKeys.RemoveFriend),
  154. };
  155. RemoveButton.Click += (o, e) =>
  156. {
  157. if (SelectedFriend == null) return;
  158. MirMessageBox messageBox = new MirMessageBox(GameLanguage.ClientTextMap.GetLocalization((ClientTextKeys.ConfirmRemoveItem), SelectedFriend.Name), MirMessageBoxButtons.YesNo);
  159. messageBox.YesButton.Click += (o1, e1) =>
  160. {
  161. Network.Enqueue(new C.RemoveFriend { CharacterIndex = SelectedFriend.Index });
  162. messageBox.Dispose();
  163. };
  164. messageBox.Show();
  165. };
  166. MemoButton = new MirButton
  167. {
  168. Index = 560,
  169. HoverIndex = 561,
  170. PressedIndex = 562,
  171. Library = Libraries.Prguse,
  172. Location = new Point(116, 241),
  173. Parent = this,
  174. Sound = SoundList.ButtonA,
  175. Hint = GameLanguage.ClientTextMap.GetLocalization(ClientTextKeys.FriendMemo)
  176. };
  177. MemoButton.Click += (o, e) =>
  178. {
  179. if (SelectedFriend == null) return;
  180. GameScene.Scene.MemoDialog.Friend = SelectedFriend;
  181. GameScene.Scene.MemoDialog.Show();
  182. };
  183. EmailButton = new MirButton
  184. {
  185. Index = 563,
  186. HoverIndex = 564,
  187. PressedIndex = 565,
  188. Library = Libraries.Prguse,
  189. Location = new Point(144, 241),
  190. Parent = this,
  191. Sound = SoundList.ButtonA,
  192. Hint = GameLanguage.ClientTextMap.GetLocalization(ClientTextKeys.FriendMail),
  193. };
  194. EmailButton.Click += (o, e) =>
  195. {
  196. if (SelectedFriend == null) return;
  197. GameScene.Scene.MailComposeLetterDialog.ComposeMail(SelectedFriend.Name);
  198. };
  199. WhisperButton = new MirButton
  200. {
  201. Index = 566,
  202. HoverIndex = 567,
  203. PressedIndex = 568,
  204. Library = Libraries.Prguse,
  205. Location = new Point(172, 241),
  206. Parent = this,
  207. Sound = SoundList.ButtonA,
  208. Hint = GameLanguage.ClientTextMap.GetLocalization(ClientTextKeys.FriendWhisper)
  209. };
  210. WhisperButton.Click += (o, e) =>
  211. {
  212. if (SelectedFriend == null) return;
  213. if (!SelectedFriend.Online)
  214. {
  215. GameScene.Scene.ChatDialog.ReceiveChat(GameLanguage.ClientTextMap.GetLocalization(ClientTextKeys.PlayerNotOnline), ChatType.System);
  216. return;
  217. }
  218. GameScene.Scene.ChatDialog.ChatTextBox.SetFocus();
  219. GameScene.Scene.ChatDialog.ChatTextBox.Text = "/" + SelectedFriend.Name + " ";
  220. GameScene.Scene.ChatDialog.ChatTextBox.Visible = true;
  221. GameScene.Scene.ChatDialog.ChatTextBox.TextBox.SelectionLength = 0;
  222. GameScene.Scene.ChatDialog.ChatTextBox.TextBox.SelectionStart = GameScene.Scene.ChatDialog.ChatTextBox.Text.Length;
  223. };
  224. #endregion
  225. }
  226. private void UpdateDisplay()
  227. {
  228. if (!Visible) return;
  229. if (_blockedTab != _tempBlockedTab)
  230. {
  231. _blockedTab = _tempBlockedTab;
  232. if (_blockedTab)
  233. {
  234. FriendLabel.Index = 164;
  235. BlacklistLabel.Index = 166;
  236. }
  237. else
  238. {
  239. FriendLabel.Index = 163;
  240. BlacklistLabel.Index = 167;
  241. }
  242. Update();
  243. GameScene.Scene.MemoDialog.Hide();
  244. GameScene.Scene.DisposeMemoLabel();
  245. }
  246. }
  247. public void Update(bool clearSelection = true)
  248. {
  249. if (clearSelection)
  250. SelectedFriend = null;
  251. for (int i = 0; i < Rows.Length; i++)
  252. {
  253. if (Rows[i] != null) Rows[i].Dispose();
  254. Rows[i] = null;
  255. }
  256. List<ClientFriend> filteredFriends = new List<ClientFriend>();
  257. if (_blockedTab)
  258. filteredFriends = Friends.Where(e => e.Blocked).ToList();
  259. else
  260. filteredFriends = Friends.Where(e => !e.Blocked).ToList();
  261. int maxPage = filteredFriends.Count / Rows.Length + 1;
  262. if (maxPage < 1) maxPage = 1;
  263. PageNumberLabel.Text = (Page + 1) + " / " + maxPage;
  264. int maxIndex = filteredFriends.Count - 1;
  265. if (StartIndex > maxIndex) StartIndex = maxIndex;
  266. if (StartIndex < 0) StartIndex = 0;
  267. for (int i = 0; i < Rows.Length; i++)
  268. {
  269. if (i + StartIndex >= filteredFriends.Count) break;
  270. if (Rows[i] != null)
  271. Rows[i].Dispose();
  272. Rows[i] = new FriendRow
  273. {
  274. Friend = filteredFriends[i + StartIndex],
  275. Location = new Point((i % 2) * 115 + 16, 55 + ((i) / 2) * 22),
  276. Parent = this,
  277. };
  278. Rows[i].Click += (o, e) =>
  279. {
  280. FriendRow row = (FriendRow)o;
  281. if (row.Friend != SelectedFriend)
  282. {
  283. SelectedFriend = row.Friend;
  284. SelectedIndex = FindSelectedIndex();
  285. UpdateRows();
  286. }
  287. };
  288. if (SelectedFriend != null)
  289. {
  290. if (SelectedIndex == i)
  291. {
  292. SelectedFriend = Rows[i].Friend;
  293. }
  294. }
  295. }
  296. }
  297. public void UpdateRows()
  298. {
  299. if (SelectedFriend == null)
  300. {
  301. if (Rows[0] == null) return;
  302. SelectedFriend = Rows[0].Friend;
  303. }
  304. for (int i = 0; i < Rows.Length; i++)
  305. {
  306. if (Rows[i] == null) continue;
  307. Rows[i].Selected = false;
  308. if (Rows[i].Friend == SelectedFriend)
  309. {
  310. Rows[i].Selected = true;
  311. }
  312. Rows[i].UpdateInterface();
  313. }
  314. }
  315. public int FindSelectedIndex()
  316. {
  317. int selectedIndex = 0;
  318. if (SelectedFriend != null)
  319. {
  320. for (int i = 0; i < Rows.Length; i++)
  321. {
  322. if (Rows[i] == null || SelectedFriend != Rows[i].Friend) continue;
  323. selectedIndex = i;
  324. }
  325. }
  326. return selectedIndex;
  327. }
  328. public override void Hide()
  329. {
  330. if (!Visible) return;
  331. Visible = false;
  332. GameScene.Scene.MemoDialog.Hide();
  333. }
  334. public override void Show()
  335. {
  336. if (Visible) return;
  337. Visible = true;
  338. UpdateDisplay();
  339. Network.Enqueue(new C.RefreshFriends());
  340. }
  341. }
  342. public sealed class FriendRow : MirControl
  343. {
  344. public ClientFriend Friend;
  345. public MirLabel NameLabel, OnlineLabel;
  346. public bool Selected = false;
  347. public FriendRow()
  348. {
  349. Sound = SoundList.ButtonA;
  350. Size = new Size(115, 17);
  351. BeforeDraw += FriendRow_BeforeDraw;
  352. NameLabel = new MirLabel
  353. {
  354. Location = new Point(0, 0),
  355. Size = new Size(115, 17),
  356. BackColour = Color.Empty,
  357. DrawFormat = TextFormatFlags.VerticalCenter,
  358. Parent = this,
  359. NotControl = true,
  360. };
  361. UpdateInterface();
  362. }
  363. void FriendRow_BeforeDraw(object sender, EventArgs e)
  364. {
  365. UpdateInterface();
  366. }
  367. public void UpdateInterface()
  368. {
  369. if (Friend == null) return;
  370. NameLabel.Text = Friend.Name;
  371. if (Friend.Online)
  372. {
  373. NameLabel.ForeColour = Color.Green;
  374. }
  375. else
  376. {
  377. NameLabel.ForeColour = Color.White;
  378. }
  379. if (Selected)
  380. {
  381. NameLabel.BackColour = Color.Gray;
  382. }
  383. else
  384. {
  385. NameLabel.BackColour = Color.Empty;
  386. }
  387. }
  388. protected override void OnMouseEnter()
  389. {
  390. if (Friend == null || Friend.Memo.Length < 1) return;
  391. base.OnMouseEnter();
  392. GameScene.Scene.CreateMemoLabel(Friend);
  393. }
  394. protected override void OnMouseLeave()
  395. {
  396. base.OnMouseLeave();
  397. GameScene.Scene.DisposeMemoLabel();
  398. }
  399. protected override void Dispose(bool disposing)
  400. {
  401. base.Dispose(disposing);
  402. Friend = null;
  403. NameLabel = null;
  404. Selected = false;
  405. }
  406. }
  407. public sealed class MemoDialog : MirImageControl
  408. {
  409. //public MirImageControl TitleLabel;
  410. public MirTextBox MemoTextBox;
  411. public MirButton CloseButton, OKButton, CancelButton;
  412. public ClientFriend Friend;
  413. public MemoDialog()
  414. {
  415. Index = 209;
  416. Library = Libraries.Title;
  417. Movable = true;
  418. Sort = true;
  419. Location = Center;
  420. MemoTextBox = new MirTextBox
  421. {
  422. ForeColour = Color.White,
  423. Parent = this,
  424. Font = new Font(Settings.FontName, 8F),
  425. Location = new Point(15, 30),
  426. Size = new Size(165, 100),
  427. };
  428. MemoTextBox.MultiLine();
  429. OKButton = new MirButton
  430. {
  431. Index = 382,
  432. HoverIndex = 383,
  433. PressedIndex = 384,
  434. Parent = this,
  435. Library = Libraries.Title,
  436. Sound = SoundList.ButtonA,
  437. Location = new Point(30, 133)
  438. };
  439. OKButton.Click += (o, e) =>
  440. {
  441. Network.Enqueue(new C.AddMemo { CharacterIndex = Friend.Index, Memo = MemoTextBox.Text });
  442. Hide();
  443. };
  444. CancelButton = new MirButton
  445. {
  446. Index = 385,
  447. HoverIndex = 386,
  448. PressedIndex = 387,
  449. Parent = this,
  450. Library = Libraries.Title,
  451. Sound = SoundList.ButtonA,
  452. Location = new Point(115, 133)
  453. };
  454. CancelButton.Click += (o, e) => Hide();
  455. #region Buttons
  456. CloseButton = new MirButton
  457. {
  458. HoverIndex = 361,
  459. Index = 360,
  460. Location = new Point(168, 3),
  461. Library = Libraries.Prguse2,
  462. Parent = this,
  463. PressedIndex = 362,
  464. Sound = SoundList.ButtonA,
  465. };
  466. CloseButton.Click += (o, e) => Hide();
  467. #endregion
  468. }
  469. public override void Show()
  470. {
  471. if (Visible) return;
  472. Visible = true;
  473. if (Friend == null)
  474. {
  475. Hide();
  476. return;
  477. }
  478. MemoTextBox.Text = Friend.Memo;
  479. MemoTextBox.SetFocus();
  480. MemoTextBox.TextBox.SelectionLength = 0;
  481. MemoTextBox.TextBox.SelectionStart = MemoTextBox.Text.Length;
  482. }
  483. }
  484. }