context: using UnityEngine;
  using UnityEngine.Sprites;
  using UnityEditor;
  using UnityEditorInternal;
  using UnityEditor.Sprites;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Reflection;
  
  namespace Anima2D 
  {
  	public class SpriteMeshEditorWindow : TextureEditorWindow
  	{		
  		public enum Mode {
  			Mesh,
  			Blendshapes
  		}
  
  		RectManipulator m_RectManipulator = new RectManipulator();
  
  		WeightEditor weightEditor = new WeightEditor();
  		BlendShapeEditor blendShapeEditor = new BlendShapeEditor();
  		BlendShapeFrameEditor blendShapeFrameEditor = new BlendShapeFrameEditor();
  		InspectorEditor inspectorEditor = new InspectorEditor();
  		MeshToolEditor meshToolEditor = new MeshToolEditor();
  		SliceEditor sliceEditor = new SliceEditor();
  
  		SpriteMeshCache m_SpriteMeshCache;
  
  		Material mMeshGuiMaterial;
  		Material meshGuiMaterial {
  			get {
  				if(!mMeshGuiMaterial)
  				{
  					mMeshGuiMaterial = new Material(Shader.Find("Hidden/Internal-GUITextureClip"));
  					mMeshGuiMaterial.hideFlags = HideFlags.DontSave;
  				}
  				
  				return mMeshGuiMaterial;
  			}
  		}
  		
  		Node hoveredNode { get; set; }
  		Hole hoveredHole { get; set; }
  		Edge hoveredEdge { get; set; }
  		BindInfo hoveredBindPose { get; set; }
  		Bone2D hoveredBone { get; set; }
  		
  		TextureImporter m_TextureImporter = null;
  		Texture2D m_OriginalTexture = null;
  		
  		List<Color> m_BindPoseColors;
  		int m_selectionControlID = 0;
  		
  		[MenuItem("Window/Anima2D/SpriteMesh Editor",false,0)]
  		static void ContextInitialize()
  		{
  			GetWindow();
  		}
  		
  		public static SpriteMeshEditorWindow GetWindow()
  		{
  			return EditorWindow.GetWindow<SpriteMeshEditorWindow>("SpriteMesh Editor");
  		}
  		
  		void OnEnable()
  		{
  			Undo.undoRedoPerformed -= UndoRedoPerformed;
  			Undo.undoRedoPerformed += UndoRedoPerformed;
  			
  			showBones = true;
  			
  			weightEditor.canShow = () => {
  				return  meshToolEditor.canShow() && m_SpriteMeshCache.isBound;
  			};
  			
  			inspectorEditor.canShow = () => {
  				return  true;
  			};
  			
  			meshToolEditor.canShow = () => {
  				return  m_SpriteMeshCache.mode == Mode.Mesh;
  			};
  
  			sliceEditor.canShow = () => {
  				return meshToolEditor.canShow() && meshToolEditor.sliceToggle;
  			};
  			
  			blendShapeEditor.canShow = () => {
  				return m_SpriteMeshCache.mode == Mode.Blendshapes;
  			};
  
  			blendShapeFrameEditor.canShow = () => {
  				return blendShapeEditor.canShow() && m_SpriteMeshCache.selectedBlendshape;
  			};
  
  			UpdateFromSelection();
  			
  			EditorApplication.delayCall += () => {
  				RefreshTexture(true);
  				SetScrollPositionAndZoomFromSprite();
  			};
  		}
  		
  		void OnDisable()
  		{
  			Undo.undoRedoPerformed -= UndoRedoPerformed;
  			
  			if(mMeshGuiMaterial)
  			{
  				DestroyImmediate(mMeshGuiMaterial);
  			}
  			
  			if(m_Texture)
  			{
  				DestroyImmediate(m_Texture);
  			}
  		}
  		
  		void UndoRedoPerformed()
  		{
  			m_SpriteMeshCache.PrepareManipulableVertices();
  
  			Repaint();
  		}
  		
  		bool showBones { get; set; }
  		bool showTriangles { get; set; }
  		
  		Vector2 m_MousePositionWorld;
  		
  		List<Vector3> m_Points;
  		List<Vector2> m_UVs;
  		
  		Edge m_ClosestEdge = null;
  		
  		Matrix4x4 m_SpriteMeshMatrix = new Matrix4x4();
  
  		//GUIStyle m_DefaultWindowStyle = null;
  		//GUIStyle m_Style = null;
  
  		Color vertexColor {
  			get {
  				return Color.cyan;
  			}
  		}
  		
  		int m_LastNearestControl = -1;
  
  		override protected void OnGUI()
  		{
  			Matrix4x4 matrix = Handles.matrix;
  			
  			textureColor = Color.gray;
  			
  			if (!m_SpriteMeshCache || !m_SpriteMeshCache.spriteMesh)
  			{
  				EditorGUI.BeginDisabledGroup(true);
  				GUILayout.Label("No sprite mesh selected");
  				EditorGUI.EndDisabledGroup();
  			}else{
  
  				/*
  				if(m_Style == null || m_DefaultWindowStyle == null)
  				{
  					m_DefaultWindowStyle = GUI.skin.window;
  					m_Style = new GUIStyle(m_DefaultWindowStyle);
  
  					m_Style.active.background = null;
  					m_Style.focused.background = null;
  					m_Style.hover.background = null;
  					m_Style.normal.background = null;
  					m_Style.onActive.background = null;
  					m_Style.onFocused.background = null;
  					m_Style.onHover.background = null;
  					m_Style.onNormal.background = null;
  				}
  				*/
  
  				autoRepaintOnSceneChange = true;
  				wantsMouseMove = true;
  				
  				HotKeys();
  				
  				if(Event.current.type == EventType.MouseUp && Event.current.button == 1)
  				{
  					meshToolEditor.tool = MeshToolEditor.MeshTool.None;
  				}
  					
  				base.OnGUI();
  				
  				GUI.color = Color.white;
  				
  				GUILayout.BeginArea(m_TextureViewRect);
  
  				BeginWindows();
  
  				//GUI.skin.window = m_Style;
  
  				if(m_SpriteMeshCache.mode == Mode.Mesh)
  				{
  					meshToolEditor.spriteMeshCache = m_SpriteMeshCache;
  					meshToolEditor.OnWindowGUI(m_TextureViewRect);
  				}else if(m_SpriteMeshCache.mode == Mode.Blendshapes)
  				{
  					blendShapeEditor.spriteMeshCache = m_SpriteMeshCache;
  					blendShapeEditor.OnWindowGUI(m_TextureViewRect);
  				}
  
  				//GUI.skin.window = m_DefaultWindowStyle;
  
  				sliceEditor.spriteMeshCache = m_SpriteMeshCache;
  				sliceEditor.OnWindowGUI(m_TextureViewRect);
  
  				weightEditor.spriteMeshCache = m_SpriteMeshCache;
  				weightEditor.OnWindowGUI(m_TextureViewRect);
  
  				blendShapeFrameEditor.spriteMeshCache = m_SpriteMeshCache;
  				blendShapeFrameEditor.OnWindowGUI(m_TextureViewRect);
  				
  				inspectorEditor.spriteMeshCache = m_SpriteMeshCache;
  				inspectorEditor.OnWindowGUI(m_TextureViewRect);
  				
  				EndWindows();
  				
  				if(m_SpriteMeshCache.mode == Mode.Mesh &&
  					!meshToolEditor.sliceToggle)
  				{
  					HandleDeleteVertex();
  					HandleDeleteHole();
  					HandleDeleteEdge();
  					HandleDeleteBone();
  					HandleDeleteBindPose();
  				}
  
  				GUILayout.EndArea();
  			}
  			
  			Handles.matrix = matrix;
  			
  			if(GUIUtility.hotControl == 0 && HandleUtility.nearestControl != m_LastNearestControl)
  			{
  				m_LastNearestControl = HandleUtility.nearestControl;
  				Repaint();
  			}
  		}
  		
  		override protected void DoTextureGUIExtras()
  		{
  			m_selectionControlID = GUIUtility.GetControlID("SelectionRect".GetHashCode(), FocusType.Keyboard);
  			
  			m_Points = m_SpriteMeshCache.GetTexVerticesV3();
  			m_UVs = m_SpriteMeshCache.uvs;
  			m_BindPoseColors = m_SpriteMeshCache.bindPoses.ConvertAll( b => b.color );
  			
  			hoveredNode = null;
  			hoveredEdge = null;
  			hoveredHole = null;
  			hoveredBindPose = null;
  			hoveredBone = null;
  			m_ClosestEdge = null;
  			
  			m_MousePositionWorld = ScreenToWorld(Event.current.mousePosition - new Vector2(2f,3f));
  			
  			if(m_SpriteMeshCache.mode == Mode.Mesh)
  			{
  				m_ClosestEdge = GetClosestEdge(m_MousePositionWorld);
  			}
  			
  			if(showBones)
  			{
  				if(m_SpriteMeshCache.isBound)
  				{
  					BindPosesGUI(meshToolEditor.sliceToggle ||
  						m_SpriteMeshCache.mode != Mode.Mesh);
  				}else{
  					BonesGUI(meshToolEditor.sliceToggle ||
  						m_SpriteMeshCache.mode != Mode.Mesh);
  				}
  			}
  			
  			bool disableEditGeometry = m_SpriteMeshCache.mode == Mode.Mesh && meshToolEditor.sliceToggle;
  
  			EdgesGUI(disableEditGeometry);
  			VerticesGUI(disableEditGeometry);
  			
  			if(m_SpriteMeshCache.mode == Mode.Mesh)
  			{
  				PivotHandleGUI(meshToolEditor.sliceToggle);
  				HolesGUI(meshToolEditor.sliceToggle);
  				
  				if(!meshToolEditor.sliceToggle)
  				{
  					HandleAddVertex();
  					HandleAddHole();
  					HandleAddEdge();
  					HandleAddBindPose();
  				}
  			}
  			
  			if(!meshToolEditor.sliceToggle || m_SpriteMeshCache.mode != Mode.Mesh)
  			{
  				HandleSelection();
  			}
  
  			RectManipulatorGUI();
  		}
  		
  		void HandleAddBindPose()
  		{
  			if(m_SpriteMeshCache.spriteMeshInstance && DragAndDrop.objectReferences.Length > 0)
  			{
  				EventType eventType = Event.current.GetTypeForControl(0);
  				
  				List<GameObject> dragAndDropGameObjects = DragAndDrop.objectReferences.ToList().ConvertAll( o => o as GameObject);
  				List<Bone2D> dragAndDropBones = dragAndDropGameObjects.ConvertAll( go => go ? go.GetComponent<Bone2D>() : null );
  				
  				if(eventType == EventType.DragPerform)
  				{
  					bool performAutoWeights = m_SpriteMeshCache.bindPoses.Count == 0;
  					
  					m_SpriteMeshCache.RegisterUndo("add bind pose");
  					foreach(Bone2D bone in dragAndDropBones)
  					{
  						m_SpriteMeshCache.BindBone(bone);
  					}
  					
  					if(performAutoWeights)
  					{
  						m_SpriteMeshCache.CalculateAutomaticWeights();
  					}
  					
  					//EditorUtility.SetDirty(m_SpriteMeshCache.spriteMeshInstance);
  					
  					Event.current.Use();
  				}
  				
  				DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  			}
  		}
  		
  		void HandleDeleteBone()
  		{
  			EventType eventType = Event.current.GetTypeForControl(0);
  			
  			if(eventType == EventType.KeyDown &&
  			   (Event.current.keyCode == KeyCode.Backspace || Event.current.keyCode == KeyCode.Delete) &&
  			   m_SpriteMeshCache.selectedBone &&
  			   m_SpriteMeshCache.selection.Count == 0 &&
  			   !m_SpriteMeshCache.selectedHole &&
  			   !m_SpriteMeshCache.selectedEdge)
  			{
  				Undo.RegisterCompleteObjectUndo(m_SpriteMeshCache.spriteMeshInstance,"delete bone");
  				m_SpriteMeshCache.DeleteBone(m_SpriteMeshCache.selectedBone);
  				
  				Event.current.Use();
  			}
  		}
  		
  		void HandleDeleteBindPose()
  		{
  			EventType eventType = Event.current.GetTypeForControl(0);
  			
  			if(eventType == EventType.KeyDown &&
  			   (Event.current.keyCode == KeyCode.Backspace || Event.current.keyCode == KeyCode.Delete) &&
  			   m_SpriteMeshCache.selectedBindPose &&
  			   m_SpriteMeshCache.selection.Count == 0 &&
  			   !m_SpriteMeshCache.selectedHole &&
  			   !m_SpriteMeshCache.selectedEdge)
  			{
  				m_SpriteMeshCache.RegisterUndo("delete bind pose");
  				m_SpriteMeshCache.DeleteBindPose(m_SpriteMeshCache.selectedBindPose);
  				
  				Event.current.Use();
  			}
  		}
  		
  		void HandleSelection()
  		{
  			if(Event.current.GetTypeForControl(0) == EventType.MouseDown &&
  			   !Event.current.alt &&
  			   (Event.current.button == 0 && HandleUtility.nearestControl == m_selectionControlID) ||
  			   Event.current.button == 1)
  			{
  				if((!EditorGUI.actionKey && m_SpriteMeshCache.selection.Count > 0) ||
  				   m_SpriteMeshCache.selectedEdge ||
  				   m_SpriteMeshCache.selectedHole ||
  				   ((m_SpriteMeshCache.selectedBindPose || m_SpriteMeshCache.selectedBone) && Event.current.button == 1))
  				{
  					m_SpriteMeshCache.RegisterUndo("selection");
  					m_SpriteMeshCache.ClearSelection();
  					m_SpriteMeshCache.selectedEdge = null;
  					m_SpriteMeshCache.selectedHole = null;
  
  					if(Event.current.button == 1 && (m_SpriteMeshCache.selectedBindPose || m_SpriteMeshCache.selectedBone))
  					{
  						m_SpriteMeshCache.selectedBindPose = null;
  						m_SpriteMeshCache.selectedBone = null;
  					}
  				}
  			}
  			
  			if(GUIUtility.hotControl == m_selectionControlID &&
  			   Event.current.GetTypeForControl(m_selectionControlID) == EventType.MouseUp &&
  			   Event.current.button == 0)
  			{
  				int numSelectedBeforeSelection = m_SpriteMeshCache.selection.Count;
  
  				m_SpriteMeshCache.RegisterUndo("selection");
  
  				m_SpriteMeshCache.EndSelection(true);
  
  				InitRectManipulatorParams(numSelectedBeforeSelection);
  
  				m_SpriteMeshCache.PrepareManipulableVertices();
  
  				Repaint();
  			}
  
  			EditorGUI.BeginChangeCheck();
  
  			Rect rect = SelectionRectTool.Do(m_selectionControlID);
  
  			if(EditorGUI.EndChangeCheck())
  			{
  				m_SpriteMeshCache.BeginSelection();
  
  				if(m_Points != null)
  				{
  					for (int i = 0; i < m_Points.Count; i++)
  					{
  						Vector2 p = m_Points [i];
  						if(rect.Contains(p,true))
  						{
  							m_SpriteMeshCache.selection.Select(i,true);
  						}
  					}
  				}
  			}
  		}
  
  		void BonesGUI(bool disable)
  		{
  			if(!m_SpriteMeshCache.spriteMeshInstance) return;
  			
  			Matrix4x4 old = Handles.matrix;
  			Handles.matrix = m_SpriteMeshMatrix;
  			float radius = 7.5f / m_TextureImporter.spritePixelsPerUnit / m_Zoom;
  			
  			for (int i = 0; i < m_SpriteMeshCache.spriteMeshInstance.bones.Count; i++)
  			{
  				int controlID = GUIUtility.GetControlID ("BonesHandle".GetHashCode(), FocusType.Passive);
  				EventType eventType = Event.current.GetTypeForControl(controlID);
  				
  				Bone2D bone = m_SpriteMeshCache.spriteMeshInstance.bones[i];
  				
  				if(bone)
  				{
  					Vector3 position = m_SpriteMeshCache.spriteMeshInstance.transform.InverseTransformPoint(bone.transform.position);
  					Vector3 endPoint = m_SpriteMeshCache.spriteMeshInstance.transform.InverseTransformPoint(bone.endPosition);
  					
  					if(!disable)
  					{
  						if(HandleUtility.nearestControl == controlID &&
  						   GUIUtility.hotControl == 0)
  						{
  							hoveredBone = bone;
  							
  							if(eventType == EventType.MouseDown &&
  							   Event.current.button == 0 &&
  							   !Event.current.shift &&
  							   !Event.current.alt)
  							{
  								if(m_SpriteMeshCache.selectedBone != bone)
  								{
  									m_SpriteMeshCache.RegisterUndo("select bone");
  									m_SpriteMeshCache.selectedBone = bone;
  								}
  								
  								Event.current.Use();
  							}
  						}
  						
  						if(eventType == EventType.Layout)
  						{
  							HandleUtility.AddControl(controlID, HandleUtility.DistanceToLine(position,endPoint));
  						}
  					}
  					
  					if(bone && eventType == EventType.Repaint)
  					{
  						if((GUIUtility.hotControl == 0 && HandleUtility.nearestControl == controlID) ||
  						   m_SpriteMeshCache.selectedBone == bone)
  						{
  							BoneUtils.DrawBoneOutline(position,endPoint,radius,radius*0.25f,Color.yellow);
  						}
  						
  						Color color = bone.color;
  						color.a = 1f;
  						BoneUtils.DrawBoneBody(position,endPoint,radius,color);
  						color = bone.color * 0.25f;
  						color.a = 1f;
  						BoneUtils.DrawBoneCap(position,radius,color);
  					}
  				}
  			}
  			
  			Handles.matrix = old;
  		}
  		
  		void BindPosesGUI(bool disable)
  		{
  			Matrix4x4 old = Handles.matrix;
  			Handles.matrix = m_SpriteMeshMatrix;
  			float radius = 7.5f / m_TextureImporter.spritePixelsPerUnit / m_Zoom;
  			
  			for (int i = 0; i < m_SpriteMeshCache.bindPoses.Count; i++)
  			{
  				int controlID = GUIUtility.GetControlID ("BindPoseHandle".GetHashCode(), FocusType.Passive);
  				EventType eventType = Event.current.GetTypeForControl(controlID);
  				
  				BindInfo bindPose = m_SpriteMeshCache.bindPoses[i];
  				
  				if(!disable)
  				{
  					if(HandleUtility.nearestControl == controlID &&
  					   GUIUtility.hotControl == 0)
  					{
  						hoveredBindPose = bindPose;
  						
  						if(eventType == EventType.MouseDown &&
  						   Event.current.button == 0 &&
  						   !Event.current.shift &&
  						   !Event.current.alt)
  						{
  							if(m_SpriteMeshCache.selectedBindPose != bindPose)
  							{
  								m_SpriteMeshCache.RegisterUndo("select bind pose");
  								m_SpriteMeshCache.selectedBindPose = bindPose;
  							}
  							
  							Event.current.Use();
  						}
  					}
  					
  					if(eventType == EventType.Layout)
  					{
  						HandleUtility.AddControl(controlID, HandleUtility.DistanceToLine(bindPose.position,bindPose.endPoint));
  					}
  				}
  				
  				if(eventType == EventType.Repaint)
  				{
  					Color innerColor = m_BindPoseColors[i] * 0.25f;
  					innerColor.a = 1f;
  					
  					if((GUIUtility.hotControl == 0 && HandleUtility.nearestControl == controlID) ||
  					   m_SpriteMeshCache.selectedBindPose == bindPose)
  					{
  						BoneUtils.DrawBoneOutline(bindPose.position,bindPose.endPoint,radius,radius*0.25f,Color.white);
  					}else if(m_SpriteMeshCache.mode == Mode.Mesh && weightEditor.overlayColors)
  					{
  						Color c = m_BindPoseColors[i] * 0.5f;
  						c.a = 1f;
  						
  						BoneUtils.DrawBoneOutline(bindPose.position,bindPose.endPoint,radius,radius*0.25f,c);
  					}
  					
  					BoneUtils.DrawBoneBody(bindPose.position, bindPose.endPoint, radius, m_BindPoseColors[i]);
  					Color color = m_BindPoseColors[i] * 0.25f;
  					color.a = 1f;
  					BoneUtils.DrawBoneCap(bindPose.position, radius,color);
  				}
  			}
  			
  			Handles.matrix = old;
  		}
  		
  		void HolesGUI(bool disable)
  		{
  			GUI.color = Color.white;
  			
  			for (int i = 0; i < m_SpriteMeshCache.holes.Count; i++)
  			{
  				Hole hole = m_SpriteMeshCache.holes[i];
  				Vector2 position = hole.vertex;
  				
  				int controlID = GUIUtility.GetControlID ("HoleHandle".GetHashCode(), FocusType.Passive);
  				EventType eventType = Event.current.GetTypeForControl(controlID);
  				
  				if(!disable)
  				{
  					if(HandleUtility.nearestControl == controlID &&
  					   GUIUtility.hotControl == 0)
  					{
  						hoveredHole = hole;
  						
  						if(eventType == EventType.MouseDown &&
  						   Event.current.button == 0 &&
  						   !Event.current.alt)
  						{
  							m_SpriteMeshCache.RegisterUndo("select hole");
  							m_SpriteMeshCache.selectedHole = hole;
  							m_SpriteMeshCache.ClearSelection();
  							m_SpriteMeshCache.selectedEdge = null;
  							Undo.IncrementCurrentGroup();
  						}
  					}
  					
  					EditorGUI.BeginChangeCheck();
  					
  					Vector2 newPosition = HandlesExtra.Slider2D(controlID, position, null);
  					
  					if (EditorGUI.EndChangeCheck())
  					{
  						Vector2 delta = newPosition - position;
  						m_SpriteMeshCache.RegisterUndo("move hole");
  						hole.vertex += delta;
  						m_SpriteMeshCache.Triangulate();
  					}
  					
  					if(eventType == EventType.Layout)
  					{
  						HandleUtility.AddControl(controlID, HandleUtility.DistanceToCircle(position, 2.5f));
  					}
  				}
  				
  				if(eventType == EventType.Repaint)
  				{
  					bool highlight = false;
  					
  					if((HandleUtility.nearestControl == controlID && GUIUtility.hotControl == 0) ||
  					   m_SpriteMeshCache.selectedHole == hole)
  					{
  						highlight = true;
  					}
  
  					if(highlight)
  					{
  						HandlesExtra.DrawDotYellow(position);
  					}else{
  						HandlesExtra.DrawDotRed(position);
  					}
  				}
  			}
  		}
  		
  		void EdgesGUI(bool disable)
  		{
  			for (int i = 0; i < m_SpriteMeshCache.edges.Count; i++)
  			{
  				int controlID = GUIUtility.GetControlID ("EdgeHandle".GetHashCode(), FocusType.Passive);
  				EventType eventType = Event.current.GetTypeForControl(controlID);
  				
  				Edge edge = m_SpriteMeshCache.edges[i];
  				Vector2 position = m_SpriteMeshCache.GetVertex(edge.node1);
  				
  				if(!disable)
  				{
  					if(HandleUtility.nearestControl == controlID &&
  					   GUIUtility.hotControl == 0)
  					{
  						hoveredEdge = edge;
  						
  						if(eventType == EventType.MouseDown &&
  						   Event.current.button == 0 &&
  						   m_SpriteMeshCache.selectedEdge != edge &&
  						   !Event.current.shift &&
  						   !Event.current.alt)
  						{
  							m_SpriteMeshCache.RegisterUndo("select edge");
  							m_SpriteMeshCache.selectedEdge = edge;
  							m_SpriteMeshCache.ClearSelection();
  							m_SpriteMeshCache.selectedHole = null;
  						}
  					}
  					
  					if(!Event.current.shift)
  					{
  						EditorGUI.BeginChangeCheck();
  						
  						Vector2 newPosition = HandlesExtra.Slider2D(controlID, position, null);
  						
  						if(EditorGUI.EndChangeCheck())
  						{
  							m_SpriteMeshCache.RegisterUndo("move edge");
  
  							Vector2 delta = newPosition - position;
  							
  							if(m_SpriteMeshCache.IsSelected(edge.node1) && m_SpriteMeshCache.IsSelected(edge.node2))
  							{
  								List<Node> selectedNodes = m_SpriteMeshCache.selectedNodes;
  
  								foreach(Node node in selectedNodes)
  								{
  									Vector2 texcoord = m_SpriteMeshCache.GetVertex(node);
  									m_SpriteMeshCache.SetVertex(node,texcoord + delta);
  								}
  							}else{
  								Vector2 texcoord1 = m_SpriteMeshCache.GetVertex(edge.node1);
  								Vector2 texcoord2 = m_SpriteMeshCache.GetVertex(edge.node2);
  								m_SpriteMeshCache.SetVertex(edge.node1,texcoord1 + delta);
  								m_SpriteMeshCache.SetVertex(edge.node2,texcoord2 + delta);
  							}
  							
  							m_SpriteMeshCache.Triangulate();
  						}
  					}
  					
  					if (eventType == EventType.Layout)
  					{
  						Vector2 texcoord1 = m_SpriteMeshCache.GetVertex(edge.node1);
  						Vector2 texcoord2 = m_SpriteMeshCache.GetVertex(edge.node2);
  						HandleUtility.AddControl(controlID, HandleUtility.DistanceToLine(texcoord1,texcoord2));
  					}
  				}
  				
  				if(eventType == EventType.Repaint)
  				{
  					bool drawEdge = true;
  					
  					if((HandleUtility.nearestControl == m_selectionControlID || HandleUtility.nearestControl == controlID) &&
  					   Event.current.shift &&
  					   !m_SpriteMeshCache.selectedNode &&
  
gt: 					   edge == m_ClosestEdge)