Skip to main content

gcrecomp_runtime/input/
profiles.rs

1// Controller profile management
2use crate::input::gamecube_mapping::GameCubeMapping;
3use anyhow::Result;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct ControllerProfile {
8    pub name: String,
9    pub controller_type: String,
10    pub mapping: SerializedMapping,
11}
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct SerializedMapping {
15    // Simplified serialization - would need full mapping structure
16    pub button_mappings: Vec<u8>,
17    pub axis_mappings: Vec<u8>,
18    pub dead_zones: Vec<f32>,
19    pub sensitivity: Vec<f32>,
20}
21
22impl ControllerProfile {
23    pub fn from_mapping(name: String, mapping: GameCubeMapping) -> Self {
24        // Convert mapping to serializable format
25        Self {
26            name,
27            controller_type: format!("{:?}", mapping.controller_type),
28            mapping: SerializedMapping {
29                button_mappings: vec![], // Would serialize actual mappings
30                axis_mappings: vec![],
31                dead_zones: vec![
32                    mapping.dead_zones.left_stick,
33                    mapping.dead_zones.right_stick,
34                    mapping.dead_zones.left_trigger,
35                    mapping.dead_zones.right_trigger,
36                ],
37                sensitivity: vec![
38                    mapping.sensitivity.left_stick,
39                    mapping.sensitivity.right_stick,
40                ],
41            },
42        }
43    }
44
45    pub fn to_gamecube_mapping(&self) -> Result<GameCubeMapping> {
46        // Convert serialized format back to mapping
47        // For now, return default based on controller type
48        match self.controller_type.as_str() {
49            "Xbox" => Ok(GameCubeMapping::xbox_default()),
50            "PlayStation" => Ok(GameCubeMapping::playstation_default()),
51            "SwitchPro" => Ok(GameCubeMapping::switch_pro_default()),
52            _ => Ok(GameCubeMapping::generic_default()),
53        }
54    }
55
56    pub fn save_to_file(&self, path: &std::path::Path) -> Result<()> {
57        let json = serde_json::to_string_pretty(self)?;
58        std::fs::write(path, json)?;
59        Ok(())
60    }
61
62    pub fn load_from_file(path: &std::path::Path) -> Result<Self> {
63        let json = std::fs::read_to_string(path)?;
64        let profile: ControllerProfile = serde_json::from_str(&json)?;
65        Ok(profile)
66    }
67}