d466e9fc2104582f846f6ef194abf8407a860363
[platform/core/uifw/dali-demo.git] / examples / super-blur-bloom / super-blur-bloom-example.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 #include <dali/dali.h>
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/controls/bloom-view/bloom-view.h>
20 #include <dali-toolkit/devel-api/controls/buttons/button-devel.h>
21 #include <dali-toolkit/devel-api/controls/super-blur-view/super-blur-view.h>
22 #include "shared/view.h"
23 #include "shared/utility.h"
24
25 using namespace Dali;
26
27 namespace
28 {
29 const char * const TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
30 const char * const TITLE_SUPER_BLUR( "Super Blur" );
31 const char * const TITLE_BLOOM( "Bloom" );
32 const char * const CHANGE_BACKGROUND_ICON( DEMO_IMAGE_DIR "icon-change.png" );
33 const char * const CHANGE_BACKGROUND_ICON_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
34 const char * const CHANGE_BLUR_ICON( DEMO_IMAGE_DIR "icon-replace.png" );
35 const char * const CHANGE_BLUR_ICON_SELECTED( DEMO_IMAGE_DIR "icon-replace-selected.png" );
36
37 const char* BACKGROUND_IMAGES[]=
38 {
39   DEMO_IMAGE_DIR "background-1.jpg",
40   DEMO_IMAGE_DIR "background-2.jpg",
41   DEMO_IMAGE_DIR "background-3.jpg",
42   DEMO_IMAGE_DIR "background-4.jpg",
43   DEMO_IMAGE_DIR "background-5.jpg",
44   DEMO_IMAGE_DIR "background-magnifier.jpg",
45 };
46 const unsigned int NUM_BACKGROUND_IMAGES( sizeof( BACKGROUND_IMAGES ) / sizeof( BACKGROUND_IMAGES[0] ) );
47
48 }
49
50 class BlurExample : public ConnectionTracker
51 {
52 public:
53   BlurExample(Application &app)
54   : mApp(app),
55     mImageIndex( 0 ),
56     mIsBlurring( false )
57   {
58     // Connect to the Application's Init signal
59     app.InitSignal().Connect(this, &BlurExample::Create);
60   }
61
62   ~BlurExample()
63   {
64   }
65 private:
66   // The Init signal is received once (only) during the Application lifetime
67   void Create(Application& app)
68   {
69     Stage stage = Stage::GetCurrent();
70     Vector2 stageSize = stage.GetSize();
71
72     stage.KeyEventSignal().Connect(this, &BlurExample::OnKeyEvent);
73
74     // Creates a default view with a default tool bar.
75     // The view is added to the stage.
76     Layer content = DemoHelper::CreateView( app,
77                                             mBackground,
78                                             mToolBar,
79                                             "",
80                                             TOOLBAR_IMAGE,
81                                             "" );
82
83     // Add a button to change background. (right of toolbar)
84     Toolkit::PushButton changeBackgroundButton = Toolkit::PushButton::New();
85     changeBackgroundButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, CHANGE_BACKGROUND_ICON );
86     changeBackgroundButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, CHANGE_BACKGROUND_ICON_SELECTED );
87
88     changeBackgroundButton.ClickedSignal().Connect( this, &BlurExample::OnChangeBackgroundIconClicked );
89     mToolBar.AddControl( changeBackgroundButton,
90         DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
91         Toolkit::Alignment::HorizontalRight,
92         DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
93
94     // Add a button to change the blur view. (left of toolbar)
95     Toolkit::PushButton changeBlurButton = Toolkit::PushButton::New();
96     changeBlurButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, CHANGE_BLUR_ICON );
97     changeBlurButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, CHANGE_BLUR_ICON_SELECTED );
98
99     changeBlurButton.ClickedSignal().Connect( this, &BlurExample::OnChangeBlurIconClicked );
100     mToolBar.AddControl( changeBlurButton,
101         DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
102         Toolkit::Alignment::HorizontalLeft,
103         DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
104
105     mSuperBlurView = Toolkit::SuperBlurView::New( 5 );
106     mSuperBlurView.SetSize( stageSize );
107     mSuperBlurView.SetParentOrigin( ParentOrigin::CENTER );
108     mSuperBlurView.SetAnchorPoint( AnchorPoint::CENTER );
109     mSuperBlurView.BlurFinishedSignal().Connect(this, &BlurExample::OnBlurFinished);
110     mCurrentImage = DemoHelper::LoadStageFillingImage( BACKGROUND_IMAGES[mImageIndex] );
111     mSuperBlurView.SetImage( mCurrentImage );
112     mBackground.Add( mSuperBlurView );
113     mIsBlurring = true;
114     SetTitle( TITLE_SUPER_BLUR );
115
116     mBloomView = Toolkit::BloomView::New();
117     mBloomView.SetParentOrigin(ParentOrigin::CENTER);
118     mBloomView.SetSize(stageSize);
119     mBloomActor = Toolkit::ImageView::New(mCurrentImage);
120     mBloomActor.SetParentOrigin( ParentOrigin::CENTER );
121     mBloomView.Add( mBloomActor );
122
123     // Connect the callback to the touch signal on the background
124     mSuperBlurView.TouchSignal().Connect( this, &BlurExample::OnTouch );
125     mBloomView.TouchSignal().Connect( this, &BlurExample::OnTouch );
126   }
127
128   // Callback function of the touch signal on the background
129   bool OnTouch(Dali::Actor actor, const Dali::TouchData& event)
130   {
131     switch( event.GetState( 0 ) )
132     {
133       case PointState::DOWN:
134       {
135         if( mAnimation )
136         {
137           mAnimation.Clear();
138         }
139
140         mAnimation = Animation::New( 2.f );
141         if( mSuperBlurView.OnStage() )
142         {
143           mAnimation.AnimateTo( Property( mSuperBlurView, mSuperBlurView.GetBlurStrengthPropertyIndex() ), 1.f );
144         }
145         else
146         {
147           mAnimation.AnimateTo( Property( mBloomView, mBloomView.GetBloomIntensityPropertyIndex() ), 3.f );
148         }
149         mAnimation.Play();
150         break;
151       }
152       case PointState::UP:
153       case PointState::LEAVE:
154       case PointState::INTERRUPTED:
155       {
156         if( mAnimation )
157         {
158           mAnimation.Clear();
159         }
160
161         mAnimation = Animation::New( 2.f );
162         if( mSuperBlurView.OnStage() )
163         {
164           mAnimation.AnimateTo( Property( mSuperBlurView, mSuperBlurView.GetBlurStrengthPropertyIndex() ), 0.f );
165         }
166         else
167         {
168           mAnimation.AnimateTo( Property( mBloomView, mBloomView.GetBloomIntensityPropertyIndex() ), 0.f );
169         }
170         mAnimation.Play();
171         break;
172       }
173       case PointState::MOTION:
174       case PointState::STATIONARY:
175       {
176         break;
177       }
178     }
179     return true;
180   }
181
182   /**
183    * Main key event handler
184    */
185   void OnKeyEvent(const KeyEvent& event)
186   {
187     if(event.state == KeyEvent::Down)
188     {
189       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
190       {
191         mApp.Quit();
192       }
193     }
194   }
195
196   bool OnChangeBackgroundIconClicked( Toolkit::Button button )
197   {
198     if( mIsBlurring )
199     {
200       return true;
201     }
202
203     if( mAnimation )
204     {
205       mAnimation.Clear();
206     }
207
208     mImageIndex = (mImageIndex+1u)%NUM_BACKGROUND_IMAGES;
209     mCurrentImage = DemoHelper::LoadStageFillingImage( BACKGROUND_IMAGES[mImageIndex] );
210
211     if( mSuperBlurView.OnStage() )
212     {
213       mIsBlurring = true;
214
215       mSuperBlurView.SetBlurStrength( 0.f );
216       mSuperBlurView.SetImage( mCurrentImage );
217     }
218     else
219     {
220       mBloomView.SetProperty( mBloomView.GetBloomIntensityPropertyIndex(), 0.f );
221       mBloomActor.SetImage( mCurrentImage );
222     }
223
224     return true;
225   }
226
227   bool OnChangeBlurIconClicked( Toolkit::Button button )
228   {
229     if( mSuperBlurView.OnStage() )
230     {
231       SetTitle( TITLE_BLOOM );
232       mBackground.Remove( mSuperBlurView );
233
234       mBloomActor.SetImage( mCurrentImage );
235       mBloomView.SetProperty( mBloomView.GetBloomIntensityPropertyIndex(), 0.f );
236       mBackground.Add( mBloomView );
237       mBloomView.Activate();
238
239     }
240     else
241     {
242       SetTitle( TITLE_SUPER_BLUR );
243       mBackground.Remove( mBloomView );
244       mBloomView.Deactivate();
245
246       mBackground.Add( mSuperBlurView );
247       mSuperBlurView.SetBlurStrength( 0.f );
248       mSuperBlurView.SetImage( mCurrentImage );
249       mIsBlurring = true;
250     }
251
252     return true;
253   }
254
255   void OnBlurFinished( Toolkit::SuperBlurView blurView )
256   {
257     mIsBlurring = false;
258   }
259
260   /**
261    * Sets/Updates the title of the View
262    * @param[in] title The new title for the view.
263    */
264   void SetTitle(const std::string& title)
265   {
266     if(!mTitleActor)
267     {
268       mTitleActor = DemoHelper::CreateToolBarLabel( title );
269       // Add title to the tool bar.
270       mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
271     }
272
273     mTitleActor.SetProperty( Toolkit::TextLabel::Property::TEXT, title );
274   }
275
276 private:
277
278   Application&               mApp;
279   Toolkit::ToolBar           mToolBar;
280   Toolkit::TextLabel         mTitleActor;             ///< The Toolbar's Title.
281   Toolkit::Control           mBackground;
282   Toolkit::SuperBlurView     mSuperBlurView;
283   Toolkit::BloomView         mBloomView;
284   Animation                  mAnimation;
285   Toolkit::ImageView         mBloomActor;
286   Image                      mCurrentImage;
287   unsigned int               mImageIndex;
288   bool                       mIsBlurring;
289 };
290
291 /*****************************************************************************/
292
293 static void
294 RunTest(Application& app)
295 {
296   BlurExample theApp(app);
297   app.MainLoop();
298 }
299
300 /*****************************************************************************/
301
302 int DALI_EXPORT_API main(int argc, char **argv)
303 {
304   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
305
306   RunTest(app);
307
308   return 0;
309 }