2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
17 #include <dali/dali.h>
18 #include <dali-toolkit/dali-toolkit.h>
21 using namespace Dali::Toolkit;
26 //#define ADD_REMOVE_FROM_STAGE_TEST
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" );
31 const Rect<int> UI_PIXEL_AREA( 0, 0, 720, 1280 );
33 const Rect<int> PANEL1_PIXEL_AREA( 0, 0, 720, 39 );
34 const Rect<int> PANEL2_PIXEL_AREA( 0, 39, 720, 100 );
36 const unsigned int NUM_MOVEMENT_ANIMATIONS = 2;
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;
48 const char* BUTTON_BACKGROUND_IMAGE_PATH = DALI_IMAGE_DIR "GreyThinLine30x30-76x78.png";
50 // These values depend on the button background image
51 const Vector4 BUTTON_IMAGE_BORDER(16.0f, 16.0f, 16.0f, 20.0f);
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";
63 * This example demonstrates a bloom effect.
65 class TestApp : public ConnectionTracker
69 TestApp( Application &application )
70 : mApplication(application)
71 , mCurrentAnimation(0)
73 application.InitSignal().Connect(this, &TestApp::Create);
82 void Create(Application& application)
85 mDiffuseImage = Image::New( UI_DIFFUSE_IMAGE );
87 // Creation is deferred until images have loaded
88 mDiffuseImage.LoadingFinishedSignal().Connect( this, &TestApp::Create2 );
91 #ifdef ADD_REMOVE_FROM_STAGE_TEST
92 void Create2(Image loadedImage)
94 Stage stage = Stage::GetCurrent();
95 Vector2 stageSize = stage.GetSize();
97 mStageRootActor = Actor::New();
98 mStageRootActor.SetParentOrigin(ParentOrigin::CENTER);
99 mStageRootActor.SetSize(stageSize);
100 stage.Add(mStageRootActor);
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();
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() ) );
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 );
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 );
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 );
132 ImageActor imageActor = ImageActor::New( mDiffuseImage );
133 imageActor.SetParentOrigin( ParentOrigin::CENTER );
134 imageActor.SetSize( stageSize * 0.75f);
135 mObjectRootActor.Add( imageActor );
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 );
144 PulseBloomIntensity();
147 mTapGestureDetector = TapGestureDetector::New();
148 mTapGestureDetector.Attach( mStageRootActor );
149 mTapGestureDetector.DetectedSignal().Connect( this, &TestApp::OnTap );
152 void AnimateBloomView()
154 if(mRotationAnimation)
156 mRotationAnimation.Stop();
160 mResizeAnimation.Stop();
162 if(mTranslationAnimation)
164 mTranslationAnimation.Stop();
168 mBlurAnimation.Stop();
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();
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();
187 #else //#ifdef ADD_REMOVE_FROM_STAGE_TEST
189 void Create2(Image loadedImage)
191 Stage stage = Stage::GetCurrent();
192 Vector2 stageSize = stage.GetSize();
194 mStageRootActor = Actor::New();
195 mStageRootActor.SetParentOrigin(ParentOrigin::CENTER);
196 mStageRootActor.SetSize(stageSize);
197 stage.Add(mStageRootActor);
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();
206 // Create the Quit 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);
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() ) );
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 );
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 );
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 );
236 ImageActor imageActor = ImageActor::New( mDiffuseImage );
237 imageActor.SetParentOrigin( ParentOrigin::CENTER );
238 imageActor.SetSize( stageSize * 0.75f);
239 mObjectRootActor.Add( imageActor );
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 );
248 PulseBloomIntensity();
251 mTapGestureDetector = TapGestureDetector::New();
252 mTapGestureDetector.Attach( mStageRootActor );
253 mTapGestureDetector.DetectedSignal().Connect( this, &TestApp::OnTap );
256 PushButton CreateButton(const std::string& name, const std::string& label, const Vector3& sizeConstraint)
259 Image img = Image::New(BUTTON_BACKGROUND_IMAGE_PATH);
260 ImageActor background = ImageActor::New(img);
262 background.SetStyle(ImageActor::STYLE_NINE_PATCH);
263 background.SetNinePatchBorder(BUTTON_IMAGE_BORDER);
265 PushButton button = PushButton::New();
266 button.SetName(name);
267 button.SetBackgroundImage(background);
269 // Set the button's size
270 button.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), RelativeToConstraint( sizeConstraint ) ) );
272 // Make sure the button only takes up its natural or allocated width.
273 button.SetSizePolicy( Toolkit::Control::Fixed, Toolkit::Control::Fixed );
275 mStageRootActor.Add(button);
277 button.ClickedSignal().Connect(this, &TestApp::OnButtonTouchEvent);
279 SetButtonLabel(button, label, TextView::Center);
284 void SetButtonLabel(PushButton button, const std::string& label, const TextView::LineJustification justification)
286 Font font = Font::New();
287 Vector3 textSize = font.MeasureText(label);
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);
299 Alignment align = Alignment::New();
301 align.SetScaling( Alignment::ShrinkToFitKeepAspect );
303 button.SetLabelText(align);
307 * Signal handler, called when the button is pressed
309 bool OnButtonTouchEvent(Button button)
311 if(button.GetName() == BUTTON_QUIT)
313 // quit the application
320 void AnimateBloomView()
322 if(mRotationAnimation)
324 mRotationAnimation.Stop();
328 mResizeAnimation.Stop();
330 if(mTranslationAnimation)
332 mTranslationAnimation.Stop();
336 mBlurAnimation.Stop();
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();
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();
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();
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();
377 void AnimateActorRoot()
379 if(mRotationAnimation)
381 mRotationAnimation.Stop();
385 mResizeAnimation.Stop();
387 if(mTranslationAnimation)
389 mTranslationAnimation.Stop();
393 mBlurAnimation.Stop();
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();
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();
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();
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();
434 void ToggleAnimation()
437 if(mCurrentAnimation == NUM_MOVEMENT_ANIMATIONS)
439 mCurrentAnimation = 0;
442 switch(mCurrentAnimation)
453 #endif //#ifdef ADD_REMOVE_FROM_STAGE_TEST
455 void PulseBloomIntensity()
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();
464 void OnTap( Actor actor, const TapGesture& tapGesture )
466 Stage stage = Stage::GetCurrent();
468 #ifdef ADD_REMOVE_FROM_STAGE_TEST
469 if(mBloomView.OnStage())
471 mStageRootActor.Remove(mBloomView);
472 mBloomView.Deactivate();
476 mStageRootActor.Add(mBloomView);
477 mBloomView.Activate();
479 #else //#ifdef ADD_REMOVE_FROM_STAGE_TEST
481 #endif //#ifdef ADD_REMOVE_FROM_STAGE_TEST
486 Application& mApplication;
488 TapGestureDetector mTapGestureDetector;
490 Actor mStageRootActor;
492 Actor mObjectRootActor;
495 unsigned int mCurrentAnimation;
496 Animation mRotationAnimation;
497 Animation mResizeAnimation;
498 Animation mTranslationAnimation;
499 Animation mBlurAnimation;
500 Animation mPulseBloomIntensityAnim;
502 // for rendering the blur / bloom
503 GaussianBlurView mGaussianBlurView;
504 BloomView mBloomView;
507 /*****************************************************************************/
510 RunTest( Application& application )
512 TestApp theApp(application);
513 application.MainLoop();
516 /*****************************************************************************/
519 main(int argc, char **argv)
521 Application application = Application::New(&argc, &argv);
523 RunTest(application);