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 );
133 void PushButton::OnSelectedImageSet()
135 Actor& selectedImage = GetSelectedImage();
137 selectedImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
140 void PushButton::OnBackgroundImageSet()
142 Actor& backgroundImage = GetBackgroundImage();
144 backgroundImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
147 void PushButton::OnSelectedBackgroundImageSet()
149 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
151 selectedBackgroundImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
154 void PushButton::OnDisabledImageSet()
156 Actor& disabledImage = GetDisabledImage();
158 disabledImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
161 void PushButton::OnDisabledBackgroundImageSet()
163 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
165 disabledBackgroundImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
168 bool PushButton::OnSelected()
170 Actor& buttonImage = GetButtonImage();
171 Actor& selectedImage = GetSelectedImage();
172 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
174 PaintState paintState = GetPaintState();
178 case UnselectedState:
180 FadeOutImage( buttonImage );
181 FadeInImage( selectedBackgroundImage );
182 FadeInImage( selectedImage );
183 StartTransitionAnimation();
188 FadeOutImage( selectedBackgroundImage );
189 FadeOutImage( selectedImage );
190 FadeInImage( buttonImage );
191 StartTransitionAnimation();
194 case UnselectedSelectedTransition:
199 opacity = selectedImage.GetCurrentOpacity();
202 StopTransitionAnimation( false );
203 FadeOutImage( selectedBackgroundImage, opacity );
204 FadeOutImage( selectedImage, opacity );
205 FadeInImage( buttonImage, 1.f - opacity );
206 StartTransitionAnimation();
209 case SelectedUnselectedTransition:
214 opacity = selectedImage.GetCurrentOpacity();
217 StopTransitionAnimation( false );
218 FadeOutImage( buttonImage, 1.f - opacity );
219 FadeInImage( selectedBackgroundImage, opacity );
220 FadeInImage( selectedImage, opacity );
221 StartTransitionAnimation();
224 case DisabledUnselectedTransition:
226 StopTransitionAnimation();
227 FadeOutImage( buttonImage );
228 FadeInImage( selectedBackgroundImage );
229 FadeInImage( selectedImage );
230 StartTransitionAnimation();
233 case DisabledSelectedTransition:
235 StopTransitionAnimation();
236 FadeOutImage( selectedBackgroundImage );
237 FadeOutImage( selectedImage );
238 FadeInImage( buttonImage );
239 StartTransitionAnimation();
248 if( mTransitionAnimation )
256 bool PushButton::OnDisabled()
258 Actor& buttonImage = GetButtonImage();
259 Actor& selectedImage = GetSelectedImage();
260 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
261 Actor& backgroundImage = GetBackgroundImage();
262 Actor& disabledImage = GetDisabledImage();
263 Actor& disabledSelectedImage = GetDisabledSelectedImage();
264 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
266 PaintState paintState = GetPaintState();
270 case UnselectedState:
272 FadeOutImage( backgroundImage );
273 FadeOutImage( buttonImage );
274 FadeInImage( disabledBackgroundImage );
275 FadeInImage( disabledImage );
276 StartTransitionAnimation();
281 FadeOutImage( backgroundImage );
282 FadeOutImage( selectedBackgroundImage );
283 FadeOutImage( selectedImage );
284 FadeInImage( disabledBackgroundImage );
285 FadeInImage( disabledSelectedImage );
286 StartTransitionAnimation();
289 case DisabledUnselectedState:
291 FadeOutImage( disabledBackgroundImage );
292 FadeOutImage( disabledImage );
293 FadeInImage( backgroundImage );
294 FadeInImage( buttonImage );
295 StartTransitionAnimation();
298 case DisabledSelectedState:
300 FadeOutImage( disabledBackgroundImage );
301 FadeOutImage( disabledSelectedImage );
302 FadeInImage( backgroundImage );
303 FadeInImage( selectedBackgroundImage );
304 FadeInImage( selectedImage );
305 StartTransitionAnimation();
308 case UnselectedSelectedTransition:
313 opacity = selectedImage.GetCurrentOpacity();
316 StopTransitionAnimation();
317 FadeOutImage( backgroundImage );
318 FadeOutImage( selectedBackgroundImage, opacity );
319 FadeOutImage( selectedImage, opacity );
320 FadeInImage( disabledBackgroundImage );
321 FadeInImage( disabledSelectedImage );
322 StartTransitionAnimation();
325 case SelectedUnselectedTransition:
330 opacity = buttonImage.GetCurrentOpacity();
333 StopTransitionAnimation();
334 FadeOutImage( backgroundImage );
335 FadeOutImage( buttonImage, opacity );
336 FadeInImage( disabledBackgroundImage );
337 FadeInImage( disabledImage );
338 StartTransitionAnimation();
341 case UnselectedDisabledTransition:
346 opacity = disabledImage.GetCurrentOpacity();
349 StopTransitionAnimation( false );
350 FadeOutImage( disabledBackgroundImage, opacity );
351 FadeOutImage( disabledImage, opacity );
352 FadeInImage( backgroundImage, 1.f - opacity );
353 FadeInImage( buttonImage, 1.f - opacity );
354 StartTransitionAnimation();
357 case DisabledUnselectedTransition:
362 opacity = buttonImage.GetCurrentOpacity();
365 StopTransitionAnimation( false );
366 FadeOutImage( backgroundImage, opacity );
367 FadeOutImage( buttonImage, opacity );
368 FadeInImage( disabledBackgroundImage, 1.f - opacity );
369 FadeInImage( disabledImage, 1.f - opacity );
370 StartTransitionAnimation();
373 case SelectedDisabledTransition:
376 if( disabledSelectedImage )
378 opacity = disabledSelectedImage.GetCurrentOpacity();
381 StopTransitionAnimation( false );
382 FadeOutImage( disabledBackgroundImage, opacity );
383 FadeOutImage( disabledSelectedImage, opacity );
384 FadeInImage( backgroundImage, 1.f - opacity );
385 FadeInImage( selectedBackgroundImage, 1.f - opacity );
386 FadeInImage( selectedImage, 1.f - opacity );
387 StartTransitionAnimation();
390 case DisabledSelectedTransition:
395 opacity = selectedImage.GetCurrentOpacity();
398 StopTransitionAnimation( false );
399 FadeOutImage( backgroundImage, opacity );
400 FadeOutImage( selectedBackgroundImage, opacity );
401 FadeOutImage( selectedImage, opacity );
402 FadeInImage( disabledBackgroundImage, 1.f - opacity );
403 FadeInImage( disabledSelectedImage, 1.f - opacity );
404 StartTransitionAnimation();
409 if( mTransitionAnimation )
417 bool PushButton::OnPressed()
419 Actor& buttonImage = GetButtonImage();
420 Actor& selectedImage = GetSelectedImage();
421 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
423 PaintState paintState = GetPaintState();
427 case UnselectedState:
429 FadeOutImage( buttonImage );
430 FadeInImage( selectedBackgroundImage );
431 FadeInImage( selectedImage );
432 StartTransitionAnimation();
435 case SelectedUnselectedTransition:
440 opacity = buttonImage.GetCurrentOpacity();
443 StopTransitionAnimation( false );
444 FadeOutImage( buttonImage, opacity );
445 FadeInImage( selectedBackgroundImage, 1.f - opacity );
446 FadeInImage( selectedImage, 1.f - opacity );
447 StartTransitionAnimation();
450 case DisabledUnselectedTransition:
455 opacity = buttonImage.GetCurrentOpacity();
458 StopTransitionAnimation();
459 FadeOutImage( buttonImage, opacity );
460 FadeInImage( selectedBackgroundImage );
461 FadeInImage( selectedImage );
462 StartTransitionAnimation();
469 if( mTransitionAnimation )
477 bool PushButton::OnReleased()
479 Actor& buttonImage = GetButtonImage();
480 Actor& selectedImage = GetSelectedImage();
481 Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
483 PaintState paintState = GetPaintState();
489 FadeOutImage( selectedBackgroundImage );
490 FadeOutImage( selectedImage );
491 FadeInImage( buttonImage );
492 StartTransitionAnimation();
495 case UnselectedSelectedTransition:
500 opacity = selectedImage.GetCurrentOpacity();
503 StopTransitionAnimation( false );
504 FadeOutImage( selectedBackgroundImage, opacity );
505 FadeOutImage( selectedImage, opacity );
506 FadeInImage( buttonImage, 1.f - opacity );
507 StartTransitionAnimation();
510 case DisabledSelectedTransition:
515 opacity = selectedImage.GetCurrentOpacity();
518 StopTransitionAnimation();
519 FadeOutImage( selectedBackgroundImage, opacity );
520 FadeOutImage( selectedImage, opacity );
521 FadeInImage( buttonImage );
522 StartTransitionAnimation();
531 if( mTransitionAnimation )
539 void PushButton::StopAllAnimations()
541 StopTransitionAnimation();
544 void PushButton::OnControlSizeSet( const Vector3& targetSize )
546 if( targetSize != mSize )
550 Actor& label = GetLabel();
554 label.SetSize( mSize );
559 Vector3 PushButton::GetNaturalSize()
561 Vector3 size = Control::GetNaturalSize();
563 const bool widthIsZero = EqualsZero( size.width );
564 const bool heightIsZero = EqualsZero( size.height );
566 if( widthIsZero || heightIsZero )
568 // If background and background not scale9 try get size from that
569 ImageActor imageActor = FindImageActor( GetButtonImage() );
570 if( imageActor && imageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
572 Vector3 imageSize = imageActor.GetNaturalSize();
576 size.width = imageSize.width;
581 size.height = imageSize.height;
585 ImageActor backgroundImageActor = FindImageActor( GetBackgroundImage() );
586 if( backgroundImageActor && backgroundImageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
588 Vector3 imageSize = backgroundImageActor.GetNaturalSize();
592 size.width = std::max( size.width, imageSize.width );
597 size.height = std::max( size.height, imageSize.height );
601 // If label, test against it's size
602 Toolkit::TextView textView = Toolkit::TextView::DownCast( GetLabel() );
605 Vector3 textViewSize = textView.GetNaturalSize();
609 size.width = std::max( size.width, textViewSize.width + TEXT_PADDING * 2.0f );
614 size.height = std::max( size.height, textViewSize.height + TEXT_PADDING * 2.0f );
622 void PushButton::StartTransitionAnimation()
624 if( mTransitionAnimation )
626 mTransitionAnimation.FinishedSignal().Connect( this, &PushButton::TransitionAnimationFinished );
627 mTransitionAnimation.Play();
631 void PushButton::StopTransitionAnimation( bool remove )
633 if( mTransitionAnimation )
635 mTransitionAnimation.Clear();
636 mTransitionAnimation.Reset();
641 UpdatePaintTransitionState();
645 void PushButton::FadeInImage( Actor& image, float opacity, Vector3 scale )
649 image.SetOpacity( opacity );
650 image.SetScale( scale );
652 if( !mTransitionAnimation )
654 mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
657 mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 1.f );
661 void PushButton::FadeOutImage( Actor& image, float opacity, Vector3 scale )
665 image.SetOpacity( opacity );
666 image.SetScale( scale );
668 if( !mTransitionAnimation )
670 mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
673 mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 0.f );
677 void PushButton::TransitionAnimationFinished( Dali::Animation& source )
679 StopTransitionAnimation();
682 } // namespace Internal
684 } // namespace Toolkit