public void DrawTextureView(tk2dSpriteCollectionDefinition param, Texture2D texture)
        {
            HandleKeys();

            if (mode == Mode.None)
            {
                mode = Mode.Texture;
            }

            // sanity check
            if (editorDisplayScale <= 1.0f)
            {
                editorDisplayScale = 1.0f;
            }

            // mirror data
            currentColliderColor = param.colliderColor;

            GUILayout.BeginVertical(tk2dEditorSkin.SC_BodyBackground, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));

            if (texture == null)
            {
                // Get somewhere to put the texture...
                GUILayoutUtility.GetRect(128.0f, 128.0f, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
            }
            else
            {
                bool allowAnchor   = param.anchor == tk2dSpriteCollectionDefinition.Anchor.Custom;
                bool allowCollider = (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Polygon ||
                                      param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.BoxCustom);
                if (mode == Mode.Anchor && !allowAnchor)
                {
                    mode = Mode.Texture;
                }
                if (mode == Mode.Collider && !allowCollider)
                {
                    mode = Mode.Texture;
                }

                Rect rect = GUILayoutUtility.GetRect(128.0f, 128.0f, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
                tk2dGrid.Draw(rect);

                // middle mouse drag and scroll zoom
                if (rect.Contains(Event.current.mousePosition))
                {
                    if (Event.current.type == EventType.MouseDrag && Event.current.button == 2)
                    {
                        textureScrollPos -= Event.current.delta * editorDisplayScale;
                        Event.current.Use();
                        HandleUtility.Repaint();
                    }
                    if (Event.current.type == EventType.ScrollWheel)
                    {
                        editorDisplayScale -= Event.current.delta.y * 0.03f;
                        Event.current.Use();
                        HandleUtility.Repaint();
                    }
                }

                bool alphaBlend = true;
                textureScrollPos = GUI.BeginScrollView(rect, textureScrollPos,
                                                       new Rect(0, 0, textureBorderPixels * 2 + (texture.width) * editorDisplayScale, textureBorderPixels * 2 + (texture.height) * editorDisplayScale));
                Rect textureRect = new Rect(textureBorderPixels, textureBorderPixels, texture.width * editorDisplayScale, texture.height * editorDisplayScale);
                texture.filterMode = FilterMode.Point;
                GUI.DrawTexture(textureRect, texture, ScaleMode.ScaleAndCrop, alphaBlend);

                if (mode == Mode.Collider)
                {
                    if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.BoxCustom)
                    {
                        DrawCustomBoxColliderEditor(textureRect, param, texture);
                    }
                    if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Polygon)
                    {
                        DrawPolygonColliderEditor(textureRect, ref param.polyColliderIslands, texture, false);
                    }
                }

                if (mode == Mode.Texture && param.customSpriteGeometry)
                {
                    DrawPolygonColliderEditor(textureRect, ref param.geometryIslands, texture, true);
                }

                // Anchor
                if (mode == Mode.Anchor)
                {
                    Color   lineColor = Color.white;
                    Vector2 anchor    = new Vector2(param.anchorX, param.anchorY);
                    Vector2 origin    = new Vector2(textureRect.x, textureRect.y);

                    int id = 99999;
                    anchor = (tk2dGuiUtility.PositionHandle(id, anchor * editorDisplayScale + origin) - origin) / editorDisplayScale;

                    Color oldColor = Handles.color;
                    Handles.color = lineColor;
                    float w = Mathf.Max(rect.width, texture.width * editorDisplayScale);
                    float h = Mathf.Max(rect.height, texture.height * editorDisplayScale);

                    Handles.DrawLine(new Vector3(textureRect.x, textureRect.y + anchor.y * editorDisplayScale, 0), new Vector3(textureRect.x + w, textureRect.y + anchor.y * editorDisplayScale, 0));
                    Handles.DrawLine(new Vector3(textureRect.x + anchor.x * editorDisplayScale, textureRect.y + 0, 0), new Vector3(textureRect.x + anchor.x * editorDisplayScale, textureRect.y + h, 0));
                    Handles.color = oldColor;

                    // constrain
                    param.anchorX = Mathf.Clamp(Mathf.Round(anchor.x), 0.0f, texture.width);
                    param.anchorY = Mathf.Clamp(Mathf.Round(anchor.y), 0.0f, texture.height);

                    tk2dGuiUtility.SetPositionHandleValue(id, new Vector2(param.anchorX, param.anchorY));

                    HandleUtility.Repaint();
                }

                if (mode == Mode.AttachPoint)
                {
                    Vector2 origin = new Vector2(textureRect.x, textureRect.y);
                    int     id     = "Mode.AttachPoint".GetHashCode();
                    foreach (tk2dSpriteDefinition.AttachPoint ap in param.attachPoints)
                    {
                        Vector2 apPosition = new Vector2(ap.position.x, ap.position.y);

                        if (showAttachPointSprites)
                        {
                            tk2dSpriteCollection.AttachPointTestSprite spriteProxy = null;
                            if (SpriteCollection.attachPointTestSprites.TryGetValue(ap.name, out spriteProxy) && spriteProxy.spriteCollection != null &&
                                spriteProxy.spriteCollection.IsValidSpriteId(spriteProxy.spriteId))
                            {
                                tk2dSpriteDefinition def = spriteProxy.spriteCollection.inst.spriteDefinitions[spriteProxy.spriteId];
                                tk2dSpriteThumbnailCache.DrawSpriteTextureInRect(textureRect, def, Color.white, ap.position, ap.angle, new Vector2(editorDisplayScale, editorDisplayScale));
                            }
                        }

                        Vector2 pos = apPosition * editorDisplayScale + origin;
                        GUI.color  = Color.clear;                        // don't actually draw the move handle center
                        apPosition = (tk2dGuiUtility.PositionHandle(id, pos) - origin) / editorDisplayScale;
                        GUI.color  = Color.white;

                        float handleSize = 30;

                        Handles.color = Color.green; Handles.DrawLine(pos, pos - Rotate(Vector2.up, ap.angle) * handleSize);
                        Handles.color = Color.red; Handles.DrawLine(pos, pos + Rotate(Vector2.right, ap.angle) * handleSize);

                        Handles.color = Color.white;
                        Handles.DrawWireDisc(pos, Vector3.forward, handleSize);

                        // rotation handle
                        Vector2 rotHandlePos    = pos + Rotate(Vector2.right, ap.angle) * handleSize;
                        Vector2 newRotHandlePos = tk2dGuiUtility.Handle(tk2dEditorSkin.RotateHandle, id + 1, rotHandlePos, false);
                        if (newRotHandlePos != rotHandlePos)
                        {
                            Vector2 deltaRot = newRotHandlePos - pos;
                            float   angle    = -Mathf.Atan2(deltaRot.y, deltaRot.x) * Mathf.Rad2Deg;
                            if (Event.current.control)
                            {
                                float snapAmount = Event.current.shift ? 15 : 5;
                                angle = Mathf.Floor(angle / snapAmount) * snapAmount;
                            }
                            else if (!Event.current.shift)
                            {
                                angle = Mathf.Floor(angle);
                            }
                            ap.angle = angle;
                        }

                        Rect r = new Rect(pos.x + 8, pos.y + 6, 1000, 50);
                        GUI.Label(r, ap.name, EditorStyles.whiteMiniLabel);

                        ap.position.x = Mathf.Round(apPosition.x);
                        ap.position.y = Mathf.Round(apPosition.y);
                        tk2dGuiUtility.SetPositionHandleValue(id, new Vector2(ap.position.x, ap.position.y));

                        id += 2;
                    }
                    Handles.color = Color.white;
                }

                if (mode == Mode.Texture)
                {
                    if (param.dice)
                    {
                        Handles.color = Color.red;
                        Vector3 p1, p2;
                        int     q, dq;

                        p1 = new Vector3(textureRect.x, textureRect.y, 0);
                        p2 = new Vector3(textureRect.x, textureRect.y + textureRect.height, 0);
                        q  = 0;
                        dq = param.diceUnitX;
                        if (dq > 0)
                        {
                            while (q <= texture.width)
                            {
                                Handles.DrawLine(p1, p2);
                                int q0 = q;
                                if (q < texture.width && (q + dq) > texture.width)
                                {
                                    q = texture.width;
                                }
                                else
                                {
                                    q += dq;
                                }
                                p1.x += (float)(q - q0) * editorDisplayScale;
                                p2.x += (float)(q - q0) * editorDisplayScale;
                            }
                        }
                        p1 = new Vector3(textureRect.x, textureRect.y + textureRect.height, 0);
                        p2 = new Vector3(textureRect.x + textureRect.width, textureRect.y + textureRect.height, 0);
                        q  = 0;
                        dq = param.diceUnitY;
                        if (dq > 0)
                        {
                            while (q <= texture.height)
                            {
                                Handles.DrawLine(p1, p2);
                                int q0 = q;
                                if (q < texture.height && (q + dq) > texture.height)
                                {
                                    q = texture.height;
                                }
                                else
                                {
                                    q += dq;
                                }
                                p1.y -= (float)(q - q0) * editorDisplayScale;
                                p2.y -= (float)(q - q0) * editorDisplayScale;
                            }
                        }

                        Handles.color = Color.white;
                    }
                }

                GUI.EndScrollView();
            }

            // Draw toolbar
            DrawToolbar(param, texture);

            GUILayout.EndVertical();
        }
		public void DrawTextureView(tk2dSpriteCollectionDefinition param, Texture2D texture)
		{
			HandleKeys();

			if (mode == Mode.None)
				mode = Mode.Texture;
			
			// sanity check
			if (editorDisplayScale <= 1.0f) editorDisplayScale = 1.0f;
			
			// mirror data
			currentColliderColor = param.colliderColor;
			
			GUILayout.BeginVertical(tk2dEditorSkin.SC_BodyBackground, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
		
			if (texture == null) 
			{
				// Get somewhere to put the texture...
				GUILayoutUtility.GetRect(128.0f, 128.0f, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
			}
			else
			{
				bool allowAnchor = param.anchor == tk2dSpriteCollectionDefinition.Anchor.Custom;
				bool allowCollider = (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Polygon ||
					param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.BoxCustom ||
					param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Advanced);
				if (mode == Mode.Anchor && !allowAnchor) mode = Mode.Texture;
				if (mode == Mode.Collider && !allowCollider) mode = Mode.Texture;

				Rect rect = GUILayoutUtility.GetRect(128.0f, 128.0f, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
				tk2dGrid.Draw(rect);
				
				// middle mouse drag and scroll zoom
				if (rect.Contains(Event.current.mousePosition))
				{
					if (Event.current.type == EventType.MouseDrag && Event.current.button == 2)
					{
						textureScrollPos -= Event.current.delta * editorDisplayScale;
						Event.current.Use();
						HandleUtility.Repaint();
					}
					if (Event.current.type == EventType.ScrollWheel)
					{
						editorDisplayScale -= Event.current.delta.y * 0.03f;
						Event.current.Use();
						HandleUtility.Repaint();
					}
				}
				
				bool alphaBlend = true;
				textureScrollPos = GUI.BeginScrollView(rect, textureScrollPos, 
					new Rect(0, 0, textureBorderPixels * 2 + (texture.width) * editorDisplayScale, textureBorderPixels * 2 + (texture.height) * editorDisplayScale));
				Rect textureRect = new Rect(textureBorderPixels, textureBorderPixels, texture.width * editorDisplayScale, texture.height * editorDisplayScale);
				texture.filterMode = FilterMode.Point;
				GUI.DrawTexture(textureRect, texture, ScaleMode.ScaleAndCrop, alphaBlend);

				if (mode == Mode.Collider)
				{
					if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.BoxCustom)
						DrawCustomBoxColliderEditor(textureRect, param, texture);
					if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Polygon)
						DrawPolygonColliderEditor(textureRect, ref param.polyColliderIslands, texture, false);
					if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Advanced)
						DrawAdvancedColliderEditor(textureRect, param, texture);
				}
				
				if (mode == Mode.Texture && param.customSpriteGeometry)
				{
					DrawPolygonColliderEditor(textureRect, ref param.geometryIslands, texture, true);
				}
				
				// Anchor
				if (mode == Mode.Anchor)
				{
					Color lineColor = Color.white;
					Vector2 anchor = new Vector2(param.anchorX, param.anchorY);
					Vector2 origin = new Vector2(textureRect.x, textureRect.y);
					
					int id = 99999;
					anchor = (tk2dGuiUtility.PositionHandle(id, anchor * editorDisplayScale + origin) - origin) / editorDisplayScale;
		
					Color oldColor = Handles.color;
					Handles.color = lineColor;
					float w = Mathf.Max(rect.width, texture.width * editorDisplayScale);
					float h = Mathf.Max(rect.height, texture.height * editorDisplayScale);
					
					Handles.DrawLine(new Vector3(textureRect.x, textureRect.y + anchor.y * editorDisplayScale, 0), new Vector3(textureRect.x + w, textureRect.y + anchor.y * editorDisplayScale, 0));
					Handles.DrawLine(new Vector3(textureRect.x + anchor.x * editorDisplayScale, textureRect.y + 0, 0), new Vector3(textureRect.x + anchor.x * editorDisplayScale, textureRect.y + h, 0));
					Handles.color = oldColor;
		
					// constrain
					param.anchorX = Mathf.Clamp(Mathf.Round(anchor.x), 0.0f, texture.width);
					param.anchorY = Mathf.Clamp(Mathf.Round(anchor.y), 0.0f, texture.height);
					
					tk2dGuiUtility.SetPositionHandleValue(id, new Vector2(param.anchorX, param.anchorY));
					
					HandleUtility.Repaint();			
				}

				if (mode == Mode.AttachPoint) {
					Vector2 origin = new Vector2(textureRect.x, textureRect.y);
					int id = "Mode.AttachPoint".GetHashCode();
					foreach (tk2dSpriteDefinition.AttachPoint ap in param.attachPoints) {
						Vector2 apPosition = new Vector2(ap.position.x, ap.position.y);

						if (showAttachPointSprites) {
							tk2dSpriteCollection.AttachPointTestSprite spriteProxy = null;
							if (SpriteCollection.attachPointTestSprites.TryGetValue(ap.name, out spriteProxy) && spriteProxy.spriteCollection != null &&
								spriteProxy.spriteCollection.IsValidSpriteId(spriteProxy.spriteId)) {
								tk2dSpriteDefinition def = spriteProxy.spriteCollection.inst.spriteDefinitions[ spriteProxy.spriteId ];
								tk2dSpriteThumbnailCache.DrawSpriteTextureInRect( textureRect, def, Color.white, ap.position, ap.angle, new Vector2(editorDisplayScale, editorDisplayScale) );
							}
						}

						Vector2 pos = apPosition * editorDisplayScale + origin;
						GUI.color = Color.clear; // don't actually draw the move handle center
						apPosition = (tk2dGuiUtility.PositionHandle(id, pos) - origin) / editorDisplayScale;
						GUI.color = Color.white;

						float handleSize = 30;
						
						Handles.color = Color.green; Handles.DrawLine(pos, pos - Rotate(Vector2.up, ap.angle) * handleSize);
						Handles.color = Color.red; Handles.DrawLine(pos, pos + Rotate(Vector2.right, ap.angle) * handleSize);

						Handles.color = Color.white;
						Handles.DrawWireDisc(pos, Vector3.forward, handleSize);

						// rotation handle
						Vector2 rotHandlePos = pos + Rotate(Vector2.right, ap.angle) * handleSize;
						Vector2 newRotHandlePos = tk2dGuiUtility.Handle(tk2dEditorSkin.RotateHandle, id + 1, rotHandlePos, false);
						if (newRotHandlePos != rotHandlePos) {
							Vector2 deltaRot = newRotHandlePos - pos;
							float angle = -Mathf.Atan2(deltaRot.y, deltaRot.x) * Mathf.Rad2Deg;
							if (Event.current.control) {
								float snapAmount = Event.current.shift ? 15 : 5;
								angle = Mathf.Floor(angle / snapAmount) * snapAmount;
							}
							else if (!Event.current.shift) {
								angle = Mathf.Floor(angle);
							}
							ap.angle = angle;
						}

						Rect r = new Rect(pos.x + 8, pos.y + 6, 1000, 50);
						GUI.Label( r, ap.name, EditorStyles.whiteMiniLabel );

						ap.position.x = Mathf.Round(apPosition.x);
						ap.position.y = Mathf.Round(apPosition.y);
						tk2dGuiUtility.SetPositionHandleValue(id, new Vector2(ap.position.x, ap.position.y));

						id += 2;
					}
					Handles.color = Color.white;
				}

				if (mode == Mode.Texture) {
					if (param.dice) {
						Handles.color = Color.red;
						Vector3 p1, p2;
						int q, dq;

						p1 = new Vector3(textureRect.x, textureRect.y, 0);
						p2 = new Vector3(textureRect.x, textureRect.y + textureRect.height, 0);
						q = 0;
						dq = param.diceUnitX;
						if (dq > 0) {
							while (q <= texture.width) {
								Handles.DrawLine(p1, p2);
								int q0 = q;
								if (q < texture.width && (q + dq) > texture.width)
									q = texture.width;
								else
									q += dq;
								p1.x += (float)(q - q0) * editorDisplayScale;
								p2.x += (float)(q - q0) * editorDisplayScale;
							}
						}
						p1 = new Vector3(textureRect.x, textureRect.y + textureRect.height, 0);
						p2 = new Vector3(textureRect.x + textureRect.width, textureRect.y + textureRect.height, 0);
						q = 0;
						dq = param.diceUnitY;
						if (dq > 0) {
							while (q <= texture.height) {
								Handles.DrawLine(p1, p2);
								int q0 = q;
								if (q < texture.height && (q + dq) > texture.height)
									q = texture.height;
								else
									q += dq;
								p1.y -= (float)(q - q0) * editorDisplayScale;
								p2.y -= (float)(q - q0) * editorDisplayScale;
							}
						}

						Handles.color = Color.white;
					}
				}

				GUI.EndScrollView();
			}
				
			// Draw toolbar
			DrawToolbar(param, texture);
			
			GUILayout.EndVertical();
		}
    void DrawPhysicsPropertiesPanel(tk2dSpriteCollectionDefinition param)
    {
        var oldColliderType = param.colliderType;
        param.colliderType = (tk2dSpriteCollectionDefinition.ColliderType)EditorGUILayout.EnumPopup("Collider Type", param.colliderType);

        int w = 4, h = 4;
        if (param.texture != null)
        {
            w = param.texture.width;
            h = param.texture.height;
        }

        if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.BoxCustom ||
            param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Polygon)
        {
            drawCollider = EditorGUILayout.Toggle("Draw collider", drawCollider);
            if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Polygon)
            {
                drawColliderNormals = EditorGUILayout.Toggle("Draw normals", drawColliderNormals);
            }
            currentColliderColor = param.colliderColor = (tk2dSpriteCollectionDefinition.ColliderColor)EditorGUILayout.EnumPopup("Collider color", param.colliderColor);
        }

        GUILayout.Space(8);

        if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.BoxCustom)
        {
            // If just switching to this
            if (oldColliderType != param.colliderType)
            {
                param.boxColliderMin = new Vector2(0, 0);
                param.boxColliderMax = new Vector2(w, h);
            }

            param.boxColliderMin = EditorGUILayout.Vector2Field("Min", param.boxColliderMin);
            param.boxColliderMax = EditorGUILayout.Vector2Field("Max", param.boxColliderMax);
        }

        if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Polygon)
        {
            EditorGUILayout.PrefixLabel("Properties");
            param.polyColliderCap = (tk2dSpriteCollectionDefinition.PolygonColliderCap)EditorGUILayout.EnumPopup("Collider Cap", param.polyColliderCap);
            param.colliderConvex = EditorGUILayout.Toggle("Convex", param.colliderConvex);
            param.colliderSmoothSphereCollisions = EditorGUILayout.Toggle(new GUIContent("SmoothSphereCollisions", "Smooth Sphere Collisions"), param.colliderSmoothSphereCollisions);

            bool reset = (param.polyColliderIslands == null || param.polyColliderIslands.Length == 0)?true:false;
            int polyCount = 0;

            if (!reset)
            {
                foreach (var island in param.polyColliderIslands)
                {
                    int numPoints = island.connected?island.points.Length:(island.points.Length-1);
                    polyCount += numPoints * 2;
                }

                GUILayout.Space(8);

                string islandStr = (param.polyColliderIslands.Length == 1)?"island":"islands";
                GUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Info", param.polyColliderIslands.Length.ToString() + " " + islandStr);
                if (GUILayout.Button("Reset"))
                    reset = true;
                GUILayout.EndHorizontal();
                EditorGUILayout.LabelField("", polyCount.ToString() + " polys");
            }

            if (reset || oldColliderType != param.colliderType &&
                (param.polyColliderIslands == null ||
                 param.polyColliderIslands.Length == 0 ||
                !param.polyColliderIslands[0].IsValid()) )
            {
                param.polyColliderIslands = new tk2dSpriteColliderIsland[1];
                param.polyColliderIslands[0] = new tk2dSpriteColliderIsland();
                param.polyColliderIslands[0].connected = true;

                Vector2[] p = new Vector2[4];
                p[0] = new Vector2(0, 0);
                p[1] = new Vector2(0, h);
                p[2] = new Vector2(w, h);
                p[3] = new Vector2(w, 0);
                param.polyColliderIslands[0].points = p;

                Repaint();
            }
        }

        if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Polygon)
        {
            GUILayout.Space(32);

            InlineMessage("Points" +
                          "\nClick drag - move point" +
                          "\nClick hold + delete/bkspace - delete point" +
                          "\nDouble click on line - add point");

            InlineMessage("Islands" +
                          "\nClick hold point + X - delete island" +
                          "\nPress C - create island at cursor" +
                          "\nClick hold point + T - toggle connected" +
                          "\nClick hold point + F - flip island");
        }
    }
        public void DrawTextureView(tk2dSpriteCollectionDefinition param, Texture2D texture)
        {
            if (mode == Mode.None)
            {
                mode = Mode.Texture;
            }

            // sanity check
            if (editorDisplayScale <= 1.0f)
            {
                editorDisplayScale = 1.0f;
            }

            // mirror data
            currentColliderColor = param.colliderColor;

            GUILayout.BeginVertical(tk2dEditorSkin.SC_BodyBackground, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));

            bool allowAnchor   = param.anchor == tk2dSpriteCollectionDefinition.Anchor.Custom;
            bool allowCollider = (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Polygon ||
                                  param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.BoxCustom);

            if (mode == Mode.Anchor && !allowAnchor)
            {
                mode = Mode.Texture;
            }
            if (mode == Mode.Collider && !allowCollider)
            {
                mode = Mode.Texture;
            }

            Rect rect = GUILayoutUtility.GetRect(128.0f, 128.0f, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));

            // middle mouse drag and scroll zoom
            if (rect.Contains(Event.current.mousePosition))
            {
                if (Event.current.type == EventType.MouseDrag && Event.current.button == 2)
                {
                    textureScrollPos -= Event.current.delta * editorDisplayScale;
                    Event.current.Use();
                    HandleUtility.Repaint();
                }
                if (Event.current.type == EventType.ScrollWheel)
                {
                    editorDisplayScale -= Event.current.delta.y * 0.03f;
                    Event.current.Use();
                    HandleUtility.Repaint();
                }
            }

            bool alphaBlend = true;

            textureScrollPos = GUI.BeginScrollView(rect, textureScrollPos, new Rect(0, 0, (texture.width) * editorDisplayScale, (texture.height) * editorDisplayScale));
            Rect textureRect = new Rect(0, 0, texture.width * editorDisplayScale, texture.height * editorDisplayScale);

            texture.filterMode = FilterMode.Point;
            GUI.DrawTexture(textureRect, texture, ScaleMode.ScaleAndCrop, alphaBlend);

            if (mode == Mode.Collider)
            {
                if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.BoxCustom)
                {
                    DrawCustomBoxColliderEditor(textureRect, param, texture);
                }
                if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Polygon)
                {
                    DrawPolygonColliderEditor(textureRect, ref param.polyColliderIslands, texture, false);
                }
            }

            if (mode == Mode.Texture && param.customSpriteGeometry)
            {
                DrawPolygonColliderEditor(textureRect, ref param.geometryIslands, texture, true);
            }

            // Anchor
            if (mode == Mode.Anchor)
            {
                Color   handleColor = new Color(0, 0, 0, 0.2f);
                Color   lineColor   = Color.white;
                Vector2 anchor      = new Vector2(param.anchorX, param.anchorY);

                anchor = tk2dGuiUtility.PositionHandle(99999, anchor * editorDisplayScale, 12.0f, handleColor, handleColor) / editorDisplayScale;

                Color oldColor = Handles.color;
                Handles.color = lineColor;
                float w = Mathf.Max(rect.width, texture.width * editorDisplayScale);
                float h = Mathf.Max(rect.height, texture.height * editorDisplayScale);

                Handles.DrawLine(new Vector3(0, anchor.y * editorDisplayScale, 0), new Vector3(w, anchor.y * editorDisplayScale, 0));
                Handles.DrawLine(new Vector3(anchor.x * editorDisplayScale, 0, 0), new Vector3(anchor.x * editorDisplayScale, h, 0));
                Handles.color = oldColor;

                // constrain
                param.anchorX = Mathf.Clamp(Mathf.Round(anchor.x), 0.0f, texture.width);
                param.anchorY = Mathf.Clamp(Mathf.Round(anchor.y), 0.0f, texture.height);
                HandleUtility.Repaint();
            }
            GUI.EndScrollView();

            // Draw toolbar
            DrawToolbar(param);

            GUILayout.EndVertical();
        }
        public void DrawTextureView(tk2dSpriteCollectionDefinition param, Texture2D texture)
        {
            if (mode == Mode.None)
                mode = Mode.Texture;

            // sanity check
            if (editorDisplayScale <= 1.0f) editorDisplayScale = 1.0f;

            // mirror data
            currentColliderColor = param.colliderColor;

            GUILayout.BeginVertical(tk2dEditorSkin.SC_BodyBackground, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));

            bool allowAnchor = param.anchor == tk2dSpriteCollectionDefinition.Anchor.Custom;
            bool allowCollider = (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Polygon ||
                param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.BoxCustom);
            if (mode == Mode.Anchor && !allowAnchor) mode = Mode.Texture;
            if (mode == Mode.Collider && !allowCollider) mode = Mode.Texture;

            Rect rect = GUILayoutUtility.GetRect(128.0f, 128.0f, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));

            // middle mouse drag and scroll zoom
            if (rect.Contains(Event.current.mousePosition))
            {
                if (Event.current.type == EventType.MouseDrag && Event.current.button == 2)
                {
                    textureScrollPos -= Event.current.delta * editorDisplayScale;
                    Event.current.Use();
                    HandleUtility.Repaint();
                }
                if (Event.current.type == EventType.ScrollWheel)
                {
                    editorDisplayScale -= Event.current.delta.y * 0.03f;
                    Event.current.Use();
                    HandleUtility.Repaint();
                }
            }

            bool alphaBlend = true;
            textureScrollPos = GUI.BeginScrollView(rect, textureScrollPos, new Rect(0, 0, (texture.width) * editorDisplayScale, (texture.height) * editorDisplayScale));
            Rect textureRect = new Rect(0, 0, texture.width * editorDisplayScale, texture.height * editorDisplayScale);
            texture.filterMode = FilterMode.Point;
            GUI.DrawTexture(textureRect, texture, ScaleMode.ScaleAndCrop, alphaBlend);

            if (mode == Mode.Collider)
            {
                if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.BoxCustom)
                    DrawCustomBoxColliderEditor(textureRect, param, texture);
                if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Polygon)
                    DrawPolygonColliderEditor(textureRect, ref param.polyColliderIslands, texture, false);
            }

            if (mode == Mode.Texture && param.customSpriteGeometry)
            {
                DrawPolygonColliderEditor(textureRect, ref param.geometryIslands, texture, true);
            }

            // Anchor
            if (mode == Mode.Anchor)
            {
                Color handleColor = new Color(0,0,0,0.2f);
                Color lineColor = Color.white;
                Vector2 anchor = new Vector2(param.anchorX, param.anchorY);

                anchor = tk2dGuiUtility.PositionHandle(99999, anchor * editorDisplayScale, 12.0f, handleColor, handleColor ) / editorDisplayScale;

                Color oldColor = Handles.color;
                Handles.color = lineColor;
                float w = Mathf.Max(rect.width, texture.width * editorDisplayScale);
                float h = Mathf.Max(rect.height, texture.height * editorDisplayScale);

                Handles.DrawLine(new Vector3(0, anchor.y * editorDisplayScale, 0), new Vector3(w, anchor.y * editorDisplayScale, 0));
                Handles.DrawLine(new Vector3(anchor.x * editorDisplayScale, 0, 0), new Vector3(anchor.x * editorDisplayScale, h, 0));
                Handles.color = oldColor;

                // constrain
                param.anchorX = Mathf.Clamp(Mathf.Round(anchor.x), 0.0f, texture.width);
                param.anchorY = Mathf.Clamp(Mathf.Round(anchor.y), 0.0f, texture.height);
                HandleUtility.Repaint();
            }
            GUI.EndScrollView();

            // Draw toolbar
            DrawToolbar(param);

            GUILayout.EndVertical();
        }