SceneAndUILoad.cpp 7.0 KB

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