/
update_view_settings.rs
143 lines (125 loc) · 5.01 KB
/
update_view_settings.rs
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
use crate::*;
use bevy::render::mesh::VertexAttributeValues;
use bevy::prelude::*;
pub fn update_view_settings(
mut window: Query<&mut Window>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
app_state: Res<FFTState>,
args: Res<FFTArgs>,
mut clear_color: ResMut<ClearColor>,
mut text_query: Query<(&mut Transform, &mut Text)>,
mut differencing_args_query: Query<&mut FFTArgs>,
mut bar_query: Query<&mut Transform, Without<Text>>,
) {
let mut differencing_args = differencing_args_query.get_single_mut().unwrap();
// Update bar sizes and positions on resize
let w = window.single_mut().width();
if differencing_args.window_width != w {
let h = window.single_mut().height();
let mut text = text_query.get_single_mut().unwrap().0;
text.translation.x = 10.0 - w / 2.0;
text.translation.y = h / 2.0 - 10.0;
let bar_size = w / app_state.fft[0].len() as f32;
for (i, b) in app_state.despawn_handles.chunks(2).enumerate() {
bar_query.get_mut(b[0]).unwrap().translation.x =
bar_size * i as f32 + bar_size / 2.0 - w / 2.0;
bar_query.get_mut(b[1]).unwrap().translation.x =
bar_size * i as f32 + bar_size / 2.0 - w / 2.0;
}
let outer_bar_size = bar_size / 2.0;
let inner_bar_size = (bar_size - args.border_size as f32) / 2.0;
for handle in app_state.curr_bars.chunks(2) {
let handle1 = handle[0].0.clone_weak();
let handle2 = handle[1].0.clone_weak();
let dims = meshes
.get_mut(handle1)
.unwrap()
.attribute_mut(Mesh::ATTRIBUTE_POSITION)
.unwrap();
match dims {
VertexAttributeValues::Float32x3(x) => {
x[0][0] = outer_bar_size;
x[1][0] = -outer_bar_size;
x[2][0] = -outer_bar_size;
x[3][0] = outer_bar_size;
}
_ => {}
}
let dims = meshes
.get_mut(handle2)
.unwrap()
.attribute_mut(Mesh::ATTRIBUTE_POSITION)
.unwrap();
match dims {
VertexAttributeValues::Float32x3(x) => {
x[0][0] = inner_bar_size;
x[1][0] = -inner_bar_size;
x[2][0] = -inner_bar_size;
x[3][0] = inner_bar_size;
}
_ => {}
}
}
differencing_args.window_width = w;
}
// Update text color + visibility + size
if differencing_args.text_color != args.text_color
|| differencing_args.track_name != args.track_name
|| differencing_args.font_size != args.font_size
{
for mut text in &mut text_query {
if args.track_name {
text.1.sections[0].style.color = args.text_color;
text.1.sections[0].style.font_size = args.font_size as f32;
} else {
text.1.sections[0].style.color = Color::rgba(0.0, 0.0, 0.0, 0.0);
}
}
differencing_args.text_color = args.text_color;
differencing_args.font_size = args.font_size;
differencing_args.track_name = args.track_name;
}
// Update background color
if differencing_args.background_color != args.background_color {
clear_color.0 = args.background_color;
differencing_args.background_color = args.background_color;
}
// Update bar colors
if differencing_args.bar_color != args.bar_color
|| differencing_args.border_color != args.border_color
{
for handle in app_state.curr_bars.chunks(2) {
let (color_handle1, color_handle2) =
(handle[0].1.clone_weak(), handle[1].1.clone_weak());
materials.get_mut(color_handle1).unwrap().color = args.border_color;
materials.get_mut(color_handle2).unwrap().color = args.bar_color;
}
differencing_args.bar_color = args.bar_color;
differencing_args.border_color = args.border_color;
}
// Update border size
if differencing_args.border_size != args.border_size {
let w = window.single_mut().width();
let bar_size =
((w as f32 / (app_state.curr_bars.len() / 2) as f32) - args.border_size as f32) / 2.0;
for handle in app_state.curr_bars.chunks(2) {
let handle1 = handle[1].0.clone_weak();
let dims = meshes
.get_mut(handle1)
.unwrap()
.attribute_mut(Mesh::ATTRIBUTE_POSITION)
.unwrap();
match dims {
VertexAttributeValues::Float32x3(x) => {
x[0][0] = bar_size;
x[1][0] = -bar_size;
x[2][0] = -bar_size;
x[3][0] = bar_size;
}
_ => {}
}
}
differencing_args.border_size = args.border_size;
}
}