private void DrawNodes(Vector3[] worldPoints)
 {
     for (int i = 0; i < layoutPath.points.Count; i++)
     {
         Vector3 pos        = layoutPath.transform.TransformPoint(layoutPath.points[i]);
         float   handleSize = HandleUtility.GetHandleSize(pos);
         Vector3 newPos     = Handles.FreeMoveHandle(pos, Quaternion.identity, handleSize * 0.09f, Vector3.one, Handles.RectangleHandleCap);
         if (newPos != pos)
         {
             newPos = FindPoint();
             //CheckAlignment(worldPoints, handleSize * 0.1f, i, ref newPos);
             Undo.RecordObject(layoutPath, "Move Node");
             layoutPath.points[i] = layoutPath.transform.InverseTransformPoint(newPos);
             layoutPath.calcLength();
         }
     }
 }
    void OnSceneGUI()
    {
        layoutPath = target as LayoutPath;
        Vector3[] localPoints = layoutPath.points.ToArray();
        Vector3[] worldPoints = new Vector3[layoutPath.points.Count];
        for (int i = 0; i < worldPoints.Length; i++)
        {
            worldPoints[i] = layoutPath.transform.TransformPoint(localPoints[i]);
        }

        DrawPolyLine(worldPoints);

        if (!layoutPath.EditLine)
        {
            return;
        }

        DrawNodes(worldPoints);

        if (Event.current.shift)
        {
            var newPoint = FindPoint();

            if (newPoint == Vector3.zero)
            {
                return;
            }

            var handleSize = HandleUtility.GetHandleSize(newPoint);
            var nodeIndex  = FindNearestNodeToMouse(worldPoints);
            if (nodeIndex < worldPoints.Length - 1)
            {
                if (Handles.Button(newPoint, Quaternion.identity, handleSize * 0.1f, handleSize, Handles.RectangleHandleCap))
                {
                    Undo.RecordObject(layoutPath, "Insert Node");
                    layoutPath.points.Insert(nodeIndex + 1, newPoint);
                    Event.current.Use();
                    layoutPath.calcLength();
                }
            }
            else
            {
                if (Handles.Button(newPoint, Quaternion.identity, handleSize * 0.1f, handleSize, Handles.RectangleHandleCap))
                {
                    Undo.RecordObject(layoutPath, "Insert Node");
                    layoutPath.points.Add(newPoint);
                    Event.current.Use();
                    layoutPath.calcLength();
                }
            }
        }

        if (Event.current.control)
        {
            //Deleting Points
            int indexToDelete = FindNearestNodeToMouse(worldPoints);
            Handles.color = Color.red;
            var handleSize = HandleUtility.GetHandleSize(worldPoints[0]);
            if (Handles.Button(worldPoints[indexToDelete], Quaternion.identity, handleSize * 0.09f, handleSize, Handles.RectangleHandleCap))
            {
                Undo.RecordObject(layoutPath, "Remove Node");
                layoutPath.points.RemoveAt(indexToDelete);
                indexToDelete = -1;
                Event.current.Use();
                layoutPath.calcLength();
            }

            Handles.color = Color.white;
        }

        if (GUI.changed)
        {
            EditorUtility.SetDirty(layoutPath);
        }
    }
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        layoutPath = target as LayoutPath;

        GUILayout.Space(10);
        GUILayout.Label("Import from Max spline generated", EditorStyles.boldLabel);
        GUI.backgroundColor = new Color32(157, 220, 207, 255);
        if (GUILayout.Button("Import XML"))
        {
            var path = EditorUtility.OpenFilePanel("Select the XML that contains the spline path points", "", "xml");
            if (path.Length != 0)
            {
                layoutPath.BuildObject(path);
            }
        }

        GUILayout.Space(10);
        GUI.backgroundColor = new Color(0.0f, 0.8f, 0.5176f);
        GUILayout.Label("Import from Sentieri waypoints", EditorStyles.boldLabel);
        if (GUILayout.Button("Create path"))
        {
            layoutPath.GenerateFromWaypoints();
        }
        if (GUILayout.Button("Export path"))
        {
            layoutPath.ExportFromWaypoints();
        }

        GUILayout.Space(10);
        GUILayout.Label("Import from Moose Procedural Rally stages", EditorStyles.boldLabel);
        GUI.backgroundColor = new Color32(248, 200, 81, 255);
        if (GUILayout.Button("Import spline.rsd"))
        {
            var path = EditorUtility.OpenFilePanel("Select the spline.rsd file", "", "rsd");
            if (path.Length != 0)
            {
                if (layoutPath.ImportMooseBin(path))
                {
                    EditorUtility.DisplayDialog("Moose Import", "Path imported!", "Ok!!");
                }
                else
                {
                    EditorUtility.DisplayDialog("Moose Import", "ERROR!, no points added\r\nCheck the file!", "Ok!!");
                }
            }
        }

#if VEGETATION_STUDIO || VEGETATION_STUDIO_PRO
        GUILayout.Space(10);
        GUI.backgroundColor = Color.white;
        GUILayout.Label("Vegetation Studio road mask creation", EditorStyles.boldLabel);

        layoutPath.VS_RemoveGrass        = EditorGUILayout.Toggle("Remove Grass", layoutPath.VS_RemoveGrass);
        layoutPath.VS_RemovePlants       = EditorGUILayout.Toggle("Remove Plants", layoutPath.VS_RemovePlants);
        layoutPath.VS_RemoveTrees        = EditorGUILayout.Toggle("Remove Trees", layoutPath.VS_RemoveTrees);
        layoutPath.VS_RemoveObjects      = EditorGUILayout.Toggle("Remove Objects", layoutPath.VS_RemoveObjects);
        layoutPath.VS_RemoveLargeObjects = EditorGUILayout.Toggle("Remove Large Objects", layoutPath.VS_RemoveLargeObjects);

        AwesomeTechnologies.Common.EditorFunctions.FloatRangeField("Additional Grass Perimeter", ref layoutPath.VS_AdditionalGrassPerimiter, ref layoutPath.VS_AdditionalGrassPerimiterMax, 0, 40);
        AwesomeTechnologies.Common.EditorFunctions.FloatRangeField("Additional Plant Perimeter", ref layoutPath.VS_AdditionalPlantPerimiter, ref layoutPath.VS_AdditionalPlantPerimiterMax, 0, 40);
        AwesomeTechnologies.Common.EditorFunctions.FloatRangeField("Additional Tree Perimeter", ref layoutPath.VS_AdditionalTreePerimiter, ref layoutPath.VS_AdditionalTreePerimiterMax, 0, 40);
        AwesomeTechnologies.Common.EditorFunctions.FloatRangeField("Additional Object Perimeter", ref layoutPath.VS_AdditionalObjectPerimiter, ref layoutPath.VS_AdditionalObjectPerimiterMax, 0, 40);
        AwesomeTechnologies.Common.EditorFunctions.FloatRangeField("Additional Large Perimeter", ref layoutPath.VS_AdditionalLargeObjectPerimiter, ref layoutPath.VS_AdditionalLargeObjectPerimiterMax, 0, 40);
        layoutPath.VS_LineWidth = EditorGUILayout.FloatField("Line Width", layoutPath.VS_LineWidth);

        GUILayout.Space(5);
        layoutPath.VS_SkipPoints = EditorGUILayout.IntField("Skip points", layoutPath.VS_SkipPoints);
        GUILayout.Space(5);

        GUI.backgroundColor = new Color32(99, 194, 214, 255);
        if (GUILayout.Button("Create"))
        {
            if (layoutPath.GenerateVsRoadMask())
            {
                EditorUtility.DisplayDialog("Vegetation Studio", "Road mask created!", "Ok!!");
            }
            else
            {
                EditorUtility.DisplayDialog("Vegetation Studio", "ERROR!, no road mask created!", "Ok!!");
            }
        }
#endif

        GUILayout.Space(10);
        GUILayout.Label("Utility", EditorStyles.boldLabel);
        GUI.backgroundColor = new Color32(115, 242, 252, 255);
        if (GUILayout.Button("Update Length"))
        {
            layoutPath.calcLength();
        }

        /* ??
         * GUI.backgroundColor = Color.white;
         * //GUILayout.Label("Import from Max spline generated", EditorStyles.boldLabel);
         * if (GUILayout.Button("Export XML"))
         * {
         *  var path = EditorUtility.SaveFilePanel("Select the folder to store the XML",
         *      "", "path.xml", "xml");
         *  if (path.Length != 0)
         *  {
         *      layoutPath.ExportXml(path);
         *  }
         * }*/
        if (selectedIndex >= 0)
        {
            GUI.backgroundColor = Color.red;
            if (GUILayout.Button("Remove selected point"))
            {
                layoutPath.RemoveAt(selectedIndex);
            }
        }

        GUI.backgroundColor = new Color32(220, 157, 170, 255);
        if (GUILayout.Button("Invert points"))
        {
            layoutPath.Invert();
        }

        GUI.backgroundColor = new Color(1.0f, 0.647f, 0.0f);
        if (GUILayout.Button("Clear"))
        {
            layoutPath.Clear();
        }

        GUI.backgroundColor = Color.white;
    }