Chat.as 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "Scripts/Utilities/Network.as"
  2. Text@ chatHistoryText;
  3. UIElement@ buttonLayout;
  4. LineEdit@ textEdit;
  5. Button@ sendButton;
  6. Button@ connectButton;
  7. Button@ disconnectButton;
  8. Button@ startServerButton;
  9. XMLFile@ uiStyle;
  10. Font@ font;
  11. Array<String> chatHistory;
  12. bool inLogMessage = false;
  13. const int MSG_CHAT = 32;
  14. void Start()
  15. {
  16. SubscribeToEvent("NetworkMessage", "HandleNetworkMessage");
  17. if (engine.headless)
  18. OpenConsoleWindow();
  19. else
  20. {
  21. InitUI();
  22. SubscribeToEvent("LogMessage", "HandleLogMessage");
  23. SubscribeToEvent("KeyDown", "HandleKeyDown");
  24. SubscribeToEvent(textEdit, "TextFinished", "HandleTextFinished");
  25. SubscribeToEvent(sendButton, "Pressed", "HandleSend");
  26. SubscribeToEvent(connectButton, "Pressed", "HandleConnect");
  27. SubscribeToEvent(disconnectButton, "Pressed", "HandleDisconnect");
  28. SubscribeToEvent(startServerButton, "Pressed", "HandleStartServer");
  29. }
  30. ParseNetworkArguments();
  31. if (runServer)
  32. network.StartServer(serverPort);
  33. if (runClient)
  34. network.Connect(serverAddress, serverPort, null);
  35. }
  36. void InitUI()
  37. {
  38. uiStyle = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  39. font = cache.GetResource("Font", "Fonts/Anonymous Pro.ttf");
  40. engine.CreateDebugHud();
  41. debugHud.style = uiStyle;
  42. debugHud.mode = DEBUGHUD_SHOW_PROFILER;
  43. debugHud.profilerText.opacity = 0.5;
  44. Cursor@ newCursor = Cursor("Cursor");
  45. newCursor.style = uiStyle;
  46. newCursor.position = IntVector2(graphics.width / 2, graphics.height / 2);
  47. ui.cursor = newCursor;
  48. chatHistoryText = Text();
  49. chatHistoryText.SetFont(font, 12);
  50. ui.root.AddChild(chatHistoryText);
  51. buttonLayout = UIElement();
  52. buttonLayout.SetFixedSize(graphics.width, 20);
  53. buttonLayout.position = IntVector2(0, graphics.height - 20);
  54. buttonLayout.layoutMode = LM_HORIZONTAL;
  55. ui.root.AddChild(buttonLayout);
  56. textEdit = LineEdit();
  57. textEdit.style = uiStyle;
  58. buttonLayout.AddChild(textEdit);
  59. sendButton = AddUIButton("Send", 70);
  60. connectButton = AddUIButton("Connect", 90);
  61. disconnectButton = AddUIButton("Disconnect", 100);
  62. startServerButton = AddUIButton("Start Server", 110);
  63. chatHistory.Resize((graphics.height - 20) / chatHistoryText.rowHeight);
  64. // No viewports or scene is defined. However, the default zone's fog color controls the fill color
  65. renderer.defaultZone.fogColor = Color(0, 0, 0.1);
  66. }
  67. Button@ AddUIButton(const String& text, int width)
  68. {
  69. Button@ button = Button();
  70. button.style = uiStyle;
  71. button.SetFixedWidth(width);
  72. Text@ buttonText = Text();
  73. buttonText.SetFont(font, 12);
  74. buttonText.horizontalAlignment = HA_CENTER;
  75. buttonText.verticalAlignment = VA_CENTER;
  76. buttonText.text = text;
  77. button.AddChild(buttonText);
  78. buttonLayout.AddChild(button);
  79. return button;
  80. }
  81. void AddChatRow(const String& row)
  82. {
  83. // If running in headless mode, only print the row to the log
  84. if (chatHistory.empty)
  85. {
  86. Print(row);
  87. return;
  88. }
  89. chatHistory.Erase(0);
  90. chatHistory.Push(row);
  91. String allRows;
  92. for (uint i = 0; i < chatHistory.length; ++i)
  93. allRows += chatHistory[i] + "\n";
  94. chatHistoryText.text = allRows;
  95. }
  96. void HandleTextFinished()
  97. {
  98. Connection@ serverConnection = network.serverConnection;
  99. if (serverConnection is null || !serverConnection.connected)
  100. HandleConnect();
  101. else
  102. HandleSend();
  103. }
  104. void HandleSend()
  105. {
  106. String chatText = textEdit.text.Trimmed();
  107. Connection@ serverConnection = network.serverConnection;
  108. if (!chatText.empty && serverConnection !is null)
  109. {
  110. VectorBuffer msg;
  111. msg.WriteString(chatText);
  112. serverConnection.SendMessage(MSG_CHAT, true, true, msg);
  113. textEdit.text = "";
  114. }
  115. }
  116. void HandleConnect()
  117. {
  118. String address = textEdit.text.Trimmed();
  119. if (!address.empty)
  120. {
  121. network.Connect(address, serverPort, null);
  122. textEdit.text = "";
  123. }
  124. }
  125. void HandleDisconnect()
  126. {
  127. network.Disconnect();
  128. }
  129. void HandleStartServer()
  130. {
  131. if (!network.serverRunning)
  132. network.StartServer(serverPort);
  133. }
  134. void HandleNetworkMessage(StringHash eventType, VariantMap& eventData)
  135. {
  136. Connection@ source = eventData["Connection"].GetConnection();
  137. if (eventData["MessageID"].GetInt() == MSG_CHAT)
  138. {
  139. VectorBuffer msg = eventData["Data"].GetBuffer();
  140. String chatText = msg.ReadString();
  141. // If we are client, only show the text. If we are server, broadcast to all clients with the sender address prepended
  142. bool broadcast = source !is network.serverConnection;
  143. if (!broadcast)
  144. AddChatRow(chatText);
  145. else
  146. {
  147. chatText = source.address + ":" + source.port + " " + chatText;
  148. AddChatRow(chatText);
  149. VectorBuffer newMsg;
  150. newMsg.WriteString(chatText);
  151. Array<Connection@> clients = network.clientConnections;
  152. for (uint i = 0; i < clients.length; ++i)
  153. clients[i].SendMessage(MSG_CHAT, true, true, newMsg);
  154. }
  155. }
  156. }
  157. void HandleLogMessage(StringHash eventType, VariantMap& eventData)
  158. {
  159. if (!inLogMessage)
  160. {
  161. inLogMessage = true;
  162. AddChatRow(eventData["Message"].GetString());
  163. inLogMessage = false;
  164. }
  165. }
  166. void HandleKeyDown(StringHash eventType, VariantMap& eventData)
  167. {
  168. if (eventData["Key"].GetInt() == KEY_ESC && ui.focusElement is null)
  169. engine.Exit();
  170. }