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