Example #1
0
    public Texture2D GetFrameTexture()
    {
        if (frameTexture == null)
        {
            frameTexture = PIATexture.CreateBlank(PIASession.Instance.ImageData.Width, PIASession.Instance.ImageData.Height);
        }
        PIATexture.Wipe(frameTexture);
        frameTexture.filterMode = FilterMode.Point;

        // ordering up textures so the last layer is always drawn on top of the first layer
        textures = textures.OrderBy(x => x.LayerIndex).ToList();

        //building up the texture without any filters
        foreach (var item in textures)
        {
            for (int x = 0; x < PIASession.Instance.ImageData.Width; x++)
            {
                for (int y = 0; y < PIASession.Instance.ImageData.Height; y++)
                {
                    Color pixelColor = item.Texture.GetPixel(x, y);
                    if (pixelColor.a > 0)
                    {
                        frameTexture.SetPixel(x, y, pixelColor);
                    }
                }
            }
        }
        frameTexture.Apply();

        return(frameTexture);
    }
Example #2
0
    public PIATexture AddTexture(PIAImageData imageData, int layerIndex)
    {
        PIATexture texture = new PIATexture();

        texture.Init(imageData.Width, imageData.Height, layerIndex);
        textures.Add(texture);
        return(texture);
    }
Example #3
0
 public static void ClearRect(PIATexture tex, Rect rectangle)
 {
     for (int x = (int)rectangle.x; x <= rectangle.xMax; x++)
     {
         for (int y = (int)rectangle.y; y >= rectangle.y - rectangle.height; y--)
         {
             tex.Paint(x, y, ClearColor);
         }
     }
     tex.Texture.Apply();
 }
Example #4
0
    public void Init(PIAImageData _imageData)
    {
        textures                = new List <PIATexture>();
        frameTexture            = PIATexture.CreateBlank(_imageData.Width, _imageData.Height);
        frameTextureWithFilters = PIATexture.CreateBlank(_imageData.Width, _imageData.Height);

        // we make sure every time a new frame is created it contains the same amount of sub-textures
        // as the layers in the image
        for (int i = 0; i < _imageData.Layers.Count; i++)
        {
            AddTexture(_imageData, i);
        }
    }
Example #5
0
    public static Rect DrawRectangle(PIATexture tex, Vector2 startingPoint, Vector2 finalPoint, Color color)
    {
        // 0,0 on upper left corner
        Rect rectangle;

        TransformToLeftTop(ref startingPoint, tex.Texture.height);
        TransformToLeftTop(ref finalPoint, tex.Texture.height);

        if (startingPoint.x > finalPoint.x)
        {
            SwapX(ref startingPoint, ref finalPoint);
        }
        if (startingPoint.y < finalPoint.y)
        {
            SwapY(ref startingPoint, ref finalPoint);
        }

        rectangle = new Rect(startingPoint.x, startingPoint.y, Mathf.Abs(finalPoint.x - startingPoint.x), Mathf.Abs(finalPoint.y - startingPoint.y));

        // Upper segment

        for (int x = (int)startingPoint.x; x <= finalPoint.x; x++)
        {
            tex.Paint(x, (int)startingPoint.y, color, true, false);
        }
        // Downer segment

        for (int x = (int)startingPoint.x; x <= finalPoint.x; x++)
        {
            tex.Paint(x, (int)finalPoint.y, color, true, false);
        }

        // Left segment

        for (int y = (int)startingPoint.y; y >= finalPoint.y; y--)
        {
            tex.Paint((int)startingPoint.x, y, color, true, false);
        }

        // Right segment

        for (int y = (int)startingPoint.y; y >= finalPoint.y; y--)
        {
            tex.Paint((int)finalPoint.x, y, color, true, false);
        }

        tex.Texture.Apply();

        return(rectangle);
    }
Example #6
0
    public void CopyFrom(PIAFrame source)
    {
        textures.Clear();

        foreach (var item in source.textures)
        {
            byte[]     itemData   = item.Texture.EncodeToPNG();
            PIATexture piaTexture = AddTexture();
            piaTexture.LayerIndex = item.LayerIndex;
            Texture2D texture = piaTexture.Texture;
            texture.LoadImage(itemData);
            texture.Apply();
        }
    }
Example #7
0
    public static Rect DrawFilledRectangle(PIATexture tex, Vector2 startingPoint, Vector2 finalPoint, Color color)
    {
        Rect rectangle = DrawRectangle(tex, startingPoint, finalPoint, color);

        for (int x = (int)rectangle.x + 1; x < rectangle.xMax; x++)
        {
            for (int y = (int)rectangle.y; y > rectangle.y - rectangle.height - 1; y--)
            {
                tex.Paint(x, y, color);
            }
        }
        tex.Texture.Apply();

        return(rectangle);
    }
Example #8
0
        public static Texture2D GenerateSpriteSheet(int framesCount, int frameWidth, int frameHeight, PIAFrame[] frames)
        {
            Texture2D spriteSheet;
            int       rows = (int)Mathf.Sqrt(framesCount);

            int spriteSheetWidth = (framesCount * frameWidth) / rows;

            spriteSheetWidth += spriteSheetWidth % frameWidth;

            int spriteSheetHeight = frameHeight * rows;

            spriteSheetHeight += spriteSheetHeight % frameHeight;

            spriteSheet = PIATexture.CreateBlank(spriteSheetWidth, spriteSheetHeight);
            int offsetX = 0;
            int offsetY = spriteSheetHeight - frameHeight;


            for (int i = 0; i < framesCount; i++)
            {
                if (i != 0 && (frameWidth * i) % spriteSheetWidth == 0)
                {
                    offsetY -= frameHeight;
                    offsetX  = 0;
                }

                for (int x = 0; x < frameWidth; x++)
                {
                    for (int y = 0; y < frameHeight; y++)
                    {
                        Color framePixelColor = frames[i].GetFrameTexture().GetPixel(x, y);
                        spriteSheet.SetPixel(x + offsetX, y + offsetY, framePixelColor);
                        spriteSheet.Apply();
                    }
                }
                offsetX += frameWidth;
            }


            return(spriteSheet);
        }
Example #9
0
    private void OnEnable()
    {
        if (_instance == null)
        {
            _instance = this;
        }

        //DRAWER
        drawer = new PIADrawer();
        grid   = new PIAGrid();

        //INIT INPUT AREAS
        globalInputArea              = new PIAInputArea();
        globalInputArea.OnGUIUpdate += ChangeSelectedTool;
        bodyInputArea              = new PIAInputArea();
        bodyInputArea.OnGUIUpdate += ChangeImageScaleMultiplier;
        bodyInputArea.OnGUIUpdate += (e) => drawer.OnGUIExecute(e, mouseCellCoordinate);
        bodyInputArea.OnGUIUpdate += ChangeImageOffset;
        SelectionTexture           = new PIATexture();
        SelectionTexture.Init(PIASession.Instance.ImageData.Width, PIASession.Instance.ImageData.Height, 0);

        //INIT WINDOW SECTIONS
        InitializeSections();

        //INIT GRAPHICS
        skin             = Resources.Load <GUISkin>("Skins/PIAPixelArtEditorSkin");
        pen              = PIATextureDatabase.Instance.GetTexture("pen");
        eraser           = PIATextureDatabase.Instance.GetTexture("eraser");
        squareTool       = PIATextureDatabase.Instance.GetTexture("squaretool");
        filledSquareTool = PIATextureDatabase.Instance.GetTexture("filledsquaretool");
        selectionBox     = PIATextureDatabase.Instance.GetTexture("selectionbox");
        ditheringTool    = PIATextureDatabase.Instance.GetTexture("ditheringtool");
        selectedToolBG   = PIATextureDatabase.Instance.GetTexture("sideslight");
        blackBarBG       = new Texture2D(1, 1);
        blackBarBG.SetPixel(0, 0, blackBarBGColor);
        blackBarBG.Apply();
    }
Example #10
0
    public void OnGUIExecute(Event e, Vector2 pixelCoordinate)
    {
        // mouse is outside the grid
        if (pixelCoordinate.x < 0 || pixelCoordinate.y < 0)
        {
            return;
        }

        // this is used to preview where we are going to draw
        PIATexture helper = PIAEditorWindow.Instance.SelectionTexture;

        PIAFrame frame  = PIASession.Instance.ImageData.CurrentFrame;
        int      width  = PIASession.Instance.ImageData.Width;
        int      height = PIASession.Instance.ImageData.Height;

        // tools state machine
        switch (ToolType)
        {
        case PIAToolType.Paint:

            if (e.type == EventType.MouseDown || e.type == EventType.MouseDrag)
            {
                if (e.button == 0)
                {
                    frame.GetCurrentImage().Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, FirstColor);
                    Debug.Log(frame.GetCurrentImage().Texture.width + "," + frame.GetCurrentImage().Texture.height);
                }
                if (e.button == 1)
                {
                    frame.GetCurrentImage().Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, SecondColor);
                }
            }
            else
            {
                helper.Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, new Color(Color.black.r, Color.black.g, Color.black.b, 0.2f), false, true, false);
            }

            break;

        case PIAToolType.Erase:
            if (e.type == EventType.MouseDown || e.type == EventType.MouseDrag)
            {
                frame.GetCurrentImage().Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, ClearColor);
            }
            else
            {
                helper.Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, new Color(Color.white.r, Color.white.g, Color.white.b, 0.5f), false, true, false);
            }
            break;

        case PIAToolType.Rectangle:

            if (e.type == EventType.MouseDown)
            {
                downPoint = new Vector2((int)pixelCoordinate.x, (int)pixelCoordinate.y);
                if (e.button == 0)
                {
                    DrawRectangle(helper, downPoint, pixelCoordinate, new Color(FirstColor.r, FirstColor.g, FirstColor.b, 0.5f));
                }
                if (e.button == 1)
                {
                    DrawRectangle(helper, downPoint, pixelCoordinate, new Color(SecondColor.r, SecondColor.g, SecondColor.b, 0.5f));
                }
            }
            if (e.type == EventType.MouseDrag)
            {
                if (e.button == 0)
                {
                    DrawRectangle(helper, downPoint, pixelCoordinate, new Color(FirstColor.r, FirstColor.g, FirstColor.b, 0.5f));
                }
                if (e.button == 1)
                {
                    DrawRectangle(helper, downPoint, pixelCoordinate, new Color(SecondColor.r, SecondColor.g, SecondColor.b, 0.5f));
                }
            }
            if (e.type == EventType.MouseUp)
            {
                upPoint = new Vector2((int)pixelCoordinate.x, (int)pixelCoordinate.y);
                if (e.button == 0)
                {
                    DrawRectangle(frame.GetCurrentImage(), downPoint, upPoint, FirstColor);
                }
                if (e.button == 1)
                {
                    DrawRectangle(frame.GetCurrentImage(), downPoint, upPoint, SecondColor);
                }

                helper.ClearTexture(true);
            }
            break;

        case PIAToolType.RectangleFilled:

            if (e.type == EventType.MouseDown)
            {
                downPoint = new Vector2((int)pixelCoordinate.x, (int)pixelCoordinate.y);
                if (e.button == 0)
                {
                    DrawFilledRectangle(helper, downPoint, pixelCoordinate, new Color(FirstColor.r, FirstColor.g, FirstColor.b, 0.5f));
                }
                if (e.button == 1)
                {
                    DrawFilledRectangle(helper, downPoint, pixelCoordinate, new Color(SecondColor.r, SecondColor.g, SecondColor.b, 0.5f));
                }
            }
            if (e.type == EventType.MouseDrag)
            {
                if (e.button == 0)
                {
                    DrawFilledRectangle(helper, downPoint, pixelCoordinate, new Color(FirstColor.r, FirstColor.g, FirstColor.b, 0.5f));
                }
                if (e.button == 1)
                {
                    DrawFilledRectangle(helper, downPoint, pixelCoordinate, new Color(SecondColor.r, SecondColor.g, SecondColor.b, 0.5f));
                }
            }
            if (e.type == EventType.MouseUp)
            {
                upPoint = new Vector2((int)pixelCoordinate.x, (int)pixelCoordinate.y);
                if (e.button == 0)
                {
                    DrawFilledRectangle(frame.GetCurrentImage(), downPoint, upPoint, FirstColor);
                }
                if (e.button == 1)
                {
                    DrawFilledRectangle(frame.GetCurrentImage(), downPoint, upPoint, SecondColor);
                }

                helper.ClearTexture(true);
            }
            break;

        case PIAToolType.Selection:
            if (e.type == EventType.MouseDown)
            {
                downPoint = new Vector2((int)pixelCoordinate.x, (int)pixelCoordinate.y);
                DrawFilledRectangle(helper, downPoint, pixelCoordinate, new Color(Color.cyan.r, Color.cyan.g, Color.cyan.b, 0.5f));
            }
            if (e.type == EventType.MouseDrag)
            {
                selectedRect = DrawFilledRectangle(helper, downPoint, pixelCoordinate, new Color(Color.cyan.r, Color.cyan.g, Color.cyan.b, 0.5f));
            }
            if (e.keyCode == KeyCode.Delete)
            {
                ClearRect(frame.GetCurrentImage(), selectedRect);
                helper.ClearTexture(true);
            }

            break;

        case PIAToolType.Dithering:
            if ((pixelCoordinate.x + pixelCoordinate.y) % 2 == 1)
            {
                helper.Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, new Color(Color.red.r, Color.red.g, Color.red.b, 0.2f), false, true, false);
                return;
            }

            if (e.type == EventType.MouseDown || e.type == EventType.MouseDrag)
            {
                if (e.button == 0)
                {
                    frame.GetCurrentImage().Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, FirstColor);
                }
                if (e.button == 1)
                {
                    frame.GetCurrentImage().Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, SecondColor);
                }
            }
            else
            {
                helper.Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, new Color(Color.black.r, Color.black.g, Color.black.b, 0.2f), false, true, false);
            }

            break;
        }
    }
Example #11
0
    public Texture2D GetFrameTextureWithLayerFilters()
    {
        // in case we load from a saved asset
        if (frameTextureWithFilters == null)
        {
            frameTextureWithFilters = PIATexture.CreateBlank(PIASession.Instance.ImageData.Width, PIASession.Instance.ImageData.Height);
        }

        // clean up
        PIATexture.Wipe(frameTextureWithFilters);
        PIAImageData imageData = PIASession.Instance.ImageData;

        frameTextureWithFilters.filterMode = FilterMode.Point;

        // caching hidden layers indexes
        List <int> hiddenLayersIndex = new List <int>();

        foreach (var item in imageData.Layers)
        {
            if (item.Hidden == true)
            {
                hiddenLayersIndex.Add(item.Index);
            }
        }

        // ordering up textures so the last layer is always drawn on top of the first layer
        textures = textures.OrderBy(x => x.LayerIndex).ToList();

        int currentLayerTextureIndex = 0;

        // building the filtered texture from each sub-texture (layer) in the frame
        for (int i = 0; i < textures.Count; i++)
        {
            var item = textures[i];
            // skipping hidden layers
            if (hiddenLayersIndex.Contains(item.LayerIndex))
            {
                continue;
            }

            // if the sub-texture index is NOT the current selected index it gets painted transparent (alpha/2)
            if (imageData.CurrentLayer != item.LayerIndex)
            {
                for (int x = 0; x < PIASession.Instance.ImageData.Width; x++)
                {
                    for (int y = 0; y < PIASession.Instance.ImageData.Height; y++)
                    {
                        Color pixelColor = item.Texture.GetPixel(x, y);
                        pixelColor.a = pixelColor.a / 2;
                        if (pixelColor.a > 0)
                        {
                            frameTextureWithFilters.SetPixel(x, y, pixelColor);
                        }
                    }
                }
            }
            else
            {
                currentLayerTextureIndex = i;
            }
        }

        // drawing current layer texture
        for (int x = 0; x < PIASession.Instance.ImageData.Width; x++)
        {
            for (int y = 0; y < PIASession.Instance.ImageData.Height; y++)
            {
                Color pixelColor = textures[currentLayerTextureIndex].Texture.GetPixel(x, y);
                if (pixelColor.a > 0)
                {
                    frameTextureWithFilters.SetPixel(x, y, pixelColor);
                }
            }
        }

        frameTextureWithFilters.Apply();



        return(frameTextureWithFilters);
    }