Merge "Simple TextEditor demo." into devel/master
[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.TouchSignal().Connect( this, &BlurExample::OnTouch );
134     mBloomView.TouchSignal().Connect( this, &BlurExample::OnTouch );
135   }
136
137   // Callback function of the touch signal on the background
138   bool OnTouch(Dali::Actor actor, const Dali::TouchData& event)
139   {
140     switch( event.GetState( 0 ) )
141     {
142       case PointState::DOWN:
143       {
144         if( mAnimation )
145         {
146           mAnimation.Clear();
147         }
148
149         mAnimation = Animation::New( 2.f );
150         if( mSuperBlurView.OnStage() )
151         {
152           mAnimation.AnimateTo( Property( mSuperBlurView, mSuperBlurView.GetBlurStrengthPropertyIndex() ), 1.f );
153         }
154         else
155         {
156           mAnimation.AnimateTo( Property( mBloomView, mBloomView.GetBloomIntensityPropertyIndex() ), 3.f );
157         }
158         mAnimation.Play();
159         break;
160       }
161       case PointState::UP:
162       case PointState::LEAVE:
163       case PointState::INTERRUPTED:
164       {
165         if( mAnimation )
166         {
167           mAnimation.Clear();
168         }
169
170         mAnimation = Animation::New( 2.f );
171         if( mSuperBlurView.OnStage() )
172         {
173           mAnimation.AnimateTo( Property( mSuperBlurView, mSuperBlurView.GetBlurStrengthPropertyIndex() ), 0.f );
174         }
175         else
176         {
177           mAnimation.AnimateTo( Property( mBloomView, mBloomView.GetBloomIntensityPropertyIndex() ), 0.f );
178         }
179         mAnimation.Play();
180         break;
181       }
182       case PointState::MOTION:
183       case PointState::STATIONARY:
184       {
185         break;
186       }
187     }
188     return true;
189   }
190
191   /**
192    * Main key event handler
193    */
194   void OnKeyEvent(const KeyEvent& event)
195   {
196     if(event.state == KeyEvent::Down)
197     {
198       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
199       {
200         mApp.Quit();
201       }
202     }
203   }
204
205   bool OnChangeBackgroundIconClicked( Toolkit::Button button )
206   {
207     if( mIsBlurring )
208     {
209       return true;
210     }
211
212     if( mAnimation )
213     {
214       mAnimation.Clear();
215     }
216
217     mImageIndex = (mImageIndex+1u)%NUM_BACKGROUND_IMAGES;
218     mCurrentImage = LoadStageFillingImage( BACKGROUND_IMAGES[mImageIndex] );
219
220     if( mSuperBlurView.OnStage() )
221     {
222       mIsBlurring = true;
223
224       mSuperBlurView.SetBlurStrength( 0.f );
225       mSuperBlurView.SetImage( mCurrentImage );
226     }
227     else
228     {
229       mBloomView.SetProperty( mBloomView.GetBloomIntensityPropertyIndex(), 0.f );
230       mBloomActor.SetImage( mCurrentImage );
231     }
232
233     return true;
234   }
235
236   bool OnChangeBlurIconClicked( Toolkit::Button button )
237   {
238     if( mSuperBlurView.OnStage() )
239     {
240       SetTitle( TITLE_BLOOM );
241       mBackground.Remove( mSuperBlurView );
242
243       mBloomActor.SetImage( mCurrentImage );
244       mBloomView.SetProperty( mBloomView.GetBloomIntensityPropertyIndex(), 0.f );
245       mBackground.Add( mBloomView );
246       mBloomView.Activate();
247
248     }
249     else
250     {
251       SetTitle( TITLE_SUPER_BLUR );
252       mBackground.Remove( mBloomView );
253       mBloomView.Deactivate();
254
255       mBackground.Add( mSuperBlurView );
256       mSuperBlurView.SetBlurStrength( 0.f );
257       mSuperBlurView.SetImage( mCurrentImage );
258       mIsBlurring = true;
259     }
260
261     return true;
262   }
263
264   void OnBlurFinished( Toolkit::SuperBlurView blurView )
265   {
266     mIsBlurring = false;
267   }
268
269   /**
270    * Sets/Updates the title of the View
271    * @param[in] title The new title for the view.
272    */
273   void SetTitle(const std::string& title)
274   {
275     if(!mTitleActor)
276     {
277       mTitleActor = DemoHelper::CreateToolBarLabel( title );
278       // Add title to the tool bar.
279       mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
280     }
281
282     mTitleActor.SetProperty( Toolkit::TextLabel::Property::TEXT, title );
283   }
284
285 private:
286
287   Application&               mApp;
288   Toolkit::ToolBar           mToolBar;
289   Toolkit::TextLabel         mTitleActor;             ///< The Toolbar's Title.
290   Toolkit::Control           mBackground;
291   Toolkit::SuperBlurView     mSuperBlurView;
292   Toolkit::BloomView         mBloomView;
293   Animation                  mAnimation;
294   Toolkit::ImageView         mBloomActor;
295   Image                      mCurrentImage;
296   unsigned int               mImageIndex;
297   bool                       mIsBlurring;
298 };
299
300 /*****************************************************************************/
301
302 static void
303 RunTest(Application& app)
304 {
305   BlurExample theApp(app);
306   app.MainLoop();
307 }
308
309 /*****************************************************************************/
310
311 int DALI_EXPORT_API main(int argc, char **argv)
312 {
313   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
314
315   RunTest(app);
316
317   return 0;
318 }