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.
19 #include "control-data-impl.h"
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/devel-api/object/handle-devel.h>
25 #include <dali/devel-api/scripting/enum-helper.h>
26 #include <dali/devel-api/scripting/scripting.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/public-api/object/type-registry-helper.h>
33 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
34 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
35 #include <dali-toolkit/internal/styling/style-manager-impl.h>
36 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
37 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
38 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
39 #include <dali-toolkit/devel-api/controls/control-devel.h>
40 #include <dali-toolkit/devel-api/controls/control-wrapper-impl.h>
51 extern const Dali::Scripting::StringEnum ControlStateTable[];
52 extern const unsigned int ControlStateTableCount;
55 // Not static or anonymous - shared with other translation units
56 const Scripting::StringEnum ControlStateTable[] = {
57 { "NORMAL", Toolkit::DevelControl::NORMAL },
58 { "FOCUSED", Toolkit::DevelControl::FOCUSED },
59 { "DISABLED", Toolkit::DevelControl::DISABLED },
61 const unsigned int ControlStateTableCount = sizeof( ControlStateTable ) / sizeof( ControlStateTable[0] );
68 #if defined(DEBUG_ENABLED)
69 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
74 void Remove( Dictionary<T>& keyValues, const std::string& name )
76 keyValues.Remove(name);
79 void Remove( DictionaryKeys& keys, const std::string& name )
81 DictionaryKeys::iterator iter = std::find( keys.begin(), keys.end(), name );
82 if( iter != keys.end())
88 Toolkit::DevelVisual::Type GetVisualTypeFromMap( const Property::Map& map )
90 Property::Value* typeValue = map.Find( Toolkit::DevelVisual::Property::TYPE, VISUAL_TYPE );
91 Toolkit::DevelVisual::Type type = Toolkit::DevelVisual::IMAGE;
94 Scripting::GetEnumerationProperty( *typeValue, VISUAL_TYPE_TABLE, VISUAL_TYPE_TABLE_COUNT, type );
100 * Finds visual in given array, returning true if found along with the iterator for that visual as a out parameter
102 bool FindVisual( Property::Index targetIndex, const RegisteredVisualContainer& visuals, RegisteredVisualContainer::Iterator& iter )
104 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
106 if ( (*iter)->index == targetIndex )
114 void FindChangableVisuals( Dictionary<Property::Map>& stateVisualsToAdd,
115 Dictionary<Property::Map>& stateVisualsToChange,
116 DictionaryKeys& stateVisualsToRemove)
118 DictionaryKeys copyOfStateVisualsToRemove = stateVisualsToRemove;
120 for( DictionaryKeys::iterator iter = copyOfStateVisualsToRemove.begin();
121 iter != copyOfStateVisualsToRemove.end(); ++iter )
123 const std::string& visualName = (*iter);
124 Property::Map* toMap = stateVisualsToAdd.Find( visualName );
127 stateVisualsToChange.Add( visualName, *toMap );
128 stateVisualsToAdd.Remove( visualName );
129 Remove( stateVisualsToRemove, visualName );
134 Toolkit::Visual::Base GetVisualByName(
135 const RegisteredVisualContainer& visuals,
136 const std::string& visualName )
138 Toolkit::Visual::Base visualHandle;
140 RegisteredVisualContainer::Iterator iter;
141 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
143 Toolkit::Visual::Base visual = (*iter)->visual;
144 if( visual && visual.GetName() == visualName )
146 visualHandle = visual;
154 * Move visual from source to destination container
156 void MoveVisual( RegisteredVisualContainer::Iterator sourceIter, RegisteredVisualContainer& source, RegisteredVisualContainer& destination )
158 Toolkit::Visual::Base visual = (*sourceIter)->visual;
161 RegisteredVisual* rv = source.Release( sourceIter );
162 destination.PushBack( rv );
167 * Performs actions as requested using the action name.
168 * @param[in] object The object on which to perform the action.
169 * @param[in] actionName The action to perform.
170 * @param[in] attributes The attributes with which to perfrom this action.
171 * @return true if action has been accepted by this control
173 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibilityActivated";
174 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
178 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
180 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
183 // if cast succeeds there is an implementation so no need to check
184 ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
192 * Connects a callback function with the object's signals.
193 * @param[in] object The object providing the signal.
194 * @param[in] tracker Used to disconnect the signal.
195 * @param[in] signalName The signal to connect to.
196 * @param[in] functor A newly allocated FunctorDelegate.
197 * @return True if the signal was connected.
198 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
200 const char* SIGNAL_KEY_EVENT = "keyEvent";
201 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "keyInputFocusGained";
202 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "keyInputFocusLost";
203 const char* SIGNAL_TAPPED = "tapped";
204 const char* SIGNAL_PANNED = "panned";
205 const char* SIGNAL_PINCHED = "pinched";
206 const char* SIGNAL_LONG_PRESSED = "longPressed";
207 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
209 Dali::BaseHandle handle( object );
211 bool connected( false );
212 Toolkit::Control control = Toolkit::Control::DownCast( handle );
215 Internal::Control& controlImpl( Internal::GetImplementation( control ) );
218 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
220 controlImpl.KeyEventSignal().Connect( tracker, functor );
222 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
224 controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
226 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
228 controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
230 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
232 controlImpl.EnableGestureDetection( Gesture::Tap );
233 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
235 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
237 controlImpl.EnableGestureDetection( Gesture::Pan );
238 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
240 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
242 controlImpl.EnableGestureDetection( Gesture::Pinch );
243 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
245 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
247 controlImpl.EnableGestureDetection( Gesture::LongPress );
248 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
255 * Creates control through type registry
259 return Internal::Control::New();
261 // Setup signals and actions using the type-registry.
262 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
264 // Note: Properties are registered separately below.
266 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
267 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
268 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
269 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
270 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
271 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
272 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
274 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
276 DALI_TYPE_REGISTRATION_END()
278 } // unnamed namespace
281 // Properties registered without macro to use specific member variables.
282 const PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "styleName", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
283 const PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "backgroundColor", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
284 const PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "backgroundImage", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
285 const PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "keyInputFocus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
286 const PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "background", Toolkit::Control::Property::BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
287 const PropertyRegistration Control::Impl::PROPERTY_6( typeRegistration, "tooltip", Toolkit::DevelControl::Property::TOOLTIP, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
288 const PropertyRegistration Control::Impl::PROPERTY_7( typeRegistration, "state", Toolkit::DevelControl::Property::STATE, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
289 const PropertyRegistration Control::Impl::PROPERTY_8( typeRegistration, "subState", Toolkit::DevelControl::Property::SUB_STATE, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
290 const PropertyRegistration Control::Impl::PROPERTY_9( typeRegistration, "leftFocusableActorId", Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID, Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
291 const PropertyRegistration Control::Impl::PROPERTY_10( typeRegistration, "rightFocusableActorId", Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID,Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
292 const PropertyRegistration Control::Impl::PROPERTY_11( typeRegistration, "upFocusableActorId", Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID, Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
293 const PropertyRegistration Control::Impl::PROPERTY_12( typeRegistration, "downFocusableActorId", Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID, Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
297 Control::Impl::Impl( Control& controlImpl )
298 : mControlImpl( controlImpl ),
299 mState( Toolkit::DevelControl::NORMAL ),
301 mLeftFocusableActorId( -1 ),
302 mRightFocusableActorId( -1 ),
303 mUpFocusableActorId( -1 ),
304 mDownFocusableActorId( -1 ),
306 mBackgroundColor(Color::TRANSPARENT),
307 mStartingPinchScale( NULL ),
309 mPinchGestureDetector(),
310 mPanGestureDetector(),
311 mTapGestureDetector(),
312 mLongPressGestureDetector(),
313 mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
314 mIsKeyboardNavigationSupported( false ),
315 mIsKeyboardFocusGroup( false )
320 Control::Impl::~Impl()
322 // All gesture detectors will be destroyed so no need to disconnect.
323 delete mStartingPinchScale;
326 Control::Impl& Control::Impl::Get( Internal::Control& internalControl )
328 return *internalControl.mImpl;
331 const Control::Impl& Control::Impl::Get( const Internal::Control& internalControl )
333 return *internalControl.mImpl;
336 // Gesture Detection Methods
337 void Control::Impl::PinchDetected(Actor actor, const PinchGesture& pinch)
339 mControlImpl.OnPinch(pinch);
342 void Control::Impl::PanDetected(Actor actor, const PanGesture& pan)
344 mControlImpl.OnPan(pan);
347 void Control::Impl::TapDetected(Actor actor, const TapGesture& tap)
349 mControlImpl.OnTap(tap);
352 void Control::Impl::LongPressDetected(Actor actor, const LongPressGesture& longPress)
354 mControlImpl.OnLongPress(longPress);
357 // Called by a Visual when it's resource is ready
358 void Control::Impl::ResourceReady( Visual::Base& object)
360 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "ResourceReady \n");
362 // A resource is ready, check if is in the replacement visual container
363 // Iterate through all visuals in replacement container and store indexes of ready visuals
364 Dali::Vector <Property::Index> readyVisuals;
365 Actor self = mControlImpl.Self();
367 for( auto replacementVisualIter = mReplacementVisuals.Begin();
368 replacementVisualIter < mReplacementVisuals.End(); ++replacementVisualIter )
370 const Toolkit::Visual::Base replacementVisual = (*replacementVisualIter)->visual;
371 const Internal::Visual::Base& replacementVisualImpl = Toolkit::GetImplementation( replacementVisual );
373 if( replacementVisualImpl.IsResourceReady() )
375 // Check if new replacement visual (index) is already queued for replacement and swap old for new.
376 RegisteredVisualContainer::Iterator registeredVisualsIter;
377 if( FindVisual( (*replacementVisualIter)->index, mVisuals, registeredVisualsIter ) )
379 Property::Index readyVisualIndex = (*replacementVisualIter)->index;
380 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "ResourceReady: %d Ready to replace\n", readyVisualIndex );
381 readyVisuals.PushBack( readyVisualIndex );
382 // Remove current shown visual from stage and from registered visuals container
383 Toolkit::GetImplementation((*registeredVisualsIter)->visual).SetOffStage( self );
384 mVisuals.Erase( registeredVisualsIter );
389 for( auto readyVisualsIter = readyVisuals.Begin(); readyVisualsIter != readyVisuals.End(); readyVisualsIter++ )
391 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "ResourceReady: %d Matched\n", (*readyVisualsIter) );
392 // Move new visual to be shown from replacement container into the control's registered visuals container
393 // Replacement visual has already been set on stage when it was added to replacement container
394 RegisteredVisualContainer::Iterator readyReplacementVisual;
395 if( FindVisual( (*readyVisualsIter) , mReplacementVisuals, readyReplacementVisual ) )
397 MoveVisual( readyReplacementVisual, mReplacementVisuals, mVisuals ); // Erases visual from replacement queue
399 // A visual has been replaced so control will most likely need relayouting
400 mControlImpl.RelayoutRequest();
403 // Emit signal if all enabled visuals registered by the control are ready.
404 // If any visual is registered and enabled but not ready then the control will not be able to emit ReadySignal.
406 if ( IsResourceReady() )
408 Dali::Toolkit::Control handle( mControlImpl.GetOwner() );
409 mResourceReadySignal.Emit( handle );
413 bool Control::Impl::IsResourceReady() const
415 // Iterate through and check all the enabled visuals are ready
416 for ( auto visualIter = mVisuals.Begin();
417 visualIter != mVisuals.End(); ++visualIter )
419 const Toolkit::Visual::Base visual = (*visualIter)->visual;
420 const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
422 // one of the enabled visuals is not ready
423 if( !visualImpl.IsResourceReady() && (*visualIter)->enabled )
431 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
433 RegisterVisual( index, visual, VisualState::ENABLED, DepthIndexValue::NOT_SET );
436 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, int depthIndex )
438 RegisterVisual( index, visual, VisualState::ENABLED, DepthIndexValue::SET, depthIndex );
441 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
443 RegisterVisual( index, visual, ( enabled ? VisualState::ENABLED : VisualState::DISABLED ), DepthIndexValue::NOT_SET );
446 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled, int depthIndex )
448 RegisterVisual( index, visual, ( enabled ? VisualState::ENABLED : VisualState::DISABLED ), DepthIndexValue::SET, depthIndex );
451 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, VisualState::Type enabled, DepthIndexValue::Type depthIndexValueSet, int depthIndex )
453 DALI_LOG_INFO( gLogFilter, Debug::Concise, "RegisterVisual:%d \n", index );
455 bool visualReplaced ( false );
456 Actor self = mControlImpl.Self();
458 if( !mVisuals.Empty() )
460 RegisteredVisualContainer::Iterator registeredVisualsiter;
461 // Check if visual (index) is already registered. Replace if so.
462 if( FindVisual( index, mVisuals, registeredVisualsiter ) )
464 if( (*registeredVisualsiter)->visual )
466 // Store current visual depth index as may need to set the replacement visual to same depth
467 const int currentDepthIndex = (*registeredVisualsiter)->visual.GetDepthIndex();
469 // Monitor when the visuals resources are ready
470 StopObservingVisual( (*registeredVisualsiter)->visual );
471 StartObservingVisual( visual );
475 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "RegisterVisual Adding visual to replacement Queue: %d \n", index );
476 // Check if visual is currently in the process of being replaced
477 RegisteredVisualContainer::Iterator queuedReplacementVisual;
478 if ( FindVisual( index, mReplacementVisuals, queuedReplacementVisual ) )
480 // If visual on replacement queue is going to be replaced before it's ready then will be removed from queue (and stage)
481 // Only the the last requested visual will be queued and then displayed.
482 Toolkit::GetImplementation( (*queuedReplacementVisual)->visual ).SetOffStage( self );
483 mReplacementVisuals.Erase(queuedReplacementVisual);
485 // Add to replacement list
486 mReplacementVisuals.PushBack( new RegisteredVisual( index, visual, ( enabled == VisualState::ENABLED ? true : false ) ) );
490 // Not staged so can just replace registered visual
491 (*registeredVisualsiter)->visual = visual;
492 (*registeredVisualsiter)->enabled = ( enabled == VisualState::ENABLED ) ? true : false;
495 // If we've not set the depth-index value and the new visual does not have a depth index applied to it, then use the previously set depth-index for this index
496 if( ( depthIndexValueSet == DepthIndexValue::NOT_SET ) &&
497 ( visual.GetDepthIndex() == 0 ) )
499 visual.SetDepthIndex( currentDepthIndex );
503 visualReplaced = true;
507 // If not set, set the name of the visual to the same name as the control's property.
508 // ( If the control has been type registered )
509 if( visual.GetName().empty() )
513 std::string visualName = self.GetPropertyName( index );
514 if( !visualName.empty() )
516 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Setting visual name for property %d to %s\n",
517 index, visualName.c_str() );
518 visual.SetName( visualName );
521 catch( Dali::DaliException e )
523 DALI_LOG_WARNING( "Attempting to register visual without a registered property, index: %d\n", index );
527 if( !visualReplaced ) // New registration entry
529 DALI_LOG_INFO( gLogFilter, Debug::Concise, "New Visual registration %d\n", index);
530 mVisuals.PushBack( new RegisteredVisual( index, visual, ( enabled == VisualState::ENABLED ? true : false ) ) );
532 // monitor when the visuals resources are ready
533 StartObservingVisual( visual );
535 // If we've not set the depth-index value, we have more than one visual and the visual does not have a depth index, then set it to be the highest
536 if( ( depthIndexValueSet == DepthIndexValue::NOT_SET ) &&
537 ( mVisuals.Size() > 1 ) &&
538 ( visual.GetDepthIndex() == 0 ) )
540 int maxDepthIndex = std::numeric_limits< int >::min();
542 RegisteredVisualContainer::ConstIterator iter;
543 const RegisteredVisualContainer::ConstIterator endIter = mVisuals.End();
544 for ( iter = mVisuals.Begin(); iter != endIter; iter++ )
546 const int visualDepthIndex = (*iter)->visual.GetDepthIndex();
547 if ( visualDepthIndex > maxDepthIndex )
549 maxDepthIndex = visualDepthIndex;
553 ++maxDepthIndex; // Add one to the current maximum depth index so that our added visual appears on top
554 visual.SetDepthIndex( maxDepthIndex );
560 // If the caller has set the depth-index, then set it here
561 if( depthIndexValueSet == DepthIndexValue::SET )
563 visual.SetDepthIndex( depthIndex );
566 // Put on stage if enabled and the control is already on the stage
567 if( ( enabled == VisualState::ENABLED ) && self.OnStage() )
569 // Visual must be set on stage for the renderer to be created and the ResourceReady triggered.
570 Toolkit::GetImplementation(visual).SetOnStage( self );
574 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual() Registered %s(%d), enabled:%s\n", visual.GetName().c_str(), index, enabled?"T":"F" );
577 void Control::Impl::UnregisterVisual( Property::Index index )
579 RegisteredVisualContainer::Iterator iter;
580 if ( FindVisual( index, mVisuals, iter ) )
582 // stop observing visual
583 StopObservingVisual( (*iter)->visual );
585 Actor self( mControlImpl.Self() );
586 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
587 (*iter)->visual.Reset();
588 mVisuals.Erase( iter );
592 Toolkit::Visual::Base Control::Impl::GetVisual( Property::Index index ) const
594 RegisteredVisualContainer::Iterator iter;
595 if ( FindVisual( index, mVisuals, iter ) )
597 return (*iter)->visual;
600 return Toolkit::Visual::Base();
603 void Control::Impl::EnableVisual( Property::Index index, bool enable )
605 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual (%d)\n", index);
607 RegisteredVisualContainer::Iterator iter;
608 if ( FindVisual( index, mVisuals, iter ) )
610 if ( (*iter)->enabled == enable )
612 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual %s(%d) already %s\n", (*iter)->visual.GetName().c_str(), index, enable?"enabled":"disabled");
616 (*iter)->enabled = enable;
617 Actor parentActor = mControlImpl.Self();
618 if ( mControlImpl.Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
622 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) on stage \n", (*iter)->visual.GetName().c_str(), index );
623 Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
627 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) off stage \n", (*iter)->visual.GetName().c_str(), index );
628 Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor ); // No need to call if control not staged.
634 bool Control::Impl::IsVisualEnabled( Property::Index index ) const
636 RegisteredVisualContainer::Iterator iter;
637 if ( FindVisual( index, mVisuals, iter ) )
639 return (*iter)->enabled;
644 void Control::Impl::StopObservingVisual( Toolkit::Visual::Base& visual )
646 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
648 // Stop observing the visual
649 visualImpl.RemoveResourceObserver( *this );
652 void Control::Impl::StartObservingVisual( Toolkit::Visual::Base& visual)
654 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
656 // start observing the visual for resource ready
657 visualImpl.AddResourceObserver( *this );
660 Dali::Animation Control::Impl::CreateTransition( const Toolkit::TransitionData& handle )
662 Dali::Animation transition;
663 const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
665 if( transitionData.Count() > 0 )
667 // Setup a Transition from TransitionData.
668 TransitionData::Iterator end = transitionData.End();
669 for( TransitionData::Iterator iter = transitionData.Begin() ;
670 iter != end; ++iter )
672 TransitionData::Animator* animator = (*iter);
674 Toolkit::Visual::Base visual = GetVisualByName( mVisuals, animator->objectName );
678 #if defined(DEBUG_ENABLED)
679 Dali::TypeInfo typeInfo;
680 ControlWrapper* controlWrapperImpl = dynamic_cast<ControlWrapper*>(&mControlImpl);
681 if( controlWrapperImpl )
683 typeInfo = controlWrapperImpl->GetTypeInfo();
686 DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Found %s visual for %s\n",
687 visual.GetName().c_str(), typeInfo?typeInfo.GetName().c_str():"Unknown" );
689 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
690 visualImpl.AnimateProperty( transition, *animator );
694 DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Could not find visual. Trying actors");
695 // Otherwise, try any actor children of control (Including the control)
696 Actor child = mControlImpl.Self().FindChildByName( animator->objectName );
699 Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
700 if( propertyIndex != Property::INVALID_INDEX )
702 if( animator->animate == false )
704 if( animator->targetValue.GetType() != Property::NONE )
706 child.SetProperty( propertyIndex, animator->targetValue );
709 else // animate the property
711 if( animator->initialValue.GetType() != Property::NONE )
713 child.SetProperty( propertyIndex, animator->initialValue );
718 transition = Dali::Animation::New( 0.1f );
721 transition.AnimateTo( Property( child, propertyIndex ),
722 animator->targetValue,
723 animator->alphaFunction,
724 TimePeriod( animator->timePeriodDelay,
725 animator->timePeriodDuration ) );
736 void Control::Impl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
738 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
742 Control& controlImpl( GetImplementation( control ) );
746 case Toolkit::Control::Property::STYLE_NAME:
748 controlImpl.SetStyleName( value.Get< std::string >() );
752 case Toolkit::DevelControl::Property::STATE:
754 bool withTransitions=true;
755 const Property::Value* valuePtr=&value;
756 Property::Map* map = value.GetMap();
759 Property::Value* value2 = map->Find("withTransitions");
762 withTransitions = value2->Get<bool>();
765 valuePtr = map->Find("state");
770 Toolkit::DevelControl::State state( controlImpl.mImpl->mState );
771 if( Scripting::GetEnumerationProperty< Toolkit::DevelControl::State >( *valuePtr, ControlStateTable, ControlStateTableCount, state ) )
773 controlImpl.mImpl->SetState( state, withTransitions );
779 case Toolkit::DevelControl::Property::SUB_STATE:
781 std::string subState;
782 if( value.Get( subState ) )
784 controlImpl.mImpl->SetSubState( subState );
789 case Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID:
792 if( value.Get( focusId ) )
794 controlImpl.mImpl->mLeftFocusableActorId = focusId;
799 case Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID:
802 if( value.Get( focusId ) )
804 controlImpl.mImpl->mRightFocusableActorId = focusId;
809 case Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID:
812 if( value.Get( focusId ) )
814 controlImpl.mImpl->mUpFocusableActorId = focusId;
819 case Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID:
822 if( value.Get( focusId ) )
824 controlImpl.mImpl->mDownFocusableActorId = focusId;
829 case Toolkit::Control::Property::BACKGROUND_COLOR:
831 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
832 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
836 case Toolkit::Control::Property::BACKGROUND_IMAGE:
838 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
839 Image image = Scripting::NewImage( value );
842 controlImpl.SetBackgroundImage( image );
846 // An empty image means the background is no longer required
847 controlImpl.ClearBackground();
852 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
854 if ( value.Get< bool >() )
856 controlImpl.SetKeyInputFocus();
860 controlImpl.ClearKeyInputFocus();
865 case Toolkit::Control::Property::BACKGROUND:
869 const Property::Map* map = value.GetMap();
870 if( map && !map->Empty() )
872 controlImpl.SetBackground( *map );
874 else if( value.Get( url ) )
876 // don't know the size to load
877 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, ImageDimensions() );
880 controlImpl.mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
883 else if( value.Get( color ) )
885 controlImpl.SetBackgroundColor(color);
889 // The background is an empty property map, so we should clear the background
890 controlImpl.ClearBackground();
895 case Toolkit::DevelControl::Property::TOOLTIP:
897 TooltipPtr& tooltipPtr = controlImpl.mImpl->mTooltip;
900 tooltipPtr = Tooltip::New( control );
902 tooltipPtr->SetProperties( value );
908 Property::Value Control::Impl::GetProperty( BaseObject* object, Property::Index index )
910 Property::Value value;
912 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
916 Control& controlImpl( GetImplementation( control ) );
920 case Toolkit::Control::Property::STYLE_NAME:
922 value = controlImpl.GetStyleName();
926 case Toolkit::DevelControl::Property::STATE:
928 value = controlImpl.mImpl->mState;
932 case Toolkit::DevelControl::Property::SUB_STATE:
934 value = controlImpl.mImpl->mSubStateName;
938 case Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID:
940 value = controlImpl.mImpl->mLeftFocusableActorId;
944 case Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID:
946 value = controlImpl.mImpl->mRightFocusableActorId;
950 case Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID:
952 value = controlImpl.mImpl->mUpFocusableActorId;
956 case Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID:
958 value = controlImpl.mImpl->mDownFocusableActorId;
962 case Toolkit::Control::Property::BACKGROUND_COLOR:
964 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
965 value = controlImpl.GetBackgroundColor();
969 case Toolkit::Control::Property::BACKGROUND_IMAGE:
971 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
973 Toolkit::Visual::Base visual = controlImpl.mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
976 visual.CreatePropertyMap( map );
982 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
984 value = controlImpl.HasKeyInputFocus();
988 case Toolkit::Control::Property::BACKGROUND:
991 Toolkit::Visual::Base visual = controlImpl.mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
994 visual.CreatePropertyMap( map );
1001 case Toolkit::DevelControl::Property::TOOLTIP:
1004 if( controlImpl.mImpl->mTooltip )
1006 controlImpl.mImpl->mTooltip->CreatePropertyMap( map );
1019 void Control::Impl::CopyInstancedProperties( RegisteredVisualContainer& visuals, Dictionary<Property::Map>& instancedProperties )
1021 for(RegisteredVisualContainer::Iterator iter = visuals.Begin(); iter!= visuals.End(); iter++)
1023 if( (*iter)->visual )
1025 Property::Map instanceMap;
1026 Toolkit::GetImplementation((*iter)->visual).CreateInstancePropertyMap(instanceMap);
1027 instancedProperties.Add( (*iter)->visual.GetName(), instanceMap );
1033 void Control::Impl::RemoveVisual( RegisteredVisualContainer& visuals, const std::string& visualName )
1035 Actor self( mControlImpl.Self() );
1037 for ( RegisteredVisualContainer::Iterator visualIter = visuals.Begin();
1038 visualIter != visuals.End(); ++visualIter )
1040 Toolkit::Visual::Base visual = (*visualIter)->visual;
1041 if( visual && visual.GetName() == visualName )
1043 Toolkit::GetImplementation(visual).SetOffStage( self );
1044 (*visualIter)->visual.Reset();
1045 visuals.Erase( visualIter );
1051 void Control::Impl::RemoveVisuals( RegisteredVisualContainer& visuals, DictionaryKeys& removeVisuals )
1053 Actor self( mControlImpl.Self() );
1054 for( DictionaryKeys::iterator iter = removeVisuals.begin(); iter != removeVisuals.end(); ++iter )
1056 const std::string visualName = *iter;
1057 RemoveVisual( visuals, visualName );
1061 void Control::Impl::RecreateChangedVisuals( Dictionary<Property::Map>& stateVisualsToChange,
1062 Dictionary<Property::Map>& instancedProperties )
1064 Dali::CustomActor handle( mControlImpl.GetOwner() );
1065 for( Dictionary<Property::Map>::iterator iter = stateVisualsToChange.Begin();
1066 iter != stateVisualsToChange.End(); ++iter )
1068 const std::string& visualName = (*iter).key;
1069 const Property::Map& toMap = (*iter).entry;
1071 // is it a candidate for re-creation?
1072 bool recreate = false;
1074 Toolkit::Visual::Base visual = GetVisualByName( mVisuals, visualName );
1077 Property::Map fromMap;
1078 visual.CreatePropertyMap( fromMap );
1080 Toolkit::DevelVisual::Type fromType = GetVisualTypeFromMap( fromMap );
1081 Toolkit::DevelVisual::Type toType = GetVisualTypeFromMap( toMap );
1083 if( fromType != toType )
1089 if( fromType == Toolkit::DevelVisual::IMAGE || fromType == Toolkit::DevelVisual::N_PATCH
1090 || fromType == Toolkit::DevelVisual::SVG || fromType == Toolkit::DevelVisual::ANIMATED_IMAGE )
1092 Property::Value* fromUrl = fromMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
1093 Property::Value* toUrl = toMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
1095 if( fromUrl && toUrl )
1097 std::string fromUrlString;
1098 std::string toUrlString;
1099 fromUrl->Get(fromUrlString);
1100 toUrl->Get(toUrlString);
1102 if( fromUrlString != toUrlString )
1110 const Property::Map* instancedMap = instancedProperties.FindConst( visualName );
1111 if( recreate || instancedMap )
1113 RemoveVisual( mVisuals, visualName );
1114 Style::ApplyVisual( handle, visualName, toMap, instancedMap );
1118 // @todo check to see if we can apply toMap without recreating the visual
1119 // e.g. by setting only animatable properties
1120 // For now, recreate all visuals, but merge in instance data.
1121 RemoveVisual( mVisuals, visualName );
1122 Style::ApplyVisual( handle, visualName, toMap, instancedMap );
1128 void Control::Impl::ReplaceStateVisualsAndProperties( const StylePtr oldState, const StylePtr newState, const std::string& subState )
1130 // Collect all old visual names
1131 DictionaryKeys stateVisualsToRemove;
1134 oldState->visuals.GetKeys( stateVisualsToRemove );
1135 if( ! subState.empty() )
1137 const StylePtr* oldSubState = oldState->subStates.FindConst(subState);
1140 DictionaryKeys subStateVisualsToRemove;
1141 (*oldSubState)->visuals.GetKeys( subStateVisualsToRemove );
1142 Merge( stateVisualsToRemove, subStateVisualsToRemove );
1147 // Collect all new visual properties
1148 Dictionary<Property::Map> stateVisualsToAdd;
1151 stateVisualsToAdd = newState->visuals;
1152 if( ! subState.empty() )
1154 const StylePtr* newSubState = newState->subStates.FindConst(subState);
1157 stateVisualsToAdd.Merge( (*newSubState)->visuals );
1162 // If a name is in both add/remove, move it to change list.
1163 Dictionary<Property::Map> stateVisualsToChange;
1164 FindChangableVisuals( stateVisualsToAdd, stateVisualsToChange, stateVisualsToRemove);
1166 // Copy instanced properties (e.g. text label) of current visuals
1167 Dictionary<Property::Map> instancedProperties;
1168 CopyInstancedProperties( mVisuals, instancedProperties );
1170 // For each visual in remove list, remove from mVisuals
1171 RemoveVisuals( mVisuals, stateVisualsToRemove );
1173 // For each visual in add list, create and add to mVisuals
1174 Dali::CustomActor handle( mControlImpl.GetOwner() );
1175 Style::ApplyVisuals( handle, stateVisualsToAdd, instancedProperties );
1177 // For each visual in change list, if it requires a new visual,
1178 // remove old visual, create and add to mVisuals
1179 RecreateChangedVisuals( stateVisualsToChange, instancedProperties );
1182 void Control::Impl::SetState( DevelControl::State newState, bool withTransitions )
1184 DevelControl::State oldState = mState;
1185 Dali::CustomActor handle( mControlImpl.GetOwner() );
1186 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Control::Impl::SetState: %s\n",
1187 (mState == DevelControl::NORMAL ? "NORMAL" :(
1188 mState == DevelControl::FOCUSED ?"FOCUSED" : (
1189 mState == DevelControl::DISABLED?"DISABLED":"NONE" ))));
1191 if( mState != newState )
1193 // If mState was Disabled, and new state is Focused, should probably
1194 // store that fact, e.g. in another property that FocusManager can access.
1197 // Trigger state change and transitions
1198 // Apply new style, if stylemanager is available
1199 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1202 const StylePtr stylePtr = GetImpl( styleManager ).GetRecordedStyle( Toolkit::Control( mControlImpl.GetOwner() ) );
1206 std::string oldStateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( oldState, ControlStateTable, ControlStateTableCount );
1207 std::string newStateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( newState, ControlStateTable, ControlStateTableCount );
1209 const StylePtr* newStateStyle = stylePtr->subStates.Find( newStateName );
1210 const StylePtr* oldStateStyle = stylePtr->subStates.Find( oldStateName );
1211 if( oldStateStyle && newStateStyle )
1213 // Only change if both state styles exist
1214 ReplaceStateVisualsAndProperties( *oldStateStyle, *newStateStyle, mSubStateName );
1221 void Control::Impl::SetSubState( const std::string& subStateName, bool withTransitions )
1223 if( mSubStateName != subStateName )
1225 // Get existing sub-state visuals, and unregister them
1226 Dali::CustomActor handle( mControlImpl.GetOwner() );
1228 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1231 const StylePtr stylePtr = GetImpl( styleManager ).GetRecordedStyle( Toolkit::Control( mControlImpl.GetOwner() ) );
1235 std::string stateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( mState, ControlStateTable, ControlStateTableCount );
1237 const StylePtr* state = stylePtr->subStates.Find( stateName );
1240 StylePtr stateStyle(*state);
1242 const StylePtr* newStateStyle = stateStyle->subStates.Find( subStateName );
1243 const StylePtr* oldStateStyle = stateStyle->subStates.Find( mSubStateName );
1244 if( oldStateStyle && newStateStyle )
1247 ReplaceStateVisualsAndProperties( *oldStateStyle, *newStateStyle, empty );
1253 mSubStateName = subStateName;
1257 } // namespace Internal
1259 } // namespace Toolkit