From 6b36f46f138e01470f2eb013a2052bd4d8ef1bca Mon Sep 17 00:00:00 2001
From: Louis Capitanchik <contact@louiscap.co>
Date: Sun, 22 Oct 2023 23:28:24 +0100
Subject: [PATCH] Support non-square tiles in split command

---
 CHANGELOG.md          |  6 +++++-
 Cargo.lock            |  2 +-
 Cargo.toml            |  2 +-
 src/cli_args.rs       |  2 +-
 src/commands/split.rs | 35 ++++++++++++++++++++++++++---------
 5 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 00207cd..167d170 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,7 +4,11 @@ All notable changes to this project will be documented in this file.
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
-## [Unreleased]
+## [0.8.0]
+### Changed
+- Support non-square tiles in `split` command with `-w` and `-h` flags
+
+## [0.7.0]
 ### Added
 - Support for `--extrude` param in `extrude` command
 
diff --git a/Cargo.lock b/Cargo.lock
index 2601312..f687bcf 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -233,7 +233,7 @@ dependencies = [
 
 [[package]]
 name = "crunch-cli"
-version = "0.7.0"
+version = "0.8.0"
 dependencies = [
  "anyhow",
  "clap",
diff --git a/Cargo.toml b/Cargo.toml
index 2d9f63a..e883f02 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "crunch-cli"
-version = "0.7.1"
+version = "0.8.0"
 edition = "2021"
 
 homepage = "https://microhacks.lcr.app/crunch/"
diff --git a/src/cli_args.rs b/src/cli_args.rs
index f50c3b3..ba1cc53 100644
--- a/src/cli_args.rs
+++ b/src/cli_args.rs
@@ -13,7 +13,7 @@ use crate::load_image;
 #[derive(Parser, Debug, Clone, Serialize, Deserialize)]
 #[clap(name = "crunch")]
 #[clap(author = "Louis Capitanchik <louis@microhacks.co.uk>")]
-#[clap(version = "0.7.0")]
+#[clap(version = "0.8.0")]
 #[clap(about, long_about = None)]
 #[serde(tag = "command", content = "params")]
 pub enum Args {
diff --git a/src/commands/split.rs b/src/commands/split.rs
index d07ea8a..d491bd8 100644
--- a/src/commands/split.rs
+++ b/src/commands/split.rs
@@ -11,7 +11,7 @@ fn tile_size() -> u32 {
 
 /// Take a spritesheet and split into individual sprites, skipping empty space
 #[derive(Parser, Serialize, Deserialize, Clone, Debug)]
-#[clap(author, version = "0.5.0")]
+#[clap(author, version = "0.8.0")]
 pub struct Split {
 	/// The path to the spritesheet file
 	#[serde(default)]
@@ -33,9 +33,15 @@ pub struct Split {
 	#[serde(default)]
 	padding: u32,
 	/// The size of each tile in the spritesheet. Assumed to be square tiles
-	#[clap(short, long, default_value_t = 32)]
-	#[serde(default = "tile_size")]
-	tile_size: u32,
+	#[clap(short, long)]
+	#[serde(default)]
+	tile_size: Option<u32>,
+	#[clap(short = 'w', long)]
+	#[serde(default)]
+	tile_width: Option<u32>,
+	#[clap(short = 'h', long)]
+	#[serde(default)]
+	tile_height: Option<u32>,
 	/// When set, files will be numbered consecutively, regardless of empty sprites. By default, files
 	/// will be numbered based on their inferred "tile id", based on position in the sheet
 	#[clap(short, long, default_value_t = false)]
@@ -66,8 +72,19 @@ impl Split {
 			image.height()
 		);
 
-		let columns = params_to_columns(image.width(), self.tile_size, self.padding, self.space_x);
-		let rows = params_to_columns(image.height(), self.tile_size, self.padding, self.space_y);
+		let tile_width = self.tile_width.or(self.tile_size).ok_or_else(|| {
+			anyhow::Error::msg(
+				"Must provide either '--tile-size' or '--tile-x' flags to specify tile width",
+			)
+		})?;
+		let tile_height = self.tile_height.or(self.tile_size).ok_or_else(|| {
+			anyhow::Error::msg(
+				"Must provide either '--tile-size' or '--tile-y' flags to specify tile height",
+			)
+		})?;
+
+		let columns = params_to_columns(image.width(), tile_width, self.padding, self.space_x);
+		let rows = params_to_columns(image.height(), tile_height, self.padding, self.space_y);
 		log::info!("Inferred sheet contains {} columns", columns);
 		log::info!("Inferred sheet contains {} rows", rows);
 
@@ -77,11 +94,11 @@ impl Split {
 		let mut file_count = 0;
 		for x in 0..columns {
 			for y in 0..rows {
-				let img_x = self.padding + (x * self.tile_size) + (x * self.space_x);
-				let img_y = self.padding + (y * self.tile_size) + (y * self.space_y);
+				let img_x = self.padding + (x * tile_width) + (x * self.space_x);
+				let img_y = self.padding + (y * tile_height) + (y * self.space_y);
 				// let img_y = y * self.tile_size;
 
-				let view = image.view(img_x, img_y, self.tile_size, self.tile_size);
+				let view = image.view(img_x, img_y, tile_width, tile_height);
 
 				if view
 					.pixels()
-- 
GitLab