Further Setter/Getter public API removal from Dali::Actor
[platform/core/uifw/dali-demo.git] / examples / bloom-view / bloom-view-example.cpp
1 /*
2  * Copyright (c) 2019 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 <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <dali-toolkit/devel-api/controls/bloom-view/bloom-view.h>
21
22 using namespace Dali;
23 using namespace Dali::Toolkit;
24
25 namespace
26 {
27
28 const char* BACKGROUND_IMAGE_PATH( DEMO_IMAGE_DIR "desktop_background_1440x2560.png" );
29 const char* UI_DIFFUSE_IMAGE( DEMO_IMAGE_DIR "UI-Leather-DIFF.png" );
30
31 const Rect<int> UI_PIXEL_AREA( 0, 0, 720, 1280 );
32
33 const Rect<int> PANEL1_PIXEL_AREA( 0, 0, 720,  39 );
34 const Rect<int> PANEL2_PIXEL_AREA( 0, 39, 720, 100 );
35
36 const unsigned int NUM_MOVEMENT_ANIMATIONS = 2;
37
38 // for animating bloom intensity on tap gesture
39 const float PULSE_BLOOM_INCREASE_ANIM_TIME = 1.175;
40 const float PULSE_BLOOM_DECREASE_ANIM_TIME = 2.4;
41 const float PULSE_BLOOM_TOTAL_ANIM_TIME = PULSE_BLOOM_INCREASE_ANIM_TIME + PULSE_BLOOM_DECREASE_ANIM_TIME;
42 const float PULSE_BLOOM_INTENSITY_DEFAULT = 1.0f;
43 const float PULSE_BLOOM_INTENSITY_INCREASE = 3.0f;
44
45 // These values depend on the button background image
46 const Vector4 BUTTON_IMAGE_BORDER(16.0f, 16.0f, 16.0f, 20.0f);
47
48 const float UI_MARGIN = 4.0f;                              ///< Screen Margin for placement of UI buttons
49 const Vector3 BUTTON_SIZE_CONSTRAINT( 0.24f, 0.09f, 1.0f );
50
51 } // namespace
52
53 /**
54  * This example demonstrates a bloom effect.
55  */
56 class BloomExample : public ConnectionTracker
57 {
58 public:
59
60   BloomExample( Application &application )
61   : mApplication(application),
62     mCurrentAnimation(0)
63   {
64     application.InitSignal().Connect( this, &BloomExample::Create );
65   }
66
67   ~BloomExample()
68   {
69   }
70
71 public:
72
73   void Create( Application& application )
74   {
75     Stage stage = Stage::GetCurrent();
76     Vector2 stageSize = stage.GetSize();
77     Vector2 viewSize( stageSize );
78
79     mRootActor = Actor::New();
80     mRootActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
81     mRootActor.SetProperty( Actor::Property::SIZE, stageSize );
82     stage.Add( mRootActor );
83
84     // Create the object that will perform the blooming work
85     mBloomView = Dali::Toolkit::BloomView::New();
86     mBloomView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
87     mBloomView.SetProperty( Actor::Property::SIZE,  viewSize );
88     mRootActor.Add( mBloomView );
89     mBloomView.Activate();
90
91     Layer backgroundLayer = Layer::New();
92     backgroundLayer.SetProperty( Actor::Property::SIZE,  viewSize );
93     backgroundLayer.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
94     mBloomView.Add( backgroundLayer );
95
96     // Create the background image
97     ImageView backgroundImage = ImageView::New( BACKGROUND_IMAGE_PATH );
98     backgroundImage.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
99     backgroundImage.SetProperty( Actor::Property::SIZE,  viewSize );
100     backgroundLayer.Add( backgroundImage );
101
102     Layer foregroundLayer = Layer::New();
103     foregroundLayer.SetProperty( Actor::Property::SIZE,  viewSize );
104     foregroundLayer.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
105     mBloomView.Add( foregroundLayer );
106
107     // Create visible actors
108     mObjectRootActor = Actor::New();
109     mObjectRootActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
110     mObjectRootActor.SetProperty( Actor::Property::SIZE,  viewSize );
111     foregroundLayer.Add( mObjectRootActor );
112
113     ImageView imageView = ImageView::New( UI_DIFFUSE_IMAGE );
114     imageView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
115     imageView.SetProperty( Actor::Property::SIZE,  viewSize );
116     mObjectRootActor.Add( imageView );
117
118     imageView = ImageView::New( UI_DIFFUSE_IMAGE );
119     imageView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
120     imageView.SetProperty( Actor::Property::SIZE, stageSize * 0.5f );
121     imageView.SetProperty( Actor::Property::POSITION, Vector3( 0.0f, 0.0f, 100.0f ) );
122     mObjectRootActor.Add( imageView );
123
124     AnimateBloomView();
125     PulseBloomIntensity();
126
127     // Respond to key events
128     stage.KeyEventSignal().Connect( this, &BloomExample::OnKeyEvent );
129   }
130
131   void OnKeyEvent( const KeyEvent& event )
132   {
133     if( event.state == KeyEvent::Down )
134     {
135       if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
136       {
137         mApplication.Quit();
138       }
139     }
140   }
141
142   void AnimateBloomView()
143   {
144     if(mRotationAnimation)
145     {
146       mRotationAnimation.Stop();
147     }
148     if(mResizeAnimation)
149     {
150       mResizeAnimation.Stop();
151     }
152     if(mTranslationAnimation)
153     {
154       mTranslationAnimation.Stop();
155     }
156     if(mBlurAnimation)
157     {
158       mBlurAnimation.Stop();
159     }
160
161     // ROTATE
162     mRotationAnimation = Animation::New( 5.0f );
163     mRotationAnimation.AnimateBy( Property( mObjectRootActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
164     mRotationAnimation.SetEndAction( Animation::Discard );
165     mRotationAnimation.SetLooping( true );
166     mRotationAnimation.Play();
167
168     // TRANSLATE
169     mTranslationAnimation = Animation::New( 7.5f );
170     mTranslationAnimation.AnimateBy( Property(mObjectRootActor, Actor::Property::POSITION), Vector3(100.0f, 0.0f, 0.0f), AlphaFunction::BOUNCE, TimePeriod(2.5f) );
171     mTranslationAnimation.AnimateBy( Property(mObjectRootActor, Actor::Property::POSITION), Vector3(300.0f, 0.0f, 0.0f), AlphaFunction::BOUNCE, TimePeriod(2.5f, 2.5f) );
172     mTranslationAnimation.AnimateBy( Property(mObjectRootActor, Actor::Property::POSITION), Vector3(0.0f, 0.0f, 0.0f),   AlphaFunction::BOUNCE, TimePeriod(5.0f, 2.5f) );
173     mTranslationAnimation.SetEndAction( Animation::Discard );
174     mTranslationAnimation.SetLooping( true );
175     //mTranslationAnimation.Play();
176
177     // BLUR
178     mBlurAnimation = Animation::New( 4.0f );
179     mBlurAnimation.AnimateTo( Property( mBloomView, mBloomView.GetBlurStrengthPropertyIndex() ), 0.0f, AlphaFunction::LINEAR, TimePeriod(0.0f, 0.5f) );
180     mBlurAnimation.AnimateTo( Property( mBloomView, mBloomView.GetBlurStrengthPropertyIndex() ), 1.0f, AlphaFunction::LINEAR, TimePeriod(2.0f, 0.5f) );
181     mBlurAnimation.SetEndAction( Animation::Discard );
182     mBlurAnimation.SetLooping( true );
183     mBlurAnimation.Play();
184   }
185
186   void PulseBloomIntensity()
187   {
188     mPulseBloomIntensityAnim = Animation::New( 2.5f );
189     mPulseBloomIntensityAnim.AnimateTo( Property(mBloomView, mBloomView.GetBloomIntensityPropertyIndex()), 3.0f, AlphaFunction::BOUNCE, TimePeriod(2.5f) );
190     mPulseBloomIntensityAnim.SetEndAction( Animation::Discard );
191     mPulseBloomIntensityAnim.SetLooping( true );
192     mPulseBloomIntensityAnim.Play();
193   }
194
195 private:
196
197   Application& mApplication;
198
199   Actor mRootActor;
200
201   Actor mObjectRootActor;
202
203   unsigned int mCurrentAnimation;
204   Animation mRotationAnimation;
205   Animation mResizeAnimation;
206   Animation mTranslationAnimation;
207   Animation mBlurAnimation;
208   Animation mPulseBloomIntensityAnim;
209
210   BloomView mBloomView;
211 };
212
213 int DALI_EXPORT_API main(int argc, char **argv)
214 {
215   Application application = Application::New( &argc, &argv );
216
217   BloomExample theApp( application );
218   application.MainLoop();
219
220   return 0;
221 }