53_LANDiscovery.as 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // This first example, maintaining tradition, prints a "Hello World" message.
  2. // Furthermore it shows:
  3. // - Using the Sample utility functions as a base for the application
  4. // - Adding a Text element to the graphical user interface
  5. // - Subscribing to and handling of update events
  6. #include "Scripts/Utilities/Sample.as"
  7. Button@ startServer;
  8. Button@ stopServer;
  9. Button@ refreshServerList;
  10. Text@ serverList;
  11. const int SERVER_PORT = 54654;
  12. void Start()
  13. {
  14. // Execute the common startup for samples
  15. SampleStart();
  16. // Create "Hello World" Text
  17. CreateText();
  18. // Set the mouse mode to use in the sample
  19. SampleInitMouseMode(MM_FREE);
  20. // Finally, hook-up this HelloWorld instance to handle update events
  21. SubscribeToEvents();
  22. }
  23. void CreateText()
  24. {
  25. XMLFile@ uiStyle = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  26. // Set style to the UI root so that elements will inherit it
  27. ui.root.defaultStyle = uiStyle;
  28. int marginTop = 20;
  29. CreateLabel("1. Start server", IntVector2(20, marginTop-20));
  30. startServer = CreateButton("Start server", 160, IntVector2(20, marginTop));
  31. stopServer = CreateButton("Stop server", 160, IntVector2(20, marginTop));
  32. stopServer.visible = false;
  33. // Create client connection related fields
  34. marginTop += 80;
  35. CreateLabel("2. Discover LAN servers", IntVector2(20, marginTop-20));
  36. refreshServerList = CreateButton("Search...", 160, IntVector2(20, marginTop));
  37. marginTop += 80;
  38. CreateLabel("Local servers:", IntVector2(20, marginTop - 20));
  39. serverList = CreateLabel("", IntVector2(20, marginTop));
  40. }
  41. void SubscribeToEvents()
  42. {
  43. SubscribeToEvent("NetworkHostDiscovered", "HandleNetworkHostDiscovered");
  44. SubscribeToEvent(startServer, "Released", "HandleStartServer");
  45. SubscribeToEvent(stopServer, "Released", "HandleStopServer");
  46. SubscribeToEvent(refreshServerList, "Released", "HandleDoNetworkDiscovery");
  47. }
  48. Text@ CreateLabel(const String&in text, IntVector2 pos)
  49. {
  50. // Create log element to view latest logs from the system
  51. Font@ font = cache.GetResource("Font", "Fonts/Anonymous Pro.ttf");
  52. Text@ label = ui.root.CreateChild("Text");
  53. label.SetFont(font, 12);
  54. label.color = Color(0.0f, 1.0f, 0.0f);
  55. label.SetPosition(pos.x, pos.y);
  56. label.text = text;
  57. return label;
  58. }
  59. Button@ CreateButton(const String&in text, int width, IntVector2 pos)
  60. {
  61. Font@ font = cache.GetResource("Font", "Fonts/Anonymous Pro.ttf");
  62. Button@ button = ui.root.CreateChild("Button");
  63. button.SetStyleAuto();
  64. button.SetFixedWidth(width);
  65. button.SetFixedHeight(30);
  66. button.SetPosition(pos.x, pos.y);
  67. Text@ buttonText = button.CreateChild("Text");
  68. buttonText.SetFont(font, 12);
  69. buttonText.SetAlignment(HA_CENTER, VA_CENTER);
  70. buttonText.text = text;
  71. return button;
  72. }
  73. void HandleNetworkHostDiscovered(StringHash eventType, VariantMap& eventData)
  74. {
  75. log.Info("Server discovered!");
  76. String text = serverList.text;
  77. VariantMap data = eventData["Beacon"].GetVariantMap();
  78. text += "\n" + data["Name"].GetString() + "(" + String(data["Players"].GetInt()) + ")" + eventData["Address"].GetString() + ":" + String(eventData["Port"].GetInt());
  79. serverList.text = text;
  80. }
  81. void HandleStartServer(StringHash eventType, VariantMap& eventData)
  82. {
  83. if (network.StartServer(SERVER_PORT)) {
  84. VariantMap data;
  85. data["Name"] = "Test server";
  86. data["Players"] = 100;
  87. /// Set data which will be sent to all who requests LAN network discovery
  88. network.SetDiscoveryBeacon(data);
  89. startServer.visible = false;
  90. stopServer.visible = true;
  91. }
  92. }
  93. void HandleStopServer(StringHash eventType, VariantMap& eventData)
  94. {
  95. network.StopServer();
  96. startServer.visible = true;
  97. stopServer.visible = false;
  98. }
  99. void HandleDoNetworkDiscovery(StringHash eventType, VariantMap& eventData)
  100. {
  101. /// Pass in the port that should be checked
  102. network.DiscoverHosts(SERVER_PORT);
  103. serverList.text = "";
  104. }
  105. // Create XML patch instructions for screen joystick layout specific to this sample app
  106. String patchInstructions =
  107. "<patch>" +
  108. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" +
  109. " <attribute name=\"Is Visible\" value=\"false\" />" +
  110. " </add>" +
  111. "</patch>";