void OnSceneGUI()
    {
        if (!plotter)
        {
            return;
        }

        plotter.InitEditing();

        if (plotter.CurrentAction == HairyPlotterActions.None)
        {
            plotter.SetTriangleHover(HoverTriangle());

            if (plotter.ToggleSelected(PickTriangle()))
            {
                Repaint();
            }

            if (plotter.HoveredTriangle != null)
            {
                SceneView.lastActiveSceneView.Repaint();
            }
        }

        if (plotter.CurrentAction == HairyPlotterActions.TriangleSwitch)
        {
            HairyPlotterTriangle triangle = PickTriangle();

            if (triangle != null)
            {
                List <HairyPlotterVertex> vertices = plotter.SelectedVertices;

                if (vertices.Count == 2)
                {
                    triangle.SwitchVertices(vertices[0], vertices[1]);
                    plotter.ClearVertexSelection();
                }
                else
                {
                    Debug.LogWarning("Switch In Triangle requires exactly two selected vertices");
                }
            }
        }

        DrawTriangles();
    }
    void OnSceneGUI()
    {
        if (!plotter)
        {
            return;
        }

        plotter.InitEditing();

        if (plotter.CurrentAction == HairyPlotterActions.None)
        {
            // don't hover triangle when moving vertices
            if (allowMoveVerts)
            {
                plotter.SetTriangleHover(null);
            }
            else
            {
                plotter.SetTriangleHover(HoverTriangle());
            }

            if (plotter.ToggleSelected(PickTriangle()) && !allowMoveVerts)
            {
                Repaint();
            }

            if (plotter.HoveredTriangle != null)
            {
                SceneView.lastActiveSceneView.Repaint();
            }

            // detect if dragging a single vertex or selected vertices and update position/s according mouse position
            if (Event.current.button == 0)
            {
                KeyCode   _keyCode = Event.current.keyCode;
                EventType _type    = Event.current.type;

                if (_type == EventType.MouseDown)
                {
                    // pick selected vertices?
                    if (_keyCode == KeyCode.LeftShift || _keyCode == KeyCode.RightShift)
                    {
                        // use plotter.SelectedVertices
                    }
                    // or pick single vertex
                    else
                    {
                        hoveredVertex = PickVertex();
                    }
                    // set flag for allow moving vertices
                    allowMoveVerts = true;
                }
                // on mouse up finish the edition
                else if (_type == EventType.MouseUp)
                {
                    allowMoveVerts = false;
                    hoveredVertex  = null;
                }
                // while dragging mouse update vertex position
                else if (_type == EventType.MouseDrag && allowMoveVerts)
                {
                    Ray       r = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                    Transform t = plotter.transform;                     // this for position gizmo vertices and triangles according parent game object world coordinates
                    Vector2   p = t.InverseTransformPoint(r.origin);     // use inverse trasnform because then we transform again for displaying

                    // moving selected vertices?
                    if (_keyCode == KeyCode.LeftShift || _keyCode == KeyCode.RightShift)
                    {
                        for (int i = 0, c = plotter.SelectedVertices.Count; i < c; ++i)
                        {
                            plotter.SelectedVertices[i].Position.x = p.x;
                            plotter.SelectedVertices[i].Position.y = p.y;
                            if (HairyPlotterEditor.KeepUVOnVertsMove)
                            {
                                plotter.SelectedVertices[i].Uv.x = 0.5f + p.x;
                                plotter.SelectedVertices[i].Uv.y = 0.5f + p.y;
                            }
                        }
                    }
                    // or single vertex?
                    else if (hoveredVertex != null)
                    {
                        hoveredVertex.Position.x = p.x;
                        hoveredVertex.Position.y = p.y;
                        if (HairyPlotterEditor.KeepUVOnVertsMove)
                        {
                            hoveredVertex.Uv.x = 0.5f + p.x;
                            hoveredVertex.Uv.y = 0.5f + p.y;
                        }
                    }
                    plotter.Dirty = true;
                    plotter.ResetSelectionPosition();
                }
            }
        }

        if (plotter.CurrentAction == HairyPlotterActions.TriangleSwitch)
        {
            HairyPlotterTriangle triangle = PickTriangle();

            if (triangle != null)
            {
                List <HairyPlotterVertex> vertices = plotter.SelectedVertices;

                if (vertices.Count == 2)
                {
                    triangle.SwitchVertices(vertices[0], vertices[1]);
                    plotter.ClearVertexSelection();
                }
                else
                {
                    Debug.LogWarning("Switch In Triangle requires exactly two selected vertices");
                }
            }
        }

        DrawTriangles();
    }