ConstrainTargetToBounds() public method

Constrain the current target position to be within panel bounds.
public ConstrainTargetToBounds ( Transform target, Bounds &targetBounds, bool immediate ) : bool
target Transform
targetBounds Bounds
immediate bool
return bool
Esempio n. 1
0
    public void Reposition()
    {
        if (Application.isPlaying && !mInitDone && NGUITools.GetActive(this))
        {
            mReposition = true;
            return;
        }

        if (!mInitDone)
        {
            Init();
        }

        mReposition = false;
        Transform myTrans = transform;

        int x = 0;
        int y = 0;

        if (sorted)
        {
            List <Transform> list = new List <Transform>();

            for (int i = 0; i < myTrans.childCount; ++i)
            {
                Transform t = myTrans.GetChild(i);
                if (t && (!hideInactive || NGUITools.GetActive(t.gameObject)))
                {
                    list.Add(t);
                }
            }
            list.Sort(SortByName);

            for (int i = 0, imax = list.Count; i < imax; ++i)
            {
                Transform t = list[i];

                if (!NGUITools.GetActive(t.gameObject) && hideInactive)
                {
                    continue;
                }

                float   depth = t.localPosition.z;
                Vector3 pos   = (arrangement == Arrangement.Horizontal) ?
                                new Vector3(cellWidth * x, -cellHeight * y, depth) :
                                new Vector3(cellWidth * y, -cellHeight * x, depth);

                if (animateSmoothly && Application.isPlaying)
                {
                    SpringPosition.Begin(t.gameObject, pos, 15f);
                }
                else
                {
                    t.localPosition = pos;
                }

                if (++x >= maxPerLine && maxPerLine > 0)
                {
                    x = 0;
                    ++y;
                }
            }
        }
        else
        {
            for (int i = 0; i < myTrans.childCount; ++i)
            {
                Transform t = myTrans.GetChild(i);

                if (!NGUITools.GetActive(t.gameObject) && hideInactive)
                {
                    continue;
                }

                float   depth = t.localPosition.z;
                Vector3 pos   = (arrangement == Arrangement.Horizontal) ?
                                new Vector3(cellWidth * x, -cellHeight * y, depth) :
                                new Vector3(cellWidth * y, -cellHeight * x, depth);

                if (animateSmoothly && Application.isPlaying)
                {
                    SpringPosition.Begin(t.gameObject, pos, 15f);
                }
                else
                {
                    t.localPosition = pos;
                }

                if (++x >= maxPerLine && maxPerLine > 0)
                {
                    x = 0;
                    ++y;
                }
            }
        }

        if (keepWithinPanel && mPanel != null)
        {
            mPanel.ConstrainTargetToBounds(myTrans, true);
        }

        if (onReposition != null)
        {
            onReposition();
        }
    }
    public void Reposition()
    {
        if (Application.isPlaying && !mStarted)
        {
            mReposition = true;
            return;
        }

        //if (!mInitDone) Init();

        mReposition = false;
        Transform myTrans = transform;

        /*
         * int x = 0;
         * int y = 0;
         * //if (sorted)
         * if( false )
         * {
         *      List<Transform> list = new List<Transform>();
         *
         *      for (int i = 0; i < myTrans.childCount; ++i)
         *      {
         *              Transform t = myTrans.GetChild(i);
         *              if (t && (!hideInactive || NGUITools.GetActive(t.gameObject))) list.Add(t);
         *      }
         *      list.Sort(SortByName);
         *
         *      for (int i = 0, imax = list.Count; i < imax; ++i)
         *      {
         *              Transform t = list[i];
         *
         *              if (!NGUITools.GetActive(t.gameObject) && hideInactive) continue;
         *
         *              float depth = t.localPosition.z;
         *              Vector3 pos = (arrangement == Arrangement.Horizontal) ?
         *                      new Vector3(cellWidth * x, -cellHeight * y, depth) :
         *                      new Vector3(cellWidth * y, -cellHeight * x, depth);
         *
         *              if (animateSmoothly && Application.isPlaying)
         *              {
         *                      SpringPosition.Begin(t.gameObject, pos, 15f);
         *              }
         *              else t.localPosition = pos;
         *
         *              if (++x >= maxPerLine && maxPerLine > 0)
         *              {
         *                      x = 0;
         ++y;
         *              }
         *      }
         * /*		}
         * else
         * {
         *      /*for (int i = 0; i < myTrans.childCount; ++i)
         *      {
         *              Transform t = myTrans.GetChild(i);
         *
         *              if (!NGUITools.GetActive(t.gameObject) && hideInactive) continue;
         *
         *              float depth = t.localPosition.z;
         *              Vector3 pos = (arrangement == Arrangement.Horizontal) ?
         *                      new Vector3(cellWidth * x, -cellHeight * y, depth) :
         *                      new Vector3(cellWidth * y, -cellHeight * x, depth);
         *
         *              if (animateSmoothly && Application.isPlaying)
         *              {
         *                      SpringPosition.Begin(t.gameObject, pos, 15f);
         *              }
         *              else t.localPosition = pos;
         *
         *              if (++x >= maxPerLine && maxPerLine > 0)
         *              {
         *                      x = 0;
         ++y;
         *              }
         *      }
         * }
         */

        if (mDrag != null)
        {
            mDrag.UpdateScrollbars(true);
            mDrag.RestrictWithinBounds(true);
        }
        else if (mPanel != null)
        {
            mPanel.ConstrainTargetToBounds(myTrans, true);
        }

        if (onReposition != null)
        {
            onReposition();
        }
    }
    public virtual void Reposition()
    {
        if (Application.isPlaying && !mInitDone && NGUITools.GetActive(this))
        {
            mReposition = true;
            return;
        }

        if (!mInitDone)
        {
            Init();
        }

        mReposition = false;
        Transform myTrans = transform;

        int x     = 0;
        int y     = 0;
        int total = 0;      //wilson
        int maxX  = 0;
        int maxY  = 0;

        if (sorting != Sorting.None || sorted)
        {
            list.Clear();
            for (int i = 0; i < myTrans.childCount; ++i)
            {
                Transform t = myTrans.GetChild(i);
                if (t && (!hideInactive || NGUITools.GetActive(t.gameObject)))
                {
                    list.Add(t);
                }
            }

            if (sorting == Sorting.Alphabetic)
            {
                list.Sort(SortByName);
            }
            else if (sorting == Sorting.Horizontal)
            {
                list.Sort(SortHorizontal);
            }
            else if (sorting == Sorting.Vertical)
            {
                list.Sort(SortVertical);
            }
            else
            {
                Sort(list);
            }

            for (int i = 0, imax = list.size; i < imax; ++i)
            {
                Transform t = list[i];

                if (!NGUITools.GetActive(t.gameObject) && hideInactive)
                {
                    continue;
                }

                float   depth = t.localPosition.z;
                Vector3 pos   = Vector3.zero;

                //wilson
                if (arrangement == Arrangement.Mix)
                {
                    pos.x = cellWidth * x + total / (maxPerLine * maxPerRank) * cellWidth * maxPerLine;
                    pos.y = -cellHeight * y;
                    pos.z = depth;
                }
                else if (arrangement == Arrangement.Vertical)
                {
                    pos.x = cellWidth * x;
                    pos.y = -cellHeight * y;
                    pos.z = depth;
                }
                else
                {
                    pos.x = cellWidth * y;
                    pos.y = -cellHeight * x - cellHeight * y;
                    pos.z = depth;
                }

                if (animateSmoothly && Application.isPlaying)
                {
                    SpringPosition.Begin(t.gameObject, pos, 15f).updateScrollView = true;
                }
                else
                {
                    t.localPosition = pos;
                }

                maxX = Mathf.Max(maxX, x);
                maxY = Mathf.Max(maxY, y);

                //wilson
                if (arrangement == Arrangement.Mix)
                {
                    if (++x >= maxPerLine && maxPerLine > 0)
                    {
                        x = 0;
                        if (++y >= maxPerRank && maxPerRank > 0)
                        {
                            y = 0;
                        }
                    }
                }
                else
                {
                    if (++x >= maxPerLine && maxPerLine > 0)
                    {
                        x = 0;
                        ++y;
                    }
                }
                ++total;
            }
        }
        else
        {
            for (int i = 0; i < myTrans.childCount; ++i)
            {
                Transform t = myTrans.GetChild(i);

                if (!NGUITools.GetActive(t.gameObject) && hideInactive)
                {
                    continue;
                }

                float   depth = t.localPosition.z;
                Vector3 pos   = Vector3.zero;

                //wilson
                if (arrangement == Arrangement.Mix)
                {
                    pos.x = cellWidth * x + total / (maxPerLine * maxPerRank) * cellWidth * maxPerLine;
                    pos.y = -cellHeight * y;
                    pos.z = depth;
                }

                else if (arrangement == Arrangement.Vertical)
                {
                    pos.x = cellWidth * x;
                    pos.y = -cellHeight * y;
                    pos.z = depth;
                }
                else
                {
                    pos.x = cellWidth * y;
                    pos.y = -cellHeight * x;
                    pos.z = depth;
                }
                t.localPosition = pos;

                //wilson
                if (arrangement == Arrangement.Mix)
                {
                    if (++x >= maxPerLine && maxPerLine > 0)
                    {
                        x = 0;
                        if (++y >= maxPerRank && maxPerRank > 0)
                        {
                            y = 0;
                        }
                    }
                }
                else
                {
                    if (++x >= maxPerLine && maxPerLine > 0)
                    {
                        x = 0;
                        ++y;
                    }
                }
                ++total;
            }
        }

        // Apply the origin offset
        if (pivot != UIWidget.Pivot.TopLeft)
        {
            Vector2 po = NGUIMath.GetPivotOffset(pivot);

            float fx, fy;

            if (arrangement == Arrangement.Horizontal)
            {
                fx = Mathf.Lerp(0f, maxX * cellWidth, po.x);
                fy = Mathf.Lerp(-maxY * cellHeight, 0f, po.y);
            }
            else
            {
                fx = Mathf.Lerp(0f, maxY * cellWidth, po.x);
                fy = Mathf.Lerp(-maxX * cellHeight, 0f, po.y);
            }

            for (int i = 0; i < myTrans.childCount; ++i)
            {
                Transform t = myTrans.GetChild(i);

                if (!NGUITools.GetActive(t.gameObject) && hideInactive)
                {
                    continue;
                }

                SpringPosition sp = t.GetComponent <SpringPosition>();

                if (sp != null)
                {
                    sp.target.x -= fx;
                    sp.target.y -= fy;
                }
                else
                {
                    Vector3 pos = t.localPosition;
                    pos.x          -= fx;
                    pos.y          -= fy;
                    t.localPosition = pos;
                }
            }
        }

        if (keepWithinPanel && mPanel != null)
        {
            mPanel.ConstrainTargetToBounds(myTrans, true);
        }

        if (onReposition != null)
        {
            onReposition();
        }
    }
Esempio n. 4
0
    public void Reposition()
    {
        if (Application.isPlaying && !mStarted)
        {
            mReposition = true;
            return;
        }

        if (!mInitDone)
        {
            Init();
        }

        mReposition = false;
        Transform myTrans = transform;

        int x      = 0;
        int y      = 0;
        int height = 0;

        List <ChatItemBase> list = OnSortTime();

        for (int i = 0; i < list.Count; ++i)
        {
            ChatItemBase item = list[i];

            if (item == null)
            {
                continue;
            }

            Transform t = item.transform;

            if (!NGUITools.GetActive(t.gameObject) && hideInactive)
            {
                continue;
            }

            float   depth = t.localPosition.z;
            Vector3 vTemp = Vector3.zero;
            vTemp.x = 0;
            vTemp.y = height;
            vTemp.z = depth;
            Vector3 pos = vTemp;
            height += cellHeight;
            if (animateSmoothly && Application.isPlaying)
            {
                SpringPosition.Begin(t.gameObject, pos, 15f);
            }
            else
            {
                t.localPosition = pos;
            }
            if (++x >= maxPerLine && maxPerLine > 0)
            {
                x = 0;
                ++y;
            }
        }

        if (mDrag != null)
        {
            mDrag.UpdateScrollbars(true);

            if (isMainHeight)
            {
                mDrag.RestrictWithinBounds(true);
            }
            else
            {
                mDrag.RestrictWithinBounds(true, false, true);
            }
        }
        else if (mPanel != null)
        {
            //if (isMainHeight)
            mPanel.ConstrainTargetToBounds(myTrans, true);
        }

        if (onReposition != null)
        {
            onReposition();
        }
    }
Esempio n. 5
0
    /// <summary>
    /// Create a plane on which we will be performing the dragging.
    /// </summary>

    void OnPress(bool pressed)
    {
        //Debug.Log ("CLICKED --- 1");
        if (enabled && NGUITools.GetActive(gameObject) && target != null)
        {
            //Debug.Log ("CLICKED --- 2");
            if (pressed)
            {
                isPressed = true;
                EventDelegate.Execute(onPressedBtn);

                //Debug.Log ("CLICKED --- 3");
                if (!mPressed)
                {
                    //Debug.Log ("CLICKED --- 4");
                    // Remove all momentum on press
                    mTouchID = UICamera.currentTouchID;
                    mPressed = true;
                    mStarted = false;
                    CancelMovement();

                    if (restrictWithinPanel && panelRegion == null)
                    {
                        FindPanel();
                    }
                    if (restrictWithinPanel)
                    {
                        UpdateBounds();
                    }

                    // Disable the spring movement
                    CancelSpring();

                    // Create the plane to drag along
                    Transform trans = UICamera.currentCamera.transform;
                    mPlane = new Plane((panelRegion != null ? panelRegion.cachedTransform.rotation : trans.rotation) * Vector3.back, UICamera.lastWorldPosition);
                }
            }
            else if (mPressed && mTouchID == UICamera.currentTouchID)
            {
                isPressed = false;
                if (wasDragged)
                {
                    EventDelegate.Execute(onDragRelease);
                    wasDragged = false;
                }
                else
                {
                    EventDelegate.Execute(onReleasedBtn);
                }

                mPressed = false;

                if (restrictWithinPanel && dragEffect == DragEffect.MomentumAndSpring)
                {
                    if (panelRegion.ConstrainTargetToBounds(target, ref mBounds, false))
                    {
                        CancelMovement();
                    }
                }
            }
        }
    }
Esempio n. 6
0
    /// <summary>
    /// Create a plane on which we will be performing the dragging.
    /// </summary>

    void OnPress(bool pressed)
    {
        if (!enabled)
        {
            return;
        }

        if (UICamera.currentTouchID == -2 || UICamera.currentTouchID == -3)
        {
            return;
        }

        // Unity's physics seems to break when timescale is not quite zero. Raycasts start to fail completely.
        var ts = Time.timeScale;

        if (ts < 0.01f && ts != 0f)
        {
            return;
        }

        if (enabled && NGUITools.GetActive(gameObject) && target != null)
        {
            if (pressed)
            {
                if (!mPressed)
                {
                    // Remove all momentum on press
                    mTouchID = UICamera.currentTouchID;
                    mPressed = true;
                    mStarted = false;
                    CancelMovement();

                    if (restrictWithinPanel && panelRegion == null)
                    {
                        FindPanel();
                    }
                    if (restrictWithinPanel)
                    {
                        UpdateBounds();
                    }

                    // Disable the spring movement
                    CancelSpring();

                    // Create the plane to drag along
                    var trans = UICamera.currentCamera.transform;
                    mPlane = new Plane((panelRegion != null ? panelRegion.cachedTransform.rotation : trans.rotation) * Vector3.back, UICamera.lastWorldPosition);
                }
            }
            else if (mPressed && mTouchID == UICamera.currentTouchID)
            {
                mPressed = false;

                if (restrictWithinPanel && dragEffect == DragEffect.MomentumAndSpring)
                {
                    if (panelRegion.ConstrainTargetToBounds(target, ref mBounds, false))
                    {
                        CancelMovement();
                    }
                }
            }
        }
    }
Esempio n. 7
0
    public virtual void Reposition()
    {
        if (UnityEngine.Application.isPlaying && !mInitDone && NGUITools.GetActive(this))
        {
            mReposition = true;
            return;
        }

        if (!mInitDone)
        {
            Init();
        }

        mReposition = false;
        UnityEngine.Transform myTrans = transform;
        bool noonesort = true;
        int  x         = 0;
        int  y         = 0;

        if (DFReposition)
        {
            DFReposition = false;
            noonesort    = false;
            List <UnityEngine.Transform> list = new List <UnityEngine.Transform>();

            for (int i = 0; i < myTrans.childCount; ++i)
            {
                UnityEngine.Transform t = myTrans.GetChild(i);
                if (t && (!hideInactive || NGUITools.GetActive(t.gameObject)))
                {
                    list.Add(t);
                }
            }
            DFMSort(list);

            for (int i = 0, imax = list.Count; i < imax; ++i)
            {
                UnityEngine.Transform t = list[i];
                t.name = "Item" + i;

                if (!NGUITools.GetActive(t.gameObject) && hideInactive)
                {
                    continue;
                }

                float depth             = t.localPosition.z;
                UnityEngine.Vector3 pos = (arrangement == Arrangement.Horizontal) ?
                                          new UnityEngine.Vector3(cellWidth * x, -cellHeight * y, depth) :
                                          new UnityEngine.Vector3(cellWidth * y, -cellHeight * x, depth);

                if (animateSmoothly && UnityEngine.Application.isPlaying)
                {
                    SpringPosition.Begin(t.gameObject, pos, 15f).updateScrollView = true;
                }
                else
                {
                    t.localPosition = pos;
                }

                if (++x >= maxPerLine && maxPerLine > 0)
                {
                    x = 0;
                    ++y;
                }
            }
        }
        if (sorted && noonesort)
        {
            List <UnityEngine.Transform> list = new List <UnityEngine.Transform>();

            for (int i = 0; i < myTrans.childCount; ++i)
            {
                UnityEngine.Transform t = myTrans.GetChild(i);
                if (t && (!hideInactive || NGUITools.GetActive(t.gameObject)))
                {
                    list.Add(t);
                }
            }
            Sort(list);

            for (int i = 0, imax = list.Count; i < imax; ++i)
            {
                UnityEngine.Transform t = list[i];

                if (!NGUITools.GetActive(t.gameObject) && hideInactive)
                {
                    continue;
                }

                float depth             = t.localPosition.z;
                UnityEngine.Vector3 pos = (arrangement == Arrangement.Horizontal) ?
                                          new UnityEngine.Vector3(cellWidth * x, -cellHeight * y, depth) :
                                          new UnityEngine.Vector3(cellWidth * y, -cellHeight * x, depth);

                if (animateSmoothly && UnityEngine.Application.isPlaying)
                {
                    SpringPosition.Begin(t.gameObject, pos, 15f).updateScrollView = true;
                }
                else
                {
                    t.localPosition = pos;
                }

                if (++x >= maxPerLine && maxPerLine > 0)
                {
                    x = 0;
                    ++y;
                }
            }
        }

        if (keepWithinPanel && mPanel != null)
        {
            mPanel.ConstrainTargetToBounds(myTrans, true);
        }

        if (onReposition != null)
        {
            onReposition();
        }
    }