38_SceneAndUILoad.as 5.1 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. #include "Scripts/Utilities/Sample.as"
  7. void Start()
  8. {
  9. // Execute the common startup for samples
  10. SampleStart();
  11. // Create the scene content
  12. CreateScene();
  13. // Create the UI content and subscribe to UI events
  14. CreateUI();
  15. // Setup the viewport for displaying the scene
  16. SetupViewport();
  17. // Set the mouse mode to use in the sample
  18. SampleInitMouseMode(MM_RELATIVE);
  19. // Subscribe to global events for camera movement
  20. SubscribeToEvents();
  21. }
  22. void CreateScene()
  23. {
  24. scene_ = Scene();
  25. // Load scene content prepared in the editor (XML format). GetFile() returns an open file from the resource system
  26. // which scene.LoadXML() will read
  27. scene_.LoadXML(cache.GetFile("Scenes/SceneLoadExample.xml"));
  28. // Create the camera (not included in the scene file)
  29. cameraNode = scene_.CreateChild("Camera");
  30. cameraNode.CreateComponent("Camera");
  31. // Set an initial position for the camera scene node above the plane
  32. cameraNode.position = Vector3(0.0f, 2.0f, -10.0f);
  33. }
  34. void CreateUI()
  35. {
  36. // Set up global UI style into the root UI element
  37. XMLFile@ 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. Cursor@ cursor = 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. UIElement@ 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. Button@ button = layoutRoot.GetChild("ToggleLight1", true);
  51. if (button !is null)
  52. SubscribeToEvent(button, "Released", "ToggleLight1");
  53. button = layoutRoot.GetChild("ToggleLight2", true);
  54. if (button !is null)
  55. SubscribeToEvent(button, "Released", "ToggleLight2");
  56. }
  57. void ToggleLight1()
  58. {
  59. Node@ lightNode = scene_.GetChild("Light1", true);
  60. if (lightNode !is null)
  61. lightNode.enabled = !lightNode.enabled;
  62. }
  63. void ToggleLight2()
  64. {
  65. Node@ lightNode = scene_.GetChild("Light2", true);
  66. if (lightNode !is null)
  67. lightNode.enabled = !lightNode.enabled;
  68. }
  69. void SetupViewport()
  70. {
  71. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  72. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  73. renderer.viewports[0] = viewport;
  74. }
  75. void SubscribeToEvents()
  76. {
  77. // Subscribe HandleUpdate() function for camera motion
  78. SubscribeToEvent("Update", "HandleUpdate");
  79. }
  80. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  81. {
  82. // Take the frame time step, which is stored as a float
  83. float timeStep = eventData["TimeStep"].GetFloat();
  84. // Move the camera, scale movement with time step
  85. MoveCamera(timeStep);
  86. }
  87. void MoveCamera(float timeStep)
  88. {
  89. input.mouseVisible = input.mouseMode != MM_RELATIVE;
  90. bool mouseDown = input.mouseButtonDown[MOUSEB_RIGHT];
  91. // Override the MM_RELATIVE mouse grabbed settings, to allow interaction with UI
  92. input.mouseGrabbed = mouseDown;
  93. // Right mouse button controls mouse cursor visibility: hide when pressed
  94. ui.cursor.visible = !mouseDown;
  95. // Do not move if the UI has a focused element
  96. if (ui.focusElement !is null)
  97. return;
  98. // Movement speed as world units per second
  99. const float MOVE_SPEED = 20.0f;
  100. // Mouse sensitivity as degrees per pixel
  101. const float MOUSE_SENSITIVITY = 0.1f;
  102. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  103. // Only move the camera when the cursor is hidden
  104. if (!ui.cursor.visible)
  105. {
  106. IntVector2 mouseMove = input.mouseMove;
  107. yaw += MOUSE_SENSITIVITY * mouseMove.x;
  108. pitch += MOUSE_SENSITIVITY * mouseMove.y;
  109. pitch = Clamp(pitch, -90.0f, 90.0f);
  110. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  111. cameraNode.rotation = Quaternion(pitch, yaw, 0.0f);
  112. }
  113. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  114. if (input.keyDown[KEY_W])
  115. cameraNode.Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  116. if (input.keyDown[KEY_S])
  117. cameraNode.Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  118. if (input.keyDown[KEY_A])
  119. cameraNode.Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  120. if (input.keyDown[KEY_D])
  121. cameraNode.Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  122. }
  123. // Create XML patch instructions for screen joystick layout specific to this sample app
  124. String patchInstructions = "";