Author Topic: Scrollview convenience function  (Read 3104 times)

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Scrollview convenience function
« on: August 17, 2015, 05:38:30 AM »
i couldnt find an *easy* way to retrieve the top/bottom local position of a scrollview.

any chance there is something hidden that returns these values?

hack of the SetDragAmount method:

  1. public Vector3 GetScrollviewPosition(int x, int y)
  2. {
  3.         // (0, 0) is the top-left corner, (1, 1) is the bottom-right.
  4.         //float x = 0;
  5.         //float y = 1;
  6.  
  7.         UIPanel mPanel = scrollview.panel;
  8.  
  9.         //DisableSpring();
  10.  
  11.         Bounds b = scrollview.bounds;
  12.         if (b.min.x == b.max.x || b.min.y == b.max.y) return Vector3.zero;
  13.  
  14.         Vector4 clip = mPanel.finalClipRegion;
  15.  
  16.         float hx = clip.z * 0.5f;
  17.         float hy = clip.w * 0.5f;
  18.         float left = b.min.x + hx;
  19.         float right = b.max.x - hx;
  20.         float bottom = b.min.y + hy;
  21.         float top = b.max.y - hy;
  22.  
  23.         if (mPanel.clipping == UIDrawCall.Clipping.SoftClip)
  24.         {
  25.                 left -= mPanel.clipSoftness.x;
  26.                 right += mPanel.clipSoftness.x;
  27.                 bottom -= mPanel.clipSoftness.y;
  28.                 top += mPanel.clipSoftness.y;
  29.         }
  30.  
  31.         // Calculate the offset based on the scroll value
  32.         float ox = Mathf.Lerp(left, right, x);
  33.         float oy = Mathf.Lerp(top, bottom, y);
  34.  
  35.         // Update the position
  36.         //if (!updateScrollbars)
  37.         //{
  38.         Vector3 pos = scrollview.transform.localPosition;
  39.         //if (canMoveHorizontally) pos.x += clip.x - ox;
  40.         pos.y += clip.y - oy;//if (canMoveVertically)
  41.         return pos;
  42.         //}
  43. }
  44.  

edit:  is there a way to detect scrollbar "dragstart".  there isnt a tandem event to onDragFinished...  id prefer not to use uicamera for this.
« Last Edit: August 17, 2015, 08:14:41 PM by devomage »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scrollview convenience function
« Reply #1 on: August 18, 2015, 05:09:05 PM »
Scroll view uses a panel, panel derives from a rect that has bounds like any widget. UIRect.localCorners tells you the local coordinates.

OnDragStart/OnDragEnd events are sent to whatever you are dragging.