38_SceneAndUILoad.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. -- Scene & UI load example.
  2. -- This sample demonstrates:
  3. -- - Loading a scene from a file and showing it
  4. -- - Loading a UI layout from a file and showing it
  5. -- - Subscribing to the UI layout's events
  6. require "LuaScripts/Utilities/Sample"
  7. function Start()
  8. -- Execute the common startup for samples
  9. SampleStart()
  10. -- Create the scene content
  11. CreateScene()
  12. -- Create the UI content
  13. CreateUI()
  14. -- Setup the viewport for displaying the scene
  15. SetupViewport()
  16. -- Subscribe to global events for camera movement
  17. SubscribeToEvents()
  18. end
  19. function CreateScene()
  20. scene_ = Scene()
  21. -- Load scene content prepared in the editor (XML format). GetFile() returns an open file from the resource system
  22. -- which scene.LoadXML() will read
  23. local file = cache:GetFile("Scenes/SceneLoadExample.xml")
  24. scene_:LoadXML(file)
  25. -- In Lua the file returned by GetFile() needs to be deleted manually
  26. file:delete()
  27. -- Create the camera (not included in the scene file)
  28. cameraNode = scene_:CreateChild("Camera")
  29. cameraNode:CreateComponent("Camera")
  30. -- Set an initial position for the camera scene node above the plane
  31. cameraNode.position = Vector3(0.0, 2.0, -10.0)
  32. end
  33. function CreateUI()
  34. -- Set up global UI style into the root UI element
  35. local style = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
  36. ui.root.defaultStyle = style
  37. -- Create a Cursor UI element because we want to be able to hide and show it at will. When hidden, the mouse cursor will
  38. -- control the camera, and when visible, it will interact with the UI
  39. local cursor = ui.root:CreateChild("Cursor")
  40. cursor:SetStyleAuto()
  41. ui.cursor = cursor
  42. -- Set starting position of the cursor at the rendering window center
  43. cursor:SetPosition(graphics.width / 2, graphics.height / 2)
  44. -- Load UI content prepared in the editor and add to the UI hierarchy
  45. local layoutRoot = ui:LoadLayout(cache:GetResource("XMLFile", "UI/UILoadExample.xml"))
  46. ui.root:AddChild(layoutRoot)
  47. -- Subscribe to button actions (toggle scene lights when pressed then released)
  48. local button = layoutRoot:GetChild("ToggleLight1", true)
  49. if button ~= nil then
  50. SubscribeToEvent(button, "Released", "ToggleLight1")
  51. end
  52. button = layoutRoot:GetChild("ToggleLight2", true)
  53. if button ~= nil then
  54. SubscribeToEvent(button, "Released", "ToggleLight2")
  55. end
  56. end
  57. function ToggleLight1()
  58. local lightNode = scene_:GetChild("Light1", true)
  59. if lightNode ~= nil then
  60. lightNode.enabled = not lightNode.enabled
  61. end
  62. end
  63. function ToggleLight2()
  64. local lightNode = scene_:GetChild("Light2", true)
  65. if lightNode ~= nil then
  66. lightNode.enabled = not lightNode.enabled
  67. end
  68. end
  69. function SetupViewport()
  70. -- Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  71. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  72. renderer:SetViewport(0, viewport)
  73. end
  74. function SubscribeToEvents()
  75. -- Subscribe HandleUpdate() function for camera motion
  76. SubscribeToEvent("Update", "HandleUpdate")
  77. end
  78. function HandleUpdate(eventType, eventData)
  79. -- Take the frame time step, which is stored as a float
  80. local timeStep = eventData:GetFloat("TimeStep")
  81. -- Move the camera, scale movement with time step
  82. MoveCamera(timeStep)
  83. end
  84. function MoveCamera(timeStep)
  85. -- Right mouse button controls mouse cursor visibility: hide when pressed
  86. ui.cursor.visible = not input:GetMouseButtonDown(MOUSEB_RIGHT)
  87. -- Do not move if the UI has a focused element
  88. if ui.focusElement ~= nil then
  89. return
  90. end
  91. -- Movement speed as world units per second
  92. local MOVE_SPEED = 20.0
  93. -- Mouse sensitivity as degrees per pixel
  94. local MOUSE_SENSITIVITY = 0.1
  95. -- Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  96. -- Only move the camera when the cursor is hidden
  97. if not ui.cursor.visible then
  98. local mouseMove = input.mouseMove
  99. yaw = yaw + MOUSE_SENSITIVITY * mouseMove.x
  100. pitch = pitch + MOUSE_SENSITIVITY * mouseMove.y
  101. pitch = Clamp(pitch, -90.0, 90.0)
  102. -- Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  103. cameraNode.rotation = Quaternion(pitch, yaw, 0.0)
  104. end
  105. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  106. if input:GetKeyDown(KEY_W) then
  107. cameraNode:Translate(Vector3(0.0, 0.0, 1.0) * MOVE_SPEED * timeStep)
  108. end
  109. if input:GetKeyDown(KEY_S) then
  110. cameraNode:Translate(Vector3(0.0, 0.0, -1.0) * MOVE_SPEED * timeStep)
  111. end
  112. if input:GetKeyDown(KEY_A) then
  113. cameraNode:Translate(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  114. end
  115. if input:GetKeyDown(KEY_D) then
  116. cameraNode:Translate(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  117. end
  118. end