Пример #1
0
 public void DrawPointSelected(Vector3 position)
 {
     ShapeEditorUtility.DrawGUIStyleCap(0, position, Quaternion.identity, ShapeEditorUtility.GetHandleSize(position), styles.pointSelectedStyle);
 }
        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);
        }
Пример #3
0
 public void DrawRemovePointPreview(Vector3 position)
 {
     ShapeEditorUtility.DrawGUIStyleCap(0, position, Quaternion.identity, ShapeEditorUtility.GetHandleSize(position), styles.pointRemovePreviewStyle);
 }