/// <summary>
        /// Initializes a new instance of the <see cref="DockMap"/> form.
        /// </summary>
        /// <param name="map"><see cref="TileMap"/> to edit.</param>
        public DockMap(EditorTileMap map)
        {
            InitializeComponent();

            MapManager.Instance.MapChanged += MapChanged;
            MapManager.Instance.MapDeleted += MapDeleted;

            map.UnsavedChangesChanged += UnsavedChangesChanged;

            FormClosing += DockMap_FormClosing;

            viewMap.SetMap(map);

            this.Name = @"Map" + Map.Name;
            this.Text = Map.Name;
            this.TabText = Map.Name;

            txtName.Text = Map.Name;
            mnuTabSave.Text = @"Save " + Map.Name;

            mnuProperties.Click += mnuProperties_Click;
            mnuSize.Click += mnuSize_Click;
            mnuExport.Click += mnuExport_Click;
            mnuGrid.Click += mnuGrid_Click;

            mnuTab.Opening += mnuTab_Opening;
            mnuTabSave.Click += mnuTabSave_Click;
            mnuTabClose.Click += mnuTabClose_Click;
            mnuTabFolder.Click += mnuTabFolder_Click;
        }
        /// <summary>
        /// Handles the MapSelected event of the <see cref="MapManager"/> class.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="MapSelectedEventArgs"/> instance containing the event data.</param>
        private void MapSelected(object sender, MapSelectedEventArgs e)
        {
            if (e.Map == null)
            {
                _map = null;
                Disable();
                return;
            }

            if (_map == e.Map) return;

            if (_map != null)
            {
                _map.LayerAdded -= LayerAdded;
                _map.LayerChanged -= LayerChanged;
                _map.LayerOrderChanged -= LayerOrderChanged;
                _map.LayerDeleted -= LayerDeleted;
                _map.LayerSelected -= LayerSelected;
                _map.UndoRedoArea.CommandDone -= UndoRedoCommandDone;
            }

            _map = e.Map;

            _map.LayerAdded += LayerAdded;
            _map.LayerChanged += LayerChanged;
            _map.LayerOrderChanged += LayerOrderChanged;
            _map.LayerDeleted += LayerDeleted;
            _map.LayerSelected += LayerSelected;
            _map.UndoRedoArea.CommandDone += UndoRedoCommandDone;

            BuildList();
            Enable();
        }
 public MapAddedEventArgs(EditorTileMap map)
 {
     _map = map;
 }
        /// <summary>
        /// Handles the MapSelected event of the <see cref="MapManager"/> class.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="MapSelectedEventArgs"/> instance containing the event data.</param>
        private void MapSelected(object sender, MapSelectedEventArgs e)
        {
            if (e.Map == null)
            {
                if (_map != null)
                {
                    _map.UndoRedoArea.CommandDone -= UndoRedoCommandDone;
                    _map = null;
                }
                Disable();
                return;
            }

            if (_map == e.Map) return;

            _map = e.Map;
            _map.UndoRedoArea.CommandDone += UndoRedoCommandDone;

            lstHistory.Items.Clear();
            BuildList();

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

                var name = dialog.MapName;
                var width = dialog.MapWidth;
                var height = dialog.MapHeight;
                var tileWidth = dialog.TileWidth;
                var tileHeight = dialog.TileHeight;

                if (CheckMap(name))
                {
                    MessageBox.Show(@"A map with this name already exists.", @"New Map");
                    return;
                }

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

                    if (exit)
                        break;

                    index++;
                }

                var map = new TileMap(index, name, width, height, tileWidth, tileHeight);
                map.Layers.Add(new TileLayer("Default", 100, true));

                var editormap = new EditorTileMap(map);

                Maps.Add(editormap);

                Console.WriteLine(@"Map {0} created.", editormap.Name);

                SaveMap(index);

                if (MapAdded != null)
                    MapAdded.Invoke(this, new MapAddedEventArgs(editormap));

                SelectMap(index);
            }
        }
        /// <summary>
        /// Deserializes <see cref="Maps"/> list from directory.
        /// </summary>
        /// <param name="path">Directory containing the xml files.</param>
        public void LoadMaps(string path)
        {
            Maps.Clear();

            if (!Directory.Exists(path)) return;

            var files = Directory.GetFiles(path, "*.xml", SearchOption.TopDirectoryOnly);

            foreach (var str in files)
            {
                if (!File.Exists(str)) continue;

                var map = TileMap.LoadFromXml(str);
                var editormap = new EditorTileMap(map);

                Maps.Add(editormap);
            }

            if (MapLoaded != null)
                MapLoaded.Invoke(this, new MapLoadedEventArgs(Maps));

            Console.WriteLine(@"All maps loaded.");
        }
        public EditorTileMap Clone()
        {
            using (UndoRedoManager.Start("Cloning map"))
            {
                var newMap = new EditorTileMap
                {
                    ID = ID,
                    Name = Name,
                    Width = Width,
                    Height = Height,
                    TileWidth = TileWidth,
                    TileHeight = TileHeight
                };

                return newMap;
            }
        }
 public MapChangedEventArgs(EditorTileMap oldMap, EditorTileMap newMap)
 {
     _oldMap = oldMap;
     _newMap = newMap;
 }
 public MapSelectedEventArgs(EditorTileMap map)
 {
     _map = map;
 }
        /// <summary>
        /// Sets the <see cref="Map"/> object.
        /// </summary>
        /// <param name="map"><see cref="EditorTileMap"/> object to set the <see cref="Map"/> object to.</param>
        public void SetMap(EditorTileMap map)
        {
            if (Map != null)
            {
                Map.LayerAdded -= LayerAdded;
                Map.LayerChanged -= LayerChanged;
                Map.LayerDeleted -= LayerDeleted;
                Map.LayerOrderChanged -= LayerOrderChanged;
                Map.UndoRedoArea.CommandDone -= UndoRedoCommandDone;
            }

            Map = map;

            if (Map != null)
            {
                ObjectWidth = Map.Width * Map.TileWidth;
                ObjectHeight = Map.Height * Map.TileHeight;
                XOffset = 0;
                YOffset = 0;

                Map.LayerAdded += LayerAdded;
                Map.LayerChanged += LayerChanged;
                Map.LayerDeleted += LayerDeleted;
                Map.LayerOrderChanged += LayerOrderChanged;
                Map.UndoRedoArea.CommandDone += UndoRedoCommandDone;

                //Map.CacheAllTiles();
            }
            else
            {
                ObjectWidth = 0;
                ObjectHeight = 0;
                XOffset = 0;
                YOffset = 0;
            }
        }