void SelectScratchpad(tk2dTileMapScratchpad pad)
 {
     currentScratchpad = pad;
     if (currentScratchpad != null)
     {
         currentScratchpad.GetDimensions(out padWidthField, out padHeightField);
     }
 }
    public void SetActiveScratchpads(List <tk2dTileMapScratchpad> scratchpads)
    {
        activeScratchpads = scratchpads;

        currentScratchpad = null;

        UpdateFilteredScratchpads();
    }
	public void DrawGUI() {
		GUILayout.BeginHorizontal();

		GUIStyle centeredLabel = new GUIStyle(EditorStyles.label);
		centeredLabel.alignment = TextAnchor.MiddleCenter;
		GUIStyle centeredTextField = new GUIStyle(EditorStyles.textField);
		centeredTextField.alignment = TextAnchor.MiddleCenter;

		GUILayout.BeginVertical(GUILayout.Width(150.0f));

		GUILayout.BeginHorizontal();
		if (GUILayout.Button("New Scratchpad")) {
			if (activeScratchpads != null) {
				pendingAction = delegate(int i) {
					tk2dTileMapScratchpad newPad = new tk2dTileMapScratchpad();
					newPad.SetNumLayers(1);
					newPad.SetDimensions(15, 15);
					activeScratchpads.Add(newPad);
					SelectScratchpad(newPad);
					UpdateFilteredScratchpads();
					focusName = true;
				};
			}
		}
		GUILayout.EndHorizontal();

		bool pressedUp = (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.UpArrow);
		bool pressedDown = (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.DownArrow);
		bool pressedReturn = (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Return);
		bool pressedEscape = (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape);

		if (pressedUp || pressedDown) {
			Event.current.Use();
			if (filteredScratchpads != null) {
				int curIdx = 0;
				for (int i = 0; i < filteredScratchpads.Count(); ++i) {
					if (filteredScratchpads[i] == currentScratchpad)
						curIdx = i;
				}
				curIdx += pressedDown ? 1 : -1;
				curIdx = Mathf.Clamp(curIdx, 0, filteredScratchpads.Count() - 1);
				for (int i = 0; i < filteredScratchpads.Count(); ++i) {
					if (i == curIdx)
						SelectScratchpad(filteredScratchpads[i]);
				}
			}
		}

		GUILayout.BeginHorizontal();
		GUI.SetNextControlName("SearchFilter");
		string newSearchFilter = GUILayout.TextField(searchFilter, tk2dEditorSkin.ToolbarSearch);
		if (newSearchFilter != searchFilter) {
			searchFilter = newSearchFilter;
			UpdateFilteredScratchpads();
			if (searchFilter.Length > 0 && filteredScratchpads != null && filteredScratchpads.Count() > 0) {
				SelectScratchpad(filteredScratchpads[0]);
			}
		}
		GUILayout.Label("", tk2dEditorSkin.ToolbarSearchRightCap);
		GUI.SetNextControlName("dummy");
		GUILayout.Box("", GUIStyle.none, GUILayout.Width(0), GUILayout.Height(0));
		if (focusSearchFilter || (focusSearchFilterOnKeyUp && Event.current.type == EventType.KeyUp)) {
			GUI.FocusControl("dummy");
			GUI.FocusControl("SearchFilter");
			focusSearchFilter = false;
			focusSearchFilterOnKeyUp = false;
		}
		GUILayout.EndHorizontal();

		bool searchHasFocus = (GUI.GetNameOfFocusedControl() == "SearchFilter");
		if (pressedEscape) {
			if (searchHasFocus && searchFilter.Length > 0) {
				searchFilter = "";
				UpdateFilteredScratchpads();
			}
			else {
				requestClose = true;
			}
		}

		// Select All
		if (pressedReturn && GUI.GetNameOfFocusedControl() != "ScratchpadName") {
			requestSelectAllTiles = true;
			requestClose = true;
		}

		padsScrollPos = GUILayout.BeginScrollView(padsScrollPos);
		List<tk2dTileMapScratchpad> curList = null;
		if (filteredScratchpads != null)
			curList = filteredScratchpads;
		else if (activeScratchpads != null)
			curList = activeScratchpads;
		if (curList != null) {
			GUILayout.BeginVertical();
			foreach (var pad in curList) {
				bool selected = currentScratchpad == pad;
				if (selected) {
					GUILayout.BeginHorizontal();
				}
				if (GUILayout.Toggle(selected, pad.name, tk2dEditorSkin.SC_ListBoxItem)) {
					if (currentScratchpad != pad) {
						SelectScratchpad(pad);
					}
				}
				if (selected) {
					if (GUILayout.Button("", tk2dEditorSkin.GetStyle("TilemapDeleteItem"))) {
						pendingAction = delegate(int i) {
							if (EditorUtility.DisplayDialog("Delete Scratchpad \"" + currentScratchpad.name + "\" ?", " ", "Yes", "No"))
							{
								activeScratchpads.Remove(currentScratchpad);
								SelectScratchpad(null);
								UpdateFilteredScratchpads();
							}
						};
					}
					GUILayout.EndHorizontal();
				}
			}

			GUILayout.EndVertical();
		}
		GUILayout.EndScrollView();

		if (currentScratchpad != null) {
			GUILayout.Space(20.0f);

			GUILayout.BeginVertical(tk2dEditorSkin.SC_ListBoxBG);
			// Select All
			if (GUILayout.Button(new GUIContent("Select All", "(Enter)"))) {
				requestSelectAllTiles = true;
				requestClose = true;
			}

			// Name
			GUI.SetNextControlName("ScratchpadName");
			currentScratchpad.name = EditorGUILayout.TextField(currentScratchpad.name, centeredTextField);
			if (focusName) {
				GUI.FocusControl("ScratchpadName");
				focusName = false;
			}

			// Size
			int padWidth, padHeight;
			currentScratchpad.GetDimensions(out padWidth, out padHeight);
			GUILayout.BeginHorizontal();
			padWidthField = EditorGUILayout.IntField(padWidthField);
			padHeightField = EditorGUILayout.IntField(padHeightField);
			GUILayout.EndHorizontal();
			if (padWidthField != padWidth || padHeightField != padHeight) {
				GUILayout.BeginHorizontal();
				GUILayout.FlexibleSpace();
				if (GUILayout.Button("Apply size change")) {
					currentScratchpad.SetDimensions(padWidthField, padHeightField);
				}
				GUILayout.EndHorizontal();
			}

			GUILayout.EndVertical();
		}

		tooltip = GUI.tooltip;

		GUILayout.EndVertical();

		GUILayout.BeginVertical();

		// Painting area
		doMouseDown = false;
		doMouseDrag = false;
		doMouseUp = false;
		doMouseMove = false;
		if (currentScratchpad != null) {
			//temp
			currentScratchpad.UpdateCanvas();

			int scratchW, scratchH;
			currentScratchpad.GetDimensions(out scratchW, out scratchH);
			canvasScrollPos = EditorGUILayout.BeginScrollView(canvasScrollPos, GUILayout.Width(Mathf.Min(scratchW * tileSize.x * scratchZoom + 24.0f, Screen.width - 190.0f)));

			Rect padRect = GUILayoutUtility.GetRect(scratchW * tileSize.x * scratchZoom, scratchH * tileSize.y * scratchZoom, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false));
			tk2dGrid.Draw(padRect);
			padAreaRect = padRect;

			Matrix4x4 canvasMatrix = Matrix4x4.identity;
			SceneView sceneview = SceneView.lastActiveSceneView;
			if (sceneview != null) {
				Camera sceneCam = sceneview.camera;
				if (sceneCam != null) {
					canvasMatrix = sceneCam.cameraToWorldMatrix;
				}
			}
			canvasMatrix *= Matrix4x4.TRS(	new Vector3(padRect.x, padRect.y + padRect.height, 0.0f),
											Quaternion.identity,
											new Vector3(scratchZoom / texelSize.x, -scratchZoom / texelSize.y, 1.0f));

			if (Event.current.type == EventType.Repaint) {
				if (brushRenderer != null) {
					brushRenderer.DrawBrushInScratchpad(currentScratchpad.CanvasBrush, canvasMatrix, false);
					if (workingBrush != null && workingHere) {
						brushRenderer.DrawBrushInScratchpad(workingBrush, canvasMatrix, true);
					}
				}
				if (workingHere && parent != null) {
					parent.DrawTileCursor();
				}
			}

			Event ev = Event.current;
			if (ev.type == EventType.MouseMove || ev.type == EventType.MouseDrag) {
				paintMousePosition.x = ev.mousePosition.x - padRect.x;
				paintMousePosition.y = padRect.height - (ev.mousePosition.y - padRect.y);
				HandleUtility.Repaint();
			}
			if (ev.button == 0 || ev.button == 1) {
				doMouseDown = (ev.type == EventType.MouseDown);
				doMouseDrag = (ev.type == EventType.MouseDrag);
				doMouseUp = (ev.rawType == EventType.MouseUp);
			}
			doMouseMove = (ev.type == EventType.MouseMove);

			EditorGUILayout.EndScrollView();

			GUILayout.BeginHorizontal();
			if (GUILayout.Button("+", GUILayout.Width(20)))
				scratchZoom *= 1.5f;
			if (GUILayout.Button("-", GUILayout.Width(20)))
				scratchZoom /= 1.5f;
			GUILayout.EndHorizontal();
		}

		GUILayout.EndVertical();

		GUILayout.EndHorizontal();

		if (pendingAction != null && Event.current.type == EventType.Repaint) {
			pendingAction(0);
			pendingAction = null;
			HandleUtility.Repaint();
		}
	}
	void SelectScratchpad(tk2dTileMapScratchpad pad) {
		currentScratchpad = pad;
		if (currentScratchpad != null)
			currentScratchpad.GetDimensions(out padWidthField, out padHeightField);
	}
	public void SetActiveScratchpads(List<tk2dTileMapScratchpad> scratchpads) {
		activeScratchpads = scratchpads;
		currentScratchpad = null;
		UpdateFilteredScratchpads();
	}
    public void DrawGUI()
    {
        GUILayout.BeginHorizontal();

        GUIStyle centeredLabel = new GUIStyle(EditorStyles.label);

        centeredLabel.alignment = TextAnchor.MiddleCenter;
        GUIStyle centeredTextField = new GUIStyle(EditorStyles.textField);

        centeredTextField.alignment = TextAnchor.MiddleCenter;

        GUILayout.BeginVertical(tk2dEditorSkin.SC_ListBoxBG, GUILayout.Width(150.0f));

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("New Scratchpad"))
        {
            if (activeScratchpads != null)
            {
                pendingAction = delegate(int i) {
                    tk2dTileMapScratchpad newPad = new tk2dTileMapScratchpad();
                    newPad.SetNumLayers(1);
                    newPad.SetDimensions(15, 15);
                    activeScratchpads.Add(newPad);
                    SelectScratchpad(newPad);
                    UpdateFilteredScratchpads();
                    focusName = true;
                };
            }
        }
        GUILayout.EndHorizontal();

        bool pressedUp     = (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.UpArrow);
        bool pressedDown   = (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.DownArrow);
        bool pressedReturn = (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Return);
        bool pressedEscape = (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape);

        if (pressedUp || pressedDown)
        {
            Event.current.Use();
            if (filteredScratchpads != null)
            {
                int curIdx = 0;
                for (int i = 0; i < filteredScratchpads.Count(); ++i)
                {
                    if (filteredScratchpads[i] == currentScratchpad)
                    {
                        curIdx = i;
                    }
                }
                curIdx += pressedDown ? 1 : -1;
                curIdx  = Mathf.Clamp(curIdx, 0, filteredScratchpads.Count() - 1);
                for (int i = 0; i < filteredScratchpads.Count(); ++i)
                {
                    if (i == curIdx)
                    {
                        SelectScratchpad(filteredScratchpads[i]);
                    }
                }
            }
        }

        GUILayout.BeginHorizontal();
        GUI.SetNextControlName("SearchFilter");
        string newSearchFilter = GUILayout.TextField(searchFilter, tk2dEditorSkin.ToolbarSearch);

        if (newSearchFilter != searchFilter)
        {
            searchFilter = newSearchFilter;
            UpdateFilteredScratchpads();
            if (searchFilter.Length > 0 && filteredScratchpads != null && filteredScratchpads.Count() > 0)
            {
                SelectScratchpad(filteredScratchpads[0]);
            }
        }
        GUILayout.Label("", tk2dEditorSkin.ToolbarSearchRightCap);
        GUI.SetNextControlName("dummy");
        GUILayout.Box("", GUIStyle.none, GUILayout.Width(0), GUILayout.Height(0));
        if (focusSearchFilter || (focusSearchFilterOnKeyUp && Event.current.type == EventType.KeyUp))
        {
            GUI.FocusControl("dummy");
            GUI.FocusControl("SearchFilter");
            focusSearchFilter        = false;
            focusSearchFilterOnKeyUp = false;
        }
        GUILayout.EndHorizontal();

        bool searchHasFocus = (GUI.GetNameOfFocusedControl() == "SearchFilter");

        if (pressedEscape)
        {
            if (searchHasFocus && searchFilter.Length > 0)
            {
                searchFilter = "";
                UpdateFilteredScratchpads();
            }
            else
            {
                requestClose = true;
            }
        }

        // Select All
        if (pressedReturn && GUI.GetNameOfFocusedControl() != "ScratchpadName")
        {
            requestSelectAllTiles = true;
            requestClose          = true;
        }

        padsScrollPos = GUILayout.BeginScrollView(padsScrollPos);
        List <tk2dTileMapScratchpad> curList = null;

        if (filteredScratchpads != null)
        {
            curList = filteredScratchpads;
        }
        else if (activeScratchpads != null)
        {
            curList = activeScratchpads;
        }
        if (curList != null)
        {
            GUILayout.BeginVertical();
            foreach (var pad in curList)
            {
                bool selected = currentScratchpad == pad;
                if (selected)
                {
                    GUILayout.BeginHorizontal();
                }
                if (GUILayout.Toggle(selected, pad.name, tk2dEditorSkin.SC_ListBoxItem))
                {
                    if (currentScratchpad != pad)
                    {
                        SelectScratchpad(pad);
                    }
                }
                if (selected)
                {
                    if (GUILayout.Button("", tk2dEditorSkin.GetStyle("TilemapDeleteItem")))
                    {
                        pendingAction = delegate(int i) {
                            if (EditorUtility.DisplayDialog("Delete Scratchpad \"" + currentScratchpad.name + "\" ?", " ", "Yes", "No"))
                            {
                                activeScratchpads.Remove(currentScratchpad);
                                SelectScratchpad(null);
                                UpdateFilteredScratchpads();
                            }
                        };
                    }
                    GUILayout.EndHorizontal();
                }
            }

            GUILayout.EndVertical();
        }
        GUILayout.EndScrollView();

        if (currentScratchpad != null)
        {
            GUILayout.Space(20.0f);

            GUILayout.BeginVertical(tk2dEditorSkin.SC_ListBoxBG);
            // Select All
            if (GUILayout.Button(new GUIContent("Select All", "(Enter)")))
            {
                requestSelectAllTiles = true;
                requestClose          = true;
            }

            // Name
            GUI.SetNextControlName("ScratchpadName");
            currentScratchpad.name = EditorGUILayout.TextField(currentScratchpad.name, centeredTextField);
            if (focusName)
            {
                GUI.FocusControl("ScratchpadName");
                focusName = false;
            }

            // Size
            int padWidth, padHeight;
            currentScratchpad.GetDimensions(out padWidth, out padHeight);
            GUILayout.BeginHorizontal();
            padWidthField  = EditorGUILayout.IntField(padWidthField);
            padHeightField = EditorGUILayout.IntField(padHeightField);
            GUILayout.EndHorizontal();
            if (padWidthField != padWidth || padHeightField != padHeight)
            {
                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("Apply size change"))
                {
                    currentScratchpad.SetDimensions(padWidthField, padHeightField);
                }
                GUILayout.EndHorizontal();
            }

            GUILayout.EndVertical();
        }

        tooltip = GUI.tooltip;

        GUILayout.EndVertical();

        GUILayout.BeginVertical();

        // Painting area
        doMouseDown = false;
        doMouseDrag = false;
        doMouseUp   = false;
        doMouseMove = false;
        if (currentScratchpad != null)
        {
            //temp
            currentScratchpad.UpdateCanvas();

            int scratchW, scratchH;
            currentScratchpad.GetDimensions(out scratchW, out scratchH);
            canvasScrollPos = EditorGUILayout.BeginScrollView(canvasScrollPos, GUILayout.Width(Mathf.Min(scratchW * tileSize.x * scratchZoom + 24.0f, Screen.width - 190.0f)));

            Rect padRect = GUILayoutUtility.GetRect(scratchW * tileSize.x * scratchZoom, scratchH * tileSize.y * scratchZoom, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false));
            tk2dGrid.Draw(padRect);
            padAreaRect = padRect;

            Matrix4x4 canvasMatrix = Matrix4x4.identity;
            SceneView sceneview    = SceneView.lastActiveSceneView;
            if (sceneview != null)
            {
                Camera sceneCam = sceneview.camera;
                if (sceneCam != null)
                {
                    canvasMatrix = sceneCam.cameraToWorldMatrix;
                }
            }
            canvasMatrix *= Matrix4x4.TRS(new Vector3(padRect.x, padRect.y + padRect.height, 0.0f),
                                          Quaternion.identity,
                                          new Vector3(scratchZoom / texelSize.x, -scratchZoom / texelSize.y, 1.0f));

            if (Event.current.type == EventType.Repaint)
            {
                if (brushRenderer != null)
                {
                    brushRenderer.DrawBrushInScratchpad(currentScratchpad.CanvasBrush, canvasMatrix, false);
                    if (workingBrush != null && workingHere)
                    {
                        brushRenderer.DrawBrushInScratchpad(workingBrush, canvasMatrix, true);
                    }
                }
                if (workingHere && parent != null)
                {
                    parent.DrawTileCursor();
                }
            }

            Event ev = Event.current;
            if (ev.type == EventType.MouseMove || ev.type == EventType.MouseDrag)
            {
                paintMousePosition.x = ev.mousePosition.x - padRect.x;
                paintMousePosition.y = padRect.height - (ev.mousePosition.y - padRect.y);
                HandleUtility.Repaint();
            }
            if (ev.button == 0 || ev.button == 1)
            {
                doMouseDown = (ev.type == EventType.MouseDown);
                doMouseDrag = (ev.type == EventType.MouseDrag);
                doMouseUp   = (ev.rawType == EventType.MouseUp);
            }
            doMouseMove = (ev.type == EventType.MouseMove);

            EditorGUILayout.EndScrollView();

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("+", GUILayout.Width(20)))
            {
                scratchZoom *= 1.5f;
            }
            if (GUILayout.Button("-", GUILayout.Width(20)))
            {
                scratchZoom /= 1.5f;
            }
            GUILayout.EndHorizontal();
        }

        GUILayout.EndVertical();

        GUILayout.EndHorizontal();

        if (pendingAction != null && Event.current.type == EventType.Repaint)
        {
            pendingAction(0);
            pendingAction = null;
            HandleUtility.Repaint();
        }
    }