1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::io::Write;
use clap::Subcommand;
use rpack_cli::TilemapGenerationConfig;
use rpack_cli::SaveImageFormat;
#[derive(Subcommand, Debug, Clone)]
pub enum Commands {
/// Generates a tilemap
Generate {
/// Name of the tilemap to build, when no value is provided uses 'tilemap'
#[clap(action)]
name: Option<String>,
/// size of the tilemap, default: 2048
#[arg(long)]
size: Option<u32>,
/// Image format
#[clap(short, long)]
format: Option<SaveImageFormat>,
/// Asset sources path, argument can be passed multiple times
#[clap(short, long)]
source_paths: Vec<String>,
/// Size of the padding between frames in pixel. Default value is `2`
texture_padding: Option<u32>,
/// Size of the padding on the outer edge of the packed image in pixel. Default value is `0`.
border_padding: Option<u32>,
},
/// Creates a tilemap generation config
ConfigCreate {
/// path of the config to create
#[clap(action)]
config_path: String,
/// path of the tilemap to build, when no value is provided uses '/tilemap'
#[clap(long)]
output_path: Option<String>,
/// size of the tilemap, default: 2048
#[arg(long)]
size: Option<u32>,
/// Image format, png by default
#[clap(short, long)]
format: Option<SaveImageFormat>,
/// Asset sources path, argument can be passed multiple times
#[clap(short, long)]
source_paths: Vec<String>,
/// Size of the padding between frames in pixel. Default value is `2`
#[clap(short, long)]
texture_padding: Option<u32>,
/// Size of the padding on the outer edge of the packed image in pixel. Default value is `0`.
#[clap(short, long)]
border_padding: Option<u32>,
},
/// Generates a tilemap from config
GenerateFromConfig {
/// path of the config to use
#[clap(action)]
config_path: String,
},
}
impl Commands {
pub(crate) fn run(&self) -> anyhow::Result<()> {
match self.clone() {
Commands::Generate {
name,
size,
format,
source_paths,
texture_padding,
border_padding,
} => Self::generate_tilemap(
name,
size,
format,
source_paths,
texture_padding,
border_padding,
),
Commands::ConfigCreate {
config_path,
output_path,
size,
format,
source_paths,
texture_padding,
border_padding,
} => Self::create_config(
config_path,
output_path,
size,
format,
source_paths,
texture_padding,
border_padding,
),
Commands::GenerateFromConfig { config_path } => {
Self::generate_tilemap_from_config(config_path)
}
}
}
fn generate_tilemap(
name: Option<String>,
size: Option<u32>,
format: Option<SaveImageFormat>,
source_paths: Vec<String>,
texture_padding: Option<u32>,
border_padding: Option<u32>,
) -> anyhow::Result<()> {
let name = name.unwrap_or("tilemap".to_owned());
let source_paths = if source_paths.is_empty() {
vec![".".to_owned()]
} else {
source_paths
};
let config = TilemapGenerationConfig {
asset_patterns: source_paths,
output_path: name,
format,
size,
texture_padding,
border_padding,
..Default::default()
};
config.generate()
}
fn create_config(
config_path: String,
output_path: Option<String>,
size: Option<u32>,
format: Option<SaveImageFormat>,
source_paths: Vec<String>,
texture_padding: Option<u32>,
border_padding: Option<u32>,
) -> Result<(), anyhow::Error> {
let name = output_path.unwrap_or("tilemap".to_owned());
let config = TilemapGenerationConfig {
size,
asset_patterns: source_paths,
output_path: name,
format,
texture_padding,
border_padding,
..Default::default()
};
let json = serde_json::to_string_pretty(&config)?;
let mut file = std::fs::File::create(format!("{}.rpack_gen.json", config_path)).unwrap();
file.write_all(json.as_bytes())?;
Ok(())
}
fn generate_tilemap_from_config(config_path: String) -> anyhow::Result<()> {
let config = TilemapGenerationConfig::read_from_file(config_path)?;
config.generate()
}
}