Пример #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
        // mở file map có sẵn
        private void miOpen_Click_1(object sender, RoutedEventArgs e)
        {
            if (this._isUnsaved) // nếu trước đó tạo map mà chưa lưu thì hỏi
            {
                MessageBoxResult r = MessageBox.Show("You haven't still saved the map yet. Are you want to save it?",
                                                     "Create New Question", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (r == MessageBoxResult.Yes)
                {
                    this.miSave_Click_1(this, new RoutedEventArgs());
                }
            }

            Stream         stream;
            OpenFileDialog ofd = new OpenFileDialog();

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

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

            if (result == true)
            {
                try
                {
                    string filename = ofd.FileName;
                    stream = File.OpenRead(filename);

                    XmlSerializer ser = new XmlSerializer(typeof(SaveData));
                    this._saveData = (SaveData)ser.Deserialize(stream);

                    stream.Close();

                    // tạo mới map và load các đối tượng trong file map vào canvas
                    this.cvMap.Children.RemoveRange(0, this.cvMap.Children.Count);
                    this.InitializeMap(this._saveData.Width / 2, this._saveData.Height / 2); // khởi tạo mới map
                    this._origin = this._saveData.Height;

                    // Tạo list các đối tượng
                    List <GameObject> objects = new List <GameObject>();
                    bool flag;
                    foreach (var item in this._saveData.Nodes)
                    {
                        if (item.GameObjects.Length > 0)
                        {
                            foreach (var i in item.GameObjects)
                            {
                                flag = false;
                                if (objects.Count > 0)
                                {
                                    foreach (var o in objects)
                                    {
                                        if (o.ID == i.ID)
                                        {
                                            flag = true; // tồn lại đối tượng trong list
                                        }
                                    }
                                }

                                if (flag == false) // chưa tồn tại đối tượng trong list
                                {
                                    objects.Add(i);
                                }
                            }
                        }
                    }


                    if (objects.Count > 0) // add các đối tượng có trong file
                    {
                        foreach (var item in objects)
                        {
                            Image i = new Image();

                            i.Tag = item.Type;

                            int w, h;
                            Factory.ConvertToStandardImage(i, out w, out h);
                            i.Width  = w;
                            i.Height = h;
                            i.MouseLeftButtonDown += i_MouseLeftButtonDown;
                            i.MouseLeftButtonUp   += i_MouseLeftButtonUp;

                            //Canvas.SetLeft(i, item.X - i.Width / 2);
                            Canvas.SetLeft(i, item.X);
                            //Canvas.SetTop(i, (this._origin - (item.Y + i.Height / 2)));
                            Canvas.SetTop(i, item.Y);


                            if ((EObjectName)i.Tag == EObjectName.ITEM_COIN_ACTIVATED)
                            {
                                i.Source = Factory.GetBitmapImage(EObjectName.ITEM_COIN_ACTIVATED);
                            }
                            else
                            {
                                i.Source = Factory.GetBitmapImage((EObjectName)i.Tag);
                            }

                            if ((EObjectName)i.Tag != EObjectName.BRICK_QUESTION_ITEM)
                            {
                                this.cvMap.Children.Add(i);
                            }
                        }
                    }

                    this.swMap.Visibility  = System.Windows.Visibility.Visible;
                    this.gbInfo.Visibility = System.Windows.Visibility.Visible;
                    this._curFilename      = string.Copy(filename);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Пример #3
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);
                }
            }
        }