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 * Get size of Actor if larger than given size
57 * @param[in] root the actor to get the size of
58 * @param[out] size the greater of the given size or the size of the Actor
60 void SizeOfActorIfLarger( Actor root, Vector3& size )
64 // RelayoutSize retreived for Actor to use any padding set to it.
65 size.width = std::max( root.GetRelayoutSize( WIDTH ), size.width );
66 size.height = std::max( root.GetRelayoutSize( HEIGHT ), size.height );
70 } // unnamed namespace
72 Dali::Toolkit::PushButton PushButton::New()
74 // Create the implementation, temporarily owned on stack
75 IntrusivePtr< PushButton > internalPushButton = new PushButton();
77 // Pass ownership to CustomActor
78 Dali::Toolkit::PushButton pushButton( *internalPushButton );
80 // Second-phase init of the implementation
81 // This can only be done after the CustomActor connection has been made...
82 internalPushButton->Initialize();
87 PushButton::PushButton()
91 SetAnimationTime( ANIMATION_TIME );
94 PushButton::~PushButton()
98 void PushButton::OnButtonInitialize()
100 // Push button requires the Leave event.
102 self.SetLeaveRequired( true );
104 // Set resize policy to natural size so that buttons will resize to background images
105 self.SetResizePolicy( USE_NATURAL_SIZE, ALL_DIMENSIONS );
108 void PushButton::OnLabelSet()
110 Actor& label = GetLabel();
114 label.SetAnchorPoint( AnchorPoint::CENTER );
115 label.SetParentOrigin( ParentOrigin::CENTER );
116 label.SetSize( mSize );
120 void PushButton::OnButtonImageSet()
122 Actor& buttonImage = GetButtonImage();
124 buttonImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
126 buttonImage.RelayoutRequestTree();
131 void PushButton::OnSelectedImageSet()
133 Actor& selectedImage = GetSelectedImage();
135 selectedImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
137 selectedImage.RelayoutRequestTree();
142 void PushButton::OnBackgroundImageSet()
144 Actor& backgroundImage = GetBackgroundImage();
146 backgroundImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
148 backgroundImage.RelayoutRequestTree();
153 void PushButton::OnSelectedBackgroundImageSet()
155 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
157 selectedBackgroundImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
160 void PushButton::OnDisabledImageSet()
162 Actor& disabledImage = GetDisabledImage();
164 disabledImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
166 disabledImage.RelayoutRequestTree();
171 void PushButton::OnDisabledBackgroundImageSet()
173 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
175 disabledBackgroundImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
177 disabledBackgroundImage.RelayoutRequestTree();
182 bool PushButton::OnSelected()
184 Actor& buttonImage = GetButtonImage();
185 Actor& selectedImage = GetSelectedImage();
186 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
188 PaintState paintState = GetPaintState();
192 case UnselectedState:
194 FadeOutImage( buttonImage );
195 FadeInImage( selectedBackgroundImage );
196 FadeInImage( selectedImage );
197 StartTransitionAnimation();
202 FadeOutImage( selectedBackgroundImage );
203 FadeOutImage( selectedImage );
204 FadeInImage( buttonImage );
205 StartTransitionAnimation();
208 case UnselectedSelectedTransition:
213 opacity = selectedImage.GetCurrentOpacity();
216 StopTransitionAnimation( false );
217 FadeOutImage( selectedBackgroundImage, opacity );
218 FadeOutImage( selectedImage, opacity );
219 FadeInImage( buttonImage, 1.f - opacity );
220 StartTransitionAnimation();
223 case SelectedUnselectedTransition:
228 opacity = selectedImage.GetCurrentOpacity();
231 StopTransitionAnimation( false );
232 FadeOutImage( buttonImage, 1.f - opacity );
233 FadeInImage( selectedBackgroundImage, opacity );
234 FadeInImage( selectedImage, opacity );
235 StartTransitionAnimation();
238 case DisabledUnselectedTransition:
240 StopTransitionAnimation();
241 FadeOutImage( buttonImage );
242 FadeInImage( selectedBackgroundImage );
243 FadeInImage( selectedImage );
244 StartTransitionAnimation();
247 case DisabledSelectedTransition:
249 StopTransitionAnimation();
250 FadeOutImage( selectedBackgroundImage );
251 FadeOutImage( selectedImage );
252 FadeInImage( buttonImage );
253 StartTransitionAnimation();
262 if( mTransitionAnimation )
270 bool PushButton::OnDisabled()
272 Actor& buttonImage = GetButtonImage();
273 Actor& selectedImage = GetSelectedImage();
274 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
275 Actor& backgroundImage = GetBackgroundImage();
276 Actor& disabledImage = GetDisabledImage();
277 Actor& disabledSelectedImage = GetDisabledSelectedImage();
278 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
280 PaintState paintState = GetPaintState();
284 case UnselectedState:
286 FadeOutImage( backgroundImage );
287 FadeOutImage( buttonImage );
288 FadeInImage( disabledBackgroundImage );
289 FadeInImage( disabledImage );
290 StartTransitionAnimation();
295 FadeOutImage( backgroundImage );
296 FadeOutImage( selectedBackgroundImage );
297 FadeOutImage( selectedImage );
298 FadeInImage( disabledBackgroundImage );
299 FadeInImage( disabledSelectedImage );
300 StartTransitionAnimation();
303 case DisabledUnselectedState:
305 FadeOutImage( disabledBackgroundImage );
306 FadeOutImage( disabledImage );
307 FadeInImage( backgroundImage );
308 FadeInImage( buttonImage );
309 StartTransitionAnimation();
312 case DisabledSelectedState:
314 FadeOutImage( disabledBackgroundImage );
315 FadeOutImage( disabledSelectedImage );
316 FadeInImage( backgroundImage );
317 FadeInImage( selectedBackgroundImage );
318 FadeInImage( selectedImage );
319 StartTransitionAnimation();
322 case UnselectedSelectedTransition:
327 opacity = selectedImage.GetCurrentOpacity();
330 StopTransitionAnimation();
331 FadeOutImage( backgroundImage );
332 FadeOutImage( selectedBackgroundImage, opacity );
333 FadeOutImage( selectedImage, opacity );
334 FadeInImage( disabledBackgroundImage );
335 FadeInImage( disabledSelectedImage );
336 StartTransitionAnimation();
339 case SelectedUnselectedTransition:
344 opacity = buttonImage.GetCurrentOpacity();
347 StopTransitionAnimation();
348 FadeOutImage( backgroundImage );
349 FadeOutImage( buttonImage, opacity );
350 FadeInImage( disabledBackgroundImage );
351 FadeInImage( disabledImage );
352 StartTransitionAnimation();
355 case UnselectedDisabledTransition:
360 opacity = disabledImage.GetCurrentOpacity();
363 StopTransitionAnimation( false );
364 FadeOutImage( disabledBackgroundImage, opacity );
365 FadeOutImage( disabledImage, opacity );
366 FadeInImage( backgroundImage, 1.f - opacity );
367 FadeInImage( buttonImage, 1.f - opacity );
368 StartTransitionAnimation();
371 case DisabledUnselectedTransition:
376 opacity = buttonImage.GetCurrentOpacity();
379 StopTransitionAnimation( false );
380 FadeOutImage( backgroundImage, opacity );
381 FadeOutImage( buttonImage, opacity );
382 FadeInImage( disabledBackgroundImage, 1.f - opacity );
383 FadeInImage( disabledImage, 1.f - opacity );
384 StartTransitionAnimation();
387 case SelectedDisabledTransition:
390 if( disabledSelectedImage )
392 opacity = disabledSelectedImage.GetCurrentOpacity();
395 StopTransitionAnimation( false );
396 FadeOutImage( disabledBackgroundImage, opacity );
397 FadeOutImage( disabledSelectedImage, opacity );
398 FadeInImage( backgroundImage, 1.f - opacity );
399 FadeInImage( selectedBackgroundImage, 1.f - opacity );
400 FadeInImage( selectedImage, 1.f - opacity );
401 StartTransitionAnimation();
404 case DisabledSelectedTransition:
409 opacity = selectedImage.GetCurrentOpacity();
412 StopTransitionAnimation( false );
413 FadeOutImage( backgroundImage, opacity );
414 FadeOutImage( selectedBackgroundImage, opacity );
415 FadeOutImage( selectedImage, opacity );
416 FadeInImage( disabledBackgroundImage, 1.f - opacity );
417 FadeInImage( disabledSelectedImage, 1.f - opacity );
418 StartTransitionAnimation();
423 if( mTransitionAnimation )
431 bool PushButton::OnPressed()
433 Actor& buttonImage = GetButtonImage();
434 Actor& selectedImage = GetSelectedImage();
435 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
437 PaintState paintState = GetPaintState();
441 case UnselectedState:
443 FadeOutImage( buttonImage );
444 FadeInImage( selectedBackgroundImage );
445 FadeInImage( selectedImage );
446 StartTransitionAnimation();
449 case SelectedUnselectedTransition:
454 opacity = buttonImage.GetCurrentOpacity();
457 StopTransitionAnimation( false );
458 FadeOutImage( buttonImage, opacity );
459 FadeInImage( selectedBackgroundImage, 1.f - opacity );
460 FadeInImage( selectedImage, 1.f - opacity );
461 StartTransitionAnimation();
464 case DisabledUnselectedTransition:
469 opacity = buttonImage.GetCurrentOpacity();
472 StopTransitionAnimation();
473 FadeOutImage( buttonImage, opacity );
474 FadeInImage( selectedBackgroundImage );
475 FadeInImage( selectedImage );
476 StartTransitionAnimation();
483 if( mTransitionAnimation )
491 bool PushButton::OnReleased()
493 Actor& buttonImage = GetButtonImage();
494 Actor& selectedImage = GetSelectedImage();
495 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
497 PaintState paintState = GetPaintState();
503 FadeOutImage( selectedBackgroundImage );
504 FadeOutImage( selectedImage );
505 FadeInImage( buttonImage );
506 StartTransitionAnimation();
509 case UnselectedSelectedTransition:
514 opacity = selectedImage.GetCurrentOpacity();
517 StopTransitionAnimation( false );
518 FadeOutImage( selectedBackgroundImage, opacity );
519 FadeOutImage( selectedImage, opacity );
520 FadeInImage( buttonImage, 1.f - opacity );
521 StartTransitionAnimation();
524 case DisabledSelectedTransition:
529 opacity = selectedImage.GetCurrentOpacity();
532 StopTransitionAnimation();
533 FadeOutImage( selectedBackgroundImage, opacity );
534 FadeOutImage( selectedImage, opacity );
535 FadeInImage( buttonImage );
536 StartTransitionAnimation();
545 if( mTransitionAnimation )
553 void PushButton::StopAllAnimations()
555 StopTransitionAnimation();
558 void PushButton::OnControlSizeSet( const Vector3& targetSize )
560 if( targetSize != mSize )
564 Actor& label = GetLabel();
568 label.SetSize( mSize );
573 Vector3 PushButton::GetNaturalSize()
577 // Check Image and Background image and use the largest size as the control's Natural size.
578 SizeOfActorIfLarger( GetButtonImage(), size );
579 SizeOfActorIfLarger( GetBackgroundImage(), size );
581 // If label, test against it's size
582 Toolkit::TextView textView = Toolkit::TextView::DownCast( GetLabel() );
585 Vector3 textViewSize = textView.GetNaturalSize();
587 size.width = std::max( size.width, textViewSize.width + TEXT_PADDING * 2.0f );
588 size.height = std::max( size.height, textViewSize.height + TEXT_PADDING * 2.0f );
594 void PushButton::StartTransitionAnimation()
596 if( mTransitionAnimation )
598 mTransitionAnimation.FinishedSignal().Connect( this, &PushButton::TransitionAnimationFinished );
599 mTransitionAnimation.Play();
603 void PushButton::StopTransitionAnimation( bool remove )
605 if( mTransitionAnimation )
607 mTransitionAnimation.Clear();
608 mTransitionAnimation.Reset();
613 UpdatePaintTransitionState();
617 void PushButton::FadeInImage( Actor& image, float opacity, Vector3 scale )
621 image.SetOpacity( opacity );
622 image.SetScale( scale );
624 if( !mTransitionAnimation )
626 mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
629 mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 1.f );
633 void PushButton::FadeOutImage( Actor& image, float opacity, Vector3 scale )
637 image.SetOpacity( opacity );
638 image.SetScale( scale );
640 if( !mTransitionAnimation )
642 mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
645 mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 0.f );
649 void PushButton::TransitionAnimationFinished( Dali::Animation& source )
651 StopTransitionAnimation();
654 } // namespace Internal
656 } // namespace Toolkit