// // Copyright (c) 2008-2020 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "LANDiscovery.h" #include // Undefine Windows macro, as our Connection class has a function called SendMessage #ifdef SendMessage #undef SendMessage #endif URHO3D_DEFINE_APPLICATION_MAIN(LANDiscovery) LANDiscovery::LANDiscovery(Context* context) : Sample(context) { } void LANDiscovery::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 LANDiscovery::CreateUI() { SetLogoVisible(true); // 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); int marginTop = 20; CreateLabel("1. Start server", IntVector2(20, marginTop-20)); startServer_ = CreateButton("Start server", 160, IntVector2(20, marginTop)); stopServer_ = CreateButton("Stop server", 160, IntVector2(20, marginTop)); stopServer_->SetVisible(false); // Create client connection related fields marginTop += 80; CreateLabel("2. Discover LAN servers", IntVector2(20, marginTop-20)); refreshServerList_ = CreateButton("Search...", 160, IntVector2(20, marginTop)); marginTop += 80; CreateLabel("Local servers:", IntVector2(20, marginTop - 20)); serverList_ = CreateLabel("", IntVector2(20, marginTop)); // 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 LANDiscovery::SubscribeToEvents() { SubscribeToEvent(E_NETWORKHOSTDISCOVERED, URHO3D_HANDLER(LANDiscovery, HandleNetworkHostDiscovered)); SubscribeToEvent(startServer_, "Released", URHO3D_HANDLER(LANDiscovery, HandleStartServer)); SubscribeToEvent(stopServer_, "Released", URHO3D_HANDLER(LANDiscovery, HandleStopServer)); SubscribeToEvent(refreshServerList_, "Released", URHO3D_HANDLER(LANDiscovery, HandleDoNetworkDiscovery)); } Button* LANDiscovery::CreateButton(const String& text, int width, IntVector2 position) { auto* cache = GetSubsystem(); auto* font = cache->GetResource("Fonts/Anonymous Pro.ttf"); auto* button = GetSubsystem()->GetRoot()->CreateChild