Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / fpp-game / fpp-game-tutorial-controller.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "fpp-game-tutorial-controller.h"
19
20 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
21 #include <dali-toolkit/public-api/visuals/visual-properties.h>
22 #include <dali/public-api/actors/camera-actor.h>
23 #include <dali/public-api/animation/animation.h>
24 #include <dali/public-api/events/touch-event.h>
25 #include <dali/public-api/object/property-map.h>
26 #include <dali/public-api/render-tasks/render-task-list.h>
27
28 using namespace Dali;
29 using namespace Dali::Toolkit;
30
31 FppGameTutorialController::FppGameTutorialController()
32 : mLeftTutorialComplete(false),
33   mRightTutorialComplete(false)
34 {
35 }
36
37 FppGameTutorialController::~FppGameTutorialController()
38 {
39 }
40
41 void FppGameTutorialController::OnTouch(const TouchEvent& touchEvent)
42 {
43   Vector2 size(mWindow.GetSize());
44
45   bool isLandscape(size.x > size.y);
46
47   if(!isLandscape)
48   {
49     std::swap(size.x, size.y);
50   }
51
52   Vector2 sizeHalf(size * 0.5f);
53
54   for(size_t i = 0; i < touchEvent.GetPointCount(); ++i)
55   {
56     Vector2 pos = touchEvent.GetScreenPosition(i);
57     if(!isLandscape)
58     {
59       std::swap(pos.x, pos.y);
60     }
61
62     // left label touched
63     if(touchEvent.GetState(i) == PointState::STARTED)
64     {
65       if(pos.x < sizeHalf.x && !mLeftTutorialComplete)
66       {
67         mLeftTutorialComplete = true;
68         Animation animation   = Animation::New(1.0f);
69         animation.AnimateTo(Property(mLeftLabel, Actor::Property::COLOR_ALPHA), 0.0f);
70
71         // connect complete signal
72         if(mRightTutorialComplete)
73         {
74           animation.FinishedSignal().Connect(this, &FppGameTutorialController::OnTutorialComplete);
75         }
76         animation.Play();
77       }
78       // right label touched
79       else if(!mRightTutorialComplete)
80       {
81         mRightTutorialComplete = true;
82         Animation animation    = Animation::New(1.0f);
83         animation.AnimateTo(Property(mRightLabel, Actor::Property::COLOR_ALPHA), 0.0f);
84         // connect complete signal
85         if(mLeftTutorialComplete)
86         {
87           animation.FinishedSignal().Connect(this, &FppGameTutorialController::OnTutorialComplete);
88         }
89         animation.Play();
90       }
91     }
92   }
93 }
94
95 void FppGameTutorialController::DisplayTutorial(Dali::Window window)
96 {
97   mWindow = window;
98
99   Vector2 windowSize(mWindow.GetSize());
100   bool    isLandscape(windowSize.x > windowSize.y);
101   if(!isLandscape)
102   {
103     std::swap(windowSize.x, windowSize.y);
104   }
105
106   mUiRoot = Actor::New();
107   mWindow.Add(mUiRoot);
108
109   // left tutorial text label
110   mLeftLabel = Toolkit::TextLabel::New("Touch here to walk");
111   mLeftLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
112   mLeftLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
113   mLeftLabel.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS);
114   mLeftLabel.SetProperty(Actor::Property::SIZE, Vector3(windowSize.x * 0.5, windowSize.y, 1.0f));
115   mLeftLabel.SetProperty(Toolkit::Control::Property::BACKGROUND,
116                          Property::Map().Add(Toolkit::Visual::Property::TYPE, Visual::COLOR).Add(ColorVisual::Property::MIX_COLOR, Vector4(0.0, 0.0, 0.7, 0.2)));
117   mLeftLabel.SetProperty(Toolkit::TextLabel::Property::TEXT_COLOR, Vector4(1.0f, 1.0f, 1.0f, 1.0f)); // White.
118   mLeftLabel.SetProperty(Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER");
119   mLeftLabel.SetProperty(Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER");
120
121   // right tutorial text label
122   mRightLabel = Toolkit::TextLabel::New("Touch here to look around");
123   mRightLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
124   mRightLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
125   mRightLabel.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS);
126   mRightLabel.SetProperty(Actor::Property::SIZE, Vector3(windowSize.x * 0.5, windowSize.y, 1.0f));
127   mRightLabel.SetProperty(Toolkit::Control::Property::BACKGROUND,
128                           Property::Map().Add(Toolkit::Visual::Property::TYPE, Visual::COLOR).Add(ColorVisual::Property::MIX_COLOR, Vector4(0.5, 0.0, 0.0, 0.2)));
129   mRightLabel.SetProperty(Toolkit::TextLabel::Property::TEXT_COLOR, Vector4(1.0f, 1.0f, 1.0f, 1.0f)); // White.
130   mRightLabel.SetProperty(Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER");
131   mRightLabel.SetProperty(Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER");
132
133   // create camera dedicated to be used with UI controls
134   CameraActor uiCamera = CameraActor::New();
135   mTutorialRenderTask  = mWindow.GetRenderTaskList().CreateTask();
136   mTutorialRenderTask.SetCameraActor(uiCamera);
137   mTutorialRenderTask.SetClearEnabled(false);
138   mTutorialRenderTask.SetSourceActor(mUiRoot);
139   mTutorialRenderTask.SetExclusive(true);
140
141   if(!isLandscape)
142   {
143     uiCamera.RotateBy(Degree(90.0f), Vector3(0.0f, 0.0f, 1.0f));
144   }
145
146   mLeftLabel.SetProperty(Actor::Property::POSITION, Vector3(-windowSize.x * 0.25f, 0.0, 0.0));
147   mRightLabel.SetProperty(Actor::Property::POSITION, Vector3(windowSize.x * 0.25f, 0.0, 0.0));
148
149   mUiRoot.Add(mLeftLabel);
150   mUiRoot.Add(mRightLabel);
151   mWindow.Add(uiCamera);
152
153   Animation animation = Animation::New(1.0f);
154   animation.AnimateTo(Property(mLeftLabel, Actor::Property::COLOR_ALPHA), 1.0f, AlphaFunction::EASE_OUT);
155   animation.AnimateTo(Property(mRightLabel, Actor::Property::COLOR_ALPHA), 1.0f, AlphaFunction::EASE_OUT);
156
157   animation.FinishedSignal().Connect(this, &FppGameTutorialController::OnTutorialAnimationFinished);
158
159   animation.Play();
160 }
161
162 void FppGameTutorialController::OnTutorialAnimationFinished(Animation& animation)
163 {
164   // touch signal will wait for a single touch on each side of screen
165   mWindow.TouchedSignal().Connect(this, &FppGameTutorialController::OnTouch);
166 }
167
168 void FppGameTutorialController::OnTutorialComplete(Animation& animation)
169 {
170   mWindow.Remove(mUiRoot);
171   mUiRoot.Reset();
172   mWindow.GetRenderTaskList().RemoveTask(mTutorialRenderTask);
173 }