private void CloseTab(Image image)
 {
     selectedImageIndex = openImages.Count - 1;
     PixelEditorUtility.RemoveImageAsPersistant(image);
     openImages.Remove(image);
     //RefreshLayerList ();
 }
 private void OpenTab(Image image)
 {
     if (openImages.Contains(image))
     {
         selectedImageIndex = openImages.IndexOf(image);
         return;
     }
     openImages.Add(image);
     selectedImageIndex = openImages.Count - 1;
     //RefreshLayerList ();
     PixelEditorUtility.AddImageAsPersistant(image);
 }
        void DrawCreateNewWindow(int id)
        {
            Rect labelRect = new Rect(2.5f, EditorGUIUtility.singleLineHeight + 2.5f, this.position.width / 3 - 5, EditorGUIUtility.singleLineHeight);

            GUI.Label(new Rect(labelRect.x, labelRect.y, labelRect.width / 6 - 2.5f, labelRect.height), "Path:");
            labelRect.x += labelRect.width / 6;

            defaultPath  = GUI.TextField(new Rect(labelRect.x, labelRect.y, labelRect.width / 6 * 4 - 5, labelRect.height), defaultPath);
            labelRect.x += labelRect.width / 6 * 4;
            if (GUI.Button(new Rect(labelRect.x, labelRect.y, labelRect.width / 6 - 5, labelRect.height), "..."))
            {
                defaultPath = EditorUtility.SaveFilePanelInProject("Create Image", "New Image", "asset", "");
            }
            labelRect.x  = 2.5f;
            labelRect.y += EditorGUIUtility.singleLineHeight * 1.5f;
            GUI.Label(labelRect, "Settings", EditorStyles.boldLabel);
            labelRect.y += EditorGUIUtility.singleLineHeight;
            GUI.Label(new Rect(labelRect.x, labelRect.y, labelRect.width / 8 * 1.5f - 2.5f, labelRect.height), "Width:");
            labelRect.x += labelRect.width / 8 * 1.5f;
            defaultWidth = EditorGUI.IntSlider(new Rect(labelRect.x, labelRect.y, labelRect.width / 8 * 6f - 2.5f, labelRect.height), defaultWidth, 1, 512);
            labelRect.x += labelRect.width / 8 * 6f - 2.5F;
            GUI.Label(new Rect(labelRect.x, labelRect.y, labelRect.width / 8, labelRect.height), "px");
            labelRect.x  = 2.5f;
            labelRect.y += EditorGUIUtility.singleLineHeight * 1.1f;
            GUI.Label(new Rect(labelRect.x, labelRect.y, labelRect.width / 8 * 1.5f - 2.5f, labelRect.height), "Height:");
            labelRect.x  += labelRect.width / 8 * 1.5f;
            defaultHeight = EditorGUI.IntSlider(new Rect(labelRect.x, labelRect.y, labelRect.width / 8 * 6f - 2.5f, labelRect.height), defaultHeight, 1, 512);
            labelRect.x  += labelRect.width / 8 * 6f - 2.5F;
            GUI.Label(new Rect(labelRect.x, labelRect.y, labelRect.width / 8, labelRect.height), "px");
            labelRect.x = 2.5f;


            Rect buttonRect = new Rect(2.5f, this.position.height / 3 - EditorGUIUtility.singleLineHeight * 1.5f - 7.5f, this.position.width / 6 - 5f, EditorGUIUtility.singleLineHeight * 1.5f);

            GUI.backgroundColor = new Color(0.25f, 0.75f, 0.25f);
            GUI.enabled         = !string.IsNullOrEmpty(defaultPath);
            if (GUI.Button(buttonRect, "Create"))
            {
                Image image = PixelEditorUtility.CreateImage(defaultPath, defaultWidth, defaultHeight);
                if (image)
                {
                    OpenTab(image);
                    showCreateNewWindow = false;
                }
            }
            GUI.enabled         = true;
            buttonRect.x       += this.position.width / 6 - 2.5f;
            GUI.backgroundColor = new Color(0.7f, 0.25f, 0.25f);
            if (GUI.Button(buttonRect, "Cancel"))
            {
                showCreateNewWindow = false;
            }
        }
 private void OnEnable()
 {
     OnLayerWindowEnable();
     openImages = PixelEditorUtility.GetPersistantImages();
 }
        private void DrawToolbar(Rect rect)
        {
            //Event Handler
            Event e = Event.current;

            if (rect.Contains(e.mousePosition))
            {
                Repaint();
            }

            GUI.Box(rect, GUIContent.none, EditorStyles.toolbar);
            //Tabs
            Rect tabsRect = new Rect(rect.x, rect.y, rect.width - rect.width / 4 - 10, rect.height);

            for (int i = 0; i < openImages.Count; i++)
            {
                bool isValid = openImages [i] != null;
                if (!isValid)
                {
                    openImages.RemoveAt(i);
                    continue;
                }

                Color tabColor = (i == selectedImageIndex) ? new Color(0.55f, 0.55f, 0.55f) : new Color(0.85f, 0.85f, 0.85f);
                GUI.backgroundColor = tabColor;

                float tabWidth      = Mathf.Clamp(tabsRect.width / openImages.Count, 75, 200);
                bool  isDisplayable = tabsRect.x + tabWidth * (i + 1.5f) < rect.width;

                if (isDisplayable)
                {
                    Rect currentTabRect = new Rect(tabsRect.x + tabWidth * i, tabsRect.y, tabWidth, tabsRect.height);
                    currentTabRect.width -= 25;
                    if (GUI.Button(currentTabRect, openImages [i].name, EditorStyles.toolbarButton))
                    {
                        selectedImageIndex = i;
                        //RefreshLayerList ();
                    }
                    currentTabRect.x    += currentTabRect.width;
                    currentTabRect.width = 20;

                    GUI.backgroundColor = (currentTabRect.Contains(e.mousePosition)) ? new Color(0.7f, 0.25f, 0.25f) : tabColor;
                    if (GUI.Button(currentTabRect, "X", EditorStyles.toolbarButton))
                    {
                        CloseTab(openImages[i]);
                        continue;
                    }
                }
                GUI.backgroundColor = Color.white;
            }

            //Buttons
            GUI.contentColor = Color.black;
            Rect buttonsRect = new Rect(rect.width - rect.width / 4, rect.y, rect.width / 4, rect.height);

            if (GUI.Button(new Rect(buttonsRect.x, buttonsRect.y, buttonsRect.width / 4, buttonsRect.height), Resources.Load <Texture2D>("Icons/new"), EditorStyles.toolbarButton))
            {
                defaultPath         = "";
                showCreateNewWindow = true;
            }
            buttonsRect.x += buttonsRect.width / 4;
            if (GUI.Button(new Rect(buttonsRect.x, buttonsRect.y, buttonsRect.width / 4, buttonsRect.height), Resources.Load <Texture2D> ("Icons/save"), EditorStyles.toolbarButton))
            {
            }
            buttonsRect.x += buttonsRect.width / 4;
            if (GUI.Button(new Rect(buttonsRect.x, buttonsRect.y, buttonsRect.width / 4, buttonsRect.height), Resources.Load <Texture2D> ("Icons/open"), EditorStyles.toolbarButton))
            {
                Image image = PixelEditorUtility.OpenImage();
                if (image)
                {
                    OpenTab(image);
                }
            }
            buttonsRect.x += buttonsRect.width / 4;
            if (GUI.Button(new Rect(buttonsRect.x, buttonsRect.y, buttonsRect.width / 4, buttonsRect.height), Resources.Load <Texture2D> ("Icons/settings"), EditorStyles.toolbarButton))
            {
            }
            GUI.contentColor    = Color.white;
            GUI.backgroundColor = Color.white;
        }