konnektoren_bevy/screens/settings/
plugin.rs

1use super::*;
2use bevy::prelude::*;
3
4/// Plugin for reusable settings screen functionality
5pub struct SettingsScreenPlugin;
6
7impl Plugin for SettingsScreenPlugin {
8    fn build(&self, app: &mut App) {
9        app.add_event::<SettingsScreenEvent>()
10            .add_event::<ComponentSettingsEvent>()
11            .add_systems(
12                Update,
13                (
14                    check_settings_screen_config,
15                    handle_settings_screen_events,
16                    update_settings_screen_values,
17                    cleanup_component_settings,
18                ),
19            )
20            .add_systems(bevy_egui::EguiContextPass, render_settings_screen_ui)
21            // Add input configuration plugin
22            .add_plugins(InputConfigurationPlugin);
23
24        // Add component-based settings systems if settings feature is enabled
25        #[cfg(feature = "settings")]
26        {
27            app.add_systems(
28                Update,
29                (check_component_settings, process_pending_setting_updates),
30            )
31            .add_systems(bevy_egui::EguiContextPass, render_component_settings_ui);
32        }
33    }
34}
35
36/// Helper trait for easy settings screen setup
37pub trait SettingsScreenExt {
38    /// Add a settings screen with the given configuration
39    fn spawn_settings_screen(&mut self, config: SettingsScreenConfig) -> Entity;
40
41    /// Add a simple settings screen with basic audio settings
42    fn spawn_simple_settings_screen(&mut self, title: impl Into<String>) -> Entity;
43
44    /// Spawn a component-based settings screen that uses Setting components
45    fn spawn_component_settings_screen(&mut self, title: impl Into<String>) -> Entity;
46
47    /// Spawn input configuration screen
48    fn spawn_input_configuration(&mut self, max_players: u32) -> Entity;
49}
50
51impl SettingsScreenExt for Commands<'_, '_> {
52    fn spawn_settings_screen(&mut self, config: SettingsScreenConfig) -> Entity {
53        self.spawn((Name::new("Settings Screen"), config)).id()
54    }
55
56    fn spawn_simple_settings_screen(&mut self, title: impl Into<String>) -> Entity {
57        let audio_section = SettingsSection::audio_section();
58        let config = SettingsScreenConfig::new(title).add_section(audio_section);
59        self.spawn_settings_screen(config)
60    }
61
62    fn spawn_component_settings_screen(&mut self, title: impl Into<String>) -> Entity {
63        self.spawn((
64            Name::new("Component-Based Settings Trigger"),
65            ActiveComponentSettings {
66                title: title.into(),
67                allow_dismissal: true,
68                back_button_text: "Back".to_string(),
69                navigation_state: ComponentSettingsNavigationState::default(),
70            },
71        ))
72        .id()
73    }
74
75    fn spawn_input_configuration(&mut self, max_players: u32) -> Entity {
76        self.spawn((
77            Name::new("Input Configuration Screen"),
78            ActiveInputConfiguration {
79                max_players,
80                current_players: max_players.min(4), // Default to 4 players max
81            },
82        ))
83        .id()
84    }
85}