示例#1
0
        // save as
        private void miSaveAs_Click_1(object sender, RoutedEventArgs e)
        {
            if (!Management.HasMarioAdded(this.cvMap))
            {
                MessageBox.Show("Mario hasn't still added on map yet. Please add Mario on map!");
                return;
            }

            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter           = "Mario Game Map (*.xml)|*.xml";
            sfd.FilterIndex      = 1;
            sfd.RestoreDirectory = true;

            Nullable <bool> result = sfd.ShowDialog();

            if (result == true)
            {
                try
                {
                    string             filename;
                    Stream             stream;
                    XmlSerializer      ser;
                    List <QGameObject> qGameObjects = new List <QGameObject>();

                    #region DRAW
                    QNode.Mode     = QMode.DRAW;
                    this._saveData = LoadSaveData.CreateSaveData(qGameObjects, this.cvMap, (int)this.cvMap.Width, (int)this.cvMap.Height, QNode.Mode); // tạo data để lưu vào file

                    filename          = sfd.FileName;
                    this._curFilename = filename;
                    if (File.Exists(filename)) // nếu có file thì xóa để tránh lỗi file xảy ra
                    {
                        File.Delete(filename);
                    }

                    stream = File.OpenWrite(filename);
                    ser    = new XmlSerializer(typeof(SaveData));
                    ser.Serialize(stream, _saveData);

                    stream.Close();
                    #endregion

                    #region UPDATE
                    QNode.Mode     = QMode.UPDATE;
                    this._saveData = LoadSaveData.CreateSaveData(qGameObjects, this.cvMap, (int)this.cvMap.Width, (int)this.cvMap.Height, QNode.Mode); // tạo data để lưu vào file

                    filename = sfd.FileName;
                    filename = filename.Insert(filename.Length - 4, "Update");
                    if (File.Exists(filename)) // nếu có file thì xóa để tránh lỗi file xảy ra
                    {
                        File.Delete(filename);
                    }

                    stream = File.OpenWrite(filename);
                    ser    = new XmlSerializer(typeof(SaveData));
                    ser.Serialize(stream, _saveData);

                    stream.Close();
                    #endregion

                    this._isUnsaved = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
示例#2
0
        // lưu map
        private void miSave_Click_1(object sender, RoutedEventArgs e)
        {
            if (this._isUnsaved)
            {
                if (!Management.HasMarioAdded(this.cvMap))
                {
                    MessageBox.Show("Mario hasn't still added on map yet. Please add Mario on map!");
                    return;
                }

                if (!string.IsNullOrEmpty(this._curFilename))
                {
                    try
                    {
                        Stream             stream;
                        string             filename;
                        XmlSerializer      ser;
                        List <QGameObject> qGameObjects = new List <QGameObject>();

                        #region DRAW
                        QNode.Mode     = QMode.DRAW;
                        this._saveData = LoadSaveData.CreateSaveData(qGameObjects, this.cvMap, (int)this.cvMap.Width, (int)this.cvMap.Height, QNode.Mode); // tạo data để lưu vào file

                        filename = this._curFilename;
                        if (File.Exists(filename)) // nếu có file thì xóa để tránh lỗi file xảy ra
                        {
                            File.Delete(filename);
                        }

                        stream = File.OpenWrite(filename);
                        ser    = new XmlSerializer(typeof(SaveData));
                        ser.Serialize(stream, _saveData);

                        stream.Close();
                        #endregion

                        #region UPDATE
                        QNode.Mode     = QMode.UPDATE;
                        this._saveData = LoadSaveData.CreateSaveData(qGameObjects, this.cvMap, (int)this.cvMap.Width, (int)this.cvMap.Height, QNode.Mode); // tạo data để lưu vào file

                        filename = this._curFilename;
                        filename = filename.Insert(filename.Length - 4, "Update");
                        if (File.Exists(filename)) // nếu có file thì xóa để tránh lỗi file xảy ra
                        {
                            File.Delete(filename);
                        }

                        stream = File.OpenWrite(filename);
                        ser    = new XmlSerializer(typeof(SaveData));
                        ser.Serialize(stream, _saveData);

                        stream.Close();
                        #endregion

                        this._isUnsaved = false;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    this.miSaveAs_Click_1(sender, e);
                }
            }
        }