예제 #1
0
        public void UpdateTilePlacement(int oldx = -1, int oldy = -1, int newx = -1, int newy = -1)
        {
            if (!MainContainer.WidgetIM.Hovering)
            {
                return;
            }
            if (MainContainer.HScrollBar != null && (MainContainer.HScrollBar.SliderDragging || MainContainer.HScrollBar.SliderHovering))
            {
                return;
            }
            if (MainContainer.VScrollBar != null && (MainContainer.VScrollBar.SliderDragging || MainContainer.VScrollBar.SliderHovering))
            {
                return;
            }
            bool Left  = WidgetIM.ClickedLeftInArea == true;
            bool Right = WidgetIM.ClickedRightInArea == true;

            if (Left || Right)
            {
                if (OriginPoint == null)
                {
                    OriginPoint = new Point(
                        (int)Math.Floor(oldx / 32d),
                        (int)Math.Floor(oldy / 32d)
                        );
                }
            }

            // Input handling
            if (Left)
            {
                if (TilesPanel.SelectButton.Selected) // Selection tool
                {
                    int sx = OriginPoint.X < MapTileX ? OriginPoint.X : MapTileX;
                    int ex = OriginPoint.X < MapTileX ? MapTileX : OriginPoint.X;
                    int sy = OriginPoint.Y < MapTileY ? OriginPoint.Y : MapTileY;
                    int ey = OriginPoint.Y < MapTileY ? MapTileY : OriginPoint.Y;
                    if (sx < 0)
                    {
                        sx = 0;
                    }
                    else if (sx >= Map.Width)
                    {
                        sx = Map.Width - 1;
                    }
                    if (sy < 0)
                    {
                        sy = 0;
                    }
                    else if (sy >= Map.Height)
                    {
                        sy = Map.Height - 1;
                    }
                    if (ex < 0)
                    {
                        ex = 0;
                    }
                    else if (ex >= Map.Width)
                    {
                        ex = Map.Width - 1;
                    }
                    if (ey < 0)
                    {
                        ey = 0;
                    }
                    else if (ey >= Map.Height)
                    {
                        ey = Map.Height - 1;
                    }
                    SelectionX      = sx;
                    SelectionY      = sy;
                    SelectionWidth  = ex - sx + 1;
                    SelectionHeight = ey - sy + 1;
                    UpdateSelection();
                }
                else // Pencil tool
                {
                    int Layer = LayerPanel.SelectedLayer;
                    if (TileDataList.Count == 0)
                    {
                        if (TilesPanel.EraserButton.Selected)
                        {
                            TileDataList.Add(null);
                        }
                        else
                        {
                            TilesPanel.SelectTile(null);
                            TileDataList.Add(null);
                            throw new Exception($"The tile data list is empty, but the eraser tool is not selected.\nCan't find tiles to draw with.");
                        }
                    }
                    MapWidget.DrawTiles(oldx, oldy, newx, newy, Layer);
                }
            }
            else if (Right)
            {
                if (TilesPanel.SelectButton.Selected) // Selection tool
                {
                    if (SelectionX != -1 || SelectionY != -1 || SelectionWidth != 0 || SelectionHeight != 0)
                    {
                        SelectionX      = -1;
                        SelectionY      = -1;
                        SelectionWidth  = 0;
                        SelectionHeight = 0;
                        UpdateSelection();
                    }
                }
                int Layer = LayerPanel.SelectedLayer;
                TileDataList.Clear();

                int OriginDiffX = MapTileX - OriginPoint.X;
                int OriginDiffY = MapTileY - OriginPoint.Y;
                CursorOrigin = Location.BottomRight;
                if (OriginDiffX < 0)
                {
                    OriginDiffX  = -OriginDiffX;
                    CursorOrigin = Location.BottomLeft;
                }
                if (OriginDiffY < 0)
                {
                    OriginDiffY = -OriginDiffY;
                    if (CursorOrigin == Location.BottomLeft)
                    {
                        CursorOrigin = Location.TopLeft;
                    }
                    else
                    {
                        CursorOrigin = Location.TopRight;
                    }
                }
                CursorWidth  = OriginDiffX;
                CursorHeight = OriginDiffY;
                UpdateCursorPosition();

                if (CursorWidth == 0 && CursorHeight == 0)
                {
                    int MapTileIndex = MapTileY * Map.Width + MapTileX;
                    if (MapTileX < 0 || MapTileX >= Map.Width || MapTileY < 0 || MapTileY >= Map.Height)
                    {
                        TilesPanel.SelectTile(null);
                    }
                    else
                    {
                        TileData tile = Map.Layers[Layer].Tiles[MapTileIndex];
                        if (tile == null)
                        {
                            TilesPanel.SelectTile(null);
                        }
                        else
                        {
                            TilesPanel.SelectTile(tile);
                        }
                    }
                }
                else
                {
                    SelectionOnMap = true;
                    TilesPanel.EraserButton.SetSelected(false);
                    int sx = OriginPoint.X < MapTileX ? OriginPoint.X : MapTileX;
                    int ex = OriginPoint.X < MapTileX ? MapTileX : OriginPoint.X;
                    int sy = OriginPoint.Y < MapTileY ? OriginPoint.Y : MapTileY;
                    int ey = OriginPoint.Y < MapTileY ? MapTileY : OriginPoint.Y;
                    TileDataList.Clear();
                    for (int y = sy; y <= ey; y++)
                    {
                        for (int x = sx; x <= ex; x++)
                        {
                            int index = y * Map.Width + x;
                            if (x < 0 || x >= Map.Width || y < 0 || y >= Map.Height)
                            {
                                TileDataList.Add(null);
                            }
                            else
                            {
                                TileData tile = Map.Layers[Layer].Tiles[index];
                                TileDataList.Add(tile);
                            }
                        }
                    }
                }
            }
        }