// Creates a new mao based the height width and name specified by the user private void CreateNewMap_Click(object sender, EventArgs e) { int mapWidth = 20; int mapHeight = 20; int size = 0; CheckSaveExisiting(); newMapForm = new NewMapForm(); newMapForm.ShowDialog(); mapWidth = newMapForm.width; mapHeight = newMapForm.height; size = newMapForm.size; //Create new window MapEditor newMapEditor = new MapEditor(this); newMapEditor.Text = newMapForm.name; newMapEditor.Show(); newMapEditor.NewMap(mapWidth, mapHeight, size); this.welcomePanel.Visible = false; this.saveMapToolStripMenuItem.Enabled = true; this.importTilesToolStripMenuItem.Enabled = true; this.importSingleTileToolStripMenuItem.Enabled = true; }
// Called on closing current project // clears listeners and checks if user wishes to save the current Map public void CheckSaveExisiting() { //if (newMapForm != null) // foreach (Form form in this.MdiChildren) { Console.WriteLine(form.GetType().Name); if (form.GetType().Name == "MapEditor") { DialogResult result = MessageBox.Show("Would you like to Save your your current Map?", "Save", MessageBoxButtons.YesNo); switch (result) { case DialogResult.Yes: SaveCurrentMap(); break; case DialogResult.No: break; } ; newMapForm = null; MapEditor mapEditor = form as MapEditor; mapEditor.ClearListeners(); } form.Close(); form.Dispose(); } }
// Saves out the cutrrent map to file public void SaveCurrentMap() { Form[] forms = this.MdiChildren; byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine); MapEditor map = forms[0] as MapEditor; SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.FileName = map.Text + ".txt"; string file = ""; if (saveFileDialog.ShowDialog() == DialogResult.OK) { if (saveFileDialog.FileName != "") { try { FileStream fs = (FileStream)saveFileDialog.OpenFile(); byte[] ndata = Encoding.Default.GetBytes(map.ToString()); fs.Write(ndata, 0, ndata.Length); fs.Write(newline, 0, newline.Length); // Save out Spritesheet details for (int i = 0; i < map.sheets.Count; i++) { byte[] bdata = Encoding.Default.GetBytes(map.sheets[i].ToString()); fs.Write(bdata, 0, bdata.Length); fs.Write(newline, 0, newline.Length); } //save out current Palette for (int i = 0; i < map.paletteTiles.Count; i++) { byte[] bdata = Encoding.Default.GetBytes(map.paletteTiles[i].ToString()); fs.Write(bdata, 0, bdata.Length); fs.Write(newline, 0, newline.Length); } // Save out current Map for (int i = 0; i < map.map.Length; i++) { byte[] bdata = Encoding.Default.GetBytes(map.map[i].ToString()); fs.Write(bdata, 0, bdata.Length); fs.Write(newline, 0, newline.Length); } fs.Close(); Console.WriteLine("Successfully saved file!"); } catch (Exception) { DialogResult result = MessageBox.Show("Unable to save file. Would you like to try again?", "Save", MessageBoxButtons.YesNo); switch (result) { case DialogResult.Yes: SaveCurrentMap(); break; case DialogResult.No: break; } ; } } } }
// 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; } ; } } } }