예제 #1
0
 public void PasteBitmap(int X, int Y, RagePixelBitmap bitmap)
 {
     for(int y = Mathf.Max(Y, 0); (y - Y) < bitmap.Height() && y < H; y++)
     {
         for(int x = Mathf.Max(X, 0); (x - X) < bitmap.Width() && x < W; x++)
         {
             pixels[y * W + x] = bitmap.pixels[(y - Y) * bitmap.Width() + (x - X)];
             //SetPixel(x, y, bitmap.GetPixel(x - X, y - Y));
         }
     }
 }
예제 #2
0
 public void PasteBitmap(int X, int Y, RagePixelBitmap bitmap)
 {
     for (int y = Mathf.Max(Y, 0); (y - Y) < bitmap.Height() && y < H; y++)
     {
         for (int x = Mathf.Max(X, 0); (x - X) < bitmap.Width() && x < W; x++)
         {
             pixels[y * W + x] = bitmap.pixels[(y - Y) * bitmap.Width() + (x - X)];
             //SetPixel(x, y, bitmap.GetPixel(x - X, y - Y));
         }
     }
 }
예제 #3
0
    public void PasteBitmapAlpha(int X, int Y, RagePixelBitmap bitmap)
    {
        for(int y = Mathf.Max(Y, 0); (y - Y) < bitmap.Height() && y < H; y++)
        {
            for(int x = Mathf.Max(X, 0); (x - X) < bitmap.Width() && x < W; x++)
            {
                Color src = bitmap.pixels[(y - Y) * bitmap.Width() + (x - X)];

                pixels[y * W + x] = (1f - src.a) * pixels[y * W + x] + src.a * src;
            }
        }
    }
예제 #4
0
    public void PasteBitmapAlpha(int X, int Y, RagePixelBitmap bitmap)
    {
        for (int y = Mathf.Max(Y, 0); (y - Y) < bitmap.Height() && y < H; y++)
        {
            for (int x = Mathf.Max(X, 0); (x - X) < bitmap.Width() && x < W; x++)
            {
                Color src = bitmap.pixels[(y - Y) * bitmap.Width() + (x - X)];

                pixels[y * W + x] = (1f - src.a) * pixels[y * W + x] + src.a * src;
            }
        }
    }
    public void HandleKeyboard()
    {
        if (Event.current.keyCode == KeyCode.Alpha1)
        {
            brushType = BrushType.Brush1;
        }
        if (Event.current.keyCode == KeyCode.Alpha2)
        {
            brushType = BrushType.Brush3;
        }
        if (Event.current.keyCode == KeyCode.Alpha3)
        {
            brushType = BrushType.Brush5;
        }
        if (Event.current.keyCode == KeyCode.RightBracket)
        {
            brushType = (BrushType)((int)brushType < 2 ? (int)brushType + 1 : 0);
        }
        if (Event.current.keyCode == KeyCode.LeftBracket)
        {
            brushType = (BrushType)((int)brushType > 0 ? (int)brushType - 1 : 2);
        }
        if (Event.current.keyCode == KeyCode.RightArrow && (Event.current.control || Event.current.command) && Event.current.type == EventType.keyDown)
        {
            selection = null;
            selectionActive = false;
            ragePixelSprite.shiftCell(1, true);
            ragePixelSprite.refreshMesh();
            animStripGUI.isDirty = true;
            Event.current.Use();
        }
        if (Event.current.keyCode == KeyCode.LeftArrow && (Event.current.control || Event.current.command) && Event.current.type == EventType.keyDown)
        {
            selection = null;
            selectionActive = false;
            ragePixelSprite.shiftCell(-1, true);
            ragePixelSprite.refreshMesh();
            animStripGUI.isDirty = true;
            Event.current.Use();
        }
        if (Event.current.keyCode == KeyCode.Z && (Event.current.control || Event.current.command) && Event.current.alt && Event.current.type == EventType.keyDown)
        {
            DoPaintUndo();
            animStripGUI.isDirty = true;
            spriteSheetGUI.isDirty = true;
            atlasTextureIsDirty = true;
            Event.current.Use();
        }
        if (Event.current.keyCode == KeyCode.D && (Event.current.control || Event.current.command) && Event.current.alt && Event.current.type == EventType.keyDown && mode == Mode.Select)
        {
            backBuffer.PasteBitmap(selection.X, selection.Y, frontBuffer);
            animStripGUI.isDirty = true;
            spriteSheetGUI.isDirty = true;
            atlasTextureIsDirty = true;
            Event.current.Use();
        }
        if (Event.current.keyCode == KeyCode.X && (Event.current.control || Event.current.command) && Event.current.alt && Event.current.type == EventType.keyDown && mode == Mode.Select)
        {
            SavePaintUndo();
            RagePixelUtil.Settings.clipboard = new RagePixelBitmap(frontBuffer.pixels, frontBuffer.Width(), frontBuffer.Height());
            selectionActive = false;
            Rect currentUV = ragePixelSprite.GetCurrentCell().uv;
            Rect selectionUV = new Rect(
                currentUV.xMin + (float)selection.X / (float)spritesheetTexture.width,
                currentUV.yMin + (float)selection.Y / (float)spritesheetTexture.height,
                (float)(selection.X2 - selection.X + 1) / (float)spritesheetTexture.width,
                (float)(selection.Y2 - selection.Y + 1) / (float)spritesheetTexture.height
                );
            RagePixelUtil.clearPixels(spritesheetTexture, selectionUV);
            spritesheetTexture.Apply();
            atlasTextureIsDirty = true;

            selection = null;
            Event.current.Use();
        }
        if (Event.current.keyCode == KeyCode.C && (Event.current.control || Event.current.command) && Event.current.alt && Event.current.type == EventType.keyDown && mode == Mode.Select)
        {
            RagePixelUtil.Settings.clipboard = new RagePixelBitmap(frontBuffer.pixels, frontBuffer.Width(), frontBuffer.Height());
            selection = null;
            selectionActive = false;
            Event.current.Use();
        }
        if (Event.current.keyCode == KeyCode.V && (Event.current.control || Event.current.command) && Event.current.alt && Event.current.type == EventType.keyDown)
        {
            if (RagePixelUtil.Settings.clipboard != null)
            {
                mode = Mode.Select;

                SavePaintUndo();

                Rect spriteUV = ragePixelSprite.GetCurrentCell().uv;

                selection = new RagePixelTexelRect(
                    0,
                    0,
                    Mathf.Min(RagePixelUtil.Settings.clipboard.Width(), ragePixelSprite.GetCurrentRow().pixelSizeX),
                    Mathf.Min(RagePixelUtil.Settings.clipboard.Height(), ragePixelSprite.GetCurrentRow().pixelSizeY)
                    );

                backBuffer = GrabSprite(spriteUV);
                frontBuffer = RagePixelUtil.Settings.clipboard;

                frontBufferPosition = new RagePixelTexel(0, 0);
                frontBufferDragStartPosition = new RagePixelTexel(0, 0);

                PasteBitmapToSpritesheetAlpha(frontBufferPosition, spriteUV, frontBuffer);

                selectionActive = true;
                spritesheetTexture.Apply();

                Event.current.Use();
            }
        }
    }
    public void PasteBitmapToSpritesheetAlpha(RagePixelTexel position, Rect spriteUV, RagePixelBitmap bitmap)
    {
        for (int y = Mathf.Max(position.Y, 0); y < position.Y + bitmap.Height() && y < (int)(spriteUV.height * spritesheetTexture.height); y++)
        {
            for (int x = Mathf.Max(position.X, 0); x < position.X + bitmap.Width() && x < (int)(spriteUV.width * spritesheetTexture.width); x++)
            {
                Color src = bitmap.GetPixel(x - position.X, y - position.Y);
                Color trg = spritesheetTexture.GetPixel((int)(spriteUV.x * spritesheetTexture.width) + x, (int)(spriteUV.y * spritesheetTexture.height) + y);

                spritesheetTexture.SetPixel(
                    (int)(spriteUV.x * spritesheetTexture.width) + x,
                    (int)(spriteUV.y * spritesheetTexture.height) + y,
                    src + (1f-src.a) * trg
                    );
            }
        }
    }
 public void PasteBitmapToSpritesheet(RagePixelTexel position, Rect spriteUV, RagePixelBitmap bitmap)
 {
     for (int y = Mathf.Max(position.Y, 0); y < position.Y + bitmap.Height() && y < (int)(spriteUV.height * spritesheetTexture.height); y++)
     {
         for (int x = Mathf.Max(position.X, 0); x < position.X + bitmap.Width() && x < (int)(spriteUV.width * spritesheetTexture.width); x++)
         {
             spritesheetTexture.SetPixel(
                 (int)(spriteUV.x * spritesheetTexture.width) + x,
                 (int)(spriteUV.y * spritesheetTexture.height) + y,
                 bitmap.GetPixel(x - position.X, y - position.Y)
                 );
         }
     }
 }
    public void HandleModeSelect()
    {
        if (Event.current.type == EventType.mouseDown || Event.current.type == EventType.mouseDrag || Event.current.type == EventType.mouseUp)
        {
            Vector3 mouseWorldPosition = sceneScreenToWorldPoint(Event.current.mousePosition);

            RagePixelTexel texel = WorldToTexelCoords(spritesheetTexture, ragePixelSprite.transform, mouseWorldPosition);
            int spriteWidth = ragePixelSprite.GetCurrentRow().pixelSizeX;
            int spriteHeight = ragePixelSprite.GetCurrentRow().pixelSizeY;

            if (texel.X >= 0 && texel.X < spriteWidth &&
                texel.Y >= 0 && texel.Y < spriteHeight)
            {
                if (Event.current.type == EventType.mouseDown)
                {
                    if (Event.current.button == 0)
                    {
                        if (selection == null)
                        {
                            selectionActive = false;
                            selectionStart = texel;
                            selection = new RagePixelTexelRect(texel.X, texel.Y, texel.X, texel.Y);
                        }
                        else
                        {
                            if (texel.X < frontBufferPosition.X || texel.Y < frontBufferPosition.Y || texel.X > frontBufferPosition.X + selection.Width() || texel.Y > frontBufferPosition.Y + selection.Height())
                            {
                                selectionActive = false;
                                selectionStart = texel;
                                selection = new RagePixelTexelRect(texel.X, texel.Y, texel.X, texel.Y);
                            }
                            else
                            {
                                frontBufferDragStartMousePosition = texel;
                                frontBufferDragStartPosition = frontBufferPosition;
                            }
                        }
                    }
                }
                if (Event.current.type == EventType.mouseDrag)
                {
                    if (selectionActive)
                    {
                        RagePixelTexel movement = new RagePixelTexel(texel.X - frontBufferDragStartMousePosition.X, texel.Y - frontBufferDragStartMousePosition.Y);
                        frontBufferPosition = new RagePixelTexel(frontBufferDragStartPosition.X + movement.X, frontBufferDragStartPosition.Y + movement.Y);

                        Rect spriteUV = ragePixelSprite.GetCurrentCell().uv;

                        PasteBitmapToSpritesheet(new RagePixelTexel(0, 0), spriteUV, backBuffer);
                        PasteBitmapToSpritesheetAlpha(frontBufferPosition, spriteUV, frontBuffer);

                        spritesheetTexture.Apply();
                    }
                    else
                    {
                        selection = new RagePixelTexelRect(selectionStart.X, selectionStart.Y, texel.X, texel.Y);
                    }
                }
                if (Event.current.type == EventType.mouseUp && !selectionActive)
                {
                    if (selection.Width() > 1 || selection.Height() > 1)
                    {
                        SavePaintUndo();

                        Rect spriteUV = ragePixelSprite.GetCurrentCell().uv;

                        frontBuffer = GrabRectFromSpritesheet(selection);
                        CutRectInSpritesheet(selection, spriteUV);
                        backBuffer = GrabSprite(spriteUV);

                        frontBufferPosition = new RagePixelTexel(selection.X, selection.Y);
                        frontBufferDragStartPosition = new RagePixelTexel(selection.X, selection.Y);

                        PasteBitmapToSpritesheetAlpha(frontBufferPosition, spriteUV, frontBuffer);

                        selectionActive = true;
                        spritesheetTexture.Apply();
                    }
                    else
                    {
                        selection = null;
                        selectionActive = false;
                    }
                }
                if (selectionActive && Event.current.type == EventType.mouseUp)
                {
                    spritesheetTexture.Apply();
                    atlasTextureIsDirty = true;
                }
            }
            else
            {
                if (Event.current.type != EventType.mouseDrag)
                {
                    mode = Mode.Default;
                }

            }
            if (Event.current.type != EventType.mouseUp)
            {
                Event.current.Use();
            }
        }
    }