示例#1
0
    private void Input(Event e, Vector3 mouse)
    {
        Points points = (Points)target;
        Rect   rect   = new Rect(5, 5, width + 10, height + 10);

        #region Input while over point
        if (!rect.Contains(e.mousePosition) && mouseOverPoint != null)
        {
            if (mouseOverPoint != null)
            {
                if (dragging)
                {
                    Undo.RecordObject(points, "Point Moved");
                    points.paths[currentPath].points[(int)mouseOverPoint] = mouse;
                    if (autoBuild)
                    {
                        points.BuildCollider();
                    }
                }

                if (e.type == EventType.MouseDown && e.button == 0 && e.modifiers != EventModifiers.Shift)
                {
                    if (e.modifiers != EventModifiers.Control)
                    {
                        dragging = true;
                    }
                }

                if (e.type == EventType.MouseUp && e.button == 0)
                {
                    dragging = false;
                }

                if (e.type == EventType.MouseDown && e.button == 0 && e.modifiers == EventModifiers.Control)
                {
                    Undo.RecordObject(points, "Point Removed");
                    points.paths[currentPath].points.RemoveAt((int)mouseOverPoint);
                    if (autoBuild)
                    {
                        points.BuildCollider();
                    }
                }

                if (e.type == EventType.MouseDown && e.button == 1 && e.modifiers != EventModifiers.Control)
                {
                    positionSettingStart = e.mousePosition;

                    GenericMenu menu = new GenericMenu();
                    menu.AddItem(new GUIContent("Set Position"), false, Callback, 1);

                    string str = showPositionSetting ? "Close" : "Cancel";
                    menu.AddItem(new GUIContent(str), false, Callback, 2);
                    menu.ShowAsContext();
                }

                if (e.type == EventType.MouseDown && e.button == 1 && e.modifiers == EventModifiers.Control)
                {
                    Undo.RecordObject(points, "Remove Path");
                    points.RemovePath(currentPath);
                    if (autoBuild)
                    {
                        points.BuildCollider();
                    }
                }

                if (e.type == EventType.MouseDown)
                {
                    if ((int)mouseOverPoint != rightClickPoint)
                    {
                        showPositionSetting = false;
                    }
                }
            }
        }
        #endregion

        #region Input while not over point
        if (!rect.Contains(e.mousePosition) && mouseOverPoint == null)
        {
            if (e.type == EventType.MouseDown && e.button == 0 && e.modifiers == EventModifiers.Shift)
            {
                handleSize = points.useGlobal ? points.settings.handleSize : points.handleSize;

                float   closestLine = handleSize;
                int     index       = -1;
                Vector2 m2          = new Vector2(mouse.x, mouse.y);
                for (int i = 0; i < points.paths.Count; i++)
                {
                    for (int j = 0; j < points.paths[i].points.Count; j++)
                    {
                        Vector2 p1 = new Vector2(points.paths[i].points[j].x, points.paths[i].points[j].y);
                        Vector2 p2 = Vector2.zero;
                        if (j > 0)
                        {
                            p2 = new Vector2(points.paths[i].points[j - 1].x, points.paths[i].points[j - 1].y);
                        }
                        else
                        {
                            p2 = new Vector2(points.paths[i].points[points.paths[i].points.Count - 1].x, points.paths[i].points[points.paths[i].points.Count - 1].y);
                        }

                        float dist = HandleUtility.DistancePointToLineSegment(m2, p1, p2);

                        if (dist < closestLine)
                        {
                            closestLine = dist;
                            index       = j;
                        }
                    }
                }

                Undo.RecordObject(points, "Add Point");

                if (index > -1)
                {
                    points.InsertPoint(m2, index, currentPath);
                }
                else
                {
                    points.AddPoint(m2, currentPath);
                }

                SceneView.RepaintAll();
                if (autoBuild)
                {
                    points.BuildCollider();
                }
            }

            if (e.type == EventType.MouseDown && e.button == 1 && e.modifiers == EventModifiers.Shift)
            {
                Undo.RecordObject(points, "Add Path");
                points.AddPath();
                currentPath = points.paths.Count - 1;
                points.AddPoint(mouse, currentPath);
                if (autoBuild)
                {
                    points.BuildCollider();
                }
            }
        }
        #endregion
    }