bevy_archie 0.1.7

A comprehensive game controller support module for Bevy
Documentation
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Build script helpers for controller asset management.
//!
//! This module provides utilities for use in build.rs scripts to help
//! manage controller icons and other assets at compile time.
//!
//! # Example build.rs
//!
//! ```rust,no_run
//! use bevy_archie::build_helpers::{generate_icon_manifest, ControllerIconConfig};
//!
//! let config = ControllerIconConfig::default();
//! let _manifest = generate_icon_manifest("assets/icons", &config);
//! ```

use std::collections::HashMap;
use std::fmt::Write;
use std::fs;
use std::path::Path;

/// Configuration for controller icon generation.
#[derive(Debug, Clone)]
pub struct ControllerIconConfig {
    /// Supported controller types
    pub controller_types: Vec<ControllerType>,
    /// Icon sizes to generate/validate
    pub icon_sizes: Vec<u32>,
    /// Output format for generated manifests
    pub manifest_format: ManifestFormat,
    /// Whether to validate icons exist
    pub validate_icons: bool,
}

impl Default for ControllerIconConfig {
    fn default() -> Self {
        Self {
            controller_types: vec![
                ControllerType::Xbox,
                ControllerType::PlayStation,
                ControllerType::Nintendo,
                ControllerType::Generic,
            ],
            icon_sizes: vec![32, 64, 128],
            manifest_format: ManifestFormat::Json,
            validate_icons: true,
        }
    }
}

/// Supported controller types for icons.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ControllerType {
    /// Xbox controller family
    Xbox,
    /// `PlayStation` controller family
    PlayStation,
    /// Nintendo controller family
    Nintendo,
    /// Steam Deck
    SteamDeck,
    /// Generic/fallback controller
    Generic,
}

impl ControllerType {
    /// Get the directory name for this controller type.
    #[must_use]
    pub fn dir_name(self) -> &'static str {
        match self {
            Self::Xbox => "xbox",
            Self::PlayStation => "playstation",
            Self::Nintendo => "nintendo",
            Self::SteamDeck => "steamdeck",
            Self::Generic => "generic",
        }
    }

    /// Get all button names for this controller type.
    #[must_use]
    #[expect(clippy::too_many_lines, reason = "exhaustive controller button mapping requires comprehensive listing")]
    pub fn button_names(self) -> &'static [&'static str] {
        match self {
            Self::Xbox => &[
                "a",
                "b",
                "x",
                "y",
                "lb",
                "rb",
                "lt",
                "rt",
                "start",
                "select",
                "guide",
                "ls",
                "rs",
                "dpad_up",
                "dpad_down",
                "dpad_left",
                "dpad_right",
            ],
            Self::PlayStation => &[
                "cross",
                "circle",
                "square",
                "triangle",
                "l1",
                "r1",
                "l2",
                "r2",
                "options",
                "share",
                "ps",
                "l3",
                "r3",
                "dpad_up",
                "dpad_down",
                "dpad_left",
                "dpad_right",
                "touchpad",
            ],
            Self::Nintendo => &[
                "a",
                "b",
                "x",
                "y",
                "l",
                "r",
                "zl",
                "zr",
                "plus",
                "minus",
                "home",
                "ls",
                "rs",
                "dpad_up",
                "dpad_down",
                "dpad_left",
                "dpad_right",
            ],
            Self::SteamDeck => &[
                "a",
                "b",
                "x",
                "y",
                "l1",
                "r1",
                "l2",
                "r2",
                "l4",
                "r4",
                "l5",
                "r5",
                "start",
                "select",
                "steam",
                "qam",
                "ls",
                "rs",
                "dpad_up",
                "dpad_down",
                "dpad_left",
                "dpad_right",
                "trackpad_left",
                "trackpad_right",
            ],
            Self::Generic => &[
                "a",
                "b",
                "x",
                "y",
                "lb",
                "rb",
                "lt",
                "rt",
                "start",
                "select",
                "ls",
                "rs",
                "dpad_up",
                "dpad_down",
                "dpad_left",
                "dpad_right",
            ],
        }
    }
}

/// Format for generated manifests.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ManifestFormat {
    /// JSON format
    Json,
    /// RON (Rusty Object Notation) format
    Ron,
    /// TOML format
    Toml,
}

/// A manifest entry for a controller icon.
#[derive(Debug, Clone)]
pub struct IconManifestEntry {
    /// Button/input name
    pub name: String,
    /// Controller type
    pub controller_type: ControllerType,
    /// Available sizes
    pub sizes: Vec<u32>,
    /// Relative path to icon
    pub path: String,
}

/// Result of icon manifest generation.
#[derive(Debug)]
pub struct IconManifest {
    /// All icon entries
    pub entries: Vec<IconManifestEntry>,
    /// Missing icons (if validation enabled)
    pub missing: Vec<String>,
    /// Extra/unexpected files found
    pub extras: Vec<String>,
}

/// Generate an icon manifest from an assets directory.
#[must_use]
pub fn generate_icon_manifest(
    assets_dir: impl AsRef<Path>,
    config: &ControllerIconConfig,
) -> IconManifest {
    let assets_dir = assets_dir.as_ref();
    let mut manifest = IconManifest {
        entries: Vec::new(),
        missing: Vec::new(),
        extras: Vec::new(),
    };

    for controller_type in &config.controller_types {
        let controller_dir = assets_dir.join(controller_type.dir_name());

        if !controller_dir.exists() {
            if config.validate_icons {
                manifest
                    .missing
                    .push(format!("Directory missing: {}", controller_dir.display()));
            }
            continue;
        }

        // Check for each expected button
        for button_name in controller_type.button_names() {
            let mut found_sizes = Vec::new();

            for size in &config.icon_sizes {
                let icon_path = controller_dir.join(format!("{button_name}_{size}.png"));
                if icon_path.exists() {
                    found_sizes.push(*size);
                } else if config.validate_icons {
                    manifest.missing.push(format!(
                        "Icon missing: {button_name} for {controller_type:?} at {size}x{size}"
                    ));
                }
            }

            if !found_sizes.is_empty() {
                manifest.entries.push(IconManifestEntry {
                    name: (*button_name).to_string(),
                    controller_type: *controller_type,
                    sizes: found_sizes,
                    path: format!("{}/{}", controller_type.dir_name(), button_name),
                });
            }
        }
    }

    manifest
}

/// Write the manifest to a file.
///
/// # Errors
///
/// Returns an error if the file can't be written.
pub fn write_manifest(
    manifest: &IconManifest,
    output_path: impl AsRef<Path>,
    format: ManifestFormat,
) -> Result<(), std::io::Error> {
    let content = match format {
        ManifestFormat::Json => {
            let mut map: HashMap<String, Vec<HashMap<String, serde_json::Value>>> = HashMap::new();

            for entry in &manifest.entries {
                let controller_key = format!("{:?}", entry.controller_type).to_lowercase();
                let entry_map = map.entry(controller_key).or_default();

                let mut icon_data = HashMap::new();
                icon_data.insert("name".into(), serde_json::Value::String(entry.name.clone()));
                icon_data.insert("path".into(), serde_json::Value::String(entry.path.clone()));
                icon_data.insert(
                    "sizes".into(),
                    serde_json::Value::Array(
                        entry
                            .sizes
                            .iter()
                            .map(|s| serde_json::Value::Number((*s).into()))
                            .collect(),
                    ),
                );

                entry_map.push(icon_data);
            }

            serde_json::to_string_pretty(&map)
                .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?
        }
        ManifestFormat::Ron => {
            // Simplified RON output
            let mut output = String::from("(\n");
            for entry in &manifest.entries {
                let _ = writeln!(
                    output,
                    "  ({:?}, \"{}\", {:?}, \"{}\"),",
                    entry.controller_type, entry.name, entry.sizes, entry.path
                );
            }
            output.push(')');
            output
        }
        ManifestFormat::Toml => {
            // Simplified TOML output
            let mut output = String::new();
            for entry in &manifest.entries {
                let _ = write!(
                    output,
                    "[[{:?}]]\nname = \"{}\"\npath = \"{}\"\nsizes = {:?}\n\n",
                    entry.controller_type, entry.name, entry.path, entry.sizes
                );
            }
            output
        }
    };

    fs::write(output_path, content)
}

/// Validate that all required icons exist.
///
/// Returns a list of missing icons.
#[must_use]
pub fn validate_icons(assets_dir: impl AsRef<Path>, config: &ControllerIconConfig) -> Vec<String> {
    let mut config = config.clone();
    config.validate_icons = true;

    generate_icon_manifest(assets_dir, &config).missing
}

/// Generate Rust code for icon constants.
///
/// This can be used in build.rs to generate a constants file.
#[must_use]
pub fn generate_icon_constants(manifest: &IconManifest) -> String {
    let mut output = String::from("// Auto-generated icon constants\n\n");

    // Group by controller type
    let mut by_type: HashMap<&str, Vec<&IconManifestEntry>> = HashMap::new();
    for entry in &manifest.entries {
        by_type
            .entry(entry.controller_type.dir_name())
            .or_default()
            .push(entry);
    }

    for (controller_type, entries) in &by_type {
        let _ = write!(output, "\npub mod {controller_type} {{\n");

        for entry in entries {
            let const_name = entry.name.to_uppercase().replace('-', "_");
            let _ = writeln!(
                output,
                "    pub const {const_name}: &str = \"{}\";",
                entry.path
            );
        }

        output.push_str("}\n");
    }

    output
}

/// Print cargo rerun-if-changed directives for icon assets.
pub fn print_rerun_if_changed(assets_dir: impl AsRef<Path>) {
    let assets_dir = assets_dir.as_ref();

    println!("cargo:rerun-if-changed={}", assets_dir.display());

    if let Ok(entries) = fs::read_dir(assets_dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            println!("cargo:rerun-if-changed={}", path.display());

            if path.is_dir()
                && let Ok(sub_entries) = fs::read_dir(&path)
            {
                for sub_entry in sub_entries.flatten() {
                    println!("cargo:rerun-if-changed={}", sub_entry.path().display());
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_controller_type_buttons() {
        let xbox = ControllerType::Xbox;
        assert!(xbox.button_names().contains(&"a"));
        assert!(xbox.button_names().contains(&"lb"));

        let ps = ControllerType::PlayStation;
        assert!(ps.button_names().contains(&"cross"));
        assert!(ps.button_names().contains(&"touchpad"));
    }

    #[test]
    fn test_config_default() {
        let config = ControllerIconConfig::default();
        assert!(!config.controller_types.is_empty());
        assert!(!config.icon_sizes.is_empty());
        assert!(config.validate_icons);
    }

    #[test]
    fn test_generate_constants() {
        let manifest = IconManifest {
            entries: vec![IconManifestEntry {
                name: "a".into(),
                controller_type: ControllerType::Xbox,
                sizes: vec![32, 64],
                path: "xbox/a".into(),
            }],
            missing: vec![],
            extras: vec![],
        };

        let output = generate_icon_constants(&manifest);
        assert!(output.contains("pub mod xbox"));
        assert!(output.contains("pub const A: &str"));
    }
}