Exemplo n.º 1
0
    public void UpdatePalette()
    {
        // -------------------------------------------------
        // MOUSE INPUT
        // -------------------------------------------------
        bool leftMouse      = Input.GetMouseButton(0);
        bool rightMouse     = Input.GetMouseButton(1);
        bool leftMouseDown  = Input.GetMouseButtonDown(0);
        bool leftMouseUp    = Input.GetMouseButtonUp(0);
        bool rightMouseDown = Input.GetMouseButtonDown(1);
        bool rightMouseUp   = Input.GetMouseButtonUp(1);

        bool zoomedThisFrame = false;

        RaycastHit ray = new RaycastHit();

        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out ray))
        {
            Texture2D textureMap = (Texture2D)ray.transform.renderer.material.mainTexture;
            Vector2   pixelUV    = ray.textureCoord;
            pixelUV.x *= textureMap.width;
            pixelUV.y *= textureMap.height;

            int x = (int)pixelUV.x;
            int y = (int)pixelUV.y;

            // -------------------------------------------------------------------------
            // PALETTE PLANE
            // -------------------------------------------------------------------------
            if (ray.transform.gameObject == _palettePlane)
            {
                if (!_editor.GetIsDragging())
                {
                    int index = (PALETTE_NUM_Y - y - 1) * PALETTE_NUM_X + x;

                    if (GetColorExists(index))
                    {
                        if (leftMouse)
                        {
                            Color32 color = _paletteColors[index];
                            if (!color.Equals(_editor.GetCurrentColor()))
                            {
                                if (_editor.GetToolMode() == ToolMode.Eraser)
                                {
                                    _editor.SetToolMode(ToolMode.Brush);
                                }

                                _editor.SetCurrentColor(_paletteColors[index]);
                            }
                        }
                        else if (rightMouse && _editor.GetModifierKey() && !_editor.GetIsEditingPixels())
                        {
                            _editor.AddNewCommand(new DeletePaletteColorCommand(index));
                            _editor.SetTextEnteringMode(TextEnteringMode.None);
                        }
                        else if (rightMouse && !_editor.GetModifierKey())
                        {
                            _editor.SetTextEnteringMode(TextEnteringMode.PaletteColor);

                            Color32 color = _paletteColors[index];
                            _editor.PaletteColorString = color.r.ToString() + "," + color.g.ToString() + "," + color.b.ToString() + ((color.a < 255) ? ("," + color.a.ToString()) : "");
                            _paletteColorEditIndex     = index;

                            _editor.PaletteInputScreenPosition = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);                             // gotta flip the y-position;
                        }

                        // -----------------------------------------------------------
                        // COLOR SWAPPING
                        // -----------------------------------------------------------
                        if (leftMouseDown)
                        {
                            _paletteColorEditIndex       = index;
                            _draggingCurrentPaletteColor = true;
                        }
                        else if (leftMouseUp && _draggingCurrentPaletteColor && index != _paletteColorEditIndex && _editor.GetModifierKey() && !_editor.GetIsEditingPixels())
                        {
                            if (_editor.GetModifierKey())
                            {
                                _editor.AddNewCommand(new SetPaletteColorCommand(_paletteColors[_paletteColorEditIndex], index));                                 // duplicate color
                            }
                            else
                            {
                                _editor.AddNewCommand(new SwapPaletteColorsCommand(index, _paletteColorEditIndex));
                            }

                            _draggingCurrentPaletteColor = false;
                        }
                    }
                    else
                    {
                        if (leftMouseDown)
                        {
                            _draggingPaletteBorder = true;

                            Vector2 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                            _borderDragWorldOffset = (Vector2)_paletteBorderPlane.transform.position - mouseWorldPos;
                        }
                        else if (rightMouse)
                        {
                            _editor.SetTextEnteringMode(TextEnteringMode.PaletteColor);

                            _editor.PaletteColorString = "...";

                            _editor.PaletteInputScreenPosition = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);                             // gotta flip the y-position;
                            _paletteColorEditIndex             = index;
                        }
                        else if (leftMouseUp && _draggingCurrentPaletteColor && index != _paletteColorEditIndex && _editor.GetModifierKey() && !_editor.GetIsEditingPixels())
                        {
                            _editor.AddNewCommand(new SwapPaletteColorsCommand(index, _paletteColorEditIndex));

                            _draggingCurrentPaletteColor = false;
                        }
                    }
                }

                ZoomPalettePlane();
                zoomedThisFrame = true;
            }
            // -------------------------------------------------------------------------
            // PALETTE BORDER PLANE
            // -------------------------------------------------------------------------
            else if (ray.transform.gameObject == _paletteBorderPlane)
            {
                if (leftMouseDown && !_editor.GetIsDragging() && !_editor.GetIsEditingPixels())
                {
                    _draggingPaletteBorder = true;

                    Vector2 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                    _borderDragWorldOffset = (Vector2)_paletteBorderPlane.transform.position - mouseWorldPos;
                }

                ZoomPalettePlane();
                zoomedThisFrame = true;
            }
        }

        // -------------------------------------------------------------------------
        // ZOOMING
        // -------------------------------------------------------------------------
        if (!zoomedThisFrame && _zoomTimer > 0.0f)
        {
            ZoomPalettePlane();
        }

        // -------------------------------------------------
        // WRAP UP SOME PALETTE MOUSE SHIT
        // -------------------------------------------------
        if (leftMouseUp && _draggingCurrentPaletteColor)
        {
            _draggingCurrentPaletteColor = false;
        }

        if (leftMouse && _editor.GetTextEnteringMode() == TextEnteringMode.PaletteColor)
        {
            _editor.SetTextEnteringMode(TextEnteringMode.None);
        }

        if (_draggingPaletteBorder)
        {
            if (Input.GetMouseButtonUp(0))
            {
                _draggingPaletteBorder = false;
                PlayerPrefs.SetFloat("palettePosX", _palettePlane.transform.position.x);
                PlayerPrefs.SetFloat("palettePosY", _palettePlane.transform.position.y);
            }
            else
            {
                Vector2 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                _palettePlane.transform.position = new Vector3(mouseWorldPos.x + _borderDragWorldOffset.x, mouseWorldPos.y + _borderDragWorldOffset.y, PixelEditorScreen.PALETTE_PLANE_DEPTH);
            }
        }
    }
Exemplo n.º 2
0
    public void UpdateCanvas()
    {
        // -------------------------------------------------
        // MOUSE INPUT
        // -------------------------------------------------
        bool leftMouse      = Input.GetMouseButton(0);
        bool rightMouse     = Input.GetMouseButton(1);
        bool leftMouseDown  = Input.GetMouseButtonDown(0);
        bool leftMouseUp    = Input.GetMouseButtonUp(0);
        bool rightMouseDown = Input.GetMouseButtonDown(1);
        bool rightMouseUp   = Input.GetMouseButtonUp(1);

        bool zoomedThisFrame = false;

        // -------------------------------------------------
        // MOUSE INPUT
        // -------------------------------------------------
        RaycastHit ray = new RaycastHit();

        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out ray))
        {
            Texture2D textureMap = (Texture2D)ray.transform.renderer.material.mainTexture;
            Vector2   pixelUV    = ray.textureCoord;
            pixelUV.x *= textureMap.width;
            pixelUV.y *= textureMap.height;

            int x = (int)pixelUV.x;
            int y = (int)pixelUV.y;

            _editor.GridIndexString = "(" + x.ToString() + "," + y.ToString() + ")";

            // -------------------------------------------------------------------------
            // CANVAS PLANE
            // -------------------------------------------------------------------------
            if (ray.transform.gameObject == _canvasPlane)
            {
                int  index       = y * pixelWidth + x;
                bool pixelExists = _pixels[_editor.CurrentFrame][index].a > 0;

                if (leftMouse && !_editor.GetIsDragging())
                {
                    if (_editor.GetToolMode() == ToolMode.Brush)
                    {
                        Color32 indexColor = _pixels[_editor.CurrentFrame][index];
                        if (!indexColor.Equals(_editor.GetCurrentColor()) || indexColor.a != 255)
                        {
                            if (_editor.GetModifierKey())
                            {
                                DrawLine(_lastDrawnPoint.x, _lastDrawnPoint.y, x, y);
                                if (_mirroringMode == MirroringMode.Horizontal || _mirroringMode == MirroringMode.Both)
                                {
                                    DrawLine(pixelWidth - 1 - _lastDrawnPoint.x, _lastDrawnPoint.y, pixelWidth - 1 - x, y);
                                }
                                if (_mirroringMode == MirroringMode.Vertical || _mirroringMode == MirroringMode.Both)
                                {
                                    DrawLine(_lastDrawnPoint.x, pixelHeight - 1 - _lastDrawnPoint.y, x, pixelHeight - 1 - y);
                                }
                                if (_mirroringMode == MirroringMode.Both)
                                {
                                    DrawLine(pixelWidth - 1 - _lastDrawnPoint.x, pixelHeight - 1 - _lastDrawnPoint.y, pixelWidth - 1 - x, pixelHeight - 1 - y);
                                }
                            }
                            else
                            {
                                ToolDrawPixel(x, y);
                                if (_mirroringMode == MirroringMode.Horizontal || _mirroringMode == MirroringMode.Both)
                                {
                                    ToolDrawPixel(pixelWidth - 1 - x, y);
                                }
                                if (_mirroringMode == MirroringMode.Vertical || _mirroringMode == MirroringMode.Both)
                                {
                                    ToolDrawPixel(x, pixelHeight - 1 - y);
                                }
                                if (_mirroringMode == MirroringMode.Both)
                                {
                                    ToolDrawPixel(pixelWidth - 1 - x, pixelHeight - 1 - y);
                                }
                            }

                            _lastDrawnPoint = new PixelPoint(x, y);
                        }
                    }
                    else if (_editor.GetToolMode() == ToolMode.Eraser)
                    {
                        ToolErasePixel(x, y);
                        if (_mirroringMode == MirroringMode.Horizontal || _mirroringMode == MirroringMode.Both)
                        {
                            ToolErasePixel(pixelWidth - 1 - x, y);
                        }
                        if (_mirroringMode == MirroringMode.Vertical || _mirroringMode == MirroringMode.Both)
                        {
                            ToolErasePixel(x, pixelHeight - 1 - y);
                        }
                        if (_mirroringMode == MirroringMode.Both)
                        {
                            ToolErasePixel(pixelWidth - 1 - x, pixelHeight - 1 - y);
                        }
                    }
                    else if (_editor.GetToolMode() == ToolMode.Bucket)
                    {
                        Color32 indexColor = _pixels[_editor.CurrentFrame][index];
                        if (!indexColor.Equals(_editor.GetCurrentColor()) || indexColor.a != 255)
                        {
                            if (!_pixelsDrawnThisStroke.Contains(index))
                            {
                                _editor.AddNewCommand(new FloodFillCommand(x, y, _editor.GetCurrentColor()));
                                _pixelsDrawnThisStroke.Add(index);
                            }
                        }
                    }
                    else if (_editor.GetToolMode() == ToolMode.Dropper)
                    {
                        Color32 indexColor = _pixels[_editor.CurrentFrame][index];
                        if (!indexColor.Equals(_editor.GetCurrentColor()))
                        {
                            if (pixelExists)
                            {
                                _editor.SetCurrentColor(_canvasTexture.GetPixel(x, y));
                            }
                        }
                    }
                }
                else if (leftMouseUp && _editor.GetToolMode() == ToolMode.Dropper && !_editor.GetIsDragging())
                {
                    Color32 indexColor = _pixels[_editor.CurrentFrame][index];
                    if (pixelExists)
                    {
                        _editor.SetCurrentColor(_canvasTexture.GetPixel(x, y));
                        _editor.SetToolMode(ToolMode.Brush);
                    }
                    else
                    {
                        _editor.SetToolMode(ToolMode.Eraser);
                    }
                }
                else if (_editor.GetModifierKey() && rightMouseDown && pixelExists)
                {
                    _editor.GetPalette().AddColorToFirstEmptySpot(_canvasTexture.GetPixel(x, y));
                }
                else if (rightMouse)
                {
                    _editor.SetToolMode(ToolMode.Dropper);

                    // check to see if there is any color at this pixel
                    // if so, eyedropper it
                    if (pixelExists)
                    {
                        _editor.SetCurrentColor(_canvasTexture.GetPixel(x, y));
                    }
                }
                else if (rightMouseUp)
                {
                    if (pixelExists)
                    {
                        _editor.SetToolMode(ToolMode.Brush);
                    }
                    else
                    {
                        _editor.SetToolMode(ToolMode.Eraser);
                    }
                }

                ZoomCanvasPlane();
                zoomedThisFrame = true;
            }
            // -------------------------------------------------------------------------
            // CANVAS BORDER PLANE
            // -------------------------------------------------------------------------
            else if (ray.transform.gameObject == _canvasBorderPlane)
            {
                if (leftMouseDown && !_editor.GetIsDragging() && !CurrentlyEditingPixels)
                {
                    _draggingCanvasBorder = true;

                    Vector2 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                    _borderDragWorldOffset = (Vector2)_canvasBorderPlane.transform.position - mouseWorldPos;
                }

                ZoomCanvasPlane();
                zoomedThisFrame = true;
            }
        }

        // -------------------------------------------------------------------------
        // ZOOMING
        // -------------------------------------------------------------------------
        if (!zoomedThisFrame && _zoomTimer > 0.0f)
        {
            ZoomCanvasPlane();
        }

        if (_draggingCanvasBorder)
        {
            if (Input.GetMouseButtonUp(0))
            {
                _draggingCanvasBorder = false;
                PlayerPrefs.SetFloat("canvasPosX", _canvasPlane.transform.position.x);
                PlayerPrefs.SetFloat("canvasPosY", _canvasPlane.transform.position.y);
            }
            else
            {
                Vector2 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                _canvasPlane.transform.position = new Vector3(mouseWorldPos.x + _borderDragWorldOffset.x, mouseWorldPos.y + _borderDragWorldOffset.y, PixelEditorScreen.CANVAS_PLANE_DEPTH);
            }
        }

        // -------------------------------------------------------------------------
        // CURRENTLY EDITING STUFF
        // -------------------------------------------------------------------------
        if (Input.GetMouseButtonUp(0))
        {
            if (_isCurrentlyEditingPixels)
            {
                _isCurrentlyEditingPixels = false;

                _editor.AddNewCommand(new EditPixelsCommand(_currentlyReplacedPixels, _currentlyAddedPixels));
                _currentlyReplacedPixels.Clear();
                _currentlyAddedPixels.Clear();
            }
        }

        if (_pixelsDrawnThisStroke.Count > 0 && leftMouseUp)
        {
            _pixelsDrawnThisStroke.Clear();
        }

        // -------------------------------------------------
        // CLEARING FRAME
        // -------------------------------------------------
        if (!_editor.GetModifierKey() && (Input.GetKeyDown(KeyCode.Backspace) || Input.GetKeyDown(KeyCode.Q)) && !_editor.GetIsEditingPixels() && _editor.GetTextEnteringMode() == TextEnteringMode.None)
        {
            _editor.AddNewCommand(new ClearCurrentFrameCommand());
        }

        // -------------------------------------------------
        // SHIFTING PIXELS
        // -------------------------------------------------
        if (!_editor.GetModifierKey() && !_editor.GetIsEditingPixels() && _editor.GetTextEnteringMode() == TextEnteringMode.None)
        {
            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                _editor.AddNewCommand(new ShiftCanvasCommand(Direction.Up));
            }
            else if (Input.GetKeyDown(KeyCode.DownArrow))
            {
                _editor.AddNewCommand(new ShiftCanvasCommand(Direction.Down));
            }
            else if (Input.GetKeyDown(KeyCode.RightArrow))
            {
                _editor.AddNewCommand(new ShiftCanvasCommand(Direction.Right));
            }
            else if (Input.GetKeyDown(KeyCode.LeftArrow))
            {
                _editor.AddNewCommand(new ShiftCanvasCommand(Direction.Left));
            }
        }

        // -------------------------------------------------
        // FLIPPING CANVAS
        // -------------------------------------------------
        if (_editor.GetModifierKey() && !_editor.GetIsEditingPixels() && _editor.GetTextEnteringMode() == TextEnteringMode.None)
        {
            if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.DownArrow))
            {
                _editor.AddNewCommand(new FlipCanvasCommand(false));
            }
            else if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow))
            {
                _editor.AddNewCommand(new FlipCanvasCommand(true));
            }
        }

        // -------------------------------------------------
        // ONION SKIN
        // -------------------------------------------------
        if (!_editor.GetModifierKey() && Input.GetKeyDown(KeyCode.O) && !_editor.GetIsEditingPixels() && _editor.GetTextEnteringMode() == TextEnteringMode.None)
        {
            ToggleOnionSkinMode();
        }

        if (!_editor.GetIsEditingPixels() && _editor.GetTextEnteringMode() == TextEnteringMode.None)
        {
            if (Input.GetKeyDown(KeyCode.Minus))
            {
                AdjustOnionSkinOpacity(false);
            }
            else if (Input.GetKeyDown(KeyCode.Equals))
            {
                AdjustOnionSkinOpacity(true);
            }
        }

        // -------------------------------------------------
        // MIRRORING
        // -------------------------------------------------
        if (!_editor.GetModifierKey() && Input.GetKeyDown(KeyCode.M) && !_editor.GetIsEditingPixels() && _editor.GetTextEnteringMode() == TextEnteringMode.None)
        {
            ToggleMirroringMode();
        }
    }