53_LANDiscovery.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. -- Chat example
  2. -- This sample demonstrates:
  3. -- - Starting up a network server or connecting to it
  4. -- - Implementing simple chat functionality with network messages
  5. require "LuaScripts/Utilities/Sample"
  6. local startServer = nil;
  7. local stopServer = nil;
  8. local refreshServerList = nil;
  9. local serverList = nil;
  10. -- Local server port
  11. local SERVER_PORT = 54654
  12. function Start()
  13. -- Execute the common startup for samples
  14. SampleStart()
  15. -- Enable OS cursor
  16. input.mouseVisible = true
  17. -- Create the user interface
  18. CreateUI()
  19. -- Set the mouse mode to use in the sample
  20. SampleInitMouseMode(MM_FREE)
  21. -- Subscribe to UI and network events
  22. SubscribeToEvents()
  23. end
  24. function CreateUI()
  25. SetLogoVisible(true) -- We need the full rendering window
  26. local uiStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
  27. -- Set style to the UI root so that elements will inherit it
  28. ui.root.defaultStyle = uiStyle
  29. local marginTop = 20
  30. CreateLabel("1. Start server", IntVector2(20, marginTop-20));
  31. startServer = CreateButton("Start server", 160, IntVector2(20, marginTop));
  32. stopServer = CreateButton("Stop server", 160, IntVector2(20, marginTop));
  33. stopServer.visible = false;
  34. marginTop = marginTop + 80;
  35. CreateLabel("2. Discover LAN servers", IntVector2(20, marginTop-20));
  36. refreshServerList = CreateButton("Search...", 160, IntVector2(20, marginTop));
  37. marginTop = marginTop + 80;
  38. CreateLabel("Local servers:", IntVector2(20, marginTop - 20));
  39. serverList = CreateLabel("", IntVector2(20, marginTop));
  40. end
  41. function SubscribeToEvents()
  42. SubscribeToEvent("NetworkHostDiscovered", "HandleNetworkHostDiscovered");
  43. SubscribeToEvent(startServer, "Released", "HandleStartServer");
  44. SubscribeToEvent(stopServer, "Released", "HandleStopServer");
  45. SubscribeToEvent(refreshServerList, "Released", "HandleDoNetworkDiscovery");
  46. end
  47. function CreateButton(text, width, position)
  48. local font = cache:GetResource("Font", "Fonts/Anonymous Pro.ttf")
  49. local button = ui.root:CreateChild("Button")
  50. button:SetStyleAuto()
  51. button:SetFixedWidth(width)
  52. button:SetFixedHeight(30)
  53. button:SetPosition(position.x, position.y)
  54. local buttonText = button:CreateChild("Text")
  55. buttonText:SetFont(font, 12)
  56. buttonText:SetAlignment(HA_CENTER, VA_CENTER)
  57. buttonText.text = text
  58. return button
  59. end
  60. function CreateLabel(text, position)
  61. local font = cache:GetResource("Font", "Fonts/Anonymous Pro.ttf")
  62. local label = ui.root:CreateChild("Text")
  63. label:SetFont(font, 12)
  64. label.color = Color(0.0, 1.0, 0.0)
  65. label:SetPosition(position.x, position.y)
  66. label.text = text
  67. return label
  68. end
  69. function HandleNetworkHostDiscovered(eventType, eventData)
  70. local text = serverList.text
  71. local data = eventData["Beacon"]:GetVariantMap()
  72. text = text .. "\n" .. data["Name"]:GetString() .. "(" .. data["Players"]:GetInt() .. ")" .. eventData["Address"]:GetString() .. ":" .. eventData["Port"]:GetInt()
  73. serverList:SetText(text)
  74. end
  75. function HandleStartServer(eventType, eventData)
  76. if network:StartServer(SERVER_PORT) == true
  77. then
  78. local data = VariantMap();
  79. data["Name"] = "Test server";
  80. data["Players"] = 100;
  81. -- Set data which will be sent to all who requests LAN network discovery
  82. network:SetDiscoveryBeacon(data);
  83. startServer.visible = false;
  84. stopServer.visible = true;
  85. end
  86. end
  87. function HandleStopServer(eventType, eventData)
  88. network:StopServer();
  89. startServer.visible = true;
  90. stopServer.visible = false;
  91. end
  92. function HandleDoNetworkDiscovery(eventType, eventData)
  93. network:DiscoverHosts(SERVER_PORT)
  94. serverList:SetText("")
  95. end
  96. -- Create XML patch instructions for screen joystick layout specific to this sample app
  97. function GetScreenJoystickPatchString()
  98. return
  99. "<patch>" ..
  100. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button2']]\">" ..
  101. " <attribute name=\"Is Visible\" value=\"false\" />" ..
  102. " </add>" ..
  103. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" ..
  104. " <attribute name=\"Is Visible\" value=\"false\" />" ..
  105. " </add>" ..
  106. "</patch>"
  107. end