Ejemplo n.º 1
0
    /// <summary>
    /// Restrict the scroll view's contents to be within the scroll view's bounds.
    /// </summary>

    public bool RestrictWithinBounds(bool instant, bool horizontal, bool vertical)
    {
        if (mPanel == null)
        {
            return(false);
        }

        Bounds  b          = bounds;
        Vector3 constraint = mPanel.CalculateConstrainOffset(b.min, b.max);

        // 检测边界
        bool isTop = false, isBottom = false, isLeft = false, isRight = false;

        mPanel.CheckBorder(ref isTop, ref isBottom, ref isLeft, ref isRight);

        // Debug.LogError("Reset 1" + constraint);
        if (!horizontal)
        {
            constraint.x = 0f;
        }
        if (!vertical)
        {
            constraint.y = 0f;
        }

        mConstraint = constraint;
        if (isRight)
        {
            mConstraint.x = 1;
        }
        if (isLeft)
        {
            mConstraint.x = -1;
        }
        if (isTop)
        {
            mConstraint.y = 1;
        }
        if (isBottom)
        {
            mConstraint.y = -1;
        }
        if (!horizontal)
        {
            mConstraint.x = 0f;
        }
        if (!vertical)
        {
            mConstraint.y = 0f;
        }

        //Debug.LogError("Reset 2" + mConstraint);
        if (constraint.sqrMagnitude > 0.1f)
        {
            if (!instant && dragEffect == DragEffect.MomentumAndSpring)
            {
                // Spring back into place
                Vector3 pos = mTrans.localPosition + constraint;
                pos.x = Mathf.Round(pos.x);
                pos.y = Mathf.Round(pos.y);

                if (backWhenFits && !disableDragIfFits)
                {
                    pos += GetMoveOffsets() - constraint;
                }

                sp          = SpringPanel.Begin(mPanel.gameObject, pos, 13f, sp);
                sp.strength = 8f;
            }
            else
            {
                // Jump back into place
                MoveRelative(constraint);

                // Clear the momentum in the constrained direction
                if (Mathf.Abs(constraint.x) > 0.01f)
                {
                    mMomentum.x = 0;
                }
                if (Mathf.Abs(constraint.y) > 0.01f)
                {
                    mMomentum.y = 0;
                }
                if (Mathf.Abs(constraint.z) > 0.01f)
                {
                    mMomentum.z = 0;
                }
                mScroll = 0f;
            }
            return(true);
        }
        return(false);
    }