38_SceneAndUILoad.lua 5.2 KB

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