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