| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- #include "Scripts/Utilities/Network.as"
- Text@ chatHistoryText;
- UIElement@ buttonLayout;
- LineEdit@ textEdit;
- Button@ sendButton;
- Button@ connectButton;
- Button@ disconnectButton;
- Button@ startServerButton;
- XMLFile@ uiStyle;
- Font@ font;
- Array<String> chatHistory;
- bool inLogMessage = false;
- const int MSG_CHAT = 32;
- void Start()
- {
- SubscribeToEvent("NetworkMessage", "HandleNetworkMessage");
- if (engine.headless)
- OpenConsoleWindow();
- else
- {
- InitUI();
- SubscribeToEvent("LogMessage", "HandleLogMessage");
- SubscribeToEvent("KeyDown", "HandleKeyDown");
- SubscribeToEvent(textEdit, "TextFinished", "HandleTextFinished");
- SubscribeToEvent(sendButton, "Pressed", "HandleSend");
- SubscribeToEvent(connectButton, "Pressed", "HandleConnect");
- SubscribeToEvent(disconnectButton, "Pressed", "HandleDisconnect");
- SubscribeToEvent(startServerButton, "Pressed", "HandleStartServer");
- }
-
- ParseNetworkArguments();
- if (runServer)
- network.StartServer(serverPort);
- if (runClient)
- network.Connect(serverAddress, serverPort, null);
- }
- void InitUI()
- {
- uiStyle = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
- font = cache.GetResource("Font", "Fonts/Anonymous Pro.ttf");
- engine.CreateDebugHud();
- debugHud.style = uiStyle;
- debugHud.mode = DEBUGHUD_SHOW_PROFILER;
- debugHud.profilerText.opacity = 0.5;
- Cursor@ newCursor = Cursor("Cursor");
- newCursor.style = uiStyle;
- newCursor.position = IntVector2(graphics.width / 2, graphics.height / 2);
- ui.cursor = newCursor;
- chatHistoryText = Text();
- chatHistoryText.SetFont(font, 12);
- ui.root.AddChild(chatHistoryText);
- buttonLayout = UIElement();
- buttonLayout.SetFixedSize(graphics.width, 20);
- buttonLayout.position = IntVector2(0, graphics.height - 20);
- buttonLayout.layoutMode = LM_HORIZONTAL;
- ui.root.AddChild(buttonLayout);
- textEdit = LineEdit();
- textEdit.style = uiStyle;
- buttonLayout.AddChild(textEdit);
- sendButton = AddUIButton("Send", 70);
- connectButton = AddUIButton("Connect", 90);
- disconnectButton = AddUIButton("Disconnect", 100);
- startServerButton = AddUIButton("Start Server", 110);
- chatHistory.Resize((graphics.height - 20) / chatHistoryText.rowHeight);
-
- // No viewports or scene is defined. However, the default zone's fog color controls the fill color
- renderer.defaultZone.fogColor = Color(0, 0, 0.1);
- }
- Button@ AddUIButton(const String& text, int width)
- {
- Button@ button = Button();
- button.style = uiStyle;
- button.SetFixedWidth(width);
- Text@ buttonText = Text();
- buttonText.SetFont(font, 12);
- buttonText.horizontalAlignment = HA_CENTER;
- buttonText.verticalAlignment = VA_CENTER;
- buttonText.text = text;
-
- button.AddChild(buttonText);
- buttonLayout.AddChild(button);
- return button;
- }
- void AddChatRow(const String& row)
- {
- // If running in headless mode, only print the row to the log
- if (chatHistory.empty)
- {
- Print(row);
- return;
- }
- chatHistory.Erase(0);
- chatHistory.Push(row);
-
- String allRows;
- for (uint i = 0; i < chatHistory.length; ++i)
- allRows += chatHistory[i] + "\n";
- chatHistoryText.text = allRows;
- }
- void HandleTextFinished()
- {
- Connection@ serverConnection = network.serverConnection;
- if (serverConnection is null || !serverConnection.connected)
- HandleConnect();
- else
- HandleSend();
- }
- void HandleSend()
- {
- String chatText = textEdit.text.Trimmed();
- Connection@ serverConnection = network.serverConnection;
-
- if (!chatText.empty && serverConnection !is null)
- {
- VectorBuffer msg;
- msg.WriteString(chatText);
- serverConnection.SendMessage(MSG_CHAT, true, true, msg);
- textEdit.text = "";
- }
- }
- void HandleConnect()
- {
- String address = textEdit.text.Trimmed();
- if (!address.empty)
- {
- network.Connect(address, serverPort, null);
- textEdit.text = "";
- }
- }
- void HandleDisconnect()
- {
- network.Disconnect();
- }
- void HandleStartServer()
- {
- if (!network.serverRunning)
- network.StartServer(serverPort);
- }
- void HandleNetworkMessage(StringHash eventType, VariantMap& eventData)
- {
- Connection@ source = eventData["Connection"].GetConnection();
- if (eventData["MessageID"].GetInt() == MSG_CHAT)
- {
- VectorBuffer msg = eventData["Data"].GetBuffer();
- String chatText = msg.ReadString();
- // If we are client, only show the text. If we are server, broadcast to all clients with the sender address prepended
- bool broadcast = source !is network.serverConnection;
- if (!broadcast)
- AddChatRow(chatText);
- else
- {
- chatText = source.address + ":" + source.port + " " + chatText;
- AddChatRow(chatText);
- VectorBuffer newMsg;
- newMsg.WriteString(chatText);
- Array<Connection@> clients = network.clientConnections;
- for (uint i = 0; i < clients.length; ++i)
- clients[i].SendMessage(MSG_CHAT, true, true, newMsg);
- }
- }
- }
- void HandleLogMessage(StringHash eventType, VariantMap& eventData)
- {
- if (!inLogMessage)
- {
- inLogMessage = true;
- AddChatRow(eventData["Message"].GetString());
- inLogMessage = false;
- }
- }
- void HandleKeyDown(StringHash eventType, VariantMap& eventData)
- {
- if (eventData["Key"].GetInt() == KEY_ESC && ui.focusElement is null)
- engine.Exit();
- }
|