Merge "BloomView & GaussianBlurView demo (for verification purpose only )" into tizen
[platform/core/uifw/dali-demo.git] / examples / gaussian-blur-view / gaussian-blur-view-example.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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
20 using namespace Dali;
21 using namespace Dali::Toolkit;
22
23 namespace
24 {
25
26 //#define ADD_REMOVE_FROM_STAGE_TEST
27
28 const char* BACKGROUND_IMAGE_PATH = DALI_IMAGE_DIR "desktop_background_1440x2560.png";
29 const char* UI_DIFFUSE_IMAGE( DALI_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
46 // buttons
47 const char* BUTTON_BACKGROUND_IMAGE_PATH = DALI_IMAGE_DIR "GreyThinLine30x30-76x78.png";
48
49 // These values depend on the button background image
50 const float BUTTON_IMAGE_BORDER_LEFT   = 16.0f;
51 const float BUTTON_IMAGE_BORDER_RIGHT  = 16.0f;
52 const float BUTTON_IMAGE_BORDER_TOP    = 13.0f;
53 const float BUTTON_IMAGE_BORDER_BOTTOM = 20.0f;
54
55 const Vector4 BUTTON_IMAGE_BORDER( BUTTON_IMAGE_BORDER_LEFT, BUTTON_IMAGE_BORDER_TOP, BUTTON_IMAGE_BORDER_RIGHT, BUTTON_IMAGE_BORDER_BOTTOM );
56
57 const float UI_MARGIN = 4.0f;                              ///< Screen Margin for placement of UI buttons
58 const Vector3 BUTTON_SIZE_CONSTRAINT( 0.24f, 0.09f, 1.0f );
59 const char * BUTTON_QUIT = "Quit";
60 const char * BUTTON_QUIT_LABEL ="Quit";
61
62 } // namespace
63
64
65
66 /**
67  * This example demonstrates a blur effect.
68  */
69 class TestApp : public ConnectionTracker
70 {
71 public:
72
73   TestApp( Application &application )
74   : mApplication(application)
75   , mCurrentAnimation(0)
76   {
77     application.InitSignal().Connect(this, &TestApp::Create);
78     application.TerminateSignal().Connect(this, &TestApp::Terminate);
79   }
80
81   ~TestApp()
82   {
83   }
84
85 public:
86
87   void Create(Application& application)
88   {
89     // Preload images
90     mDiffuseImage = Image::New( UI_DIFFUSE_IMAGE );
91
92     // Creation is deferred until images have loaded
93     mDiffuseImage.LoadingFinishedSignal().Connect( this, &TestApp::Create2 );
94   }
95
96 #ifdef ADD_REMOVE_FROM_STAGE_TEST
97   void Create2(Image loadedImage)
98   {
99     Stage stage = Stage::GetCurrent();
100     Vector2 stageSize = stage.GetSize();
101
102     mStageRootActor = Actor::New();
103     mStageRootActor.SetParentOrigin(ParentOrigin::CENTER);
104     mStageRootActor.SetSize(stageSize);
105     stage.Add(mStageRootActor);
106
107     // Create the object that will perform the blurring work
108     mGaussianBlurView = Dali::Toolkit::GaussianBlurView::New();
109     mGaussianBlurView.SetParentOrigin(ParentOrigin::CENTER);
110     mGaussianBlurView.SetSize(stageSize * 0.75f);
111     mStageRootActor.Add(mGaussianBlurView);
112     mGaussianBlurView.Activate();
113
114     // Create the background image
115     Image background = Image::New(BACKGROUND_IMAGE_PATH);
116     ImageActor backgroundImage = ImageActor::New(background);
117     backgroundImage.SetParentOrigin(ParentOrigin::CENTER);
118     backgroundImage.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
119
120     Layer backgroundLayer = Layer::New();
121     backgroundLayer.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
122     backgroundLayer.SetParentOrigin( ParentOrigin::CENTER );
123     mGaussianBlurView.Add( backgroundLayer );
124     backgroundLayer.Add( backgroundImage );
125
126     Layer foregroundLayer = Layer::New();
127     foregroundLayer.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
128     foregroundLayer.SetParentOrigin( ParentOrigin::CENTER );
129     mGaussianBlurView.Add( foregroundLayer );
130
131     // Create visible actors
132     mObjectRootActor = Actor::New();
133     mObjectRootActor.SetParentOrigin( ParentOrigin::CENTER );
134     mObjectRootActor.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
135     foregroundLayer.Add( mObjectRootActor );
136
137     ImageActor imageActor = ImageActor::New( mDiffuseImage );
138     imageActor.SetParentOrigin( ParentOrigin::CENTER );
139     imageActor.SetSize( stageSize * 0.75f);
140     mObjectRootActor.Add( imageActor );
141
142     imageActor = ImageActor::New( mDiffuseImage );
143     imageActor.SetParentOrigin( ParentOrigin::CENTER );
144     imageActor.SetSize( stageSize * 0.5f );
145     imageActor.SetPosition(0.0f, 0.0f, 100.0f);
146     mObjectRootActor.Add( imageActor );
147
148     AnimateGaussianBlurView();
149
150     // Gesture detection
151     mTapGestureDetector = TapGestureDetector::New();
152     mTapGestureDetector.Attach( mStageRootActor );
153     mTapGestureDetector.DetectedSignal().Connect( this, &TestApp::OnTap );
154   }
155
156   void AnimateGaussianBlurView()
157   {
158     if(mRotationAnimation)
159     {
160       mRotationAnimation.Stop();
161     }
162     if(mResizeAnimation)
163     {
164       mResizeAnimation.Stop();
165     }
166     if(mTranslationAnimation)
167     {
168       mTranslationAnimation.Stop();
169     }
170     if(mBlurAnimation)
171     {
172       mBlurAnimation.Stop();
173     }
174
175     // ROTATE
176     float animDuration = 10.0f;
177     mRotationAnimation = Animation::New(animDuration);
178     mRotationAnimation.RotateBy(mGaussianBlurView, Degree(720), Vector3::YAXIS, AlphaFunctions::EaseInOut);
179     mRotationAnimation.SetEndAction( Animation::Discard );
180     mRotationAnimation.SetLooping( true );
181     mRotationAnimation.Play();
182
183     // BLUR
184     mBlurAnimation = Animation::New( 4.0f );
185     mBlurAnimation.AnimateTo( Property( mGaussianBlurView, mGaussianBlurView.GetBlurStrengthPropertyIndex() ), 0.0f, AlphaFunctions::Linear, TimePeriod(0.0f, 0.5f) );
186     mBlurAnimation.AnimateTo( Property( mGaussianBlurView, mGaussianBlurView.GetBlurStrengthPropertyIndex() ), 1.0f, AlphaFunctions::Linear, TimePeriod(2.0f, 0.5f) );
187     mBlurAnimation.SetEndAction( Animation::Discard );
188     mBlurAnimation.SetLooping( true );
189     mBlurAnimation.Play();
190   }
191 #else //#ifdef ADD_REMOVE_FROM_STAGE_TEST
192
193   void Create2(Image loadedImage)
194   {
195     Stage stage = Stage::GetCurrent();
196     Vector2 stageSize = stage.GetSize();
197
198     mStageRootActor = Actor::New();
199     mStageRootActor.SetParentOrigin(ParentOrigin::CENTER);
200     mStageRootActor.SetSize(stageSize);
201     stage.Add(mStageRootActor);
202
203     // Create the object that will perform the blurring work
204     mGaussianBlurView = Dali::Toolkit::GaussianBlurView::New();
205     mGaussianBlurView.SetParentOrigin(ParentOrigin::CENTER);
206     mGaussianBlurView.SetSize(stageSize * 0.75f);
207     mStageRootActor.Add(mGaussianBlurView);
208     mGaussianBlurView.Activate();
209
210     // Create the Quit button
211     PushButton button;
212     button = CreateButton(BUTTON_QUIT, BUTTON_QUIT_LABEL, BUTTON_SIZE_CONSTRAINT);
213     button.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
214     button.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
215     button.SetPosition(-UI_MARGIN, -UI_MARGIN);
216
217     // Create the background image
218     Image background = Image::New(BACKGROUND_IMAGE_PATH);
219     ImageActor backgroundImage = ImageActor::New(background);
220     backgroundImage.SetParentOrigin(ParentOrigin::CENTER);
221     backgroundImage.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
222
223     Layer backgroundLayer = Layer::New();
224     backgroundLayer.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
225     backgroundLayer.SetParentOrigin( ParentOrigin::CENTER );
226     mGaussianBlurView.Add( backgroundLayer );
227     backgroundLayer.Add( backgroundImage );
228
229     Layer foregroundLayer = Layer::New();
230     foregroundLayer.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
231     foregroundLayer.SetParentOrigin( ParentOrigin::CENTER );
232     mGaussianBlurView.Add( foregroundLayer );
233
234     // Create visible actors
235     mObjectRootActor = Actor::New();
236     mObjectRootActor.SetParentOrigin( ParentOrigin::CENTER );
237     mObjectRootActor.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
238     foregroundLayer.Add( mObjectRootActor );
239
240     ImageActor imageActor = ImageActor::New( mDiffuseImage );
241     imageActor.SetParentOrigin( ParentOrigin::CENTER );
242     imageActor.SetSize( stageSize * 0.75f);
243     mObjectRootActor.Add( imageActor );
244
245     imageActor = ImageActor::New( mDiffuseImage );
246     imageActor.SetParentOrigin( ParentOrigin::CENTER );
247     imageActor.SetSize( stageSize * 0.5f );
248     imageActor.SetPosition(0.0f, 0.0f, 100.0f);
249     mObjectRootActor.Add( imageActor );
250
251     ToggleAnimation();
252
253     // Gesture detection
254     mTapGestureDetector = TapGestureDetector::New();
255     mTapGestureDetector.Attach( mStageRootActor );
256     mTapGestureDetector.DetectedSignal().Connect( this, &TestApp::OnTap );
257   }
258
259   PushButton CreateButton(const std::string& name, const std::string& label, const Vector3& sizeConstraint)
260   {
261     // Create the button
262     Image img = Image::New(BUTTON_BACKGROUND_IMAGE_PATH);
263     ImageActor background = ImageActor::New(img);
264
265     background.SetStyle(ImageActor::STYLE_NINE_PATCH);
266     background.SetNinePatchBorder(BUTTON_IMAGE_BORDER);
267
268     PushButton button = PushButton::New();
269     button.SetName(name);
270     button.SetBackgroundImage(background);
271
272     // Set the button's size
273     button.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), RelativeToConstraint( sizeConstraint ) ) );
274
275     mStageRootActor.Add(button);
276
277     button.ClickedSignal().Connect(this, &TestApp::OnButtonTouchEvent);
278
279     SetButtonLabel(button, label, TextView::Center);
280
281     return button;
282   }
283
284   void SetButtonLabel(PushButton button, const std::string& label, const TextView::LineJustification justification)
285   {
286     Font font = Font::New();
287     Vector3 textSize = font.MeasureText(label);
288
289     // Add a text label to the button
290     TextView textView = TextView::New( label );
291     textView.SetAnchorPoint( AnchorPoint::CENTER );
292     textView.SetParentOrigin( ParentOrigin::CENTER );
293     textView.SetMultilinePolicy(TextView::SplitByWord);
294     textView.SetWidthExceedPolicy(TextView::Original);
295     textView.SetTextAlignment(static_cast< Alignment::Type >( Alignment::HorizontalCenter | Alignment::VerticalCenter ));
296     textView.SetLineJustification(justification);
297     textView.SetSize(textSize);
298
299     Alignment align = Alignment::New();
300     align.Add(textView);
301     align.SetScaling( Alignment::ShrinkToFitKeepAspect );
302
303     button.SetLabelText(align);
304   }
305
306   /**
307    * Signal handler, called when the button is pressed
308    */
309   bool OnButtonTouchEvent(Button button)
310   {
311     if(button.GetName() == BUTTON_QUIT)
312     {
313       // quit the application
314       mApplication.Quit();
315     }
316
317     return true;
318   }
319
320   void AnimateGaussianBlurView()
321   {
322     if(mRotationAnimation)
323     {
324       mRotationAnimation.Stop();
325     }
326     if(mResizeAnimation)
327     {
328       mResizeAnimation.Stop();
329     }
330     if(mTranslationAnimation)
331     {
332       mTranslationAnimation.Stop();
333     }
334     if(mBlurAnimation)
335     {
336       mBlurAnimation.Stop();
337     }
338
339     // ROTATE
340     float animDuration = 10.0f;
341     mRotationAnimation = Animation::New(animDuration);
342     mRotationAnimation.RotateBy(mGaussianBlurView, Degree(720), Vector3::YAXIS, AlphaFunctions::EaseInOut);
343     mRotationAnimation.SetEndAction( Animation::Discard );
344     mRotationAnimation.SetLooping( true );
345     mRotationAnimation.Play();
346
347
348     // RESIZE
349     Stage stage = Stage::GetCurrent();
350     Vector3 stageSize(stage.GetSize().width, stage.GetSize().height, 1.0f);
351     mResizeAnimation = Animation::New( 7.5f );
352     mResizeAnimation.AnimateTo( Property(mGaussianBlurView, Actor::SIZE), stageSize * Vector3(0.5f, 0.5f, 1.0f), AlphaFunctions::Bounce, TimePeriod(2.5f) );
353     mResizeAnimation.SetEndAction( Animation::Discard );
354     mResizeAnimation.SetLooping( true );
355     mResizeAnimation.Play();
356
357
358     // TRANSLATE
359     mTranslationAnimation = Animation::New( 7.5f );
360     mTranslationAnimation.AnimateBy( Property(mObjectRootActor, Actor::POSITION), Vector3(100.0f, 0.0f, 0.0f), AlphaFunctions::Bounce, TimePeriod(2.5f) );
361     mTranslationAnimation.AnimateBy( Property(mObjectRootActor, Actor::POSITION), Vector3(300.0f, 0.0f, 0.0f), AlphaFunctions::Bounce, TimePeriod(2.5f, 2.5f) );
362     mTranslationAnimation.AnimateBy( Property(mObjectRootActor, Actor::POSITION), Vector3(0.0f, 0.0f, 0.0f), AlphaFunctions::Bounce, TimePeriod(5.0f, 2.5f) );
363     mTranslationAnimation.SetEndAction( Animation::Discard );
364     mTranslationAnimation.SetLooping( true );
365     mTranslationAnimation.Play();
366
367
368     // BLUR
369     mBlurAnimation = Animation::New( 4.0f );
370     mBlurAnimation.AnimateTo( Property( mGaussianBlurView, mGaussianBlurView.GetBlurStrengthPropertyIndex() ), 0.0f, AlphaFunctions::Linear, TimePeriod(0.0f, 0.5f) );
371     mBlurAnimation.AnimateTo( Property( mGaussianBlurView, mGaussianBlurView.GetBlurStrengthPropertyIndex() ), 1.0f, AlphaFunctions::Linear, TimePeriod(2.0f, 0.5f) );
372     mBlurAnimation.SetEndAction( Animation::Discard );
373     mBlurAnimation.SetLooping( true );
374     mBlurAnimation.Play();
375   }
376
377   void AnimateActorRoot()
378   {
379     if(mRotationAnimation)
380     {
381       mRotationAnimation.Stop();
382     }
383     if(mResizeAnimation)
384     {
385       mResizeAnimation.Stop();
386     }
387     if(mTranslationAnimation)
388     {
389       mTranslationAnimation.Stop();
390     }
391     if(mBlurAnimation)
392     {
393       mBlurAnimation.Stop();
394     }
395
396     // ROTATE
397     float animDuration = 10.0f;
398     mRotationAnimation = Animation::New(animDuration);
399     mRotationAnimation.RotateBy(mObjectRootActor, Degree(720), Vector3::YAXIS, AlphaFunctions::EaseInOut);
400     mRotationAnimation.SetEndAction( Animation::Discard );
401     mRotationAnimation.SetLooping( true );
402     mRotationAnimation.Play();
403
404
405     // RESIZE
406     Stage stage = Stage::GetCurrent();
407     Vector3 stageSize(stage.GetSize().width, stage.GetSize().height, 1.0f);
408     mResizeAnimation = Animation::New( 7.5f );
409     mResizeAnimation.AnimateTo( Property(mGaussianBlurView, Actor::SIZE), stageSize * Vector3(0.5f, 0.5f, 1.0f), AlphaFunctions::Bounce, TimePeriod(2.5f) );
410     mResizeAnimation.SetEndAction( Animation::Discard );
411     mResizeAnimation.SetLooping( true );
412     mResizeAnimation.Play();
413
414
415     // TRANSLATE
416     mTranslationAnimation = Animation::New( 7.5f );
417     mTranslationAnimation.AnimateBy( Property(mObjectRootActor, Actor::POSITION), Vector3(100.0f, 0.0f, 0.0f), AlphaFunctions::Bounce, TimePeriod(2.5f) );
418     mTranslationAnimation.AnimateBy( Property(mObjectRootActor, Actor::POSITION), Vector3(300.0f, 0.0f, 0.0f), AlphaFunctions::Bounce, TimePeriod(2.5f, 2.5f) );
419     mTranslationAnimation.AnimateBy( Property(mObjectRootActor, Actor::POSITION), Vector3(0.0f, 0.0f, 0.0f), AlphaFunctions::Bounce, TimePeriod(5.0f, 2.5f) );
420     mTranslationAnimation.SetEndAction( Animation::Discard );
421     mTranslationAnimation.SetLooping( true );
422     mTranslationAnimation.Play();
423
424
425     // BLUR
426     mBlurAnimation = Animation::New( 4.0f );
427     mBlurAnimation.AnimateTo( Property( mGaussianBlurView, mGaussianBlurView.GetBlurStrengthPropertyIndex() ), 0.0f, AlphaFunctions::Linear, TimePeriod(0.0f, 0.5f) );
428     mBlurAnimation.AnimateTo( Property( mGaussianBlurView, mGaussianBlurView.GetBlurStrengthPropertyIndex() ), 1.0f, AlphaFunctions::Linear, TimePeriod(2.0f, 0.5f) );
429     mBlurAnimation.SetEndAction( Animation::Discard );
430     mBlurAnimation.SetLooping( true );
431     mBlurAnimation.Play();
432   }
433
434   void ToggleAnimation()
435   {
436     mCurrentAnimation++;
437     if(mCurrentAnimation == NUM_MOVEMENT_ANIMATIONS)
438     {
439       mCurrentAnimation = 0;
440     }
441
442     switch(mCurrentAnimation)
443     {
444     case 0:
445       AnimateActorRoot();
446       break;
447
448     case 1:
449       AnimateGaussianBlurView();
450       break;
451     }
452   }
453 #endif //#ifdef ADD_REMOVE_FROM_STAGE_TEST
454
455   void Terminate( Application& app )
456   {
457   }
458
459   void OnTap( Actor actor, const TapGesture& tapGesture )
460   {
461     Stage stage = Stage::GetCurrent();
462
463 #ifdef ADD_REMOVE_FROM_STAGE_TEST
464     if(mGaussianBlurView.OnStage())
465     {
466       mStageRootActor.Remove(mGaussianBlurView);
467       mGaussianBlurView.Deactivate();
468     }
469     else
470     {
471       mStageRootActor.Add(mGaussianBlurView);
472       mGaussianBlurView.Activate();
473     }
474 #else //#ifdef ADD_REMOVE_FROM_STAGE_TEST
475     ToggleAnimation();
476 #endif //#ifdef ADD_REMOVE_FROM_STAGE_TEST
477   }
478
479 private:
480
481   Application& mApplication;
482
483   TapGestureDetector mTapGestureDetector;
484
485   Actor mStageRootActor;
486
487   Actor mObjectRootActor;
488   Image mDiffuseImage;
489
490   unsigned int mCurrentAnimation;
491   Animation mRotationAnimation;
492   Animation mResizeAnimation;
493   Animation mTranslationAnimation;
494   Animation mBlurAnimation;
495   Animation mPulseBloomIntensityAnim;
496
497   // for rendering the blur / bloom
498   GaussianBlurView mGaussianBlurView;
499   BloomView mBloomView;
500 };
501
502 /*****************************************************************************/
503
504 static void
505 RunTest( Application& application )
506 {
507   TestApp theApp(application);
508   application.MainLoop();
509 }
510
511 /*****************************************************************************/
512
513 int
514 main(int argc, char **argv)
515 {
516   Application application = Application::New(&argc, &argv);
517
518   RunTest(application);
519
520   return 0;
521 }