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 <dali-toolkit/internal/controls/slider/slider-impl.h>
22 #include <dali/public-api/events/touch-event.h>
23 #include <dali/public-api/object/type-registry.h>
26 #include <dali-toolkit/public-api/controls/control-impl.h>
27 #include <dali/public-api/images/resource-image.h>
40 const Property::Index Slider::LOWER_BOUND_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX;
41 const Property::Index Slider::UPPER_BOUND_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 1;
43 const Property::Index Slider::VALUE_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 2;
44 const Property::Index Slider::HIT_REGION_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 3;
45 const Property::Index Slider::BACKING_REGION_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 4;
46 const Property::Index Slider::HANDLE_REGION_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 5;
48 const Property::Index Slider::BACKING_IMAGE_NAME_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 6;
49 const Property::Index Slider::HANDLE_IMAGE_NAME_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 7;
50 const Property::Index Slider::PROGRESS_IMAGE_NAME_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 8;
51 const Property::Index Slider::POPUP_IMAGE_NAME_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 9;
52 const Property::Index Slider::POPUP_ARROW_IMAGE_NAME_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 10;
54 const Property::Index Slider::DISABLE_COLOR_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 11;
55 const Property::Index Slider::POPUP_TEXT_COLOR_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 12;
57 const Property::Index Slider::VALUE_PRECISION_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 13;
59 const Property::Index Slider::SHOW_POPUP_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 14;
60 const Property::Index Slider::SHOW_VALUE_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 15;
62 const Property::Index Slider::ENABLED_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 16;
64 const Property::Index Slider::MARKS_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 17;
65 const Property::Index Slider::SNAP_TO_MARKS_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 18;
66 const Property::Index Slider::MARK_TOLERANCE_PROPERTY = Internal::Slider::SLIDER_PROPERTY_START_INDEX + 19;
73 const float BACKING_Z = -0.1f;
74 const float PROGRESS_Z = 0.1f;
75 const float HANDLE_Z = 1.0f;
76 const float VALUE_TEXT_INCREMENT = 0.01f;
77 const float HANDLE_VALUE_DISPLAY_TEXT_Z = HANDLE_Z + VALUE_TEXT_INCREMENT;
78 const float VALUE_DISPLAY_TEXT_Z = VALUE_TEXT_INCREMENT + VALUE_TEXT_INCREMENT; // Put above HANDLE_VALUE_DISPLAY_TEXT_Z (parented to handle)
80 const float MARK_SNAP_TOLERANCE = 0.05f; // 5% of slider width
82 const int VALUE_VIEW_SHOW_DURATION = 1000; // millisec
83 const int VALUE_VIEW_SHOW_DURATION_LONG = 2000; // millisec
85 const float VALUE_VERTICAL_OFFSET = 48.0f;
87 const float DEFAULT_WIDTH = 0.0f;
88 const float DEFAULT_HEIGHT = 27.0f;
89 const float DEFAULT_HIT_HEIGHT = 72.0f;
90 const float DEFAULT_HANDLE_HEIGHT = DEFAULT_HIT_HEIGHT;
92 const char* SKINNED_BACKING_IMAGE_NAME = DALI_IMAGE_DIR "slider-skin.9.png";
93 const char* SKINNED_HANDLE_IMAGE_NAME = DALI_IMAGE_DIR "slider-skin-handle.png";;
94 const char* SKINNED_PROGRESS_IMAGE_NAME = DALI_IMAGE_DIR "slider-skin-progress.9.png";
95 const char* SKINNED_POPUP_IMAGE_NAME = DALI_IMAGE_DIR "slider-popup.9.png";
96 const char* SKINNED_POPUP_ARROW_IMAGE_NAME = DALI_IMAGE_DIR "slider-popup-arrow.png";
98 const Vector2 DEFAULT_HIT_REGION( DEFAULT_WIDTH, DEFAULT_HIT_HEIGHT );
99 const Vector2 DEFAULT_BACKING_REGION( DEFAULT_WIDTH, DEFAULT_HEIGHT );
100 const Vector2 DEFAULT_HANDLE_REGION( DEFAULT_HANDLE_HEIGHT, DEFAULT_HANDLE_HEIGHT );
102 const Vector4 DEFAULT_DISABLE_COLOR( 0.5f, 0.5f, 0.5f, 1.0f );
103 const Vector4 DEFAULT_POPUP_TEXT_COLOR( 0.5f, 0.5f, 0.5f, 1.0f );
105 const float VALUE_POPUP_MARGIN = 10.0f;
106 const float VALUE_POPUP_HEIGHT = 81.0f;
107 const float VALUE_POPUP_MIN_WIDTH = 54.0f;
108 const Vector2 VALUE_POPUP_ARROW_SIZE( 18.0f, 18.0f );
110 const float DEFAULT_LOWER_BOUND = 0.0f;
111 const float DEFAULT_UPPER_BOUND = 1.0f;
112 const float DEFAULT_VALUE = 0.0f;
113 const int DEFAULT_VALUE_PRECISION = 0;
114 const bool DEFAULT_SHOW_POPUP = false;
115 const bool DEFAULT_SHOW_VALUE = true;
116 const bool DEFAULT_ENABLED = true;
117 const bool DEFAULT_SNAP_TO_MARKS = false;
121 return Dali::Toolkit::Slider::New();
124 TypeRegistration typeRegistration( typeid(Dali::Toolkit::Slider), typeid(Dali::Toolkit::Control), Create );
126 SignalConnectorType signalConnector1( typeRegistration, Toolkit::Slider::SIGNAL_VALUE_CHANGED, &Toolkit::Internal::Slider::DoConnectSignal );
127 SignalConnectorType signalConnector2( typeRegistration, Toolkit::Slider::SIGNAL_MARK, &Toolkit::Internal::Slider::DoConnectSignal );
129 PropertyRegistration property1( typeRegistration, "lower-bound", Toolkit::Slider::LOWER_BOUND_PROPERTY, Property::FLOAT, &Slider::SetProperty, &Slider::GetProperty );
130 PropertyRegistration property2( typeRegistration, "upper-bound", Toolkit::Slider::UPPER_BOUND_PROPERTY, Property::FLOAT, &Slider::SetProperty, &Slider::GetProperty );
131 PropertyRegistration property3( typeRegistration, "value", Toolkit::Slider::VALUE_PROPERTY, Property::FLOAT, &Slider::SetProperty, &Slider::GetProperty );
133 PropertyRegistration property4( typeRegistration, "hit-region", Toolkit::Slider::HIT_REGION_PROPERTY, Property::VECTOR2, &Slider::SetProperty, &Slider::GetProperty );
134 PropertyRegistration property5( typeRegistration, "backing-region", Toolkit::Slider::BACKING_REGION_PROPERTY, Property::VECTOR2, &Slider::SetProperty, &Slider::GetProperty );
135 PropertyRegistration property6( typeRegistration, "handle-region", Toolkit::Slider::HANDLE_REGION_PROPERTY, Property::VECTOR2, &Slider::SetProperty, &Slider::GetProperty );
137 PropertyRegistration property7( typeRegistration, "backing-image-name", Toolkit::Slider::BACKING_IMAGE_NAME_PROPERTY, Property::STRING, &Slider::SetProperty, &Slider::GetProperty );
138 PropertyRegistration property8( typeRegistration, "handle-image-name", Toolkit::Slider::HANDLE_IMAGE_NAME_PROPERTY, Property::STRING, &Slider::SetProperty, &Slider::GetProperty );
139 PropertyRegistration property9( typeRegistration, "progress-image-name", Toolkit::Slider::PROGRESS_IMAGE_NAME_PROPERTY, Property::STRING, &Slider::SetProperty, &Slider::GetProperty );
140 PropertyRegistration property10( typeRegistration, "popup-image-name", Toolkit::Slider::POPUP_IMAGE_NAME_PROPERTY, Property::STRING, &Slider::SetProperty, &Slider::GetProperty );
141 PropertyRegistration property11( typeRegistration, "popup-arrow-image-name", Toolkit::Slider::POPUP_ARROW_IMAGE_NAME_PROPERTY, Property::STRING, &Slider::SetProperty, &Slider::GetProperty );
143 PropertyRegistration property12( typeRegistration, "disable-color", Toolkit::Slider::DISABLE_COLOR_PROPERTY, Property::VECTOR4, &Slider::SetProperty, &Slider::GetProperty );
144 PropertyRegistration property13( typeRegistration, "popup-text-color", Toolkit::Slider::POPUP_TEXT_COLOR_PROPERTY, Property::VECTOR4, &Slider::SetProperty, &Slider::GetProperty );
146 PropertyRegistration property14( typeRegistration, "value-precision", Toolkit::Slider::VALUE_PRECISION_PROPERTY, Property::INTEGER, &Slider::SetProperty, &Slider::GetProperty );
148 PropertyRegistration property15( typeRegistration, "show-popup", Toolkit::Slider::SHOW_POPUP_PROPERTY, Property::BOOLEAN, &Slider::SetProperty, &Slider::GetProperty );
149 PropertyRegistration property16( typeRegistration, "show-value", Toolkit::Slider::SHOW_VALUE_PROPERTY, Property::BOOLEAN, &Slider::SetProperty, &Slider::GetProperty );
151 PropertyRegistration property17( typeRegistration, "enabled", Toolkit::Slider::ENABLED_PROPERTY, Property::BOOLEAN, &Slider::SetProperty, &Slider::GetProperty );
153 PropertyRegistration property18( typeRegistration, "marks", Toolkit::Slider::MARKS_PROPERTY, Property::ARRAY, &Slider::SetProperty, &Slider::GetProperty );
154 PropertyRegistration property19( typeRegistration, "snap-to-marks", Toolkit::Slider::SNAP_TO_MARKS_PROPERTY, Property::BOOLEAN, &Slider::SetProperty, &Slider::GetProperty );
155 PropertyRegistration property20( typeRegistration, "mark-tolerance", Toolkit::Slider::MARK_TOLERANCE_PROPERTY, Property::FLOAT, &Slider::SetProperty, &Slider::GetProperty );
159 ///////////////////////////////////////////////////////////////////////////////////////////////////
161 ///////////////////////////////////////////////////////////////////////////////////////////////////
163 Dali::Toolkit::Slider Slider::New()
165 // Create the implementation
166 SliderPtr slider( new Slider() );
168 // Pass ownership to CustomActor via derived handle
169 Dali::Toolkit::Slider handle( *slider );
171 // Second-phase init of the implementation
172 // This can only be done after the CustomActor connection has been made...
173 slider->Initialize();
179 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
181 mDisableColor( 0.0f, 0.0f, 0.0f, 0.0f ),
182 mPopupTextColor( 0.0f, 0.0f, 0.0f, 0.0f ),
183 mHitRegion( 0.0f, 0.0f ),
184 mBackingRegion( 0.0f, 0.0f ),
185 mHandleRegionSize( 0.0f, 0.0f ),
189 mMarkTolerance( 0.0f ),
190 mValuePrecision( 0 ),
193 mSnapToMarks( false )
201 void Slider::OnInitialize()
209 SetHitRegion( DEFAULT_HIT_REGION );
210 SetBackingRegion( DEFAULT_BACKING_REGION );
211 SetHandleRegion( DEFAULT_HANDLE_REGION );
213 SetBackingImageName( SKINNED_BACKING_IMAGE_NAME );
214 SetHandleImageName( SKINNED_HANDLE_IMAGE_NAME );
215 SetProgressImageName( SKINNED_PROGRESS_IMAGE_NAME );
216 SetPopupImageName( SKINNED_POPUP_IMAGE_NAME );
217 SetPopupArrowImageName( SKINNED_POPUP_ARROW_IMAGE_NAME );
219 SetPopupTextColor( DEFAULT_POPUP_TEXT_COLOR );
221 SetShowPopup( DEFAULT_SHOW_POPUP );
222 SetShowValue( DEFAULT_SHOW_VALUE );
224 SetEnabled( DEFAULT_ENABLED );
225 SetDisableColor( DEFAULT_DISABLE_COLOR );
227 SetSnapToMarks( DEFAULT_SNAP_TO_MARKS );
228 SetMarkTolerance( MARK_SNAP_TOLERANCE );
230 SetLowerBound( DEFAULT_LOWER_BOUND );
231 SetUpperBound( DEFAULT_UPPER_BOUND );
233 SetValuePrecision( DEFAULT_VALUE_PRECISION );
234 mValue = DEFAULT_VALUE;
235 DisplayValue( mValue, false ); // Run this last to display the correct value
237 // Size the Slider actor to a default
238 self.SetSize( DEFAULT_HIT_REGION.x, DEFAULT_HIT_REGION.y );
241 void Slider::OnControlSizeSet( const Vector3& size )
243 // Factor in handle overshoot into size of backing
244 SetHitRegion( Vector2( size.x, GetHitRegion().y ) );
245 SetBackingRegion( Vector2( size.x - GetHandleRegion().x, GetBackingRegion().y ) );
248 bool Slider::OnTouchEvent(Actor actor, const TouchEvent& event)
250 if( mState != DISABLED )
252 TouchPoint::State touchState = event.GetPoint(0).state;
254 if( touchState == TouchPoint::Down )
258 float percentage = MapPercentage( event.GetPoint(0).local );
259 float value = MapBounds( ( GetSnapToMarks() ) ? SnapToMark( percentage ) : MarkFilter( percentage ), GetLowerBound(), GetUpperBound() );
261 DisplayPopup( value );
263 else if( touchState == TouchPoint::Up)
265 if( mState == PRESSED )
268 mSlidingFinishedSignal.Emit( Toolkit::Slider::DownCast( Self() ), GetValue() );
276 void Slider::OnPan( Actor actor, const PanGesture& gesture )
278 // gesture.position is in local actor coordinates
279 if( mState != DISABLED )
281 switch( gesture.state )
283 case Gesture::Continuing:
285 if( mState == PRESSED )
287 float value = MapBounds( MarkFilter ( MapPercentage( gesture.position ) ), GetLowerBound(), GetUpperBound() );
289 DisplayPopup( value );
293 case Gesture::Finished:
295 if( mState == PRESSED )
297 if( GetSnapToMarks() )
299 float value = MapBounds( SnapToMark( MapPercentage( gesture.position ) ), GetLowerBound(), GetUpperBound() );
301 DisplayPopup( value );
303 mSlidingFinishedSignal.Emit( Toolkit::Slider::DownCast( Self() ), GetValue() );
317 float Slider::HitSpaceToDomain( float x )
319 float halfRegionWidth = GetHitRegion().x * 0.5f;
320 float halfDomainWidth = ( mDomain.to.x - mDomain.from.x ) * 0.5f;
321 float endDiff = halfRegionWidth - halfDomainWidth;
326 float Slider::MapPercentage( const Vector2& point )
328 return Clamp( ( HitSpaceToDomain( point.x ) - mDomain.from.x ) / ( mDomain.to.x - mDomain.from.x ), 0.0f, 1.0f );
331 float Slider::MapValuePercentage( float value )
333 return ( value - GetLowerBound() ) / ( GetUpperBound() - GetLowerBound() );
336 float Slider::MapBounds( float percent, float lowerBound, float upperBound )
338 return lowerBound + percent * ( upperBound - lowerBound );
341 Slider::Domain Slider::CalcDomain( const Vector2& currentSize )
343 return Domain( Vector2( 0.0f, 0.0f ), currentSize );
346 void Slider::DisplayValue( float value, bool raiseSignals )
348 float clampledValue = Clamp( value, GetLowerBound(), GetUpperBound() );
350 float percent = MapValuePercentage( clampledValue );
352 float x = mDomain.from.x + percent * ( mDomain.to.x - mDomain.from.x );
354 mHandle.SetPosition( x, 0.0f, HANDLE_Z );
359 if( clampledValue > 0.0f )
361 mProgress.SetVisible( true ); // Deliberately set this in case multiple SetValues are fired at once
362 mProgress.SetSize( x, GetBackingRegion().y );
366 mProgress.SetVisible( false );
373 Toolkit::Slider self = Toolkit::Slider::DownCast( Self() );
374 mValueChangedSignal.Emit( self, clampledValue );
377 if( MarkReached( percent, markIndex ) )
379 mMarkSignal.Emit( self, markIndex );
383 if( mHandleValueTextView )
385 std::stringstream ss;
386 ss.precision( GetValuePrecision() );
387 ss << std::fixed << clampledValue;
388 mHandleValueTextView.SetText( ss.str() );
392 void Slider::SetMarks( const MarkList& marks )
395 for( MarkList::const_iterator it = marks.begin(), itEnd = marks.end(); it != itEnd; ++it )
397 const Property::Value& propertyValue = *it;
398 propertyValue.Get( value );
400 mMarks.push_back( value );
404 const Slider::MarkList& Slider::GetMarks() const
409 void Slider::SetSnapToMarks( bool snap )
414 bool Slider::GetSnapToMarks() const
419 Actor Slider::CreateHitRegion()
421 Actor hitRegion = Actor::New();
422 hitRegion.SetParentOrigin( ParentOrigin::CENTER );
423 hitRegion.SetAnchorPoint( AnchorPoint::CENTER );
424 hitRegion.TouchedSignal().Connect( this, &Slider::OnTouchEvent );
429 ImageActor Slider::CreateBacking()
431 ImageActor backing = ImageActor::New();
432 backing.SetParentOrigin( ParentOrigin::CENTER );
433 backing.SetAnchorPoint( AnchorPoint::CENTER );
434 backing.SetZ( BACKING_Z );
439 void Slider::SetBackingImageName( const std::string& imageName )
441 if( mBacking && imageName != String::EMPTY )
443 Image image = ResourceImage::New( imageName );
444 mBacking.SetImage( image );
448 std::string Slider::GetBackingImageName()
452 return ResourceImage::DownCast( mBacking.GetImage() ).GetUrl();
455 return std::string( "" );
458 ImageActor Slider::CreateProgress()
460 ImageActor progress = ImageActor::New();
461 progress.SetParentOrigin( ParentOrigin::CENTER_LEFT );
462 progress.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
463 progress.SetZ( PROGRESS_Z );
468 void Slider::SetProgressImageName( const std::string& imageName )
470 if( mProgress && imageName != String::EMPTY )
472 Image image = ResourceImage::New( imageName );
473 mProgress.SetImage( image );
477 std::string Slider::GetProgressImageName()
481 return ResourceImage::DownCast( mProgress.GetImage()).GetUrl();
484 return std::string( "" );
487 void Slider::SetPopupImageName( const std::string& imageName )
489 mPopupImageName = imageName;
492 std::string Slider::GetPopupImageName()
494 return mPopupImageName;
497 void Slider::CreatePopupImage( const std::string& imageName )
499 if( mPopup && imageName != String::EMPTY )
501 Image image = ResourceImage::New( imageName );
502 mPopup.SetImage( image );
506 void Slider::SetPopupArrowImageName( const std::string& imageName )
508 mPopupArrowImageName = imageName;
511 std::string Slider::GetPopupArrowImageName()
513 return mPopupArrowImageName;
516 void Slider::CreatePopupArrowImage( const std::string& imageName )
518 if( mPopupArrow && imageName != String::EMPTY )
520 Image image = ResourceImage::New( imageName );
521 mPopupArrow.SetImage( image );
525 void Slider::ResizeProgressRegion( const Vector2& region )
529 mProgress.SetSize( region );
533 ImageActor Slider::CreateHandle()
535 ImageActor handle = ImageActor::New();
536 handle.SetParentOrigin( ParentOrigin::CENTER_LEFT );
537 handle.SetAnchorPoint( AnchorPoint::CENTER );
538 handle.SetZ( HANDLE_Z );
543 ImageActor Slider::CreatePopupArrow()
545 ImageActor arrow = ImageActor::New();
546 arrow.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
547 arrow.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
548 arrow.SetZ( HANDLE_Z );
553 Toolkit::TextView Slider::CreatePopupText()
555 Toolkit::TextView textView = Toolkit::TextView::New();
556 textView.SetParentOrigin( ParentOrigin::CENTER );
557 textView.SetAnchorPoint( AnchorPoint::CENTER );
558 textView.SetSizePolicy( Toolkit::Control::Flexible, Toolkit::Control::Flexible );
559 textView.SetZ( VALUE_DISPLAY_TEXT_Z );
563 ImageActor Slider::CreatePopup()
565 ImageActor popup = ImageActor::New();
566 popup.SetParentOrigin( ParentOrigin::TOP_CENTER );
567 popup.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
569 mValueTextView = CreatePopupText();
570 popup.Add( mValueTextView );
575 void Slider::SetHandleImageName( const std::string& imageName )
577 if( mHandle && imageName != String::EMPTY )
579 Image image = ResourceImage::New( imageName );
580 mHandle.SetImage( image );
584 std::string Slider::GetHandleImageName()
588 return ResourceImage::DownCast( mHandle.GetImage() ).GetUrl();
591 return std::string( "" );
594 void Slider::ResizeHandleRegion( const Vector2& region )
598 mHandle.SetSize( region );
602 void Slider::CreateHandleValueDisplay()
604 if( mHandle && !mHandleValueTextView )
606 mHandleValueTextView = Toolkit::TextView::New();
607 mHandleValueTextView.SetParentOrigin( ParentOrigin::CENTER );
608 mHandleValueTextView.SetAnchorPoint( AnchorPoint::CENTER );
609 mHandleValueTextView.SetSize( GetHandleRegion() );
610 mHandleValueTextView.SetZ( HANDLE_VALUE_DISPLAY_TEXT_Z );
611 mHandle.Add( mHandleValueTextView );
615 void Slider::DestroyHandleValueDisplay()
617 if(mHandleValueTextView)
619 mHandleValueTextView.Unparent();
620 mHandleValueTextView.Reset();
624 void Slider::SetPopupTextColor( const Vector4& color )
626 mPopupTextColor = color;
629 Actor Slider::CreateValueDisplay()
631 Actor popup = Actor::New();
632 popup.SetParentOrigin( ParentOrigin::TOP_CENTER );
633 popup.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
635 mPopupArrow = CreatePopupArrow();
636 popup.Add( mPopupArrow );
638 mPopup = CreatePopup();
639 mPopup.SetSize( 0.0f, VALUE_POPUP_HEIGHT );
640 mPopupArrow.Add( mPopup );
645 Toolkit::Slider::ValueChangedSignalType& Slider::ValueChangedSignal()
647 return mValueChangedSignal;
650 Toolkit::Slider::ValueChangedSignalType& Slider::SlidingFinishedSignal()
652 return mSlidingFinishedSignal;
655 Toolkit::Slider::MarkSignalType& Slider::MarkSignal()
660 void Slider::UpdateSkin()
666 mBacking.SetColor( Color::WHITE );
667 mHandle.SetColor( Color::WHITE );
668 mProgress.SetColor( Color::WHITE );
673 Vector4 disableColor = GetDisableColor();
674 mBacking.SetColor( disableColor );
675 mHandle.SetColor( disableColor );
676 mProgress.SetColor( disableColor );
690 void Slider::CreateChildren()
695 mHitArea = CreateHitRegion();
696 mPanDetector = PanGestureDetector::New();
697 mPanDetector.Attach( mHitArea );
698 mPanDetector.DetectedSignal().Connect( this, &Slider::OnPan );
699 self.Add( mHitArea );
702 mBacking = CreateBacking();
703 self.Add( mBacking );
706 mProgress = CreateProgress();
707 mBacking.Add( mProgress );
710 mHandle = CreateHandle();
711 mBacking.Add( mHandle );
714 void Slider::SetHitRegion( const Vector2& size )
720 mHitArea.SetSize( mHitRegion );
724 const Vector2& Slider::GetHitRegion() const
729 void Slider::AddPopup()
733 mValueDisplay = CreateValueDisplay();
734 mValueDisplay.SetVisible( false );
735 mHandle.Add( mValueDisplay );
737 CreatePopupImage( GetPopupImageName() );
738 CreatePopupArrowImage( GetPopupArrowImageName() );
740 mValueTimer = Timer::New( VALUE_VIEW_SHOW_DURATION );
741 mValueTimer.TickSignal().Connect( this, &Slider::HideValueView );
745 void Slider::RemovePopup()
752 mPopupArrow.Unparent();
755 mValueDisplay.Unparent();
756 mValueDisplay.Reset();
758 mValueTimer.TickSignal().Disconnect( this, &Slider::HideValueView );
764 float Slider::MarkFilter( float value )
766 const float MARK_TOLERANCE = GetMarkTolerance();
769 for( MarkList::iterator it = mMarks.begin(), itEnd = mMarks.end(); it != itEnd; ++it )
771 const Property::Value& propertyValue = *it;
772 propertyValue.Get( mark );
773 mark = MapValuePercentage( mark );
775 // If close to a mark, return the mark
776 if( fabsf( mark - value ) < MARK_TOLERANCE )
785 float Slider::SnapToMark( float value )
787 float closestMark = value;
788 float closestDist = std::numeric_limits<float>::max();
791 for( MarkList::iterator it = mMarks.begin(), itEnd = mMarks.end(); it != itEnd; ++it )
793 const Property::Value& propertyValue = *it;
794 propertyValue.Get( mark );
795 mark = MapValuePercentage( mark );
797 float dist = fabsf( mark - value );
798 if( dist < closestDist )
808 bool Slider::MarkReached( float value, int& outIndex )
810 const float MARK_TOLERANCE = GetMarkTolerance();
814 tail = mMarks.size() - 1;
818 while( head <= tail )
820 current = head + ( tail - head ) / 2;
822 const Property::Value& propertyValue = mMarks[ current ];
823 propertyValue.Get( mark );
824 mark = MapValuePercentage( mark );
826 if( fabsf( mark - value ) < MARK_TOLERANCE )
847 bool Slider::HideValueView()
851 mValueDisplay.SetVisible( false );
857 void Slider::SetLowerBound( float bound )
860 DisplayValue( GetValue(), false );
863 float Slider::GetLowerBound() const
868 void Slider::SetUpperBound( float bound )
871 DisplayValue( GetValue(), false );
874 float Slider::GetUpperBound() const
879 void Slider::SetValue( float value )
882 DisplayValue( mValue, true );
885 float Slider::GetValue() const
890 void Slider::SetBackingRegion( const Vector2& region )
892 mBackingRegion = region;
896 mBacking.SetSize( mBackingRegion );
899 ResizeProgressRegion( Vector2( 0.0f, mBackingRegion.y ) );
901 mDomain = CalcDomain( mBackingRegion );
903 DisplayValue( GetValue(), false ); // Set the progress bar to correct width
906 const Vector2& Slider::GetBackingRegion() const
908 return mBackingRegion;
911 void Slider::SetHandleRegion( const Vector2& region )
913 mHandleRegionSize = region;
915 ResizeHandleRegion( mHandleRegionSize );
917 Vector2 hitRegion = GetHitRegion();
918 hitRegion.x += mHandleRegionSize.x;
919 SetHitRegion( hitRegion );
922 const Vector2& Slider::GetHandleRegion() const
924 return mHandleRegionSize;
927 void Slider::SetDisableColor( const Vector4& color )
929 mDisableColor = color;
934 Vector4 Slider::GetDisableColor() const
936 return mDisableColor;
939 Vector4 Slider::GetPopupTextColor() const
941 return mPopupTextColor;
944 void Slider::SetValuePrecision( int precision )
946 mValuePrecision = precision;
949 int Slider::GetValuePrecision() const
951 return mValuePrecision;
954 void Slider::SetShowPopup( bool showPopup )
956 mShowPopup = showPopup;
969 bool Slider::GetShowPopup() const
974 void Slider::SetShowValue( bool showValue )
976 mShowValue = showValue;
980 CreateHandleValueDisplay();
984 DestroyHandleValueDisplay();
988 bool Slider::GetShowValue() const
993 void Slider::SetEnabled( bool enabled )
1007 bool Slider::IsEnabled() const
1009 return mState != DISABLED;
1012 void Slider::SetMarkTolerance( float tolerance )
1014 mMarkTolerance = tolerance;
1017 float Slider::GetMarkTolerance() const
1019 return mMarkTolerance;
1022 // static class method to support script connecting signals
1024 bool Slider::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1026 Dali::BaseHandle handle( object );
1028 bool connected = true;
1029 Toolkit::Slider slider = Toolkit::Slider::DownCast( handle );
1031 if( signalName == Dali::Toolkit::Slider::SIGNAL_VALUE_CHANGED )
1033 slider.ValueChangedSignal().Connect( tracker, functor );
1035 else if( signalName == Dali::Toolkit::Slider::SIGNAL_MARK )
1037 slider.MarkSignal().Connect( tracker, functor );
1041 // signalName does not match any signal
1048 void Slider::DisplayPopup( float value )
1050 // Value displayDoConnectSignal
1051 if( mValueTextView )
1053 std::stringstream ss;
1054 ss.precision( GetValuePrecision() );
1055 ss << std::fixed << value;
1056 mValueTextView.SetText( ss.str() );
1058 style.SetTextColor( GetPopupTextColor() );
1059 mValueTextView.SetStyleToCurrentText( style, TextStyle::COLOR);
1063 Font font = Font::New();
1064 float popupWidth = font.MeasureText( ss.str() ).x + VALUE_POPUP_MARGIN * 2.0f;
1065 if( popupWidth < VALUE_POPUP_MIN_WIDTH )
1067 popupWidth = VALUE_POPUP_MIN_WIDTH;
1070 mPopup.SetSize( popupWidth, VALUE_POPUP_HEIGHT );
1071 mValueDisplay.SetVisible( true );
1073 mValueTimer.SetInterval( VALUE_VIEW_SHOW_DURATION );
1078 void Slider::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
1080 Toolkit::Slider slider = Toolkit::Slider::DownCast( Dali::BaseHandle( object ) );
1084 Slider& sliderImpl( GetImpl( slider ) );
1086 switch ( propertyIndex )
1088 case Toolkit::Slider::LOWER_BOUND_PROPERTY:
1090 sliderImpl.SetLowerBound( value.Get< float >() );
1094 case Toolkit::Slider::UPPER_BOUND_PROPERTY:
1096 sliderImpl.SetUpperBound( value.Get< float >() );
1100 case Toolkit::Slider::VALUE_PROPERTY:
1102 sliderImpl.SetValue( value.Get< float >() );
1106 case Toolkit::Slider::HIT_REGION_PROPERTY:
1108 sliderImpl.SetHitRegion( value.Get< Vector2 >() );
1112 case Toolkit::Slider::BACKING_REGION_PROPERTY:
1114 sliderImpl.SetBackingRegion( value.Get< Vector2 >() );
1118 case Toolkit::Slider::HANDLE_REGION_PROPERTY:
1120 sliderImpl.SetHandleRegion( value.Get< Vector2 >() );
1124 case Toolkit::Slider::BACKING_IMAGE_NAME_PROPERTY:
1126 sliderImpl.SetBackingImageName( value.Get< std::string >() );
1130 case Toolkit::Slider::HANDLE_IMAGE_NAME_PROPERTY:
1132 sliderImpl.SetHandleImageName( value.Get< std::string >() );
1136 case Toolkit::Slider::PROGRESS_IMAGE_NAME_PROPERTY:
1138 sliderImpl.SetProgressImageName( value.Get< std::string >() );
1142 case Toolkit::Slider::POPUP_IMAGE_NAME_PROPERTY:
1144 sliderImpl.SetPopupImageName( value.Get< std::string >() );
1148 case Toolkit::Slider::POPUP_ARROW_IMAGE_NAME_PROPERTY:
1150 sliderImpl.SetPopupArrowImageName( value.Get< std::string >() );
1154 case Toolkit::Slider::DISABLE_COLOR_PROPERTY:
1156 sliderImpl.SetDisableColor( value.Get< Vector4 >() );
1160 case Toolkit::Slider::POPUP_TEXT_COLOR_PROPERTY:
1162 sliderImpl.SetPopupTextColor( value.Get< Vector4 >() );
1166 case Toolkit::Slider::VALUE_PRECISION_PROPERTY:
1168 sliderImpl.SetValuePrecision( value.Get< int >() );
1172 case Toolkit::Slider::SHOW_POPUP_PROPERTY:
1174 sliderImpl.SetShowPopup( value.Get< bool >() );
1178 case Toolkit::Slider::SHOW_VALUE_PROPERTY:
1180 sliderImpl.SetShowValue( value.Get< bool >() );
1184 case Toolkit::Slider::ENABLED_PROPERTY:
1186 sliderImpl.SetEnabled( value.Get< bool >() );
1190 case Toolkit::Slider::MARKS_PROPERTY:
1192 sliderImpl.SetMarks( value.Get< Property::Array >() );
1196 case Toolkit::Slider::SNAP_TO_MARKS_PROPERTY:
1198 sliderImpl.SetSnapToMarks( value.Get< bool >() );
1202 case Toolkit::Slider::MARK_TOLERANCE_PROPERTY:
1204 sliderImpl.SetMarkTolerance( value.Get< float >() );
1211 Property::Value Slider::GetProperty( BaseObject* object, Property::Index propertyIndex )
1213 Property::Value value;
1215 Toolkit::Slider slider = Toolkit::Slider::DownCast( Dali::BaseHandle( object ) );
1219 Slider& sliderImpl( GetImpl( slider ) );
1221 switch ( propertyIndex )
1223 case Toolkit::Slider::LOWER_BOUND_PROPERTY:
1225 value = sliderImpl.GetLowerBound();
1229 case Toolkit::Slider::UPPER_BOUND_PROPERTY:
1231 value = sliderImpl.GetUpperBound();
1235 case Toolkit::Slider::VALUE_PROPERTY:
1237 value = sliderImpl.GetValue();
1241 case Toolkit::Slider::HIT_REGION_PROPERTY:
1243 value = sliderImpl.GetHitRegion();
1247 case Toolkit::Slider::BACKING_REGION_PROPERTY:
1249 value = sliderImpl.GetBackingRegion();
1253 case Toolkit::Slider::HANDLE_REGION_PROPERTY:
1255 value = sliderImpl.GetHandleRegion();
1259 case Toolkit::Slider::BACKING_IMAGE_NAME_PROPERTY:
1261 value = sliderImpl.GetBackingImageName();
1265 case Toolkit::Slider::HANDLE_IMAGE_NAME_PROPERTY:
1267 value = sliderImpl.GetHandleImageName();
1271 case Toolkit::Slider::PROGRESS_IMAGE_NAME_PROPERTY:
1273 value = sliderImpl.GetProgressImageName();
1277 case Toolkit::Slider::POPUP_IMAGE_NAME_PROPERTY:
1279 value = sliderImpl.GetPopupImageName();
1283 case Toolkit::Slider::POPUP_ARROW_IMAGE_NAME_PROPERTY:
1285 value = sliderImpl.GetPopupArrowImageName();
1289 case Toolkit::Slider::DISABLE_COLOR_PROPERTY:
1291 value = sliderImpl.GetDisableColor();
1295 case Toolkit::Slider::POPUP_TEXT_COLOR_PROPERTY:
1297 value = sliderImpl.GetPopupTextColor();
1301 case Toolkit::Slider::VALUE_PRECISION_PROPERTY:
1303 value = sliderImpl.GetValuePrecision();
1307 case Toolkit::Slider::SHOW_POPUP_PROPERTY:
1309 value = sliderImpl.GetShowPopup();
1313 case Toolkit::Slider::SHOW_VALUE_PROPERTY:
1315 value = sliderImpl.GetShowValue();
1319 case Toolkit::Slider::ENABLED_PROPERTY:
1321 value = sliderImpl.IsEnabled();
1325 case Toolkit::Slider::MARKS_PROPERTY:
1327 // TODO: Need to be able to return a PropertyArray
1328 // value = sliderImpl.GetMarks();
1332 case Toolkit::Slider::SNAP_TO_MARKS_PROPERTY:
1334 value = sliderImpl.GetSnapToMarks();
1338 case Toolkit::Slider::MARK_TOLERANCE_PROPERTY:
1340 value = sliderImpl.GetMarkTolerance();
1349 } // namespace Internal
1351 } // namespace Toolkit