// // Copyright (c) 2008-2014 the Urho3D project. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // #include "Audio.h" #include "Button.h" #include "Engine.h" #include "Font.h" #include "Graphics.h" #include "Input.h" #include "IOEvents.h" #include "LineEdit.h" #include "Log.h" #include "MemoryBuffer.h" #include "Network.h" #include "NetworkEvents.h" #include "ResourceCache.h" #include "Scene.h" #include "Sound.h" #include "Text.h" #include "UI.h" #include "UIEvents.h" #include "VectorBuffer.h" #include "Zone.h" #include "Chat.h" #include "DebugNew.h" // 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 = 32; // UDP port we will use const unsigned short CHAT_SERVER_PORT = 2345; 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(); } void Chat::CreateUI() { SetLogoVisible(false); // We need the full rendering window Graphics* graphics = GetSubsystem(); UIElement* root = GetSubsystem()->GetRoot(); ResourceCache* cache = GetSubsystem(); XMLFile* uiStyle = cache->GetResource("UI/DefaultStyle.xml"); // Set style to the UI root so that elements will inherit it root->SetDefaultStyle(uiStyle); Font* 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(); int rowHeight = chatHistoryText_->GetRowHeight(); // Row height would be zero if the font failed to load if (rowHeight) chatHistory_.Resize((graphics->GetHeight() - 20) / rowHeight); // 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, HANDLER(Chat, HandleSend)); SubscribeToEvent(sendButton_, E_RELEASED, HANDLER(Chat, HandleSend)); SubscribeToEvent(connectButton_, E_RELEASED, HANDLER(Chat, HandleConnect)); SubscribeToEvent(disconnectButton_, E_RELEASED, HANDLER(Chat, HandleDisconnect)); SubscribeToEvent(startServerButton_, E_RELEASED, HANDLER(Chat, HandleStartServer)); // Subscribe to log messages so that we can pipe them to the chat window SubscribeToEvent(E_LOGMESSAGE, HANDLER(Chat, HandleLogMessage)); // Subscribe to network events SubscribeToEvent(E_NETWORKMESSAGE, HANDLER(Chat, HandleNetworkMessage)); SubscribeToEvent(E_SERVERCONNECTED, HANDLER(Chat, HandleConnectionStatus)); SubscribeToEvent(E_SERVERDISCONNECTED, HANDLER(Chat, HandleConnectionStatus)); SubscribeToEvent(E_CONNECTFAILED, HANDLER(Chat, HandleConnectionStatus)); } Button* Chat::CreateButton(const String& text, int width) { ResourceCache* cache = GetSubsystem(); Font* font = cache->GetResource("Fonts/Anonymous Pro.ttf"); Button* button = buttonContainer_->CreateChild