-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sample.inl
377 lines (331 loc) · 13.1 KB
/
Sample.inl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//
// Copyright (c) 2008-2017 the Urho3D project.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include <Urho3D/Engine/Application.h>
#include <Urho3D/Graphics/Camera.h>
#include <Urho3D/Engine/Console.h>
#include <Urho3D/UI/Cursor.h>
#include <Urho3D/Engine/DebugHud.h>
#include <Urho3D/Engine/Engine.h>
#include <Urho3D/Engine/EngineDefs.h>
#include <Urho3D/IO/FileSystem.h>
#include <Urho3D/Graphics/Graphics.h>
#include <Urho3D/Input/Input.h>
#include <Urho3D/Input/InputEvents.h>
#include <Urho3D/Graphics/Renderer.h>
#include <Urho3D/Resource/ResourceCache.h>
#include <Urho3D/Scene/Scene.h>
#include <Urho3D/Scene/SceneEvents.h>
#include <Urho3D/UI/Sprite.h>
#include <Urho3D/Graphics/Texture2D.h>
#include <Urho3D/Core/Timer.h>
#include <Urho3D/UI/UI.h>
#include <Urho3D/Resource/XMLFile.h>
#include <Urho3D/IO/Log.h>
Sample::Sample(Context* context) :
Application(context),
yaw_(55.0f),
pitch_(20.0f),
touchEnabled_(false),
useMouseMode_(MM_ABSOLUTE),
screenJoystickIndex_(M_MAX_UNSIGNED),
screenJoystickSettingsIndex_(M_MAX_UNSIGNED),
paused_(false)
{
}
void Sample::Setup()
{
// Modify engine startup parameters
engineParameters_[EP_WINDOW_TITLE] = GetTypeName();
engineParameters_[EP_LOG_NAME] = GetSubsystem<FileSystem>()->GetAppPreferencesDir("urho3d", "logs") + GetTypeName() + ".log";
engineParameters_[EP_FULL_SCREEN] = false;
engineParameters_[EP_HEADLESS] = false;
engineParameters_[EP_SOUND] = false;
engineParameters_[EP_WINDOW_WIDTH] = 1920;
engineParameters_[EP_WINDOW_HEIGHT] = 1080;
// Construct a search path to find the resource prefix with two entries:
// The first entry is an empty path which will be substituted with program/bin directory -- this entry is for binary when it is still in build tree
// The second and third entries are possible relative paths from the installed program/bin directory to the asset directory -- these entries are for binary when it is in the Urho3D SDK installation location
if (!engineParameters_.Contains(EP_RESOURCE_PREFIX_PATHS))
engineParameters_[EP_RESOURCE_PREFIX_PATHS] = ";../share/Resources;../share/Urho3D/Resources";
}
void Sample::Start()
{
if (GetPlatform() == "Android" || GetPlatform() == "iOS")
// On mobile platform, enable touch by adding a screen joystick
InitTouchInput();
else if (GetSubsystem<Input>()->GetNumJoysticks() == 0)
// On desktop platform, do not detect touch when we already got a joystick
SubscribeToEvent(E_TOUCHBEGIN, URHO3D_HANDLER(Sample, HandleTouchBegin));
// Set custom window Title & Icon
SetWindowTitleAndIcon();
// Create console and debug HUD
CreateConsoleAndDebugHud();
// Subscribe key down event
SubscribeToEvent(E_KEYDOWN, URHO3D_HANDLER(Sample, HandleKeyDown));
// Subscribe key up event
SubscribeToEvent(E_KEYUP, URHO3D_HANDLER(Sample, HandleKeyUp));
// Subscribe scene update event
SubscribeToEvent(E_SCENEUPDATE, URHO3D_HANDLER(Sample, HandleSceneUpdate));
}
void Sample::Stop()
{
engine_->DumpResources(true);
}
void Sample::InitTouchInput()
{
touchEnabled_ = true;
ResourceCache* cache = GetSubsystem<ResourceCache>();
Input* input = GetSubsystem<Input>();
XMLFile* layout = cache->GetResource<XMLFile>("UI/ScreenJoystick_Samples.xml");
const String& patchString = GetScreenJoystickPatchString();
if (!patchString.Empty())
{
// Patch the screen joystick layout further on demand
SharedPtr<XMLFile> patchFile(new XMLFile(context_));
if (patchFile->FromString(patchString))
layout->Patch(patchFile);
}
screenJoystickIndex_ = (unsigned)input->AddScreenJoystick(layout, cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
input->SetScreenJoystickVisible(screenJoystickSettingsIndex_, true);
}
void Sample::InitMouseMode(MouseMode mode)
{
useMouseMode_ = mode;
Input* input = GetSubsystem<Input>();
if (GetPlatform() != "Web")
{
if (useMouseMode_ == MM_FREE)
input->SetMouseVisible(true);
Console* console = GetSubsystem<Console>();
if (useMouseMode_ != MM_ABSOLUTE)
{
input->SetMouseMode(useMouseMode_);
if (console && console->IsVisible())
input->SetMouseMode(MM_ABSOLUTE, true);
}
}
else
{
input->SetMouseVisible(true);
SubscribeToEvent(E_MOUSEBUTTONDOWN, URHO3D_HANDLER(Sample, HandleMouseModeRequest));
SubscribeToEvent(E_MOUSEMODECHANGED, URHO3D_HANDLER(Sample, HandleMouseModeChange));
}
}
void Sample::SetLogoVisible(bool enable)
{
if (logoSprite_)
logoSprite_->SetVisible(enable);
}
void Sample::SetWindowTitleAndIcon()
{
ResourceCache* cache = GetSubsystem<ResourceCache>();
Graphics* graphics = GetSubsystem<Graphics>();
Image* icon = cache->GetResource<Image>("Textures/UrhoIcon.png");
graphics->SetWindowIcon(icon);
graphics->SetWindowTitle("Billiards");
}
void Sample::CreateConsoleAndDebugHud()
{
// Get default style
ResourceCache* cache = GetSubsystem<ResourceCache>();
XMLFile* xmlFile = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
// Create console
Console* console = engine_->CreateConsole();
console->SetDefaultStyle(xmlFile);
console->GetBackground()->SetOpacity(0.8f);
// Create debug HUD.
DebugHud* debugHud = engine_->CreateDebugHud();
debugHud->SetDefaultStyle(xmlFile);
}
void Sample::HandleKeyUp(StringHash /*eventType*/, VariantMap& eventData)
{
using namespace KeyUp;
int key = eventData[P_KEY].GetInt();
// Close console (if open) or exit when ESC is pressed
if (key == KEY_ESCAPE)
{
Console* console = GetSubsystem<Console>();
if (console->IsVisible())
console->SetVisible(false);
else
{
if (GetPlatform() == "Web")
{
GetSubsystem<Input>()->SetMouseVisible(true);
if (useMouseMode_ != MM_ABSOLUTE)
GetSubsystem<Input>()->SetMouseMode(MM_FREE);
}
else
engine_->Exit();
}
}
}
void Sample::HandleKeyDown(StringHash /*eventType*/, VariantMap& eventData)
{
using namespace KeyDown;
int key = eventData[P_KEY].GetInt();
// Toggle console with F1
if (key == KEY_F1)
GetSubsystem<Console>()->Toggle();
// Toggle debug HUD with F2
else if (key == KEY_F2)
GetSubsystem<DebugHud>()->ToggleAll();
// Common rendering quality controls, only when UI has no focused element
else if (!GetSubsystem<UI>()->GetFocusElement())
{
Renderer* renderer = GetSubsystem<Renderer>();
// Preferences / Pause
if (key == KEY_SELECT && touchEnabled_)
{
paused_ = !paused_;
Input* input = GetSubsystem<Input>();
if (screenJoystickSettingsIndex_ == M_MAX_UNSIGNED)
{
// Lazy initialization
ResourceCache* cache = GetSubsystem<ResourceCache>();
screenJoystickSettingsIndex_ = (unsigned)input->AddScreenJoystick(cache->GetResource<XMLFile>("UI/ScreenJoystickSettings_Samples.xml"), cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
}
else
input->SetScreenJoystickVisible(screenJoystickSettingsIndex_, paused_);
}
// Texture quality
else if (key == '1')
{
int quality = renderer->GetTextureQuality();
++quality;
if (quality > QUALITY_HIGH)
quality = QUALITY_LOW;
renderer->SetTextureQuality(quality);
}
// Material quality
else if (key == '2')
{
int quality = renderer->GetMaterialQuality();
++quality;
if (quality > QUALITY_HIGH)
quality = QUALITY_LOW;
renderer->SetMaterialQuality(quality);
}
// Specular lighting
else if (key == '3')
renderer->SetSpecularLighting(!renderer->GetSpecularLighting());
// Shadow rendering
else if (key == '4')
renderer->SetDrawShadows(!renderer->GetDrawShadows());
// Shadow map resolution
else if (key == '5')
{
int shadowMapSize = renderer->GetShadowMapSize();
shadowMapSize *= 2;
if (shadowMapSize > 2048)
shadowMapSize = 512;
renderer->SetShadowMapSize(shadowMapSize);
}
// Shadow depth and filtering quality
else if (key == '6')
{
ShadowQuality quality = renderer->GetShadowQuality();
quality = (ShadowQuality)(quality + 1);
if (quality > SHADOWQUALITY_BLUR_VSM)
quality = SHADOWQUALITY_SIMPLE_16BIT;
renderer->SetShadowQuality(quality);
}
// Occlusion culling
else if (key == '7')
{
bool occlusion = renderer->GetMaxOccluderTriangles() > 0;
occlusion = !occlusion;
renderer->SetMaxOccluderTriangles(occlusion ? 5000 : 0);
}
// Instancing
else if (key == '8')
renderer->SetDynamicInstancing(!renderer->GetDynamicInstancing());
// Take screenshot
else if (key == '9')
{
Graphics* graphics = GetSubsystem<Graphics>();
Image screenshot(context_);
graphics->TakeScreenShot(screenshot);
// Here we save in the Data folder with date and time appended
screenshot.SavePNG(GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Screenshot_" +
Time::GetTimeStamp().Replaced(':', '_').Replaced('.', '_').Replaced(' ', '_') + ".png");
}
}
}
void Sample::HandleSceneUpdate(StringHash /*eventType*/, VariantMap& eventData)
{
// Move the camera by touch, if the camera node is initialized by descendant sample class
if (touchEnabled_ && cameraNode_)
{
Input* input = GetSubsystem<Input>();
for (unsigned i = 0; i < input->GetNumTouches(); ++i)
{
TouchState* state = input->GetTouch(i);
if (!state->touchedElement_) // Touch on empty space
{
if (state->delta_.x_ ||state->delta_.y_)
{
Camera* camera = cameraNode_->GetComponent<Camera>();
if (!camera)
return;
Graphics* graphics = GetSubsystem<Graphics>();
yaw_ += TOUCH_SENSITIVITY * camera->GetFov() / graphics->GetHeight() * state->delta_.x_;
pitch_ += TOUCH_SENSITIVITY * camera->GetFov() / graphics->GetHeight() * state->delta_.y_;
// Construct new orientation for the camera scene node from yaw and pitch; roll is fixed to zero
cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
}
else
{
// Move the cursor to the touch position
Cursor* cursor = GetSubsystem<UI>()->GetCursor();
if (cursor && cursor->IsVisible())
cursor->SetPosition(state->position_);
}
}
}
}
}
void Sample::HandleTouchBegin(StringHash /*eventType*/, VariantMap& eventData)
{
// On some platforms like Windows the presence of touch input can only be detected dynamically
InitTouchInput();
UnsubscribeFromEvent("TouchBegin");
}
// If the user clicks the canvas, attempt to switch to relative mouse mode on web platform
void Sample::HandleMouseModeRequest(StringHash /*eventType*/, VariantMap& eventData)
{
Console* console = GetSubsystem<Console>();
if (console && console->IsVisible())
return;
Input* input = GetSubsystem<Input>();
if (useMouseMode_ == MM_ABSOLUTE)
input->SetMouseVisible(false);
else if (useMouseMode_ == MM_FREE)
input->SetMouseVisible(true);
input->SetMouseMode(useMouseMode_);
}
void Sample::HandleMouseModeChange(StringHash /*eventType*/, VariantMap& eventData)
{
Input* input = GetSubsystem<Input>();
bool mouseLocked = eventData[MouseModeChanged::P_MOUSELOCKED].GetBool();
input->SetMouseVisible(!mouseLocked);
}