Exemplo n.º 1
0
        /// <summary>
        /// Loads a project from a support file type
        /// </summary>
        /// <param name="file">The path pointing to a supported file</param>
        public static GMareRoom OpenProject(string file)
        {
            // Get file extension
            string ext = file.Substring(file.LastIndexOf('.')).ToLower();

            // If not a gmpx file
            if (ext != ".gmpx")
            {
                return(null);
            }

            // Create a new room
            GMareRoom room = null;

            // Create a new stream reader, read in standard xml project file
            using (StreamReader sr = new StreamReader(file))
            {
                XmlSerializer writer = new XmlSerializer(typeof(GMareRoom));
                room = (GMareRoom)writer.Deserialize(sr);
            }

            // If the room was not loaded successfully, inform the user
            if (room == null)
            {
                MessageBox.Show("There was a problem loading the project data. The file may be invalid or corrupt.", "GMare", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return(null);
            }

            // Return the opened room
            return(room);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructs a new project form for writing
        /// </summary>
        /// <param name="project">The project to write</param>
        /// <param name="path">The path to write to</param>
        public ProjectIOForm(GMareRoom project, string path)
        {
            // Set text
            this.Text = "Saving...";

            // Set project to write
            _gmareProject = project;

            // Write project
        }
Exemplo n.º 3
0
        /// <summary>
        /// Loads a project from a support file type
        /// </summary>
        /// <param name="file">The path pointing to a supported file</param>
        public static void SetProject(string file)
        {
            // Get file extension
            string ext = file.Substring(file.LastIndexOf('.')).ToLower();

            // Image file
            if (ext == ".png" || ext == ".bmp" || ext == ".gif")
            {
                // Create a new file stream
                using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
                {
                    // Create a new image import form
                    using (ImportImageForm form = new ImportImageForm((Bitmap)Bitmap.FromStream(fs)))
                    {
                        // If Ok was clicked from the dialog, set room
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            Room = form.NewRoom;
                        }

                        return;
                    }
                }
            }

            // If a room already exists, dispose of it
            if (Room != null)
            {
                Room.Dispose();
            }

            // Create a new stream reader, read in standard xml project file
            using (StreamReader sr = new StreamReader(file))
            {
                XmlSerializer reader = new XmlSerializer(typeof(GMareRoom));
                Room = (GMareRoom)reader.Deserialize(sr);
            }

            // If the room was not loaded successfully, inform the user
            if (Room == null)
            {
                MessageBox.Show("There was a problem loading the project data. The file may be invalid or corrupt.", "GMare", MessageBoxButtons.OK,
                                MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }

            // Remove default background
            Room.Backgrounds.RemoveAt(0);

            // Correct values for earlier versions
            CorrectValues(Room);

            // Set edit room and save path, update the UI
            App.SavePath = file;
        }
Exemplo n.º 4
0
        private GMareRoom _import = null;  // Room to import

        #endregion

        #region Constructors

        /// <summary>
        /// Constructs a new import GMare dialog
        /// </summary>
        /// <param name="room">The room to import</param>
        public ImportGMareForm(GMareRoom room)
        {
            InitializeComponent();

            // If no room has been imported, notify the user and return
            if (room == null)
            {
                MessageBox.Show("The imported room was not loaded correctly. Please verify the project file and try again.",
                                "GMare", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                DialogResult = DialogResult.Cancel;
                return;
            }

            // Set the room
            _import = room;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Compiler done event
        /// </summary>
        private void bgwCompile_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // Setup GUI
            SetUI(false);

            // Reset progress bar
            barProgress.Value = 0;

            // Update status information
            SetStatus("Ready");

            // Check for ok button enable
            CheckAccept();

            // Set the room
            _newRoom = e.Result as GMareRoom;
        }
Exemplo n.º 6
0
        /// <summary>
        /// On load
        /// </summary>
        protected override void OnCreateControl()
        {
            // Call base load
            base.OnCreateControl();

            // If game maker loading, return
            if (_gameMaker)
            {
                return;
            }

            // Read the room in
            var reader = new ReadProgressStream(new FileStream(_path, FileMode.Open));

            _gmareProject = ContainerStream.Deserialize <GMareRoom>(reader, reader_ProgressChanged);

            // Close the form
            tmrClose.Interval = 500;
            tmrClose.Start();
        }
Exemplo n.º 7
0
        /// <summary>
        /// Corrects previous version values if necessary
        /// </summary>
        public static void CorrectValues(GMareRoom room)
        {
            // If the room is empty return
            if (room == null)
            {
                return;
            }

            // Correct values for versions under 1.0.0.14
            if (VersionCompare(room.Version, "1.0.0.14") == VersionResultType.Less)
            {
                // Set instance color blend to white
                foreach (GMareInstance instance in room.Instances)
                {
                    instance.UBlendColor = 4294967295;
                }

                // Set width and height based on old column and row values
                room.Width  = room.Backgrounds.Count == 0 ? 16 : room.Columns * room.Backgrounds[0].TileWidth;
                room.Height = room.Backgrounds.Count == 0 ? 16 : room.Rows * room.Backgrounds[0].TileHeight;
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Constructs a new import image form
        /// </summary>
        /// <param name="image">The image used as a room</param>
        public ImportImageForm(Bitmap image)
        {
            InitializeComponent();

            // Create a new room
            _newRoom      = new GMareRoom();
            _newRoom.Name = "New Room";

            // Set target room image
            pnlImage.BackgroundImage   = image;
            pnlImage.AutoScrollMinSize = image.Size;

            // Disable cancel tile compile button
            butCompileCancel.Enabled = false;
            butUndo.Enabled          = false;
            butRedo.Enabled          = false;
            txtName.Text             = _newRoom.Name;

            // Create new undo/redo history
            _history = new UndoRedoHistory <ITilesOwner>(this, 20);

            // Check for ok button enable
            CheckAccept();
        }
Exemplo n.º 9
0
        /// <summary>
        /// Writes a GMare binary file
        /// </summary>
        public void WriteBinaryFile(string path)
        {
            // Create a new file stream
            using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                // Create a binary reader
                BinaryWriter writer = new BinaryWriter(stream);

                // Write file id
                writer.Write(new char[4] {
                    'G', 'B', 'I', 'N'
                });
                writer.Write((short)GetRoomCount());

                // Write rooms
                foreach (ExportProject project in lstRooms.CheckedItems)
                {
                    // If not exporting the project, continue
                    if (!project.Exporting)
                    {
                        continue;
                    }

                    // Get the room
                    GMareRoom room = project.Native ? App.Room : App.GetProject(project.RoomPath);

                    // Write room name and size of room data
                    writer.Write((byte)project.Name.Length);
                    writer.Write((char[])project.Name.ToCharArray());

                    // Get the complete byte size of the room
                    int size = room.GetByteCount(project.Background, project.WriteTiles, project.UseFlipValues,
                                                 project.UseBlendColor, project.WriteInstances, true);

                    writer.Write((int)size);

                    // Write room data
                    writer.Write((bool)project.WriteTiles);
                    writer.Write((bool)project.UseFlipValues);
                    writer.Write((bool)project.UseBlendColor);
                    writer.Write((bool)project.WriteInstances);

                    // If writing tiles to file and there's a background
                    if (project.WriteTiles)
                    {
                        // Calculate tile columns
                        int width          = project.Background.Image.Width;
                        int offsetX        = project.Background.OffsetX;
                        int tileWidth      = project.Background.TileWidth;
                        int separationX    = project.Background.SeparationX;
                        int backgroundCols = project.Background.GetGridSize().Width;

                        // Write room data
                        writer.Write((int)room.Layers.Count);
                        writer.Write((short)room.Columns);
                        writer.Write((short)room.Rows);
                        writer.Write((byte)project.Background.OffsetX);
                        writer.Write((byte)project.Background.OffsetY);
                        writer.Write((byte)project.Background.SeparationX);
                        writer.Write((byte)project.Background.SeparationY);
                        writer.Write((byte)project.Background.TileWidth);
                        writer.Write((byte)project.Background.TileHeight);
                        writer.Write((int)backgroundCols);
                        writer.Write((int)project.Background.GameMakerId);
                        writer.Write((int)project.Background.Name.Length);
                        writer.Write((char[])project.Background.Name.ToCharArray());

                        // Write layers
                        foreach (GMareLayer layer in room.Layers.OrderByDescending(l => l.Depth))
                        {
                            // Get binary tiles, then determine the best method
                            int              tiledSize    = 0;
                            int              standardSize = 0;
                            ExportTile[]     tiles        = layer.GetExportTiles(project.Background, true, -1);
                            BinaryMethodType method       = room.GetTileMethodType(layer, tiles, project.Background,
                                                                                   project.UseFlipValues, project.UseBlendColor, out tiledSize, out standardSize);

                            // Write depth of the layer and binary method used
                            writer.Write((int)layer.Depth);
                            writer.Write((byte)method);

                            // Write data based on tile method
                            switch (method)
                            {
                            // Tiled data method
                            case BinaryMethodType.Tiled:

                                // Write tile data
                                for (int row = 0; row < layer.Tiles.GetLength(1); row++)
                                {
                                    for (int col = 0; col < layer.Tiles.GetLength(0); col++)
                                    {
                                        // Write tile id
                                        writer.Write((int)layer.Tiles[col, row].TileId);

                                        // If the tile is empty, no need to put other data in
                                        if (layer.Tiles[col, row].TileId == -1)
                                        {
                                            continue;
                                        }

                                        // If flipping data is used
                                        if (project.UseFlipValues)
                                        {
                                            writer.Write((byte)layer.Tiles[col, row].FlipMode);
                                        }

                                        // If blend color data is used
                                        if (project.UseBlendColor)
                                        {
                                            writer.Write((int)GameMaker.Common.GMUtilities.ColorToGMColor(layer.Tiles[col, row].Blend));
                                        }
                                    }
                                }

                                break;

                            // GM Tile method
                            case BinaryMethodType.Standard:

                                // Write the number of tiles
                                writer.Write((int)tiles.Length);

                                // Iterate through tiles
                                foreach (ExportTile tile in tiles)
                                {
                                    writer.Write((int)tile.Rect.X);
                                    writer.Write((int)tile.Rect.Y);
                                    writer.Write((int)tile.Location.X);
                                    writer.Write((int)tile.Location.Y);
                                    writer.Write((int)tile.Rect.Width);
                                    writer.Write((int)tile.Rect.Height);

                                    // If using flip data
                                    if (project.UseFlipValues)
                                    {
                                        writer.Write((byte)tile.FlipMode);
                                    }

                                    // If using color data
                                    if (project.UseBlendColor)
                                    {
                                        writer.Write((int)GameMaker.Common.GMUtilities.ColorToGMColor(tile.BlendColor));
                                    }
                                }

                                break;
                            }
                        }
                    }

                    // If instances should be written to the file
                    if (project.WriteInstances)
                    {
                        // Write the number of instances
                        writer.Write((int)room.Instances.Count);

                        // Iterate through instances
                        foreach (GMInstance instance in room.Instances)
                        {
                            // Write instance data
                            writer.Write((int)instance.ObjectName.Length);
                            writer.Write((char[])instance.ObjectName.ToCharArray());
                            writer.Write((int)instance.ObjectId);
                            writer.Write((int)instance.X);
                            writer.Write((int)instance.Y);
                            writer.Write((int)instance.CreationCode.Length);
                            writer.Write((char[])instance.CreationCode.ToCharArray());
                        }
                    }
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Add rooms button click
        /// </summary>
        private void butRoomAdd_Click(object sender, EventArgs e)
        {
            // Create a new open file dialog
            using (OpenFileDialog form = new OpenFileDialog())
            {
                // Set file format filter
                form.Title       = "Add Project(s)";
                form.Filter      = "GMare Project Files (.gmpx)|*.gmpx;";
                form.Multiselect = true;

                // If ok was not clicked, return
                if (form.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                // Iterate through file names, try adding a project for each path
                foreach (string path in form.FileNames)
                {
                    // If the path is the same as this project's path, continue
                    if (path == App.SavePath)
                    {
                        continue;
                    }

                    // Get the room data
                    GMareRoom room = App.GetProject(path);

                    // If the room is empty, continue
                    if (room == null)
                    {
                        continue;
                    }

                    // Export project to add
                    ExportProject project = null;

                    // If the native export room is not present, create a new project else clone the existing one
                    if (room.Projects.Find(p => p.Native) == null)
                    {
                        project            = new ExportProject();
                        project.Name       = room.Name;
                        project.RoomPath   = path;
                        project.Background = App.Room.Backgrounds[0].Clone();
                    }
                    else
                    {
                        project = room.Projects.Find(p => p.Native).Clone();
                    }

                    // The project is not native to this project
                    project.Native     = false;
                    project.Background = room.Backgrounds[0];

                    // Add the room to the project list
                    App.Room.Projects.Add(project);
                }

                // Update project list
                UpdateProjectList((lstRooms.Items.Count - 1) + form.FileNames.Length);

                // Export projects have changed
                _changed = _allowChange;
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Compile tiles
        /// </summary>
        private void bgwCompile_DoWork(object sender, DoWorkEventArgs e)
        {
            // Hook into background worker for any cancellations
            BackgroundWorker bw = sender as BackgroundWorker;

            // Get neccessary data
            GMareRoom room       = new GMareRoom();
            Size      tileSize   = new Size((int)nudTileX.Value, (int)nudTileY.Value);
            Size      seperation = new Size((int)nudSeperationX.Value, (int)nudSeperationY.Value);
            Point     offset     = new Point((int)nudOffsetX.Value, (int)nudOffsetY.Value);
            Bitmap    image      = (Bitmap)pnlImage.BackgroundImage;

            // Calculate the number of columns and rows
            int cols = (int)Math.Ceiling((double)(image.Width - offset.X) / (double)(tileSize.Width + seperation.Width));
            int rows = (int)Math.Ceiling((double)(image.Height - offset.Y) / (double)(tileSize.Height + seperation.Height));

            // Set looping variables
            bool match = false;
            int  i     = 0;
            int  total = cols * rows;

            // Byte array collection for unique tiles
            List <byte[]> imageData = new List <byte[]>();

            // A list of accepted unique tile bitmaps
            List <Bitmap> tiles = new List <Bitmap>();

            // Create an empty layer
            GMareLayer layer = new GMareLayer(cols, rows);

            // Copy rectangle
            Rectangle rect = new Rectangle(0, 0, tileSize.Width, tileSize.Height);

            // Iterate through image tile rows
            for (int row = 0; row < rows; row++)
            {
                // Iterate through image tile columns
                for (int col = 0; col < cols; col++)
                {
                    // Set copy rectangle position
                    rect.X = (col * tileSize.Width + (col * seperation.Width)) + offset.X;
                    rect.Y = (row * tileSize.Height + (row * seperation.Height)) + offset.Y;

                    // Copy a section of the image pixel data fast
                    byte[] compare = GetTileBytes(image, rect);

                    // If the compare is empty, skip over validation process
                    if (compare == null)
                    {
                        continue;
                    }

                    // Set match variable
                    match = false;

                    // Iterate through existing unique tiles for a match
                    for (int j = 0; j < imageData.Count; j++)
                    {
                        // If the compare is equal to the tile
                        if (CompareTiles(compare, imageData[j]) == true)
                        {
                            // Match is true
                            match = true;

                            // Set tile id, which is the same as the tile's index at this point
                            layer.Tiles[col, row].TileId = j;
                            break;
                        }
                    }

                    // No match was found
                    if (match == false)
                    {
                        // New tile id
                        layer.Tiles[col, row].TileId = imageData.Count;

                        // Add tile to unique tile list
                        imageData.Add(compare);

                        try
                        {
                            // Create a tile bitmap, and remember it's original id
                            Bitmap tile = GMare.Graphics.PixelMap.PixelDataToBitmap(GMare.Graphics.PixelMap.GetPixels(image, rect));
                            tile.Tag = imageData.Count - 1;

                            // Add bitmap to tiles
                            tiles.Add(tile);
                        }
                        catch (Exception error)
                        {
                            MessageBox.Show("When compiling the tiles, the image's color depth of: " + error.Message + ", is not supported.", "GMare", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }

                    // Increment progress
                    i++;

                    // Report progress
                    bw.ReportProgress(i * 100 / total);
                }

                // If cancelling, return
                if (bw.CancellationPending == true)
                {
                    // Clear tile data
                    tiles.Clear();
                    tiles = null;

                    // Cancel processing
                    e.Cancel = true;
                    return;
                }
            }

            // Populate tile editor
            pnlTileset.SnapSize = tileSize;
            pnlTileset.Tiles    = tiles;

            // Set room properties
            room.Backgrounds[0].TileWidth  = tileSize.Width;
            room.Backgrounds[0].TileHeight = tileSize.Height;
            room.Width  = cols * tileSize.Width;
            room.Height = rows * tileSize.Width;
            room.Layers.Clear();
            room.Layers.Add(layer);
            e.Result = room;
        }
Exemplo n.º 12
0
 /// <summary>
 /// Constructs a new room data object
 /// </summary>
 /// <param name="room">The room object</param>
 /// <param name="layer">The selected edit item</param>
 /// <param name="instance">The selected instance</param>
 /// <param name="shape">The selected shape</param>
 public RoomData(GMareRoom room)
 {
     // Set data
     _room = room;
 }