2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 <dali-toolkit/internal/controls/scroll-component/scroll-bar-internal-impl.h>
18 #include <dali-toolkit/public-api/enums.h>
27 * @param[in] x Input value to be squared
28 * @return Result (x*x)
36 const char* BAR_TAB_IMAGE_PATH = DALI_IMAGE_DIR "popup_scroll.png";
37 const Vector4 BAR_TAB_NINE_PATCH_BORDER(0.0f, 12.0f, 14.0f, 14.0f);
38 const Vector3 BAR_TAB_SIZE(18.0f, 72.0f, 0.0f);
39 const Vector3 BAR_TAB_OFFSET_V(-18.0f, 0.0f, 0.1f);
40 const Vector3 BAR_TAB_OFFSET_H(0.0f, -18.0f, 0.1f);
41 const float BAR_CONTRACT_DELAY(0.8f);
42 const float BAR_SHOW_TIME(0.4f);
43 const float BAR_HIDE_TIME(0.5f);
44 const int SECOND_UNIT(1000);
47 * ScrollBarInternal Visibility Constraint
48 * Returns whether scroll bar is visible
50 bool ScrollBarInternalVisibilityConstraint(const bool& current,
51 const PropertyInput& canScrollProperty)
53 bool canScroll = canScrollProperty.GetBoolean();
58 * ScrollBarInternal Size Constraint
59 * Resize ScrollBarInternal Size depends on both ScrollSize and DomainSize
61 struct ScrollBarInternalSizeConstraint
64 * @param[in] vertical Whether this constraint controls a vertical scrollbar (true)
65 * or a horizontal one (false)
67 ScrollBarInternalSizeConstraint(bool vertical)
74 * @param[in] current The current ScrollBarInternal size
75 * @param[in] scrollMinProperty The container's minimum position.
76 * @param[in] scrollMaxProperty The container's maximum position.
77 * @param[in] scrollDirectionProperty The container's scroll direction.
78 * @param[in] scrollSizeProperty The container's size of viewport.
79 * @return The new ScrollBarInternal position is returned.
81 Vector3 operator()(const Vector3& current,
82 const PropertyInput& scrollMinProperty,
83 const PropertyInput& scrollMaxProperty,
84 const PropertyInput& scrollDirectionProperty,
85 const PropertyInput& scrollSizeProperty)
87 const Vector3& min = scrollMinProperty.GetVector3();
88 const Vector3& max = scrollMaxProperty.GetVector3();
89 const Vector3& scrollDirection = scrollDirectionProperty.GetVector3();
90 const Toolkit::ControlOrientation::Type& orientation = static_cast<Toolkit::ControlOrientation::Type>(scrollDirection.z);
91 const Vector3& size = scrollSizeProperty.GetVector3();
92 const Vector3 domainSize = max - min;
94 if (mVertical && Toolkit::IsVertical(orientation))
96 float mod = fabsf(domainSize.height) > size.height ? size.height * ( size.height / fabsf(domainSize.height) ) : size.height * ( (size.height - fabsf(domainSize.height * 0.5f)) / size.height);
97 return Vector3( current.width, mod, current.depth );
101 float mod = fabsf(domainSize.height) > size.width ? size.width * ( size.width / fabsf(domainSize.height) ) : size.width * ( (size.width - fabsf(domainSize.height * 0.5f)) / size.width);
102 return Vector3( current.width, mod, current.depth );
106 bool mVertical; ///< Whether vertical or horizontal
110 * ScrollBarInternal rotation Constraint
111 * Rotate ScrollBarInternal depends on the scroll direction
113 struct ScrollBarInternalRotationConstraint
116 * @param[in] vertical Whether this constraint controls a vertical scrollbar (true)
117 * or a horizontal one (false)
119 ScrollBarInternalRotationConstraint(bool vertical)
120 : mVertical(vertical)
125 * Constraint operator
126 * @param[in] current The current ScrollBarInternal rotation
127 * @param[in] scrollDirectionProperty The container's scroll direction.
128 * @return The new ScrollBarInternal rotation is returned.
130 Quaternion operator()(const Quaternion& current,
131 const PropertyInput& scrollDirectionProperty)
133 const Vector3& scrollDirection = scrollDirectionProperty.GetVector3();
134 const Toolkit::ControlOrientation::Type& orientation = static_cast<Toolkit::ControlOrientation::Type>(scrollDirection.z);
136 if( (mVertical && Toolkit::IsVertical(orientation)) || (!mVertical && Toolkit::IsHorizontal(orientation)) )
138 return Quaternion(0.0f, Vector3::ZAXIS);
142 return Quaternion(0.5f * Math::PI, Vector3::ZAXIS);
146 bool mVertical; ///< Whether vertical or horizontal
150 * ScrollBarInternal Position Constraint
151 * Positions the scroll bar to reflect the current scroll position
154 struct ScrollBarInternalPositionConstraint
157 * @param[in] vertical Whether this constraint controls a vertical scrollbar (true)
158 * or a horizontal one (false)
159 * @param[in] wrap Whether to base scrollbar on original position or wrapped position
161 ScrollBarInternalPositionConstraint(bool vertical, bool wrap = false)
162 : mVertical(vertical),
168 * Constraint operator
169 * @param[in] current The current ScrollBarInternal position
170 * @param[in] scrollBarSizeProperty ScrollBarInternal size
171 * @param[in] scrollRelativePositionProperty The container's relative position (from 0.0 -> 1.0 in each axis)
172 * @param[in] scrollMinProperty The container's minimum position.
173 * @param[in] scrollMaxProperty The container's maximum position.
174 * @param[in] scrollDirectionProperty The container's scroll direction.
175 * @param[in] scrollSizeProperty The container's size of viewport.
176 * @return The new ScrollBarInternal position is returned.
178 Vector3 operator()(const Vector3& current,
179 const PropertyInput& scrollBarSizeProperty,
180 const PropertyInput& scrollRelativePositionProperty,
181 const PropertyInput& scrollMinProperty,
182 const PropertyInput& scrollMaxProperty,
183 const PropertyInput& scrollDirectionProperty,
184 const PropertyInput& scrollSizeProperty)
186 Vector3 barSize = scrollBarSizeProperty.GetVector3();
187 Vector3 relativePosition = scrollRelativePositionProperty.GetVector3();
188 Vector3 size = scrollSizeProperty.GetVector3();
189 const Vector3& min = scrollMinProperty.GetVector3();
190 const Vector3& max = scrollMaxProperty.GetVector3();
191 const Vector3& scrollDirection = scrollDirectionProperty.GetVector3();
192 const Toolkit::ControlOrientation::Type& orientation = static_cast<Toolkit::ControlOrientation::Type>(scrollDirection.z);
194 Vector3 domainSize = max - min;
195 domainSize.x = fabsf(domainSize.x);
196 domainSize.y = fabsf(domainSize.y);
199 Vector3 mask; // Mask movement aspect of scroll bar
200 Vector3 relativeOffset; // base position of scroll bar in relation to the container
201 Vector3 absoluteOffset; // absolute offset position of scroll bar
207 case Toolkit::ControlOrientation::Up:
209 mask = Vector3::YAXIS;
210 relativeOffset = (scrollDirection.y < 0.0f && relativePosition.y <= 0.0f) ? Vector3(1.0f, 1.0f, 0.0f) : Vector3(1.0f, 0.0f, 0.0f); // Right side of stage.
211 absoluteOffset = (scrollDirection.y < 0.0f && relativePosition.y <= 0.0f) ? BAR_TAB_OFFSET_V + Vector3( barSize.width * 0.5f, -barSize.height * 0.5f, 1.0f ) : BAR_TAB_OFFSET_V + Vector3( barSize.width * 0.5f, barSize.height * 0.5f, 1.0f );
214 case Toolkit::ControlOrientation::Left:
216 mask = Vector3::XAXIS;
217 relativeOffset = (scrollDirection.x <= 0.0f && relativePosition.y <= 0.0f) ? Vector3(1.0f, 0.0f, 0.0f) : Vector3(0.0f, 0.0f, 0.0f); // Bottom side of stage.
218 absoluteOffset = (scrollDirection.x <= 0.0f && relativePosition.y <= 0.0f) ? Vector3( -barSize.height * 0.5f, barSize.width * 0.5f, 1.0f ) : Vector3( barSize.height * 0.5f, barSize.width * 0.5f, 1.0f );
221 case Toolkit::ControlOrientation::Down:
223 mask = Vector3::YAXIS;
224 relativeOffset = (scrollDirection.y <= 0.0f && relativePosition.y <= 0.0f) ? Vector3(0.0f, 1.0f, 0.0f) : Vector3(0.0f, 0.0f, 0.0f); // Left side of stage.
225 absoluteOffset = (scrollDirection.y <= 0.0f && relativePosition.y <= 0.0f) ? Vector3( barSize.width * 0.5f, -barSize.height * 0.5f, 1.0f ) : Vector3( barSize.width * 0.5f, barSize.height * 0.5f, 1.0f );
228 case Toolkit::ControlOrientation::Right:
230 mask = Vector3::XAXIS;
231 relativeOffset = (scrollDirection.x <= 0.0f && relativePosition.y <= 0.0f) ? Vector3(1.0f, 1.0f, 0.0f) : Vector3(0.0f, 1.0f, 0.0f); // Up side of stage.
232 absoluteOffset = (scrollDirection.x <= 0.0f && relativePosition.y <= 0.0f) ? Vector3( -barSize.height * 0.5f, -barSize.width * 0.5f, 1.0f ) : Vector3( barSize.height * 0.5f, -barSize.width * 0.5f, 1.0f );
239 mask = Vector3::XAXIS;
240 relativeOffset = Vector3(0.0f, 1.0f, 0.0f); // Bottom side of stage.
241 absoluteOffset = BAR_TAB_OFFSET_H + Vector3( barSize.height * 0.5f, barSize.width * 0.5f, 1.0f );
244 Vector3 maskedRelativePosition = Toolkit::IsVertical(orientation) ? Vector3(relativePosition.x * (size.x-barSize.y), relativePosition.y * (size.y-barSize.y), 0.0f) * mask
245 : Vector3(relativePosition.y * (size.x-barSize.y), relativePosition.x * (size.y-barSize.y), 0.0f) * mask;
247 Vector3 finalPosition = relativeOffset * size + absoluteOffset + maskedRelativePosition;
249 // If Wrapped Slider, then position 1 domain either before or after current slider.
252 if(finalPosition.x < 0.5f)
254 finalPosition.x += size.x;
258 finalPosition.x -= size.x;
261 if(finalPosition.y < 0.5f)
263 finalPosition.y += size.y;
267 finalPosition.y -= size.y;
271 return finalPosition;
274 bool mVertical; ///< Whether vertical or horizontal.
275 bool mWrap; ///< Whether to wrap this position.
279 * ScrollBarInternal HitSize Constraint
280 * Resizes HitArea to size of the container.
282 struct ScrollBarInternalHitSizeConstraint
285 * @param[in] vertical Whether this constraint controls a vertical scrollbar (true)
286 * or a horizontal one (false)
287 * @param[in] thickness The thickness of the scrollbar
289 ScrollBarInternalHitSizeConstraint(bool vertical,
291 : mVertical(vertical),
292 mThickness(thickness)
297 * Constraint operator
298 * @param[in] current The current HitSize
299 * @param[in] scrollDirectionProperty The container's scroll direction.
300 * @param[in] scrollSizeProperty The container's size of viewport.
301 * @return The new ScrollBarInternal Hit Area size is returned.
303 Vector3 operator()(const Vector3& current,
304 const PropertyInput& scrollDirectionProperty,
305 const PropertyInput& scrollSizeProperty)
307 const Vector3& scrollDirection = scrollDirectionProperty.GetVector3();
308 const Toolkit::ControlOrientation::Type& orientation = static_cast<Toolkit::ControlOrientation::Type>(scrollDirection.z);
309 Vector3 size = scrollSizeProperty.GetVector3();
311 Vector3 mask; // Mask size aspect of hit area.
312 Vector3 offset; // Add Offset size.
314 if( (mVertical && Toolkit::IsVertical(orientation)) || (!mVertical && Toolkit::IsHorizontal(orientation)) )
316 mask = Vector3::YAXIS;
317 offset = Vector3::XAXIS * mThickness;
321 mask = Vector3::XAXIS;
322 offset = Vector3::YAXIS * mThickness;
325 return size * mask + offset;
328 bool mVertical; ///< Whether vertical or horizontal.
329 float mThickness; ///< Thickness of the scroll bar
332 } // unnamed namespace
346 using namespace Dali;
353 TypeRegistration mType( typeid(Toolkit::ScrollBarInternal), typeid(Toolkit::ScrollComponent), Create );
357 ScrollBarInternal::ScrollBarInternal(Toolkit::Scrollable& container, bool vertical)
358 : mContainer(static_cast<Toolkit::Internal::Scrollable&>(container.GetImplementation())),
360 mAxisMask(vertical ? Vector3::YAXIS : Vector3::XAXIS),
363 Image sliderImage = Image::New( BAR_TAB_IMAGE_PATH );
365 mSlider = ImageActor::New( sliderImage );
366 mSlider.SetParentOrigin( ParentOrigin::TOP_LEFT );
367 mSlider.SetAnchorPoint( AnchorPoint::CENTER );
368 mSlider.SetSize( BAR_TAB_SIZE );
369 mSlider.SetStyle( ImageActor::STYLE_NINE_PATCH );
370 mSlider.SetNinePatchBorder( BAR_TAB_NINE_PATCH_BORDER );
372 // A duplicate Slider should appear 1 domain away from the original Slider
373 mSliderWrap = ImageActor::New( sliderImage );
374 mSliderWrap.SetParentOrigin( ParentOrigin::TOP_LEFT );
375 mSliderWrap.SetAnchorPoint( AnchorPoint::CENTER );
376 mSliderWrap.SetSize( BAR_TAB_SIZE );
377 mSliderWrap.SetStyle( ImageActor::STYLE_NINE_PATCH );
378 mSliderWrap.SetNinePatchBorder( BAR_TAB_NINE_PATCH_BORDER );
380 // target the container to observe for scrolling
381 Actor target = mContainer.Self();
382 Constraint constraint = Constraint::New<bool>( Actor::VISIBLE,
383 Source( target, vertical ? target.GetPropertyIndex(Scrollable::SCROLLABLE_CAN_SCROLL_VERTICAL) : target.GetPropertyIndex(Scrollable::SCROLLABLE_CAN_SCROLL_HORIZONTAL)),
384 ScrollBarInternalVisibilityConstraint );
385 mSlider.ApplyConstraint( constraint );
386 mSliderWrap.ApplyConstraint( constraint );
388 constraint = Constraint::New<Vector3>( Actor::SIZE,
389 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
390 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
391 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME ) ),
392 Source( target, Actor::SIZE ),
393 ScrollBarInternalSizeConstraint( vertical ) );
394 mSlider.ApplyConstraint( constraint );
395 mSliderWrap.ApplyConstraint( constraint );
397 constraint = Constraint::New<Quaternion>( Actor::ROTATION,
398 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME ) ),
399 ScrollBarInternalRotationConstraint( vertical ) );
400 mSlider.ApplyConstraint( constraint );
401 mSliderWrap.ApplyConstraint( constraint );
403 constraint = Constraint::New<Vector3>( Actor::POSITION,
404 Source( mSlider, Actor::SIZE),
405 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_RELATIVE_POSITION_PROPERTY_NAME ) ),
406 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
407 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
408 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME ) ),
409 Source( target, Actor::SIZE ),
410 ScrollBarInternalPositionConstraint(vertical) );
412 mSlider.ApplyConstraint( constraint );
414 constraint = Constraint::New<Vector3>( Actor::POSITION,
415 Source( mSlider, Actor::SIZE),
416 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_RELATIVE_POSITION_PROPERTY_NAME ) ),
417 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
418 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
419 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME ) ),
420 Source( target, Actor::SIZE ),
421 ScrollBarInternalPositionConstraint(vertical, true) );
422 mSliderWrap.ApplyConstraint( constraint );
424 // Add Sliders to internal Actor, to avoid mixing up with regular
425 // Actors added by user.
426 mContainer.AddOverlay( mSlider );
427 mContainer.AddOverlay( mSliderWrap );
428 mContainer.ScrollStartedSignal().Connect( this, &ScrollBarInternal::OnStarted );
429 mContainer.ScrollCompletedSignal().Connect( this, &ScrollBarInternal::OnCompleted );
431 // Hit Area for dragging slider /////////////////////////////////////////////
432 mHitArea = Actor::New();
433 mHitArea.SetPosition(0.0f, 0.0f, 0.2f);
435 mContainer.AddOverlay( mHitArea );
436 constraint = Constraint::New<Vector3>( Actor::SIZE,
437 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME ) ),
438 Source( target, Actor::SIZE ),
439 ScrollBarInternalHitSizeConstraint(vertical, BAR_TAB_SIZE.width) );
440 mHitArea.ApplyConstraint( constraint );
444 mHitArea.SetParentOrigin(ParentOrigin::CENTER_RIGHT);
445 mHitArea.SetAnchorPoint(AnchorPoint::CENTER_RIGHT);
449 mHitArea.SetParentOrigin(ParentOrigin::BOTTOM_CENTER);
450 mHitArea.SetAnchorPoint(AnchorPoint::BOTTOM_CENTER);
453 WaitingContractDelay();
456 ScrollBarInternal::~ScrollBarInternal()
461 void ScrollBarInternal::OnInitialize()
463 EnableGestureDetection(Gesture::Type(Gesture::Pan));
466 void ScrollBarInternal::OnDisconnect()
468 // Disconnect all connected callback functions.
469 mContainer.RemoveOverlay( mSlider );
470 mContainer.RemoveOverlay( mSliderWrap );
473 void ScrollBarInternal::OnPanGesture(Actor actor, PanGesture gesture)
475 switch(gesture.state)
477 case Gesture::Started:
481 mScrollStart = mContainer.GetCurrentScrollPosition();
482 mGestureDisplacement = Vector3::ZERO;
485 case Gesture::Continuing:
487 Vector3 delta(gesture.displacement.x, gesture.displacement.y, 0.0f);
488 mGestureDisplacement+=delta;
490 Vector3 size = mContainer.Self().GetCurrentSize();
491 Vector3 span = size - Vector3(BAR_TAB_SIZE.y, BAR_TAB_SIZE.y, 1.0f);
492 Vector3 domainSize = mContainer.GetDomainSize();
494 Vector3 position = mScrollStart + mGestureDisplacement * mAxisMask * domainSize / span;
495 mContainer.ScrollTo(position, 0.0f);
506 void ScrollBarInternal::OnStarted(const Vector3& position)
508 // TODO: Need to disable this for the scrollbar which isn't being scrolled.
516 void ScrollBarInternal::OnCompleted(const Vector3& position)
522 WaitingContractDelay();
526 bool ScrollBarInternal::OnContractDelayExpired()
538 void ScrollBarInternal::Show()
540 // Cancel any animation
547 mAnimation = Animation::New( BAR_SHOW_TIME );
548 mAnimation.OpacityTo( mSlider, 1.0f, AlphaFunctions::EaseIn );
549 mAnimation.OpacityTo( mSliderWrap, 1.0f, AlphaFunctions::EaseIn );
555 void ScrollBarInternal::Hide()
557 // Cancel any animation
564 mAnimation = Animation::New( BAR_HIDE_TIME );
565 mAnimation.OpacityTo( mSlider, 0.0f, AlphaFunctions::EaseIn );
566 mAnimation.OpacityTo( mSliderWrap, 0.0f, AlphaFunctions::EaseIn );
570 void ScrollBarInternal::CreateTimer()
574 // Create timer for contract delay
575 mTimer = Timer::New( BAR_CONTRACT_DELAY * SECOND_UNIT );
576 mTimer.TickSignal().Connect( this, &ScrollBarInternal::OnContractDelayExpired );
580 void ScrollBarInternal::DestructTimer()
585 mTimer.TickSignal().Disconnect( this, &ScrollBarInternal::OnContractDelayExpired );
590 void ScrollBarInternal::WaitingContractDelay()
596 Toolkit::ScrollBarInternal ScrollBarInternal::New(Toolkit::Scrollable& container, bool vertical)
598 // Create the implementation, temporarily owned by this handle on stack
599 IntrusivePtr< ScrollBarInternal > impl = new ScrollBarInternal( container, vertical );
601 // Pass ownership to CustomActor handle
602 Toolkit::ScrollBarInternal handle( *impl );
604 // Second-phase init of the implementation
605 // This can only be done after the CustomActor connection has been made...
611 } // namespace Internal
613 } // namespace Toolkit