public TilesetSelectedEventArgs(Tileset tileset)
 {
     _tileset = tileset;
 }
 public TilesetChangedEventArgs(Tileset oldTileset, Tileset newTileset)
 {
     _oldTileset = oldTileset;
     _newTileset = newTileset;
 }
        /// <summary>
        /// Handles the TilesetSelected event of the <see cref="TilesetManager"/> object;
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="TilesetSelectedEventArgs"/> instance containing the event data.</param>
        private void TilesetSelected(object sender, TilesetSelectedEventArgs e)
        {
            if (e.Tileset == null)
            {
                viewTexture.Texture = null;
                return;
            }

            for (int i = 0; i < cmbTilesets.Items.Count; i++)
            {
                if (cmbTilesets.Items[i].ToString() == e.Tileset.Name)
                    cmbTilesets.SelectedIndex = i;
            }

            Tileset = e.Tileset;
        }
        /// <summary>
        /// Runs a flood-fill algorithm with the passed co-ordinates of the root.
        /// Returns an enumerable list of points based on the results.
        /// </summary>
        /// <param name="tileset">Tileset to replace with.</param>
        /// <param name="selection">Tile selection to replace with.</param>
        /// <param name="x">X co-ordinate of the root tile.</param>
        /// <param name="y">Y co-ordinate of the root tile.</param>
        /// <returns>An enumerable list of points.</returns>
        public List<Point> FillCache(Tileset tileset, PointSelection selection, int x, int y)
        {
            if (x < 0 || x > Width - 1 || y < 0 || y > Height - 1) return null;

            _tileCache = new List<Point>();

            _cacheMap = new FillTile[Width, Height];
            for (var x2 = 0; x2 < Width; x2++)
            {
                for (var y2 = 0; y2 < Height; y2++)
                {
                    var tile = SelectedLayer.Tiles[x2, y2];
                    _cacheMap[x2, y2] = new FillTile(tile.Tileset, tile.SrcX, tile.SrcY, tile.Terrain);
                }
            }

            _tileChecked = new bool[Width, Height];

            var point = selection.GetTopLeftMostPoint();
            var replacementTile = new FillTile(tileset.ID, point.X, point.Y, 0);
            var currTile = GetFillCacheTile(x, y, replacementTile);
            var originalFillTile = new FillTile(currTile.Tileset, currTile.X, currTile.Y, currTile.Terrain);

            FillCacheTiles(originalFillTile, replacementTile, x, y);

            return _tileCache;
        }
        /// <summary>
        /// Handles the <see cref="DialogTileset"/> form, checks all the directories and builds the <see cref="Tileset"/> object.
        /// </summary>
        public void NewTileset()
        {
            using (var dialog = new DialogTileset())
            {
                var result = dialog.ShowDialog();
                if (result != DialogResult.OK) return;

                var name = dialog.TilesetName;
                var path = dialog.FilePath;
                int width = dialog.TileWidth;
                int height = dialog.TileHeight;

                if (CheckTileset(name))
                {
                    MessageBox.Show(@"A tileset with this name already exists.", @"New Tileset");
                    return;
                }

                var index = 1;
                while (true)
                {
                    var exit = true;
                    foreach (var tmp in Tilesets)
                    {
                        if (tmp.ID == index)
                            exit = false;
                    }

                    if (exit)
                        break;

                    index++;
                }

                string newName = name + Path.GetExtension(path);
                string newPath = Path.Combine(ProjectManager.Instance.Project.TilesetPath, newName);
                File.Copy(path, newPath);

                path = newPath;

                var tileset = new Tileset(index, name, path, width, height);

                Tilesets.Add(tileset);

                SaveTileset(index);

                if (TilesetAdded != null)
                    TilesetAdded.Invoke(this, new TilesetAddedEventArgs(tileset));

                SelectTileset(index);
            }
        }
 /// <summary>
 /// Sets an individual tile to the map.
 /// </summary>
 /// <param name="tileset">Tileset of the tile.</param>
 /// <param name="layer">Layer to set the tile to.</param>
 /// <param name="destX">X co-ordinate to place to.</param>
 /// <param name="destY">Y co-ordinate to place to.</param>
 /// <param name="srcX">X co-ordinate of the tile on the tileset.</param>
 /// <param name="srcY">Y co-ordinate of the tile on the tileset.</param>
 private void SetTile(Tileset tileset, EditorTileLayer layer, int destX, int destY, int srcX, int srcY)
 {
     layer.Tiles[destX, destY].Tileset = tileset.ID;
     layer.Tiles[destX, destY].X = destX;
     layer.Tiles[destX, destY].Y = destY;
     layer.Tiles[destX, destY].SrcX = srcX;
     layer.Tiles[destX, destY].SrcY = srcY;
     layer.Tiles[destX, destY].Terrain = 0;
 }
        public void Fill(Tileset tileset, PointSelection selection, int x, int y)
        {
            if (x < 0 || x > Width - 1 || y < 0 || y > Height - 1) return;

            using (UndoRedoArea.Start("Fill Tool"))
            {
                _tileChecked = new bool[Width, Height];
                _tileMap = SelectedLayer.Tiles;
                _tilesToCache = new List<Point>();

                var tile = selection.GetTopLeftMostPoint();
                var replacementTile = new FillTile(tileset.ID, tile.X, tile.Y, 0);
                var currTile = GetFillTile(x, y, replacementTile);
                var originalFillTile = new FillTile(currTile.Tileset, currTile.SrcX, currTile.SrcY, currTile.Terrain);

                FillTiles(currTile, originalFillTile, replacementTile);

                UndoRedoArea.Commit();
            }
        }
        /// <summary>
        /// Sets the selection of tiles to the map.
        /// </summary>
        /// <param name="tileset">Tileset of the tiles.</param>
        /// <param name="layer">Layer to set the tiles to.</param>
        /// <param name="selection">Selection of tiles to place.</param>
        /// <param name="x">X co-ordinate to place to.</param>
        /// <param name="y">Y co-orindate to place to.</param>
        public void SetTiles(Tileset tileset, EditorTileLayer layer, PointSelection selection, int x, int y)
        {
            foreach (var point in selection.Points)
            {
                var x2 = (x - selection.Offset.X) + point.X;
                var y2 = (y - selection.Offset.Y) + point.Y;

                if (x2 < 0 || y2 < 0 || x2 > Width - 1 || y2 > Height - 1) continue;

                SetTile(tileset, layer, x2, y2, point.X, point.Y);
            }
        }
 public TilesetAddedEventArgs(Tileset tileset)
 {
     _tileset = tileset;
 }
 /// <summary>
 /// Handles the TilesetSelected event of the <see cref="TilesetManager"/> object.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="TilesetChangedEventArgs"/> instance containing the event data.</param>
 private void TilesetSelected(object sender, TilesetSelectedEventArgs e)
 {
     if (e.Tileset == null) return;
     Tileset = e.Tileset;
     TilesetManager.Instance.SetSelectionTileSize(e.Tileset.TileWidth, e.Tileset.TileHeight);
     LoadTexture(e.Tileset.Image);
 }
        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                if (Tileset != null) Tileset = null;
                if (_tileTexture != null) _tileTexture.Dispose();
                if (_tileSprite != null) _tileSprite.Dispose();
                if (_highlightTexture != null) _highlightTexture.Dispose();
                foreach (var sprite in _highlightSprites)
                {
                    if (sprite != null) sprite.Dispose();
                }
                if (_highlightSprites != null) _highlightSprites = null;

                components.Dispose();
            }
            base.Dispose(disposing);
        }
        /// <summary>
        /// Handles the TilesetSelected event of the <see cref="TilesetManager"/> class.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="TilesetSelectedEventArgs"/> instance containing the event data.</param>
        private void TilesetSelected(object sender, TilesetSelectedEventArgs e)
        {
            if (e.Tileset == null) return;

            _tileset = e.Tileset;

            _tilesetTexture = ResourceManager.Instance.LoadTexture(_tileset.Image);
            BuildTilesetSprites();
        }