LANDiscovery.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Audio/Audio.h>
  4. #include <Urho3D/Audio/Sound.h>
  5. #include <Urho3D/Engine/Engine.h>
  6. #include <Urho3D/Graphics/Graphics.h>
  7. #include <Urho3D/Graphics/Zone.h>
  8. #include <Urho3D/Input/Input.h>
  9. #include <Urho3D/IO/IOEvents.h>
  10. #include <Urho3D/IO/Log.h>
  11. #include <Urho3D/IO/MemoryBuffer.h>
  12. #include <Urho3D/IO/VectorBuffer.h>
  13. #include <Urho3D/Network/Network.h>
  14. #include <Urho3D/Network/NetworkEvents.h>
  15. #include <Urho3D/Resource/ResourceCache.h>
  16. #include <Urho3D/Scene/Scene.h>
  17. #include <Urho3D/UI/Button.h>
  18. #include <Urho3D/UI/Font.h>
  19. #include <Urho3D/UI/LineEdit.h>
  20. #include <Urho3D/UI/Text.h>
  21. #include <Urho3D/UI/UI.h>
  22. #include <Urho3D/UI/UIEvents.h>
  23. #include "LANDiscovery.h"
  24. #include <Urho3D/DebugNew.h>
  25. // Undefine Windows macro, as our Connection class has a function called SendMessage
  26. #ifdef SendMessage
  27. #undef SendMessage
  28. #endif
  29. URHO3D_DEFINE_APPLICATION_MAIN(LANDiscovery)
  30. LANDiscovery::LANDiscovery(Context* context) :
  31. Sample(context)
  32. {
  33. }
  34. void LANDiscovery::Start()
  35. {
  36. // Execute base class startup
  37. Sample::Start();
  38. // Enable OS cursor
  39. GetSubsystem<Input>()->SetMouseVisible(true);
  40. // Create the user interface
  41. CreateUI();
  42. // Subscribe to UI and network events
  43. SubscribeToEvents();
  44. // Set the mouse mode to use in the sample
  45. Sample::InitMouseMode(MM_FREE);
  46. }
  47. void LANDiscovery::CreateUI()
  48. {
  49. SetLogoVisible(true); // We need the full rendering window
  50. auto* graphics = GetSubsystem<Graphics>();
  51. UIElement* root = GetSubsystem<UI>()->GetRoot();
  52. auto* cache = GetSubsystem<ResourceCache>();
  53. auto* uiStyle = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  54. // Set style to the UI root so that elements will inherit it
  55. root->SetDefaultStyle(uiStyle);
  56. int marginTop = 20;
  57. CreateLabel("1. Start server", IntVector2(20, marginTop-20));
  58. startServer_ = CreateButton("Start server", 160, IntVector2(20, marginTop));
  59. stopServer_ = CreateButton("Stop server", 160, IntVector2(20, marginTop));
  60. stopServer_->SetVisible(false);
  61. // Create client connection related fields
  62. marginTop += 80;
  63. CreateLabel("2. Discover LAN servers", IntVector2(20, marginTop-20));
  64. refreshServerList_ = CreateButton("Search...", 160, IntVector2(20, marginTop));
  65. marginTop += 80;
  66. CreateLabel("Local servers:", IntVector2(20, marginTop - 20));
  67. serverList_ = CreateLabel("", IntVector2(20, marginTop));
  68. // No viewports or scene is defined. However, the default zone's fog color controls the fill color
  69. GetSubsystem<Renderer>()->GetDefaultZone()->SetFogColor(Color(0.0f, 0.0f, 0.1f));
  70. }
  71. void LANDiscovery::SubscribeToEvents()
  72. {
  73. SubscribeToEvent(E_NETWORKHOSTDISCOVERED, URHO3D_HANDLER(LANDiscovery, HandleNetworkHostDiscovered));
  74. SubscribeToEvent(startServer_, "Released", URHO3D_HANDLER(LANDiscovery, HandleStartServer));
  75. SubscribeToEvent(stopServer_, "Released", URHO3D_HANDLER(LANDiscovery, HandleStopServer));
  76. SubscribeToEvent(refreshServerList_, "Released", URHO3D_HANDLER(LANDiscovery, HandleDoNetworkDiscovery));
  77. }
  78. Button* LANDiscovery::CreateButton(const String& text, int width, IntVector2 position)
  79. {
  80. auto* cache = GetSubsystem<ResourceCache>();
  81. auto* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf");
  82. auto* button = GetSubsystem<UI>()->GetRoot()->CreateChild<Button>();
  83. button->SetStyleAuto();
  84. button->SetFixedWidth(width);
  85. button->SetFixedHeight(30);
  86. button->SetPosition(position);
  87. auto* buttonText = button->CreateChild<Text>();
  88. buttonText->SetFont(font, 12);
  89. buttonText->SetAlignment(HA_CENTER, VA_CENTER);
  90. buttonText->SetText(text);
  91. return button;
  92. }
  93. Text* LANDiscovery::CreateLabel(const String& text, IntVector2 pos)
  94. {
  95. auto* cache = GetSubsystem<ResourceCache>();
  96. // Create log element to view latest logs from the system
  97. auto* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf");
  98. auto* label = GetSubsystem<UI>()->GetRoot()->CreateChild<Text>();
  99. label->SetFont(font, 12);
  100. label->SetColor(Color(0.0f, 1.0f, 0.0f));
  101. label->SetPosition(pos);
  102. label->SetText(text);
  103. return label;
  104. }
  105. void LANDiscovery::HandleNetworkHostDiscovered(StringHash eventType, VariantMap& eventData)
  106. {
  107. using namespace NetworkHostDiscovered;
  108. URHO3D_LOGINFO("Server discovered!");
  109. String text = serverList_->GetText();
  110. VariantMap data = eventData[P_BEACON].GetVariantMap();
  111. text += "\n" + data["Name"].GetString() + "(" + String(data["Players"].GetI32()) + ")" + eventData[P_ADDRESS].GetString() + ":" + String(eventData[P_PORT].GetI32());
  112. serverList_->SetText(text);
  113. }
  114. void LANDiscovery::HandleStartServer(StringHash eventType, VariantMap& eventData)
  115. {
  116. if (GetSubsystem<Network>()->StartServer(SERVER_PORT)) {
  117. VariantMap data;
  118. data["Name"] = "Test server";
  119. data["Players"] = 100;
  120. /// Set data which will be sent to all who requests LAN network discovery
  121. GetSubsystem<Network>()->SetDiscoveryBeacon(data);
  122. startServer_->SetVisible(false);
  123. stopServer_->SetVisible(true);
  124. }
  125. }
  126. void LANDiscovery::HandleStopServer(StringHash eventType, VariantMap& eventData)
  127. {
  128. GetSubsystem<Network>()->StopServer();
  129. startServer_->SetVisible(true);
  130. stopServer_->SetVisible(false);
  131. }
  132. void LANDiscovery::HandleDoNetworkDiscovery(StringHash eventType, VariantMap& eventData)
  133. {
  134. /// Pass in the port that should be checked
  135. GetSubsystem<Network>()->DiscoverHosts(SERVER_PORT);
  136. serverList_->SetText("");
  137. }