/// <summary> /// Form closing. /// </summary> private void ShiftForm_FormClosing(object sender, FormClosingEventArgs e) { // If ok was not clicked from the dialog. if (DialogResult != DialogResult.OK) return; // If All Layers was not selected. if (cmbobx_Layer.SelectedIndex != 0) _layer = (GMareLayer)cmbobx_Layer.SelectedItem; // Set the amount of tiles to move. _amount = (int)nud_Amount.Value; }
/// <summary> /// Ok button click. /// </summary> private void tsb_Ok_Click(object sender, EventArgs e) { // If all the layers are going to be swapped, set to null, else set to desired layer. if (tscb_Layers.SelectedIndex == 0) _layer = null; else _layer = (GMareLayer)tscb_Layers.SelectedItem; // Set tile ids. _target = pnl_Target.Selection; _swap = pnl_Swap.Selection; // If the set target empty button is checked. if (tsb_Empty.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; // Close the form. Close(); }
/// <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) { // Clear any existing text. rtb_Text.Clear(); // Local variables. int index = 0; int roomWidth = ProjectManager.Room.Background.Width; Size roomTileSize = ProjectManager.Room.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].ToString() + ", "); break; case ArrayType.Standard: line.Append(layerId + "[" + col + "," + row + "] = " + layer.Tiles[col, row].ToString() + "; "); break; case ArrayType.List: line.Append("ds_list_add(" + layerId + "," + layer.Tiles[col, row].ToString() + "); "); break; case ArrayType.Grid: line.Append("ds_grid_add(" + layerId + "," + col + "," + row + "," + layer.Tiles[col, row].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] == -1) continue; // Create a new rectangle that represents the source rectangle. Point point = TileGrid.TileIdToPosition(layer.Tiles[col, row], 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] == -1) continue; // Create a new rectangle that represents the source rectangle. Rectangle rect = new Rectangle(); rect.Location = TileGrid.TileIdToPosition(layer.Tiles[col, row], 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. rtb_Text.Text = text.ToString(); }
/// <summary> /// Compile tiles. /// </summary> private void bgw_Compile_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { // Hook into background worker for any cancellations. BackgroundWorker bw = sender as BackgroundWorker; // Get neccessary data. Size tileSize = new Size((int)nud_TileX.Value, (int)nud_TileY.Value); Size seperation = new Size((int)nud_SeperationX.Value, (int)nud_SeperationY.Value); Point offset = new Point((int)nud_OffsetX.Value, (int)nud_OffsetY.Value); Bitmap image = (Bitmap)pnl_Image.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.Tiles2[col, row].TileId = j; break; } } // No match was found. if (match == false) { // New tile id. layer.Tiles2[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; } } // Tilesize. pnl_Tileset.TileSize = tileSize; // Set tiles. pnl_Tileset.Tiles = tiles; // Set room properties. _newRoom.TileWidth = tileSize.Width; _newRoom.TileHeight = tileSize.Height; _newRoom.Columns = cols; _newRoom.Rows = rows; _newRoom.Layers.Clear(); _newRoom.Layers.Add(layer); }
/// <summary> /// Makes a shallow copy of this layer. /// </summary> /// <returns>A shallow copy of this layer.</returns> public GMareLayer Clone() { // Create a new layer. GMareLayer layer = new GMareLayer(_tiles2.GetLength(0), _tiles2.GetLength(1)); // Set data. layer.Name = (string)_name.Clone(); layer.Depth = _depth; // Iterate through tiles. for (int y = 0; y < _tiles2.GetLength(1); y++) for (int x = 0; x < _tiles2.GetLength(0); x++) layer.Tiles2[x, y] = _tiles2[x, y].Clone(); return layer; }
/// <summary> /// Shifts room tiles in a desired direction, by a desired amount, on the desired layer. /// </summary> /// <param name="layer">The layer to shift.</param> /// <param name="direction">The direction to shift the tiles.</param> /// <param name="amount">The amount of tiles to shift.</param> public void Shift(GMareLayer layer, ShiftDirection direction, int amount) { // If the tile swap happens on all layers. if (layer == null) { // Iterate through each layer. foreach (GMareLayer temp in _layers) { // Shift layer. temp.Shift(direction, amount); } } else { // Get the index of the desired layer. int index = _layers.IndexOf(layer); // Shift layer. _layers[index].Shift(direction, amount); } }
/// <summary> /// Converts a rectangle to an array of tile ids. /// </summary> /// <param name="rectangle">The source rectangle to copy tiles from.</param> /// <param name="layer">The layer to copy tiles from.</param> /// <returns>An array fo tile ids.</returns> public GMareTile[] GetTiles(Rectangle rectangle, GMareLayer layer) { // If the layer does not exist, return. if (_layers.Contains(layer) == false) return null; // Create a new list of tiles. List<GMareTile> tiles = new List<GMareTile>(); // Iterate through layer tiles. foreach (GMareTile tile in layer.Tiles2) { // The check rectangle. Rectangle rect = new Rectangle(GMareBrush.TileIdToPosition(tile.TileId, _background.Width, TileSize), TileSize); // Add the included tile. if (rectangle.Contains(rect) == true) tiles.Add(tile.Clone()); } // Return the array of tile ids. return tiles.ToArray(); }
/// <summary> /// Gets the total amount of non-empty tiles. /// </summary> /// <param name="layer">The layer to count.</param> /// <returns>The total amount of non-empty tiles.</returns> public static int GetTileCount(GMareLayer layer) { // Total tile count. int count = 0; // Iterate through columns. for (int col = 0; col < layer.Tiles2.GetLength(0); col++) { // Iterate through rows. for (int row = 0; row < layer.Tiles2.GetLength(1); row++) { // If the tile is not empty, add it to the count. if (layer.Tiles2[col, row].TileId != -1) count++; } } // Return the amount of actual tiles. return count; }
/// <summary> /// Tests the binary methods for the smallest size. /// </summary> /// <param name="layer">The layer to test.</param> /// <returns>The method to be used.</returns> public static BinaryMethod GetBinaryMethod(GMareLayer layer) { // Simple size = 2 (1 short (Tile Id)) // Complex size = 10 (1 short, 2 ints (Tile Id, horizontal position, vertical position)) // Complex method size. int complex = 0; int simple = 0; // Calculate complex method size. complex += GetTileCount(layer) * 10; // Calculate simple method size. simple += layer.Tiles2.Length * 2; // If the simple method size is bigger, return complex method, else return simple method. return simple >= complex ? BinaryMethod.Tile : BinaryMethod.Sector; }