private void createBtn_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(islandNameTxtBox.Text))
            {
                MessageBox.Show("Invalid island name", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            float x, y;

            if (!float.TryParse(sizeXTxtBox.Text, out x) || !float.TryParse(sizeYTxtBox.Text, out y))
            {
                MessageBox.Show("Invalid island dimensions", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrEmpty(pictureBox1.ImageLocation))
            {
                MessageBox.Show("Invalid image", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            int landscapeMaterialOverride = -1;

            if (!int.TryParse(landscapeMaterialOverrideTxtBox.Text, out landscapeMaterialOverride))
            {
                MessageBox.Show("Invalid landscape material override index", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //Make sure there are no duplicate names
            HashSet <string> names = new HashSet <string>();

            foreach (DataGridViewRow row in spawnerOverridesGrid.Rows)
            {
                if (row.Index == spawnerOverridesGrid.Rows.Count - 1)
                {
                    continue;                                                   //Last row is the new row
                }
                string name = (string)row.Cells[SpawnerName.Name].Value;

                if (names.Contains(name))
                {
                    //Duplicate name
                    MessageBox.Show("Duplicate spawner override names found\nOverride names must be unique", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                names.Add(name);
            }

            foreach (DataGridViewRow row in spawnerOverridesGrid.Rows)
            {
                if (row.Index == spawnerOverridesGrid.Rows.Count - 1)
                {
                    continue;                                                   //Last row is the new row
                }
                string val = (string)row.Cells[SpawnerTemplate.Name].Value;
                if (string.IsNullOrEmpty(val))
                {
                    //invalid template
                    MessageBox.Show(string.Format("Template not selected for {0}", (string)row.Cells[SpawnerName.Name].Value), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            float minTreasureQuality = -1;
            float maxTreasureQuality = -1;

            float.TryParse(minTreasureQualityTxtBox.Text, out minTreasureQuality);
            float.TryParse(maxTreasureQualityTxtBox.Text, out maxTreasureQuality);

            float spSpawnPointX = 0.0f;
            float spSpawnPointY = 0.0f;
            float spSpawnPointZ = 0.0f;

            float.TryParse(singleSpawnPointXTxtBox.Text, out spSpawnPointX);
            float.TryParse(singleSpawnPointYTxtBox.Text, out spSpawnPointY);
            float.TryParse(singleSpawnPointZTxtBox.Text, out spSpawnPointZ);

            int islandPoints = 1;

            int.TryParse(islandPointsTxtBox.Text, out islandPoints);
            string islandRemovedFromMod = null;

            if (editedIsland != null)
            {
                if (islandNameTxtBox.Text != editedIsland.name) //name changed
                {
                    if (mainForm.islands.ContainsKey(islandNameTxtBox.Text))
                    {
                        MessageBox.Show("An island with the same name already exist.", "Error", MessageBoxButtons.OK);
                        return;
                    }

                    if (MessageBox.Show("Renaming islands will result in renaming all placed islands in the opened project.\nNote: The editor will not be able to load projects that contained the old name.\nSave?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
                    {
                        return;
                    }

                    //rename all the instances in the current project
                    if (mainForm.currentProject != null)
                    {
                        foreach (IslandInstanceData instance in mainForm.currentProject.islandInstances)
                        {
                            if (instance.name == editedIsland.name)
                            {
                                instance.name = islandNameTxtBox.Text;
                            }
                        }
                    }

                    mainForm.islands.Remove(editedIsland.name);
                    if (islandNameTxtBox.Text != editedIsland.name)
                    {
                        bIslandNameChanged = true;
                    }
                    editedIsland.name = islandNameTxtBox.Text;
                    mainForm.islands.Add(editedIsland.name, editedIsland);

                    //rename image
                    string originalDirectory = Path.GetDirectoryName(editedIsland.imagePath);
                    string newImgPath        = originalDirectory + "/" + editedIsland.name + "_img.jpg";
                    editedIsland.InvalidateImage();
                    File.Move(editedIsland.imagePath, newImgPath);

                    if (pictureBox1.ImageLocation == editedIsland.imagePath)
                    {
                        pictureBox1.ImageLocation = newImgPath;
                    }

                    editedIsland.imagePath = newImgPath;
                }

                if (editedIsland.modDir != null && editedIsland.modDir != null && modNameTxtBox.Text != editedIsland.modDir)
                {
                    //Mod dir changed
                    if (!string.IsNullOrWhiteSpace(modNameTxtBox.Text))
                    {
                        modNameTxtBox.Text.Trim();

                        string modDir = null;
                        modDir = MainForm.islandModsDir + "/" + modNameTxtBox.Text;

                        if (!Directory.Exists(modDir))
                        {
                            Directory.CreateDirectory(modDir);
                        }
                        if (!Directory.Exists(modDir + MainForm.modImgsDir))
                        {
                            Directory.CreateDirectory(modDir + MainForm.modImgsDir);
                        }

                        editedIsland.InvalidateImage();

                        string newImgPath = modDir + MainForm.modImgsDir + editedIsland.name + "_img.jpg";
                        File.Move(editedIsland.imagePath, newImgPath);

                        islandRemovedFromMod = editedIsland.modDir;
                        editedIsland.modDir  = modNameTxtBox.Text;

                        if (pictureBox1.ImageLocation == editedIsland.imagePath)
                        {
                            pictureBox1.ImageLocation = newImgPath;
                        }

                        editedIsland.imagePath = newImgPath;
                    }
                    else
                    {
                        editedIsland.InvalidateImage();
                        string newImgPath = MainForm.imgsDir + "/" + editedIsland.name + "_img.jpg";
                        if (File.Exists(newImgPath))
                        {
                            File.Delete(newImgPath);
                        }
                        File.Move(editedIsland.imagePath, newImgPath);

                        islandRemovedFromMod = editedIsland.modDir;
                        editedIsland.modDir  = null;

                        if (pictureBox1.ImageLocation == editedIsland.imagePath)
                        {
                            pictureBox1.ImageLocation = newImgPath;
                        }

                        editedIsland.imagePath = newImgPath;
                    }
                }

                editedIsland.x = x;
                editedIsland.y = y;

                if (pictureBox1.ImageLocation != editedIsland.imagePath) //picture changed
                {
                    editedIsland.InvalidateImage();
                    File.Copy(pictureBox1.ImageLocation, editedIsland.imagePath, true);
                }

                editedIsland.landscapeMaterialOverride = landscapeMaterialOverride;

                editedIsland.sublevelNames = new List <string>(sublevelsList.Items.Cast <string>());


                editedIsland.spawnerOverrides = new Dictionary <string, string>();

                foreach (DataGridViewRow row in spawnerOverridesGrid.Rows)
                {
                    if (row.Index == spawnerOverridesGrid.Rows.Count - 1)
                    {
                        continue;                                                   //Last row is the new row
                    }
                    string name     = (string)row.Cells[SpawnerName.Name].Value;
                    string template = (string)row.Cells[SpawnerTemplate.Name].Value;

                    editedIsland.spawnerOverrides.Add(name, template);
                }

                editedIsland.minTreasureQuality = minTreasureQuality;
                editedIsland.maxTreasureQuality = maxTreasureQuality;

                editedIsland.useNpcVolumesForTreasures     = useNpcVolumesForTreasuresChkBox.Checked;
                editedIsland.useLevelBoundsForTreasures    = useLevelBoundsForTreasuresChkBox.Checked;
                editedIsland.prioritizeVolumesForTreasures = prioritizeVolumesForTreasuresChkBox.Checked;
                editedIsland.singleSpawnPointX             = spSpawnPointX;
                editedIsland.singleSpawnPointY             = spSpawnPointY;
                editedIsland.singleSpawnPointZ             = spSpawnPointZ;

                editedIsland.islandTreasureBottleSupplyCrateOverrides = IslandTreasureBottleSupplyCrateOverridesTxtBox.Text;

                editedIsland.islandPoints = islandPoints;
                List <string> NewEntries = new List <string>(extraSublevelsTxtBox.Lines);
                NewEntries.RemoveAll(item => { return(string.IsNullOrWhiteSpace(item)); });
                editedIsland.extraSublevels = NewEntries;

                List <string> NewTreasureMapEntries = new List <string>(TreasureMapSpawnPointsTxtBox.Lines);
                NewTreasureMapEntries.RemoveAll(item => { return(string.IsNullOrWhiteSpace(item)); });
                if (NewTreasureMapEntries.Count == 0)
                {
                    NewTreasureMapEntries = null;
                }
                editedIsland.treasureMapSpawnPoints = NewTreasureMapEntries;

                List <string> NewWildPirateCampEntries = new List <string>(WildPirateCampSpawnPointsTxtBox.Lines);
                NewWildPirateCampEntries.RemoveAll(item => { return(string.IsNullOrWhiteSpace(item)); });
                if (NewWildPirateCampEntries.Count == 0)
                {
                    NewWildPirateCampEntries = null;
                }
                editedIsland.wildPirateCampSpawnPoints = NewWildPirateCampEntries;


                mainForm.SaveIslands(islandRemovedFromMod);
                DialogResult = DialogResult.OK;
                Close();
            }
            else
            {
                string Name = islandNameTxtBox.Text;

                if (mainForm.islands.ContainsKey(Name))
                {
                    MessageBox.Show("Duplicate island name", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                string        ImgLocation   = pictureBox1.ImageLocation;
                List <string> sublevelNames = new List <string>(sublevelsList.Items.Cast <string>());

                List <string> treasureMapSpawnPoints = new List <string>(TreasureMapSpawnPointsTxtBox.Lines);
                treasureMapSpawnPoints.RemoveAll(item => { return(string.IsNullOrWhiteSpace(item)); });
                if (treasureMapSpawnPoints.Count == 0)
                {
                    treasureMapSpawnPoints = null;
                }

                List <string> wildPirateCampSpawnPoints = new List <string>(WildPirateCampSpawnPointsTxtBox.Lines);
                wildPirateCampSpawnPoints.RemoveAll(item => { return(string.IsNullOrWhiteSpace(item)); });
                if (wildPirateCampSpawnPoints.Count == 0)
                {
                    wildPirateCampSpawnPoints = null;
                }

                Dictionary <string, string> spawnerOverrides = new Dictionary <string, string>();

                foreach (DataGridViewRow row in spawnerOverridesGrid.Rows)
                {
                    if (row.Index == spawnerOverridesGrid.Rows.Count - 1)
                    {
                        continue;                                                   //Last row is the new row
                    }
                    string name     = (string)row.Cells[SpawnerName.Name].Value;
                    string template = (string)row.Cells[SpawnerTemplate.Name].Value;

                    spawnerOverrides.Add(name, template);
                }

                string modDir = null;
                if (!string.IsNullOrWhiteSpace(modNameTxtBox.Text))
                {
                    modDir = MainForm.islandModsDir + "/" + modNameTxtBox.Text;
                    if (!Directory.Exists(modDir))
                    {
                        Directory.CreateDirectory(modDir);
                    }
                    if (!Directory.Exists(modDir + MainForm.modImgsDir))
                    {
                        Directory.CreateDirectory(modDir + MainForm.modImgsDir);
                    }
                }

                //Copy the image to our local imgs directory
                string newImgPath = (modDir != null) ? (modDir + MainForm.modImgsDir) : MainForm.imgsDir;
                newImgPath += "/" + Name + "_img.jpg";
                File.Copy(ImgLocation, newImgPath, true);


                mainForm.islands.Add(Name, new Island(Name, x, y, newImgPath, landscapeMaterialOverride, sublevelNames, spawnerOverrides,
                                                      treasureMapSpawnPoints, wildPirateCampSpawnPoints, minTreasureQuality, maxTreasureQuality, useNpcVolumesForTreasuresChkBox.Checked, useLevelBoundsForTreasuresChkBox.Checked,
                                                      prioritizeVolumesForTreasuresChkBox.Checked, IslandTreasureBottleSupplyCrateOverridesTxtBox.Text, new List <string>(extraSublevelsTxtBox.Lines), islandPoints, spSpawnPointX, spSpawnPointY, spSpawnPointZ));

                mainForm.islands.Last().Value.modDir = modNameTxtBox.Text.Trim();

                mainForm.RefreshIslandList();
                mainForm.SaveIslands();

                DialogResult = DialogResult.OK;
                Close();
            }
        }
Ejemplo n.º 2
0
        private void createBtn_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(islandNameTxtBox.Text))
            {
                MessageBox.Show("Invalid island name", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            float x, y;

            if (!float.TryParse(sizeXTxtBox.Text, out x) || !float.TryParse(sizeYTxtBox.Text, out y))
            {
                MessageBox.Show("Invalid island dimensions", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrEmpty(pictureBox1.ImageLocation))
            {
                MessageBox.Show("Invalid image", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            int landscapeMaterialOverride = -1;

            if (!int.TryParse(landscapeMaterialOverrideTxtBox.Text, out landscapeMaterialOverride))
            {
                MessageBox.Show("Invalid landscape material override index", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //Make sure there are no duplicate names
            HashSet <string> names = new HashSet <string>();

            foreach (DataGridViewRow row in spawnerOverridesGrid.Rows)
            {
                if (row.Index == spawnerOverridesGrid.Rows.Count - 1)
                {
                    continue;                                                   //Last row is the new row
                }
                string name = (string)row.Cells[SpawnerName.Name].Value;

                if (names.Contains(name))
                {
                    //Duplicate name
                    MessageBox.Show("Duplicate spawner override names found\nOverride names must be unique", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                names.Add(name);
            }

            foreach (DataGridViewRow row in spawnerOverridesGrid.Rows)
            {
                if (row.Index == spawnerOverridesGrid.Rows.Count - 1)
                {
                    continue;                                                   //Last row is the new row
                }
                string val = (string)row.Cells[SpawnerTemplate.Name].Value;
                if (string.IsNullOrEmpty(val))
                {
                    //invalid template
                    MessageBox.Show(string.Format("Template not selected for {0}", (string)row.Cells[SpawnerName.Name].Value), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            float minTreasureQuality = -1;
            float maxTreasureQuality = -1;

            float.TryParse(minTreasureQualityTxtBox.Text, out minTreasureQuality);
            float.TryParse(maxTreasureQualityTxtBox.Text, out maxTreasureQuality);

            if (editedIsland != null)
            {
                if (islandNameTxtBox.Text != editedIsland.name) //name changed
                {
                    if (mainForm.islands.ContainsKey(islandNameTxtBox.Text))
                    {
                        MessageBox.Show("An island with the same name already exist.", "Error", MessageBoxButtons.OK);
                        return;
                    }

                    if (MessageBox.Show("Renaming islands will result in renaming all placed islands in the opened project.\nNote: The editor will not be able to load projects that contained the old name.\nSave?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
                    {
                        return;
                    }

                    //rename all the instances in the current project
                    if (mainForm.currentProject != null)
                    {
                        foreach (IslandInstanceData instance in mainForm.currentProject.islandInstances)
                        {
                            if (instance.name == editedIsland.name)
                            {
                                instance.name = islandNameTxtBox.Text;
                            }
                        }
                    }

                    mainForm.islands.Remove(editedIsland.name);
                    if (islandNameTxtBox.Text != editedIsland.name)
                    {
                        bIslandNameChanged = true;
                    }
                    editedIsland.name = islandNameTxtBox.Text;
                    mainForm.islands.Add(editedIsland.name, editedIsland);

                    //rename image
                    string newImgPath = MainForm.imgsDir + "/" + editedIsland.name + "_img" + (mainForm.currentProject.exportPngs ? ".png" : ".jpg");
                    editedIsland.InvalidateImage();
                    File.Move(editedIsland.imagePath, newImgPath);

                    if (pictureBox1.ImageLocation == editedIsland.imagePath)
                    {
                        pictureBox1.ImageLocation = newImgPath;
                    }

                    editedIsland.imagePath = newImgPath;
                }

                editedIsland.x = x;
                editedIsland.y = y;

                if (pictureBox1.ImageLocation != editedIsland.imagePath) //picture changed
                {
                    editedIsland.InvalidateImage();
                    File.Copy(pictureBox1.ImageLocation, editedIsland.imagePath, true);
                }

                editedIsland.landscapeMaterialOverride = landscapeMaterialOverride;

                editedIsland.sublevelNames = new List <string>(sublevelsList.Items.Cast <string>());


                editedIsland.spawnerOverrides = new Dictionary <string, string>();

                foreach (DataGridViewRow row in spawnerOverridesGrid.Rows)
                {
                    if (row.Index == spawnerOverridesGrid.Rows.Count - 1)
                    {
                        continue;                                                   //Last row is the new row
                    }
                    string name     = (string)row.Cells[SpawnerName.Name].Value;
                    string template = (string)row.Cells[SpawnerTemplate.Name].Value;

                    editedIsland.spawnerOverrides.Add(name, template);
                }

                editedIsland.minTreasureQuality = minTreasureQuality;
                editedIsland.maxTreasureQuality = maxTreasureQuality;

                editedIsland.useNpcVolumesForTreasures     = useNpcVolumesForTreasuresChkBox.Checked;
                editedIsland.useLevelBoundsForTreasures    = useLevelBoundsForTreasuresChkBox.Checked;
                editedIsland.prioritizeVolumesForTreasures = prioritizeVolumesForTreasuresChkBox.Checked;

                editedIsland.islandTreasureBottleSupplyCrateOverrides = IslandTreasureBottleSupplyCrateOverridesTxtBox.Text;

                List <string> NewEntries = new List <string>(extraSublevelsTxtBox.Lines);
                NewEntries.RemoveAll(item => { return(string.IsNullOrWhiteSpace(item)); });
                editedIsland.extraSublevels = NewEntries;

                mainForm.SaveIslands();
                DialogResult = DialogResult.OK;
                Close();
            }
            else
            {
                string Name = islandNameTxtBox.Text;

                if (mainForm.islands.ContainsKey(Name))
                {
                    MessageBox.Show("Duplicate island name", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                string        ImgLocation   = pictureBox1.ImageLocation;
                List <string> sublevelNames = new List <string>(sublevelsList.Items.Cast <string>());

                Dictionary <string, string> spawnerOverrides = new Dictionary <string, string>();

                foreach (DataGridViewRow row in spawnerOverridesGrid.Rows)
                {
                    if (row.Index == spawnerOverridesGrid.Rows.Count - 1)
                    {
                        continue;                                                   //Last row is the new row
                    }
                    string name     = (string)row.Cells[SpawnerName.Name].Value;
                    string template = (string)row.Cells[SpawnerTemplate.Name].Value;

                    spawnerOverrides.Add(name, template);
                }

                //Copy the image to our local imgs directory
                string newImgPath = MainForm.imgsDir + "/" + Name + "_img" + (mainForm.currentProject.exportPngs ? ".png" : ".jpg");
                File.Copy(ImgLocation, newImgPath, true);


                mainForm.islands.Add(Name, new Island(Name, x, y, newImgPath, landscapeMaterialOverride, sublevelNames, spawnerOverrides,
                                                      minTreasureQuality, maxTreasureQuality, useNpcVolumesForTreasuresChkBox.Checked, useLevelBoundsForTreasuresChkBox.Checked,
                                                      prioritizeVolumesForTreasuresChkBox.Checked, IslandTreasureBottleSupplyCrateOverridesTxtBox.Text, new List <string>(extraSublevelsTxtBox.Lines)));

                mainForm.RefreshIslandList();
                mainForm.SaveIslands();

                DialogResult = DialogResult.OK;
                Close();
            }
        }