private void OnBeforeDeleteSelectedPoints()
        {
            string      nodeString  = "";
            List <Node> deleteNodes = new List <Node>();

            for (int i = 0; i < selectedPoints.Count; i++)
            {
                Node node = spline.GetNode(selectedPoints[i]);
                if (node)
                {
                    spline.DisconnectNode(selectedPoints[i]);
                    if (node.GetConnections().Length == 0)
                    {
                        deleteNodes.Add(node);
                        if (nodeString != "")
                        {
                            nodeString += ", ";
                        }
                        string trimmed = node.name.Trim();
                        if (nodeString.Length + trimmed.Length > 80)
                        {
                            nodeString += "...";
                        }
                        else
                        {
                            nodeString += node.name.Trim();
                        }
                    }
                }
            }

            if (deleteNodes.Count > 0)
            {
                string message = "The following nodes:\r\n" + nodeString + "\r\n were only connected to the currently selected points. Would you like to remove them from the scene?";
                if (EditorUtility.DisplayDialog("Remove nodes?", message, "Yes", "No"))
                {
                    for (int i = 0; i < deleteNodes.Count; i++)
                    {
                        Undo.DestroyObjectImmediate(deleteNodes[i].gameObject);
                    }
                }
            }

            int min = spline.pointCount - 1;

            for (int i = 0; i < selectedPoints.Count; i++)
            {
                if (selectedPoints[i] < min)
                {
                    min = selectedPoints[i];
                }
            }

            int pointsDeletedBefore = 0;

            for (int i = 0; i < spline.pointCount; i++)
            {
                if (selectedPoints.Contains(i))
                {
                    pointsDeletedBefore++;
                    continue;
                }
                Node node = spline.GetNode(i);
                if (pointsDeletedBefore > 0 && node)
                {
                    spline.TransferNode(i, i - pointsDeletedBefore);
                }
            }
        }
Example #2
0
        void Merge(int index, MergeSide mergingSide)
        {
            RecordUndo("Merge Splines");
            SplineComputer mergedSpline = availableMergeComputers[index];

            SplinePoint[]      mergedPoints = mergedSpline.GetPoints();
            SplinePoint[]      original     = spline.GetPoints();
            List <SplinePoint> pointsList   = new List <SplinePoint>();

            SplinePoint[] points;
            if (!mergeEndpoints)
            {
                points = new SplinePoint[mergedPoints.Length + original.Length];
            }
            else
            {
                points = new SplinePoint[mergedPoints.Length + original.Length - 1];
            }

            if (mergeSide == MergeSide.End)
            {
                if (mergingSide == MergeSide.Start)
                {
                    for (int i = 0; i < original.Length; i++)
                    {
                        pointsList.Add(original[i]);
                    }
                    for (int i = mergeEndpoints ? 1 : 0; i < mergedPoints.Length; i++)
                    {
                        pointsList.Add(mergedPoints[i]);
                    }
                }
                else
                {
                    for (int i = 0; i < original.Length; i++)
                    {
                        pointsList.Add(original[i]);
                    }
                    for (int i = 0; i < mergedPoints.Length - (mergeEndpoints ? 1 : 0); i++)
                    {
                        pointsList.Add(mergedPoints[(mergedPoints.Length - 1) - i]);
                    }
                }
            }
            else
            {
                if (mergingSide == MergeSide.Start)
                {
                    for (int i = 0; i < mergedPoints.Length - (mergeEndpoints ? 1 : 0); i++)
                    {
                        pointsList.Add(mergedPoints[(mergedPoints.Length - 1) - i]);
                    }
                    for (int i = 0; i < original.Length; i++)
                    {
                        pointsList.Add(original[i]);
                    }
                }
                else
                {
                    for (int i = mergeEndpoints ? 1 : 0; i < mergedPoints.Length; i++)
                    {
                        pointsList.Add(mergedPoints[i]);
                    }
                    for (int i = 0; i < original.Length; i++)
                    {
                        pointsList.Add(original[i]);
                    }
                }
            }
            points = pointsList.ToArray();
            double mergedPercent = (double)(mergedPoints.Length - 1) / (points.Length - 1);
            double from          = 0.0;
            double to            = 1.0;

            if (mergeSide == MergeSide.End)
            {
                from = 1.0 - mergedPercent;
                to   = 1.0;
            }
            else
            {
                from = 0.0;
                to   = mergedPercent;
            }


            List <Node> mergedNodes   = new List <Node>();
            List <int>  mergedIndices = new List <int>();

            for (int i = 0; i < mergedSpline.pointCount; i++)
            {
                Node node = mergedSpline.GetNode(i);
                if (node != null)
                {
                    mergedNodes.Add(node);
                    mergedIndices.Add(i);
                    Undo.RecordObject(node, "Disconnect Node");
                    mergedSpline.DisconnectNode(i);
                    i--;
                }
            }

            SplineUser[] subs = mergedSpline.GetSubscribers();
            for (int i = 0; i < subs.Length; i++)
            {
                mergedSpline.Unsubscribe(subs[i]);
                subs[i].spline   = spline;
                subs[i].clipFrom = DMath.Lerp(from, to, subs[i].clipFrom);
                subs[i].clipTo   = DMath.Lerp(from, to, subs[i].clipTo);
            }
            spline.SetPoints(points);

            if (mergeSide == MergeSide.Start)
            {
                spline.ShiftNodes(0, spline.pointCount - 1, mergedSpline.pointCount);
                for (int i = 0; i < mergedNodes.Count; i++)
                {
                    spline.ConnectNode(mergedNodes[i], mergedIndices[i]);
                }
            }
            else
            {
                for (int i = 0; i < mergedNodes.Count; i++)
                {
                    int connectIndex = mergedIndices[i] + original.Length;
                    if (mergeEndpoints)
                    {
                        connectIndex--;
                    }
                    spline.ConnectNode(mergedNodes[i], connectIndex);
                }
            }
            if (EditorUtility.DisplayDialog("Keep merged computer's GameObject?", "Do you want to keep the merged computer's game object?", "Yes", "No"))
            {
                Undo.DestroyObjectImmediate(mergedSpline);
            }
            else
            {
                for (int i = 0; i < mergedNodes.Count; i++)
                {
                    if (TransformUtility.IsParent(mergedNodes[i].transform, mergedSpline.transform))
                    {
                        Undo.SetTransformParent(mergedNodes[i].transform, mergedSpline.transform.parent, "Reparent Node");
                    }
                }
                Undo.DestroyObjectImmediate(mergedSpline.gameObject);
            }

            FindAvailableComputers();
        }