Example of First Person 3D game with DALI
[platform/core/uifw/dali-demo.git] / examples / fpp-game / fpp-game-tutorial-controller.cpp
1 /*
2  * Copyright (c) 2016 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/public-api/events/touch-data.h>
21 #include <dali/public-api/animation/animation.h>
22 #include <dali/public-api/actors/camera-actor.h>
23 #include <dali/public-api/render-tasks/render-task-list.h>
24
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27
28 FppGameTutorialController::FppGameTutorialController()
29 : mLeftTutorialComplete( false ),
30   mRightTutorialComplete( false )
31 {
32
33 }
34
35 FppGameTutorialController::~FppGameTutorialController()
36 {
37
38 }
39
40 void FppGameTutorialController::OnTouch( const TouchData& touchEvent )
41 {
42   Vector2 size( mStage.GetSize() );
43
44   bool isLandscape( size.x > size.y );
45
46   if( !isLandscape )
47   {
48     std::swap( size.x, size.y );
49   }
50
51   Vector2 sizeHalf( size * 0.5f );
52
53   for( size_t i = 0; i < touchEvent.GetPointCount(); ++i )
54   {
55     Vector2 pos = touchEvent.GetScreenPosition( i );
56     if( !isLandscape )
57     {
58       std::swap( pos.x, pos.y );
59     }
60
61     // left label touched
62     if( touchEvent.GetState( i ) == PointState::STARTED )
63     {
64       if( pos.x < sizeHalf.x && !mLeftTutorialComplete )
65       {
66         mLeftTutorialComplete = true;
67         Animation animation = Animation::New( 1.0f );
68         animation.AnimateTo( Property( mLeftLabel, Actor::Property::COLOR_ALPHA ), 0.0f );
69
70         // connect complete signal
71         if( mRightTutorialComplete )
72         {
73           animation.FinishedSignal().Connect( this, &FppGameTutorialController::OnTutorialComplete );
74         }
75         animation.Play();
76       }
77       // right label touched
78       else if( !mRightTutorialComplete )
79       {
80         mRightTutorialComplete = true;
81         Animation animation = Animation::New( 1.0f );
82         animation.AnimateTo( Property( mRightLabel, Actor::Property::COLOR_ALPHA ), 0.0f );
83         // connect complete signal
84         if( mLeftTutorialComplete )
85         {
86           animation.FinishedSignal().Connect( this, &FppGameTutorialController::OnTutorialComplete );
87         }
88         animation.Play();
89       }
90     }
91   }
92 }
93
94 void FppGameTutorialController::DisplayTutorial()
95 {
96   mStage = Stage::GetCurrent();
97
98   Vector2 stageSize( mStage.GetSize() );
99   bool isLandscape( stageSize.x > stageSize.y );
100   if( !isLandscape )
101   {
102     std::swap( stageSize.x, stageSize.y );
103   }
104
105   mUiRoot = Actor::New();
106   mStage.Add( mUiRoot );
107
108   // left tutorial text label
109   mLeftLabel = Toolkit::TextLabel::New("Touch here to walk");
110   mLeftLabel.SetParentOrigin( ParentOrigin::CENTER );
111   mLeftLabel.SetAnchorPoint( AnchorPoint::CENTER );
112   mLeftLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
113   mLeftLabel.SetSize( Vector3( stageSize.x*0.5, stageSize.y, 1.0f ) );
114   mLeftLabel.SetProperty( Toolkit::Control::Property::BACKGROUND_COLOR, Vector4( 0.0, 0.0, 0.7, 0.2 ));
115   mLeftLabel.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) ); // White.
116   mLeftLabel.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
117   mLeftLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
118
119   // right tutorial text label
120   mRightLabel = Toolkit::TextLabel::New("Touch here to look around");
121   mRightLabel.SetParentOrigin( ParentOrigin::CENTER );
122   mRightLabel.SetAnchorPoint( AnchorPoint::CENTER );
123   mRightLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
124   mRightLabel.SetSize( Vector3( stageSize.x*0.5, stageSize.y, 1.0f ) );
125   mRightLabel.SetProperty( Toolkit::Control::Property::BACKGROUND_COLOR, Vector4( 0.5, 0.0, 0.0, 0.2 ));
126   mRightLabel.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) ); // White.
127   mRightLabel.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
128   mRightLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
129
130   // create camera dedicated to be used with UI controls
131   CameraActor uiCamera = CameraActor::New();
132   mTutorialRenderTask = mStage.GetRenderTaskList().CreateTask();
133   mTutorialRenderTask.SetCameraActor( uiCamera );
134   mTutorialRenderTask.SetClearEnabled( false );
135   mTutorialRenderTask.SetSourceActor( mUiRoot );
136   mTutorialRenderTask.SetExclusive( true );
137
138   if( !isLandscape )
139   {
140     uiCamera.RotateBy( Degree(90.0f), Vector3( 0.0f, 0.0f, 1.0f ));
141   }
142
143   mLeftLabel.SetPosition( Vector3( -stageSize.x*0.25f, 0.0, 0.0 ) );
144   mRightLabel.SetPosition( Vector3( stageSize.x*0.25f, 0.0, 0.0 ) );
145
146   mUiRoot.Add( mLeftLabel );
147   mUiRoot.Add( mRightLabel );
148   mStage.Add( uiCamera );
149
150   Animation animation = Animation::New( 1.0f );
151   animation.AnimateTo( Property( mLeftLabel, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_OUT );
152   animation.AnimateTo( Property( mRightLabel, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_OUT );
153
154   animation.FinishedSignal().Connect( this, &FppGameTutorialController::OnTutorialAnimationFinished );
155
156   animation.Play();
157 }
158
159 void FppGameTutorialController::OnTutorialAnimationFinished( Animation& animation )
160 {
161   // touch signal will wait for a single touch on each side of screen
162   mStage.TouchSignal().Connect( this, &FppGameTutorialController::OnTouch );
163 }
164
165 void FppGameTutorialController::OnTutorialComplete( Animation& animation )
166 {
167   mStage.Remove( mUiRoot );
168   mUiRoot.Reset();
169   mStage.GetRenderTaskList().RemoveTask( mTutorialRenderTask );
170 }
171