Beispiel #1
0
    /**
     * Draw a working scale handle in 2d space.
     */
    public static Vector2 ScaleHandle2d(int id, Vector2 position, Vector2 scale, int size)
    {
        Event   e             = Event.current;
        Vector2 mousePosition = e.mousePosition;
        int     width         = size / 4;

        Handles.color = HANDLE_COLOR_UP;
        Handles.DrawLine(position, position - Vector2.up * size * scale.y);

        if (position.y - size > 0f)
        {
            Handles.CubeCap(0,
                            ((Vector3)((position - Vector2.up * scale.y * size))) - Vector3.forward * 16,
                            QuaternionUp,
                            width / 3);
        }

        Handles.color = HANDLE_COLOR_RIGHT;
        Handles.DrawLine(position, position + Vector2.right * size * scale.x);

        if (position.y > 0f)
        {
            Handles.CubeCap(0,
                            ((Vector3)((position + Vector2.right * scale.x * size))) - Vector3.forward * 16,
                            Quaternion.Euler(Vector3.up * 90f),
                            width / 3);
        }

        Handles.color = HANDLE_COLOR_SCALE;
        Handles.CubeCap(0,
                        ((Vector3)position) - Vector3.forward * 16,
                        QuaternionUp,
                        width / 2);

        // If a Tool already is engaged and it's not this one, bail.
        if (currentId >= 0 && currentId != id)
        {
            return(scale);
        }

        Rect handleRectUp     = new Rect(position.x - width / 2, position.y - size - HANDLE_PADDING, width, size + HANDLE_PADDING);
        Rect handleRectRight  = new Rect(position.x, position.y - width / 2, size + 8, width);
        Rect handleRectCenter = new Rect(position.x - width / 2, position.y - width / 2, width, width);

        if (currentId == id)
        {
            switch (e.type)
            {
            case EventType.MouseDrag:
                Vector2 diff = axisConstraint.Mask(mousePosition - initialMousePosition);
                diff.x += size;
                diff.y  = -diff.y;                              // gui space Y is opposite-world
                diff.y += size;
                scale   = diff / size;
                if (axisConstraint == pb_HandleConstraint2D.None)
                {
                    scale.x = Mathf.Min(scale.x, scale.y);
                    scale.y = Mathf.Min(scale.x, scale.y);
                }
                break;

            case EventType.MouseUp:
            case EventType.Ignore:
                currentId = -1;
                break;
            }
        }
        else
        {
            if (e.type == EventType.MouseDown && ((!limitToLeftButton && e.button != MIDDLE_MOUSE_BUTTON) || e.button == LEFT_MOUSE_BUTTON))
            {
                if (handleRectCenter.Contains(mousePosition))
                {
                    currentId            = id;
                    handleOffset         = position - mousePosition;
                    initialMousePosition = mousePosition;
                    axisConstraint       = new pb_HandleConstraint2D(1, 1);
                }
                else
                if (handleRectRight.Contains(mousePosition))
                {
                    currentId            = id;
                    handleOffset         = position - mousePosition;
                    initialMousePosition = mousePosition;
                    axisConstraint       = new pb_HandleConstraint2D(1, 0);
                }
                else if (handleRectUp.Contains(mousePosition))
                {
                    currentId            = id;
                    handleOffset         = position - mousePosition;
                    initialMousePosition = mousePosition;
                    axisConstraint       = new pb_HandleConstraint2D(0, 1);
                }
            }
        }

        return(scale);
    }
Beispiel #2
0
    /**
     * A 2D GUI view position handle.
     * @param id The Handle id.
     * @param position The position in GUI coordinates.
     * @param size How large in pixels to draw this handle.
     */
    public static Vector2 PositionHandle2d(int id, Vector2 position, int size)
    {
        int width = size / 4;

        Rect handleRectUp    = new Rect(position.x - width / 2, position.y - size - HANDLE_PADDING, width, size + HANDLE_PADDING);
        Rect handleRectRight = new Rect(position.x, position.y - width / 2, size, width + HANDLE_PADDING);

        Handles.color = Color.yellow;
        Handles.CircleCap(-1, position, Quaternion.identity, width / 2);
        Handles.color = HANDLE_COLOR_UP;

        // Y Line
        Handles.DrawLine(position, position - Vector2.up * size);

        // Y Cone
        if (position.y - size > 0f)
        {
            Handles.ConeCap(0,
                            ((Vector3)((position - Vector2.up * size))) - ConeDepth,
                            QuaternionUp,
                            width / 2);
        }

        Handles.color = HANDLE_COLOR_RIGHT;

        // X Line
        Handles.DrawLine(position, position + Vector2.right * size);

        // X Cap
        if (position.y > 0f)
        {
            Handles.ConeCap(0,
                            ((Vector3)((position + Vector2.right * size))) - ConeDepth,
                            QuaternionRight,
                            width / 2);
        }

        // If a Tool already is engaged and it's not this one, bail.
        if (currentId >= 0 && currentId != id)
        {
            return(position);
        }

        Event   e             = Event.current;
        Vector2 mousePosition = e.mousePosition;
        Vector2 newPosition   = position;

        if (currentId == id)
        {
            switch (e.type)
            {
            case EventType.MouseDrag:
                newPosition = axisConstraint.Mask(mousePosition + handleOffset) + axisConstraint.InverseMask(position);
                break;

            case EventType.MouseUp:
            case EventType.Ignore:
                currentId = -1;
                break;
            }
        }
        else
        {
            if (e.type == EventType.MouseDown && ((!limitToLeftButton && e.button != MIDDLE_MOUSE_BUTTON) || e.button == LEFT_MOUSE_BUTTON))
            {
                if (Vector2.Distance(mousePosition, position) < width / 2)
                {
                    currentId      = id;
                    handleOffset   = position - mousePosition;
                    axisConstraint = new pb_HandleConstraint2D(1, 1);
                }
                else
                if (handleRectRight.Contains(mousePosition))
                {
                    currentId      = id;
                    handleOffset   = position - mousePosition;
                    axisConstraint = new pb_HandleConstraint2D(1, 0);
                }
                else if (handleRectUp.Contains(mousePosition))
                {
                    currentId      = id;
                    handleOffset   = position - mousePosition;
                    axisConstraint = new pb_HandleConstraint2D(0, 1);
                }
            }
        }

        return(newPosition);
    }