2 * Copyright (c) 2017 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.
17 #include "shadow-button-impl.h"
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali/public-api/object/type-registry-helper.h>
20 #include <dali/devel-api/scripting/enum-helper.h>
21 #include <dali-toolkit/devel-api/align-enums.h>
22 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
23 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
24 #include <dali-toolkit/devel-api/controls/control-devel.h>
28 using namespace Dali; // Needed for macros
29 using namespace Dali::Toolkit;
40 Dali::BaseHandle Create()
42 return Demo::ShadowButton::New();
45 DALI_TYPE_REGISTRATION_BEGIN( ShadowButton, Dali::Toolkit::Button, Create );
47 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "activeTransition", ARRAY, ACTIVE_TRANSITION );
48 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "inactiveTransition", ARRAY, INACTIVE_TRANSITION );
49 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "checkTransition", ARRAY, CHECK_TRANSITION );
50 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "uncheckTransition", ARRAY, UNCHECK_TRANSITION );
51 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "backgroundVisual", MAP, BACKGROUND_VISUAL );
52 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "checkboxBgVisual", MAP, CHECKBOX_BG_VISUAL );
53 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "checkboxFgVisual", MAP, CHECKBOX_FG_VISUAL );
54 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "labelVisual", MAP, LABEL_VISUAL );
55 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "checkState", BOOLEAN, ACTIVE_STATE );
56 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "checkState", BOOLEAN, CHECK_STATE );
58 DALI_TYPE_REGISTRATION_END();
60 DALI_ENUM_TO_STRING_TABLE_BEGIN( VISUAL_PROPERTIES )
61 { "backgroundVisual", Demo::ShadowButton::Property::BACKGROUND_VISUAL },
62 { "checkboxBgVisual", Demo::ShadowButton::Property::CHECKBOX_BG_VISUAL},
63 { "checkboxFgVisual", Demo::ShadowButton::Property::CHECKBOX_FG_VISUAL},
64 { "labelVisual", Demo::ShadowButton::Property::LABEL_VISUAL}
65 DALI_ENUM_TO_STRING_TABLE_END( VISUAL_PROPERTIES )
67 Toolkit::TransitionData ConvertPropertyToTransition( const Property::Value& value )
69 Toolkit::TransitionData transitionData;
71 if( value.GetType() == Property::ARRAY )
73 transitionData = Toolkit::TransitionData::New( *value.GetArray());
75 else if( value.GetType() == Property::MAP )
77 transitionData = Toolkit::TransitionData::New( *value.GetMap() );
79 return transitionData;
82 } // anonymous namespace
85 Internal::ShadowButton::ShadowButton()
86 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
92 Internal::ShadowButton::~ShadowButton()
96 Demo::ShadowButton Internal::ShadowButton::New()
98 IntrusivePtr<Internal::ShadowButton> impl = new Internal::ShadowButton();
99 Demo::ShadowButton handle = Demo::ShadowButton( *impl );
104 void ShadowButton::SetActiveState( bool active )
106 if( active != mActiveState )
108 mActiveState = active;
111 StartTransition( Demo::ShadowButton::Property::ACTIVE_TRANSITION );
115 StartTransition( Demo::ShadowButton::Property::INACTIVE_TRANSITION );
120 bool ShadowButton::GetActiveState()
125 void ShadowButton::SetCheckState( bool checkState )
127 mCheckState = checkState;
128 DevelControl::EnableVisual( *this, Demo::ShadowButton::Property::CHECKBOX_FG_VISUAL, true );
129 if( Self().OnStage() )
133 StartTransition( Demo::ShadowButton::Property::CHECK_TRANSITION );
137 StartTransition( Demo::ShadowButton::Property::UNCHECK_TRANSITION );
143 bool ShadowButton::GetCheckState()
148 void ShadowButton::StartTransition( Property::Index transitionId )
150 Transitions::iterator iter = FindTransition( transitionId );
151 if( iter != mTransitions.end() )
153 if( iter->mAnimation )
155 iter->mAnimation.Stop();
156 iter->mPlaying = false;
158 iter->mAnimation.FinishedSignal().Disconnect( this, &ShadowButton::OnTransitionFinished );
161 iter->mAnimation = DevelControl::CreateTransition( *this, iter->mTransitionData );
162 StoreTargetLayouts( iter->mTransitionData );
164 iter->mAnimation.FinishedSignal().Connect( this, &ShadowButton::OnTransitionFinished );
165 iter->mAnimation.Play();
166 iter->mPlaying = true;
170 void ShadowButton::OnTransitionFinished( Animation& src )
172 ShadowButton::Transitions::iterator iter = mTransitions.begin();
173 for( ; iter != mTransitions.end(); ++iter )
175 if( iter->mAnimation == src )
177 iter->mPlaying = false;
178 iter->mAnimation.Reset(); // Remove the animation when it's finished.
179 switch( iter->mTransitionId )
181 case Demo::ShadowButton::Property::ACTIVE_TRANSITION:
183 // Consider relayouting the text.
186 case Demo::ShadowButton::Property::INACTIVE_TRANSITION:
188 // Consider relayouting the text.
191 case Demo::ShadowButton::Property::UNCHECK_TRANSITION:
193 DevelControl::EnableVisual( *this, Demo::ShadowButton::Property::CHECKBOX_FG_VISUAL, false );
202 void ShadowButton::OnInitialize()
207 void ShadowButton::OnStageConnection( int depth )
209 Control::OnStageConnection( depth );
212 void ShadowButton::OnStageDisconnection()
214 Control::OnStageDisconnection();
217 void ShadowButton::OnSizeSet( const Vector3& targetSize )
219 Control::OnSizeSet( targetSize );
220 RelayoutVisuals( Vector2( targetSize ) );
223 void ShadowButton::OnRelayout( const Vector2& targetSize, RelayoutContainer& container )
225 RelayoutVisuals( targetSize );
228 void ShadowButton::RelayoutVisuals( const Vector2& targetSize )
230 bool transitioning = false;
231 ShadowButton::Transitions::iterator iter = mTransitions.begin();
232 for( ; iter != mTransitions.end(); ++iter )
234 if( iter->mPlaying == true )
236 transitioning = true;
241 if( ! transitioning )
243 for( ShadowButton::Transforms::iterator iter = mTransforms.begin();
244 iter != mTransforms.end(); ++iter )
246 switch( iter->mTransformId )
248 case Demo::ShadowButton::Property::BACKGROUND_VISUAL:
250 mBackgroundVisual.SetTransformAndSize( iter->mTransform, targetSize );
253 case Demo::ShadowButton::Property::CHECKBOX_BG_VISUAL:
255 mCheckboxBgVisual.SetTransformAndSize( iter->mTransform, targetSize );
258 case Demo::ShadowButton::Property::CHECKBOX_FG_VISUAL:
260 mCheckboxFgVisual.SetTransformAndSize( iter->mTransform, targetSize );
263 case Demo::ShadowButton::Property::LABEL_VISUAL:
265 mLabelVisual.SetTransformAndSize( iter->mTransform, targetSize );
273 Vector3 ShadowButton::GetNaturalSize()
278 Vector2 checkboxBgSize;
279 Vector2 checkboxFgSize;
281 mCheckboxBgVisual.GetNaturalSize( checkboxBgSize );
282 mCheckboxFgVisual.GetNaturalSize( checkboxFgSize );
283 mLabelVisual.GetNaturalSize( labelSize );
285 width = std::max( checkboxBgSize.x, checkboxFgSize.x ) + labelSize.x;
286 height = std::max( std::max( checkboxFgSize.y, checkboxBgSize.y ), labelSize.y );
288 return Vector3( width, height, height );
291 void ShadowButton::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
294 Control::OnStyleChange( styleManager, change );
297 ShadowButton::Transitions::iterator ShadowButton::FindTransition( Property::Index index )
300 ShadowButton::Transitions::iterator iter = mTransitions.begin();
301 for( ; iter != mTransitions.end(); ++iter )
303 if( iter->mTransitionId == index )
311 iter = mTransitions.end();
316 ShadowButton::Transforms::iterator ShadowButton::FindTransform( Property::Index index )
319 ShadowButton::Transforms::iterator iter = mTransforms.begin();
320 for( ; iter != mTransforms.end(); ++iter )
322 if( iter->mTransformId == index )
330 iter = mTransforms.end();
335 void ShadowButton::ResetVisual(
336 Property::Index index,
337 Visual::Base& visual,
338 const Property::Value& value )
342 // we are replacing an existing visual, so force relayout
345 Property::Map* map = value.GetMap();
348 visual = Toolkit::VisualFactory::Get().CreateVisual( *map );
350 // Set the appropriate depth index.
351 // @todo Should be able to set this from the style sheet
354 case Demo::ShadowButton::Property::BACKGROUND_VISUAL:
356 DevelControl::RegisterVisual( *this, index, visual, 0 );
359 case Demo::ShadowButton::Property::CHECKBOX_BG_VISUAL:
361 DevelControl::RegisterVisual( *this, index, visual, 1 );
364 case Demo::ShadowButton::Property::CHECKBOX_FG_VISUAL:
366 DevelControl::RegisterVisual( *this, index, visual, mCheckState, 2 );
369 case Demo::ShadowButton::Property::LABEL_VISUAL:
371 DevelControl::RegisterVisual( *this, index, visual, 1 );
376 // Extract transform maps out of the visual definition and store them
377 Property::Value* value = map->Find( DevelVisual::Property::TRANSFORM, "transform");
380 Property::Map* transformMap = value->GetMap();
383 ShadowButton::Transforms::iterator iter = FindTransform( index );
384 if( iter != mTransforms.end() )
386 iter->mTransform = *transformMap;
390 mTransforms.push_back( Transform( index, *transformMap ) );
397 bool IsTransformProperty( const std::string& property )
399 const char* transformProperties[]= { "size", "offset", "origin", "anchorPoint", "offsetPolicy", "sizePolicy" };
400 const int NUM_TRANSFORM_PROPERTIES = sizeof( transformProperties ) / sizeof( const char * );
403 for( int i=0; i<NUM_TRANSFORM_PROPERTIES; ++i )
405 if( property == transformProperties[i] )
414 void ShadowButton::StoreTargetLayouts( TransitionData transitionData )
417 // foreach animator in transitionData
418 // if animator{"property"} in [ "size", "offset", "origin", "anchorPoint", "offsetPolicy", "sizePolicy" ]
419 // transforms{ animator{"target"} }->{animator{"property"}} = animator{"targetValue"}
422 for( unsigned int i=0; i < transitionData.Count(); ++i )
424 Property::Map animator = transitionData.GetAnimatorAt(i);
425 Property::Value* target = animator.Find( "target" );
429 Property::Index index;
430 if( Scripting::GetEnumerationProperty( *target, VISUAL_PROPERTIES_TABLE, VISUAL_PROPERTIES_TABLE_COUNT, index ) )
432 ShadowButton::Transforms::iterator iter = FindTransform( index );
433 if( iter != mTransforms.end() )
435 Property::Value* property = animator.Find( "property" );
438 std::string propertyString;
439 property->Get(propertyString);
440 if( IsTransformProperty( propertyString ) )
442 Property::Value* targetValue = animator.Find( "targetValue" );
445 iter->mTransform[ propertyString ] = *targetValue;
455 void ShadowButton::ResetTransition(
456 Property::Index index,
457 const Property::Value& value)
459 ShadowButton::Transitions::iterator iter = FindTransition( index );
460 if( iter != mTransitions.end() )
463 iter->mTransitionData = ConvertPropertyToTransition( value );
464 iter->mAnimation.Stop();
465 iter->mAnimation.Clear();
469 mTransitions.push_back( Transition( index, ConvertPropertyToTransition( value ) ) );
474 void ShadowButton::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
476 Demo::ShadowButton shadowButton = Demo::ShadowButton::DownCast( Dali::BaseHandle( object ) );
480 ShadowButton& impl = GetImpl( shadowButton );
483 case Demo::ShadowButton::Property::BACKGROUND_VISUAL:
485 impl.ResetVisual( index, impl.mBackgroundVisual, value );
488 case Demo::ShadowButton::Property::CHECKBOX_BG_VISUAL:
490 impl.ResetVisual( index, impl.mCheckboxBgVisual, value );
493 case Demo::ShadowButton::Property::CHECKBOX_FG_VISUAL:
495 impl.ResetVisual( index, impl.mCheckboxFgVisual, value );
496 DevelControl::EnableVisual( impl, Demo::ShadowButton::Property::CHECKBOX_FG_VISUAL, impl.mCheckState );
499 case Demo::ShadowButton::Property::LABEL_VISUAL:
501 impl.ResetVisual( index, impl.mLabelVisual, value );
504 case Demo::ShadowButton::Property::ACTIVE_TRANSITION:
505 case Demo::ShadowButton::Property::INACTIVE_TRANSITION:
506 case Demo::ShadowButton::Property::CHECK_TRANSITION:
507 case Demo::ShadowButton::Property::UNCHECK_TRANSITION:
509 impl.ResetTransition( index, value );
516 Property::Value ShadowButton::GetProperty( BaseObject* object, Property::Index propertyIndex )
518 Property::Value value;
520 Demo::ShadowButton shadowButton = Demo::ShadowButton::DownCast( Dali::BaseHandle( object ) );
524 ShadowButton& impl = GetImpl( shadowButton );
525 switch ( propertyIndex )
527 case Demo::ShadowButton::Property::BACKGROUND_VISUAL:
530 impl.mBackgroundVisual.CreatePropertyMap(map);
534 case Demo::ShadowButton::Property::CHECKBOX_BG_VISUAL:
537 impl.mCheckboxBgVisual.CreatePropertyMap(map);
541 case Demo::ShadowButton::Property::CHECKBOX_FG_VISUAL:
544 impl.mCheckboxFgVisual.CreatePropertyMap(map);
548 case Demo::ShadowButton::Property::LABEL_VISUAL:
551 impl.mLabelVisual.CreatePropertyMap(map);