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