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