LANDiscovery.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Audio/Audio.h>
  23. #include <Urho3D/Audio/Sound.h>
  24. #include <Urho3D/Engine/Engine.h>
  25. #include <Urho3D/Graphics/Graphics.h>
  26. #include <Urho3D/Graphics/Zone.h>
  27. #include <Urho3D/Input/Input.h>
  28. #include <Urho3D/IO/IOEvents.h>
  29. #include <Urho3D/IO/Log.h>
  30. #include <Urho3D/IO/MemoryBuffer.h>
  31. #include <Urho3D/IO/VectorBuffer.h>
  32. #include <Urho3D/Network/Network.h>
  33. #include <Urho3D/Network/NetworkEvents.h>
  34. #include <Urho3D/Resource/ResourceCache.h>
  35. #include <Urho3D/Scene/Scene.h>
  36. #include <Urho3D/UI/Button.h>
  37. #include <Urho3D/UI/Font.h>
  38. #include <Urho3D/UI/LineEdit.h>
  39. #include <Urho3D/UI/Text.h>
  40. #include <Urho3D/UI/UI.h>
  41. #include <Urho3D/UI/UIEvents.h>
  42. #include "LANDiscovery.h"
  43. #include <Urho3D/DebugNew.h>
  44. // Undefine Windows macro, as our Connection class has a function called SendMessage
  45. #ifdef SendMessage
  46. #undef SendMessage
  47. #endif
  48. URHO3D_DEFINE_APPLICATION_MAIN(LANDiscovery)
  49. LANDiscovery::LANDiscovery(Context* context) :
  50. Sample(context)
  51. {
  52. }
  53. void LANDiscovery::Start()
  54. {
  55. // Execute base class startup
  56. Sample::Start();
  57. // Enable OS cursor
  58. GetSubsystem<Input>()->SetMouseVisible(true);
  59. // Create the user interface
  60. CreateUI();
  61. // Subscribe to UI and network events
  62. SubscribeToEvents();
  63. // Set the mouse mode to use in the sample
  64. Sample::InitMouseMode(MM_FREE);
  65. }
  66. void LANDiscovery::CreateUI()
  67. {
  68. SetLogoVisible(true); // We need the full rendering window
  69. auto* graphics = GetSubsystem<Graphics>();
  70. UIElement* root = GetSubsystem<UI>()->GetRoot();
  71. auto* cache = GetSubsystem<ResourceCache>();
  72. auto* uiStyle = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  73. // Set style to the UI root so that elements will inherit it
  74. root->SetDefaultStyle(uiStyle);
  75. int marginTop = 20;
  76. CreateLabel("1. Start server", IntVector2(20, marginTop-20));
  77. startServer_ = CreateButton("Start server", 160, IntVector2(20, marginTop));
  78. stopServer_ = CreateButton("Stop server", 160, IntVector2(20, marginTop));
  79. stopServer_->SetVisible(false);
  80. // Create client connection related fields
  81. marginTop += 80;
  82. CreateLabel("2. Discover LAN servers", IntVector2(20, marginTop-20));
  83. refreshServerList_ = CreateButton("Search...", 160, IntVector2(20, marginTop));
  84. marginTop += 80;
  85. CreateLabel("Local servers:", IntVector2(20, marginTop - 20));
  86. serverList_ = CreateLabel("", IntVector2(20, marginTop));
  87. // No viewports or scene is defined. However, the default zone's fog color controls the fill color
  88. GetSubsystem<Renderer>()->GetDefaultZone()->SetFogColor(Color(0.0f, 0.0f, 0.1f));
  89. }
  90. void LANDiscovery::SubscribeToEvents()
  91. {
  92. SubscribeToEvent(E_NETWORKHOSTDISCOVERED, URHO3D_HANDLER(LANDiscovery, HandleNetworkHostDiscovered));
  93. SubscribeToEvent(startServer_, "Released", URHO3D_HANDLER(LANDiscovery, HandleStartServer));
  94. SubscribeToEvent(stopServer_, "Released", URHO3D_HANDLER(LANDiscovery, HandleStopServer));
  95. SubscribeToEvent(refreshServerList_, "Released", URHO3D_HANDLER(LANDiscovery, HandleDoNetworkDiscovery));
  96. }
  97. Button* LANDiscovery::CreateButton(const String& text, int width, IntVector2 position)
  98. {
  99. auto* cache = GetSubsystem<ResourceCache>();
  100. auto* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf");
  101. auto* button = GetSubsystem<UI>()->GetRoot()->CreateChild<Button>();
  102. button->SetStyleAuto();
  103. button->SetFixedWidth(width);
  104. button->SetFixedHeight(30);
  105. button->SetPosition(position);
  106. auto* buttonText = button->CreateChild<Text>();
  107. buttonText->SetFont(font, 12);
  108. buttonText->SetAlignment(HA_CENTER, VA_CENTER);
  109. buttonText->SetText(text);
  110. return button;
  111. }
  112. Text* LANDiscovery::CreateLabel(const String& text, IntVector2 pos)
  113. {
  114. auto* cache = GetSubsystem<ResourceCache>();
  115. // Create log element to view latest logs from the system
  116. auto* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf");
  117. auto* label = GetSubsystem<UI>()->GetRoot()->CreateChild<Text>();
  118. label->SetFont(font, 12);
  119. label->SetColor(Color(0.0f, 1.0f, 0.0f));
  120. label->SetPosition(pos);
  121. label->SetText(text);
  122. return label;
  123. }
  124. void LANDiscovery::HandleNetworkHostDiscovered(StringHash eventType, VariantMap& eventData)
  125. {
  126. using namespace NetworkHostDiscovered;
  127. URHO3D_LOGINFO("Server discovered!");
  128. String text = serverList_->GetText();
  129. VariantMap data = eventData[P_BEACON].GetVariantMap();
  130. text += "\n" + data["Name"].GetString() + "(" + String(data["Players"].GetInt()) + ")" + eventData[P_ADDRESS].GetString() + ":" + String(eventData[P_PORT].GetInt());
  131. serverList_->SetText(text);
  132. }
  133. void LANDiscovery::HandleStartServer(StringHash eventType, VariantMap& eventData)
  134. {
  135. if (GetSubsystem<Network>()->StartServer(SERVER_PORT)) {
  136. VariantMap data;
  137. data["Name"] = "Test server";
  138. data["Players"] = 100;
  139. /// Set data which will be sent to all who requests LAN network discovery
  140. GetSubsystem<Network>()->SetDiscoveryBeacon(data);
  141. startServer_->SetVisible(false);
  142. stopServer_->SetVisible(true);
  143. }
  144. }
  145. void LANDiscovery::HandleStopServer(StringHash eventType, VariantMap& eventData)
  146. {
  147. GetSubsystem<Network>()->StopServer();
  148. startServer_->SetVisible(true);
  149. stopServer_->SetVisible(false);
  150. }
  151. void LANDiscovery::HandleDoNetworkDiscovery(StringHash eventType, VariantMap& eventData)
  152. {
  153. /// Pass in the port that should be checked
  154. GetSubsystem<Network>()->DiscoverHosts(SERVER_PORT);
  155. serverList_->SetText("");
  156. }