void OnSceneGUI()
    {
        e = Event.current;

        switch (e.type)
        {
        case EventType.KeyDown:
            if (e.keyCode == KeyCode.Delete)
            {
                DeleteNode(selectedNodeIndex);
                e.Use();
            }
            break;
        }

        for (int nodeIndex = 0; nodeIndex < positionData.nodes.Count; nodeIndex++)
        {
            DoNode(nodeIndex);
        }

        if (e.control)
        {
            Ray r = HandleUtility.GUIPointToWorldRay(e.mousePosition);

            int        controlId = GUIUtility.GetControlID(SurfaceHandle.hash, FocusType.Keyboard);
            RaycastHit mouseHit;
            if (Physics.Raycast(r, out mouseHit))
            {
                switch (e.type)
                {
                case EventType.MouseDown:
                    if (e.button == 0 || e.button == 1)
                    {
                        EditorUtility.SetDirty(positionData);
                        Undo.RecordObject(positionData, "Add Node");

                        positionData.nodes.Add(new Position.PositionNode {
                            position         = mouseHit.point,
                            connectedIndexes = new int[] { -1, -1, -1, -1 }
                        });

                        if (e.button == 1)
                        {
                            ChangeNodeConnection(positionData.nodes.Count - 1);
                            GUIUtility.hotControl = GUIUtility.keyboardControl = controlId;
                        }
                    }
                    break;
                }
                Handles.color = Color.white;
                SurfaceHandle.Do(mouseHit.point, .5f);
            }
        }
    }
    void DoPositionHandle(int segementIndex, Side side)
    {
        Vector3 originalPosition = new Vector3();

        AI.AISegment segment = aiData.segments[segementIndex];
        switch (side)
        {
        case Side.Left:
            originalPosition = segment.left;
            break;

        case Side.Right:
            originalPosition = segment.right;
            break;
        }

        Vector3 newPosition = originalPosition;

        if (dragMode == DragMode.Project)
        {
            int controlId = GUIUtility.GetControlID(SurfaceHandle.hash, FocusType.Keyboard);
            if (GUIUtility.keyboardControl == controlId)
            {
                switch (e.type)
                {
                case EventType.MouseUp:
                    OnSelect(segementIndex, side);
                    break;
                }
            }
            else if (HandleUtility.nearestControl == controlId)
            {
                switch (e.type)
                {
                case EventType.MouseDown:
                    if (e.button == 1 && selectedSegmentIndex != segementIndex && selectedSegmentIndex != -1)
                    {
                        ChangeSegmentConnection(segementIndex);

                        e.Use();
                    }
                    break;
                }
            }
            newPosition = SurfaceHandle.Do(controlId, newPosition, .5f);
        }
        else
        {
            if (selectedSegmentIndex == segementIndex && selectedSide == side)
            {
                newPosition = Handles.PositionHandle(newPosition, Quaternion.identity);
            }
            else
            {
                float handleScale = HandleUtility.GetHandleSize(originalPosition);
                if (Handles.Button(originalPosition, Quaternion.identity, handleScale * .03f, handleScale * .05f, Handles.DotHandleCap))
                {
                    OnSelect(segementIndex, side);
                }
            }
        }

        if (newPosition != originalPosition)
        {
            EditorUtility.SetDirty(aiData);
            Undo.RecordObject(aiData, "Move Side Position");

            switch (side)
            {
            case Side.Left:
                segment.left = newPosition;
                break;

            case Side.Right:
                segment.right = newPosition;
                break;
            }
            aiData.segments[segementIndex] = segment;
        }
    }
    void OnSceneGUI()
    {
        e = Event.current;

        switch (e.type)
        {
        case EventType.KeyDown:
            if (e.keyCode == KeyCode.Delete)
            {
                DeleteSegment(selectedSegmentIndex);
                e.Use();
            }
            break;

        case EventType.MouseUp:
            altMode     = e.alt;
            altModeLock = false;
            break;
        }

        if (!altModeLock)
        {
            altMode = e.alt;
        }

        for (int i = 0; i < aiData.segments.Count; i++)
        {
            DoSegment(i);
        }

        if (e.control)
        {
            Ray r = HandleUtility.GUIPointToWorldRay(e.mousePosition);

            GUIUtility.GetControlID(SurfaceHandle.hash, FocusType.Keyboard);

            int        controlId = GUIUtility.GetControlID(SurfaceHandle.hash, FocusType.Keyboard);
            RaycastHit mouseHit;
            if (Physics.Raycast(r, out mouseHit))
            {
                switch (e.type)
                {
                case EventType.MouseDown:
                    if (e.button == 0 || e.button == 1)
                    {
                        EditorUtility.SetDirty(aiData);
                        Undo.RecordObject(aiData, "Add Segment");

                        aiData.segments.Add(new AI.AISegment {
                            left             = mouseHit.point,
                            leftSpeed        = 30,
                            right            = mouseHit.point,
                            rightSpeed       = 30,
                            raceLine         = .5f,
                            altRaceLine      = .5f,
                            raceSpeed        = 30,
                            centerSpeed      = 30,
                            connectedIndexes = new int[] { -1, -1 }
                        });

                        if (e.button == 1)
                        {
                            ChangeSegmentConnection(aiData.segments.Count - 1);
                            GUIUtility.hotControl = GUIUtility.keyboardControl = controlId;
                        }
                    }
                    break;
                }
                Handles.color = Color.white;
                SurfaceHandle.Do(controlId, mouseHit.point, .5f);
            }
        }
    }
    private void DoNode(int nodeIndex)
    {
        Position.PositionNode positionNode = positionData.nodes[nodeIndex];
        Vector3 newPosition = positionNode.position;

        DoMidPoint(nodeIndex);

        Handles.color = Color.green;
        if (dragMode == DragMode.Project)
        {
            int controlId = GUIUtility.GetControlID(SurfaceHandle.hash, FocusType.Keyboard);
            if (GUIUtility.keyboardControl == controlId)
            {
                switch (e.type)
                {
                case EventType.MouseUp:
                    OnSelect(nodeIndex);
                    break;
                }
            }
            else if (HandleUtility.nearestControl == controlId)
            {
                switch (e.type)
                {
                case EventType.MouseDown:
                    if (e.button == 1 && selectedNodeIndex != nodeIndex && selectedNodeIndex != -1)
                    {
                        ChangeNodeConnection(nodeIndex);

                        e.Use();
                    }
                    break;
                }
            }
            newPosition = SurfaceHandle.Do(controlId, newPosition, nodeIndex == positionData.finishLineIndex ? 1.5f : .5f);
        }
        else
        {
            if (selectedNodeIndex == nodeIndex)
            {
                newPosition = Handles.PositionHandle(newPosition, Quaternion.identity);
            }
            else
            {
                float handleScale = HandleUtility.GetHandleSize(newPosition);
                if (Handles.Button(positionNode.position, Quaternion.identity, handleScale * .03f, handleScale * .05f, Handles.DotHandleCap))
                {
                    OnSelect(nodeIndex);
                }
            }
        }

        if (newPosition != positionNode.position)
        {
            EditorUtility.SetDirty(positionData);
            Undo.RecordObject(positionData, "Move Position");

            positionNode.position = newPosition;
        }

        positionData.nodes[nodeIndex] = positionNode;
    }