Esempio n. 1
0
        /// <summary>
        /// Prompts the user with a file dialog to select an existing map file.
        /// </summary>
        private void ButtonLoad_Click(object sender, EventArgs e)
        {
            // Declare temporary variables
            OpenFileDialog openFileDialog;

            // Initialize temporary variables
            openFileDialog = new OpenFileDialog();

            // Title the dialog
            openFileDialog.Title = "Open a level file.";

            // Filter files
            openFileDialog.Filter = "Level Files | *.level";

            // Show the dialog & get result
            DialogResult result = openFileDialog.ShowDialog();

            // If they load a file, open the editor form
            if (result == DialogResult.OK)
            {
                // Load in player data
                FileStream   inStream = null;
                StreamReader input    = null;

                try
                {
                    // Open file for reading
                    inStream = File.OpenRead(openFileDialog.FileName);
                    input    = new StreamReader(inStream);

                    // Read first lines for width and height data
                    String   line       = input.ReadLine();
                    String[] mapDetails = line.Split(',');
                    mapHeight = int.Parse(mapDetails[0]);
                    mapWidth  = int.Parse(mapDetails[1]);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error loading in data: {ex.Message}");
                }
                finally
                {
                    if (input != null)
                    {
                        // Close the stream
                        input.Close();
                    }
                    else if (inStream != null)
                    {
                        // File opening may have failed
                        inStream.Close();
                    }
                }

                EditorForm editorForm = new EditorForm(mapHeight, mapWidth, openFileDialog.FileName);

                editorForm.ShowDialog();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Loads the files into the map.
        /// </summary>
        private void MapLoad()
        {
            if (fileName != null)
            {
                // Load in player data
                FileStream   inStream = null;
                StreamReader input    = null;

                try
                {
                    // Open file for reading
                    inStream = File.OpenRead(fileName);
                    input    = new StreamReader(inStream);

                    // Read first lines for width and height data
                    String   line       = input.ReadLine();
                    String[] mapDetails = line.Split(',');
                    int      loadHeight = int.Parse(mapDetails[0]);
                    int      loadWidth  = int.Parse(mapDetails[1]);

                    // Load new canvas if size does not match
                    if (loadHeight != height || loadWidth != width)
                    {
                        // Close stream
                        if (input != null)
                        {
                            // Close the stream
                            input.Close();
                        }
                        else if (inStream != null)
                        {
                            // File opening may have failed
                            inStream.Close();
                        }

                        // Close form
                        this.Close();

                        // Create new form
                        EditorForm editorForm = new EditorForm(loadHeight, loadWidth, fileName);
                        editorForm.Show();
                        return;
                    }

                    // Array for colors
                    List <int> colorData = new List <int>(loadHeight * loadWidth);

                    // Read remaining lines and add to list
                    line = null;
                    while ((line = input.ReadLine()) != null)
                    {
                        colorData.Add(int.Parse(line));
                    }

                    // Paint the loaded file
                    for (int i = 0; i < colorData.Count; i++)
                    {
                        pictureBoxes[i].BackColor = Color.FromArgb(colorData[i]);
                    }

                    this.Text = $"Level Editor - {fileName}";

                    // Alert a successful load
                    MessageBox.Show(
                        "File loaded successfully",
                        "File Loaded",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                }
                catch
                {
                    // Alert an unsuccessful load
                    MessageBox.Show(
                        "Corrupted file!",
                        "Error Loading File",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
                finally
                {
                    if (input != null)
                    {
                        // Close the stream
                        input.Close();
                    }
                    else if (inStream != null)
                    {
                        // File opening may have failed
                        inStream.Close();
                    }
                }
            }
        }
Esempio n. 3
0
        // Methods
        /// <summary>
        /// Creates a new map for the user to draw on.
        /// </summary>
        private void ButtonCreate_Click(object sender, EventArgs e)
        {
            // Declare temporary variables
            string errorHeight;
            string errorWidth;

            // Initialize temporary variables
            errorHeight = null;
            errorWidth  = null;

            // Check height to see if it's an integer
            if (!int.TryParse(textHeight.Text, out mapHeight))
            {
                errorHeight = "- Height was not an integer.";
            }

            // Check width to see if it's an integer
            if (!int.TryParse(textWidth.Text, out mapWidth))
            {
                errorWidth = "- Width was not an integer.";
            }

            // Check height to see if it's within the range
            if (errorHeight == null)
            {
                if (mapHeight < 10)
                {
                    errorHeight = "- Height was too small. Minimum is 10";
                }
                else if (mapHeight > 30)
                {
                    errorHeight = "- Height was too large. Maximum is 30";
                }
            }

            // Check width to see if it's within the range
            if (errorWidth == null)
            {
                if (mapWidth < 10)
                {
                    errorWidth = "- Width was too small. Minimum is 10";
                }
                else if (mapWidth > 30)
                {
                    errorWidth = "- Width was too large. Maximum is 30";
                }
            }

            // If there are errors, present them to the user
            if (errorHeight != null || errorWidth != null)
            {
                MessageBox.Show(
                    $"Errors:\n{errorWidth}\n{errorHeight}",
                    "Error creating map",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);

                return;
            }

            // If there are no errors, create the map
            EditorForm editorForm = new EditorForm(mapHeight, mapWidth);

            editorForm.ShowDialog();
        }