// Copyright (c) 2008-2023 the Urho3D project // License: MIT #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Chat.h" #include // Undefine Windows macro, as our Connection class has a function called SendMessage #ifdef SendMessage #undef SendMessage #endif // Identifier for the chat network messages const int MSG_CHAT = MSG_USER + 0; // UDP port we will use const unsigned short CHAT_SERVER_PORT = 2345; URHO3D_DEFINE_APPLICATION_MAIN(Chat) Chat::Chat(Context* context) : Sample(context) { } void Chat::Start() { // Execute base class startup Sample::Start(); // Enable OS cursor GetSubsystem()->SetMouseVisible(true); // Create the user interface CreateUI(); // Subscribe to UI and network events SubscribeToEvents(); // Set the mouse mode to use in the sample Sample::InitMouseMode(MM_FREE); } void Chat::CreateUI() { SetLogoVisible(false); // We need the full rendering window auto* graphics = GetSubsystem(); UIElement* root = GetSubsystem()->GetRoot(); auto* cache = GetSubsystem(); auto* uiStyle = cache->GetResource("UI/DefaultStyle.xml"); // Set style to the UI root so that elements will inherit it root->SetDefaultStyle(uiStyle); auto* font = cache->GetResource("Fonts/Anonymous Pro.ttf"); chatHistoryText_ = root->CreateChild(); chatHistoryText_->SetFont(font, 12); buttonContainer_ = root->CreateChild(); buttonContainer_->SetFixedSize(graphics->GetWidth(), 20); buttonContainer_->SetPosition(0, graphics->GetHeight() - 20); buttonContainer_->SetLayoutMode(LM_HORIZONTAL); textEdit_ = buttonContainer_->CreateChild(); textEdit_->SetStyleAuto(); sendButton_ = CreateButton("Send", 70); connectButton_ = CreateButton("Connect", 90); disconnectButton_ = CreateButton("Disconnect", 100); startServerButton_ = CreateButton("Start Server", 110); UpdateButtons(); float rowHeight = chatHistoryText_->GetRowHeight(); // Row height would be zero if the font failed to load if (rowHeight) { float numberOfRows = (graphics->GetHeight() - 100) / rowHeight; chatHistory_.Resize(static_cast(numberOfRows)); } // No viewports or scene is defined. However, the default zone's fog color controls the fill color GetSubsystem()->GetDefaultZone()->SetFogColor(Color(0.0f, 0.0f, 0.1f)); } void Chat::SubscribeToEvents() { // Subscribe to UI element events SubscribeToEvent(textEdit_, E_TEXTFINISHED, URHO3D_HANDLER(Chat, HandleSend)); SubscribeToEvent(sendButton_, E_RELEASED, URHO3D_HANDLER(Chat, HandleSend)); SubscribeToEvent(connectButton_, E_RELEASED, URHO3D_HANDLER(Chat, HandleConnect)); SubscribeToEvent(disconnectButton_, E_RELEASED, URHO3D_HANDLER(Chat, HandleDisconnect)); SubscribeToEvent(startServerButton_, E_RELEASED, URHO3D_HANDLER(Chat, HandleStartServer)); // Subscribe to log messages so that we can pipe them to the chat window SubscribeToEvent(E_LOGMESSAGE, URHO3D_HANDLER(Chat, HandleLogMessage)); // Subscribe to network events SubscribeToEvent(E_NETWORKMESSAGE, URHO3D_HANDLER(Chat, HandleNetworkMessage)); SubscribeToEvent(E_SERVERCONNECTED, URHO3D_HANDLER(Chat, HandleConnectionStatus)); SubscribeToEvent(E_SERVERDISCONNECTED, URHO3D_HANDLER(Chat, HandleConnectionStatus)); SubscribeToEvent(E_CONNECTFAILED, URHO3D_HANDLER(Chat, HandleConnectionStatus)); } Button* Chat::CreateButton(const String& text, int width) { auto* cache = GetSubsystem(); auto* font = cache->GetResource("Fonts/Anonymous Pro.ttf"); auto* button = buttonContainer_->CreateChild