Example #1
0
        public void Find(IEnumerable <JunctionBase> junctions, IEnumerable <SplineBase> splines)
        {
            m_queue.Clear();

            m_visitedSplines   = new HashSet <SplineBase>(splines.Distinct());
            m_visitedJunctions = new HashSet <JunctionBase>(junctions.Distinct());
            foreach (JunctionBase junction in m_visitedJunctions)
            {
                m_queue.Enqueue(junction);
            }

            foreach (SplineBase spline in m_visitedSplines)
            {
                ExtractJunctions(spline);
            }

            while (m_queue.Count != 0)
            {
                JunctionBase junction = m_queue.Dequeue();
                for (int i = 0; i < junction.ConnectionsCount; ++i)
                {
                    SplineBase spline = junction.GetSpline(i);
                    if (spline != null)
                    {
                        if (!m_visitedSplines.Contains(spline))
                        {
                            ExtractJunctions(spline);
                            m_visitedSplines.Add(spline);
                        }
                    }
                }
            }
        }
Example #2
0
        private void EndDrag()
        {
            if (m_junctionDrag)
            {
                if (m_dragJunction.ConnectionsCount < 2)
                {
                    DestroyObject(m_dragJunction.gameObject);
                }
            }

            m_beforeSplinePointCreatorDrag = false;
            m_splinePointCreatorDrag       = false;

            m_splinePointDrag = false;

            m_beforeJunctionDrag = false;
            m_junctionDrag       = false;

            m_splineCtrlPointDrag = false;

            m_newJunction        = null;
            m_dragSpline         = null;
            m_dragPointIndex     = -1;
            m_dragCtrlPointIndex = -1;
            m_dragJunction       = null;
        }
 public ForkEventArgs(SplineBase spline, int splinePointIndex)
 {
     SelectedConnectionIndex = -1;
     m_junction         = spline.GetJunction(splinePointIndex);
     m_spline           = spline;
     m_splinePointIndex = splinePointIndex;
 }
 public void SetJunction(JunctionBase junction)
 {
     if (Spline != null)
     {
         Spline.SetJunction(Index, junction);
     }
 }
Example #5
0
        public virtual void Disable()
        {
            EndDrag();

            if (m_selectedJunctions != null)
            {
                for (int i = 0; i < m_selectedJunctions.Length; ++i)
                {
                    JunctionBase junction = m_selectedJunctions[i];
                    if (junction != null)
                    {
                        junction.Unselect();
                    }
                }
            }

            if (m_splineRenderers != null)
            {
                for (int i = 0; i < m_splineRenderers.Length; ++i)
                {
                    SplineRenderer splineRenderer = m_splineRenderers[i];
                    if (splineRenderer != null)
                    {
                        splineRenderer.IsSelected = false;
                    }
                }
            }

            if (m_selectedSplinePoints != null)
            {
                for (int i = 0; i < m_selectedSplinePoints.Length; ++i)
                {
                    SplinePointBase splinePoint = m_selectedSplinePoints[i];
                    if (splinePoint != null)
                    {
                        splinePoint.Unselect();
                    }
                }
            }

            if (m_selectedBezierPoints != null)
            {
                for (int i = 0; i < m_selectedBezierPoints.Length; ++i)
                {
                    ControlPoint bezierPoint = m_selectedBezierPoints[i];
                    if (bezierPoint != null)
                    {
                        bezierPoint.Unselect();
                    }
                }
            }

            m_splines              = null;
            m_splineRenderers      = null;
            m_selectedSplines      = null;
            m_selectedJunctions    = null;
            m_selectedSplinePoints = null;
            m_selectedBezierPoints = null;
        }
Example #6
0
 private void ExtractJunctions(SplineBase spline)
 {
     for (int i = 0; i < spline.PointsCount; ++i)
     {
         JunctionBase junction = spline.GetJunction(i);
         if (junction != null && !m_visitedJunctions.Contains(junction))
         {
             m_visitedJunctions.Add(junction);
             m_queue.Enqueue(junction);
         }
     }
 }
        protected virtual void OnSceneGUI()
        {
            if (m_splineBaseEditor == null)
            {
                m_splineBaseEditor = SplineBaseEditorImpl.Instance;
                m_splineBaseEditor.Enable(Selection.objects, Selection.activeObject);
            }
            JunctionBase junction = (JunctionBase)target;

            if (Selection.activeObject == junction.gameObject)
            {
                m_splineBaseEditor.SceneGUI();
            }
        }
Example #8
0
        public bool IsControlPointVisible(int pointIndex, int controlPointIndex)
        {
            JunctionBase junction = m_spline.GetJunction(pointIndex);

            if (junction != null)
            {
                if (m_spline.InterpolationMode == 1)
                {
                    return(false);
                }

                if (junction.GetSpline(0) != m_spline || junction.GetSplinePointIndex(0) != pointIndex)
                {
                    return(false);
                }

                return(true);
            }


            if (m_spline.InterpolationMode == 0)
            {
                if (pointIndex == 0 && controlPointIndex == 0 || pointIndex == m_spline.PointsCount - 1 && controlPointIndex == m_spline.GetCtrlPointsCount(pointIndex) - 1)
                {
                    return(false);
                }
            }
            else
            {
                if (pointIndex != 0 && pointIndex != m_spline.PointsCount - 1)
                {
                    return(false);
                }

                if (pointIndex == 0 && controlPointIndex == m_spline.GetCtrlPointsCount(pointIndex) - 1 || pointIndex == m_spline.PointsCount - 1 && controlPointIndex == 0)
                {
                    return(false);
                }
            }


            return(true);
        }
Example #9
0
        private void SnapToGrid()
        {
            for (int i = 0; i < m_selectedSplines.Length; ++i)
            {
                SplineBase spline = m_selectedSplines[i];
                if (spline != null)
                {
                    SnapToGrid(spline.transform);
                }
            }

            for (int i = 0; i < m_selectedJunctions.Length; ++i)
            {
                JunctionBase junction = m_selectedJunctions[i];
                if (junction != null)
                {
                    SnapToGrid(junction.transform);
                }
            }

            for (int i = 0; i < m_selectedSplinePoints.Length; ++i)
            {
                SplinePointBase splinePoint = m_selectedSplinePoints[i];
                if (splinePoint != null)
                {
                    SnapToGrid(splinePoint.transform);
                }
            }

            for (int i = 0; i < m_selectedBezierPoints.Length; ++i)
            {
                ControlPoint ctrlPoint = m_selectedBezierPoints[i];
                if (ctrlPoint != null)
                {
                    SnapToGrid(ctrlPoint.transform);
                }
            }
        }
Example #10
0
        private bool BeginDragSplinePoint()
        {
            Vector3 position;

            if (GetPositionOnDragPlane(ScreenPointToRay(MousePosition), out position))
            {
                Vector3     offset = position - m_dragSpline.GetPointPosition(m_dragPointIndex);
                const float s      = 0.1f;
                if (offset.magnitude > HandleSize(m_beginDragPosition) * s)
                {
                    JunctionBase junction = m_dragSpline.GetJunction(m_dragPointIndex);

                    if (m_dragPointIndex == 0 && junction == null)
                    {
                        m_dragSpline.Prepend(position);

                        GameObject splinePoint = m_dragSpline.GetPoint(0);

                        RegisterCreatedObjectUndo(splinePoint, "BH.S3.Prepend");
                        SetTransformParentUndo(splinePoint.transform, splinePoint.transform.parent, "BH.S3.Prepend");
                    }
                    else if (m_dragPointIndex == m_dragSpline.PointsCount - 1 && junction == null)
                    {
                        m_dragSpline.Append(position);
                        m_dragPointIndex = m_dragSpline.PointsCount - 1;
                        RegisterCreatedObjectUndo(m_dragSpline.GetPoint(m_dragPointIndex), "BH.S3.Append");
                    }
                    else
                    {
                        Vector3 dir;
                        if (m_dragSpline.CurveCount == m_dragPointIndex)
                        {
                            dir = m_dragSpline.GetDirection(1.0f);
                        }
                        else
                        {
                            dir = m_dragSpline.GetDirection(0, m_dragPointIndex);
                        }

                        bool isOut           = Mathf.Sign(Vector3.Dot(offset.normalized, dir)) >= 0;
                        int  connectionIndex = m_dragSpline.CreateBranch(m_dragPointIndex, isOut);

                        junction = m_dragSpline.GetJunction(m_dragPointIndex);

                        m_dragSpline = junction.GetSpline(connectionIndex);
                        RegisterCreatedObjectUndo(m_dragSpline.gameObject, "BH.S3.Branch");

                        if (junction.ConnectionsCount == 2)
                        {
                            m_newJunction = junction;
                            RegisterCreatedObjectUndo(junction.gameObject, "BH.S3.Branch");
                        }

                        m_splines = m_splines.Add(m_dragSpline);

                        SplineRenderer splineRenderer = m_dragSpline.GetComponent <SplineRenderer>();
                        m_splineRenderers = m_splineRenderers.Add(splineRenderer);

                        if (splineRenderer != null)
                        {
                            splineRenderer.IsSelected = true;
                        }

                        m_dragPointIndex = isOut ? 1 : 0;
                    }

                    return(true);
                }
            }
            return(false);
        }
Example #11
0
        public virtual void SceneGUI()
        {
            if (m_refSpline != null)
            {
                if (m_refSplinePosition != m_refSpline.transform.position ||
                    m_refSplineRotation != m_refSpline.transform.rotation ||
                    m_refSplineScale != m_refSpline.transform.localScale)
                {
                    m_refSpline.UpdateJunctions();
                    m_refSplinePosition = m_refSpline.transform.position;
                    m_refSplineRotation = m_refSpline.transform.rotation;
                    m_refSplineScale    = m_refSpline.transform.localScale;
                }
            }

            SplineEditorEventArgs eventArgs;
            SplineEditorEvent     eventType = GetEvent(out eventArgs);

            if (m_splinePointCreatorDrag || m_splinePointDrag || m_beforeSplinePointCreatorDrag || m_beforeJunctionDrag || m_junctionDrag || m_splineCtrlPointDrag)
            {
                if (IsMouseUp())
                {
                    EndDrag();
                }
            }

            if (IsSnapToGrid())
            {
                SnapToGrid();
            }

            switch (eventType)
            {
            case SplineEditorEvent.SplineCtrlPointDrag:
            {
                m_dragSpline         = eventArgs.Spline;
                m_dragPointIndex     = eventArgs.PointIndex;
                m_dragCtrlPointIndex = eventArgs.CtrlPointIndex;
                GetDragPlane(m_dragSpline.GetCtrlPointPosition(m_dragPointIndex, m_dragCtrlPointIndex));
                if (GetPositionOnDragPlane(ScreenPointToRay(MousePosition), out m_beginDragPosition))
                {
                    GameObject ctrlPoint   = m_dragSpline.GetCtrlPoint(m_dragPointIndex, m_dragCtrlPointIndex);
                    GameObject splinePoint = m_dragSpline.GetPoint(m_dragPointIndex);
                    if (ctrlPoint != null)
                    {
                        RecordObject(ctrlPoint.transform, "BH.S3.DragSplinePoint");
                    }
                    if (splinePoint != null)
                    {
                        RecordObject(splinePoint.transform, "BH.S3.DragSplinePoint");
                    }
                    m_splineCtrlPointDrag = true;
                }
                else
                {
                    m_dragSpline         = null;
                    m_dragPointIndex     = -1;
                    m_dragCtrlPointIndex = -1;
                }
            }
            break;

            case SplineEditorEvent.SplineCtrlPointDrop:
            {
                if (m_splineCtrlPointDrag)
                {
                    EndDrag();
                }
                else
                {
                    if (eventArgs.CtrlPointIndex >= 0)
                    {
                        GameObject splinePoint = eventArgs.Spline.GetCtrlPoint(eventArgs.PointIndex, eventArgs.CtrlPointIndex);
                        Select(splinePoint);
                    }
                    else
                    {
                        GameObject splinePoint = eventArgs.Spline.GetPoint(eventArgs.PointIndex);
                        Select(splinePoint);
                    }
                }
            }
            break;

            case SplineEditorEvent.SplinePointCreatorDrag:
            {
                if (!m_beforeSplinePointCreatorDrag && !m_splinePointCreatorDrag)
                {
                    m_dragSpline     = eventArgs.Spline;
                    m_dragPointIndex = eventArgs.PointIndex;
                    GetDragPlane(m_dragSpline.GetPointPosition(m_dragPointIndex));
                    if (GetPositionOnDragPlane(ScreenPointToRay(MousePosition), out m_beginDragPosition))
                    {
                        m_beforeSplinePointCreatorDrag = true;
                    }
                    else
                    {
                        m_dragSpline     = null;
                        m_dragPointIndex = -1;
                    }
                }
            }
            break;

            case SplineEditorEvent.SplinePointCreatorDrop:
            {
                if (m_splinePointCreatorDrag)
                {
                    SplineBase dropSpline     = eventArgs.Spline;
                    int        dropPointIndex = eventArgs.PointIndex;
                    if (m_dragPointIndex >= 0 && m_dragSpline != null && dropPointIndex >= 0 && dropSpline != null && (dropSpline != m_dragSpline || dropPointIndex != m_dragPointIndex))
                    {
                        dropSpline.Connect(dropPointIndex, m_dragSpline, m_dragPointIndex);
                        JunctionBase junction = dropSpline.GetJunction(dropPointIndex);
                        if (junction.ConnectionsCount == 2)
                        {
                            RegisterCreatedObjectUndo(junction.gameObject, "BH.S3.Junction");
                        }
                    }

                    Select(m_dragSpline.gameObject);
                    EndDrag();
                }
                else
                {
                    if (eventArgs.CtrlPointIndex >= 0)
                    {
                        GameObject splinePoint = eventArgs.Spline.GetCtrlPoint(eventArgs.PointIndex, eventArgs.CtrlPointIndex);
                        Select(splinePoint);
                    }
                    else
                    {
                        GameObject splinePoint = eventArgs.Spline.GetPoint(eventArgs.PointIndex);
                        Select(splinePoint);
                    }
                }
            }
            break;

            case SplineEditorEvent.SplinePointDrag:
            {
                if (!m_splinePointDrag)
                {
                    m_dragSpline     = eventArgs.Spline;
                    m_dragPointIndex = eventArgs.PointIndex;
                    GetDragPlane(m_dragSpline.GetPointPosition(m_dragPointIndex));

                    if (GetPositionOnDragPlane(ScreenPointToRay(MousePosition), out m_beginDragPosition))
                    {
                        m_splinePointDrag = true;

                        JunctionBase junction = m_dragSpline.GetJunction(m_dragPointIndex);
                        if (junction == null)
                        {
                            GameObject splinePointGO = m_dragSpline.GetPoint(m_dragPointIndex);
                            if (splinePointGO != null)
                            {
                                RegisterFullObjectHierarchyUndo(splinePointGO, "BH.S3.DragSplinePoint");
                            }
                        }
                        else
                        {
                            int connectionsCount = junction.ConnectionsCount;
                            for (int i = 0; i < connectionsCount; ++i)
                            {
                                SplineBase spline        = junction.GetSpline(i);
                                int        pointIndex    = junction.GetSplinePointIndex(i);
                                GameObject splinePointGO = spline.GetPoint(pointIndex);
                                if (splinePointGO != null)
                                {
                                    RegisterFullObjectHierarchyUndo(splinePointGO, "BH.S3.DragSplinePoint");
                                }
                            }
                            RecordObject(junction.transform, "BH.S3.DragSplinePoint");
                        }
                    }
                    else
                    {
                        m_dragSpline     = null;
                        m_dragPointIndex = -1;
                    }
                }
            }
            break;

            case SplineEditorEvent.SplinePointDrop:
            {
                if (m_splinePointDrag)
                {
                    SplineBase dropSpline     = eventArgs.Spline;
                    int        dropPointIndex = eventArgs.PointIndex;
                    if (m_dragPointIndex >= 0 && m_dragSpline != null && dropPointIndex >= 0 && dropSpline != null && (dropSpline != m_dragSpline || dropPointIndex != m_dragPointIndex))
                    {
                        JunctionBase dropJunction = dropSpline.GetJunction(dropPointIndex);
                        JunctionBase dragJunction = m_dragSpline.GetJunction(m_dragPointIndex);
                        if (dragJunction != dropJunction || dragJunction == null && dropJunction == null)
                        {
                            if (dropJunction != null)
                            {
                                RecordObject(dropJunction, "BH.S3.EndDragSplinePoint");
                                GameObject splinePointGO = m_dragSpline.GetPoint(m_dragPointIndex);
                                if (splinePointGO != null)
                                {
                                    SplinePointBase splinePoint = splinePointGO.GetComponent <SplinePointBase>();
                                    if (splinePoint != null)
                                    {
                                        RecordObject(splinePoint, "BH.S3.EndDragSplinePoint");
                                    }
                                }
                            }


                            if (dragJunction != null)
                            {
                                RecordObject(dragJunction, "BH.S3.EndDragSplinePoint");
                                GameObject splinePointGO = dropSpline.GetPoint(dropPointIndex);
                                if (splinePointGO != null)
                                {
                                    SplinePointBase splinePoint = splinePointGO.GetComponent <SplinePointBase>();
                                    if (splinePoint != null)
                                    {
                                        RegisterFullObjectHierarchyUndo(splinePointGO, "BH.S3.EndDragSplinePoint");
                                    }
                                }
                            }

                            dropSpline.Connect(dropPointIndex, m_dragSpline, m_dragPointIndex);

                            if (dragJunction != null && dropJunction != null)
                            {
                                DestroyObject(dragJunction.gameObject);
                            }

                            if (dropJunction == null)
                            {
                                dropJunction = dropSpline.GetJunction(dropPointIndex);
                                if (dropJunction.ConnectionsCount == 2)
                                {
                                    RegisterCreatedObjectUndo(dropJunction.gameObject, "BH.S3.EndDragSplinePoint");
                                }
                            }
                        }
                    }
                }
                else
                {
                    GameObject splinePoint = eventArgs.Spline.GetPoint(eventArgs.PointIndex);
                    Select(splinePoint);
                }

                EndDrag();
            }
            break;

            case SplineEditorEvent.JunctionClick:
            {
                Select(eventArgs.Junction.gameObject);
            }
            break;

            case SplineEditorEvent.JunctionDrag:
            {
                if (!m_beforeJunctionDrag && !m_junctionDrag)
                {
                    m_dragJunction = eventArgs.Junction;
                    GetDragPlane(m_dragJunction.transform.position);
                    if (GetPositionOnDragPlane(ScreenPointToRay(MousePosition), out m_beginDragPosition))
                    {
                        m_beforeJunctionDrag = true;
                    }
                    else
                    {
                        m_dragJunction = null;
                    }
                }
            }
            break;
            }

            if (m_beforeSplinePointCreatorDrag)
            {
                if (BeginDragSplinePoint())
                {
                    m_beforeSplinePointCreatorDrag = false;
                    m_splinePointCreatorDrag       = true;
                }
            }

            if (m_splinePointCreatorDrag)
            {
                DragSplinePoint();
            }

            if (m_splinePointDrag)
            {
                DragSplinePointUsingOffset();
            }

            if (m_splineCtrlPointDrag)
            {
                DragSplineCtrlPoint();
            }

            if (m_beforeJunctionDrag)
            {
                if (BeginDragJunction())
                {
                    m_beforeJunctionDrag = false;
                    m_junctionDrag       = true;
                }
            }

            if (m_junctionDrag)
            {
                DragSplinePoint();
            }
        }
Example #12
0
        public virtual void Enable(UnityObject[] selection, UnityObject activeObject)
        {
            Disable();

            m_refSpline = selection.Where(s => s is GameObject && ((GameObject)s).GetComponent <SplineBase>()).Select(g => ((GameObject)g).GetComponent <SplineBase>()).FirstOrDefault();
            if (m_refSpline != null)
            {
                m_refSplinePosition = m_refSpline.transform.position;
                m_refSplineRotation = m_refSpline.transform.rotation;
                m_refSplineScale    = m_refSpline.transform.localScale;
            }
            HashSet <JunctionBase>    junctions    = new HashSet <JunctionBase>();
            HashSet <SplineBase>      splines      = new HashSet <SplineBase>();
            HashSet <SplinePointBase> splinePoints = new HashSet <SplinePointBase>();
            HashSet <ControlPoint>    bezierPoints = new HashSet <ControlPoint>();

            List <SplineBase> selectedSplines = new List <SplineBase>();

            for (int i = 0; i < selection.Length; ++i)
            {
                UnityObject obj = selection[i];
                if (obj is GameObject)
                {
                    GameObject go = (GameObject)obj;

                    SplineBase spline = go.GetComponent <SplineBase>();
                    if (spline != null)
                    {
                        if (!splines.Contains(spline))
                        {
                            splines.Add(spline);
                        }

                        selectedSplines.Add(spline);
                    }
                    SplinePointBase splinePoint = go.GetComponent <SplinePointBase>();
                    if (splinePoint != null)
                    {
                        SplineBase parentSpline = splinePoint.GetComponentInParent <SplineBase>();
                        if (parentSpline != null)
                        {
                            if (!splines.Contains(parentSpline))
                            {
                                splines.Add(parentSpline);
                            }
                        }
                        if (!splinePoints.Contains(splinePoint))
                        {
                            splinePoints.Add(splinePoint);
                        }
                    }
                    ControlPoint bezierPoint = go.GetComponent <ControlPoint>();
                    if (bezierPoint != null)
                    {
                        SplineBase parentSpline = bezierPoint.GetComponentInParent <SplineBase>();
                        if (parentSpline != null)
                        {
                            if (!splines.Contains(parentSpline))
                            {
                                splines.Add(parentSpline);
                            }
                        }
                        if (!bezierPoints.Contains(bezierPoint))
                        {
                            bezierPoints.Add(bezierPoint);
                        }
                    }
                    JunctionBase junction = go.GetComponent <Junction>();
                    if (junction != null)
                    {
                        if (!junctions.Contains(junction))
                        {
                            junctions.Add(junction);
                        }
                    }
                }
            }

            m_selectedJunctions = junctions.ToArray();
            for (int i = 0; i < m_selectedJunctions.Length; ++i)
            {
                m_selectedJunctions[i].Select();
            }

            ConnectedSplinesFinder finder = new ConnectedSplinesFinder();

            finder.Find(junctions, splines);

            m_splines         = finder.Splines;
            m_splineRenderers = new SplineRenderer[m_splines.Length];
            for (int i = 0; i < m_splines.Length; ++i)
            {
                m_splineRenderers[i]            = m_splines[i].GetComponent <SplineRenderer>();
                m_splineRenderers[i].IsSelected = true;
            }

            m_selectedSplinePoints = splinePoints.ToArray();
            for (int i = 0; i < m_selectedSplinePoints.Length; ++i)
            {
                m_selectedSplinePoints[i].Select();
            }

            m_selectedBezierPoints = bezierPoints.ToArray();
            for (int i = 0; i < m_selectedBezierPoints.Length; ++i)
            {
                m_selectedBezierPoints[i].Select();
            }
            SyncCtrlPoints();

            m_selectedSplines = selectedSplines.ToArray();
        }
Example #13
0
 public SplineEditorEventArgs(JunctionBase junction)
 {
     Junction = junction;
 }
Example #14
0
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            ControlPoint[] points = SplineBaseEditorImpl.Instance.SelectedBezierPoints;

            int mode = points[0].Mode;

            for (int i = 1; i < points.Length; ++i)
            {
                ControlPoint bezierPoint = points[i];
                if (bezierPoint.Mode != mode)
                {
                    mode = -1;
                    break;
                }
            }

            int selectedMode;

            if (mode == -1)
            {
                selectedMode = EditorGUILayout.Popup("Mode", mode + 1, new[] { "Different Modes", "Free", "Aligned", "Mirrored" }) - 1;
            }
            else
            {
                selectedMode = EditorGUILayout.Popup("Mode", mode, new[] { "Free", "Aligned", "Mirrored" });
            }

            if (selectedMode != -1 && selectedMode != mode)
            {
                for (int i = 0; i < points.Length; ++i)
                {
                    ControlPoint bezierPoint = points[i];
                    if (bezierPoint.SplinePoint != null)
                    {
                        JunctionBase junction = bezierPoint.SplinePoint.GetJunction();
                        if (junction != null)
                        {
                            int connectionsCount = junction.ConnectionsCount;
                            for (int c = 0; c < connectionsCount; ++c)
                            {
                                SplineBase spline           = junction.GetSpline(c);
                                int        splinePointIndex = junction.GetSplinePointIndex(c);

                                GameObject      splinePointGO = spline.GetPoint(splinePointIndex);
                                SplinePointBase splinePoint   = splinePointGO.GetComponent <SplinePointBase>();
                                if (splinePoint != null)
                                {
                                    Undo.RecordObject(splinePoint, "BH.S3.BezierPoint.Mode");
                                }

                                GameObject ctrlPoint = spline.GetCtrlPoint(splinePointIndex, 0);
                                if (ctrlPoint != null)
                                {
                                    Undo.RecordObject(ctrlPoint.transform, "BH.S3.BezierPoint.Mode");
                                }
                                GameObject twinPoint = spline.GetCtrlPoint(splinePointIndex, 1);
                                if (twinPoint != null)
                                {
                                    Undo.RecordObject(twinPoint.transform, "BH.S3.BezierPoint.Mode");
                                }
                            }
                        }
                        else
                        {
                            Undo.RecordObject(bezierPoint.SplinePoint, "BH.S3.BezierPoint.Mode");

                            GameObject twinPoint = bezierPoint.TwinPoint;
                            if (twinPoint != null)
                            {
                                Undo.RecordObject(twinPoint.transform, "BH.S3.BezierPoint.Mode");
                            }

                            Undo.RecordObject(bezierPoint.transform, "BH.S3.BezierPoint.Mode");
                        }
                    }

                    bezierPoint.Mode = selectedMode;
                }

                SceneView.RepaintAll();
            }
        }
Example #15
0
        private void SetDragPointRotation()
        {
            if (m_splinePointCreatorDrag || IsRotate())
            {
                if (m_dragSpline.PointsCount > 1)
                {
                    JunctionBase junction = m_dragSpline.GetJunction(m_dragPointIndex);
                    if (junction != null)
                    {
                        SplineBase closestSpline      = null;
                        int        closestPointIndex  = -1;
                        int        closestPointIndex2 = -1;

                        int   ctrlPointIndex = -1;
                        float minMag         = float.MaxValue;

                        int connectionsCount = junction.ConnectionsCount;
                        for (int i = 0; i < connectionsCount; ++i)
                        {
                            SplineBase spline     = junction.GetSpline(i);
                            int        pointIndex = junction.GetSplinePointIndex(i);


                            if (pointIndex == spline.PointsCount - 1)
                            {
                                float mag = (spline.GetPointPosition(pointIndex) - spline.GetPointPosition(pointIndex - 1)).sqrMagnitude;
                                if (mag < minMag)
                                {
                                    ctrlPointIndex     = 1;
                                    closestPointIndex  = pointIndex;
                                    closestPointIndex2 = pointIndex - 1;
                                    closestSpline      = spline;
                                    minMag             = mag;
                                }
                            }
                            else if (pointIndex == 0)
                            {
                                float mag = (spline.GetPointPosition(pointIndex) - spline.GetPointPosition(pointIndex + 1)).sqrMagnitude;
                                if (mag < minMag)
                                {
                                    ctrlPointIndex     = 0;
                                    closestPointIndex  = pointIndex;
                                    closestPointIndex2 = pointIndex + 1;
                                    closestSpline      = spline;
                                    minMag             = mag;
                                }
                            }
                            else
                            {
                                float mag1 = (spline.GetPointPosition(pointIndex) - spline.GetPointPosition(pointIndex - 1)).sqrMagnitude;
                                float mag2 = (spline.GetPointPosition(pointIndex) - spline.GetPointPosition(pointIndex + 1)).sqrMagnitude;
                                if (mag1 < mag2)
                                {
                                    if (mag1 < minMag)
                                    {
                                        ctrlPointIndex     = 1;
                                        closestPointIndex  = pointIndex;
                                        closestPointIndex2 = pointIndex - 1;
                                        closestSpline      = spline;
                                        minMag             = mag1;
                                    }
                                }
                                else
                                {
                                    if (mag2 < minMag)
                                    {
                                        ctrlPointIndex     = 0;
                                        closestPointIndex  = pointIndex;
                                        closestPointIndex2 = pointIndex + 1;
                                        closestSpline      = spline;
                                        minMag             = mag2;
                                    }
                                }
                            }
                        }

                        if (closestSpline != null)
                        {
                            Quaternion rotation = GetDragPointRotation(closestSpline, closestPointIndex, closestPointIndex2, ctrlPointIndex);
                            closestSpline.SetPointRotation(closestPointIndex, rotation);
                        }
                    }
                    else
                    {
                        if (m_dragPointIndex == 0)
                        {
                            Quaternion rotation = GetDragPointRotation(m_dragSpline, m_dragPointIndex, m_dragPointIndex + 1, 0);
                            m_dragSpline.SetPointRotation(m_dragPointIndex, rotation);
                        }
                        else if (m_dragPointIndex == m_dragSpline.PointsCount - 1)
                        {
                            Quaternion rotation = GetDragPointRotation(m_dragSpline, m_dragPointIndex, m_dragPointIndex - 1, 1);
                            m_dragSpline.SetPointRotation(m_dragPointIndex, rotation);
                        }
                        else
                        {
                            Vector3 prevPosition = m_dragSpline.GetPointPosition(m_dragPointIndex - 1);
                            Vector3 nextPosition = m_dragSpline.GetPointPosition(m_dragPointIndex + 1);
                            Vector3 position     = m_dragSpline.GetPointPosition(m_dragPointIndex);

                            float toPrevMag = (prevPosition - position).sqrMagnitude;
                            float toNextMag = (nextPosition - position).sqrMagnitude;
                            if (toNextMag >= toPrevMag)
                            {
                                Quaternion r1 = GetDragPointRotation(m_dragSpline, m_dragPointIndex, m_dragPointIndex - 1, 1);
                                m_dragSpline.SetPointRotation(m_dragPointIndex, r1);
                            }
                            else
                            {
                                Quaternion r2 = GetDragPointRotation(m_dragSpline, m_dragPointIndex, m_dragPointIndex + 1, 0);
                                m_dragSpline.SetPointRotation(m_dragPointIndex, r2);
                            }
                        }
                    }
                }
            }
        }
Example #16
0
        private void DrawSplinePoint(ref SplineEditorEventArgs args, ref SplineEditorEvent evnt, SplineRenderer splineRenderer, SplineBase spline, int j)
        {
            int mode = spline.GetPointMode(j);

            if (mode >= 0 && mode < splineRenderer.ControlPointColors.Length)
            {
                Handles.color = splineRenderer.ControlPointColors[mode];
            }

            int controlPointsCount = spline.GetCtrlPointsCount(j);

            for (int k = 0; k < controlPointsCount; ++k)
            {
                int scale = 1;
                if (!splineRenderer.IsControlPointVisible(j, k))
                {
                    scale = 0;
                }

                Vector3 ctrlPosition = spline.GetCtrlPointPosition(j, k);
                SplinePointHandle.Result ctrlPointDragResult = DrawPointHandle(splineRenderer, ctrlPosition, scale, true);
                if (ctrlPointDragResult == SplinePointHandle.Result.Drag)
                {
                    if (evnt == SplineEditorEvent.None)
                    {
                        args = new SplineEditorEventArgs(spline, j, k);
                        evnt = SplineEditorEvent.SplineCtrlPointDrag;
                    }
                }
                if (ctrlPointDragResult == SplinePointHandle.Result.EndDrag)
                {
                    if (evnt == SplineEditorEvent.None)
                    {
                        args = new SplineEditorEventArgs(spline, j, k);
                        evnt = SplineEditorEvent.SplineCtrlPointDrop;
                    }
                }
            }

            float dragHandleScale = 1;

            if (m_draggingDragPointHandle && spline == DragSpline && DragPointIndex == j)
            {
                dragHandleScale = 0;
            }

            JunctionBase junction = spline.GetJunction(j);

            if (junction != null && junction != NewJunction)
            {
                bool drawSplinePoint = junction == null || spline == junction.GetSpline(0) && j == junction.GetSplinePointIndex(0);
                if (drawSplinePoint)
                {
                    dragHandleScale = 1.5f;
                }
                else
                {
                    dragHandleScale = 0;
                }
            }


            Handles.color = splineRenderer.SplinePointColor;
            Vector3 position = spline.GetPointPosition(j);

            SplinePointHandle.Result dragResult = DrawDragHandle(splineRenderer, position, dragHandleScale, DragSpline == null);
            if (dragResult == SplinePointHandle.Result.Drag)
            {
                if (evnt == SplineEditorEvent.None)
                {
                    args = new SplineEditorEventArgs(spline, j);
                    evnt = SplineEditorEvent.SplinePointDrag;
                }
            }
            else if (dragResult == SplinePointHandle.Result.EndDrag)
            {
                if (evnt == SplineEditorEvent.None)
                {
                    args = new SplineEditorEventArgs(spline, j);
                    evnt = SplineEditorEvent.SplinePointDrop;
                }
            }
            else if (dragResult == SplinePointHandle.Result.PointerOver)
            {
                m_pointerOverSpline           = spline;
                m_pointerOverSplinePointIndex = j;
                m_pointerOver = true;
            }

            if (junction != null && junction != NewJunction)
            {
                Handles.color = splineRenderer.JunctionColor;
                JunctionHandle.Result junctionResult = DrawJunctionHandle(splineRenderer, junction.transform.position, 1);
                if (junctionResult == JunctionHandle.Result.Click)
                {
                    evnt = SplineEditorEvent.JunctionClick;
                    args = new SplineEditorEventArgs(junction);
                }
                else if (junctionResult == JunctionHandle.Result.Drag)
                {
                    evnt = SplineEditorEvent.JunctionDrag;
                    args = new SplineEditorEventArgs(junction);
                }
            }

            Handles.color = splineRenderer.SplinePointColor;
            SplinePointHandle.Result result = DrawPointHandle(splineRenderer, position, 1, false);
            if (result == SplinePointHandle.Result.EndDrag)
            {
                m_draggingDragPointHandle = false;
                if (evnt == SplineEditorEvent.None)
                {
                    args = new SplineEditorEventArgs(spline, j);
                    evnt = SplineEditorEvent.SplinePointCreatorDrop;
                }
            }
            else if (result == SplinePointHandle.Result.Drag)
            {
                m_draggingDragPointHandle = true;
                if (evnt == SplineEditorEvent.None)
                {
                    args = new SplineEditorEventArgs(spline, j);
                    evnt = SplineEditorEvent.SplinePointCreatorDrag;
                }
            }
        }
Example #17
0
        private void Move(float deltaTime, bool allowRecursiveCall)
        {
            float v, deltaT;

            v      = m_spline.GetVelocity(m_t).magnitude;
            v     *= m_spline.CurveCount;
            deltaT = (deltaTime * Speed) / v;// F.1

            int pointIndex;

            if (deltaT >= 0)
            {
                pointIndex = m_spline.GetCurveIndex(m_t);
            }
            else
            {
                pointIndex = m_spline.GetCurveIndex(m_t) + 1;
            }

            int   nextPointIndex;
            float nextT       = m_t + deltaT;
            bool  endOfSpline = nextT <= 0.0f || 1.0f <= nextT;

            if (endOfSpline)
            {
                if (deltaT >= 0)
                {
                    nextPointIndex = (pointIndex + 1) % m_spline.PointsCount;
                }
                else
                {
                    nextPointIndex = (m_spline.PointsCount + pointIndex - 1) % m_spline.PointsCount;
                }
            }
            else
            {
                if (deltaT >= 0)
                {
                    nextPointIndex = m_spline.GetCurveIndex(nextT);
                }
                else
                {
                    nextPointIndex = m_spline.GetCurveIndex(nextT) + 1;
                }
            }


            bool continueIfLoop = IsLoop;

            if (pointIndex != nextPointIndex)
            {
                //Debug.Log("Point Index " + pointIndex + " Next Point Index " + nextPointIndex);
                JunctionBase junction = m_spline.GetJunction(nextPointIndex);
                if (junction != null)
                {
                    ForkEventArgs args = new ForkEventArgs(m_spline, nextPointIndex);
                    Fork.Invoke(args);

                    int connection = -1;
                    if (args.SelectedConnectionIndex == -1)
                    {
                        if (StopAtJunction)
                        {
                            IsRunning = false;
                            return;
                        }

                        if (endOfSpline)
                        {
                            if (deltaT > 0)
                            {
                                connection = junction.FirstOut();
                                if (connection == -1)
                                {
                                    connection = junction.FirstIn();
                                }
                            }
                            else
                            {
                                connection = junction.FirstIn();
                                if (connection == -1)
                                {
                                    connection = junction.FirstOut();
                                }
                            }

                            if (connection == -1)
                            {
                                continueIfLoop = false;
                            }
                        }
                    }
                    else
                    {
                        connection = args.SelectedConnectionIndex;
                    }

                    if (connection != -1)
                    {
                        float nextPointT = m_spline.GetTAt(nextPointIndex);
                        deltaT = m_t + deltaT - nextPointT;
                        float remDeltaTime = deltaT * v / Speed; //Calculated using F.1

                        m_spline = junction.GetSpline(connection);
                        int splinePointIndex = junction.GetSplinePointIndex(connection);
                        m_t = m_spline.GetTAt(splinePointIndex);

                        bool isOut = junction.IsOut(connection);
                        bool isIn  = junction.IsIn(connection);

                        if (isOut && !isIn && Speed < 0 || isIn && !isOut && Speed > 0)
                        {
                            Speed *= -1;
                        }

                        if (allowRecursiveCall)
                        {
                            Move(remDeltaTime, false);
                            return;
                        }
                        else
                        {
                            v      = m_spline.GetVelocity(m_t).magnitude;
                            v     *= m_spline.CurveCount;
                            deltaT = (remDeltaTime * Speed) / v;// F.1
                        }
                    }
                }
            }

            MoveOrStop(deltaT, endOfSpline, continueIfLoop);
        }