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/Graphics/Octree.h>
28 #include <Urho3D/Graphics/Renderer.h>
29 #include <Urho3D/Graphics/Zone.h>
30 #include <Urho3D/Input/Input.h>
31 #include <Urho3D/Resource/ResourceCache.h>
32 #include <Urho3D/Scene/Scene.h>
33 #include <Urho3D/UI/Font.h>
34 #include <Urho3D/UI/Text.h>
35 #include <Urho3D/Urho2D/ParticleEffect2D.h>
36 #include <Urho3D/Urho2D/ParticleEmitter2D.h>
37
38 #include "Urho2DParticle.h"
39
40 #include <Urho3D/DebugNew.h>
41
URHO3D_DEFINE_APPLICATION_MAIN(Urho2DParticle)42 URHO3D_DEFINE_APPLICATION_MAIN(Urho2DParticle)
43
44 Urho2DParticle::Urho2DParticle(Context* context) :
45 Sample(context)
46 {
47 }
48
Start()49 void Urho2DParticle::Start()
50 {
51 // Execute base class startup
52 Sample::Start();
53
54 // Set mouse visible
55 Input* input = GetSubsystem<Input>();
56 input->SetMouseVisible(true);
57
58 // Create the scene content
59 CreateScene();
60
61 // Create the UI content
62 CreateInstructions();
63
64 // Setup the viewport for displaying the scene
65 SetupViewport();
66
67 // Hook up to the frame update events
68 SubscribeToEvents();
69
70 // Set the mouse mode to use in the sample
71 Sample::InitMouseMode(MM_FREE);
72 }
73
CreateScene()74 void Urho2DParticle::CreateScene()
75 {
76 scene_ = new Scene(context_);
77 scene_->CreateComponent<Octree>();
78
79 // Create camera node
80 cameraNode_ = scene_->CreateChild("Camera");
81 // Set camera's position
82 cameraNode_->SetPosition(Vector3(0.0f, 0.0f, -10.0f));
83
84 Camera* camera = cameraNode_->CreateComponent<Camera>();
85 camera->SetOrthographic(true);
86
87 Graphics* graphics = GetSubsystem<Graphics>();
88 camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE);
89 camera->SetZoom(1.2f * Min((float)graphics->GetWidth() / 1280.0f, (float)graphics->GetHeight() / 800.0f)); // Set zoom according to user's resolution to ensure full visibility (initial zoom (1.2) is set for full visibility at 1280x800 resolution)
90
91 ResourceCache* cache = GetSubsystem<ResourceCache>();
92 ParticleEffect2D* particleEffect = cache->GetResource<ParticleEffect2D>("Urho2D/sun.pex");
93 if (!particleEffect)
94 return;
95
96 particleNode_ = scene_->CreateChild("ParticleEmitter2D");
97 ParticleEmitter2D* particleEmitter = particleNode_->CreateComponent<ParticleEmitter2D>();
98 particleEmitter->SetEffect(particleEffect);
99
100 ParticleEffect2D* greenSpiralEffect = cache->GetResource<ParticleEffect2D>("Urho2D/greenspiral.pex");
101 if (!greenSpiralEffect)
102 return;
103
104 Node* greenSpiralNode = scene_->CreateChild("GreenSpiral");
105 ParticleEmitter2D* greenSpiralEmitter = greenSpiralNode->CreateComponent<ParticleEmitter2D>();
106 greenSpiralEmitter->SetEffect(greenSpiralEffect);
107 }
108
CreateInstructions()109 void Urho2DParticle::CreateInstructions()
110 {
111 ResourceCache* cache = GetSubsystem<ResourceCache>();
112 UI* ui = GetSubsystem<UI>();
113
114 // Construct new Text object, set string to display and font to use
115 Text* instructionText = ui->GetRoot()->CreateChild<Text>();
116 instructionText->SetText("Use mouse/touch to move the particle.");
117 instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
118
119 // Position the text relative to the screen center
120 instructionText->SetHorizontalAlignment(HA_CENTER);
121 instructionText->SetVerticalAlignment(VA_CENTER);
122 instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
123 }
124
SetupViewport()125 void Urho2DParticle::SetupViewport()
126 {
127 Renderer* renderer = GetSubsystem<Renderer>();
128
129 // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
130 SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
131 renderer->SetViewport(0, viewport);
132 }
133
SubscribeToEvents()134 void Urho2DParticle::SubscribeToEvents()
135 {
136 SubscribeToEvent(E_MOUSEMOVE, URHO3D_HANDLER(Urho2DParticle, HandleMouseMove));
137 if (touchEnabled_)
138 SubscribeToEvent(E_TOUCHMOVE, URHO3D_HANDLER(Urho2DParticle, HandleMouseMove));
139
140 // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
141 UnsubscribeFromEvent(E_SCENEUPDATE);
142 }
143
HandleMouseMove(StringHash eventType,VariantMap & eventData)144 void Urho2DParticle::HandleMouseMove(StringHash eventType, VariantMap& eventData)
145 {
146 if (particleNode_)
147 {
148 using namespace MouseMove;
149 float x = (float)eventData[P_X].GetInt();
150 float y = (float)eventData[P_Y].GetInt();
151 Graphics* graphics = GetSubsystem<Graphics>();
152 Camera* camera = cameraNode_->GetComponent<Camera>();
153 particleNode_->SetPosition(camera->ScreenToWorldPoint(Vector3(x / graphics->GetWidth(), y / graphics->GetHeight(), 10.0f)));
154 }
155 }
156