konnektoren_bevy/settings/
components.rs

1use bevy::prelude::*;
2
3/// Event sent when a setting value changes
4#[derive(Event, Debug, Clone)]
5pub struct SettingChangedEvent {
6    pub setting_id: String,
7    pub old_value: SettingValue,
8    pub new_value: SettingValue,
9}
10
11/// Component that defines a setting
12#[derive(Component, Debug, Clone)]
13pub struct Setting {
14    pub id: String,
15    pub label: String,
16    pub description: Option<String>,
17    pub value: SettingValue,
18    pub setting_type: SettingType,
19    pub tab_index: Option<usize>,
20    pub category: Option<String>,
21    pub enabled: bool,
22}
23
24impl Setting {
25    pub fn new(
26        id: impl Into<String>,
27        label: impl Into<String>,
28        value: SettingValue,
29        setting_type: SettingType,
30    ) -> Self {
31        Self {
32            id: id.into(),
33            label: label.into(),
34            description: None,
35            value,
36            setting_type,
37            tab_index: None,
38            category: None,
39            enabled: true,
40        }
41    }
42
43    pub fn with_description(mut self, description: impl Into<String>) -> Self {
44        self.description = Some(description.into());
45        self
46    }
47
48    pub fn with_tab_index(mut self, index: usize) -> Self {
49        self.tab_index = Some(index);
50        self
51    }
52
53    pub fn with_category(mut self, category: impl Into<String>) -> Self {
54        self.category = Some(category.into());
55        self
56    }
57
58    pub fn enabled(mut self, enabled: bool) -> Self {
59        self.enabled = enabled;
60        self
61    }
62}
63
64/// Different types of setting values
65#[derive(Debug, Clone, PartialEq)]
66pub enum SettingValue {
67    Bool(bool),
68    Int(i32),
69    Float(f32),
70    String(String),
71    Selection(usize), // Index of selected option
72}
73
74impl SettingValue {
75    pub fn as_bool(&self) -> Option<bool> {
76        match self {
77            SettingValue::Bool(v) => Some(*v),
78            _ => None,
79        }
80    }
81
82    pub fn as_int(&self) -> Option<i32> {
83        match self {
84            SettingValue::Int(v) => Some(*v),
85            _ => None,
86        }
87    }
88
89    pub fn as_float(&self) -> Option<f32> {
90        match self {
91            SettingValue::Float(v) => Some(*v),
92            _ => None,
93        }
94    }
95
96    pub fn as_string(&self) -> Option<&str> {
97        match self {
98            SettingValue::String(v) => Some(v),
99            _ => None,
100        }
101    }
102
103    pub fn as_selection(&self) -> Option<usize> {
104        match self {
105            SettingValue::Selection(v) => Some(*v),
106            _ => None,
107        }
108    }
109}
110
111/// Different types of settings and their constraints
112#[derive(Debug, Clone)]
113pub enum SettingType {
114    /// Boolean toggle
115    Toggle,
116    /// Integer with min/max bounds
117    IntRange { min: i32, max: i32, step: i32 },
118    /// Float with min/max bounds
119    FloatRange { min: f32, max: f32, step: f32 },
120    /// Text input
121    Text { max_length: Option<usize> },
122    /// Selection from a list of options
123    Selection { options: Vec<String> },
124    /// Custom setting type with validation function
125    Custom {
126        validator: fn(&SettingValue) -> bool,
127        display_fn: fn(&SettingValue) -> String,
128    },
129}
130
131/// Component that marks a setting as changed
132#[derive(Component)]
133pub struct SettingChanged {
134    pub old_value: SettingValue,
135}