Esempio n. 1
0
        public RectSelector(GUISystem guiSystem)
        {
            m_GUISystem = guiSystem;

            m_RectSelectorControl = new GenericDefaultControl("RectSelector");

            var start   = Vector2.zero;
            var rectEnd = Vector2.zero;

            m_RectSelectAction = new SliderAction(m_RectSelectorControl)
            {
                enable        = (guiState, action) => !IsAltDown(guiState),
                enableRepaint = (guiState, action) =>
                {
                    var size = start - rectEnd;
                    return(size != Vector2.zero && guiState.hotControl == action.ID);
                },
                onSliderBegin = (guiState, control, position) =>
                {
                    start     = guiState.mousePosition;
                    rectEnd   = guiState.mousePosition;
                    m_GUIRect = FromToRect(start, rectEnd);

                    if (onSelectionBegin != null)
                    {
                        onSelectionBegin(this, guiState.isShiftDown);
                    }
                },
                onSliderChanged = (guiState, control, position) =>
                {
                    rectEnd   = guiState.mousePosition;
                    m_GUIRect = FromToRect(start, rectEnd);

                    if (onSelectionChanged != null)
                    {
                        onSelectionChanged(this);
                    }
                },
                onSliderEnd = (guiState, control, position) =>
                {
                    if (onSelectionEnd != null)
                    {
                        onSelectionEnd(this);
                    }
                },
                onRepaint = (guiState, action) =>
                {
                    Handles.BeginGUI();
                    styles.selectionRectStyle.Draw(m_GUIRect, GUIContent.none, false, false, false, false);
                    Handles.EndGUI();
                }
            };

            m_GUISystem.AddControl(m_RectSelectorControl);
            m_GUISystem.AddAction(m_RectSelectAction);
        }
 public void Install(GUISystem guiSystem)
 {
     guiSystem.AddControl(m_EdgeControl);
     guiSystem.AddControl(m_PointControl);
     guiSystem.AddControl(m_LeftTangentControl);
     guiSystem.AddControl(m_RightTangentControl);
     guiSystem.AddAction(m_CreatePointAction);
     guiSystem.AddAction(m_RemovePointAction1);
     guiSystem.AddAction(m_RemovePointAction2);
     guiSystem.AddAction(m_MovePointAction);
     guiSystem.AddAction(m_MoveEdgeAction);
     guiSystem.AddAction(m_MoveLeftTangentAction);
     guiSystem.AddAction(m_MoveRightTangentAction);
 }
Esempio n. 3
0
        public RectSelector(GUISystem guiSystem)
        {
            m_GUISystem = guiSystem;

            m_RectSelectorControl = new GenericDefaultControl("RectSelector")
            {
                position = (guiState) =>
                {
                    return(GUIToWorld(guiState, guiState.mousePosition));
                },
                forward = (guiState) =>
                {
                    if (Camera.current)
                    {
                        return(Camera.current.transform.forward);
                    }

                    return(Vector3.forward);
                },
                right = (guiState) =>
                {
                    if (Camera.current)
                    {
                        return(Camera.current.transform.right);
                    }

                    return(Vector3.right);
                },
                up = (guiState) =>
                {
                    if (Camera.current)
                    {
                        return(Camera.current.transform.up);
                    }

                    return(Vector3.up);
                }
            };

            m_RectSelectAction = new SliderAction(m_RectSelectorControl)
            {
                enableRepaint = (guiState, action) =>
                {
                    var size = m_RectStart - m_RectEnd;
                    return(size != Vector3.zero && guiState.hotControl == action.ID);
                },
                onClick = (guiState, control) =>
                {
                    m_RectStart = GUIToWorld(guiState, guiState.mousePosition);
                    m_RectEnd   = m_RectStart;
                    m_GUIRect   = CalculateGUIRect();
                },
                onSliderBegin = (guiState, control, position) =>
                {
                    m_RectEnd = position;
                    m_GUIRect = CalculateGUIRect();

                    if (onSelectionBegin != null)
                    {
                        onSelectionBegin(this, guiState.isShiftDown);
                    }
                },
                onSliderChanged = (guiState, control, position) =>
                {
                    m_RectEnd = position;
                    m_GUIRect = CalculateGUIRect();

                    if (onSelectionChanged != null)
                    {
                        onSelectionChanged(this);
                    }
                },
                onSliderEnd = (guiState, control, position) =>
                {
                    if (onSelectionEnd != null)
                    {
                        onSelectionEnd(this);
                    }
                },
                onRepaint = (guiState, action) =>
                {
                    m_Drawer.DrawSelectionRect(m_GUIRect);
                }
            };

            m_GUISystem.AddControl(m_RectSelectorControl);
            m_GUISystem.AddAction(m_RectSelectAction);
        }
        private GUISystem CreateSystem(UnityObject target)
        {
            var guiSystem = new GUISystem(new GUIState());

            GUIAction removePointAction = null;

            var pointControl = new GenericControl("Point")
            {
                count = () =>
                {
                    return(GetPointCount(target));
                },
                distance = (guiState, i) =>
                {
                    var position = GetPointWorld(target, i);
                    return(ShapeEditorUtility.DistanceToCircle(position, ShapeEditorUtility.GetHandleSize(position) * 10f));
                },
                position = (i) =>
                {
                    return(GetPointWorld(target, i));
                },
                forward = (i) =>
                {
                    return(GetForward(target));
                },
                up = (i) =>
                {
                    return(GetUp(target));
                },
                right = (i) =>
                {
                    return(GetRight(target));
                },
                onRepaint = (IGUIState guiState, Control control, int index) =>
                {
                    var position = GetPointWorld(target, index);

                    if (guiState.hotControl == control.actionID && control.hotLayoutData.index == index)
                    {
                        m_Drawer.DrawPointSelected(position);
                    }
                    else if (guiState.hotControl == 0 && guiState.nearestControl == control.ID && control.layoutData.index == index)
                    {
                        if (removePointAction.IsEnabled(guiState))
                        {
                            m_Drawer.DrawRemovePointPreview(position);
                        }
                        else
                        {
                            m_Drawer.DrawPointHovered(position);
                        }
                    }
                    else
                    {
                        m_Drawer.DrawPoint(position);
                    }
                }
            };

            var edgeControl = new GenericControl("Edge")
            {
                count = () =>
                {
                    return(GetPointCount(target));
                },
                distance = (IGUIState guiState, int index) =>
                {
                    return(ShapeEditorUtility.DistanceToSegment(GetPointWorld(target, index), NextControlPoint(target, index)));
                },
                position = (i) =>
                {
                    return(GetPointWorld(target, i));
                },
                forward = (i) =>
                {
                    return(GetForward(target));
                },
                up = (i) =>
                {
                    return(GetUp(target));
                },
                right = (i) =>
                {
                    return(GetRight(target));
                },
                onRepaint = (IGUIState guiState, Control control, int index) =>
                {
                    var nextIndex = NextIndex(target, index);
                    var prevIndex = PrevIndex(target, index);

                    var isEndpointHovered =
                        guiState.hotControl == 0 &&
                        guiState.nearestControl == pointControl.ID &&
                        (index == pointControl.layoutData.index || nextIndex == pointControl.layoutData.index);

                    var isPointHovered =
                        guiState.hotControl == 0 &&
                        guiState.nearestControl == pointControl.ID &&
                        index == pointControl.layoutData.index;

                    var color = Color.white;

                    if (guiState.hotControl == 0 && guiState.nearestControl == control.ID && control.layoutData.index == index)
                    {
                        color = Color.yellow;
                    }
                    else if (removePointAction.IsEnabled(guiState) && isEndpointHovered)
                    {
                        if (isPointHovered)
                        {
                            m_Drawer.DrawDottedLine(GetPointWorld(target, prevIndex), GetPointWorld(target, nextIndex), 5f, color);
                        }

                        color = Color.red;
                    }

                    m_Drawer.DrawLine(GetPointWorld(target, index), GetPointWorld(target, nextIndex), 5f, color);
                }
            };

            var createPointAction = new CreatePointAction(pointControl, edgeControl)
            {
                enable = (guiState, action) =>
                {
                    return(guiState.isShiftDown);
                },
                enableRepaint = (IGUIState guiState, GUIAction action) =>
                {
                    return(guiState.nearestControl != pointControl.ID && guiState.hotControl == 0);
                },
                repaintOnMouseMove = (guiState, action) =>
                {
                    return(true);
                },
                guiToWorld = (mousePosition) =>
                {
                    return(GUIToWorld(target, mousePosition));
                },
                onCreatePoint = (int index, Vector3 position) =>
                {
                    InsertPointWorld(target, index + 1, position);
                },
                onPreRepaint = (guiState, action) =>
                {
                    var position = ClosestPointInEdge(target, guiState.mousePosition, edgeControl.layoutData.index);

                    m_Drawer.DrawCreatePointPreview(position);
                }
            };

            removePointAction = new ClickAction(pointControl, 0)
            {
                enable = (guiState, action) =>
                {
                    return(guiState.isActionKeyDown);
                },
                onClick = (GUIState, control) =>
                {
                    if (GetPointCount(target) > 3)
                    {
                        RemovePoint(target, control.layoutData.index);
                    }
                }
            };

            var movePointAction = new SliderAction(pointControl)
            {
                onSliderChanged = (guiState, control, position) =>
                {
                    var index         = control.hotLayoutData.index;
                    var pointPosition = GetPointWorld(target, index);
                    pointPosition = position;
                    SetPointWorld(target, index, pointPosition);
                }
            };

            var moveEdgeAction = new SliderAction(edgeControl)
            {
                onSliderChanged = (guiState, control, position) =>
                {
                    var index         = control.hotLayoutData.index;
                    var pointPosition = GetPointWorld(target, index);
                    var delta         = position - pointPosition;
                    pointPosition += delta;
                    SetPointWorld(target, index, pointPosition);
                    pointPosition  = NextControlPoint(target, index);
                    pointPosition += delta;
                    SetPointWorld(target, NextIndex(target, index), pointPosition);
                }
            };

            guiSystem.AddControl(edgeControl);
            guiSystem.AddControl(pointControl);
            guiSystem.AddAction(createPointAction);
            guiSystem.AddAction(removePointAction);
            guiSystem.AddAction(movePointAction);
            guiSystem.AddAction(moveEdgeAction);

            return(guiSystem);
        }
Esempio n. 5
0
        public PathEditor(GUISystem guiSystem)
        {
            m_GUISystem = guiSystem;

            var m_PointControl = new GenericControl("Point")
            {
                count    = GetPointCount,
                distance = (guiState, i) =>
                {
                    var position = GetPoint(i).position;
                    return(guiState.DistanceToCircle(position, guiState.GetHandleSize(position) * 10f));
                },
                position  = (i) => { return(GetPoint(i).position); },
                forward   = (i) => { return(GetForward()); },
                up        = (i) => { return(GetUp()); },
                right     = (i) => { return(GetRight()); },
                onRepaint = DrawPoint
            };

            var m_EdgeControl = new GenericControl("Edge")
            {
                count     = GetEdgeCount,
                distance  = DistanceToEdge,
                position  = (i) => { return(GetPoint(i).position); },
                forward   = (i) => { return(GetForward()); },
                up        = (i) => { return(GetUp()); },
                right     = (i) => { return(GetRight()); },
                onRepaint = DrawEdge
            };

            m_EdgeControl.onEndLayout = (guiState) => { controller.AddClosestPath(m_EdgeControl.layoutData.distance); };

            var m_LeftTangentControl = new GenericControl("LeftTangent")
            {
                count = () =>
                {
                    if (GetShapeType() != ShapeType.Spline)
                    {
                        return(0);
                    }

                    return(GetPointCount());
                },
                distance = (guiState, i) =>
                {
                    if (linearTangentIsZero && GetPoint(i).tangentMode == TangentMode.Linear)
                    {
                        return(float.MaxValue);
                    }

                    if (!IsSelected(i) || IsOpenEnded() && i == 0)
                    {
                        return(float.MaxValue);
                    }

                    var position = GetLeftTangent(i);
                    return(guiState.DistanceToCircle(position, guiState.GetHandleSize(position) * 10f));
                },
                position  = (i) => { return(GetLeftTangent(i)); },
                forward   = (i) => { return(GetForward()); },
                up        = (i) => { return(GetUp()); },
                right     = (i) => { return(GetRight()); },
                onRepaint = (guiState, control, i) =>
                {
                    if (!IsSelected(i) || IsOpenEnded() && i == 0)
                    {
                        return;
                    }

                    var point = GetPoint(i);

                    if (linearTangentIsZero && point.tangentMode == TangentMode.Linear)
                    {
                        return;
                    }

                    var position    = point.position;
                    var leftTangent = GetLeftTangent(i);

                    drawer.DrawTangent(position, leftTangent);
                }
            };

            var m_RightTangentControl = new GenericControl("RightTangent")
            {
                count = () =>
                {
                    if (GetShapeType() != ShapeType.Spline)
                    {
                        return(0);
                    }

                    return(GetPointCount());
                },
                distance = (guiState, i) =>
                {
                    if (linearTangentIsZero && GetPoint(i).tangentMode == TangentMode.Linear)
                    {
                        return(float.MaxValue);
                    }

                    if (!IsSelected(i) || IsOpenEnded() && i == GetPointCount() - 1)
                    {
                        return(float.MaxValue);
                    }

                    var position = GetRightTangent(i);
                    return(guiState.DistanceToCircle(position, guiState.GetHandleSize(position) * 10f));
                },
                position  = (i) => { return(GetRightTangent(i)); },
                forward   = (i) => { return(GetForward()); },
                up        = (i) => { return(GetUp()); },
                right     = (i) => { return(GetRight()); },
                onRepaint = (guiState, control, i) =>
                {
                    if (!IsSelected(i) || IsOpenEnded() && i == GetPointCount() - 1)
                    {
                        return;
                    }

                    var point = GetPoint(i);

                    if (linearTangentIsZero && point.tangentMode == TangentMode.Linear)
                    {
                        return;
                    }

                    var position     = point.position;
                    var rightTangent = GetRightTangent(i);

                    drawer.DrawTangent(position, rightTangent);
                }
            };

            var m_CreatePointAction = new CreatePointAction(m_PointControl, m_EdgeControl)
            {
                enable             = (guiState, action) => !IsAltDown(guiState) && !guiState.isActionKeyDown && controller.closestEditablePath == controller.editablePath,
                enableRepaint      = (guiState, action) => EnableCreatePointRepaint(guiState, m_PointControl, m_LeftTangentControl, m_RightTangentControl),
                repaintOnMouseMove = (guiState, action) => true,
                guiToWorld         = GUIToWorld,
                onCreatePoint      = (index, position) =>
                {
                    controller.RegisterUndo("Create Point");
                    controller.CreatePoint(index, position);
                },
                onPreRepaint = (guiState, action) =>
                {
                    if (GetPointCount() > 0)
                    {
                        var position = ClosestPointInEdge(guiState, guiState.mousePosition, m_EdgeControl.layoutData.index);
                        drawer.DrawCreatePointPreview(position);
                    }
                }
            };

            Action <IGUIState> removePoints = (guiState) =>
            {
                controller.RegisterUndo("Remove Point");
                controller.RemoveSelectedPoints();
                guiState.changed = true;
            };

            var m_RemovePointAction1 = new CommandAction(kDeleteCommandName)
            {
                enable    = (guiState, action) => { return(GetSelectedPointCount() > 0); },
                onCommand = removePoints
            };

            var m_RemovePointAction2 = new CommandAction(kSoftDeleteCommandName)
            {
                enable    = (guiState, action) => { return(GetSelectedPointCount() > 0); },
                onCommand = removePoints
            };

            var dragged           = false;
            var m_MovePointAction = new SliderAction(m_PointControl)
            {
                enable  = (guiState, action) => !IsAltDown(guiState),
                onClick = (guiState, control) =>
                {
                    dragged = false;
                    var index = control.layoutData.index;

                    if (!IsSelected(index))
                    {
                        controller.RegisterUndo("Selection");

                        if (!guiState.isActionKeyDown)
                        {
                            controller.ClearSelection();
                        }

                        controller.SelectPoint(index, true);
                        guiState.changed = true;
                    }
                },
                onSliderChanged = (guiState, control, position) =>
                {
                    var index = control.hotLayoutData.index;
                    var delta = SnapIfNeeded(position) - GetPoint(index).position;

                    if (!dragged)
                    {
                        controller.RegisterUndo("Move Point");
                        dragged = true;
                    }

                    controller.MoveSelectedPoints(delta);
                }
            };

            var m_MoveEdgeAction = new SliderAction(m_EdgeControl)
            {
                enable        = (guiState, action) => !IsAltDown(guiState) && guiState.isActionKeyDown,
                onSliderBegin = (guiState, control, position) =>
                {
                    dragged = false;
                },
                onSliderChanged = (guiState, control, position) =>
                {
                    var index = control.hotLayoutData.index;
                    var delta = position - GetPoint(index).position;

                    if (!dragged)
                    {
                        controller.RegisterUndo("Move Edge");
                        dragged = true;
                    }

                    controller.MoveEdge(index, delta);
                }
            };

            var cachedRightTangent = Vector3.zero;
            var cachedLeftTangent  = Vector3.zero;
            var cachedTangentMode  = TangentMode.Linear;

            var m_MoveLeftTangentAction = new SliderAction(m_LeftTangentControl)
            {
                enable        = (guiState, action) => !IsAltDown(guiState),
                onSliderBegin = (guiState, control, position) =>
                {
                    dragged = false;
                    var point = GetPoint(control.hotLayoutData.index);
                    cachedRightTangent = point.rightTangent;
                    cachedTangentMode  = point.tangentMode;
                },
                onSliderChanged = (guiState, control, position) =>
                {
                    var index       = control.hotLayoutData.index;
                    var setToLinear = m_PointControl.distance(guiState, index) <= DefaultControl.kPickDistance;

                    if (!dragged)
                    {
                        controller.RegisterUndo("Move Tangent");
                        dragged = true;
                    }

                    position = SnapIfNeeded(position);
                    controller.SetLeftTangent(index, position, setToLinear, guiState.isShiftDown, cachedRightTangent, cachedTangentMode);
                }
            };

            var m_MoveRightTangentAction = new SliderAction(m_RightTangentControl)
            {
                enable        = (guiState, action) => !IsAltDown(guiState),
                onSliderBegin = (guiState, control, position) =>
                {
                    dragged = false;
                    var point = GetPoint(control.hotLayoutData.index);
                    cachedLeftTangent = point.leftTangent;
                    cachedTangentMode = point.tangentMode;
                },
                onSliderChanged = (guiState, control, position) =>
                {
                    var index       = control.hotLayoutData.index;
                    var setToLinear = m_PointControl.distance(guiState, index) <= DefaultControl.kPickDistance;

                    if (!dragged)
                    {
                        controller.RegisterUndo("Move Tangent");
                        dragged = true;
                    }

                    position = SnapIfNeeded(position);
                    controller.SetRightTangent(index, position, setToLinear, guiState.isShiftDown, cachedLeftTangent, cachedTangentMode);
                }
            };

            m_GUISystem.AddControl(m_EdgeControl);
            m_GUISystem.AddControl(m_PointControl);
            m_GUISystem.AddControl(m_LeftTangentControl);
            m_GUISystem.AddControl(m_RightTangentControl);
            m_GUISystem.AddAction(m_CreatePointAction);
            m_GUISystem.AddAction(m_RemovePointAction1);
            m_GUISystem.AddAction(m_RemovePointAction2);
            m_GUISystem.AddAction(m_MovePointAction);
            m_GUISystem.AddAction(m_MoveEdgeAction);
            m_GUISystem.AddAction(m_MoveLeftTangentAction);
            m_GUISystem.AddAction(m_MoveRightTangentAction);
        }
Esempio n. 6
0
 public void AddNullAction_Trows()
 {
     Assert.Throws <System.NullReferenceException>(() => { m_GUISystem.AddAction(null); }, "Adding null action should throw");
 }