GetNearestEdge() 공개 정적인 메소드

public static GetNearestEdge ( Vector2 point, int &pathIndex, int &pointIndex0, int &pointIndex1, float &distance, bool loop ) : bool
point Vector2
pathIndex int
pointIndex0 int
pointIndex1 int
distance float
loop bool
리턴 bool
예제 #1
0
 public void OnSceneGUI()
 {
     if ((this.m_ActiveCollider != null) && !Tools.viewToolActive)
     {
         float   num;
         Vector2 offset  = this.m_ActiveCollider.offset;
         Event   current = Event.current;
         this.m_DeleteMode = current.command || current.control;
         Transform transform = this.m_ActiveCollider.transform;
         GUIUtility.keyboardControl         = 0;
         HandleUtility.s_CustomPickDistance = 50f;
         Plane plane = new Plane(-transform.forward, Vector3.zero);
         Ray   ray   = HandleUtility.GUIPointToWorldRay(current.mousePosition);
         plane.Raycast(ray, out num);
         Vector3 point   = ray.GetPoint(num);
         Vector2 vector3 = transform.InverseTransformPoint(point);
         if ((current.type == EventType.MouseMove) || this.m_FirstOnSceneGUIAfterReset)
         {
             int   num2;
             int   num3;
             int   num4;
             float num5;
             if (PolygonEditor.GetNearestPoint(vector3 - offset, out num2, out num3, out num5))
             {
                 this.m_SelectedPath     = num2;
                 this.m_SelectedVertex   = num3;
                 this.m_SelectedDistance = num5;
             }
             else
             {
                 this.m_SelectedPath = -1;
             }
             if (PolygonEditor.GetNearestEdge(vector3 - offset, out num2, out num3, out num4, out num5, this.m_LoopingCollider))
             {
                 this.m_SelectedEdgePath     = num2;
                 this.m_SelectedEdgeVertex0  = num3;
                 this.m_SelectedEdgeVertex1  = num4;
                 this.m_SelectedEdgeDistance = num5;
             }
             else
             {
                 this.m_SelectedEdgePath = -1;
             }
             if (current.type == EventType.MouseMove)
             {
                 current.Use();
             }
         }
         else if (current.type == EventType.MouseUp)
         {
             this.m_LeftIntersect  = false;
             this.m_RightIntersect = false;
         }
         bool flag  = false;
         bool flag2 = false;
         if ((this.m_SelectedPath != -1) && (this.m_SelectedEdgePath != -1))
         {
             Vector2 vector4;
             PolygonEditor.GetPoint(this.m_SelectedPath, this.m_SelectedVertex, out vector4);
             vector4 += offset;
             float num6 = HandleUtility.GetHandleSize(transform.TransformPoint((Vector3)vector4)) * 0.2f;
             flag2 = this.m_SelectedEdgeDistance < (this.m_SelectedDistance - num6);
             flag  = !flag2;
         }
         else if (this.m_SelectedPath != -1)
         {
             flag = true;
         }
         else if (this.m_SelectedEdgePath != -1)
         {
             flag2 = true;
         }
         if (this.m_DeleteMode && flag2)
         {
             flag2 = false;
             flag  = true;
         }
         bool flag3 = false;
         if (flag2 && !this.m_DeleteMode)
         {
             Vector2 vector6;
             Vector2 vector7;
             PolygonEditor.GetPoint(this.m_SelectedEdgePath, this.m_SelectedEdgeVertex0, out vector6);
             PolygonEditor.GetPoint(this.m_SelectedEdgePath, this.m_SelectedEdgeVertex1, out vector7);
             vector6 += offset;
             vector7 += offset;
             Vector3 start = transform.TransformPoint((Vector3)vector6);
             Vector3 end   = transform.TransformPoint((Vector3)vector7);
             start.z       = end.z = 0f;
             Handles.color = Color.green;
             Vector3[] points = new Vector3[] { start, end };
             Handles.DrawAAPolyLine((float)4f, points);
             Handles.color = Color.white;
             Vector2 vector10 = this.GetNearestPointOnEdge(transform.TransformPoint((Vector3)vector3), start, end);
             EditorGUI.BeginChangeCheck();
             float handleSize = HandleUtility.GetHandleSize((Vector3)vector10) * 0.04f;
             Handles.color = Color.green;
             vector10      = Handles.Slider2D((Vector3)vector10, new Vector3(0f, 0f, 1f), new Vector3(1f, 0f, 0f), new Vector3(0f, 1f, 0f), handleSize, new Handles.DrawCapFunction(Handles.DotCap), Vector3.zero);
             Handles.color = Color.white;
             if (EditorGUI.EndChangeCheck())
             {
                 PolygonEditor.InsertPoint(this.m_SelectedEdgePath, this.m_SelectedEdgeVertex1, ((Vector2)((vector6 + vector7) / 2f)) - offset);
                 this.m_SelectedPath     = this.m_SelectedEdgePath;
                 this.m_SelectedVertex   = this.m_SelectedEdgeVertex1;
                 this.m_SelectedDistance = 0f;
                 flag  = true;
                 flag3 = true;
             }
         }
         if (flag)
         {
             Vector2 vector11;
             PolygonEditor.GetPoint(this.m_SelectedPath, this.m_SelectedVertex, out vector11);
             vector11 += offset;
             Vector3 world = transform.TransformPoint((Vector3)vector11);
             world.z = 0f;
             Vector2 a    = HandleUtility.WorldToGUIPoint(world);
             float   num8 = HandleUtility.GetHandleSize(world) * 0.04f;
             if (((this.m_DeleteMode && (current.type == EventType.MouseDown)) && (Vector2.Distance(a, Event.current.mousePosition) < 50f)) || this.DeleteCommandEvent(current))
             {
                 if ((current.type != EventType.ValidateCommand) && (PolygonEditor.GetPointCount(this.m_SelectedPath) > this.m_MinPathPoints))
                 {
                     PolygonEditor.RemovePoint(this.m_SelectedPath, this.m_SelectedVertex);
                     this.Reset();
                     flag3 = true;
                 }
                 current.Use();
             }
             EditorGUI.BeginChangeCheck();
             Handles.color = !this.m_DeleteMode ? Color.green : Color.red;
             Vector3 position = Handles.Slider2D(world, new Vector3(0f, 0f, 1f), new Vector3(1f, 0f, 0f), new Vector3(0f, 1f, 0f), num8, new Handles.DrawCapFunction(Handles.DotCap), Vector3.zero);
             Handles.color = Color.white;
             if (EditorGUI.EndChangeCheck() && !this.m_DeleteMode)
             {
                 vector11 = transform.InverseTransformPoint(position) - offset;
                 PolygonEditor.TestPointMove(this.m_SelectedPath, this.m_SelectedVertex, vector11, out this.m_LeftIntersect, out this.m_RightIntersect, this.m_LoopingCollider);
                 PolygonEditor.SetPoint(this.m_SelectedPath, this.m_SelectedVertex, vector11);
                 flag3 = true;
             }
             if (!flag3)
             {
                 this.DrawEdgesForSelectedPoint(position, transform, this.m_LeftIntersect, this.m_RightIntersect, this.m_LoopingCollider);
             }
         }
         if (flag3)
         {
             Undo.RecordObject(this.m_ActiveCollider, "Edit Collider");
             PolygonEditor.ApplyEditing(this.m_ActiveCollider);
         }
         if (this.DeleteCommandEvent(current))
         {
             Event.current.Use();
         }
         this.m_FirstOnSceneGUIAfterReset = false;
     }
 }
예제 #2
0
        public void OnSceneGUI()
        {
            if (this.m_ActiveCollider == null)
            {
                return;
            }
            if (Tools.viewToolActive)
            {
                return;
            }
            Vector2 offset  = this.m_ActiveCollider.offset;
            Event   current = Event.current;

            this.m_DeleteMode = (current.command || current.control);
            Transform transform = this.m_ActiveCollider.transform;

            GUIUtility.keyboardControl         = 0;
            HandleUtility.s_CustomPickDistance = 50f;
            Plane plane = new Plane(-transform.forward, Vector3.zero);
            Ray   ray   = HandleUtility.GUIPointToWorldRay(current.mousePosition);
            float distance;

            plane.Raycast(ray, out distance);
            Vector3 point  = ray.GetPoint(distance);
            Vector2 vector = transform.InverseTransformPoint(point);

            if (current.type == EventType.MouseMove || this.m_FirstOnSceneGUIAfterReset)
            {
                int   num;
                int   num2;
                float num3;
                if (PolygonEditor.GetNearestPoint(vector - offset, out num, out num2, out num3))
                {
                    this.m_SelectedPath     = num;
                    this.m_SelectedVertex   = num2;
                    this.m_SelectedDistance = num3;
                }
                else
                {
                    this.m_SelectedPath = -1;
                }
                int selectedEdgeVertex;
                if (PolygonEditor.GetNearestEdge(vector - offset, out num, out num2, out selectedEdgeVertex, out num3, this.m_LoopingCollider))
                {
                    this.m_SelectedEdgePath     = num;
                    this.m_SelectedEdgeVertex0  = num2;
                    this.m_SelectedEdgeVertex1  = selectedEdgeVertex;
                    this.m_SelectedEdgeDistance = num3;
                }
                else
                {
                    this.m_SelectedEdgePath = -1;
                }
                if (current.type == EventType.MouseMove)
                {
                    current.Use();
                }
            }
            else if (current.type == EventType.MouseUp)
            {
                this.m_LeftIntersect  = false;
                this.m_RightIntersect = false;
            }
            bool flag  = false;
            bool flag2 = false;

            if (this.m_SelectedPath != -1 && this.m_SelectedEdgePath != -1)
            {
                Vector2 vector2;
                PolygonEditor.GetPoint(this.m_SelectedPath, this.m_SelectedVertex, out vector2);
                vector2 += offset;
                Vector3 position = transform.TransformPoint(vector2);
                float   num4     = HandleUtility.GetHandleSize(position) * 0.2f;
                flag2 = (this.m_SelectedEdgeDistance < this.m_SelectedDistance - num4);
                flag  = !flag2;
            }
            else if (this.m_SelectedPath != -1)
            {
                flag = true;
            }
            else if (this.m_SelectedEdgePath != -1)
            {
                flag2 = true;
            }
            if (this.m_DeleteMode && flag2)
            {
                flag2 = false;
                flag  = true;
            }
            bool flag3 = false;

            if (flag2 && !this.m_DeleteMode)
            {
                Vector2 vector3;
                PolygonEditor.GetPoint(this.m_SelectedEdgePath, this.m_SelectedEdgeVertex0, out vector3);
                Vector2 vector4;
                PolygonEditor.GetPoint(this.m_SelectedEdgePath, this.m_SelectedEdgeVertex1, out vector4);
                vector3 += offset;
                vector4 += offset;
                Vector3 vector5 = transform.TransformPoint(vector3);
                Vector3 vector6 = transform.TransformPoint(vector4);
                vector5.z     = (vector6.z = 0f);
                Handles.color = Color.green;
                Handles.DrawAAPolyLine(4f, new Vector3[]
                {
                    vector5,
                    vector6
                });
                Handles.color = Color.white;
                Vector2 v = this.GetNearestPointOnEdge(transform.TransformPoint(vector), vector5, vector6);
                EditorGUI.BeginChangeCheck();
                float handleSize = HandleUtility.GetHandleSize(v) * 0.04f;
                Handles.color = Color.green;
                v             = Handles.Slider2D(v, new Vector3(0f, 0f, 1f), new Vector3(1f, 0f, 0f), new Vector3(0f, 1f, 0f), handleSize, new Handles.DrawCapFunction(Handles.DotCap), Vector3.zero);
                Handles.color = Color.white;
                if (EditorGUI.EndChangeCheck())
                {
                    PolygonEditor.InsertPoint(this.m_SelectedEdgePath, this.m_SelectedEdgeVertex1, (vector3 + vector4) / 2f - offset);
                    this.m_SelectedPath     = this.m_SelectedEdgePath;
                    this.m_SelectedVertex   = this.m_SelectedEdgeVertex1;
                    this.m_SelectedDistance = 0f;
                    flag  = true;
                    flag3 = true;
                }
            }
            if (flag)
            {
                Vector2 vector7;
                PolygonEditor.GetPoint(this.m_SelectedPath, this.m_SelectedVertex, out vector7);
                vector7 += offset;
                Vector3 vector8 = transform.TransformPoint(vector7);
                vector8.z = 0f;
                Vector2 a           = HandleUtility.WorldToGUIPoint(vector8);
                float   handleSize2 = HandleUtility.GetHandleSize(vector8) * 0.04f;
                if ((this.m_DeleteMode && current.type == EventType.MouseDown && Vector2.Distance(a, Event.current.mousePosition) < 50f) || this.DeleteCommandEvent(current))
                {
                    if (current.type != EventType.ValidateCommand)
                    {
                        int pointCount = PolygonEditor.GetPointCount(this.m_SelectedPath);
                        if (pointCount > this.m_MinPathPoints)
                        {
                            PolygonEditor.RemovePoint(this.m_SelectedPath, this.m_SelectedVertex);
                            this.Reset();
                            flag3 = true;
                        }
                    }
                    current.Use();
                }
                EditorGUI.BeginChangeCheck();
                Handles.color = ((!this.m_DeleteMode) ? Color.green : Color.red);
                Vector3 vector9 = Handles.Slider2D(vector8, new Vector3(0f, 0f, 1f), new Vector3(1f, 0f, 0f), new Vector3(0f, 1f, 0f), handleSize2, new Handles.DrawCapFunction(Handles.DotCap), Vector3.zero);
                Handles.color = Color.white;
                if (EditorGUI.EndChangeCheck() && !this.m_DeleteMode)
                {
                    vector7  = transform.InverseTransformPoint(vector9);
                    vector7 -= offset;
                    PolygonEditor.TestPointMove(this.m_SelectedPath, this.m_SelectedVertex, vector7, out this.m_LeftIntersect, out this.m_RightIntersect, this.m_LoopingCollider);
                    PolygonEditor.SetPoint(this.m_SelectedPath, this.m_SelectedVertex, vector7);
                    flag3 = true;
                }
                if (!flag3)
                {
                    this.DrawEdgesForSelectedPoint(vector9, transform, this.m_LeftIntersect, this.m_RightIntersect, this.m_LoopingCollider);
                }
            }
            if (flag3)
            {
                Undo.RecordObject(this.m_ActiveCollider, "Edit Collider");
                PolygonEditor.ApplyEditing(this.m_ActiveCollider);
            }
            if (this.DeleteCommandEvent(current))
            {
                Event.current.Use();
            }
            this.m_FirstOnSceneGUIAfterReset = false;
        }
        public void OnSceneGUI()
        {
            if ((UnityEngine.Object) this.m_ActiveCollider == (UnityEngine.Object)null || Tools.viewToolActive)
            {
                return;
            }
            Vector2 offset  = this.m_ActiveCollider.offset;
            Event   current = Event.current;

            this.m_DeleteMode = current.command || current.control;
            Transform transform = this.m_ActiveCollider.transform;

            GUIUtility.keyboardControl         = 0;
            HandleUtility.s_CustomPickDistance = 50f;
            Plane plane    = new Plane(-transform.forward, Vector3.zero);
            Ray   worldRay = HandleUtility.GUIPointToWorldRay(current.mousePosition);
            float enter;

            plane.Raycast(worldRay, out enter);
            Vector3 point1    = worldRay.GetPoint(enter);
            Vector2 vector2_1 = (Vector2)transform.InverseTransformPoint(point1);

            if (current.type == EventType.MouseMove || this.m_FirstOnSceneGUIAfterReset)
            {
                int   pathIndex;
                int   num;
                float distance;
                if (PolygonEditor.GetNearestPoint(vector2_1 - offset, out pathIndex, out num, out distance))
                {
                    this.m_SelectedPath     = pathIndex;
                    this.m_SelectedVertex   = num;
                    this.m_SelectedDistance = distance;
                }
                else
                {
                    this.m_SelectedPath = -1;
                }
                int pointIndex1;
                if (PolygonEditor.GetNearestEdge(vector2_1 - offset, out pathIndex, out num, out pointIndex1, out distance, this.m_LoopingCollider))
                {
                    this.m_SelectedEdgePath     = pathIndex;
                    this.m_SelectedEdgeVertex0  = num;
                    this.m_SelectedEdgeVertex1  = pointIndex1;
                    this.m_SelectedEdgeDistance = distance;
                }
                else
                {
                    this.m_SelectedEdgePath = -1;
                }
                if (current.type == EventType.MouseMove)
                {
                    current.Use();
                }
            }
            else if (current.type == EventType.MouseUp)
            {
                this.m_LeftIntersect  = false;
                this.m_RightIntersect = false;
            }
            bool flag1 = false;
            bool flag2 = false;

            if (this.m_SelectedPath != -1 && this.m_SelectedEdgePath != -1)
            {
                Vector2 point2;
                PolygonEditor.GetPoint(this.m_SelectedPath, this.m_SelectedVertex, out point2);
                point2 += offset;
                flag2   = (double)this.m_SelectedEdgeDistance < (double)this.m_SelectedDistance - (double)(HandleUtility.GetHandleSize(transform.TransformPoint((Vector3)point2)) * 0.2f);
                flag1   = !flag2;
            }
            else if (this.m_SelectedPath != -1)
            {
                flag1 = true;
            }
            else if (this.m_SelectedEdgePath != -1)
            {
                flag2 = true;
            }
            if (this.m_DeleteMode && flag2)
            {
                flag2 = false;
                flag1 = true;
            }
            bool flag3 = false;

            if (flag2 && !this.m_DeleteMode)
            {
                Vector2 point2;
                PolygonEditor.GetPoint(this.m_SelectedEdgePath, this.m_SelectedEdgeVertex0, out point2);
                Vector2 point3;
                PolygonEditor.GetPoint(this.m_SelectedEdgePath, this.m_SelectedEdgeVertex1, out point3);
                point2 += offset;
                Vector2 vector2_2 = point3 + offset;
                Vector3 vector3_1 = transform.TransformPoint((Vector3)point2);
                Vector3 vector3_2 = transform.TransformPoint((Vector3)vector2_2);
                vector3_1.z   = vector3_2.z = 0.0f;
                Handles.color = Color.green;
                Handles.DrawAAPolyLine(4f, new Vector3[2]
                {
                    vector3_1,
                    vector3_2
                });
                Handles.color = Color.white;
                Vector2 nearestPointOnEdge = this.GetNearestPointOnEdge((Vector2)transform.TransformPoint((Vector3)vector2_1), (Vector2)vector3_1, (Vector2)vector3_2);
                EditorGUI.BeginChangeCheck();
                float handleSize = HandleUtility.GetHandleSize((Vector3)nearestPointOnEdge) * 0.04f;
                Handles.color = Color.green;
                Vector2 vector2_3 = (Vector2)Handles.Slider2D((Vector3)nearestPointOnEdge, new Vector3(0.0f, 0.0f, 1f), new Vector3(1f, 0.0f, 0.0f), new Vector3(0.0f, 1f, 0.0f), handleSize, new Handles.DrawCapFunction(Handles.DotCap), (Vector2)Vector3.zero);
                Handles.color = Color.white;
                if (EditorGUI.EndChangeCheck())
                {
                    PolygonEditor.InsertPoint(this.m_SelectedEdgePath, this.m_SelectedEdgeVertex1, (point2 + vector2_2) / 2f - offset);
                    this.m_SelectedPath     = this.m_SelectedEdgePath;
                    this.m_SelectedVertex   = this.m_SelectedEdgeVertex1;
                    this.m_SelectedDistance = 0.0f;
                    flag1 = true;
                    flag3 = true;
                }
            }
            if (flag1)
            {
                Vector2 point2;
                PolygonEditor.GetPoint(this.m_SelectedPath, this.m_SelectedVertex, out point2);
                point2 += offset;
                Vector3 vector3_1 = transform.TransformPoint((Vector3)point2);
                vector3_1.z = 0.0f;
                Vector2 guiPoint   = HandleUtility.WorldToGUIPoint(vector3_1);
                float   handleSize = HandleUtility.GetHandleSize(vector3_1) * 0.04f;
                if (this.m_DeleteMode && current.type == EventType.MouseDown && (double)Vector2.Distance(guiPoint, Event.current.mousePosition) < 50.0 || this.DeleteCommandEvent(current))
                {
                    if (current.type != EventType.ValidateCommand && PolygonEditor.GetPointCount(this.m_SelectedPath) > this.m_MinPathPoints)
                    {
                        PolygonEditor.RemovePoint(this.m_SelectedPath, this.m_SelectedVertex);
                        this.Reset();
                        flag3 = true;
                    }
                    current.Use();
                }
                EditorGUI.BeginChangeCheck();
                Handles.color = !this.m_DeleteMode ? Color.green : Color.red;
                Vector3 vector3_2 = Handles.Slider2D(vector3_1, new Vector3(0.0f, 0.0f, 1f), new Vector3(1f, 0.0f, 0.0f), new Vector3(0.0f, 1f, 0.0f), handleSize, new Handles.DrawCapFunction(Handles.DotCap), (Vector2)Vector3.zero);
                Handles.color = Color.white;
                if (EditorGUI.EndChangeCheck() && !this.m_DeleteMode)
                {
                    point2  = (Vector2)transform.InverseTransformPoint(vector3_2);
                    point2 -= offset;
                    PolygonEditor.TestPointMove(this.m_SelectedPath, this.m_SelectedVertex, point2, out this.m_LeftIntersect, out this.m_RightIntersect, this.m_LoopingCollider);
                    PolygonEditor.SetPoint(this.m_SelectedPath, this.m_SelectedVertex, point2);
                    flag3 = true;
                }
                if (!flag3)
                {
                    this.DrawEdgesForSelectedPoint(vector3_2, transform, this.m_LeftIntersect, this.m_RightIntersect, this.m_LoopingCollider);
                }
            }
            if (flag3)
            {
                Undo.RecordObject((UnityEngine.Object) this.m_ActiveCollider, "Edit Collider");
                PolygonEditor.ApplyEditing(this.m_ActiveCollider);
            }
            if (this.DeleteCommandEvent(current))
            {
                Event.current.Use();
            }
            this.m_FirstOnSceneGUIAfterReset = false;
        }
예제 #4
0
        public void OnSceneGUI()
        {
            if (m_ActiveCollider == null)
            {
                return;
            }

            // Fetch the collider offset.
            var colliderOffset = m_ActiveCollider.offset;

            Event evt = Event.current;

            m_DeleteMode = evt.command || evt.control;
            Transform transform = m_ActiveCollider.transform;

            // Handles.Slider2D will render active point as yellow if there is keyboardControl set. We don't want that happening.
            GUIUtility.keyboardControl = 0;

            HandleUtility.s_CustomPickDistance = k_HandlePickDistance;

            // Find mouse positions in local and world space
            Plane plane    = new Plane(-transform.forward, Vector3.zero);
            Ray   mouseRay = HandleUtility.GUIPointToWorldRay(evt.mousePosition);
            float dist;

            plane.Raycast(mouseRay, out dist);

            Vector3 mouseWorldPos = mouseRay.GetPoint(dist);
            Vector2 mouseLocalPos = transform.InverseTransformPoint(mouseWorldPos);

            // Select the active vertex and edge
            if (evt.type == EventType.MouseMove || m_FirstOnSceneGUIAfterReset)
            {
                int   pathIndex;
                int   pointIndex0, pointIndex1;
                float distance;
                if (PolygonEditor.GetNearestPoint(mouseLocalPos - colliderOffset, out pathIndex, out pointIndex0, out distance))
                {
                    m_SelectedPath   = pathIndex;
                    m_SelectedVertex = pointIndex0;
                }
                else
                {
                    m_SelectedPath = -1;
                }

                if (PolygonEditor.GetNearestEdge(mouseLocalPos - colliderOffset, out pathIndex, out pointIndex0, out pointIndex1, out distance, m_LoopingCollider))
                {
                    m_SelectedEdgePath    = pathIndex;
                    m_SelectedEdgeVertex0 = pointIndex0;
                    m_SelectedEdgeVertex1 = pointIndex1;
                }
                else
                {
                    m_SelectedEdgePath = -1;
                }

                if (evt.type == EventType.MouseMove)
                {
                    evt.Use();
                }
            }
            else if (evt.type == EventType.MouseUp)
            {
                m_LeftIntersect  = false;
                m_RightIntersect = false;
            }

            // Do we handle point or line?
            // TODO: there probably isn't a case when selectedPath is valid and selectedEdge is invalid. This needs a refactor.


            if (GUIUtility.hotControl == 0)
            {
                if (m_SelectedPath != -1 && m_SelectedEdgePath != -1)
                {
                    // Calculate snapping distance
                    Vector2 point;
                    PolygonEditor.GetPoint(m_SelectedPath, m_SelectedVertex, out point);
                    point += colliderOffset;
                    Vector3 worldPos = transform.TransformPoint(point);
                    m_HandleEdge  = (HandleUtility.WorldToGUIPoint(worldPos) - Event.current.mousePosition).sqrMagnitude > k_HandlePointSnap * k_HandlePointSnap;
                    m_HandlePoint = !m_HandleEdge;
                }
                else if (m_SelectedPath != -1)
                {
                    m_HandlePoint = true;
                }
                else if (m_SelectedEdgePath != -1)
                {
                    m_HandleEdge = true;
                }

                if (m_DeleteMode && m_HandleEdge)
                {
                    m_HandleEdge  = false;
                    m_HandlePoint = true;
                }
            }

            // If this is a polygon collider being used by a composite then draw the polygon paths.
            if ((m_ActiveCollider is PolygonCollider2D) && m_ActiveCollider.usedByComposite)
            {
                DrawPolygonCollider(transform);
            }

            bool applyToCollider = false;

            // Edge handle
            if (m_HandleEdge && !m_DeleteMode)
            {
                Vector2 p0, p1;
                PolygonEditor.GetPoint(m_SelectedEdgePath, m_SelectedEdgeVertex0, out p0);
                PolygonEditor.GetPoint(m_SelectedEdgePath, m_SelectedEdgeVertex1, out p1);
                p0 += colliderOffset;
                p1 += colliderOffset;
                Vector3 worldPosV0 = transform.TransformPoint(p0);
                Vector3 worldPosV1 = transform.TransformPoint(p1);
                worldPosV0.z = worldPosV1.z = 0;

                Handles.color = Color.green;
                Handles.DrawAAPolyLine(4.0f, new Vector3[] { worldPosV0, worldPosV1 });
                Handles.color = Color.white;

                Vector2 newPoint = GetNearestPointOnEdge(transform.TransformPoint(mouseLocalPos), worldPosV0, worldPosV1);

                EditorGUI.BeginChangeCheck();
                float guiSize = HandleUtility.GetHandleSize(newPoint) * 0.04f;
                Handles.color = Color.green;

                newPoint = Handles.Slider2D(
                    newPoint,
                    new Vector3(0, 0, 1),
                    new Vector3(1, 0, 0),
                    new Vector3(0, 1, 0),
                    guiSize,
                    Handles.DotHandleCap,
                    Vector3.zero);
                Handles.color = Color.white;
                if (EditorGUI.EndChangeCheck())
                {
                    PolygonEditor.InsertPoint(m_SelectedEdgePath, m_SelectedEdgeVertex1, ((p0 + p1) / 2) - colliderOffset);
                    m_SelectedPath   = m_SelectedEdgePath;
                    m_SelectedVertex = m_SelectedEdgeVertex1;
                    m_HandleEdge     = false;
                    m_HandlePoint    = true;
                    applyToCollider  = true;
                }
            }

            // Point handle
            if (m_HandlePoint)
            {
                Vector2 point;
                PolygonEditor.GetPoint(m_SelectedPath, m_SelectedVertex, out point);
                point += colliderOffset;
                Vector3 worldPos = transform.TransformPoint(point);
                worldPos.z = 0;
                Vector2 screenPos = HandleUtility.WorldToGUIPoint(worldPos);

                float guiSize = HandleUtility.GetHandleSize(worldPos) * 0.04f;

                if (m_DeleteMode && evt.type == EventType.MouseDown &&
                    Vector2.Distance(screenPos, Event.current.mousePosition) < k_HandlePickDistance ||
                    DeleteCommandEvent(evt))
                {
                    if (evt.type != EventType.ValidateCommand)
                    {
                        int pathPointCount = PolygonEditor.GetPointCount(m_SelectedPath);
                        if (pathPointCount > m_MinPathPoints)
                        {
                            PolygonEditor.RemovePoint(m_SelectedPath, m_SelectedVertex);
                            Reset();
                            applyToCollider = true;
                        }
                    }
                    evt.Use();
                }

                EditorGUI.BeginChangeCheck();
                Handles.color = m_DeleteMode ? Color.red : Color.green;
                Vector3 newWorldPos = Handles.Slider2D(
                    worldPos,
                    new Vector3(0, 0, 1),
                    new Vector3(1, 0, 0),
                    new Vector3(0, 1, 0),
                    guiSize,
                    Handles.DotHandleCap,
                    Vector3.zero);
                Handles.color = Color.white;
                if (EditorGUI.EndChangeCheck() && !m_DeleteMode)
                {
                    point  = transform.InverseTransformPoint(newWorldPos);
                    point -= colliderOffset;
                    PolygonEditor.TestPointMove(m_SelectedPath, m_SelectedVertex, point, out m_LeftIntersect, out m_RightIntersect, m_LoopingCollider);
                    PolygonEditor.SetPoint(m_SelectedPath, m_SelectedVertex, point);
                    applyToCollider = true;
                }

                if (!applyToCollider)
                {
                    DrawEdgesForSelectedPoint(newWorldPos, transform, m_LeftIntersect, m_RightIntersect, m_LoopingCollider);
                }
            }

            // Apply changes
            if (applyToCollider)
            {
                Undo.RecordObject(m_ActiveCollider, "Edit Collider");
                PolygonEditor.ApplyEditing(m_ActiveCollider);
            }

            if (DeleteCommandEvent(evt))
            {
                Event.current.Use();  // If we don't use the delete event in all cases, it sceneview might delete the entire object
            }
            m_FirstOnSceneGUIAfterReset = false;
        }
예제 #5
0
 public void OnSceneGUI()
 {
     if ((this.m_ActiveCollider != null) && !Tools.viewToolActive)
     {
         float   num;
         Vector2 offset  = this.m_ActiveCollider.offset;
         Event   current = Event.current;
         this.m_DeleteMode = current.command || current.control;
         Transform transform = this.m_ActiveCollider.transform;
         GUIUtility.keyboardControl         = 0;
         HandleUtility.s_CustomPickDistance = 50f;
         Plane plane = new Plane(-transform.forward, Vector3.zero);
         Ray   ray   = HandleUtility.GUIPointToWorldRay(current.mousePosition);
         plane.Raycast(ray, out num);
         Vector3 point   = ray.GetPoint(num);
         Vector2 vector3 = transform.InverseTransformPoint(point);
         if ((current.type == EventType.MouseMove) || this.m_FirstOnSceneGUIAfterReset)
         {
             int   num2;
             int   num3;
             int   num4;
             float num5;
             if (PolygonEditor.GetNearestPoint(vector3 - offset, out num2, out num3, out num5))
             {
                 this.m_SelectedPath     = num2;
                 this.m_SelectedVertex   = num3;
                 this.m_SelectedDistance = num5;
             }
             else
             {
                 this.m_SelectedPath = -1;
             }
             if (PolygonEditor.GetNearestEdge(vector3 - offset, out num2, out num3, out num4, out num5, this.m_LoopingCollider))
             {
                 this.m_SelectedEdgePath     = num2;
                 this.m_SelectedEdgeVertex0  = num3;
                 this.m_SelectedEdgeVertex1  = num4;
                 this.m_SelectedEdgeDistance = num5;
             }
             else
             {
                 this.m_SelectedEdgePath = -1;
             }
             if (current.type == EventType.MouseMove)
             {
                 current.Use();
             }
         }
         else if (current.type == EventType.MouseUp)
         {
             this.m_LeftIntersect  = false;
             this.m_RightIntersect = false;
         }
         bool flag  = false;
         bool flag2 = false;
         if ((this.m_SelectedPath != -1) && (this.m_SelectedEdgePath != -1))
         {
             Vector2 vector4;
             PolygonEditor.GetPoint(this.m_SelectedPath, this.m_SelectedVertex, out vector4);
             vector4 += offset;
             float num6 = HandleUtility.GetHandleSize(transform.TransformPoint((Vector3)vector4)) * 0.2f;
             flag2 = this.m_SelectedEdgeDistance < (this.m_SelectedDistance - num6);
             flag  = !flag2;
         }
         else if (this.m_SelectedPath != -1)
         {
             flag = true;
         }
         else if (this.m_SelectedEdgePath != -1)
         {
             flag2 = true;
         }
         if (this.m_DeleteMode && flag2)
         {
             flag2 = false;
             flag  = true;
         }
         bool flag3 = false;
         if (flag2 && !this.m_DeleteMode)
         {
             Vector2 vector6;
             Vector2 vector7;
             PolygonEditor.GetPoint(this.m_SelectedEdgePath, this.m_SelectedEdgeVertex0, out vector6);
             PolygonEditor.GetPoint(this.m_SelectedEdgePath, this.m_SelectedEdgeVertex1, out vector7);
             vector6 += offset;
             vector7 += offset;
             Vector3 start = transform.TransformPoint((Vector3)vector6);
             Vector3 end   = transform.TransformPoint((Vector3)vector7);
             start.z       = end.z = 0f;
             Handles.color = Color.green;
             Vector3[] points = new Vector3[] { start, end };
             Handles.DrawAAPolyLine((float)4f, points);
             Handles.color = Color.white;
             Vector2 vector10 = this.GetNearestPointOnEdge(transform.TransformPoint((Vector3)vector3), start, end);
             EditorGUI.BeginChangeCheck();
             float handleSize = HandleUtility.GetHandleSize((Vector3)vector10) * 0.04f;
             Handles.color = Color.green;
             if (< > f__mg$cache0 == null)
             {