Example #1
0
        // loads an existing map
        public void LoadExistingMap()
        {
            CheckSaveExisiting();

            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "txt files (*.txt)|*.txt";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog.FileName != "")
                {
                    try
                    {
                        string     data;
                        FileStream fsSource = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read);
                        using (StreamReader sr = new StreamReader(fsSource))
                        {
                            //Create new window
                            MapEditor newMapEditor = new MapEditor(this);

                            int    height = 0;
                            int    width  = 0;
                            string name   = "";
                            int    size   = 0;
                            while ((data = sr.ReadLine()) != null)
                            {
                                string[] strList = data.Split(',');
                                switch (strList[0])
                                {
                                case ("N"):
                                    width  = int.Parse(strList[1]);
                                    height = int.Parse(strList[2]);
                                    name   = strList[3] + "";
                                    size   = int.Parse(strList[4]);
                                    newMapEditor.NewMap(width, height, size);
                                    newMapEditor.Text = name;
                                    break;

                                case ("F"):
                                    string fileTileMap = strList[2];
                                    if (!File.Exists(fileTileMap))
                                    {
                                        DialogResult result = MessageBox.Show("Unable to find file " + fileTileMap + ".  Would you like to find it?",
                                                                              "Find File", MessageBoxButtons.YesNo);
                                        switch (result)
                                        {
                                        case DialogResult.Yes:
                                            openFileDialog.FileName = "";
                                            openFileDialog.Filter   = "Image Files|*.jpg;*.jpeg;*.png;*.gif;*.tif;";
                                            if (openFileDialog.ShowDialog() == DialogResult.OK)
                                            {
                                                if (openFileDialog.FileName != "")
                                                {
                                                    fileTileMap = openFileDialog.FileName;
                                                    break;
                                                }
                                            }
                                            MessageBox.Show("Unable to locate file. Map unable to be loaded.");
                                            break;

                                        case DialogResult.No:
                                            break;
                                        }
                                        ;
                                    }
                                    SpriteSheet sheet = new SpriteSheet(fileTileMap);
                                    sheet.UniqueID   = int.Parse(strList[1]);
                                    sheet.GridHeight = int.Parse(strList[3]);
                                    sheet.GridWidth  = int.Parse(strList[4]);
                                    sheet.TilesHigh  = int.Parse(strList[5]);
                                    sheet.TilesWide  = int.Parse(strList[6]);
                                    sheet.Spacing    = int.Parse(strList[7]);
                                    newMapEditor.sheets.Add(sheet);
                                    FillCurrentPallette();
                                    break;

                                case ("T"):
                                    break;

                                case ("s"):
                                    break;

                                case ("M"):
                                    int tileHeight = int.Parse(strList[1]);
                                    int tileWidth  = int.Parse(strList[2]);
                                    int uniqueID   = int.Parse(strList[3]);
                                    int palleteID  = int.Parse(strList[4]);

                                    if (OnEditMapTile != null)
                                    {
                                        OnEditMapTile(tileHeight, tileWidth, uniqueID, palleteID);
                                    }

                                    break;

                                default:
                                    break;
                                }
                            }
                            newMapEditor.Show();

                            this.welcomePanel.Visible                      = false;
                            this.saveMapToolStripMenuItem.Enabled          = true;
                            this.importTilesToolStripMenuItem.Enabled      = true;
                            this.importSingleTileToolStripMenuItem.Enabled = true;
                            sr.Close();
                            fsSource.Close();
                        }
                    }
                    catch (Exception)
                    {
                        DialogResult result = MessageBox.Show("Unable to load file. Would you like to try again?",
                                                              "Save", MessageBoxButtons.YesNo);
                        switch (result)
                        {
                        case DialogResult.Yes:
                            LoadExistingMap();
                            break;

                        case DialogResult.No:
                            if (newMapForm != null)
                            {
                                foreach (Form form in this.MdiChildren)
                                {
                                    newMapForm = null;
                                    form.Close();
                                    form.Dispose();
                                }
                            }
                            break;
                        }
                        ;
                    }
                }
            }
        }