private void SaveAsExecuted(object sender, ExecutedRoutedEventArgs e) { if (GlobalState.IsPlaying) { ExplosionPanel.PlayPauseSequenceEventHandler(null, null); } var dlg = new Microsoft.Win32.SaveFileDialog { FileName = currentFileName, DefaultExt = ".xml", Filter = "Extensible Markup Language File|*.xml" }; if (dlg.ShowDialog() == true) { string filename = dlg.FileName; try { var fileData = new BoundMakerFile { Locations = GlobalState.Locations, Tiles = MapEditor.MapTerrain.GetTiles(), Sequences = GlobalState.Sequences }; XmlHandler.WriteXmlDocument(filename, fileData); currentFileName = dlg.SafeFileName; fullFilePath = dlg.FileName; fileExists = true; GlobalState.HasMadeChanges = false; RefreshTitle(); } catch { MessageBox.Show("Failed to save file. Sorry :("); } } }
private void SaveExecuted(object sender, ExecutedRoutedEventArgs e) { if (fileExists) { if (GlobalState.IsPlaying) { ExplosionPanel.PlayPauseSequenceEventHandler(null, null); } try { var fileData = new BoundMakerFile { Locations = GlobalState.Locations, Tiles = MapEditor.MapTerrain.GetTiles(), Sequences = GlobalState.Sequences }; XmlHandler.WriteXmlDocument(fullFilePath, fileData); fileExists = true; GlobalState.HasMadeChanges = false; RefreshTitle(); } catch { MessageBox.Show("Failed to save file. Try 'Save As'."); } } else { SaveAsExecuted(sender, e); } }
public static BoundMakerFile ReadXmlDocument(string fileLocation) { if (fileLocation == null) { throw new ArgumentNullException(nameof(fileLocation)); } var file = new BoundMakerFile(); var settings = new XmlReaderSettings { IgnoreComments = true, IgnoreWhitespace = true }; using (var reader = XmlReader.Create(fileLocation, settings)) { reader.Read(); if (!reader.Name.Equals("xml", StringComparison.OrdinalIgnoreCase)) { throw new XmlException(); } reader.Read(); if (!reader.Name.Equals("Bound")) { throw new XmlException(); } while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "Locations") { file.Locations = ReadLocations(reader).ToList(); } else if (reader.Name == "Tiles") { file.Tiles = ReadTiles(reader).ToList(); } else if (reader.Name == "Sequences") { // Will error if XML document reads sequences before locations, due to (bad) dependency file.Sequences = ReadSequences(reader, file.Locations).ToList(); } } } } return(file); }
public static void WriteXmlDocument(string fileLocation, BoundMakerFile file) { if (fileLocation == null) { throw new ArgumentNullException(nameof(fileLocation)); } if (file == null) { throw new ArgumentNullException(nameof(file)); } var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true }; using (var writer = XmlWriter.Create(fileLocation, settings)) { writer.WriteStartElement("Bound"); WriteLocations(writer, file.Locations); WriteTiles(writer, file.Tiles); WriteSequences(writer, file.Sequences); writer.WriteEndElement(); } }
private void OpenExecuted(object sender, ExecutedRoutedEventArgs e) { if (GlobalState.IsPlaying) { ExplosionPanel.PlayPauseSequenceEventHandler(null, null); } if (GlobalState.HasMadeChanges && MessageBox.Show($"Do you want save changes to {currentFileName}?", "Confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { if (fileExists) { SaveExecuted(sender, e); } else { SaveAsExecuted(sender, e); } } var dialog = new Microsoft.Win32.OpenFileDialog { DefaultExt = ".xml", Filter = "Extensible Markup Language File|*.xml" }; bool?result = dialog.ShowDialog(); if (result == true) { string filename = dialog.FileName; List <MapLocation> backupLocations = GlobalState.Locations; List <BoundSequence> backupSequences = GlobalState.Sequences; try { BoundMakerFile fileData = XmlHandler.ReadXmlDocument(filename); NewExecuted(null, null); GlobalState.Locations = fileData.Locations.ToList(); GlobalState.Sequences = fileData.Sequences.ToList(); foreach (MapTerrainTile newTile in fileData.Tiles) { MapEditor.MapTerrain.GetCell(Grid.GetRow(newTile), Grid.GetColumn(newTile)).SetTerrain(newTile.Terrain); } foreach (MapLocation m in GlobalState.Locations) { MapEditor.MapCanvas.Children.Add(m); m.SetWindowInstance(this); } LocationPanel.SetLocationNames(); if (GlobalState.Sequences.Count == 0) { GlobalState.Sequences.Add(new BoundSequence()); } ExplosionPanel.CurrentSequence = GlobalState.Sequences[0]; ExplosionPanel.UpdateNavigation(); if (GlobalState.TerrainPanelIsActive) { SetToTerrainMode(null, null); } else if (GlobalState.LocationPanelIsActive) { SetToLocationMode(null, null); } else if (GlobalState.ExplosionPanelIsActive) { SetToExplosionMode(null, null); } currentFileName = dialog.SafeFileName; fullFilePath = dialog.FileName; fileExists = true; GlobalState.HasMadeChanges = false; RefreshTitle(); } catch { GlobalState.Locations = backupLocations; GlobalState.Sequences = backupSequences; MessageBox.Show("Failed to read file. And I tried really hard, too :("); } } e.Handled = true; }