bevy_basic_ui/settings/
systems.rs1use bevy::prelude::*;
2
3use crate::{
4 components::Screen,
5 settings::resources::TomlAsset,
6 styles::{get_title_text_styles, CENTRAL_PANEL_STYLES, TITLE_STYLE},
7 widgets::slider::components::{Knob, Rack},
8};
9
10use super::{
11 components::SettingsUi,
12 resources::{AllSettings, SettingsVals},
13 styles::get_subtitle_text_styles,
14};
15
16pub fn spawn_settings(
17 commands: Commands,
18 asset_server: Res<AssetServer>,
19 knobs_q: Query<(Entity, &Knob)>,
20 settings: Res<SettingsVals>,
21) {
22 build_settings(commands, asset_server, knobs_q, settings);
23}
24
25pub fn build_settings(
26 mut commands: Commands,
27 asset_server: Res<AssetServer>,
28 knobs_q: Query<(Entity, &Knob)>,
29 settings: Res<SettingsVals>,
30) {
31 let _root_node = commands
32 .spawn((
33 NodeBundle {
34 style: CENTRAL_PANEL_STYLES,
35 ..default()
36 },
37 SettingsUi,
38 Screen,
39 ))
40 .with_children(|parent| {
41 parent.spawn(NodeBundle {
42 style: TITLE_STYLE,
43 ..default()
44 });
45 parent.spawn(TextBundle {
46 text: Text {
47 sections: vec![TextSection::new(
48 "Settings".to_string(),
49 get_title_text_styles(&asset_server),
50 )],
51 ..default()
52 },
53 ..default()
54 });
55 for (idx, settings) in settings.0.iter().enumerate() {
56 parent.spawn(TextBundle {
57 text: Text {
58 sections: vec![TextSection::new(
59 settings.tag.to_string(),
60 get_subtitle_text_styles(&asset_server),
61 )],
62 ..default()
63 },
64 ..default()
65 });
66 let mut rack = parent.spawn((
67 NodeBundle {
68 style: Style {
69 width: Val::Percent(80.0),
70 height: Val::Px(30.0),
71 margin: UiRect::all(Val::Px(40.0)),
72 ..default()
73 },
74
75 background_color: BackgroundColor(Color::DARK_GRAY),
76 ..default()
77 },
78 Rack {
79 index_tag: idx,
80 root_res: settings.tag.to_string(),
81 },
82 ));
83 for knob in knobs_q.iter() {
84 if knob.1.index_tag == idx {
85 rack.insert_children(0, &[knob.0]);
86 }
87 }
88 }
89 })
90 .id();
91}
92
93pub fn init_settings(mut commands: Commands, settings: Res<SettingsVals>) {
94 for (idx, x) in settings.0.iter().enumerate() {
95 commands.spawn((
96 NodeBundle {
97 style: Style {
98 width: Val::Px(30.0),
99 height: Val::Px(30.0),
100 border: UiRect::all(Val::Px(10.0)),
101 ..default()
102 },
103
104 background_color: BackgroundColor(Color::GRAY),
105 ..default()
106 },
107 Knob {
108 index_tag: idx,
109 value: x.value,
110 },
111 ));
112 }
113}
114
115pub fn load_settings_toml(mut commands: Commands, asset_server: Res<AssetServer>) {
116 let handle = TomlAsset(asset_server.load("settings.toml"));
117 commands.insert_resource(handle);
118}
119
120pub fn assign_to_resource(
121 mut commands: Commands,
122 settings: Res<Assets<AllSettings>>,
123 toml: Res<TomlAsset>,
124) {
125 if let Some(stngs) = settings.get(&toml.0) {
126 let asset = &stngs.categories[0].contents;
127 commands.insert_resource(SettingsVals(asset.to_vec()));
128 }
129}