SceneAndUILoad.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // Copyright (c) 2008-2015 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/Urho3D.h>
  23. #include <Urho3D/Core/CoreEvents.h>
  24. #include <Urho3D/Engine/Engine.h>
  25. #include <Urho3D/Graphics/Camera.h>
  26. #include <Urho3D/Graphics/Graphics.h>
  27. #include <Urho3D/Graphics/Zone.h>
  28. #include <Urho3D/Input/Input.h>
  29. #include <Urho3D/Resource/ResourceCache.h>
  30. #include <Urho3D/Resource/XMLFile.h>
  31. #include <Urho3D/Scene/Scene.h>
  32. #include <Urho3D/UI/Button.h>
  33. #include <Urho3D/UI/Cursor.h>
  34. #include <Urho3D/UI/UI.h>
  35. #include <Urho3D/UI/UIEvents.h>
  36. #include "SceneAndUILoad.h"
  37. #include <Urho3D/DebugNew.h>
  38. DEFINE_APPLICATION_MAIN(SceneAndUILoad)
  39. SceneAndUILoad::SceneAndUILoad(Context* context) :
  40. Sample(context)
  41. {
  42. }
  43. void SceneAndUILoad::Start()
  44. {
  45. // Execute base class startup
  46. Sample::Start();
  47. // Create the scene content
  48. CreateScene();
  49. // Create the UI content
  50. CreateUI();
  51. // Setup the viewport for displaying the scene
  52. SetupViewport();
  53. // Subscribe to global events for camera movement
  54. SubscribeToEvents();
  55. }
  56. void SceneAndUILoad::CreateScene()
  57. {
  58. ResourceCache* cache = GetSubsystem<ResourceCache>();
  59. scene_ = new Scene(context_);
  60. // Load scene content prepared in the editor (XML format). GetFile() returns an open file from the resource system
  61. // which scene.LoadXML() will read
  62. SharedPtr<File> file = cache->GetFile("Scenes/SceneLoadExample.xml");
  63. scene_->LoadXML(*file);
  64. // Create the camera (not included in the scene file)
  65. cameraNode_ = scene_->CreateChild("Camera");
  66. cameraNode_->CreateComponent<Camera>();
  67. // Set an initial position for the camera scene node above the plane
  68. cameraNode_->SetPosition(Vector3(0.0f, 2.0f, -10.0f));
  69. }
  70. void SceneAndUILoad::CreateUI()
  71. {
  72. ResourceCache* cache = GetSubsystem<ResourceCache>();
  73. UI* ui = GetSubsystem<UI>();
  74. // Set up global UI style into the root UI element
  75. XMLFile* style = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  76. ui->GetRoot()->SetDefaultStyle(style);
  77. // Create a Cursor UI element because we want to be able to hide and show it at will. When hidden, the mouse cursor will
  78. // control the camera, and when visible, it will interact with the UI
  79. SharedPtr<Cursor> cursor(new Cursor(context_));
  80. cursor->SetStyleAuto();
  81. ui->SetCursor(cursor);
  82. // Set starting position of the cursor at the rendering window center
  83. Graphics* graphics = GetSubsystem<Graphics>();
  84. cursor->SetPosition(graphics->GetWidth() / 2, graphics->GetHeight() / 2);
  85. // Load UI content prepared in the editor and add to the UI hierarchy
  86. SharedPtr<UIElement> layoutRoot = ui->LoadLayout(cache->GetResource<XMLFile>("UI/UILoadExample.xml"));
  87. ui->GetRoot()->AddChild(layoutRoot);
  88. // Subscribe to button actions (toggle scene lights when pressed then released)
  89. Button* button = static_cast<Button*>(layoutRoot->GetChild("ToggleLight1", true));
  90. if (button)
  91. SubscribeToEvent(button, E_RELEASED, HANDLER(SceneAndUILoad, ToggleLight1));
  92. button = static_cast<Button*>(layoutRoot->GetChild("ToggleLight2", true));
  93. if (button)
  94. SubscribeToEvent(button, E_RELEASED, HANDLER(SceneAndUILoad, ToggleLight2));
  95. }
  96. void SceneAndUILoad::SetupViewport()
  97. {
  98. Renderer* renderer = GetSubsystem<Renderer>();
  99. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  100. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  101. renderer->SetViewport(0, viewport);
  102. }
  103. void SceneAndUILoad::SubscribeToEvents()
  104. {
  105. // Subscribe HandleUpdate() function for camera motion
  106. SubscribeToEvent(E_UPDATE, HANDLER(SceneAndUILoad, HandleUpdate));
  107. }
  108. void SceneAndUILoad::MoveCamera(float timeStep)
  109. {
  110. // Right mouse button controls mouse cursor visibility: hide when pressed
  111. UI* ui = GetSubsystem<UI>();
  112. Input* input = GetSubsystem<Input>();
  113. ui->GetCursor()->SetVisible(!input->GetMouseButtonDown(MOUSEB_RIGHT));
  114. // Do not move if the UI has a focused element
  115. if (ui->GetFocusElement())
  116. return;
  117. // Movement speed as world units per second
  118. const float MOVE_SPEED = 20.0f;
  119. // Mouse sensitivity as degrees per pixel
  120. const float MOUSE_SENSITIVITY = 0.1f;
  121. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  122. // Only move the camera when the cursor is hidden
  123. if (!ui->GetCursor()->IsVisible())
  124. {
  125. IntVector2 mouseMove = input->GetMouseMove();
  126. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  127. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  128. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  129. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  130. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  131. }
  132. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  133. if (input->GetKeyDown('W'))
  134. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  135. if (input->GetKeyDown('S'))
  136. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  137. if (input->GetKeyDown('A'))
  138. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  139. if (input->GetKeyDown('D'))
  140. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  141. }
  142. void SceneAndUILoad::HandleUpdate(StringHash eventType, VariantMap& eventData)
  143. {
  144. using namespace Update;
  145. // Take the frame time step, which is stored as a float
  146. float timeStep = eventData[P_TIMESTEP].GetFloat();
  147. // Move the camera, scale movement with time step
  148. MoveCamera(timeStep);
  149. }
  150. void SceneAndUILoad::ToggleLight1(StringHash eventType, VariantMap& eventData)
  151. {
  152. Node* lightNode = scene_->GetChild("Light1", true);
  153. if (lightNode)
  154. lightNode->SetEnabled(!lightNode->IsEnabled());
  155. }
  156. void SceneAndUILoad::ToggleLight2(StringHash eventType, VariantMap& eventData)
  157. {
  158. Node* lightNode = scene_->GetChild("Light2", true);
  159. if (lightNode)
  160. lightNode->SetEnabled(!lightNode->IsEnabled());
  161. }