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>
27 using namespace Dali; // Needed for macros
28 using namespace Dali::Toolkit;
39 Dali::BaseHandle Create()
41 return Demo::ShadowButton::New();
44 DALI_TYPE_REGISTRATION_BEGIN( ShadowButton, Dali::Toolkit::Button, Create );
46 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "activeTransition", ARRAY, ACTIVE_TRANSITION );
47 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "inactiveTransition", ARRAY, INACTIVE_TRANSITION );
48 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "clickTransition", ARRAY, CLICK_TRANSITION );
49 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "backgroundVisual", MAP, BACKGROUND_VISUAL );
50 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "checkboxBgVisual", MAP, CHECKBOX_BG_VISUAL );
51 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "checkboxFgVisual", MAP, CHECKBOX_FG_VISUAL );
52 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "labelVisual", MAP, LABEL_VISUAL );
53 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "checkState", BOOLEAN, ACTIVE_STATE );
54 DALI_PROPERTY_REGISTRATION( Demo, ShadowButton, "checkState", BOOLEAN, CHECK_STATE );
56 DALI_TYPE_REGISTRATION_END();
58 DALI_ENUM_TO_STRING_TABLE_BEGIN( VISUAL_PROPERTIES )
59 { "backgroundVisual", Demo::ShadowButton::Property::BACKGROUND_VISUAL },
60 { "checkboxBgVisual", Demo::ShadowButton::Property::CHECKBOX_BG_VISUAL},
61 { "checkboxFgVisual", Demo::ShadowButton::Property::CHECKBOX_FG_VISUAL},
62 { "labelVisual", Demo::ShadowButton::Property::LABEL_VISUAL}
63 DALI_ENUM_TO_STRING_TABLE_END( VISUAL_PROPERTIES )
65 Toolkit::TransitionData ConvertPropertyToTransition( const Property::Value& value )
67 Toolkit::TransitionData transitionData;
69 if( value.GetType() == Property::ARRAY )
71 transitionData = Toolkit::TransitionData::New( *value.GetArray());
73 else if( value.GetType() == Property::MAP )
75 transitionData = Toolkit::TransitionData::New( *value.GetMap() );
77 return transitionData;
80 } // anonymous namespace
83 Internal::ShadowButton::ShadowButton()
84 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
90 Internal::ShadowButton::~ShadowButton()
94 Demo::ShadowButton Internal::ShadowButton::New()
96 IntrusivePtr<Internal::ShadowButton> impl = new Internal::ShadowButton();
97 Demo::ShadowButton handle = Demo::ShadowButton( *impl );
102 void ShadowButton::SetActiveState( bool active )
104 if( active != mActiveState )
106 mActiveState = active;
109 StartTransition( Demo::ShadowButton::Property::ACTIVE_TRANSITION );
113 StartTransition( Demo::ShadowButton::Property::INACTIVE_TRANSITION );
118 bool ShadowButton::GetActiveState()
123 void ShadowButton::SetCheckState( bool checkState )
125 mCheckState = checkState;
126 EnableVisual( Demo::ShadowButton::Property::CHECKBOX_FG_VISUAL, mCheckState );
130 bool ShadowButton::GetCheckState()
135 void ShadowButton::StartTransition( Property::Index transitionId )
137 Transitions::iterator iter = FindTransition( transitionId );
138 if( iter != mTransitions.end() )
140 if( iter->mAnimation )
142 iter->mAnimation.Stop();
143 iter->mPlaying = false;
145 iter->mAnimation.FinishedSignal().Disconnect( this, &ShadowButton::OnTransitionFinished );
148 iter->mAnimation = CreateTransition( iter->mTransitionData );
149 StoreTargetLayouts( iter->mTransitionData );
151 iter->mAnimation.FinishedSignal().Connect( this, &ShadowButton::OnTransitionFinished );
152 iter->mAnimation.Play();
153 iter->mPlaying = true;
157 void ShadowButton::OnTransitionFinished( Animation& src )
159 ShadowButton::Transitions::iterator iter = mTransitions.begin();
160 for( ; iter != mTransitions.end(); ++iter )
162 if( iter->mAnimation == src )
164 iter->mPlaying = false;
165 iter->mAnimation.Reset(); // Remove the animation when it's finished.
166 switch( iter->mTransitionId )
168 case Demo::ShadowButton::Property::ACTIVE_TRANSITION:
170 // Consider relayouting the text.
173 case Demo::ShadowButton::Property::INACTIVE_TRANSITION:
175 // Consider relayouting the text.
184 void ShadowButton::OnInitialize()
189 void ShadowButton::OnStageConnection( int depth )
191 Control::OnStageConnection( depth );
194 void ShadowButton::OnStageDisconnection()
196 Control::OnStageDisconnection();
199 void ShadowButton::OnSizeSet( const Vector3& targetSize )
201 Control::OnSizeSet( targetSize );
202 RelayoutVisuals( Vector2( targetSize ) );
205 void ShadowButton::OnRelayout( const Vector2& targetSize, RelayoutContainer& container )
207 RelayoutVisuals( targetSize );
210 void ShadowButton::RelayoutVisuals( const Vector2& targetSize )
212 bool transitioning = false;
213 ShadowButton::Transitions::iterator iter = mTransitions.begin();
214 for( ; iter != mTransitions.end(); ++iter )
216 if( iter->mPlaying == true )
218 transitioning = true;
223 if( ! transitioning )
225 for( ShadowButton::Transforms::iterator iter = mTransforms.begin();
226 iter != mTransforms.end(); ++iter )
228 switch( iter->mTransformId )
230 case Demo::ShadowButton::Property::BACKGROUND_VISUAL:
232 mBackgroundVisual.SetTransformAndSize( iter->mTransform, targetSize );
235 case Demo::ShadowButton::Property::CHECKBOX_BG_VISUAL:
237 mCheckboxBgVisual.SetTransformAndSize( iter->mTransform, targetSize );
240 case Demo::ShadowButton::Property::CHECKBOX_FG_VISUAL:
242 mCheckboxFgVisual.SetTransformAndSize( iter->mTransform, targetSize );
245 case Demo::ShadowButton::Property::LABEL_VISUAL:
247 mLabelVisual.SetTransformAndSize( iter->mTransform, targetSize );
255 Vector3 ShadowButton::GetNaturalSize()
260 Vector2 checkboxBgSize;
261 Vector2 checkboxFgSize;
263 mCheckboxBgVisual.GetNaturalSize( checkboxBgSize );
264 mCheckboxFgVisual.GetNaturalSize( checkboxFgSize );
265 mLabelVisual.GetNaturalSize( labelSize );
267 width = std::max( checkboxBgSize.x, checkboxFgSize.x ) + labelSize.x;
268 height = std::max( std::max( checkboxFgSize.y, checkboxBgSize.y ), labelSize.y );
270 return Vector3( width, height, height );
273 void ShadowButton::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
276 Control::OnStyleChange( styleManager, change );
279 ShadowButton::Transitions::iterator ShadowButton::FindTransition( Property::Index index )
282 ShadowButton::Transitions::iterator iter = mTransitions.begin();
283 for( ; iter != mTransitions.end(); ++iter )
285 if( iter->mTransitionId == index )
293 iter = mTransitions.end();
298 ShadowButton::Transforms::iterator ShadowButton::FindTransform( Property::Index index )
301 ShadowButton::Transforms::iterator iter = mTransforms.begin();
302 for( ; iter != mTransforms.end(); ++iter )
304 if( iter->mTransformId == index )
312 iter = mTransforms.end();
317 void ShadowButton::ResetVisual(
318 Property::Index index,
319 Visual::Base& visual,
320 const Property::Value& value )
324 // we are replacing an existing visual, so force relayout
327 Property::Map* map = value.GetMap();
330 visual = Toolkit::VisualFactory::Get().CreateVisual( *map );
331 RegisterVisual( index, visual );
333 // Set the appropriate depth index.
334 // @todo Should be able to set this from the style sheet
337 case Demo::ShadowButton::Property::BACKGROUND_VISUAL:
339 visual.SetDepthIndex(0.0f);
342 case Demo::ShadowButton::Property::CHECKBOX_BG_VISUAL:
344 visual.SetDepthIndex(1.0f);
347 case Demo::ShadowButton::Property::CHECKBOX_FG_VISUAL:
349 visual.SetDepthIndex(2.0f);
352 case Demo::ShadowButton::Property::LABEL_VISUAL:
354 visual.SetDepthIndex(1.0f);
359 // Extract transform maps out of the visual definition and store them
360 Property::Value* value = map->Find( DevelVisual::Property::TRANSFORM, "transform");
363 Property::Map* transformMap = value->GetMap();
366 ShadowButton::Transforms::iterator iter = FindTransform( index );
367 if( iter != mTransforms.end() )
369 iter->mTransform = *transformMap;
373 mTransforms.push_back( Transform( index, *transformMap ) );
380 bool IsTransformProperty( const std::string& property )
382 const char* transformProperties[]= { "size", "offset", "origin", "anchorPoint", "offsetSizeMode" };
383 const int NUM_TRANSFORM_PROPERTIES = sizeof( transformProperties ) / sizeof( const char * );
386 for( int i=0; i<NUM_TRANSFORM_PROPERTIES; ++i )
388 if( property == transformProperties[i] )
397 void ShadowButton::StoreTargetLayouts( TransitionData transitionData )
400 // foreach animator in transitionData
401 // if animator{"property"} in [ "size", "offset", "origin", "anchorPoint", "offsetSizeMode" ]
402 // transforms{ animator{"target"} }->{animator{"property"}} = animator{"targetValue"}
405 for( unsigned int i=0; i < transitionData.Count(); ++i )
407 Property::Map animator = transitionData.GetAnimatorAt(i);
408 Property::Value* target = animator.Find( "target" );
412 Property::Index index;
413 if( Scripting::GetEnumerationProperty( *target, VISUAL_PROPERTIES_TABLE, VISUAL_PROPERTIES_TABLE_COUNT, index ) )
415 ShadowButton::Transforms::iterator iter = FindTransform( index );
416 if( iter != mTransforms.end() )
418 Property::Value* property = animator.Find( "property" );
421 std::string propertyString;
422 property->Get(propertyString);
423 if( IsTransformProperty( propertyString ) )
425 Property::Value* targetValue = animator.Find( "targetValue" );
428 iter->mTransform[ propertyString ] = *targetValue;
438 void ShadowButton::ResetTransition(
439 Property::Index index,
440 const Property::Value& value)
442 ShadowButton::Transitions::iterator iter = FindTransition( index );
443 if( iter != mTransitions.end() )
446 iter->mTransitionData = ConvertPropertyToTransition( value );
447 iter->mAnimation.Stop();
448 iter->mAnimation.Clear();
452 mTransitions.push_back( Transition( index, ConvertPropertyToTransition( value ) ) );
457 void ShadowButton::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
459 Demo::ShadowButton shadowButton = Demo::ShadowButton::DownCast( Dali::BaseHandle( object ) );
463 ShadowButton& impl = GetImpl( shadowButton );
466 case Demo::ShadowButton::Property::BACKGROUND_VISUAL:
468 impl.ResetVisual( index, impl.mBackgroundVisual, value );
471 case Demo::ShadowButton::Property::CHECKBOX_BG_VISUAL:
473 impl.ResetVisual( index, impl.mCheckboxBgVisual, value );
476 case Demo::ShadowButton::Property::CHECKBOX_FG_VISUAL:
478 impl.ResetVisual( index, impl.mCheckboxFgVisual, value );
479 impl.EnableVisual( Demo::ShadowButton::Property::CHECKBOX_FG_VISUAL, impl.mCheckState );
482 case Demo::ShadowButton::Property::LABEL_VISUAL:
484 impl.ResetVisual( index, impl.mLabelVisual, value );
487 case Demo::ShadowButton::Property::ACTIVE_TRANSITION:
488 case Demo::ShadowButton::Property::INACTIVE_TRANSITION:
489 case Demo::ShadowButton::Property::CLICK_TRANSITION:
491 impl.ResetTransition( index, value );
498 Property::Value ShadowButton::GetProperty( BaseObject* object, Property::Index propertyIndex )
500 Property::Value value;
502 Demo::ShadowButton shadowButton = Demo::ShadowButton::DownCast( Dali::BaseHandle( object ) );
506 ShadowButton& impl = GetImpl( shadowButton );
507 switch ( propertyIndex )
509 case Demo::ShadowButton::Property::BACKGROUND_VISUAL:
512 impl.mBackgroundVisual.CreatePropertyMap(map);
516 case Demo::ShadowButton::Property::CHECKBOX_BG_VISUAL:
519 impl.mCheckboxBgVisual.CreatePropertyMap(map);
523 case Demo::ShadowButton::Property::CHECKBOX_FG_VISUAL:
526 impl.mCheckboxFgVisual.CreatePropertyMap(map);
530 case Demo::ShadowButton::Property::LABEL_VISUAL:
533 impl.mLabelVisual.CreatePropertyMap(map);