SceneAndUILoad.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Core/CoreEvents.h>
  23. #include <Urho3D/Engine/Engine.h>
  24. #include <Urho3D/Graphics/Camera.h>
  25. #include <Urho3D/Graphics/Graphics.h>
  26. #include <Urho3D/Input/Input.h>
  27. #include <Urho3D/Resource/ResourceCache.h>
  28. #include <Urho3D/Scene/Scene.h>
  29. #include <Urho3D/UI/Button.h>
  30. #include <Urho3D/UI/UI.h>
  31. #include <Urho3D/UI/UIEvents.h>
  32. #include "SceneAndUILoad.h"
  33. #include <Urho3D/DebugNew.h>
  34. URHO3D_DEFINE_APPLICATION_MAIN(SceneAndUILoad)
  35. SceneAndUILoad::SceneAndUILoad(Context* context) :
  36. Sample(context)
  37. {
  38. }
  39. void SceneAndUILoad::Start()
  40. {
  41. // Execute base class startup
  42. Sample::Start();
  43. // Create the scene content
  44. CreateScene();
  45. // Create the UI content
  46. CreateUI();
  47. // Setup the viewport for displaying the scene
  48. SetupViewport();
  49. // Subscribe to global events for camera movement
  50. SubscribeToEvents();
  51. // Set the mouse mode to use in the sample
  52. Sample::InitMouseMode(MM_RELATIVE);
  53. }
  54. void SceneAndUILoad::CreateScene()
  55. {
  56. auto* cache = GetSubsystem<ResourceCache>();
  57. scene_ = new Scene(context_);
  58. // Load scene content prepared in the editor (XML format). GetFile() returns an open file from the resource system
  59. // which scene.LoadXML() will read
  60. SharedPtr<File> file = cache->GetFile("Scenes/SceneLoadExample.xml");
  61. scene_->LoadXML(*file);
  62. // Create the camera (not included in the scene file)
  63. cameraNode_ = scene_->CreateChild("Camera");
  64. cameraNode_->CreateComponent<Camera>();
  65. // Set an initial position for the camera scene node above the plane
  66. cameraNode_->SetPosition(Vector3(0.0f, 2.0f, -10.0f));
  67. }
  68. void SceneAndUILoad::CreateUI()
  69. {
  70. auto* cache = GetSubsystem<ResourceCache>();
  71. auto* ui = GetSubsystem<UI>();
  72. // Set up global UI style into the root UI element
  73. auto* style = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  74. ui->GetRoot()->SetDefaultStyle(style);
  75. // Create a Cursor UI element because we want to be able to hide and show it at will. When hidden, the mouse cursor will
  76. // control the camera, and when visible, it will interact with the UI
  77. SharedPtr<Cursor> cursor(new Cursor(context_));
  78. cursor->SetStyleAuto();
  79. ui->SetCursor(cursor);
  80. // Set starting position of the cursor at the rendering window center
  81. auto* graphics = GetSubsystem<Graphics>();
  82. cursor->SetPosition(graphics->GetWidth() / 2, graphics->GetHeight() / 2);
  83. // Load UI content prepared in the editor and add to the UI hierarchy
  84. SharedPtr<UIElement> layoutRoot = ui->LoadLayout(cache->GetResource<XMLFile>("UI/UILoadExample.xml"));
  85. ui->GetRoot()->AddChild(layoutRoot);
  86. // Subscribe to button actions (toggle scene lights when pressed then released)
  87. auto* button = layoutRoot->GetChildStaticCast<Button>("ToggleLight1", true);
  88. if (button)
  89. SubscribeToEvent(button, E_RELEASED, URHO3D_HANDLER(SceneAndUILoad, ToggleLight1));
  90. button = layoutRoot->GetChildStaticCast<Button>("ToggleLight2", true);
  91. if (button)
  92. SubscribeToEvent(button, E_RELEASED, URHO3D_HANDLER(SceneAndUILoad, ToggleLight2));
  93. }
  94. void SceneAndUILoad::SetupViewport()
  95. {
  96. auto* renderer = GetSubsystem<Renderer>();
  97. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  98. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  99. renderer->SetViewport(0, viewport);
  100. }
  101. void SceneAndUILoad::SubscribeToEvents()
  102. {
  103. // Subscribe HandleUpdate() function for camera motion
  104. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(SceneAndUILoad, HandleUpdate));
  105. }
  106. void SceneAndUILoad::MoveCamera(float timeStep)
  107. {
  108. // Right mouse button controls mouse cursor visibility: hide when pressed
  109. auto* ui = GetSubsystem<UI>();
  110. auto* input = GetSubsystem<Input>();
  111. ui->GetCursor()->SetVisible(!input->GetMouseButtonDown(MOUSEB_RIGHT));
  112. // Do not move if the UI has a focused element
  113. if (ui->GetFocusElement())
  114. return;
  115. // Movement speed as world units per second
  116. const float MOVE_SPEED = 20.0f;
  117. // Mouse sensitivity as degrees per pixel
  118. const float MOUSE_SENSITIVITY = 0.1f;
  119. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  120. // Only move the camera when the cursor is hidden
  121. if (!ui->GetCursor()->IsVisible())
  122. {
  123. IntVector2 mouseMove = input->GetMouseMove();
  124. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  125. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  126. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  127. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  128. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  129. }
  130. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  131. if (input->GetKeyDown(KEY_W))
  132. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  133. if (input->GetKeyDown(KEY_S))
  134. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  135. if (input->GetKeyDown(KEY_A))
  136. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  137. if (input->GetKeyDown(KEY_D))
  138. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  139. }
  140. void SceneAndUILoad::HandleUpdate(StringHash eventType, VariantMap& eventData)
  141. {
  142. using namespace Update;
  143. // Take the frame time step, which is stored as a float
  144. float timeStep = eventData[P_TIMESTEP].GetFloat();
  145. // Move the camera, scale movement with time step
  146. MoveCamera(timeStep);
  147. }
  148. void SceneAndUILoad::ToggleLight1(StringHash eventType, VariantMap& eventData)
  149. {
  150. Node* lightNode = scene_->GetChild("Light1", true);
  151. if (lightNode)
  152. lightNode->SetEnabled(!lightNode->IsEnabled());
  153. }
  154. void SceneAndUILoad::ToggleLight2(StringHash eventType, VariantMap& eventData)
  155. {
  156. Node* lightNode = scene_->GetChild("Light2", true);
  157. if (lightNode)
  158. lightNode->SetEnabled(!lightNode->IsEnabled());
  159. }