示例#1
0
        public static void ClearTiles(D2DMapEditor d2d, Map map, ref Tile[] tileLibrary, Panel pnlTileLibrary)
        {   // clear all tiles in the tile library
            // delete all controls
            pnlTileLibrary.Controls.Clear();
            pnlTileLibrary.Refresh();

            if (tileLibrary != null)
            {
                Array.Clear(tileLibrary, 0, tileLibrary.Length);
                Array.Resize(ref tileLibrary, 0);
            }

            // initialized map
            foreach (Layer layer in map.Layers)
            {
                for (int x = 0; x < layer.Width; x++)
                {
                    for (int y = 0; y < layer.Height; y++)
                    {
                        layer.LayerData[x, y] = -1;
                    }
                }
            }

            d2d.ClearSelectedTile();
        }
        public static void Brush(D2DMapEditor d2d, Map map, History history, int layerIndex, int tileIndex)
        {
            history.ClearRedo();
            int id = history.UndoNextId;

            while (InputHelper.OldMapPosition.X != InputHelper.MapPosition.X ||
                   InputHelper.OldMapPosition.Y != InputHelper.MapPosition.Y)
            {
                if (map.Layers[layerIndex].LayerData[InputHelper.OldMapPosition.X, InputHelper.OldMapPosition.Y] != tileIndex)
                {
                    history.PushUndo(id, map.Layers[layerIndex].LayerId,
                                      InputHelper.OldMapPosition.X, InputHelper.OldMapPosition.Y,
                                      map.Layers[layerIndex].LayerData[InputHelper.OldMapPosition.X, InputHelper.OldMapPosition.Y],
                                      ActionType.Brush);
                    map.Layers[layerIndex].LayerData[InputHelper.OldMapPosition.X, InputHelper.OldMapPosition.Y] = tileIndex;
                }

                InputHelper.UpdateOldMapPosition();
            }
            if (map.Layers[layerIndex].LayerData[InputHelper.MapPosition.X, InputHelper.MapPosition.Y] != tileIndex)
            {
                history.PushUndo(map.Layers[layerIndex].LayerId,
                                  InputHelper.MapPosition.X, InputHelper.MapPosition.Y,
                                  map.Layers[layerIndex].LayerData[InputHelper.MapPosition.X, InputHelper.MapPosition.Y],
                                  ActionType.Brush);
                map.Layers[layerIndex].LayerData[InputHelper.MapPosition.X, InputHelper.MapPosition.Y] = tileIndex;
            }
        }
示例#3
0
        public static void Brush(D2DMapEditor d2d, Map map, History history, int layerIndex, int tileIndex)
        {
            history.ClearRedo();
            int id = history.UndoNextId;

            while (InputHelper.OldMapPosition.X != InputHelper.MapPosition.X ||
                   InputHelper.OldMapPosition.Y != InputHelper.MapPosition.Y)
            {
                if (map.Layers[layerIndex].LayerData[InputHelper.OldMapPosition.X, InputHelper.OldMapPosition.Y] != tileIndex)
                {
                    history.PushUndo(id, map.Layers[layerIndex].LayerId,
                                     InputHelper.OldMapPosition.X, InputHelper.OldMapPosition.Y,
                                     map.Layers[layerIndex].LayerData[InputHelper.OldMapPosition.X, InputHelper.OldMapPosition.Y],
                                     ActionType.Brush);
                    map.Layers[layerIndex].LayerData[InputHelper.OldMapPosition.X, InputHelper.OldMapPosition.Y] = tileIndex;
                }

                InputHelper.UpdateOldMapPosition();
            }
            if (map.Layers[layerIndex].LayerData[InputHelper.MapPosition.X, InputHelper.MapPosition.Y] != tileIndex)
            {
                history.PushUndo(map.Layers[layerIndex].LayerId,
                                 InputHelper.MapPosition.X, InputHelper.MapPosition.Y,
                                 map.Layers[layerIndex].LayerData[InputHelper.MapPosition.X, InputHelper.MapPosition.Y],
                                 ActionType.Brush);
                map.Layers[layerIndex].LayerData[InputHelper.MapPosition.X, InputHelper.MapPosition.Y] = tileIndex;
            }
        }
 public ImportTilesEditor(D2DMapEditor parentForm)
 {
     InitializeComponent();
     _parent_form = parentForm;
     nudImageWidth.ValueChanged += new EventHandler(nudImageWidth_ValueChanged);
     nudImageHeight.ValueChanged += new EventHandler(nudImageHeight_ValueChanged);
     _image_loaded = false;
 }
 public ImportTilesEditor(D2DMapEditor parentForm)
 {
     InitializeComponent();
     _parent_form = parentForm;
     nudImageWidth.ValueChanged  += new EventHandler(nudImageWidth_ValueChanged);
     nudImageHeight.ValueChanged += new EventHandler(nudImageHeight_ValueChanged);
     _image_loaded = false;
 }
示例#6
0
        public MapSetup(D2DMapEditor parentForm)
        {
            InitializeComponent();
            _parent_form = parentForm;

            tbNewMapName.Text = "Untitled";
            nudNewMapWidth.Value = 10;
            nudNewMapHeight.Value = 10;
            nudNewTileWidth.Value = 32;
            nudNewTileHeight.Value = 32;
        }
示例#7
0
        public static void AddTiles(D2DMapEditor d2d, Map map, ref Tile[] tileLibrary, Panel pnlTileLibrary, string folderName)
        {   // add tiles to tile library
            ArrayList tilesArrayList = new ArrayList();

            Cursor.Current = Cursors.WaitCursor;

            foreach (string f in Directory.GetFiles(folderName))
            {   // load tiles
                if (Path.GetExtension(f) == ".bmp" || Path.GetExtension(f) == ".jpg" || Path.GetExtension(f) == ".gif" || Path.GetExtension(f) == ".png")
                {
                    tilesArrayList.Add(f.ToString());
                }
            }

            // delete all controls
            pnlTileLibrary.Controls.Clear();
            pnlTileLibrary.Refresh();

            int t = 0;

            // resize tileLibrary
            if (tileLibrary != null && tileLibrary.Length > 0)
            {   // add to the library
                t = tileLibrary.Length;
                Array.Resize(ref tileLibrary, tilesArrayList.Count + tileLibrary.Length);
            }
            else
            {   // load new library
                Array.Resize(ref tileLibrary, tilesArrayList.Count);
            }

            foreach (string i in tilesArrayList)
            {   // update tiles library
                Tile       newTile = new Tile();
                PictureBox pB      = new PictureBox();
                pB.Left   = 20;
                pB.Top    = (t * (map.TileHeight + 5)) + 20;
                pB.Width  = map.TileWidth;
                pB.Height = map.TileHeight;
                pB.Name   = t.ToString();
                pB.Load(i);
                newTile.TileID         = t;
                newTile.TileName       = t.ToString();
                newTile.TilePictureBox = pB;
                tileLibrary[t]         = newTile;
                pB.MouseClick         += new MouseEventHandler(d2d.tilePicBox_MouseClick);

                t++;
            }

            RenderTiles(map, ref tileLibrary, pnlTileLibrary);

            Cursor.Current = Cursors.Default;
        }
示例#8
0
        public MapSetup(D2DMapEditor parentForm)
        {
            InitializeComponent();
            _parent_form = parentForm;

            tbNewMapName.Text      = "Untitled";
            nudNewMapWidth.Value   = 10;
            nudNewMapHeight.Value  = 10;
            nudNewTileWidth.Value  = 32;
            nudNewTileHeight.Value = 32;
        }
示例#9
0
        public void DoUndo(Map map, D2DMapEditor d2d)
        {
            int nodeId     = Undo.Peek().Id;
            int layerIndex = -1;

            if (Undo.Peek().Action == ActionType.AddLayer)
            {
                HistoryNode undo = Undo.Pop();
                Redo.Push(new HistoryNode(undo.Id, undo.LayerId, undo.Layer, undo.LayerIndex, undo.Action));
                layerIndex = map.FindLayerIndexWithId(undo.LayerId);
                map.Layers.RemoveAt(undo.LayerIndex);
                d2d.ReloadLayers(-1);
            }
            else if (Undo.Peek().Action == ActionType.DeleteLayer)
            {
                HistoryNode undo = Undo.Pop();
                Redo.Push(new HistoryNode(undo.Id, undo.LayerId, undo.Layer, undo.LayerIndex, undo.Action));
                map.AddLayer(undo.Layer, undo.LayerIndex);
                layerIndex = map.FindLayerIndexWithId(undo.LayerId);
                d2d.ReloadLayers(-1);
            }
            else if (Undo.Peek().Action == ActionType.SwapLayer)
            {
                HistoryNode undo = Undo.Pop();
                Redo.Push(new HistoryNode(undo.Id, undo.LayerIndex, undo.LayerIndex2, undo.Action));
                map.SwapLayers(map, undo.LayerIndex, undo.LayerIndex2);
                layerIndex = undo.LayerIndex;
                d2d.ReloadLayers(-1);
            }
            else
            {
                while (Undo.Count > 0 && nodeId == Undo.Peek().Id)
                {   // undo
                    HistoryNode undo = Undo.Pop();
                    layerIndex = map.FindLayerIndexWithId(undo.LayerId);

                    if (layerIndex >= 0)
                    {
                        // save for redo
                        Redo.Push(new HistoryNode(undo.Id, undo.LayerId, undo.X, undo.Y, map.Layers[layerIndex].LayerData[undo.X, undo.Y], undo.Action));
                        // render undo
                        map.Layers[layerIndex].LayerData[undo.X, undo.Y] = undo.Value;
                    }
                }
            }

            if (layerIndex == -1)
            {
                MessageBox.Show("Cannot undo! Layer was removed", "Undo Failed");
            }
        }
示例#10
0
        public static void DeleteSelectedTile(D2DMapEditor d2d, Map map, ref Tile[] tileLibrary, Panel pnlTileLibrary, int selectedTileID)
        {     // remove selected tile from tile library
            if (tileLibrary != null)
            { // remove that tile
                int i = 0;
                for (i = selectedTileID; i < tileLibrary.Length - 1; i++)
                {
                    tileLibrary[i].TileWidth      = tileLibrary[i + 1].TileWidth;
                    tileLibrary[i].TileHeight     = tileLibrary[i + 1].TileHeight;
                    tileLibrary[i].TilePath       = tileLibrary[i + 1].TilePath;
                    tileLibrary[i].TileWalkable   = tileLibrary[i + 1].TileWalkable;
                    tileLibrary[i].TilePictureBox = tileLibrary[i + 1].TilePictureBox;

                    if (tileLibrary[i + 1].TileName == tileLibrary[i + 1].TileID.ToString())
                    {
                        tileLibrary[i].TileName = tileLibrary[i].TileID.ToString();
                    }
                    else
                    {
                        tileLibrary[i].TileName = tileLibrary[i + 1].TileName;
                    }

                    //tileLibrary[i].TileID = i;
                }
                Array.Clear(tileLibrary, i, 1);
                Array.Resize(ref tileLibrary, tileLibrary.Length - 1);
            }

            // update map
            foreach (Layer layer in map.Layers)
            {
                for (int x = 0; x < layer.Width; x++)
                {
                    for (int y = 0; y < layer.Height; y++)
                    {
                        if (layer.LayerData[x, y] == selectedTileID)
                        {
                            layer.LayerData[x, y] = -1;
                        }
                        else if (layer.LayerData[x, y] > selectedTileID)
                        {
                            layer.LayerData[x, y] = layer.LayerData[x, y] - 1;
                        }
                    }
                }
            }

            RenderTiles(map, ref tileLibrary, pnlTileLibrary);
            d2d.RenderMap();
            d2d.ClearSelectedTile();
        }
示例#11
0
        public void DoRedo(Map map, D2DMapEditor d2d)
        {
            int nodeId = Redo.Peek().Id;
            int layerIndex = -1;

            if (Redo.Peek().Action == ActionType.AddLayer)
            {
                HistoryNode redo = Redo.Pop();
                Undo.Push(new HistoryNode(redo.Id, redo.LayerId, redo.Layer, redo.LayerIndex, redo.Action));
                map.AddLayer(redo.Layer, redo.LayerIndex);
                layerIndex = map.FindLayerIndexWithId(redo.LayerId);
                d2d.ReloadLayers(-1);
            }
            else if (Redo.Peek().Action == ActionType.DeleteLayer)
            {
                HistoryNode redo = Redo.Pop();
                Undo.Push(new HistoryNode(redo.Id, redo.LayerId, redo.Layer, redo.LayerIndex, redo.Action));
                layerIndex = map.FindLayerIndexWithId(redo.LayerId);
                map.Layers.RemoveAt(redo.LayerIndex);
                d2d.ReloadLayers(-1);
            }
            else if (Redo.Peek().Action == ActionType.SwapLayer)
            {
                HistoryNode redo = Redo.Pop();
                Undo.Push(new HistoryNode(redo.Id, redo.LayerIndex, redo.LayerIndex2, redo.Action));
                map.SwapLayers(map, redo.LayerIndex, redo.LayerIndex2);
                layerIndex = redo.LayerIndex;
                d2d.ReloadLayers(-1);
            }
            else
            {
                while (Redo.Count > 0 && nodeId == Redo.Peek().Id)
                {   // redo
                    HistoryNode redo = Redo.Pop();
                    layerIndex = map.FindLayerIndexWithId(redo.LayerId);

                    if (layerIndex >= 0)
                    {
                        // save current map for undo
                        Undo.Push(new HistoryNode(redo.Id, redo.LayerId, redo.X, redo.Y, map.Layers[layerIndex].LayerData[redo.X, redo.Y], redo.Action));
                        // render redo
                        map.Layers[layerIndex].LayerData[redo.X, redo.Y] = redo.Value;
                    }

                }
            }

            if (layerIndex == -1)
                MessageBox.Show("Cannot redo! Layer was removed", "Redo Failed");
        }
示例#12
0
        public static void KeyboardShortCut(D2DMapEditor d2d, Map map, MapInfo mapInfo, History history, int layerIndex, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.S)
            {
                d2d.SelectTool(ToolType.selection);
            }
            else if (e.KeyCode == Keys.B)
            {
                d2d.SelectTool(ToolType.brush);
            }
            else if (e.KeyCode == Keys.E)
            {
                d2d.SelectTool(ToolType.eraser);
            }
            else if (e.KeyCode == Keys.F)
            {
                d2d.SelectTool(ToolType.fill);
            }
            else if (e.KeyCode == Keys.T)
            {
                d2d.SelectTool(ToolType.selectTile);
            }
            else if (e.KeyCode == Keys.Delete)
            {
                if (mapInfo.Selection.StartDrag.X != -1 && mapInfo.Selection.StartDrag.Y != -1 && mapInfo.Selection.StopDrag.X != -1 && mapInfo.Selection.StopDrag.Y != -1)
                {   // selection was made
                    history.ClearRedo();
                    int id = history.UndoNextId;

                    mapInfo.Selection.ForceWithinSelection(map.Layers[layerIndex].Width, map.Layers[layerIndex].Height);
                    for (int i = mapInfo.Selection.TopLeftX; i < mapInfo.Selection.BottomRightX; i++)
                    {   // delete selected tiles
                        for (int j = mapInfo.Selection.TopLeftY; j < mapInfo.Selection.BottomRightY; j++)
                        {
                            history.PushUndo(id, map.Layers[layerIndex].LayerId, i, j, map.Layers[layerIndex].LayerData[i, j], ActionType.Erase);
                            map.Layers[layerIndex].LayerData[i, j] = -1;
                        }
                    }

                    d2d.RenderMap();
                }
            }
        }
示例#13
0
        public static void Selection(D2DMapEditor d2d, Map map, MapInfo mapInfo, int layerIndex, SelectionState state)
        {
            switch (state)
            {
            case SelectionState.StartSelection:
                mapInfo.Selection            = new SelectionTool();
                mapInfo.Selection.IsDragging = true;
                mapInfo.Selection.StartDrag  = new Point(InputHelper.MapPosition.X, InputHelper.MapPosition.Y);
                mapInfo.Selection.StopDrag   = new Point(InputHelper.MapPosition.X, InputHelper.MapPosition.Y);
                break;

            case SelectionState.Dragging:
                if (mapInfo.Selection.IsDragging)
                {
                    mapInfo.Selection.StopDrag = new Point(InputHelper.MapPosition.X, InputHelper.MapPosition.Y);
                    d2d.RenderMap();
                }
                break;

            case SelectionState.EndSelection:
                mapInfo.Selection.IsDragging = false;
                mapInfo.Selection.StopDrag   = new Point(InputHelper.MapPosition.X, InputHelper.MapPosition.Y);
                d2d.RenderMap();
                break;

            case SelectionState.SelectAll:
                mapInfo.Selection           = new SelectionTool();
                mapInfo.Selection.StartDrag = new Point(0, 0);
                mapInfo.Selection.StopDrag  = new Point(map.Layers[layerIndex].Width, map.Layers[layerIndex].Height);
                d2d.RenderMap();
                break;

            case SelectionState.ClearSelection:
                mapInfo.Selection = new SelectionTool();
                break;

            default:
                break;
            }
        }
        public static void DeleteSelectedTile(D2DMapEditor d2d, Map map, ref Tile[] tileLibrary, ListView lvTileLibrary, ImageList ilTileImages, int selectedTileID)
        {
            // remove selected tile from tile library
            if (tileLibrary != null)
            {   // remove that tile
                int i = 0;
                for (i = selectedTileID; i < tileLibrary.Length - 1; i++)
                {
                    tileLibrary[i].TileWidth = tileLibrary[i + 1].TileWidth;
                    tileLibrary[i].TileHeight = tileLibrary[i + 1].TileHeight;
                    tileLibrary[i].TilePath = tileLibrary[i + 1].TilePath;
                    tileLibrary[i].TileWalkable = tileLibrary[i + 1].TileWalkable;
                    tileLibrary[i].TilePictureBox = tileLibrary[i + 1].TilePictureBox;
                    tileLibrary[i].TilePictureBox.Name = tileLibrary[i].TileName;

                    //if (tileLibrary[i + 1].TileName == tileLibrary[i + 1].TileID.ToString())
                    //    tileLibrary[i].TileName = tileLibrary[i].TileID.ToString();
                    //else
                    //    tileLibrary[i].TileName = tileLibrary[i + 1].TileName;

                    //tileLibrary[i].TileID = i;
                }
                Array.Clear(tileLibrary, i, 1);
                Array.Resize(ref tileLibrary, tileLibrary.Length - 1);

                //ilTileImages.Images.RemoveAt(selectedTileID);
                lvTileLibrary.Items.RemoveAt(selectedTileID);
            }

            // update map
            foreach (Layer layer in map.Layers)
            {
                for (int x = 0; x < layer.Width; x++)
                {
                    for (int y = 0; y < layer.Height; y++)
                    {
                        if (layer.LayerData[x, y] == selectedTileID)
                        {
                            layer.LayerData[x, y] = -1;
                        }
                        else if (layer.LayerData[x, y] > selectedTileID)
                        {
                            layer.LayerData[x, y] = layer.LayerData[x, y] - 1;
                        }
                    }
                }
            }

            //RenderTiles(map, ref tileLibrary, lvTileLibrary);
            lvTileLibrary.Refresh();
            d2d.RenderMap();
            d2d.ClearSelectedTile();
        }
示例#15
0
        public static void KeyboardShortCut(D2DMapEditor d2d, Map map, MapInfo mapInfo, History history, int layerIndex, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.S)
            {
                d2d.SelectTool(ToolType.selection);
            }
            else if (e.KeyCode == Keys.B)
            {
                d2d.SelectTool(ToolType.brush);
            }
            else if (e.KeyCode == Keys.E)
            {
                d2d.SelectTool(ToolType.eraser);
            }
            else if (e.KeyCode == Keys.F)
            {
                d2d.SelectTool(ToolType.fill);
            }
            else if (e.KeyCode == Keys.T)
            {
                d2d.SelectTool(ToolType.selectTile);
            }
            else if (e.KeyCode == Keys.Delete)
            {
                if (mapInfo.Selection.StartDrag.X != -1 && mapInfo.Selection.StartDrag.Y != -1 && mapInfo.Selection.StopDrag.X != -1 && mapInfo.Selection.StopDrag.Y != -1)
                {   // selection was made
                    history.ClearRedo();
                    int id = history.UndoNextId;

                    mapInfo.Selection.ForceWithinSelection(map.Layers[layerIndex].Width, map.Layers[layerIndex].Height);
                    for (int i = mapInfo.Selection.TopLeftX; i < mapInfo.Selection.BottomRightX; i++)
                    {   // delete selected tiles
                        for (int j = mapInfo.Selection.TopLeftY; j < mapInfo.Selection.BottomRightY; j++)
                        {
                            history.PushUndo(id, map.Layers[layerIndex].LayerId, i, j, map.Layers[layerIndex].LayerData[i, j], ActionType.Erase);
                            map.Layers[layerIndex].LayerData[i, j] = -1;
                        }
                    }

                    d2d.RenderMap();
                }
            }
        }
示例#16
0
        public static void LoadTiles(D2DMapEditor d2d, Map map, ref Tile[] tileLibrary, Panel pnlTileLibrary, string fileName)
        {   // load tiles from the saved tile library
            // clear tile library
            if (tileLibrary != null)
            {
                Array.Clear(tileLibrary, 0, tileLibrary.Length);
            }

            // load tiles
            ///////////////
            string pbDirName = Path.GetDirectoryName(fileName) + "\\" + Path.GetFileNameWithoutExtension(fileName);

            ArrayList tilesArrayList = new ArrayList();

            // delete all controls
            pnlTileLibrary.Controls.Clear();
            pnlTileLibrary.Refresh();

            if (Directory.Exists(pbDirName))
            {
                foreach (string f in Directory.GetFiles(pbDirName))
                {   // load tiles
                    if (Path.GetExtension(f) == ".bmp" || Path.GetExtension(f) == ".jpg" || Path.GetExtension(f) == ".gif" || Path.GetExtension(f) == ".png")
                    {
                        tilesArrayList.Add(f.ToString());
                    }
                }

                int t = 0;

                // resize tileLibrary
                Array.Resize(ref tileLibrary, tilesArrayList.Count);

                foreach (string i in tilesArrayList)
                {   // update tiles library
                    Tile       newTile = new Tile();
                    PictureBox pB      = new PictureBox();
                    pB.Left   = 20;
                    pB.Top    = (t * (map.TileHeight + 5)) + 20;
                    pB.Width  = map.TileWidth;
                    pB.Height = map.TileHeight;
                    pB.Name   = t.ToString();
                    pB.Load(i);
                    newTile.TileID         = t;
                    newTile.TileName       = t.ToString();
                    newTile.TilePictureBox = pB;
                    tileLibrary[t]         = newTile;
                    pB.MouseClick         += new MouseEventHandler(d2d.tilePicBox_MouseClick);

                    t++;
                }
            }
            else
            {
                MessageBox.Show(pbDirName + " doesn't exist! This folder is needed for the Tiles Library.", "Cannot Find Folder", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


            string      tileLibraryFileName = Path.GetDirectoryName(fileName) + "\\" + Path.GetFileNameWithoutExtension(fileName) + "-tile.xml";
            FileStream  tfs = new FileStream(tileLibraryFileName, FileMode.Open);
            XmlDocument tr  = new XmlDocument();

            tr.Load(tfs);

            XmlNodeList tileList = tr.GetElementsByTagName("Tile");

            foreach (XmlNode node in tileList)
            {
                XmlElement tileElement = (XmlElement)node;

                int tileID = Convert.ToInt32(tileElement.Attributes["ID"].InnerText);

                tileLibrary[tileID].TileName     = tileElement.GetElementsByTagName("Name")[0].InnerText;
                tileLibrary[tileID].TileWidth    = Convert.ToInt32(tileElement.GetElementsByTagName("Width")[0].InnerText);
                tileLibrary[tileID].TileHeight   = Convert.ToInt32(tileElement.GetElementsByTagName("Height")[0].InnerText);
                tileLibrary[tileID].TileWalkable = Convert.ToBoolean(tileElement.GetElementsByTagName("Walkable")[0].InnerText);
            }

            tfs.Close();
        }
示例#17
0
 public static void Selection(D2DMapEditor d2d, Map map, MapInfo mapInfo, int layerIndex, SelectionState state)
 {
     switch (state)
     {
         case SelectionState.StartSelection:
             mapInfo.Selection = new SelectionTool();
             mapInfo.Selection.IsDragging = true;
             mapInfo.Selection.StartDrag = new Point(InputHelper.MapPosition.X, InputHelper.MapPosition.Y);
             mapInfo.Selection.StopDrag = new Point(InputHelper.MapPosition.X, InputHelper.MapPosition.Y);
             break;
         case SelectionState.Dragging:
             if (mapInfo.Selection.IsDragging)
             {
                 mapInfo.Selection.StopDrag = new Point(InputHelper.MapPosition.X, InputHelper.MapPosition.Y);
                 d2d.RenderMap();
             }
             break;
         case SelectionState.EndSelection:
             mapInfo.Selection.IsDragging = false;
             mapInfo.Selection.StopDrag = new Point(InputHelper.MapPosition.X, InputHelper.MapPosition.Y);
             d2d.RenderMap();
             break;
         case SelectionState.SelectAll:
             mapInfo.Selection = new SelectionTool();
             mapInfo.Selection.StartDrag = new Point(0, 0);
             mapInfo.Selection.StopDrag = new Point(map.Layers[layerIndex].Width, map.Layers[layerIndex].Height);
             d2d.RenderMap();
             break;
         case SelectionState.ClearSelection:
             mapInfo.Selection = new SelectionTool();
             break;
         default:
             break;
     }
 }
        public static void ClearTiles(D2DMapEditor d2d, Map map, ref Tile[] tileLibrary, ListView lvTileLibrary, ImageList ilTileImages)
        {
            // clear all tiles in the tile library
            // delete all controls
            lvTileLibrary.Items.Clear();
            //ilTileImages.Images.Clear();

            if (tileLibrary != null)
            {
                Array.Clear(tileLibrary, 0, tileLibrary.Length);
                Array.Resize(ref tileLibrary, 0);
            }

            // initialized map
            foreach (Layer layer in map.Layers)
                for (int x = 0; x < layer.Width; x++)
                    for (int y = 0; y < layer.Height; y++)
                        layer.LayerData[x, y] = -1;

            lvTileLibrary.Refresh();
            d2d.ClearSelectedTile();
        }
        public static void AddTiles(D2DMapEditor d2d, Map map, ref Tile[] tileLibrary, ListView lvTileLibrary, ImageList ilTileImages, string folderName)
        {
            // add tiles to tile library
            ArrayList tilesArrayList = new ArrayList();

            Cursor.Current = Cursors.WaitCursor;

            foreach (string f in Directory.GetFiles(folderName))
            {   // load tiles
                if (Path.GetExtension(f) == ".bmp" || Path.GetExtension(f) == ".jpg" || Path.GetExtension(f) == ".gif" || Path.GetExtension(f) == ".png")
                    tilesArrayList.Add(f.ToString());
            }

            // delete all controls
            //lvTileLibrary.Controls.Clear();
            //lvTileLibrary.Refresh();

            int t = 0;

            // resize tileLibrary
            if (tileLibrary != null && tileLibrary.Length > 0)
            {   // add to the library
                t = tileLibrary.Length;
                Array.Resize(ref tileLibrary, tilesArrayList.Count + tileLibrary.Length);
            }
            else
            {   // load new library
                Array.Resize(ref tileLibrary, tilesArrayList.Count);
            }

            PictureBox pB = null;
            ListViewItem item = null;

            foreach (string i in tilesArrayList)
            {   // update tiles library
                Tile newTile = new Tile();
                pB = new PictureBox();
                pB.Width = map.TileWidth;
                pB.Height = map.TileHeight;
                pB.Name = t.ToString();
                pB.Load(i);
                newTile.TileID = t;
                newTile.TileName = t.ToString();
                newTile.TilePictureBox = pB;
                tileLibrary[t] = newTile;

                ilTileImages.Images.Add(pB.Image);

                item = new ListViewItem();
                item.ImageIndex = t;
                lvTileLibrary.Items.Add(item);

                t++;
            }

            if (pB != null)
            {
                ilTileImages.ImageSize = new Size(pB.Width, pB.Height);
                lvTileLibrary.TileSize = new Size(ilTileImages.ImageSize.Width + TileMargin, ilTileImages.ImageSize.Height + TileMargin);
            }

            lvTileLibrary.LargeImageList = ilTileImages;

            lvTileLibrary.MouseClick += new MouseEventHandler(d2d.tilePicBox_MouseClick);

            lvTileLibrary.Refresh();
            //RenderTiles(map, ref tileLibrary, lvTileLibrary);

            Cursor.Current = Cursors.Default;
        }
        public static void LoadTiles(D2DMapEditor d2d, Map map, ref Tile[] tileLibrary, ListView lvTileLibrary, ImageList ilTileImages, string fileName)
        {
            // load tiles from the saved tile library
            // clear tile library
            if (tileLibrary != null)
                Array.Clear(tileLibrary, 0, tileLibrary.Length);

            // load tiles
            ///////////////
            string pbDirName = Path.GetDirectoryName(fileName) + "\\" + Path.GetFileNameWithoutExtension(fileName);

            ArrayList tilesArrayList = new ArrayList();

            // delete all controls
            lvTileLibrary.Controls.Clear();
            lvTileLibrary.Refresh();

            if (Directory.Exists(pbDirName))
            {
                foreach (string f in Directory.GetFiles(pbDirName))
                {   // load tiles
                    if (Path.GetExtension(f) == ".bmp" || Path.GetExtension(f) == ".jpg" || Path.GetExtension(f) == ".gif" || Path.GetExtension(f) == ".png")
                        tilesArrayList.Add(f.ToString());
                }

                int t = 0;

                // resize tileLibrary
                Array.Resize(ref tileLibrary, tilesArrayList.Count);

                PictureBox pB = null;
                ListViewItem item = null;

                foreach (string i in tilesArrayList)
                {   // update tiles library
                    Tile newTile = new Tile();
                    pB = new PictureBox();
                    pB.Width = map.TileWidth;
                    pB.Height = map.TileHeight;
                    pB.Name = t.ToString();
                    pB.Load(i);
                    newTile.TileID = t;
                    newTile.TileName = t.ToString();
                    newTile.TilePictureBox = pB;
                    tileLibrary[t] = newTile;

                    ilTileImages.Images.Add(pB.Image);

                    item = new ListViewItem();
                    item.ImageIndex = t;
                    lvTileLibrary.Items.Add(item);

                    t++;
                }

                if (pB != null)
                {
                    ilTileImages.ImageSize = new Size(pB.Width, pB.Height);
                    lvTileLibrary.TileSize = new Size(ilTileImages.ImageSize.Width + TileMargin, ilTileImages.ImageSize.Height + TileMargin);
                }

                lvTileLibrary.LargeImageList = ilTileImages;

                lvTileLibrary.MouseClick += new MouseEventHandler(d2d.tilePicBox_MouseClick);

                lvTileLibrary.Refresh();
            }
            else
            {
                MessageBox.Show(pbDirName + " doesn't exist! This folder is needed for the Tiles Library.", "Cannot Find Folder", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            string tileLibraryFileName = Path.GetDirectoryName(fileName) + "\\" + Path.GetFileNameWithoutExtension(fileName) + "-tile.xml";
            FileStream tfs = new FileStream(tileLibraryFileName, FileMode.Open);
            XmlDocument tr = new XmlDocument();
            tr.Load(tfs);

            XmlNodeList tileList = tr.GetElementsByTagName("Tile");

            foreach (XmlNode node in tileList)
            {
                XmlElement tileElement = (XmlElement)node;

                int tileID = Convert.ToInt32(tileElement.Attributes["ID"].InnerText);

                tileLibrary[tileID].TileName = tileElement.GetElementsByTagName("Name")[0].InnerText;
                tileLibrary[tileID].TileWidth = Convert.ToInt32(tileElement.GetElementsByTagName("Width")[0].InnerText);
                tileLibrary[tileID].TileHeight = Convert.ToInt32(tileElement.GetElementsByTagName("Height")[0].InnerText);
                tileLibrary[tileID].TileWalkable = Convert.ToBoolean(tileElement.GetElementsByTagName("Walkable")[0].InnerText);
                tileLibrary[tileID].Type = Convert.ToInt32(tileElement.GetElementsByTagName("Type")[0].InnerText);
            }

            tfs.Close();
        }