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-controls/text-label.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( Dimension::WIDTH ), size.width );
66 size.height = std::max( root.GetRelayoutSize( Dimension::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( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
108 void PushButton::OnLabelSet()
110 Actor& label = GetLabel();
114 label.SetAnchorPoint( AnchorPoint::CENTER );
115 label.SetParentOrigin( ParentOrigin::CENTER );
116 label.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
118 Toolkit::TextLabel textLabel = Toolkit::TextLabel::DownCast( label );
121 textLabel.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
122 textLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
123 textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true );
128 void PushButton::OnButtonImageSet()
130 Actor& buttonImage = GetButtonImage();
132 buttonImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
134 buttonImage.RelayoutRequestTree();
139 void PushButton::OnSelectedImageSet()
141 Actor& selectedImage = GetSelectedImage();
143 selectedImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
145 selectedImage.RelayoutRequestTree();
150 void PushButton::OnBackgroundImageSet()
152 Actor& backgroundImage = GetBackgroundImage();
154 backgroundImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
156 backgroundImage.RelayoutRequestTree();
161 void PushButton::OnSelectedBackgroundImageSet()
163 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
165 selectedBackgroundImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
168 void PushButton::OnDisabledImageSet()
170 Actor& disabledImage = GetDisabledImage();
172 disabledImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
174 disabledImage.RelayoutRequestTree();
179 void PushButton::OnDisabledBackgroundImageSet()
181 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
183 disabledBackgroundImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
185 disabledBackgroundImage.RelayoutRequestTree();
190 bool PushButton::OnSelected()
192 Actor& buttonImage = GetButtonImage();
193 Actor& selectedImage = GetSelectedImage();
194 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
196 PaintState paintState = GetPaintState();
200 case UnselectedState:
202 FadeOutImage( buttonImage );
203 FadeInImage( selectedBackgroundImage );
204 FadeInImage( selectedImage );
205 StartTransitionAnimation();
210 FadeOutImage( selectedBackgroundImage );
211 FadeOutImage( selectedImage );
212 FadeInImage( buttonImage );
213 StartTransitionAnimation();
216 case UnselectedSelectedTransition:
221 opacity = selectedImage.GetCurrentOpacity();
224 StopTransitionAnimation( false );
225 FadeOutImage( selectedBackgroundImage, opacity );
226 FadeOutImage( selectedImage, opacity );
227 FadeInImage( buttonImage, 1.f - opacity );
228 StartTransitionAnimation();
231 case SelectedUnselectedTransition:
236 opacity = selectedImage.GetCurrentOpacity();
239 StopTransitionAnimation( false );
240 FadeOutImage( buttonImage, 1.f - opacity );
241 FadeInImage( selectedBackgroundImage, opacity );
242 FadeInImage( selectedImage, opacity );
243 StartTransitionAnimation();
246 case DisabledUnselectedTransition:
248 StopTransitionAnimation();
249 FadeOutImage( buttonImage );
250 FadeInImage( selectedBackgroundImage );
251 FadeInImage( selectedImage );
252 StartTransitionAnimation();
255 case DisabledSelectedTransition:
257 StopTransitionAnimation();
258 FadeOutImage( selectedBackgroundImage );
259 FadeOutImage( selectedImage );
260 FadeInImage( buttonImage );
261 StartTransitionAnimation();
270 if( mTransitionAnimation )
278 bool PushButton::OnDisabled()
280 Actor& buttonImage = GetButtonImage();
281 Actor& selectedImage = GetSelectedImage();
282 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
283 Actor& backgroundImage = GetBackgroundImage();
284 Actor& disabledImage = GetDisabledImage();
285 Actor& disabledSelectedImage = GetDisabledSelectedImage();
286 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
288 PaintState paintState = GetPaintState();
292 case UnselectedState:
294 FadeOutImage( backgroundImage );
295 FadeOutImage( buttonImage );
296 FadeInImage( disabledBackgroundImage );
297 FadeInImage( disabledImage );
298 StartTransitionAnimation();
303 FadeOutImage( backgroundImage );
304 FadeOutImage( selectedBackgroundImage );
305 FadeOutImage( selectedImage );
306 FadeInImage( disabledBackgroundImage );
307 FadeInImage( disabledSelectedImage );
308 StartTransitionAnimation();
311 case DisabledUnselectedState:
313 FadeOutImage( disabledBackgroundImage );
314 FadeOutImage( disabledImage );
315 FadeInImage( backgroundImage );
316 FadeInImage( buttonImage );
317 StartTransitionAnimation();
320 case DisabledSelectedState:
322 FadeOutImage( disabledBackgroundImage );
323 FadeOutImage( disabledSelectedImage );
324 FadeInImage( backgroundImage );
325 FadeInImage( selectedBackgroundImage );
326 FadeInImage( selectedImage );
327 StartTransitionAnimation();
330 case UnselectedSelectedTransition:
335 opacity = selectedImage.GetCurrentOpacity();
338 StopTransitionAnimation();
339 FadeOutImage( backgroundImage );
340 FadeOutImage( selectedBackgroundImage, opacity );
341 FadeOutImage( selectedImage, opacity );
342 FadeInImage( disabledBackgroundImage );
343 FadeInImage( disabledSelectedImage );
344 StartTransitionAnimation();
347 case SelectedUnselectedTransition:
352 opacity = buttonImage.GetCurrentOpacity();
355 StopTransitionAnimation();
356 FadeOutImage( backgroundImage );
357 FadeOutImage( buttonImage, opacity );
358 FadeInImage( disabledBackgroundImage );
359 FadeInImage( disabledImage );
360 StartTransitionAnimation();
363 case UnselectedDisabledTransition:
368 opacity = disabledImage.GetCurrentOpacity();
371 StopTransitionAnimation( false );
372 FadeOutImage( disabledBackgroundImage, opacity );
373 FadeOutImage( disabledImage, opacity );
374 FadeInImage( backgroundImage, 1.f - opacity );
375 FadeInImage( buttonImage, 1.f - opacity );
376 StartTransitionAnimation();
379 case DisabledUnselectedTransition:
384 opacity = buttonImage.GetCurrentOpacity();
387 StopTransitionAnimation( false );
388 FadeOutImage( backgroundImage, opacity );
389 FadeOutImage( buttonImage, opacity );
390 FadeInImage( disabledBackgroundImage, 1.f - opacity );
391 FadeInImage( disabledImage, 1.f - opacity );
392 StartTransitionAnimation();
395 case SelectedDisabledTransition:
398 if( disabledSelectedImage )
400 opacity = disabledSelectedImage.GetCurrentOpacity();
403 StopTransitionAnimation( false );
404 FadeOutImage( disabledBackgroundImage, opacity );
405 FadeOutImage( disabledSelectedImage, opacity );
406 FadeInImage( backgroundImage, 1.f - opacity );
407 FadeInImage( selectedBackgroundImage, 1.f - opacity );
408 FadeInImage( selectedImage, 1.f - opacity );
409 StartTransitionAnimation();
412 case DisabledSelectedTransition:
417 opacity = selectedImage.GetCurrentOpacity();
420 StopTransitionAnimation( false );
421 FadeOutImage( backgroundImage, opacity );
422 FadeOutImage( selectedBackgroundImage, opacity );
423 FadeOutImage( selectedImage, opacity );
424 FadeInImage( disabledBackgroundImage, 1.f - opacity );
425 FadeInImage( disabledSelectedImage, 1.f - opacity );
426 StartTransitionAnimation();
431 if( mTransitionAnimation )
439 bool PushButton::OnPressed()
441 Actor& buttonImage = GetButtonImage();
442 Actor& selectedImage = GetSelectedImage();
443 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
445 PaintState paintState = GetPaintState();
449 case UnselectedState:
451 FadeOutImage( buttonImage );
452 FadeInImage( selectedBackgroundImage );
453 FadeInImage( selectedImage );
454 StartTransitionAnimation();
457 case SelectedUnselectedTransition:
462 opacity = buttonImage.GetCurrentOpacity();
465 StopTransitionAnimation( false );
466 FadeOutImage( buttonImage, opacity );
467 FadeInImage( selectedBackgroundImage, 1.f - opacity );
468 FadeInImage( selectedImage, 1.f - opacity );
469 StartTransitionAnimation();
472 case DisabledUnselectedTransition:
477 opacity = buttonImage.GetCurrentOpacity();
480 StopTransitionAnimation();
481 FadeOutImage( buttonImage, opacity );
482 FadeInImage( selectedBackgroundImage );
483 FadeInImage( selectedImage );
484 StartTransitionAnimation();
491 if( mTransitionAnimation )
499 bool PushButton::OnReleased()
501 Actor& buttonImage = GetButtonImage();
502 Actor& selectedImage = GetSelectedImage();
503 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
505 PaintState paintState = GetPaintState();
511 FadeOutImage( selectedBackgroundImage );
512 FadeOutImage( selectedImage );
513 FadeInImage( buttonImage );
514 StartTransitionAnimation();
517 case UnselectedSelectedTransition:
522 opacity = selectedImage.GetCurrentOpacity();
525 StopTransitionAnimation( false );
526 FadeOutImage( selectedBackgroundImage, opacity );
527 FadeOutImage( selectedImage, opacity );
528 FadeInImage( buttonImage, 1.f - opacity );
529 StartTransitionAnimation();
532 case DisabledSelectedTransition:
537 opacity = selectedImage.GetCurrentOpacity();
540 StopTransitionAnimation();
541 FadeOutImage( selectedBackgroundImage, opacity );
542 FadeOutImage( selectedImage, opacity );
543 FadeInImage( buttonImage );
544 StartTransitionAnimation();
553 if( mTransitionAnimation )
561 void PushButton::StopAllAnimations()
563 StopTransitionAnimation();
566 void PushButton::OnControlSizeSet( const Vector3& targetSize )
568 if( targetSize != mSize )
572 Actor& label = GetLabel();
576 label.SetSize( mSize );
581 Vector3 PushButton::GetNaturalSize()
585 // Check Image and Background image and use the largest size as the control's Natural size.
586 SizeOfActorIfLarger( GetButtonImage(), size );
587 SizeOfActorIfLarger( GetBackgroundImage(), size );
589 // If label, test against it's size
590 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( GetLabel() );
593 Vector3 labelSize = label.GetNaturalSize();
595 size.width = std::max( size.width, labelSize.width + TEXT_PADDING * 2.0f );
596 size.height = std::max( size.height, labelSize.height + TEXT_PADDING * 2.0f );
602 void PushButton::StartTransitionAnimation()
604 if( mTransitionAnimation )
606 mTransitionAnimation.FinishedSignal().Connect( this, &PushButton::TransitionAnimationFinished );
607 mTransitionAnimation.Play();
611 void PushButton::StopTransitionAnimation( bool remove )
613 if( mTransitionAnimation )
615 mTransitionAnimation.Clear();
616 mTransitionAnimation.Reset();
621 UpdatePaintTransitionState();
625 void PushButton::FadeInImage( Actor& image, float opacity, Vector3 scale )
629 image.SetOpacity( opacity );
630 image.SetScale( scale );
632 if( !mTransitionAnimation )
634 mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
637 mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 1.f );
641 void PushButton::FadeOutImage( Actor& image, float opacity, Vector3 scale )
645 image.SetOpacity( opacity );
646 image.SetScale( scale );
648 if( !mTransitionAnimation )
650 mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
653 mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 0.f );
657 void PushButton::TransitionAnimationFinished( Dali::Animation& source )
659 StopTransitionAnimation();
662 } // namespace Internal
664 } // namespace Toolkit