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;
47 const char* BUTTON_BACKGROUND_IMAGE_PATH = DALI_IMAGE_DIR "GreyThinLine30x30-76x78.png";
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;
55 const Vector4 BUTTON_IMAGE_BORDER( BUTTON_IMAGE_BORDER_LEFT, BUTTON_IMAGE_BORDER_TOP, BUTTON_IMAGE_BORDER_RIGHT, BUTTON_IMAGE_BORDER_BOTTOM );
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";
67 * This example demonstrates a blur effect.
69 class TestApp : public ConnectionTracker
73 TestApp( Application &application )
74 : mApplication(application)
75 , mCurrentAnimation(0)
77 application.InitSignal().Connect(this, &TestApp::Create);
78 application.TerminateSignal().Connect(this, &TestApp::Terminate);
87 void Create(Application& application)
90 mDiffuseImage = Image::New( UI_DIFFUSE_IMAGE );
92 // Creation is deferred until images have loaded
93 mDiffuseImage.LoadingFinishedSignal().Connect( this, &TestApp::Create2 );
96 #ifdef ADD_REMOVE_FROM_STAGE_TEST
97 void Create2(Image loadedImage)
99 Stage stage = Stage::GetCurrent();
100 Vector2 stageSize = stage.GetSize();
102 mStageRootActor = Actor::New();
103 mStageRootActor.SetParentOrigin(ParentOrigin::CENTER);
104 mStageRootActor.SetSize(stageSize);
105 stage.Add(mStageRootActor);
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();
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() ) );
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 );
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 );
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 );
137 ImageActor imageActor = ImageActor::New( mDiffuseImage );
138 imageActor.SetParentOrigin( ParentOrigin::CENTER );
139 imageActor.SetSize( stageSize * 0.75f);
140 mObjectRootActor.Add( imageActor );
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 );
148 AnimateGaussianBlurView();
151 mTapGestureDetector = TapGestureDetector::New();
152 mTapGestureDetector.Attach( mStageRootActor );
153 mTapGestureDetector.DetectedSignal().Connect( this, &TestApp::OnTap );
156 void AnimateGaussianBlurView()
158 if(mRotationAnimation)
160 mRotationAnimation.Stop();
164 mResizeAnimation.Stop();
166 if(mTranslationAnimation)
168 mTranslationAnimation.Stop();
172 mBlurAnimation.Stop();
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();
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();
191 #else //#ifdef ADD_REMOVE_FROM_STAGE_TEST
193 void Create2(Image loadedImage)
195 Stage stage = Stage::GetCurrent();
196 Vector2 stageSize = stage.GetSize();
198 mStageRootActor = Actor::New();
199 mStageRootActor.SetParentOrigin(ParentOrigin::CENTER);
200 mStageRootActor.SetSize(stageSize);
201 stage.Add(mStageRootActor);
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();
210 // Create the Quit 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);
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() ) );
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 );
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 );
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 );
240 ImageActor imageActor = ImageActor::New( mDiffuseImage );
241 imageActor.SetParentOrigin( ParentOrigin::CENTER );
242 imageActor.SetSize( stageSize * 0.75f);
243 mObjectRootActor.Add( imageActor );
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 );
254 mTapGestureDetector = TapGestureDetector::New();
255 mTapGestureDetector.Attach( mStageRootActor );
256 mTapGestureDetector.DetectedSignal().Connect( this, &TestApp::OnTap );
259 PushButton CreateButton(const std::string& name, const std::string& label, const Vector3& sizeConstraint)
262 Image img = Image::New(BUTTON_BACKGROUND_IMAGE_PATH);
263 ImageActor background = ImageActor::New(img);
265 background.SetStyle(ImageActor::STYLE_NINE_PATCH);
266 background.SetNinePatchBorder(BUTTON_IMAGE_BORDER);
268 PushButton button = PushButton::New();
269 button.SetName(name);
270 button.SetBackgroundImage(background);
272 // Set the button's size
273 button.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), RelativeToConstraint( sizeConstraint ) ) );
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 AnimateGaussianBlurView()
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(mGaussianBlurView, 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(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();
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( 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();
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(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();
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( 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();
434 void ToggleAnimation()
437 if(mCurrentAnimation == NUM_MOVEMENT_ANIMATIONS)
439 mCurrentAnimation = 0;
442 switch(mCurrentAnimation)
449 AnimateGaussianBlurView();
453 #endif //#ifdef ADD_REMOVE_FROM_STAGE_TEST
455 void Terminate( Application& app )
459 void OnTap( Actor actor, const TapGesture& tapGesture )
461 Stage stage = Stage::GetCurrent();
463 #ifdef ADD_REMOVE_FROM_STAGE_TEST
464 if(mGaussianBlurView.OnStage())
466 mStageRootActor.Remove(mGaussianBlurView);
467 mGaussianBlurView.Deactivate();
471 mStageRootActor.Add(mGaussianBlurView);
472 mGaussianBlurView.Activate();
474 #else //#ifdef ADD_REMOVE_FROM_STAGE_TEST
476 #endif //#ifdef ADD_REMOVE_FROM_STAGE_TEST
481 Application& mApplication;
483 TapGestureDetector mTapGestureDetector;
485 Actor mStageRootActor;
487 Actor mObjectRootActor;
490 unsigned int mCurrentAnimation;
491 Animation mRotationAnimation;
492 Animation mResizeAnimation;
493 Animation mTranslationAnimation;
494 Animation mBlurAnimation;
495 Animation mPulseBloomIntensityAnim;
497 // for rendering the blur / bloom
498 GaussianBlurView mGaussianBlurView;
499 BloomView mBloomView;
502 /*****************************************************************************/
505 RunTest( Application& application )
507 TestApp theApp(application);
508 application.MainLoop();
511 /*****************************************************************************/
514 main(int argc, char **argv)
516 Application application = Application::New(&argc, &argv);
518 RunTest(application);