SceneAndUILoad.cpp 5.9 KB

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