FormChat.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using CommonRPG.Protocol.Client;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace CommonRPG.Client.Win32.Forms
  12. {
  13. public partial class FormChat : Form
  14. {
  15. private readonly RPGClient client;
  16. public FormChat(RPGClient client)
  17. {
  18. this.client = client;
  19. InitializeComponent();
  20. this.comboBox1.Text = "world";
  21. richTextBox.LinkClicked += OnClickLink;
  22. this.client.GameClient.Listen<ClientChatNotify>(OnChatNotify);
  23. }
  24. protected override void OnClosing(CancelEventArgs e)
  25. {
  26. base.OnClosing(e);
  27. e.Cancel = true;
  28. this.Hide();
  29. }
  30. public void OnClickLink(object sender, LinkClickedEventArgs e)
  31. {
  32. this.comboBox1.Text = e.LinkText;
  33. }
  34. private void OnChatNotify(ClientChatNotify notify)
  35. {
  36. this.richTextBox.SelectedText = GetChannelTag(notify.channel_type);
  37. this.richTextBox.InsertLink(notify.from_name, notify.from_uuid);
  38. if (notify.channel_type == ClientChatRequest.CHANNEL_TYPE_INVALID)
  39. {
  40. this.richTextBox.SelectedText = " to you";
  41. }
  42. this.richTextBox.SelectedText = ":" + notify.content + "\n";
  43. }
  44. private void button1_Click(object sender, EventArgs e)
  45. {
  46. if (string.IsNullOrEmpty(this.comboBox1.Text))
  47. {
  48. MessageBox.Show("发送对象不能为空");
  49. return;
  50. }
  51. if (!string.IsNullOrEmpty(this.textBox1.Text))
  52. {
  53. ClientChatRequest req = new ClientChatRequest();
  54. req.content = this.textBox1.Text;
  55. if (this.comboBox1.Text.Equals("world", StringComparison.OrdinalIgnoreCase))
  56. {
  57. req.channel_type = ClientChatRequest.CHANNEL_TYPE_WORLD;
  58. }
  59. else if (this.comboBox1.Text.Equals("trade", StringComparison.OrdinalIgnoreCase))
  60. {
  61. req.channel_type = ClientChatRequest.CHANNEL_TYPE_TRADE;
  62. }
  63. else if (this.comboBox1.Text.Equals("guild", StringComparison.OrdinalIgnoreCase))
  64. {
  65. req.channel_type = ClientChatRequest.CHANNEL_TYPE_GUILD;
  66. }
  67. else if (this.comboBox1.Text.Equals("team", StringComparison.OrdinalIgnoreCase))
  68. {
  69. req.channel_type = ClientChatRequest.CHANNEL_TYPE_TEAM;
  70. }
  71. else
  72. {
  73. req.channel_type = ClientChatRequest.CHANNEL_TYPE_INVALID;
  74. req.to_uuid = this.comboBox1.Text.Split('#')[1];
  75. }
  76. this.client.GameClient.Request<ClientChatResponse>(req, (err, rsp) =>
  77. {
  78. if (rsp.IsSuccess)
  79. {
  80. this.richTextBox.SelectedText = GetChannelTag(req.channel_type);
  81. this.richTextBox.SelectedText = "you";
  82. if (req.to_uuid != null)
  83. {
  84. this.richTextBox.SelectedText = " to ";
  85. this.richTextBox.InsertLink(this.comboBox1.Text.Split('#')[0], req.to_uuid);
  86. }
  87. this.richTextBox.SelectedText = ":" + req.content + "\n";
  88. }
  89. else
  90. {
  91. this.richTextBox.SelectedText = "! errcode:" + rsp.s2c_code + "\n";
  92. }
  93. });
  94. }
  95. this.textBox1.Text = "";
  96. }
  97. private string GetChannelTag(short channel_type)
  98. {
  99. switch (channel_type)
  100. {
  101. case ClientChatRequest.CHANNEL_TYPE_INVALID:
  102. return "[whisper]";
  103. case ClientChatRequest.CHANNEL_TYPE_WORLD:
  104. return "[world]";
  105. case ClientChatRequest.CHANNEL_TYPE_TRADE:
  106. return "[trade]";
  107. case ClientChatRequest.CHANNEL_TYPE_GUILD:
  108. return "[guild]";
  109. case ClientChatRequest.CHANNEL_TYPE_TEAM:
  110. return "[team]";
  111. case ClientChatRequest.CHANNEL_TYPE_BATTLE:
  112. return "[battle]";
  113. case ClientChatRequest.CHANNEL_TYPE_AREA:
  114. return "[area]";
  115. }
  116. return "[error_channel]";
  117. }
  118. private void textBox1_TextChanged(object sender, EventArgs e)
  119. {
  120. }
  121. private void FormChat_Load(object sender, EventArgs e)
  122. {
  123. }
  124. }
  125. }