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/object/type-registry.h>
23 #include <dali/public-api/images/resource-image.h>
26 #include <dali-toolkit/public-api/enums.h>
35 * @param[in] x Input value to be squared
36 * @return Result (x*x)
44 const char* BAR_TAB_IMAGE_PATH = DALI_IMAGE_DIR "popup_scroll.png";
45 const Vector4 BAR_TAB_NINE_PATCH_BORDER(0.0f, 12.0f, 14.0f, 14.0f);
46 const Vector3 BAR_TAB_SIZE(18.0f, 72.0f, 0.0f);
47 const Vector3 BAR_TAB_OFFSET_V(-18.0f, 0.0f, 0.1f);
48 const Vector3 BAR_TAB_OFFSET_H(0.0f, -18.0f, 0.1f);
49 const float BAR_CONTRACT_DELAY(0.8f);
50 const float BAR_SHOW_TIME(0.4f);
51 const float BAR_HIDE_TIME(0.5f);
52 const int SECOND_UNIT(1000);
55 * ScrollBarInternal Visibility Constraint
56 * Returns whether scroll bar is visible
58 bool ScrollBarInternalVisibilityConstraint(const bool& current,
59 const PropertyInput& canScrollProperty)
61 bool canScroll = canScrollProperty.GetBoolean();
66 * ScrollBarInternal Size Constraint
67 * Resize ScrollBarInternal Size depends on both ScrollSize and DomainSize
69 struct ScrollBarInternalSizeConstraint
72 * @param[in] vertical Whether this constraint controls a vertical scrollbar (true)
73 * or a horizontal one (false)
75 ScrollBarInternalSizeConstraint(bool vertical)
82 * @param[in] current The current ScrollBarInternal size
83 * @param[in] scrollMinProperty The container's minimum position.
84 * @param[in] scrollMaxProperty The container's maximum position.
85 * @param[in] scrollDirectionProperty The container's scroll direction.
86 * @param[in] scrollSizeProperty The container's size of viewport.
87 * @return The new ScrollBarInternal position is returned.
89 Vector3 operator()(const Vector3& current,
90 const PropertyInput& scrollMinProperty,
91 const PropertyInput& scrollMaxProperty,
92 const PropertyInput& scrollDirectionProperty,
93 const PropertyInput& scrollSizeProperty)
95 const Vector3& min = scrollMinProperty.GetVector3();
96 const Vector3& max = scrollMaxProperty.GetVector3();
97 const Vector3& scrollDirection = scrollDirectionProperty.GetVector3();
98 const Toolkit::ControlOrientation::Type& orientation = static_cast<Toolkit::ControlOrientation::Type>(scrollDirection.z);
99 const Vector3& size = scrollSizeProperty.GetVector3();
100 const Vector3 domainSize = max - min;
102 if (mVertical && Toolkit::IsVertical(orientation))
104 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);
105 return Vector3( current.width, mod, current.depth );
109 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);
110 return Vector3( current.width, mod, current.depth );
114 bool mVertical; ///< Whether vertical or horizontal
118 * ScrollBarInternal rotation Constraint
119 * Rotate ScrollBarInternal depends on the scroll direction
121 struct ScrollBarInternalRotationConstraint
124 * @param[in] vertical Whether this constraint controls a vertical scrollbar (true)
125 * or a horizontal one (false)
127 ScrollBarInternalRotationConstraint(bool vertical)
128 : mVertical(vertical)
133 * Constraint operator
134 * @param[in] current The current ScrollBarInternal rotation
135 * @param[in] scrollDirectionProperty The container's scroll direction.
136 * @return The new ScrollBarInternal rotation is returned.
138 Quaternion operator()(const Quaternion& current,
139 const PropertyInput& scrollDirectionProperty)
141 const Vector3& scrollDirection = scrollDirectionProperty.GetVector3();
142 const Toolkit::ControlOrientation::Type& orientation = static_cast<Toolkit::ControlOrientation::Type>(scrollDirection.z);
144 if( (mVertical && Toolkit::IsVertical(orientation)) || (!mVertical && Toolkit::IsHorizontal(orientation)) )
146 return Quaternion(0.0f, Vector3::ZAXIS);
150 return Quaternion(0.5f * Math::PI, Vector3::ZAXIS);
154 bool mVertical; ///< Whether vertical or horizontal
158 * ScrollBarInternal Position Constraint
159 * Positions the scroll bar to reflect the current scroll position
162 struct ScrollBarInternalPositionConstraint
165 * @param[in] vertical Whether this constraint controls a vertical scrollbar (true)
166 * or a horizontal one (false)
167 * @param[in] wrap Whether to base scrollbar on original position or wrapped position
169 ScrollBarInternalPositionConstraint(bool vertical, bool wrap = false)
170 : mVertical(vertical),
176 * Constraint operator
177 * @param[in] current The current ScrollBarInternal position
178 * @param[in] scrollBarSizeProperty ScrollBarInternal size
179 * @param[in] scrollRelativePositionProperty The container's relative position (from 0.0 -> 1.0 in each axis)
180 * @param[in] scrollMinProperty The container's minimum position.
181 * @param[in] scrollMaxProperty The container's maximum position.
182 * @param[in] scrollDirectionProperty The container's scroll direction.
183 * @param[in] scrollSizeProperty The container's size of viewport.
184 * @return The new ScrollBarInternal position is returned.
186 Vector3 operator()(const Vector3& current,
187 const PropertyInput& scrollBarSizeProperty,
188 const PropertyInput& scrollRelativePositionProperty,
189 const PropertyInput& scrollMinProperty,
190 const PropertyInput& scrollMaxProperty,
191 const PropertyInput& scrollDirectionProperty,
192 const PropertyInput& scrollSizeProperty)
194 Vector3 barSize = scrollBarSizeProperty.GetVector3();
195 Vector3 relativePosition = scrollRelativePositionProperty.GetVector3();
196 Vector3 size = scrollSizeProperty.GetVector3();
197 const Vector3& min = scrollMinProperty.GetVector3();
198 const Vector3& max = scrollMaxProperty.GetVector3();
199 const Vector3& scrollDirection = scrollDirectionProperty.GetVector3();
200 const Toolkit::ControlOrientation::Type& orientation = static_cast<Toolkit::ControlOrientation::Type>(scrollDirection.z);
202 Vector3 domainSize = max - min;
203 domainSize.x = fabsf(domainSize.x);
204 domainSize.y = fabsf(domainSize.y);
207 Vector3 mask; // Mask movement aspect of scroll bar
208 Vector3 relativeOffset; // base position of scroll bar in relation to the container
209 Vector3 absoluteOffset; // absolute offset position of scroll bar
215 case Toolkit::ControlOrientation::Up:
217 mask = Vector3::YAXIS;
218 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.
219 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 );
222 case Toolkit::ControlOrientation::Left:
224 mask = Vector3::XAXIS;
225 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.
226 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 );
229 case Toolkit::ControlOrientation::Down:
231 mask = Vector3::YAXIS;
232 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.
233 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 );
236 case Toolkit::ControlOrientation::Right:
238 mask = Vector3::XAXIS;
239 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.
240 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 );
247 mask = Vector3::XAXIS;
248 relativeOffset = Vector3(0.0f, 1.0f, 0.0f); // Bottom side of stage.
249 absoluteOffset = BAR_TAB_OFFSET_H + Vector3( barSize.height * 0.5f, barSize.width * 0.5f, 1.0f );
252 Vector3 maskedRelativePosition = Toolkit::IsVertical(orientation) ? Vector3(relativePosition.x * (size.x-barSize.y), relativePosition.y * (size.y-barSize.y), 0.0f) * mask
253 : Vector3(relativePosition.y * (size.x-barSize.y), relativePosition.x * (size.y-barSize.y), 0.0f) * mask;
255 Vector3 finalPosition = relativeOffset * size + absoluteOffset + maskedRelativePosition;
257 // If Wrapped Slider, then position 1 domain either before or after current slider.
260 if(finalPosition.x < 0.5f)
262 finalPosition.x += size.x;
266 finalPosition.x -= size.x;
269 if(finalPosition.y < 0.5f)
271 finalPosition.y += size.y;
275 finalPosition.y -= size.y;
279 return finalPosition;
282 bool mVertical; ///< Whether vertical or horizontal.
283 bool mWrap; ///< Whether to wrap this position.
287 * ScrollBarInternal HitSize Constraint
288 * Resizes HitArea to size of the container.
290 struct ScrollBarInternalHitSizeConstraint
293 * @param[in] vertical Whether this constraint controls a vertical scrollbar (true)
294 * or a horizontal one (false)
295 * @param[in] thickness The thickness of the scrollbar
297 ScrollBarInternalHitSizeConstraint(bool vertical,
299 : mVertical(vertical),
300 mThickness(thickness)
305 * Constraint operator
306 * @param[in] current The current HitSize
307 * @param[in] scrollDirectionProperty The container's scroll direction.
308 * @param[in] scrollSizeProperty The container's size of viewport.
309 * @return The new ScrollBarInternal Hit Area size is returned.
311 Vector3 operator()(const Vector3& current,
312 const PropertyInput& scrollDirectionProperty,
313 const PropertyInput& scrollSizeProperty)
315 const Vector3& scrollDirection = scrollDirectionProperty.GetVector3();
316 const Toolkit::ControlOrientation::Type& orientation = static_cast<Toolkit::ControlOrientation::Type>(scrollDirection.z);
317 Vector3 size = scrollSizeProperty.GetVector3();
319 Vector3 mask; // Mask size aspect of hit area.
320 Vector3 offset; // Add Offset size.
322 if( (mVertical && Toolkit::IsVertical(orientation)) || (!mVertical && Toolkit::IsHorizontal(orientation)) )
324 mask = Vector3::YAXIS;
325 offset = Vector3::XAXIS * mThickness;
329 mask = Vector3::XAXIS;
330 offset = Vector3::YAXIS * mThickness;
333 return size * mask + offset;
336 bool mVertical; ///< Whether vertical or horizontal.
337 float mThickness; ///< Thickness of the scroll bar
340 } // unnamed namespace
354 using namespace Dali;
361 TypeRegistration mType( typeid(Toolkit::ScrollBarInternal), typeid(Toolkit::ScrollComponent), Create );
365 ScrollBarInternal::ScrollBarInternal(Toolkit::Scrollable& container, bool vertical)
366 : mContainer(static_cast<Toolkit::Internal::Scrollable&>(container.GetImplementation())),
368 mAxisMask(vertical ? Vector3::YAXIS : Vector3::XAXIS),
371 Image sliderImage = ResourceImage::New( BAR_TAB_IMAGE_PATH );
373 mSlider = ImageActor::New( sliderImage );
374 mSlider.SetParentOrigin( ParentOrigin::TOP_LEFT );
375 mSlider.SetAnchorPoint( AnchorPoint::CENTER );
376 mSlider.SetSize( BAR_TAB_SIZE );
377 mSlider.SetStyle( ImageActor::STYLE_NINE_PATCH );
378 mSlider.SetNinePatchBorder( BAR_TAB_NINE_PATCH_BORDER );
380 // A duplicate Slider should appear 1 domain away from the original Slider
381 mSliderWrap = ImageActor::New( sliderImage );
382 mSliderWrap.SetParentOrigin( ParentOrigin::TOP_LEFT );
383 mSliderWrap.SetAnchorPoint( AnchorPoint::CENTER );
384 mSliderWrap.SetSize( BAR_TAB_SIZE );
385 mSliderWrap.SetStyle( ImageActor::STYLE_NINE_PATCH );
386 mSliderWrap.SetNinePatchBorder( BAR_TAB_NINE_PATCH_BORDER );
388 // target the container to observe for scrolling
389 Actor target = mContainer.Self();
390 Constraint constraint = Constraint::New<bool>( Actor::VISIBLE,
391 Source( target, vertical ? target.GetPropertyIndex(Scrollable::SCROLLABLE_CAN_SCROLL_VERTICAL) : target.GetPropertyIndex(Scrollable::SCROLLABLE_CAN_SCROLL_HORIZONTAL)),
392 ScrollBarInternalVisibilityConstraint );
393 mSlider.ApplyConstraint( constraint );
394 mSliderWrap.ApplyConstraint( constraint );
396 constraint = Constraint::New<Vector3>( Actor::SIZE,
397 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
398 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
399 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME ) ),
400 Source( target, Actor::SIZE ),
401 ScrollBarInternalSizeConstraint( vertical ) );
402 mSlider.ApplyConstraint( constraint );
403 mSliderWrap.ApplyConstraint( constraint );
405 constraint = Constraint::New<Quaternion>( Actor::ROTATION,
406 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME ) ),
407 ScrollBarInternalRotationConstraint( vertical ) );
408 mSlider.ApplyConstraint( constraint );
409 mSliderWrap.ApplyConstraint( constraint );
411 constraint = Constraint::New<Vector3>( Actor::POSITION,
412 Source( mSlider, Actor::SIZE),
413 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_RELATIVE_POSITION_PROPERTY_NAME ) ),
414 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
415 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
416 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME ) ),
417 Source( target, Actor::SIZE ),
418 ScrollBarInternalPositionConstraint(vertical) );
420 mSlider.ApplyConstraint( constraint );
422 constraint = Constraint::New<Vector3>( Actor::POSITION,
423 Source( mSlider, Actor::SIZE),
424 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_RELATIVE_POSITION_PROPERTY_NAME ) ),
425 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
426 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
427 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME ) ),
428 Source( target, Actor::SIZE ),
429 ScrollBarInternalPositionConstraint(vertical, true) );
430 mSliderWrap.ApplyConstraint( constraint );
432 // Add Sliders to internal Actor, to avoid mixing up with regular
433 // Actors added by user.
434 mContainer.AddOverlay( mSlider );
435 mContainer.AddOverlay( mSliderWrap );
436 mContainer.ScrollStartedSignal().Connect( this, &ScrollBarInternal::OnStarted );
437 mContainer.ScrollCompletedSignal().Connect( this, &ScrollBarInternal::OnCompleted );
439 // Hit Area for dragging slider /////////////////////////////////////////////
440 mHitArea = Actor::New();
441 mHitArea.SetPosition(0.0f, 0.0f, 0.2f);
443 mContainer.AddOverlay( mHitArea );
444 constraint = Constraint::New<Vector3>( Actor::SIZE,
445 Source( target, target.GetPropertyIndex( Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME ) ),
446 Source( target, Actor::SIZE ),
447 ScrollBarInternalHitSizeConstraint(vertical, BAR_TAB_SIZE.width) );
448 mHitArea.ApplyConstraint( constraint );
452 mHitArea.SetParentOrigin(ParentOrigin::CENTER_RIGHT);
453 mHitArea.SetAnchorPoint(AnchorPoint::CENTER_RIGHT);
457 mHitArea.SetParentOrigin(ParentOrigin::BOTTOM_CENTER);
458 mHitArea.SetAnchorPoint(AnchorPoint::BOTTOM_CENTER);
461 WaitingContractDelay();
464 ScrollBarInternal::~ScrollBarInternal()
469 void ScrollBarInternal::OnInitialize()
471 EnableGestureDetection(Gesture::Type(Gesture::Pan));
474 void ScrollBarInternal::OnDisconnect()
476 // Disconnect all connected callback functions.
477 mContainer.RemoveOverlay( mSlider );
478 mContainer.RemoveOverlay( mSliderWrap );
481 void ScrollBarInternal::OnPanGesture(Actor actor, PanGesture gesture)
483 switch(gesture.state)
485 case Gesture::Started:
489 mScrollStart = mContainer.GetCurrentScrollPosition();
490 mGestureDisplacement = Vector3::ZERO;
493 case Gesture::Continuing:
495 Vector3 delta(gesture.displacement.x, gesture.displacement.y, 0.0f);
496 mGestureDisplacement+=delta;
498 Vector3 size = mContainer.Self().GetCurrentSize();
499 Vector3 span = size - Vector3(BAR_TAB_SIZE.y, BAR_TAB_SIZE.y, 1.0f);
500 Vector3 domainSize = mContainer.GetDomainSize();
502 Vector3 position = mScrollStart + mGestureDisplacement * mAxisMask * domainSize / span;
503 mContainer.ScrollTo(position, 0.0f);
514 void ScrollBarInternal::OnStarted(const Vector3& position)
516 // TODO: Need to disable this for the scrollbar which isn't being scrolled.
524 void ScrollBarInternal::OnCompleted(const Vector3& position)
530 WaitingContractDelay();
534 bool ScrollBarInternal::OnContractDelayExpired()
546 void ScrollBarInternal::Show()
548 // Cancel any animation
555 mAnimation = Animation::New( BAR_SHOW_TIME );
556 mAnimation.OpacityTo( mSlider, 1.0f, AlphaFunctions::EaseIn );
557 mAnimation.OpacityTo( mSliderWrap, 1.0f, AlphaFunctions::EaseIn );
563 void ScrollBarInternal::Hide()
565 // Cancel any animation
572 mAnimation = Animation::New( BAR_HIDE_TIME );
573 mAnimation.OpacityTo( mSlider, 0.0f, AlphaFunctions::EaseIn );
574 mAnimation.OpacityTo( mSliderWrap, 0.0f, AlphaFunctions::EaseIn );
578 void ScrollBarInternal::CreateTimer()
582 // Create timer for contract delay
583 mTimer = Timer::New( BAR_CONTRACT_DELAY * SECOND_UNIT );
584 mTimer.TickSignal().Connect( this, &ScrollBarInternal::OnContractDelayExpired );
588 void ScrollBarInternal::DestructTimer()
593 mTimer.TickSignal().Disconnect( this, &ScrollBarInternal::OnContractDelayExpired );
598 void ScrollBarInternal::WaitingContractDelay()
604 Toolkit::ScrollBarInternal ScrollBarInternal::New(Toolkit::Scrollable& container, bool vertical)
606 // Create the implementation, temporarily owned by this handle on stack
607 IntrusivePtr< ScrollBarInternal > impl = new ScrollBarInternal( container, vertical );
609 // Pass ownership to CustomActor handle
610 Toolkit::ScrollBarInternal handle( *impl );
612 // Second-phase init of the implementation
613 // This can only be done after the CustomActor connection has been made...
619 } // namespace Internal
621 } // namespace Toolkit