Author Topic: UICenterOnChild feature request  (Read 7576 times)

Markov

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
UICenterOnChild feature request
« on: December 06, 2013, 08:26:00 AM »
Hi!
I'm using UICenterOnChild to swipe between map lists in our game. This objects are big and if I want to see the next list I have to slide too much range (red arrow on picture). This is very invonvinient on tablets and PC.


May I ask you to add functionality in UICenterOnChild to set this range?(like on blue arrow)
« Last Edit: December 06, 2013, 08:38:49 AM by Markov »

UncleAcid

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 51
    • View Profile
Re: UICenterOnChild feature request
« Reply #1 on: December 06, 2013, 12:40:47 PM »
Add the following variables to UICenterOnChild:

  1.     public bool allowFlickToNext = false;
  2.     public float flickThreshold = 50f; // 50f has seemed to work well in my cases
  3.  

and then replace the Recenter function with the following:

  1.     public void Recenter ()
  2.         {
  3.                 if (mDrag == null)
  4.                 {
  5.                         mDrag = NGUITools.FindInParents<UIScrollView>(gameObject);
  6.  
  7.                         if (mDrag == null)
  8.                         {
  9.                                 Debug.LogWarning(GetType() + " requires " + typeof(UIScrollView) + " on a parent object in order to work", this);
  10.                                 enabled = false;
  11.                                 return;
  12.                         }
  13.                         else
  14.                         {
  15.                                 mDrag.onDragFinished = OnDragFinished;
  16.                                
  17.                                 if (mDrag.horizontalScrollBar != null)
  18.                                         mDrag.horizontalScrollBar.onDragFinished = OnDragFinished;
  19.  
  20.                                 if (mDrag.verticalScrollBar != null)
  21.                                         mDrag.verticalScrollBar.onDragFinished = OnDragFinished;
  22.                         }
  23.                 }
  24.                 if (mDrag.panel == null) return;
  25.  
  26.                 // Calculate the panel's center in world coordinates
  27.                 Vector3[] corners = mDrag.panel.worldCorners;
  28.                 Vector3 panelCenter = (corners[2] + corners[0]) * 0.5f;
  29.  
  30.                 // Offset this value by the momentum
  31.                 Vector3 pickingPoint = panelCenter - mDrag.currentMomentum * (mDrag.momentumAmount * 0.1f);
  32.                 mDrag.currentMomentum = Vector3.zero;
  33.  
  34.                 float min = float.MaxValue;
  35.                 Transform closest = null;
  36.                 Transform trans = transform;
  37.                 int index = 0;
  38.  
  39.                 // Determine the closest child
  40.                 for (int i = 0, imax = trans.childCount; i < imax; ++i)
  41.                 {
  42.                         Transform t = trans.GetChild(i);
  43.                         float sqrDist = Vector3.SqrMagnitude(t.position - pickingPoint);
  44.  
  45.                         if (sqrDist < min)
  46.                         {
  47.                                 min = sqrDist;
  48.                                 closest = t;
  49.                                 index = i;
  50.                         }
  51.                 }
  52.                 if(allowFlickToNext && UICamera.currentTouch != null){
  53.                         if(UICamera.currentTouch.totalDelta.x > flickThreshold && centeredObject.transform == trans.GetChild(index)){
  54.                                 if(index - 1 >= 0){
  55.                                         closest = trans.GetChild(index - 1);
  56.                                 }
  57.                         }
  58.                         else if(UICamera.currentTouch.totalDelta.x < -flickThreshold && centeredObject.transform == trans.GetChild(index))
  59.                         {
  60.                                 if(index + 1 < trans.childCount){
  61.                                         closest = trans.GetChild(index + 1);
  62.                                 }
  63.                         }
  64.                 }
  65.  
  66.                 CenterOn(closest, panelCenter);
  67.         }
  68.  

All these changes do is check to see if you "flicked" the panel in either direction but didn't swipe (swiping far enough to center on a different child will result in default behaviour). If you "flicked"; the closest child will be the current child and so it sets the closest to the next or previous depending on flick direction.

Markov

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: UICenterOnChild feature request
« Reply #2 on: December 06, 2013, 01:38:26 PM »
Thank you UncleAcid!
I hope this changes will be in the next update too.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UICenterOnChild feature request
« Reply #3 on: December 07, 2013, 12:25:16 AM »
Thanks, UncleAcid. I've used your code as a guide to implement a similar solution. 3.0.7 will have a 'next page threshold' value you can set that works same as your flick threshold. If something other than zero, it will behave like your solution.

UncleAcid

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 51
    • View Profile
Re: UICenterOnChild feature request
« Reply #4 on: December 09, 2013, 10:40:58 AM »
Cool.

Glad I could contribute, no matter how small :)