2 * Copyright (c) 2019 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 "toggle-button-impl.h"
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/public-api/object/type-registry-helper.h>
24 #include <dali/public-api/images/resource-image.h>
25 #include <dali/devel-api/scripting/scripting.h>
26 #include <dali/public-api/object/property-array.h>
27 #include <dali/integration-api/debug.h>
30 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
31 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
32 #include <dali-toolkit/devel-api/controls/control-devel.h>
33 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
34 #include <dali-toolkit/public-api/visuals/visual-properties.h>
35 #include <dali-toolkit/public-api/align-enumerations.h>
36 #include <dali-toolkit/devel-api/controls/tooltip/tooltip-properties.h>
37 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
38 #include <dali-toolkit/devel-api/controls/control-devel.h>
40 #if defined(DEBUG_ENABLED)
41 extern Debug::Filter* gLogButtonFilter;
58 return Toolkit::ToggleButton::New();
62 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ToggleButton, Toolkit::Button, Create )
64 DALI_PROPERTY_REGISTRATION( Toolkit, ToggleButton, "stateVisuals", ARRAY, STATE_VISUALS )
65 DALI_PROPERTY_REGISTRATION( Toolkit, ToggleButton, "tooltips", ARRAY, TOOLTIPS )
66 DALI_PROPERTY_REGISTRATION( Toolkit, ToggleButton, "currentStateIndex", INTEGER, CURRENT_STATE_INDEX )
68 DALI_TYPE_REGISTRATION_END()
70 } // unnamed namespace
72 Dali::Toolkit::ToggleButton ToggleButton::New()
74 DALI_LOG_INFO( gLogButtonFilter, Debug::General, "ToggleButton::New\n" );
75 // Create the implementation, temporarily owned on stack
76 IntrusivePtr< ToggleButton > internalToggleButton = new ToggleButton();
78 // Pass ownership to CustomActor
79 Dali::Toolkit::ToggleButton toggleButton( *internalToggleButton );
81 // Second-phase init of the implementation
82 // This can only be done after the CustomActor connection has been made...
83 internalToggleButton->Initialize();
88 ToggleButton::ToggleButton()
92 mToggleSelectedVisuals(),
93 mToggleDisabledVisuals(),
94 mToggleDisabledSelectedVisuals(),
96 mCurrentToggleIndex(0)
98 DALI_LOG_INFO( gLogButtonFilter, Debug::General, "ToggleButton::Constructor\n" );
99 SetTogglableButton( false );
102 ToggleButton::~ToggleButton()
106 void ToggleButton::OnInitialize()
108 DALI_LOG_INFO( gLogButtonFilter, Debug::General, "ToggleButton::OnInitialize\n" );
109 Button::OnInitialize();
111 // Toggle button requires the Leave event.
113 self.SetLeaveRequired( true );
116 void ToggleButton::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
118 Toolkit::ToggleButton toggleButton = Toolkit::ToggleButton::DownCast( Dali::BaseHandle( object ) );
120 DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "ToggleButton::SetProperty index[%d]\n", propertyIndex );
124 ToggleButton& toggleButtonImpl( GetImplementation( toggleButton ) );
126 switch ( propertyIndex )
128 case Toolkit::ToggleButton::Property::STATE_VISUALS:
130 Property::Array stateArray;
131 if( value.Get( stateArray ) )
133 toggleButtonImpl.SetToggleStates( stateArray );
138 case Toolkit::ToggleButton::Property::TOOLTIPS:
140 Property::Array* tipArray = value.GetArray();
143 std::vector<std::string> tips;
144 size_t tipsCount = tipArray->Count();
145 tips.resize( tipsCount );
146 for( size_t i = 0; i != tipsCount; ++i )
148 tipArray->GetElementAt( i ).Get( tips[i] );
150 toggleButtonImpl.SetToggleTooltips(tips);
162 Property::Value ToggleButton::GetProperty( BaseObject* object, Property::Index propertyIndex )
164 Property::Value value;
166 Toolkit::ToggleButton toggleButton = Toolkit::ToggleButton::DownCast( Dali::BaseHandle( object ) );
168 DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "ToggleButton::GetProperty index[%d]\n", propertyIndex );
172 ToggleButton& toggleButtonImpl( GetImplementation( toggleButton ) );
174 switch ( propertyIndex )
176 case Toolkit::ToggleButton::Property::STATE_VISUALS:
178 Property::Array array = toggleButtonImpl.GetToggleStates();
179 value = Property::Value( array );
182 case Toolkit::ToggleButton::Property::TOOLTIPS:
184 Property::Value value1( Property::ARRAY );
185 Property::Array* tipArray = value1.GetArray();
189 std::vector<std::string> tips = toggleButtonImpl.GetToggleTooltips();
190 size_t tipsCount( tips.size() );
191 for( size_t i( 0 ); i != tipsCount; ++i )
193 tipArray->PushBack( tips[i] );
199 case Toolkit::ToggleButton::Property::CURRENT_STATE_INDEX:
201 value = static_cast<int>(toggleButtonImpl.mCurrentToggleIndex);
210 void ToggleButton::CreateVisualsForAllStates( const Property::Array& states, std::vector<Toolkit::Visual::Base>& visuals )
212 DALI_LOG_INFO( gLogButtonFilter, Debug::General, "ToggleButton::CreateVisualsForAllStates\n" );
216 for ( unsigned int i = 0; i < states.Count(); i++ )
218 Property::Value value( states[i] );
220 Toolkit::VisualFactory visualFactory = Toolkit::VisualFactory::Get();
221 Toolkit::Visual::Base stateVisual;
223 if ( value.GetType() == Property::MAP )
225 Property::Map *map = value.GetMap();
226 if( map && !map->Empty() ) // Empty map results in current visual removal.
228 DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "ToggleButton::CreateVisuals Using Map\n" );
229 stateVisual = visualFactory.CreateVisual( *map );
232 else if ( value.GetType() == Property::STRING )
234 std::string imageUrl = value.Get<std::string>();
235 DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "ToggleButton::CreateVisuals Using image URL\n" );
236 if ( !imageUrl.empty() )
238 stateVisual = visualFactory.CreateVisual( imageUrl, ImageDimensions() );
244 stateVisual.SetDepthIndex( DepthIndex::CONTENT );
245 visuals.push_back( stateVisual );
248 DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "ToggleButton::CreateVisuals mToggleVisuals:%d\n", mToggleVisuals.size() );
251 void ToggleButton::SetToggleStates( const Property::Array& states )
252 { //this should really be generalized to be either string or maps so that any visual can be created.
253 DALI_LOG_INFO( gLogButtonFilter, Debug::General, "ToggleButton::SetToggleStates\n" );
254 if ( !states.Empty() )
256 mToggleStates.Clear();
257 mToggleStates = states;
258 /* New toggle button index from 0. */
259 mCurrentToggleIndex = 0;
261 // Create all visuals, save to mToggleVisuals.
262 CreateVisualsForAllStates( states, mToggleVisuals );
263 CreateVisualsForAllStates( states, mToggleSelectedVisuals );
264 CreateVisualsForAllStates( states, mToggleDisabledVisuals );
265 CreateVisualsForAllStates( states, mToggleDisabledSelectedVisuals );
267 DALI_LOG_INFO( gLogButtonFilter, Debug::General, "ToggleButton::Began to register visual.\n" );
269 PrepareVisual( Toolkit::Button::Property::UNSELECTED_VISUAL, mToggleVisuals[mCurrentToggleIndex] );
270 PrepareVisual( Toolkit::Button::Property::SELECTED_VISUAL, mToggleSelectedVisuals[mCurrentToggleIndex] );
271 PrepareVisual( Toolkit::Button::Property::DISABLED_UNSELECTED_VISUAL, mToggleDisabledVisuals[mCurrentToggleIndex] );
272 PrepareVisual( Toolkit::Button::Property::DISABLED_SELECTED_VISUAL, mToggleDisabledSelectedVisuals[mCurrentToggleIndex] );
278 Property::Array ToggleButton::GetToggleStates() const
280 return mToggleStates;
283 void ToggleButton::SetToggleTooltips( std::vector<std::string>& tips )
285 DALI_LOG_INFO( gLogButtonFilter, Debug::General, "ToggleButton::SetToggleTooltips\n" );
288 mToggleTooltips.clear();
289 mToggleTooltips.swap( tips );
292 if ( !mToggleTooltips.empty() && ( mCurrentToggleIndex < mToggleTooltips.size() ) )
294 Self().SetProperty( Toolkit::DevelControl::Property::TOOLTIP, mToggleTooltips[mCurrentToggleIndex] );
300 const std::vector<std::string>& ToggleButton::GetToggleTooltips() const
302 return mToggleTooltips;
305 void ToggleButton::PrepareVisual(Property::Index index, Toolkit::Visual::Base& visual)
307 bool enabled = false; // Disabled by default
309 // Unregister the visual with the given index if registered previously
310 if( DevelControl::GetVisual( *this, index ) )
312 // Check whether it was enabled to ensure we do the same with the new visual we're registering
313 enabled = DevelControl::IsVisualEnabled( *this, index );
314 DevelControl::UnregisterVisual( *this, index );
317 DevelControl::RegisterVisual( *this, index, visual, enabled );
320 void ToggleButton::RelayoutVisual( Property::Index index, const Vector2& size )
322 Toolkit::Visual::Base visual = DevelControl::GetVisual( *this, index );
325 Size visualSize = Size::ZERO;
326 Vector2 visualPosition = Vector2::ZERO;
328 visual.GetNaturalSize( visualSize );
330 DALI_LOG_INFO( gLogButtonFilter, Debug::General, "ToggleButton::OnRelayout Setting visual size to(%f,%f)\n", visualSize.width, visualSize.height );
331 DALI_LOG_INFO( gLogButtonFilter, Debug::General, "ToggleButton::OnRelayout Setting visual position to(%f,%f)\n", visualPosition.x, visualPosition.y );
333 Property::Map visualTransform;
334 visualTransform.Add( Toolkit::Visual::Transform::Property::SIZE, visualSize )
335 .Add( Toolkit::Visual::Transform::Property::OFFSET, visualPosition )
336 .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
337 .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
338 .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::CENTER )
339 .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER );
341 visual.SetTransformAndSize( visualTransform, size );
345 void ToggleButton::OnRelayout( const Vector2& size, RelayoutContainer& container )
347 DALI_LOG_INFO( gLogButtonFilter, Debug::General, "ToggleButton::OnRelayout targetSize(%f,%f) ptr(%p)\n", size.width, size.height, this );
349 RelayoutVisual( Toolkit::Button::Property::UNSELECTED_VISUAL, size );
350 RelayoutVisual( Toolkit::Button::Property::SELECTED_VISUAL, size );
351 RelayoutVisual( Toolkit::Button::Property::DISABLED_UNSELECTED_VISUAL, size );
352 RelayoutVisual( Toolkit::Button::Property::DISABLED_SELECTED_VISUAL, size );
355 void ToggleButton::OnPressed()
357 DALI_LOG_INFO( gLogButtonFilter, Debug::General, "ToggleButton::OnPressed\n" );
358 // State index will add 1 only when button is pressed.
359 mCurrentToggleIndex = ( mCurrentToggleIndex + 1 ) % mToggleVisuals.size();
361 // Both create SelectedVisual and UnselectedVisual
362 PrepareVisual( Toolkit::Button::Property::UNSELECTED_VISUAL, mToggleVisuals[mCurrentToggleIndex] );
363 PrepareVisual( Toolkit::Button::Property::SELECTED_VISUAL, mToggleSelectedVisuals[mCurrentToggleIndex] );
364 PrepareVisual( Toolkit::Button::Property::DISABLED_UNSELECTED_VISUAL, mToggleDisabledVisuals[mCurrentToggleIndex] );
365 PrepareVisual( Toolkit::Button::Property::DISABLED_SELECTED_VISUAL, mToggleDisabledSelectedVisuals[mCurrentToggleIndex] );
367 //Need to check mCurrentToggleIndex, it must less than the size of mToggleTooltips.
368 if ( !mToggleTooltips.empty() && ( mCurrentToggleIndex < mToggleTooltips.size() ) )
370 Self().SetProperty( Toolkit::DevelControl::Property::TOOLTIP, mToggleTooltips[mCurrentToggleIndex] );
376 } // namespace Internal
378 } // namespace Toolkit