コード例 #1
0
    public override void OnInspectorGUI()
    {
        UPAImage img = (UPAImage)target;

        GUILayout.BeginArea(new Rect(5, 53, Screen.width - 10, Screen.height));

        if (GUILayout.Button("Open", GUILayout.Height(40)))
        {
            UPAEditorWindow.CurrentImg = UPASession.OpenImageByAsset(img);
            if (UPAEditorWindow.window != null)
            {
                UPAEditorWindow.window.Repaint();
            }
        }

        if (GUILayout.Button("Export", GUILayout.Height(40)))
        {
            UPAExportWindow.Init(img);
        }

        GUILayout.EndArea();

        //Make sure the textures are loaded
        img.LoadAllTexsFromMaps();

        float ratio = (float)img.width / (float)img.height;

        EditorGUI.DrawTextureTransparent(new Rect(5, 150, Screen.width - 10, (Screen.width - 10) * ratio), img.GetFinalImage(true), ScaleMode.ScaleToFit, 0);
    }
コード例 #2
0
    // Draw the Pixel Art Editor.
    // This includes both toolbar and painting area.
    // TODO: Add comments
    void OnGUI()
    {
        if (window == null)
        {
            Init();
        }

        if (CurrentImg == null)
        {
            string curImgPath = EditorPrefs.GetString("currentImgPath", "");

            if (curImgPath.Length != 0)
            {
                CurrentImg = UPASession.OpenImageAtPath(curImgPath);
                return;
            }

            if (GUI.Button(new Rect(window.position.width / 2f - 140, window.position.height / 2f - 25, 130, 50), "New Image"))
            {
                UPAImageCreationWindow.Init();
            }
            if (GUI.Button(new Rect(window.position.width / 2f + 10, window.position.height / 2f - 25, 130, 50), "Open Image"))
            {
                CurrentImg = UPASession.OpenImage();
                return;
            }

            return;
        }

        // Init the textures correctly, won't cost performance if nothing to load
        CurrentImg.LoadAllTexsFromMaps();

        EditorGUI.DrawRect(window.position, new Color32(30, 30, 30, 255));


        #region Event handling
        Event e = Event.current;                //Init event handler

        //Capture mouse position
        Vector2 mousePos = e.mousePosition;

        // If key is pressed
        if (e.button == 0)
        {
            // Mouse buttons
            if (e.isMouse && mousePos.y > 40 && e.type != EventType.mouseUp)
            {
                if (!UPADrawer.GetLayerPanelRect(window.position).Contains(mousePos))
                {
                    if (tool == UPATool.Eraser)
                    {
                        CurrentImg.SetPixelByPos(Color.clear, mousePos, CurrentImg.selectedLayer);
                    }
                    else if (tool == UPATool.PaintBrush)
                    {
                        CurrentImg.SetPixelByPos(selectedColor, mousePos, CurrentImg.selectedLayer);
                    }
                    else if (tool == UPATool.BoxBrush)
                    {
                        Debug.Log("TODO: Add Box Brush tool.");
                    }
                    else if (tool == UPATool.ColorPicker)
                    {
                        Vector2 pCoord   = CurrentImg.GetPixelCoordinate(mousePos);
                        Color?  newColor = CurrentImg.GetBlendedPixel((int)pCoord.x, (int)pCoord.y);
                        if (newColor != null && newColor != Color.clear)
                        {
                            selectedColor = (Color)newColor;
                        }
                        tool = lastTool;
                    }
                }
            }

            // Key down
            if (e.type == EventType.keyDown)
            {
                if (e.keyCode == KeyCode.W)
                {
                    gridOffsetY += 20f;
                }
                if (e.keyCode == KeyCode.S)
                {
                    gridOffsetY -= 20f;
                }
                if (e.keyCode == KeyCode.A)
                {
                    gridOffsetX += 20f;
                }
                if (e.keyCode == KeyCode.D)
                {
                    gridOffsetX -= 20f;
                }

                if (e.keyCode == KeyCode.Alpha1)
                {
                    tool = UPATool.PaintBrush;
                }
                if (e.keyCode == KeyCode.Alpha2)
                {
                    tool = UPATool.Eraser;
                }
                if (e.keyCode == KeyCode.P)
                {
                    lastTool = tool;
                    tool     = UPATool.ColorPicker;
                }

                if (e.keyCode == KeyCode.UpArrow)
                {
                    gridSpacing *= 1.2f;
                }
                if (e.keyCode == KeyCode.DownArrow)
                {
                    gridSpacing *= 0.8f;
                    gridSpacing -= 2;
                }
            }

            if (e.control)
            {
                if (lastTool == UPATool.Empty)
                {
                    lastTool = tool;
                    tool     = UPATool.Eraser;
                }
            }
            else if (e.type == EventType.keyUp && e.keyCode == KeyCode.LeftControl)
            {
                if (lastTool != UPATool.Empty)
                {
                    tool     = lastTool;
                    lastTool = UPATool.Empty;
                }
            }
        }

        // TODO: Better way of doing this?
        // Why does it behave so weirdly with my mac tablet.
        if (e.type == EventType.scrollWheel)
        {
            gridSpacing -= e.delta.y;
        }
        #endregion

        // DRAW IMAGE
        UPADrawer.DrawImage(CurrentImg);

        UPADrawer.DrawToolbar(window.position, mousePos);

        UPADrawer.DrawLayerPanel(window.position);

        e.Use();                // Release event handler
    }
コード例 #3
0
    public static List <UPAImage> OpenAnimationsFromFolder(bool isTemplate, string path = "")
    {
        if (path.Length == 0)
        {
            path = EditorUtility.OpenFolderPanel(
                "Choose Animation Folder",
                "Assets/Sprites",
                "");
        }



        if (path.Length != 0)
        {
            List <UPAImage> animation = new List <UPAImage>();

            DirectoryInfo dir  = new DirectoryInfo(path);
            FileInfo[]    info = dir.GetFiles("*.asset");
            Debug.Log("length = " + info.Length);



            if (info.Length > 0)
            {
                Debug.Log("info length greator than 0");

                Debug.Log(path);

                //We have some asset files, so lets load those in, instead of creating new ones
                //TODO: i dont know if these frames are loaded in order, may need to sort
                for (int i = 0; i < info.Length; i++)
                {
                    string newPath;

                    newPath = path + "/" + (i + 1) + ".asset";
                    newPath = FileUtil.GetProjectRelativePath(newPath);

                    Debug.Log("expected path = Assets / Sprites / Front RunTest / 1.asset");

                    Debug.Log("new path = ");



                    //newPath = "Assets/Sprites/Front RunTest/1";

                    Debug.Log(newPath);

                    UPAImage frameImage = OpenFrameAtPath(newPath);

                    frameImage.LoadAllTexsFromMaps();

                    frameImage.setAllNormalAlpha();

                    //frameImage.initilizeAlphas();
                    animation.Add(frameImage);
                }
            }
            else
            {
                info = dir.GetFiles("*.png");

                List <FileInfo[]> frames = sortFrames(info);

                int frameNumber = 1;


                foreach (FileInfo[] frame in frames)
                {
                    string newPath = path + "\\" + frameNumber + ".asset";

                    UPAImage i = loadImageFromFileInfo(frame, isTemplate, newPath);
                    i.initilizeAlphas();
                    animation.Add(i);

                    frameNumber += 1;
                }
            }



            UPAImage img = animation[0];

            Debug.Log(img.GetType());

            EditorPrefs.SetString("currentAnimationPath", path);
            UPAEditorWindow.CurrentImg = img;


            if (UPAEditorWindow.window != null)
            {
                UPAEditorWindow.window.Repaint();
            }
            else
            {
                UPAEditorWindow.Init();
            }



            return(animation);
        }

        return(null);
    }
コード例 #4
0
    // Draw the Pixel Art Editor.
    // This includes both toolbar and painting area.
    // TODO: Add comments
    void OnGUI()
    {
        if (window == null)
        {
            Init();
        }

        if (CurrentImg == null || TemplateImage == null)
        {
            Debug.Log("one is null");

            string curImgPath      = EditorPrefs.GetString("currentAnimationPath", "");
            string templateImgPath = EditorPrefs.GetString("templateImgPath", "");



            if (curImgPath.Length != 0)
            {
                animation = UPASession.OpenAnimationsFromFolder(false, curImgPath);

                if (animation.Count > 0)
                {
                    CurrentImg = animation[0];
                    //CurrentImg.initilizeAlphas();
                    animationIndex = 0;
                }
            }

            if (templateImgPath.Length != 0)
            {
                TemplateImage = UPASession.OpenImageAtPath(templateImgPath, true);
                TemplateImage.initilizeAlphas();
                TemplateImage.loopThroughImage();
            }

            if (CurrentImg == null)
            {
                if (GUI.Button(new Rect(window.position.width / 2f - 140, window.position.height / 2f - 25, 130, 50), "Load Animation"))
                {
                    //UPASession.OpenImage();
                    animation = UPASession.OpenAnimationsFromFolder(false);
                    CurrentImg.initilizeAlphas();
                    animationIndex = 0;
                }
            }
            else if (TemplateImage == null)
            {
                if (GUI.Button(new Rect(window.position.width / 2f - 140, window.position.height / 2f - 25, 130, 50), "New Template"))
                {
                    UPAImageCreationWindow.Init();
                }
                if (GUI.Button(new Rect(window.position.width / 2f + 10, window.position.height / 2f - 25, 130, 50), "Open Template"))
                {
                    TemplateImage = UPASession.OpenFolder(true);
                    TemplateImage.LoadAllTexsFromMaps();
                    TemplateImage.initilizeAlphas();
                    TemplateImage.loopThroughImage();
                    return;
                }
            }



            return;
        }

        if (gettingPreviewArmor)
        {
            if (GUI.Button(new Rect(window.position.width / 2f, window.position.height / 8f, 130, 50), "Load Head"))
            {
                pathDictionary["head"] = EditorUtility.OpenFolderPanel(
                    "Choose Animation Folder",
                    "Assets/Sprites/Armor/Heads/",
                    "");
            }

            if (GUI.Button(new Rect(window.position.width / 2f - 70, window.position.height / 8f + 60, 130, 50), "Load Arms"))
            {
                pathDictionary["arms"] = EditorUtility.OpenFolderPanel(
                    "Choose Animation Folder",
                    "Assets/Sprites/Armor/Arms/",
                    "");
            }

            if (GUI.Button(new Rect(window.position.width / 2f + 70, window.position.height / 8f + 60, 130, 50), "Load Body"))
            {
                pathDictionary["body"] = EditorUtility.OpenFolderPanel(
                    "Choose Animation Folder",
                    "Assets/Sprites/Armor/Bodies/",
                    "");
            }

            if (GUI.Button(new Rect(window.position.width / 2f, window.position.height / 8f + 120, 130, 50), "Load Legs"))
            {
                pathDictionary["legs"] = EditorUtility.OpenFolderPanel(
                    "Choose Animation Folder",
                    "Assets/Sprites/Legs/",
                    "");
            }

            if (GUI.Button(new Rect(window.position.width / 2f + 10, window.position.height / 2f - 25, 130, 50), "Done"))
            {
                UPAImage armorTemplate = UPASession.LoadImageTemplate(pathDictionary);

                //now we have a map to draw things from lets now map the template to each proper layer

                List <UPAImage> newAnimation = new List <UPAImage>();

                foreach (UPAImage img in animation)
                {
                    UPAImage newImage = UPASession.CreateUPAImage(img.width, img.height);

                    for (int i = 0; i < img.layers.Count; i++)
                    {
                        UPALayer layer = new UPALayer(img.layers[i]);



                        UPALayer templateLayer = armorTemplate.GetLayer(layer.name);

                        if (templateLayer != null)
                        {
                            Debug.Log("layer does not == null!");



                            foreach (Vector2 key in layer.colorMapDictionary.Keys)
                            {
                                Vector2 value = layer.colorMapDictionary[key];
                                layer.SetPixel((int)key.x, (int)key.y, templateLayer.GetPixel((int)value.x, (int)value.y));
                            }
                        }

                        if (i != 0)
                        {
                            newImage.AddLayer();
                        }

                        newImage.layers[i] = layer;
                    }

                    newAnimation.Add(newImage);
                }

                animation = newAnimation;

                gettingPreviewArmor = false;
                pathDictionary.Clear();
            }

            return;
        }



        // Init the textures correctly, won't cost performance if nothing to load
        CurrentImg.LoadAllTexsFromMaps();

        TemplateImage.LoadAllTexsFromMaps();

        EditorGUI.DrawRect(window.position, new Color32(30, 30, 30, 255));


        #region Event handling
        Event e = Event.current;                //Init event handler

        //Capture mouse position
        Vector2 mousePos = e.mousePosition;

        // If key is pressed
        if (e.button == 0)
        {
            // Mouse buttons
            if (e.isMouse && mousePos.y > 40 && e.type != EventType.MouseUp)
            {
                if (!UPADrawer.GetLayerPanelRect(window.position).Contains(mousePos))
                {
                    if (selectedTool == UPATool.Eraser)
                    {
                        CurrentImg.SetPixelByPos(Color.clear, mousePos, CurrentImg.selectedLayer);
                    }
                    else if (selectedTool == UPATool.PaintBrush)
                    {
                        CurrentImg.SetPixelByPos(selectedColor, mousePos, CurrentImg.selectedLayer);
                    }
                    else if (selectedTool == UPATool.BoxBrush)
                    {
                        Debug.Log("TODO: Add Box Brush tool.");
                    }
                    else if (selectedTool == UPATool.ColorPicker)
                    {
                        Vector2 pCoord   = CurrentImg.GetPixelCoordinate(mousePos);
                        Color?  newColor = CurrentImg.GetBlendedPixel((int)pCoord.x, (int)pCoord.y);
                        if (newColor != null && newColor != Color.clear)
                        {
                            selectedColor = (Color)newColor;
                        }
                        selectedTool = lastTool;
                    }
                    else if (selectedTool == UPATool.Map)
                    {
                        CurrentImg.mapPixelByPos(mousePos, TemplateImage.currentPixelPosition);
                    }
                }
            }

            // Key down
            if (e.type == EventType.KeyDown)
            {
                if (e.keyCode == KeyCode.W)
                {
                    changeLayer(1);
                }
                if (e.keyCode == KeyCode.S)
                {
                    changeLayer(-1);
                }
                if (e.keyCode == KeyCode.A)
                {
                    TemplateImage.focusPixel(-1);
                }
                if (e.keyCode == KeyCode.D)
                {
                    TemplateImage.focusPixel(1);
                }

                if (e.keyCode == KeyCode.Alpha1)
                {
                    selectedTool = UPATool.PaintBrush;
                }
                if (e.keyCode == KeyCode.Alpha2)
                {
                    selectedTool = UPATool.Eraser;
                }
                if (e.keyCode == KeyCode.P)
                {
                    lastTool     = selectedTool;
                    selectedTool = UPATool.ColorPicker;
                }

                if (e.keyCode == KeyCode.UpArrow)
                {
                    CurrentImg.layers[0].setAlpha(true);
                }
                if (e.keyCode == KeyCode.DownArrow)
                {
                    CurrentImg.layers[0].setAlpha(false);
                }
                if (e.keyCode == KeyCode.LeftArrow)
                {
                    changeFrame(-1);
                }
                if (e.keyCode == KeyCode.RightArrow)
                {
                    changeFrame(1);
                }
            }

            if (e.control)
            {
                if (lastTool == UPATool.Empty)
                {
                    lastTool     = selectedTool;
                    selectedTool = UPATool.Eraser;
                }
            }
            else if (e.type == EventType.KeyUp && e.keyCode == KeyCode.LeftControl)
            {
                if (lastTool != UPATool.Empty)
                {
                    selectedTool = lastTool;
                    lastTool     = UPATool.Empty;
                }
            }
        }



        // TODO: Better way of doing this?
        // Why does it behave so weirdly with my mac tablet.
        if (e.type == EventType.ScrollWheel)
        {
            gridSpacing -= e.delta.y;
        }
        #endregion

        // DRAW IMAGE
        UPADrawer.DrawImage(CurrentImg, false);

        //Test draw another image
        UPADrawer.DrawImage(TemplateImage, true);

        UPADrawer.DrawToolbar(window.position, mousePos);

        UPADrawer.DrawLayerPanel(window.position);

        UPADrawer.DrawFramePanel(window.position);

        e.Use();                // Release event handler
    }