/
startup.rs
110 lines (96 loc) · 3.2 KB
/
startup.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
use crate::{FFTArgs, FFTState};
use bevy::sprite::Anchor;
use bevy::{
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
};
fn spawn_bars(
num_bars: u32,
w: f32,
args: &FFTArgs,
commands: &mut Commands,
meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<ColorMaterial>>,
) -> (Vec<(Handle<Mesh>, Handle<ColorMaterial>)>, Vec<Entity>) {
let bar_size = w as f32 / num_bars as f32;
let mut handle_vec = Vec::new();
let mut despawn_handles = Vec::new();
for i in 0..num_bars {
let handle1 = meshes.add(Rectangle::new(bar_size, 0.0));
let color_handle = materials.add(args.border_color);
handle_vec.push((handle1.clone(), color_handle.clone()));
let dh = commands
.spawn(MaterialMesh2dBundle {
mesh: Mesh2dHandle(handle1),
material: color_handle,
transform: Transform::from_xyz(
bar_size * i as f32 + (bar_size / 2.0) - (w / 2.0) as f32,
0.0,
-1.0,
),
..default()
})
.id();
despawn_handles.push(dh);
let handle2 = meshes.add(Rectangle::new(bar_size - args.border_size as f32, 0.0));
let color_handle = materials.add(args.bar_color);
handle_vec.push((handle2.clone(), color_handle.clone()));
let dh = commands
.spawn(MaterialMesh2dBundle {
mesh: Mesh2dHandle(handle2),
material: color_handle,
transform: Transform::from_xyz(
bar_size * i as f32 + (bar_size / 2.0) - (w / 2.0) as f32,
0.0,
0.0,
),
..default()
})
.id();
despawn_handles.push(dh);
}
(handle_vec, despawn_handles)
}
pub fn startup(
mut commands: Commands,
mut window: Query<&mut Window>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut fft_queue: ResMut<FFTState>,
args: Res<FFTArgs>,
) {
commands.spawn(Camera2dBundle::default());
let mut differencing_args = args.clone();
differencing_args.track_name = !args.track_name;
commands.spawn(differencing_args);
let w = window.single_mut().width();
let h = window.single_mut().height();
let text_style = TextStyle {
font: Default::default(),
font_size: args.font_size as f32,
color: args.text_color,
};
commands.spawn(Text2dBundle {
text: Text::from_section(
format!(
"Playing: \"{}\"",
args.file_path.file_name().unwrap().to_str().unwrap()
),
text_style.clone(),
),
transform: Transform::from_xyz(-(w as f32 / 2.0) + 10.0, (h as f32 / 2.0) - 10.0, 0.0),
text_anchor: Anchor::TopLeft,
..default()
});
let num_bars = fft_queue.fft[0].len();
let (mesh_handles, despawn_handles) = spawn_bars(
num_bars as u32,
w,
&args,
&mut commands,
&mut meshes,
&mut materials,
);
fft_queue.curr_bars = mesh_handles;
fft_queue.despawn_handles = despawn_handles;
}