void btnOk_Click(object sender, EventArgs e) { settings = new WorldSettings() { WorldSize = new Size(worldWidth.Value, worldHeight.Value), TileSize = new Size(tileWidth.Value, tileHeight.Value) }; }
void LoadFile(string location) { // determine if the world is already being edited foreach (FormWorld world in MdiChildren) { if (world.SaveLocation == location) { world.Activate(); return; } } try { using(var stream = File.Open(location, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) using (var reader = XmlReader.Create(stream, null)) { WorldFile worldFile = IntermediateSerializer.Deserialize<WorldFile>(reader, null); WorldSettings settings = new WorldSettings() { WorldSize = new Size(worldFile.Size.X, worldFile.Size.Y), TileSize = new Size(worldFile.TileSize.X, worldFile.TileSize.Y) }; var world = new FormWorld(settings, this); world.SaveLocation = location; world.ClearLayers(); stream.Position = 0; Dictionary<string, string> properties = new Dictionary<string, string>(); // parse properties from xml using (var propertiesReader = XmlReader.Create(stream, new XmlReaderSettings() { IgnoreWhitespace = true })) { while (propertiesReader.Read()) { if (propertiesReader.IsStartElement("Item")) { propertiesReader.ReadStartElement("Item"); propertiesReader.Read(); string key = propertiesReader.ReadString(); propertiesReader.Read(); string value = propertiesReader.ReadString(); propertiesReader.Read(); properties.Add(key, value); } } } world.UserProperties = properties; using (var settingsForm = new FormLoadSettings(worldFile.Tilesets)) { if (settingsForm.ShowDialog() == DialogResult.OK) { RawLayer[] layers = worldFile.Layers; Array.Resize<RawLayer>(ref layers, worldFile.Layers.Length + 1); worldFile.Layers = layers; worldFile.Layers[worldFile.Layers.Length - 1] = new RawLayer() { Name = "Collision Layer", Data = worldFile.CollisionData }; parseLayers(world, worldFile.Tilesets, worldFile.Layers); world.ChangesMade = false; // need to show for the graphics device in the world canvas to be created world.Show(); foreach(var tileset in worldFile.Tilesets) { world.AddTileset(new WorldTileset(tileset.File, world.CanvasGraphicsDevice)); } worldChanged(null, null); } else { world.Close(); } } } } catch (Exception e) { MessageBox.Show("Error while attempting to load " + Path.GetFileName(location) +"\n\n"+ e.Message, "Error Loading File", MessageBoxButtons.OK, MessageBoxIcon.Error); MdiChildren[MdiChildren.Length - 1].Close(); } }
/// <summary> /// Represents a form containing an editable world /// </summary> /// <param name="settings"> World settings </param> public FormWorld(WorldSettings settings, Form parent) { InitializeComponent(); MdiParent = parent; history = new WorldHistory(); this.settings = settings; userProperties = new Dictionary<string, string>(); InitWorld(); scrollBarHorizontal.Scroll += scrollBarScroll; scrollBarVertical.Scroll += scrollBarScroll; worldCanvas = new WorldCanvas(this) { ContextMenuStrip = settingsMenu }; canvasPanel.Controls.Add(worldCanvas); worldCanvas.Dock = DockStyle.Fill; }