/// <summary> /// Ok button click /// </summary> private void butOk_Click(object sender, EventArgs e) { // If all the layers are going to be swapped, set to null, else set to desired layer _layer = cboLayers.SelectedIndex == 0 ? null : (GMareLayer)cboLayers.SelectedItem; // Set tile ids _target = pnlTarget.TileBrush; _swap = pnlReplacement.TileBrush; // If the set target empty button is checked if (chkEmpty.Checked) { // Set tile grid to empty tiles for (int x = 0; x < _swap.Tiles.GetLength(0); x++) { for (int y = 0; y < _swap.Tiles.GetLength(0); y++) { _swap.Tiles[x, y].TileId = -1; } } } // Set dialog result DialogResult = DialogResult.OK; }
/// <summary> /// Constructs a new add/edit layer form with the desired layer to be edited /// </summary> /// <param name="layer">The layer to edit</param> public AddEditLayerForm(GMareLayer layer) { InitializeComponent(); // Set form text based on action Text = "Edit Layer"; // Set edit layer _layer = layer; // Set the existing depth as a valid depth to use _existingDepth = _layer.Depth; // Update the UI UpdateUI(); }
/// <summary> /// Form closing /// </summary> private void ShiftForm_FormClosing(object sender, FormClosingEventArgs e) { // If the dialog result is not Ok if (DialogResult != DialogResult.OK) { return; } // If All Layers was not selected if (cboLayer.SelectedIndex != 0) { _layer = (GMareLayer)cboLayer.SelectedItem; } // Set the amount of tiles to move _amount = (int)nudAmount.Value; }
/// <summary> /// Constructs a new add/edit layer form /// </summary> public AddEditLayerForm() { InitializeComponent(); // Set form text based on action Text = "Add Layer"; // It's a new layer, so we ignore the existing depth _new = true; // Create layer properties int cols = App.Room.Columns; int rows = App.Room.Rows; int depth = App.Room.GetUniqueDepth(); GMareTile[,] tiles = GMareLayer.GetEmptyLayer(cols, rows); // Create a new layer _layer = new GMareLayer("Layer", depth, tiles); // Update the UI UpdateUI(); }
/// <summary> /// Gets a string representation of a layer /// </summary> /// <param name="layer">The layer to get tile data from</param> /// <param name="dataType">The type of data to present</param> /// <param name="arrayType">The type of array style to use when writing the string</param> private void SetText(GMareLayer layer, DataType dataType, ArrayType arrayType) { // If no layers, display nothing if (layer == null) { return; } // Clear any existing text txtText.Clear(); // Local variables int index = 0; int roomWidth = 0; if (App.Room.Backgrounds[0].Image != null) { roomWidth = App.Room.Backgrounds[0].Image.Width; } Size roomTileSize = App.Room.Backgrounds[0].TileSize; bool tileData = TilesExist(layer.Tiles); // Create new text StringBuilder text = new StringBuilder(); string layerId = "layer" + layer.Depth.ToString(); layerId = layerId.Replace("-", "_"); // Do action based on array type switch (arrayType) { // If the array present type is a list case ArrayType.List: // Do action based on data type switch (dataType) { // Sector data type case DataType.Sectors: // Create array variable string text.AppendLine(layerId + " = ds_list_create();"); text.AppendLine(); break; // All other data types default: // If there is tile data, create array variable string if (tileData) { text.AppendLine(layerId + " = ds_list_create();"); text.AppendLine(); } break; } break; // If the array present type is a grid case ArrayType.Grid: // If the data type is sectors, create a grid variable if (dataType == DataType.Sectors) { text.AppendLine(layerId + " = ds_grid_create();"); text.AppendLine(); } break; } // Calculate columns and rows int rows = layer.Tiles.GetLength(1); int cols = layer.Tiles.GetLength(0); // Iterate through rows for (int row = 0; row < rows; row++) { // Create a new line StringBuilder line = new StringBuilder(); // Iterate through columns for (int col = 0; col < cols; col++) { // Do action based on data type switch (dataType) { // If sector data must be displayed case DataType.Sectors: // Set sector data text based on desired array type switch (arrayType) { case ArrayType.Raw: line.Append((layer.Tiles[col, row].TileId).ToString() + ", "); break; case ArrayType.Standard: line.Append(layerId + "[" + col + "," + row + "] = " + layer.Tiles[col, row].TileId.ToString() + "; "); break; case ArrayType.List: line.Append("ds_list_add(" + layerId + "," + layer.Tiles[col, row].TileId.ToString() + "); "); break; case ArrayType.Grid: line.Append("ds_grid_add(" + layerId + "," + col + "," + row + "," + layer.Tiles[col, row].TileId.ToString() + "); "); break; } break; // If point data must be displayed case DataType.Points: // If the tile id is -1, don't bother with rectangle data if (layer.Tiles[col, row].TileId == -1) { continue; } // Create a new rectangle that represents the source rectangle Point point = GMareBrush.TileIdToSourcePosition(layer.Tiles[col, row].TileId, roomWidth, roomTileSize); // Set tile data text based on desired array type switch (arrayType) { case ArrayType.Raw: text.AppendLine(new Point(col * roomTileSize.Width, row * roomTileSize.Height).ToString() + " // Destination point."); text.AppendLine(point.ToString() + " // Source Point"); break; case ArrayType.Standard: text.AppendLine(layerId + "[" + index + "] = " + col * roomTileSize.Width + "; " + " // Destination X."); index++; text.AppendLine(layerId + "[" + index + "] = " + row * roomTileSize.Height + "; " + " // Destination Y."); index++; text.AppendLine(layerId + "[" + index + "] = " + point.X + "; " + " // Source X."); index++; text.AppendLine(layerId + "[" + index + "] = " + point.Y + "; " + " // Source Y."); index++; break; case ArrayType.List: text.AppendLine("ds_list_add(" + layerId + ", " + col * roomTileSize.Width + "); " + " // Destination X."); text.AppendLine("ds_list_add(" + layerId + ", " + row * roomTileSize.Height + "); " + " // Destination Y."); text.AppendLine("ds_list_add(" + layerId + ", " + point.X + "); " + " // Source X."); text.AppendLine("ds_list_add(" + layerId + ", " + point.Y + "); " + " // Source Y."); break; } break; // If rectangle data must be displayed case DataType.Rectangles: // If the tile id is -1, don't bother with rectangle data if (layer.Tiles[col, row].TileId == -1) { continue; } // Create a new rectangle that represents the source rectangle Rectangle rect = new Rectangle(); rect.Location = GMareBrush.TileIdToSourcePosition(layer.Tiles[col, row].TileId, roomWidth, roomTileSize); rect.Size = roomTileSize; // Set tile data text based on desired array type switch (arrayType) { case ArrayType.Raw: text.AppendLine(new Point(col * roomTileSize.Width, row * roomTileSize.Height).ToString() + " // Destination point."); text.AppendLine(rect.ToString() + " // Source rectangle."); break; case ArrayType.Standard: text.AppendLine(layerId + "[" + index + "] = " + col * roomTileSize.Width + "; " + " // Destination X."); index++; text.AppendLine(layerId + "[" + index + "] = " + row * roomTileSize.Height + "; " + " // Destination Y."); index++; text.AppendLine(layerId + "[" + index + "] = " + rect.X + "; " + " // Source X."); index++; text.AppendLine(layerId + "[" + index + "] = " + rect.Y + "; " + " // Source Y."); index++; text.AppendLine(layerId + "[" + index + "] = " + rect.Width + "; " + " // Tile Width."); index++; text.AppendLine(layerId + "[" + index + "] = " + rect.Height + "; " + " // Tile Height."); index++; break; case ArrayType.List: text.AppendLine("ds_list_add(" + layerId + "," + col * roomTileSize.Width + "); " + " // Destination X."); text.AppendLine("ds_list_add(" + layerId + "," + row * roomTileSize.Height + "); " + " // Destination Y."); text.AppendLine("ds_list_add(" + layerId + "," + rect.X + "); " + " // Source X."); text.AppendLine("ds_list_add(" + layerId + "," + rect.Y + "); " + " // Source Y."); text.AppendLine("ds_list_add(" + layerId + "," + rect.Width + "); " + " // Tile Width."); text.AppendLine("ds_list_add(" + layerId + "," + rect.Height + "); " + " // Tile Height."); break; } break; } } // Append line to text if (dataType == DataType.Sectors) { text.AppendLine(line.ToString()); } } // Set rich text box text txtText.Text = text.ToString(); }
/// <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; }