Merge "BloomView & GaussianBlurView demo (for verification purpose only )" into tizen
[platform/core/uifw/dali-demo.git] / examples / bloom-view / bloom-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
47 // buttons
48 const char* BUTTON_BACKGROUND_IMAGE_PATH = DALI_IMAGE_DIR "GreyThinLine30x30-76x78.png";
49
50 // These values depend on the button background image
51 const Vector4 BUTTON_IMAGE_BORDER(16.0f, 16.0f, 16.0f, 20.0f);
52
53
54 const float UI_MARGIN = 4.0f;                              ///< Screen Margin for placement of UI buttons
55 const Vector3 BUTTON_SIZE_CONSTRAINT( 0.24f, 0.09f, 1.0f );
56 const char * BUTTON_QUIT = "Quit";
57
58 } // namespace
59
60
61
62 /**
63  * This example demonstrates a bloom effect.
64  */
65 class TestApp : public ConnectionTracker
66 {
67 public:
68
69   TestApp( Application &application )
70   : mApplication(application)
71   , mCurrentAnimation(0)
72   {
73     application.InitSignal().Connect(this, &TestApp::Create);
74   }
75
76   ~TestApp()
77   {
78   }
79
80 public:
81
82   void Create(Application& application)
83   {
84     // Preload images
85     mDiffuseImage = Image::New( UI_DIFFUSE_IMAGE );
86
87     // Creation is deferred until images have loaded
88     mDiffuseImage.LoadingFinishedSignal().Connect( this, &TestApp::Create2 );
89   }
90
91 #ifdef ADD_REMOVE_FROM_STAGE_TEST
92   void Create2(Image loadedImage)
93   {
94     Stage stage = Stage::GetCurrent();
95     Vector2 stageSize = stage.GetSize();
96
97     mStageRootActor = Actor::New();
98     mStageRootActor.SetParentOrigin(ParentOrigin::CENTER);
99     mStageRootActor.SetSize(stageSize);
100     stage.Add(mStageRootActor);
101
102     // Create the object that will perform the blooming work
103     mBloomView = Dali::Toolkit::BloomView::New();
104     mBloomView.SetParentOrigin(ParentOrigin::CENTER);
105     mBloomView.SetSize(stageSize * 0.75f);
106     mStageRootActor.Add(mBloomView);
107     mBloomView.Activate();
108
109     // Create the background image
110     Image background = Image::New(BACKGROUND_IMAGE_PATH);
111     ImageActor backgroundImage = ImageActor::New(background);
112     backgroundImage.SetParentOrigin(ParentOrigin::CENTER);
113     backgroundImage.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
114
115     Layer backgroundLayer = Layer::New();
116     backgroundLayer.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
117     backgroundLayer.SetParentOrigin( ParentOrigin::CENTER );
118     mBloomView.Add( backgroundLayer );
119     backgroundLayer.Add( backgroundImage );
120
121     Layer foregroundLayer = Layer::New();
122     foregroundLayer.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
123     foregroundLayer.SetParentOrigin( ParentOrigin::CENTER );
124     mBloomView.Add( foregroundLayer );
125
126     // Create visible actors
127     mObjectRootActor = Actor::New();
128     mObjectRootActor.SetParentOrigin( ParentOrigin::CENTER );
129     mObjectRootActor.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
130     foregroundLayer.Add( mObjectRootActor );
131
132     ImageActor imageActor = ImageActor::New( mDiffuseImage );
133     imageActor.SetParentOrigin( ParentOrigin::CENTER );
134     imageActor.SetSize( stageSize * 0.75f);
135     mObjectRootActor.Add( imageActor );
136
137     imageActor = ImageActor::New( mDiffuseImage );
138     imageActor.SetParentOrigin( ParentOrigin::CENTER );
139     imageActor.SetSize( stageSize * 0.5f );
140     imageActor.SetPosition(0.0f, 0.0f, 100.0f);
141     mObjectRootActor.Add( imageActor );
142
143     AnimateBloomView();
144     PulseBloomIntensity();
145
146     // Gesture detection
147     mTapGestureDetector = TapGestureDetector::New();
148     mTapGestureDetector.Attach( mStageRootActor );
149     mTapGestureDetector.DetectedSignal().Connect( this, &TestApp::OnTap );
150   }
151
152   void AnimateBloomView()
153   {
154     if(mRotationAnimation)
155     {
156       mRotationAnimation.Stop();
157     }
158     if(mResizeAnimation)
159     {
160       mResizeAnimation.Stop();
161     }
162     if(mTranslationAnimation)
163     {
164       mTranslationAnimation.Stop();
165     }
166     if(mBlurAnimation)
167     {
168       mBlurAnimation.Stop();
169     }
170
171     // ROTATE
172     float animDuration = 10.0f;
173     mRotationAnimation = Animation::New(animDuration);
174     mRotationAnimation.RotateBy(mBloomView, Degree(720), Vector3::YAXIS, AlphaFunctions::EaseInOut);
175     mRotationAnimation.SetEndAction( Animation::Discard );
176     mRotationAnimation.SetLooping( true );
177     mRotationAnimation.Play();
178
179     // BLUR
180     mBlurAnimation = Animation::New( 4.0f );
181     mBlurAnimation.AnimateTo( Property( mBloomView, mBloomView.GetBlurStrengthPropertyIndex() ), 0.0f, AlphaFunctions::Linear, TimePeriod(0.0f, 0.5f) );
182     mBlurAnimation.AnimateTo( Property( mBloomView, mBloomView.GetBlurStrengthPropertyIndex() ), 1.0f, AlphaFunctions::Linear, TimePeriod(2.0f, 0.5f) );
183     mBlurAnimation.SetEndAction( Animation::Discard );
184     mBlurAnimation.SetLooping( true );
185     mBlurAnimation.Play();
186   }
187 #else //#ifdef ADD_REMOVE_FROM_STAGE_TEST
188
189   void Create2(Image loadedImage)
190   {
191     Stage stage = Stage::GetCurrent();
192     Vector2 stageSize = stage.GetSize();
193
194     mStageRootActor = Actor::New();
195     mStageRootActor.SetParentOrigin(ParentOrigin::CENTER);
196     mStageRootActor.SetSize(stageSize);
197     stage.Add(mStageRootActor);
198
199     // Create the object that will perform the blooming work
200     mBloomView = Dali::Toolkit::BloomView::New();
201     mBloomView.SetParentOrigin(ParentOrigin::CENTER);
202     mBloomView.SetSize(stageSize * 0.75f);
203     mStageRootActor.Add(mBloomView);
204     mBloomView.Activate();
205
206     // Create the Quit button
207     PushButton button;
208     button = CreateButton(BUTTON_QUIT, BUTTON_QUIT, BUTTON_SIZE_CONSTRAINT);
209     button.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
210     button.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
211     button.SetPosition(-UI_MARGIN, -UI_MARGIN);
212
213     // Create the background image
214     Image background = Image::New(BACKGROUND_IMAGE_PATH);
215     ImageActor backgroundImage = ImageActor::New(background);
216     backgroundImage.SetParentOrigin(ParentOrigin::CENTER);
217     backgroundImage.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
218
219     Layer backgroundLayer = Layer::New();
220     backgroundLayer.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
221     backgroundLayer.SetParentOrigin( ParentOrigin::CENTER );
222     mBloomView.Add( backgroundLayer );
223     backgroundLayer.Add( backgroundImage );
224
225     Layer foregroundLayer = Layer::New();
226     foregroundLayer.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
227     foregroundLayer.SetParentOrigin( ParentOrigin::CENTER );
228     mBloomView.Add( foregroundLayer );
229
230     // Create visible actors
231     mObjectRootActor = Actor::New();
232     mObjectRootActor.SetParentOrigin( ParentOrigin::CENTER );
233     mObjectRootActor.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
234     foregroundLayer.Add( mObjectRootActor );
235
236     ImageActor imageActor = ImageActor::New( mDiffuseImage );
237     imageActor.SetParentOrigin( ParentOrigin::CENTER );
238     imageActor.SetSize( stageSize * 0.75f);
239     mObjectRootActor.Add( imageActor );
240
241     imageActor = ImageActor::New( mDiffuseImage );
242     imageActor.SetParentOrigin( ParentOrigin::CENTER );
243     imageActor.SetSize( stageSize * 0.5f );
244     imageActor.SetPosition(0.0f, 0.0f, 100.0f);
245     mObjectRootActor.Add( imageActor );
246
247     ToggleAnimation();
248     PulseBloomIntensity();
249
250     // Gesture detection
251     mTapGestureDetector = TapGestureDetector::New();
252     mTapGestureDetector.Attach( mStageRootActor );
253     mTapGestureDetector.DetectedSignal().Connect( this, &TestApp::OnTap );
254   }
255
256   PushButton CreateButton(const std::string& name, const std::string& label, const Vector3& sizeConstraint)
257   {
258     // Create the button
259     Image img = Image::New(BUTTON_BACKGROUND_IMAGE_PATH);
260     ImageActor background = ImageActor::New(img);
261
262     background.SetStyle(ImageActor::STYLE_NINE_PATCH);
263     background.SetNinePatchBorder(BUTTON_IMAGE_BORDER);
264
265     PushButton button = PushButton::New();
266     button.SetName(name);
267     button.SetBackgroundImage(background);
268
269     // Set the button's size
270     button.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), RelativeToConstraint( sizeConstraint ) ) );
271
272     // Make sure the button only takes up its natural or allocated width.
273     button.SetSizePolicy( Toolkit::Control::Fixed, Toolkit::Control::Fixed );
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 AnimateBloomView()
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(mBloomView, 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(mBloomView, 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( mBloomView, mBloomView.GetBlurStrengthPropertyIndex() ), 0.0f, AlphaFunctions::Linear, TimePeriod(0.0f, 0.5f) );
371     mBlurAnimation.AnimateTo( Property( mBloomView, mBloomView.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(mBloomView, 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( mBloomView, mBloomView.GetBlurStrengthPropertyIndex() ), 0.0f, AlphaFunctions::Linear, TimePeriod(0.0f, 0.5f) );
428     mBlurAnimation.AnimateTo( Property( mBloomView, mBloomView.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       AnimateBloomView();
450       break;
451     }
452   }
453 #endif //#ifdef ADD_REMOVE_FROM_STAGE_TEST
454
455   void PulseBloomIntensity()
456   {
457     mPulseBloomIntensityAnim = Animation::New( 2.5f );
458     mPulseBloomIntensityAnim.AnimateTo( Property(mBloomView, mBloomView.GetBloomIntensityPropertyIndex()), 3.0f, AlphaFunctions::Bounce, TimePeriod(2.5f) );
459     mPulseBloomIntensityAnim.SetEndAction( Animation::Discard );
460     mPulseBloomIntensityAnim.SetLooping( true );
461     mPulseBloomIntensityAnim.Play();
462   }
463
464   void OnTap( Actor actor, const TapGesture& tapGesture )
465   {
466     Stage stage = Stage::GetCurrent();
467
468 #ifdef ADD_REMOVE_FROM_STAGE_TEST
469     if(mBloomView.OnStage())
470     {
471       mStageRootActor.Remove(mBloomView);
472       mBloomView.Deactivate();
473     }
474     else
475     {
476       mStageRootActor.Add(mBloomView);
477       mBloomView.Activate();
478     }
479 #else //#ifdef ADD_REMOVE_FROM_STAGE_TEST
480     ToggleAnimation();
481 #endif //#ifdef ADD_REMOVE_FROM_STAGE_TEST
482   }
483
484 private:
485
486   Application& mApplication;
487
488   TapGestureDetector mTapGestureDetector;
489
490   Actor mStageRootActor;
491
492   Actor mObjectRootActor;
493   Image mDiffuseImage;
494
495   unsigned int mCurrentAnimation;
496   Animation mRotationAnimation;
497   Animation mResizeAnimation;
498   Animation mTranslationAnimation;
499   Animation mBlurAnimation;
500   Animation mPulseBloomIntensityAnim;
501
502   // for rendering the blur / bloom
503   GaussianBlurView mGaussianBlurView;
504   BloomView mBloomView;
505 };
506
507 /*****************************************************************************/
508
509 static void
510 RunTest( Application& application )
511 {
512   TestApp theApp(application);
513   application.MainLoop();
514 }
515
516 /*****************************************************************************/
517
518 int
519 main(int argc, char **argv)
520 {
521   Application application = Application::New(&argc, &argv);
522
523   RunTest(application);
524
525   return 0;
526 }