Пример #1
0
    public static void EndSideBar()
    {
        SidebarProperties sidebarProperties = sidebarPropertiesStack.Pop();

        // End the ScrollView, Insets and GUIArea
        GUILayout.EndScrollView();
        GUILayout.Space(sidebarProperties.inset);
        GUILayout.EndHorizontal();
        GUILayout.Space(sidebarProperties.inset);
        GUILayout.EndVertical();
        GUILayout.EndArea();
        GUI.EndGroup();

        // Prepare the Slider-Button
        Rect      buttonRect = new Rect();
        Texture2D icon       = null;

        switch (sidebarProperties.sidebarType)
        {
        case SidebarType.LEFT:
            buttonRect = new Rect(
                sidebarProperties.currentRect.xMax,
                sidebarProperties.currentRect.yMin + sidebarProperties.currentRect.height / 2 - sidebarButtonSize.y / 2f,
                sidebarButtonSize.x,
                sidebarButtonSize.y);
            icon = DefaultIconSet.ListLeftRight;
            break;

        case SidebarType.RIGHT:
            buttonRect = new Rect(
                sidebarProperties.currentRect.xMin - sidebarButtonSize.x,
                sidebarProperties.currentRect.yMin + sidebarProperties.currentRect.height / 2 - sidebarButtonSize.y / 2f,
                sidebarButtonSize.x,
                sidebarButtonSize.y);
            icon = DefaultIconSet.ListLeftRight;
            break;

        case SidebarType.TOP:
            buttonRect = new Rect(
                sidebarProperties.currentRect.xMin + sidebarProperties.currentRect.width / 2 - sidebarButtonSize.x / 2f,
                sidebarProperties.currentRect.yMax,
                sidebarButtonSize.x,
                sidebarButtonSize.y);
            icon = DefaultIconSet.ListUpDown;
            break;

        case SidebarType.BOTTOM:
            buttonRect = new Rect(
                sidebarProperties.currentRect.xMin + sidebarProperties.currentRect.width / 2 - sidebarButtonSize.x / 2f,
                sidebarProperties.currentRect.yMin - sidebarButtonSize.y,
                sidebarButtonSize.x,
                sidebarButtonSize.y);
            icon = DefaultIconSet.ListUpDown;
            break;

        default:
            break;
        }

        GUIAreas.Add(sidebarProperties.sidebarName + "_Button", buttonRect);
        // Draw the SliderButton
        GUI.Label(buttonRect, icon);


        // Handle Mouse and Touch-Input
        Event currentEvent = Event.current;

        #region Multitouch preparation
        // Check if the current touchindex is still correct and available
        if (Input.touchCount < sidebarProperties.touchIndex)
        {
            sidebarProperties.touchIndex = int.MaxValue;
        }

        for (int i = 0; i < Input.touches.Length; i++)
        {
            if (buttonRect.Contains(Input.touches[i].position.SwapScreenToWorldPoint()))
            {
                sidebarProperties.touchIndex = i;
            }
        }
        #endregion // Multitouch preparation


        #region Dragging
        bool isTouchMoving = false;
        if (Input.touchCount > sidebarProperties.touchIndex)
        {
            isTouchMoving =
                (buttonRect.Contains(Input.touches[sidebarProperties.touchIndex].position.SwapScreenToWorldPoint()) || sidebarProperties.isDragging) &&
                Input.touches[sidebarProperties.touchIndex].phase == TouchPhase.Moved;
        }

        bool isMouseDown = currentEvent.isMouse &&
                           currentEvent.type == EventType.MouseDrag &&
                           currentEvent.button == 0 &&
                           (buttonRect.Contains(currentEvent.mousePosition) || sidebarProperties.isDragging);


        if (isTouchMoving || isMouseDown || sidebarProperties.isDragging)
        {
            sidebarProperties.isDragging = true;

            float newPositionXmax;
            float newPositionXmin;
            float newPositionYmax;
            float newPositionYmin;


            Vector2 delta       = Vector2.zero;
            Vector2 newPosition = Vector2.zero;
            if (isTouchMoving)
            {
                delta       = Input.touches[sidebarProperties.touchIndex].deltaPosition;
                newPosition = Input.touches[sidebarProperties.touchIndex].position.SwapScreenToWorldPoint();
            }
            else
            {
                delta       = currentEvent.delta;
                newPosition = currentEvent.mousePosition;
            }

            if (delta != Vector2.zero)
            {
                switch (sidebarProperties.sidebarType)
                {
                case SidebarType.LEFT:
                    newPositionXmax = newPosition.x;
                    if (newPositionXmax >= sidebarProperties.openRect.xMin && newPositionXmax <= sidebarProperties.openRect.xMax)
                    {
                        sidebarProperties.currentRect.xMax = newPositionXmax;
                        sidebarProperties.current          = MathUtils.ParameterLine(sidebarProperties.openRect.xMin, sidebarProperties.openRect.xMax, sidebarProperties.currentRect.xMax);
                    }
                    sidebarProperties.draggingDirection = delta.ResetY().normalized;
                    break;

                case SidebarType.RIGHT:
                    newPositionXmin = newPosition.x;
                    if (newPositionXmin >= sidebarProperties.openRect.xMin && newPositionXmin <= sidebarProperties.openRect.xMax)
                    {
                        sidebarProperties.currentRect.xMin = newPositionXmin;
                        sidebarProperties.current          = MathUtils.ParameterLine(sidebarProperties.openRect.xMax, sidebarProperties.openRect.xMin, sidebarProperties.currentRect.xMin);
                    }
                    sidebarProperties.draggingDirection = delta.ResetY().normalized;
                    break;

                case SidebarType.TOP:
                    newPositionYmax = newPosition.y;
                    if (newPositionYmax >= sidebarProperties.openRect.yMin && newPositionYmax <= sidebarProperties.openRect.yMax)
                    {
                        sidebarProperties.currentRect.yMax = newPositionYmax;
                        sidebarProperties.current          = MathUtils.ParameterLine(sidebarProperties.openRect.yMin, sidebarProperties.openRect.yMax, sidebarProperties.currentRect.yMax);
                    }
                    sidebarProperties.draggingDirection = delta.ResetX().normalized;
                    break;

                case SidebarType.BOTTOM:
                    newPositionYmin = newPosition.y;
                    if (newPositionYmin >= sidebarProperties.openRect.yMin && newPositionYmin <= sidebarProperties.openRect.yMax)
                    {
                        sidebarProperties.currentRect.yMin = newPositionYmin;
                        sidebarProperties.current          = MathUtils.ParameterLine(sidebarProperties.openRect.yMax, sidebarProperties.openRect.yMin, sidebarProperties.currentRect.yMin);
                    }
                    sidebarProperties.draggingDirection = delta.ResetX().normalized;
                    break;

                default:
                    break;
                }
            }
            Debug.Log(delta + " --> DraggingDirection: " + sidebarProperties.draggingDirection);
        }
        #endregion // Dragging



        #region Button clicked or DragMode-Ended
        // If Touch ended inside Button-Rectangle
        bool isTouchEnded = false;
        if (Input.touchCount > sidebarProperties.touchIndex)
        {
            isTouchEnded =
                (buttonRect.Contains(Input.touches[sidebarProperties.touchIndex].position.SwapScreenToWorldPoint()) || sidebarProperties.isDragging) &&
                Input.touches[sidebarProperties.touchIndex].phase == TouchPhase.Ended;
        }

        // If Mouse clicked inside Button-Rectangle
        bool isMouseUp =
            currentEvent.isMouse &&
            currentEvent.type == EventType.MouseUp &&
            currentEvent.button == 0 &&
            (buttonRect.Contains(currentEvent.mousePosition) || sidebarProperties.isDragging);

        // If either mouse ot touch clicked the button
        //    OR DraggingMode has been ended
        if (isTouchEnded || isMouseUp)
        {
            Debug.Log("Ended Touch / Mouse Clicked" + sidebarProperties.current);
            // If we were in DraggingMode
            if (sidebarProperties.isDragging)
            {
                Debug.Log("DraggingDirection: " + sidebarProperties.draggingDirection);
                if (sidebarProperties.draggingDirection == Vector2.zero)
                {
                    // snap to the nearest side
                    //Debug.Log("Setting new Target for " + sidebarProperties.current);
                    if (sidebarProperties.current <= 0.5f)
                    {
                        sidebarProperties.target = 0;
                    }
                    if (sidebarProperties.current > 0.5f)
                    {
                        sidebarProperties.target = 1;
                    }
                    //Debug.Log("---> " + sidebarProperties.target);
                }
                else
                {
                    switch (sidebarProperties.sidebarType)
                    {
                    case SidebarType.LEFT:
                        if (sidebarProperties.draggingDirection == Vector2.right)
                        {
                            sidebarProperties.target = 1;
                        }
                        else
                        {
                            sidebarProperties.target = 0;
                        }
                        break;

                    case SidebarType.RIGHT:
                        if (sidebarProperties.draggingDirection == -Vector2.right)
                        {
                            sidebarProperties.target = 1;
                        }
                        else
                        {
                            sidebarProperties.target = 0;
                        }
                        break;

                    case SidebarType.TOP:
                        if (sidebarProperties.draggingDirection == -Vector2.up)
                        {
                            sidebarProperties.target = 1;
                        }
                        else
                        {
                            sidebarProperties.target = 0;
                        }
                        break;

                    case SidebarType.BOTTOM:
                        if (sidebarProperties.draggingDirection == Vector2.up)
                        {
                            sidebarProperties.target = 1;
                        }
                        else
                        {
                            sidebarProperties.target = 0;
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            else
            {
                // Clicked the button --> open or close the sidebar
                if (sidebarProperties.current <= 0.5f)
                {
                    sidebarProperties.target = 1;
                }
                if (sidebarProperties.current > 0.5f)
                {
                    sidebarProperties.target = 0;
                }
            }

            //Debug.Log("Setting ease TRUE");
            // set to ease --> animate the
            sidebarProperties.ease       = true;
            sidebarProperties.isDragging = false;
        }
        #endregion // Button clicked or DragMode-Ended

        #region Easing the Sidebar
        if (sidebarProperties.ease)
        {
            if (sidebarProperties.easeInType == EasingCurves.EaseType.none)
            {
                sidebarProperties.ease = false;

                if (sidebarProperties.target == 0)
                {
                    sidebarProperties.currentRect = sidebarProperties.closedRect;
                    sidebarProperties.target      = 1;
                }
                else if (sidebarProperties.target == 1)
                {
                    sidebarProperties.currentRect = sidebarProperties.openRect;
                    sidebarProperties.target      = 0;
                }
            }
            else
            {
                if (sidebarProperties.target == 0)
                {
                    if (sidebarProperties.current > 0)
                    {
                        sidebarProperties.current -= Time.deltaTime / sidebarProperties.factor;
                    }
                }
                else if (sidebarProperties.target == 1)
                {
                    if (sidebarProperties.current < 1)
                    {
                        sidebarProperties.current += Time.deltaTime / sidebarProperties.factor;
                    }
                }

                sidebarProperties.current = Mathf.Clamp01(sidebarProperties.current);

                if (sidebarProperties.ease)
                {
                    if (sidebarProperties.target == 1)
                    {
                        sidebarProperties.currentRect = EasingCurves.EaseRect(
                            sidebarProperties.closedRect,
                            sidebarProperties.openRect,
                            sidebarProperties.easeInType,
                            sidebarProperties.current);
                    }
                    else if (sidebarProperties.target == 0)
                    {
                        sidebarProperties.currentRect = EasingCurves.EaseRect(
                            sidebarProperties.closedRect,
                            sidebarProperties.openRect,
                            sidebarProperties.easeOutType,
                            sidebarProperties.current);
                    }
                }

                if (sidebarProperties.current >= 1)
                {
                    sidebarProperties.ease    = false;
                    sidebarProperties.current = 1;
                    sidebarProperties.target  = 0;
                }
                else if (sidebarProperties.current <= 0)
                {
                    sidebarProperties.ease    = false;
                    sidebarProperties.current = 0;
                    sidebarProperties.target  = 1;
                }
            }
        }
        #endregion // Easing the Sidebar
    }
Пример #2
0
    public static Rect BeginSideBar(string sidebarName, SidebarType sidebarType, Rect boundingRect, Rect openRect, bool startStateOpen, float inset, EasingCurves.EaseType easeInType, EasingCurves.EaseType easeOutType, float factor)
    {
        SidebarProperties sidebarProperties = null;

        // If the Sidebar has not been created yet --> create the Sidebar
        if (!sidebars.ContainsKey(sidebarName))
        {
            // create properties instance
            sidebarProperties              = new SidebarProperties();
            sidebarProperties.sidebarName  = sidebarName;
            sidebarProperties.sidebarType  = sidebarType;
            sidebarProperties.inset        = inset;
            sidebarProperties.easeInType   = easeInType;
            sidebarProperties.easeOutType  = easeOutType;
            sidebarProperties.factor       = factor;
            sidebarProperties.boundingRect = boundingRect;


            if (sidebarProperties.boundingRect.Equals(new Rect()))
            {
                sidebarProperties.boundingRect = new Rect(0f, 0f, Screen.width, Screen.height);
            }

            // initialize the Rectangles
            switch (sidebarType)
            {
            case SidebarType.LEFT:
                sidebarProperties.openRect   = new Rect(sidebarProperties.boundingRect.xMin, sidebarProperties.boundingRect.yMin + openRect.yMin, openRect.width, openRect.height);
                sidebarProperties.closedRect = new Rect(sidebarProperties.boundingRect.xMin, sidebarProperties.boundingRect.yMin + openRect.yMin, 0f, openRect.height);
                break;

            case SidebarType.RIGHT:
                sidebarProperties.openRect   = new Rect(sidebarProperties.boundingRect.xMax - openRect.width, sidebarProperties.boundingRect.yMin + openRect.yMin, openRect.width, openRect.height);
                sidebarProperties.closedRect = new Rect(sidebarProperties.boundingRect.xMax, sidebarProperties.boundingRect.yMin + openRect.yMin, 0f, openRect.height);
                break;

            case SidebarType.TOP:
                sidebarProperties.openRect   = new Rect(sidebarProperties.boundingRect.xMin + openRect.xMin, sidebarProperties.boundingRect.yMin, openRect.width, openRect.height);
                sidebarProperties.closedRect = new Rect(sidebarProperties.boundingRect.xMin + openRect.xMin, sidebarProperties.boundingRect.yMin, openRect.width, 0f);
                break;

            case SidebarType.BOTTOM:
                sidebarProperties.openRect   = new Rect(sidebarProperties.boundingRect.xMin + openRect.xMin, sidebarProperties.boundingRect.yMax - openRect.height, openRect.width, openRect.height);
                sidebarProperties.closedRect = new Rect(sidebarProperties.boundingRect.xMin + openRect.xMin, sidebarProperties.boundingRect.yMax, openRect.width, 0f);
                break;

            default:
                break;
            }

            // Initialize withe the desired state after creation
            if (startStateOpen)
            {
                sidebarProperties.currentRect = sidebarProperties.openRect;
                sidebarProperties.open        = 1f;
                sidebarProperties.current     = 1f;
                sidebarProperties.target      = 0f;
            }
            else
            {
                sidebarProperties.currentRect = sidebarProperties.closedRect;
                sidebarProperties.open        = 0f;
                sidebarProperties.current     = 0f;
                sidebarProperties.target      = 1f;
            }

            // Add SidebarProperties to the Dictionary
            sidebars.Add(sidebarName, sidebarProperties);
        }
        else
        {
            // A sidebar of that name already existed --> get its Properties
            sidebars.TryGetValue(sidebarName, out sidebarProperties);


            //// initialize the Rectangles
            switch (sidebarProperties.sidebarType)
            {
            case SidebarType.LEFT:
                sidebarProperties.openRect   = new Rect(sidebarProperties.boundingRect.xMin, sidebarProperties.boundingRect.yMin + openRect.yMin, openRect.width, openRect.height);
                sidebarProperties.closedRect = new Rect(sidebarProperties.boundingRect.xMin, sidebarProperties.boundingRect.yMin + openRect.yMin, 0f, openRect.height);
                break;

            case SidebarType.RIGHT:
                sidebarProperties.openRect   = new Rect(sidebarProperties.boundingRect.xMax - openRect.width, sidebarProperties.boundingRect.yMin + openRect.yMin, openRect.width, openRect.height);
                sidebarProperties.closedRect = new Rect(sidebarProperties.boundingRect.xMax, sidebarProperties.boundingRect.yMin + openRect.yMin, 0f, openRect.height);
                break;

            case SidebarType.TOP:
                sidebarProperties.openRect   = new Rect(sidebarProperties.boundingRect.xMin + openRect.xMin, sidebarProperties.boundingRect.yMin, openRect.width, openRect.height);
                sidebarProperties.closedRect = new Rect(sidebarProperties.boundingRect.xMin + openRect.xMin, sidebarProperties.boundingRect.yMin, openRect.width, 0f);
                break;

            case SidebarType.BOTTOM:
                sidebarProperties.openRect   = new Rect(sidebarProperties.boundingRect.xMin + openRect.xMin, sidebarProperties.boundingRect.yMax - openRect.height, openRect.width, openRect.height);
                sidebarProperties.closedRect = new Rect(sidebarProperties.boundingRect.xMin + openRect.xMin, sidebarProperties.boundingRect.yMax, openRect.width, 0f);
                break;

            default:
                break;
            }
        }


        GUIAreas.Add(sidebarProperties.sidebarName, sidebarProperties.currentRect);
        // Draw the current Rectangle
        CustomGUIUtils.DrawFrameBox(sidebarProperties.currentRect, Color.gray, 3f, Color.black);

        // Start GUIArea with Insets and ScrollView
        GUI.BeginGroup(currentRect);
        GUILayout.BeginArea(sidebarProperties.currentRect);
        GUILayout.BeginVertical();
        GUILayout.Space(sidebarProperties.inset);
        GUILayout.BeginHorizontal();
        GUILayout.Space(sidebarProperties.inset);
        sidebarProperties.scrollPosition = GUILayout.BeginScrollView(sidebarProperties.scrollPosition);

        sidebarPropertiesStack.Push(sidebarProperties);
        return(sidebarProperties.currentRect);
    }