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/scroll-component/scroll-bar-internal-impl.h>
22 #include <dali/public-api/animation/active-constraint.h>
23 #include <dali/public-api/animation/constraint.h>
24 #include <dali/public-api/object/property-input.h>
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/public-api/images/resource-image.h>
29 #include <dali-toolkit/public-api/enums.h>
38 * @param[in] x Input value to be squared
39 * @return Result (x*x)
47 const char* BAR_TAB_IMAGE_PATH = DALI_IMAGE_DIR "popup_scroll.png";
48 const Vector4 BAR_TAB_NINE_PATCH_BORDER(0.0f, 12.0f, 14.0f, 14.0f);
49 const Vector3 BAR_TAB_SIZE(18.0f, 72.0f, 0.0f);
50 const Vector3 BAR_TAB_OFFSET_V(-18.0f, 0.0f, 0.1f);
51 const Vector3 BAR_TAB_OFFSET_H(0.0f, -18.0f, 0.1f);
52 const float BAR_CONTRACT_DELAY(0.8f);
53 const float BAR_SHOW_TIME(0.4f);
54 const float BAR_HIDE_TIME(0.5f);
55 const int SECOND_UNIT(1000);
58 * ScrollBarInternal Visibility Constraint
59 * Returns whether scroll bar is visible
61 bool ScrollBarInternalVisibilityConstraint(const bool& current,
62 const PropertyInput& canScrollProperty)
64 bool canScroll = canScrollProperty.GetBoolean();
69 * ScrollBarInternal Size Constraint
70 * Resize ScrollBarInternal Size depends on both ScrollSize and DomainSize
72 struct ScrollBarInternalSizeConstraint
75 * @param[in] vertical Whether this constraint controls a vertical scrollbar (true)
76 * or a horizontal one (false)
78 ScrollBarInternalSizeConstraint(bool vertical)
85 * @param[in] current The current ScrollBarInternal size
86 * @param[in] scrollMinProperty The container's minimum position.
87 * @param[in] scrollMaxProperty The container's maximum position.
88 * @param[in] scrollDirectionProperty The container's scroll direction.
89 * @param[in] scrollSizeProperty The container's size of viewport.
90 * @return The new ScrollBarInternal position is returned.
92 Vector3 operator()(const Vector3& current,
93 const PropertyInput& scrollMinProperty,
94 const PropertyInput& scrollMaxProperty,
95 const PropertyInput& scrollDirectionProperty,
96 const PropertyInput& scrollSizeProperty)
98 const Vector3& min = scrollMinProperty.GetVector3();
99 const Vector3& max = scrollMaxProperty.GetVector3();
100 const Vector3& scrollDirection = scrollDirectionProperty.GetVector3();
101 const Toolkit::ControlOrientation::Type& orientation = static_cast<Toolkit::ControlOrientation::Type>(scrollDirection.z);
102 const Vector3& size = scrollSizeProperty.GetVector3();
103 const Vector3 domainSize = max - min;
105 if (mVertical && Toolkit::IsVertical(orientation))
107 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);
108 return Vector3( current.width, mod, current.depth );
112 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);
113 return Vector3( current.width, mod, current.depth );
117 bool mVertical; ///< Whether vertical or horizontal
121 * ScrollBarInternal rotation Constraint
122 * Rotate ScrollBarInternal depends on the scroll direction
124 struct ScrollBarInternalRotationConstraint
127 * @param[in] vertical Whether this constraint controls a vertical scrollbar (true)
128 * or a horizontal one (false)
130 ScrollBarInternalRotationConstraint(bool vertical)
131 : mVertical(vertical)
136 * Constraint operator
137 * @param[in] current The current ScrollBarInternal rotation
138 * @param[in] scrollDirectionProperty The container's scroll direction.
139 * @return The new ScrollBarInternal rotation is returned.
141 Quaternion operator()(const Quaternion& current,
142 const PropertyInput& scrollDirectionProperty)
144 const Vector3& scrollDirection = scrollDirectionProperty.GetVector3();
145 const Toolkit::ControlOrientation::Type& orientation = static_cast<Toolkit::ControlOrientation::Type>(scrollDirection.z);
147 if( (mVertical && Toolkit::IsVertical(orientation)) || (!mVertical && Toolkit::IsHorizontal(orientation)) )
149 return Quaternion(0.0f, Vector3::ZAXIS);
153 return Quaternion(0.5f * Math::PI, Vector3::ZAXIS);
157 bool mVertical; ///< Whether vertical or horizontal
161 * ScrollBarInternal Position Constraint
162 * Positions the scroll bar to reflect the current scroll position
165 struct ScrollBarInternalPositionConstraint
168 * @param[in] vertical Whether this constraint controls a vertical scrollbar (true)
169 * or a horizontal one (false)
170 * @param[in] wrap Whether to base scrollbar on original position or wrapped position
172 ScrollBarInternalPositionConstraint(bool vertical, bool wrap = false)
173 : mVertical(vertical),
179 * Constraint operator
180 * @param[in] current The current ScrollBarInternal position
181 * @param[in] scrollBarSizeProperty ScrollBarInternal size
182 * @param[in] scrollRelativePositionProperty The container's relative position (from 0.0 -> 1.0 in each axis)
183 * @param[in] scrollMinProperty The container's minimum position.
184 * @param[in] scrollMaxProperty The container's maximum position.
185 * @param[in] scrollDirectionProperty The container's scroll direction.
186 * @param[in] scrollSizeProperty The container's size of viewport.
187 * @return The new ScrollBarInternal position is returned.
189 Vector3 operator()(const Vector3& current,
190 const PropertyInput& scrollBarSizeProperty,
191 const PropertyInput& scrollRelativePositionProperty,
192 const PropertyInput& scrollMinProperty,
193 const PropertyInput& scrollMaxProperty,
194 const PropertyInput& scrollDirectionProperty,
195 const PropertyInput& scrollSizeProperty)
197 Vector3 barSize = scrollBarSizeProperty.GetVector3();
198 Vector3 relativePosition = scrollRelativePositionProperty.GetVector3();
199 Vector3 size = scrollSizeProperty.GetVector3();
200 const Vector3& min = scrollMinProperty.GetVector3();
201 const Vector3& max = scrollMaxProperty.GetVector3();
202 const Vector3& scrollDirection = scrollDirectionProperty.GetVector3();
203 const Toolkit::ControlOrientation::Type& orientation = static_cast<Toolkit::ControlOrientation::Type>(scrollDirection.z);
205 Vector3 domainSize = max - min;
206 domainSize.x = fabsf(domainSize.x);
207 domainSize.y = fabsf(domainSize.y);
210 Vector3 mask; // Mask movement aspect of scroll bar
211 Vector3 relativeOffset; // base position of scroll bar in relation to the container
212 Vector3 absoluteOffset; // absolute offset position of scroll bar
218 case Toolkit::ControlOrientation::Up:
220 mask = Vector3::YAXIS;
221 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.
222 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 );
225 case Toolkit::ControlOrientation::Left:
227 mask = Vector3::XAXIS;
228 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.
229 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 );
232 case Toolkit::ControlOrientation::Down:
234 mask = Vector3::YAXIS;
235 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.
236 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 );
239 case Toolkit::ControlOrientation::Right:
241 mask = Vector3::XAXIS;
242 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.
243 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 );
250 mask = Vector3::XAXIS;
251 relativeOffset = Vector3(0.0f, 1.0f, 0.0f); // Bottom side of stage.
252 absoluteOffset = BAR_TAB_OFFSET_H + Vector3( barSize.height * 0.5f, barSize.width * 0.5f, 1.0f );
255 Vector3 maskedRelativePosition = Toolkit::IsVertical(orientation) ? Vector3(relativePosition.x * (size.x-barSize.y), relativePosition.y * (size.y-barSize.y), 0.0f) * mask
256 : Vector3(relativePosition.y * (size.x-barSize.y), relativePosition.x * (size.y-barSize.y), 0.0f) * mask;
258 Vector3 finalPosition = relativeOffset * size + absoluteOffset + maskedRelativePosition;
260 // If Wrapped Slider, then position 1 domain either before or after current slider.
263 if(finalPosition.x < 0.5f)
265 finalPosition.x += size.x;
269 finalPosition.x -= size.x;
272 if(finalPosition.y < 0.5f)
274 finalPosition.y += size.y;
278 finalPosition.y -= size.y;
282 return finalPosition;
285 bool mVertical; ///< Whether vertical or horizontal.
286 bool mWrap; ///< Whether to wrap this position.
290 * ScrollBarInternal HitSize Constraint
291 * Resizes HitArea to size of the container.
293 struct ScrollBarInternalHitSizeConstraint
296 * @param[in] vertical Whether this constraint controls a vertical scrollbar (true)
297 * or a horizontal one (false)
298 * @param[in] thickness The thickness of the scrollbar
300 ScrollBarInternalHitSizeConstraint(bool vertical,
302 : mVertical(vertical),
303 mThickness(thickness)
308 * Constraint operator
309 * @param[in] current The current HitSize
310 * @param[in] scrollDirectionProperty The container's scroll direction.
311 * @param[in] scrollSizeProperty The container's size of viewport.
312 * @return The new ScrollBarInternal Hit Area size is returned.
314 Vector3 operator()(const Vector3& current,
315 const PropertyInput& scrollDirectionProperty,
316 const PropertyInput& scrollSizeProperty)
318 const Vector3& scrollDirection = scrollDirectionProperty.GetVector3();
319 const Toolkit::ControlOrientation::Type& orientation = static_cast<Toolkit::ControlOrientation::Type>(scrollDirection.z);
320 Vector3 size = scrollSizeProperty.GetVector3();
322 Vector3 mask; // Mask size aspect of hit area.
323 Vector3 offset; // Add Offset size.
325 if( (mVertical && Toolkit::IsVertical(orientation)) || (!mVertical && Toolkit::IsHorizontal(orientation)) )
327 mask = Vector3::YAXIS;
328 offset = Vector3::XAXIS * mThickness;
332 mask = Vector3::XAXIS;
333 offset = Vector3::YAXIS * mThickness;
336 return size * mask + offset;
339 bool mVertical; ///< Whether vertical or horizontal.
340 float mThickness; ///< Thickness of the scroll bar
343 } // unnamed namespace
357 using namespace Dali;
364 TypeRegistration mType( typeid(Toolkit::ScrollBarInternal), typeid(Toolkit::ScrollComponent), Create );
368 ScrollBarInternal::ScrollBarInternal(Toolkit::Scrollable& container, bool vertical)
369 : mContainer(static_cast<Toolkit::Internal::Scrollable&>(container.GetImplementation())),
371 mAxisMask(vertical ? Vector3::YAXIS : Vector3::XAXIS),
374 Image sliderImage = ResourceImage::New( BAR_TAB_IMAGE_PATH );
376 mSlider = ImageActor::New( sliderImage );
377 mSlider.SetParentOrigin( ParentOrigin::TOP_LEFT );
378 mSlider.SetAnchorPoint( AnchorPoint::CENTER );
379 mSlider.SetSize( BAR_TAB_SIZE );
380 mSlider.SetStyle( ImageActor::STYLE_NINE_PATCH );
381 mSlider.SetNinePatchBorder( BAR_TAB_NINE_PATCH_BORDER );
383 // A duplicate Slider should appear 1 domain away from the original Slider
384 mSliderWrap = ImageActor::New( sliderImage );
385 mSliderWrap.SetParentOrigin( ParentOrigin::TOP_LEFT );
386 mSliderWrap.SetAnchorPoint( AnchorPoint::CENTER );
387 mSliderWrap.SetSize( BAR_TAB_SIZE );
388 mSliderWrap.SetStyle( ImageActor::STYLE_NINE_PATCH );
389 mSliderWrap.SetNinePatchBorder( BAR_TAB_NINE_PATCH_BORDER );
391 // target the container to observe for scrolling
392 Actor target = mContainer.Self();
393 Constraint constraint = Constraint::New<bool>( Actor::Property::VISIBLE,
394 Source( target, vertical ? target.GetPropertyIndex(Scrollable::SCROLLABLE_CAN_SCROLL_VERTICAL) : target.GetPropertyIndex(Scrollable::SCROLLABLE_CAN_SCROLL_HORIZONTAL)),
395 ScrollBarInternalVisibilityConstraint );
396 mSlider.ApplyConstraint( constraint );
397 mSliderWrap.ApplyConstraint( constraint );
399 constraint = Constraint::New<Vector3>( Actor::Property::SIZE,
400 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
401 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
402 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME ) ),
403 Source( target, Actor::Property::SIZE ),
404 ScrollBarInternalSizeConstraint( vertical ) );
405 mSlider.ApplyConstraint( constraint );
406 mSliderWrap.ApplyConstraint( constraint );
408 constraint = Constraint::New<Quaternion>( Actor::Property::ROTATION,
409 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME ) ),
410 ScrollBarInternalRotationConstraint( vertical ) );
411 mSlider.ApplyConstraint( constraint );
412 mSliderWrap.ApplyConstraint( constraint );
414 constraint = Constraint::New<Vector3>( Actor::Property::POSITION,
415 Source( mSlider, Actor::Property::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::Property::SIZE ),
421 ScrollBarInternalPositionConstraint(vertical) );
423 mSlider.ApplyConstraint( constraint );
425 constraint = Constraint::New<Vector3>( Actor::Property::POSITION,
426 Source( mSlider, Actor::Property::SIZE),
427 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_RELATIVE_POSITION_PROPERTY_NAME ) ),
428 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
429 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
430 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME ) ),
431 Source( target, Actor::Property::SIZE ),
432 ScrollBarInternalPositionConstraint(vertical, true) );
433 mSliderWrap.ApplyConstraint( constraint );
435 // Add Sliders to internal Actor, to avoid mixing up with regular
436 // Actors added by user.
437 mContainer.AddOverlay( mSlider );
438 mContainer.AddOverlay( mSliderWrap );
439 mContainer.ScrollStartedSignal().Connect( this, &ScrollBarInternal::OnStarted );
440 mContainer.ScrollCompletedSignal().Connect( this, &ScrollBarInternal::OnCompleted );
442 // Hit Area for dragging slider /////////////////////////////////////////////
443 mHitArea = Actor::New();
444 mHitArea.SetPosition(0.0f, 0.0f, 0.2f);
446 mContainer.AddOverlay( mHitArea );
447 constraint = Constraint::New<Vector3>( Actor::Property::SIZE,
448 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME ) ),
449 Source( target, Actor::Property::SIZE ),
450 ScrollBarInternalHitSizeConstraint(vertical, BAR_TAB_SIZE.width) );
451 mHitArea.ApplyConstraint( constraint );
455 mHitArea.SetParentOrigin(ParentOrigin::CENTER_RIGHT);
456 mHitArea.SetAnchorPoint(AnchorPoint::CENTER_RIGHT);
460 mHitArea.SetParentOrigin(ParentOrigin::BOTTOM_CENTER);
461 mHitArea.SetAnchorPoint(AnchorPoint::BOTTOM_CENTER);
464 WaitingContractDelay();
467 ScrollBarInternal::~ScrollBarInternal()
472 void ScrollBarInternal::OnInitialize()
474 EnableGestureDetection(Gesture::Type(Gesture::Pan));
477 void ScrollBarInternal::OnDisconnect()
479 // Disconnect all connected callback functions.
480 mContainer.RemoveOverlay( mSlider );
481 mContainer.RemoveOverlay( mSliderWrap );
484 void ScrollBarInternal::OnPanGesture(Actor actor, PanGesture gesture)
486 switch(gesture.state)
488 case Gesture::Started:
492 mScrollStart = mContainer.GetCurrentScrollPosition();
493 mGestureDisplacement = Vector3::ZERO;
496 case Gesture::Continuing:
498 Vector3 delta(gesture.displacement.x, gesture.displacement.y, 0.0f);
499 mGestureDisplacement+=delta;
501 Vector3 size = mContainer.Self().GetCurrentSize();
502 Vector3 span = size - Vector3(BAR_TAB_SIZE.y, BAR_TAB_SIZE.y, 1.0f);
503 Vector3 domainSize = mContainer.GetDomainSize();
505 Vector3 position = mScrollStart + mGestureDisplacement * mAxisMask * domainSize / span;
506 mContainer.ScrollTo(position, 0.0f);
517 void ScrollBarInternal::OnStarted(const Vector3& position)
519 // TODO: Need to disable this for the scrollbar which isn't being scrolled.
527 void ScrollBarInternal::OnCompleted(const Vector3& position)
533 WaitingContractDelay();
537 bool ScrollBarInternal::OnContractDelayExpired()
549 void ScrollBarInternal::Show()
551 // Cancel any animation
558 mAnimation = Animation::New( BAR_SHOW_TIME );
559 mAnimation.OpacityTo( mSlider, 1.0f, AlphaFunctions::EaseIn );
560 mAnimation.OpacityTo( mSliderWrap, 1.0f, AlphaFunctions::EaseIn );
566 void ScrollBarInternal::Hide()
568 // Cancel any animation
575 mAnimation = Animation::New( BAR_HIDE_TIME );
576 mAnimation.OpacityTo( mSlider, 0.0f, AlphaFunctions::EaseIn );
577 mAnimation.OpacityTo( mSliderWrap, 0.0f, AlphaFunctions::EaseIn );
581 void ScrollBarInternal::CreateTimer()
585 // Create timer for contract delay
586 mTimer = Timer::New( BAR_CONTRACT_DELAY * SECOND_UNIT );
587 mTimer.TickSignal().Connect( this, &ScrollBarInternal::OnContractDelayExpired );
591 void ScrollBarInternal::DestructTimer()
596 mTimer.TickSignal().Disconnect( this, &ScrollBarInternal::OnContractDelayExpired );
601 void ScrollBarInternal::WaitingContractDelay()
607 Toolkit::ScrollBarInternal ScrollBarInternal::New(Toolkit::Scrollable& container, bool vertical)
609 // Create the implementation, temporarily owned by this handle on stack
610 IntrusivePtr< ScrollBarInternal > impl = new ScrollBarInternal( container, vertical );
612 // Pass ownership to CustomActor handle
613 Toolkit::ScrollBarInternal handle( *impl );
615 // Second-phase init of the implementation
616 // This can only be done after the CustomActor connection has been made...
622 } // namespace Internal
624 } // namespace Toolkit