2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "push-button-impl.h"
22 #include <dali/public-api/actors/image-actor.h>
23 #include <dali/public-api/object/type-registry.h>
26 #include <dali-toolkit/public-api/controls/text-view/text-view.h>
40 const float TEXT_PADDING = 12.0f;
41 const float ANIMATION_TIME( 0.2f );
45 return Toolkit::PushButton::New();
48 TypeRegistration typeRegistration( typeid(Toolkit::PushButton), typeid(Toolkit::Button), Create );
50 } // unnamed namespace
56 * Find the first image actor in the actor hierarchy
58 ImageActor FindImageActor( Actor root )
60 ImageActor imageActor = ImageActor::DownCast( root );
61 if( !imageActor && root )
63 for( unsigned int i = 0, numChildren = root.GetChildCount(); i < numChildren; ++i )
65 ImageActor childImageActor = FindImageActor( root.GetChildAt( i ) );
68 return childImageActor;
76 } // unnamed namespace
78 Dali::Toolkit::PushButton PushButton::New()
80 // Create the implementation, temporarily owned on stack
81 IntrusivePtr< PushButton > internalPushButton = new PushButton();
83 // Pass ownership to CustomActor
84 Dali::Toolkit::PushButton pushButton( *internalPushButton );
86 // Second-phase init of the implementation
87 // This can only be done after the CustomActor connection has been made...
88 internalPushButton->Initialize();
93 PushButton::PushButton()
97 SetAnimationTime( ANIMATION_TIME );
100 PushButton::~PushButton()
104 void PushButton::OnButtonInitialize()
106 // Push button requires the Leave event.
108 self.SetLeaveRequired( true );
110 // Set resize policy to natural size so that buttons will resize to background images
111 self.SetResizePolicy( USE_NATURAL_SIZE, ALL_DIMENSIONS );
114 void PushButton::OnLabelSet()
116 Actor& label = GetLabel();
120 label.SetAnchorPoint( AnchorPoint::CENTER );
121 label.SetParentOrigin( ParentOrigin::CENTER );
122 label.SetSize( mSize );
126 void PushButton::OnButtonImageSet()
128 Actor& buttonImage = GetButtonImage();
130 buttonImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
132 buttonImage.RelayoutRequestTree();
137 void PushButton::OnSelectedImageSet()
139 Actor& selectedImage = GetSelectedImage();
141 selectedImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
143 selectedImage.RelayoutRequestTree();
148 void PushButton::OnBackgroundImageSet()
150 Actor& backgroundImage = GetBackgroundImage();
152 backgroundImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
154 backgroundImage.RelayoutRequestTree();
159 void PushButton::OnSelectedBackgroundImageSet()
161 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
163 selectedBackgroundImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
166 void PushButton::OnDisabledImageSet()
168 Actor& disabledImage = GetDisabledImage();
170 disabledImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
172 disabledImage.RelayoutRequestTree();
177 void PushButton::OnDisabledBackgroundImageSet()
179 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
181 disabledBackgroundImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
183 disabledBackgroundImage.RelayoutRequestTree();
188 bool PushButton::OnSelected()
190 Actor& buttonImage = GetButtonImage();
191 Actor& selectedImage = GetSelectedImage();
192 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
194 PaintState paintState = GetPaintState();
198 case UnselectedState:
200 FadeOutImage( buttonImage );
201 FadeInImage( selectedBackgroundImage );
202 FadeInImage( selectedImage );
203 StartTransitionAnimation();
208 FadeOutImage( selectedBackgroundImage );
209 FadeOutImage( selectedImage );
210 FadeInImage( buttonImage );
211 StartTransitionAnimation();
214 case UnselectedSelectedTransition:
219 opacity = selectedImage.GetCurrentOpacity();
222 StopTransitionAnimation( false );
223 FadeOutImage( selectedBackgroundImage, opacity );
224 FadeOutImage( selectedImage, opacity );
225 FadeInImage( buttonImage, 1.f - opacity );
226 StartTransitionAnimation();
229 case SelectedUnselectedTransition:
234 opacity = selectedImage.GetCurrentOpacity();
237 StopTransitionAnimation( false );
238 FadeOutImage( buttonImage, 1.f - opacity );
239 FadeInImage( selectedBackgroundImage, opacity );
240 FadeInImage( selectedImage, opacity );
241 StartTransitionAnimation();
244 case DisabledUnselectedTransition:
246 StopTransitionAnimation();
247 FadeOutImage( buttonImage );
248 FadeInImage( selectedBackgroundImage );
249 FadeInImage( selectedImage );
250 StartTransitionAnimation();
253 case DisabledSelectedTransition:
255 StopTransitionAnimation();
256 FadeOutImage( selectedBackgroundImage );
257 FadeOutImage( selectedImage );
258 FadeInImage( buttonImage );
259 StartTransitionAnimation();
268 if( mTransitionAnimation )
276 bool PushButton::OnDisabled()
278 Actor& buttonImage = GetButtonImage();
279 Actor& selectedImage = GetSelectedImage();
280 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
281 Actor& backgroundImage = GetBackgroundImage();
282 Actor& disabledImage = GetDisabledImage();
283 Actor& disabledSelectedImage = GetDisabledSelectedImage();
284 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
286 PaintState paintState = GetPaintState();
290 case UnselectedState:
292 FadeOutImage( backgroundImage );
293 FadeOutImage( buttonImage );
294 FadeInImage( disabledBackgroundImage );
295 FadeInImage( disabledImage );
296 StartTransitionAnimation();
301 FadeOutImage( backgroundImage );
302 FadeOutImage( selectedBackgroundImage );
303 FadeOutImage( selectedImage );
304 FadeInImage( disabledBackgroundImage );
305 FadeInImage( disabledSelectedImage );
306 StartTransitionAnimation();
309 case DisabledUnselectedState:
311 FadeOutImage( disabledBackgroundImage );
312 FadeOutImage( disabledImage );
313 FadeInImage( backgroundImage );
314 FadeInImage( buttonImage );
315 StartTransitionAnimation();
318 case DisabledSelectedState:
320 FadeOutImage( disabledBackgroundImage );
321 FadeOutImage( disabledSelectedImage );
322 FadeInImage( backgroundImage );
323 FadeInImage( selectedBackgroundImage );
324 FadeInImage( selectedImage );
325 StartTransitionAnimation();
328 case UnselectedSelectedTransition:
333 opacity = selectedImage.GetCurrentOpacity();
336 StopTransitionAnimation();
337 FadeOutImage( backgroundImage );
338 FadeOutImage( selectedBackgroundImage, opacity );
339 FadeOutImage( selectedImage, opacity );
340 FadeInImage( disabledBackgroundImage );
341 FadeInImage( disabledSelectedImage );
342 StartTransitionAnimation();
345 case SelectedUnselectedTransition:
350 opacity = buttonImage.GetCurrentOpacity();
353 StopTransitionAnimation();
354 FadeOutImage( backgroundImage );
355 FadeOutImage( buttonImage, opacity );
356 FadeInImage( disabledBackgroundImage );
357 FadeInImage( disabledImage );
358 StartTransitionAnimation();
361 case UnselectedDisabledTransition:
366 opacity = disabledImage.GetCurrentOpacity();
369 StopTransitionAnimation( false );
370 FadeOutImage( disabledBackgroundImage, opacity );
371 FadeOutImage( disabledImage, opacity );
372 FadeInImage( backgroundImage, 1.f - opacity );
373 FadeInImage( buttonImage, 1.f - opacity );
374 StartTransitionAnimation();
377 case DisabledUnselectedTransition:
382 opacity = buttonImage.GetCurrentOpacity();
385 StopTransitionAnimation( false );
386 FadeOutImage( backgroundImage, opacity );
387 FadeOutImage( buttonImage, opacity );
388 FadeInImage( disabledBackgroundImage, 1.f - opacity );
389 FadeInImage( disabledImage, 1.f - opacity );
390 StartTransitionAnimation();
393 case SelectedDisabledTransition:
396 if( disabledSelectedImage )
398 opacity = disabledSelectedImage.GetCurrentOpacity();
401 StopTransitionAnimation( false );
402 FadeOutImage( disabledBackgroundImage, opacity );
403 FadeOutImage( disabledSelectedImage, opacity );
404 FadeInImage( backgroundImage, 1.f - opacity );
405 FadeInImage( selectedBackgroundImage, 1.f - opacity );
406 FadeInImage( selectedImage, 1.f - opacity );
407 StartTransitionAnimation();
410 case DisabledSelectedTransition:
415 opacity = selectedImage.GetCurrentOpacity();
418 StopTransitionAnimation( false );
419 FadeOutImage( backgroundImage, opacity );
420 FadeOutImage( selectedBackgroundImage, opacity );
421 FadeOutImage( selectedImage, opacity );
422 FadeInImage( disabledBackgroundImage, 1.f - opacity );
423 FadeInImage( disabledSelectedImage, 1.f - opacity );
424 StartTransitionAnimation();
429 if( mTransitionAnimation )
437 bool PushButton::OnPressed()
439 Actor& buttonImage = GetButtonImage();
440 Actor& selectedImage = GetSelectedImage();
441 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
443 PaintState paintState = GetPaintState();
447 case UnselectedState:
449 FadeOutImage( buttonImage );
450 FadeInImage( selectedBackgroundImage );
451 FadeInImage( selectedImage );
452 StartTransitionAnimation();
455 case SelectedUnselectedTransition:
460 opacity = buttonImage.GetCurrentOpacity();
463 StopTransitionAnimation( false );
464 FadeOutImage( buttonImage, opacity );
465 FadeInImage( selectedBackgroundImage, 1.f - opacity );
466 FadeInImage( selectedImage, 1.f - opacity );
467 StartTransitionAnimation();
470 case DisabledUnselectedTransition:
475 opacity = buttonImage.GetCurrentOpacity();
478 StopTransitionAnimation();
479 FadeOutImage( buttonImage, opacity );
480 FadeInImage( selectedBackgroundImage );
481 FadeInImage( selectedImage );
482 StartTransitionAnimation();
489 if( mTransitionAnimation )
497 bool PushButton::OnReleased()
499 Actor& buttonImage = GetButtonImage();
500 Actor& selectedImage = GetSelectedImage();
501 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
503 PaintState paintState = GetPaintState();
509 FadeOutImage( selectedBackgroundImage );
510 FadeOutImage( selectedImage );
511 FadeInImage( buttonImage );
512 StartTransitionAnimation();
515 case UnselectedSelectedTransition:
520 opacity = selectedImage.GetCurrentOpacity();
523 StopTransitionAnimation( false );
524 FadeOutImage( selectedBackgroundImage, opacity );
525 FadeOutImage( selectedImage, opacity );
526 FadeInImage( buttonImage, 1.f - opacity );
527 StartTransitionAnimation();
530 case DisabledSelectedTransition:
535 opacity = selectedImage.GetCurrentOpacity();
538 StopTransitionAnimation();
539 FadeOutImage( selectedBackgroundImage, opacity );
540 FadeOutImage( selectedImage, opacity );
541 FadeInImage( buttonImage );
542 StartTransitionAnimation();
551 if( mTransitionAnimation )
559 void PushButton::StopAllAnimations()
561 StopTransitionAnimation();
564 void PushButton::OnControlSizeSet( const Vector3& targetSize )
566 if( targetSize != mSize )
570 Actor& label = GetLabel();
574 label.SetSize( mSize );
579 Vector3 PushButton::GetNaturalSize()
583 // If background and background not scale9 try get size from that
584 ImageActor imageActor = FindImageActor( GetButtonImage() );
585 if( imageActor && imageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
587 size.width = imageActor.GetRelayoutSize( WIDTH );
588 size.height = imageActor.GetRelayoutSize( HEIGHT );
591 ImageActor backgroundImageActor = FindImageActor( GetBackgroundImage() );
592 if( backgroundImageActor && backgroundImageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
594 size.width = std::max( size.width, backgroundImageActor.GetRelayoutSize( WIDTH ) );
595 size.height = std::max( size.height, backgroundImageActor.GetRelayoutSize( HEIGHT ) );
598 // If label, test against it's size
599 Toolkit::TextView textView = Toolkit::TextView::DownCast( GetLabel() );
602 Vector3 textViewSize = textView.GetNaturalSize();
604 size.width = std::max( size.width, textViewSize.width + TEXT_PADDING * 2.0f );
605 size.height = std::max( size.height, textViewSize.height + TEXT_PADDING * 2.0f );
611 void PushButton::StartTransitionAnimation()
613 if( mTransitionAnimation )
615 mTransitionAnimation.FinishedSignal().Connect( this, &PushButton::TransitionAnimationFinished );
616 mTransitionAnimation.Play();
620 void PushButton::StopTransitionAnimation( bool remove )
622 if( mTransitionAnimation )
624 mTransitionAnimation.Clear();
625 mTransitionAnimation.Reset();
630 UpdatePaintTransitionState();
634 void PushButton::FadeInImage( Actor& image, float opacity, Vector3 scale )
638 image.SetOpacity( opacity );
639 image.SetScale( scale );
641 if( !mTransitionAnimation )
643 mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
646 mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 1.f );
650 void PushButton::FadeOutImage( Actor& image, float opacity, Vector3 scale )
654 image.SetOpacity( opacity );
655 image.SetScale( scale );
657 if( !mTransitionAnimation )
659 mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
662 mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 0.f );
666 void PushButton::TransitionAnimationFinished( Dali::Animation& source )
668 StopTransitionAnimation();
671 } // namespace Internal
673 } // namespace Toolkit