Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Christian Tellefsen on April 29, 2013, 02:16:41 PM

Title: UICenterOnChild does not check UIDraggablePanel.restrictWithinPanel
Post by: Christian Tellefsen on April 29, 2013, 02:16:41 PM
Hi,

I have an UIDraggablePanel with restrictWithinPanel set to true. The clipping size is set to exactly fill the screen. This works fine, I can drag the panel around, and it never goes out of bounds.
 
I then added UICenterOnChild to the same GameObject.

This can make the UIDraggablePanel no longer restricted to be within its bounds. If UICenterOnChild centers on a child that is too close to the edge of the panel, it will cause the panel to move out of bounds to center on the child. I would like to constrain the position SpringPanel moves to so that restrictWithinPanel is still honored.

Does anyone know how I can compute an alternate position to move to that does not cause the panel to move out of bounds?

Cheers,
Christian Tellefsen.
Title: Re: UICenterOnChild does not check UIDraggablePanel.restrictWithinPanel
Post by: Nicki on April 29, 2013, 05:46:11 PM
I suggest you look inside the UICenterOnChild and make your own version that accounts for the width of the cliprange (or height).
Title: Re: UICenterOnChild does not check UIDraggablePanel.restrictWithinPanel
Post by: Christian Tellefsen on May 06, 2013, 07:59:36 AM
Thanks, I figured it out. Here's my code:

  1. UIPanel panel = mDrag.panel;
  2. Vector4 clip = panel.clipRange;
  3. Vector3 posToCenter = mCenteredObject.transform.localPosition;
  4. Bounds panelBounds = NGUIMath.CalculateRelativeWidgetBounds(panel.transform);
  5. Bounds targetBounds = new Bounds(posToCenter, new Vector3(clip.z, clip.w));
  6. Vector3 offset = NGUIMath.ConstrainRect(targetBounds.min, targetBounds.max, panelBounds.min, panelBounds.max).ToVector3();
  7. Vector3 constrainedTargetPos = posToCenter + offset;
  8. SpringPanel.Begin(mDrag.gameObject, -constrainedTargetPos, 8.0f).onFinished = onFinished;
  9.