38_SceneAndUILoad.as 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. #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. // Subscribe to global events for camera movement
  18. SubscribeToEvents();
  19. }
  20. void CreateScene()
  21. {
  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. scene_.LoadXML(cache.GetFile("Scenes/SceneLoadExample.xml"));
  26. // Create the camera (not included in the scene file)
  27. cameraNode = scene_.CreateChild("Camera");
  28. cameraNode.CreateComponent("Camera");
  29. // Set an initial position for the camera scene node above the plane
  30. cameraNode.position = Vector3(0.0f, 2.0f, -10.0f);
  31. }
  32. void CreateUI()
  33. {
  34. // Set up global UI style into the root UI element
  35. XMLFile@ 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. Cursor@ cursor = 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. UIElement@ 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. Button@ button = layoutRoot.GetChild("ToggleLight1", true);
  49. if (button !is null)
  50. SubscribeToEvent(button, "Released", "ToggleLight1");
  51. button = layoutRoot.GetChild("ToggleLight2", true);
  52. if (button !is null)
  53. SubscribeToEvent(button, "Released", "ToggleLight2");
  54. }
  55. void ToggleLight1()
  56. {
  57. Node@ lightNode = scene_.GetChild("Light1", true);
  58. if (lightNode !is null)
  59. lightNode.enabled = !lightNode.enabled;
  60. }
  61. void ToggleLight2()
  62. {
  63. Node@ lightNode = scene_.GetChild("Light2", true);
  64. if (lightNode !is null)
  65. lightNode.enabled = !lightNode.enabled;
  66. }
  67. void SetupViewport()
  68. {
  69. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  70. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  71. renderer.viewports[0] = viewport;
  72. }
  73. void SubscribeToEvents()
  74. {
  75. // Subscribe HandleUpdate() function for camera motion
  76. SubscribeToEvent("Update", "HandleUpdate");
  77. }
  78. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  79. {
  80. // Take the frame time step, which is stored as a float
  81. float timeStep = eventData["TimeStep"].GetFloat();
  82. // Move the camera, scale movement with time step
  83. MoveCamera(timeStep);
  84. }
  85. void MoveCamera(float timeStep)
  86. {
  87. // Right mouse button controls mouse cursor visibility: hide when pressed
  88. ui.cursor.visible = !input.mouseButtonDown[MOUSEB_RIGHT];
  89. // Do not move if the UI has a focused element
  90. if (ui.focusElement !is null)
  91. return;
  92. // Movement speed as world units per second
  93. const float MOVE_SPEED = 20.0f;
  94. // Mouse sensitivity as degrees per pixel
  95. const float MOUSE_SENSITIVITY = 0.1f;
  96. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  97. // Only move the camera when the cursor is hidden
  98. if (!ui.cursor.visible)
  99. {
  100. IntVector2 mouseMove = input.mouseMove;
  101. yaw += MOUSE_SENSITIVITY * mouseMove.x;
  102. pitch += MOUSE_SENSITIVITY * mouseMove.y;
  103. pitch = Clamp(pitch, -90.0f, 90.0f);
  104. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  105. cameraNode.rotation = Quaternion(pitch, yaw, 0.0f);
  106. }
  107. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  108. if (input.keyDown['W'])
  109. cameraNode.Translate(Vector3(0.0f, 0.0f, 1.0f) * MOVE_SPEED * timeStep);
  110. if (input.keyDown['S'])
  111. cameraNode.Translate(Vector3(0.0f, 0.0f, -1.0f) * MOVE_SPEED * timeStep);
  112. if (input.keyDown['A'])
  113. cameraNode.Translate(Vector3(-1.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
  114. if (input.keyDown['D'])
  115. cameraNode.Translate(Vector3(1.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
  116. }
  117. // Create XML patch instructions for screen joystick layout specific to this sample app
  118. String patchInstructions = "";