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 );
117 Toolkit::TextLabel textLabel = Toolkit::TextLabel::DownCast( label );
120 textLabel.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
121 textLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
122 textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true );
125 ConfigureSizeNegotiation();
129 void PushButton::OnButtonImageSet()
131 ConfigureSizeNegotiation();
135 void PushButton::OnSelectedImageSet()
137 ConfigureSizeNegotiation();
141 void PushButton::OnBackgroundImageSet()
143 ConfigureSizeNegotiation();
147 void PushButton::OnSelectedBackgroundImageSet()
149 ConfigureSizeNegotiation();
153 void PushButton::OnDisabledImageSet()
155 ConfigureSizeNegotiation();
159 void PushButton::OnDisabledBackgroundImageSet()
161 ConfigureSizeNegotiation();
165 bool PushButton::OnSelected()
167 Actor& buttonImage = GetButtonImage();
168 Actor& selectedImage = GetSelectedImage();
169 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
171 PaintState paintState = GetPaintState();
175 case UnselectedState:
177 FadeOutImage( buttonImage );
178 FadeInImage( selectedBackgroundImage );
179 FadeInImage( selectedImage );
180 StartTransitionAnimation();
185 FadeOutImage( selectedBackgroundImage );
186 FadeOutImage( selectedImage );
187 FadeInImage( buttonImage );
188 StartTransitionAnimation();
191 case UnselectedSelectedTransition:
196 opacity = selectedImage.GetCurrentOpacity();
199 StopTransitionAnimation( false );
200 FadeOutImage( selectedBackgroundImage, opacity );
201 FadeOutImage( selectedImage, opacity );
202 FadeInImage( buttonImage, 1.f - opacity );
203 StartTransitionAnimation();
206 case SelectedUnselectedTransition:
211 opacity = selectedImage.GetCurrentOpacity();
214 StopTransitionAnimation( false );
215 FadeOutImage( buttonImage, 1.f - opacity );
216 FadeInImage( selectedBackgroundImage, opacity );
217 FadeInImage( selectedImage, opacity );
218 StartTransitionAnimation();
221 case DisabledUnselectedTransition:
223 StopTransitionAnimation();
224 FadeOutImage( buttonImage );
225 FadeInImage( selectedBackgroundImage );
226 FadeInImage( selectedImage );
227 StartTransitionAnimation();
230 case DisabledSelectedTransition:
232 StopTransitionAnimation();
233 FadeOutImage( selectedBackgroundImage );
234 FadeOutImage( selectedImage );
235 FadeInImage( buttonImage );
236 StartTransitionAnimation();
245 if( mTransitionAnimation )
253 bool PushButton::OnDisabled()
255 Actor& buttonImage = GetButtonImage();
256 Actor& selectedImage = GetSelectedImage();
257 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
258 Actor& backgroundImage = GetBackgroundImage();
259 Actor& disabledImage = GetDisabledImage();
260 Actor& disabledSelectedImage = GetDisabledSelectedImage();
261 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
263 PaintState paintState = GetPaintState();
267 case UnselectedState:
269 FadeOutImage( backgroundImage );
270 FadeOutImage( buttonImage );
271 FadeInImage( disabledBackgroundImage );
272 FadeInImage( disabledImage );
273 StartTransitionAnimation();
278 FadeOutImage( backgroundImage );
279 FadeOutImage( selectedBackgroundImage );
280 FadeOutImage( selectedImage );
281 FadeInImage( disabledBackgroundImage );
282 FadeInImage( disabledSelectedImage );
283 StartTransitionAnimation();
286 case DisabledUnselectedState:
288 FadeOutImage( disabledBackgroundImage );
289 FadeOutImage( disabledImage );
290 FadeInImage( backgroundImage );
291 FadeInImage( buttonImage );
292 StartTransitionAnimation();
295 case DisabledSelectedState:
297 FadeOutImage( disabledBackgroundImage );
298 FadeOutImage( disabledSelectedImage );
299 FadeInImage( backgroundImage );
300 FadeInImage( selectedBackgroundImage );
301 FadeInImage( selectedImage );
302 StartTransitionAnimation();
305 case UnselectedSelectedTransition:
310 opacity = selectedImage.GetCurrentOpacity();
313 StopTransitionAnimation();
314 FadeOutImage( backgroundImage );
315 FadeOutImage( selectedBackgroundImage, opacity );
316 FadeOutImage( selectedImage, opacity );
317 FadeInImage( disabledBackgroundImage );
318 FadeInImage( disabledSelectedImage );
319 StartTransitionAnimation();
322 case SelectedUnselectedTransition:
327 opacity = buttonImage.GetCurrentOpacity();
330 StopTransitionAnimation();
331 FadeOutImage( backgroundImage );
332 FadeOutImage( buttonImage, opacity );
333 FadeInImage( disabledBackgroundImage );
334 FadeInImage( disabledImage );
335 StartTransitionAnimation();
338 case UnselectedDisabledTransition:
343 opacity = disabledImage.GetCurrentOpacity();
346 StopTransitionAnimation( false );
347 FadeOutImage( disabledBackgroundImage, opacity );
348 FadeOutImage( disabledImage, opacity );
349 FadeInImage( backgroundImage, 1.f - opacity );
350 FadeInImage( buttonImage, 1.f - opacity );
351 StartTransitionAnimation();
354 case DisabledUnselectedTransition:
359 opacity = buttonImage.GetCurrentOpacity();
362 StopTransitionAnimation( false );
363 FadeOutImage( backgroundImage, opacity );
364 FadeOutImage( buttonImage, opacity );
365 FadeInImage( disabledBackgroundImage, 1.f - opacity );
366 FadeInImage( disabledImage, 1.f - opacity );
367 StartTransitionAnimation();
370 case SelectedDisabledTransition:
373 if( disabledSelectedImage )
375 opacity = disabledSelectedImage.GetCurrentOpacity();
378 StopTransitionAnimation( false );
379 FadeOutImage( disabledBackgroundImage, opacity );
380 FadeOutImage( disabledSelectedImage, opacity );
381 FadeInImage( backgroundImage, 1.f - opacity );
382 FadeInImage( selectedBackgroundImage, 1.f - opacity );
383 FadeInImage( selectedImage, 1.f - opacity );
384 StartTransitionAnimation();
387 case DisabledSelectedTransition:
392 opacity = selectedImage.GetCurrentOpacity();
395 StopTransitionAnimation( false );
396 FadeOutImage( backgroundImage, opacity );
397 FadeOutImage( selectedBackgroundImage, opacity );
398 FadeOutImage( selectedImage, opacity );
399 FadeInImage( disabledBackgroundImage, 1.f - opacity );
400 FadeInImage( disabledSelectedImage, 1.f - opacity );
401 StartTransitionAnimation();
406 if( mTransitionAnimation )
414 bool PushButton::OnPressed()
416 Actor& buttonImage = GetButtonImage();
417 Actor& selectedImage = GetSelectedImage();
418 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
420 PaintState paintState = GetPaintState();
424 case UnselectedState:
426 FadeOutImage( buttonImage );
427 FadeInImage( selectedBackgroundImage );
428 FadeInImage( selectedImage );
429 StartTransitionAnimation();
432 case SelectedUnselectedTransition:
437 opacity = buttonImage.GetCurrentOpacity();
440 StopTransitionAnimation( false );
441 FadeOutImage( buttonImage, opacity );
442 FadeInImage( selectedBackgroundImage, 1.f - opacity );
443 FadeInImage( selectedImage, 1.f - opacity );
444 StartTransitionAnimation();
447 case DisabledUnselectedTransition:
452 opacity = buttonImage.GetCurrentOpacity();
455 StopTransitionAnimation();
456 FadeOutImage( buttonImage, opacity );
457 FadeInImage( selectedBackgroundImage );
458 FadeInImage( selectedImage );
459 StartTransitionAnimation();
466 if( mTransitionAnimation )
474 bool PushButton::OnReleased()
476 Actor& buttonImage = GetButtonImage();
477 Actor& selectedImage = GetSelectedImage();
478 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
480 PaintState paintState = GetPaintState();
486 FadeOutImage( selectedBackgroundImage );
487 FadeOutImage( selectedImage );
488 FadeInImage( buttonImage );
489 StartTransitionAnimation();
492 case UnselectedSelectedTransition:
497 opacity = selectedImage.GetCurrentOpacity();
500 StopTransitionAnimation( false );
501 FadeOutImage( selectedBackgroundImage, opacity );
502 FadeOutImage( selectedImage, opacity );
503 FadeInImage( buttonImage, 1.f - opacity );
504 StartTransitionAnimation();
507 case DisabledSelectedTransition:
512 opacity = selectedImage.GetCurrentOpacity();
515 StopTransitionAnimation();
516 FadeOutImage( selectedBackgroundImage, opacity );
517 FadeOutImage( selectedImage, opacity );
518 FadeInImage( buttonImage );
519 StartTransitionAnimation();
528 if( mTransitionAnimation )
536 void PushButton::StopAllAnimations()
538 StopTransitionAnimation();
541 void PushButton::OnControlSizeSet( const Vector3& targetSize )
543 if( targetSize != mSize )
547 Actor& label = GetLabel();
551 label.SetSize( mSize );
556 void PushButton::StartTransitionAnimation()
558 if( mTransitionAnimation )
560 mTransitionAnimation.FinishedSignal().Connect( this, &PushButton::TransitionAnimationFinished );
561 mTransitionAnimation.Play();
565 void PushButton::StopTransitionAnimation( bool remove )
567 if( mTransitionAnimation )
569 mTransitionAnimation.Clear();
570 mTransitionAnimation.Reset();
575 UpdatePaintTransitionState();
579 void PushButton::FadeInImage( Actor& image, float opacity, Vector3 scale )
583 image.SetOpacity( opacity );
584 image.SetScale( scale );
586 if( !mTransitionAnimation )
588 mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
591 mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 1.f );
595 void PushButton::FadeOutImage( Actor& image, float opacity, Vector3 scale )
599 image.SetOpacity( opacity );
600 image.SetScale( scale );
602 if( !mTransitionAnimation )
604 mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
607 mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 0.f );
611 void PushButton::TransitionAnimationFinished( Dali::Animation& source )
613 StopTransitionAnimation();
616 Vector3 PushButton::GetNaturalSize()
620 // If label, test against it's size
621 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( GetLabel() );
624 size.width = std::max( size.width, label.GetRelayoutSize( Dimension::WIDTH ) );
625 size.height = std::max( size.height, label.GetRelayoutSize( Dimension::HEIGHT ) );
629 // Check Image and Background image and use the largest size as the control's Natural size.
630 SizeOfActorIfLarger( GetButtonImage(), size );
631 SizeOfActorIfLarger( GetBackgroundImage(), size );
637 void PushButton::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
639 ConfigureSizeNegotiation();
642 void PushButton::ConfigureSizeNegotiation()
644 std::vector< Actor > images;
647 images.push_back( GetButtonImage() );
648 images.push_back( GetSelectedImage() );
649 images.push_back( GetSelectedBackgroundImage() );
650 images.push_back( GetBackgroundImage() );
651 images.push_back( GetDisabledImage() );
652 images.push_back( GetDisabledSelectedImage() );
653 images.push_back( GetDisabledBackgroundImage() );
655 Actor label = GetLabel();
657 for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
659 ConfigureSizeNegotiationDimension( static_cast< Dimension::Type >( 1 << i ), images, label );
666 if( label.GetResizePolicy( Dimension::WIDTH ) == ResizePolicy::USE_NATURAL_SIZE )
668 padding.left = TEXT_PADDING;
669 padding.right = TEXT_PADDING;
672 if( label.GetResizePolicy( Dimension::HEIGHT ) == ResizePolicy::USE_NATURAL_SIZE )
674 padding.top = TEXT_PADDING;
675 padding.bottom = TEXT_PADDING;
678 label.SetPadding( padding );
682 void PushButton::ConfigureSizeNegotiationDimension( Dimension::Type dimension, const std::vector< Actor >& images, Actor& label )
684 ResizePolicy::Type imageResizePolicy = ResizePolicy::FILL_TO_PARENT;
685 ResizePolicy::Type labelResizePolicy = ResizePolicy::FILL_TO_PARENT;
687 switch( Self().GetResizePolicy( dimension ) )
689 case ResizePolicy::FIT_TO_CHILDREN:
691 imageResizePolicy = labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE;
694 case ResizePolicy::USE_NATURAL_SIZE:
698 labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE;
702 imageResizePolicy = ResizePolicy::USE_NATURAL_SIZE;
714 label.SetResizePolicy( labelResizePolicy, dimension );
717 for( std::vector< Actor >::const_iterator it = images.begin(), itEnd = images.end(); it != itEnd; ++it )
722 actor.SetResizePolicy( imageResizePolicy, dimension );
727 } // namespace Internal
729 } // namespace Toolkit