-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.rs
320 lines (313 loc) · 14 KB
/
input.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use bevy::prelude::*;
use bevy::window::PrimaryWindow;
use bevy_pixel_buffer::bevy_egui::egui::Pos2;
use crate::components::microphone::Microphone;
use crate::components::source::{Source, SourceType};
use crate::components::states::{Drag, Overlay, Selected};
use crate::components::wall::{WallBlock, WallCell, WallResize};
use crate::grid::plugin::ComponentIDs;
use crate::math::constants::{SIMULATION_HEIGHT, SIMULATION_WIDTH};
use crate::math::transformations::{screen_to_grid, screen_to_nearest_grid};
use crate::render::state::{ToolType, UiState, WallBrush};
pub fn button_input(
mouse_buttons: Res<ButtonInput<MouseButton>>,
keys: Res<ButtonInput<KeyCode>>,
q_windows: Query<&Window, With<PrimaryWindow>>,
sources: Query<(Entity, &Source), Without<Drag>>,
mut drag_sources: Query<(Entity, &mut Source), With<Drag>>,
microphones: Query<(Entity, &Microphone), Without<Drag>>,
mut drag_microphones: Query<(Entity, &mut Microphone), With<Drag>>,
mut selected: Query<Entity, With<Selected>>,
wallblocks: Query<(Entity, &WallBlock), (Without<Drag>, Without<WallResize>)>,
mut drag_wallblocks: Query<(Entity, &mut WallBlock), With<Drag>>,
mut resize_wallblocks: Query<
(Entity, &WallResize, &mut WallBlock),
(With<WallResize>, Without<Drag>),
>,
mut commands: Commands,
mut ui_state: ResMut<UiState>,
mut component_ids: ResMut<ComponentIDs>,
) {
if mouse_buttons.just_pressed(MouseButton::Left) && ui_state.tools_enabled {
let window = q_windows.single();
selected.iter_mut().for_each(|entity| {
commands.entity(entity).remove::<Selected>();
});
match ui_state.current_tool {
ToolType::MoveSource => {
if let Some(position) = window.cursor_position() {
if let Some((x, y)) = screen_to_nearest_grid(
position.x,
position.y,
ui_state.image_rect,
&ui_state,
) {
for (entity, source) in sources.iter() {
let (s_x, s_y) = (source.x, source.y);
if s_x.abs_diff(x) <= 10 && s_y.abs_diff(y) <= 10 {
//values should change depending on image size (smaller image -> greater radius)
commands.entity(entity).insert((Drag, Selected));
break; // only drag one at a time
}
}
}
}
}
ToolType::PlaceSource => {
if let Some(position) = window.cursor_position() {
if let Some((x, y)) =
screen_to_grid(position.x, position.y, ui_state.image_rect, &ui_state)
{
// this produces overlaping sources
commands.spawn(Source::new(
x,
y,
10.,
0.0,
10_000.0,
SourceType::Sin,
component_ids.get_current_source_id(),
));
}
}
}
ToolType::DrawWall => match ui_state.wall_brush {
WallBrush::Rectangle => {
if let Some(position) = window.cursor_position() {
if let Some((mut x, mut y)) = screen_to_nearest_grid(
position.x,
position.y,
ui_state.image_rect,
&ui_state,
) {
if keys.pressed(KeyCode::ControlLeft) {
x = (x as f32 / 10.).round() as u32 * 10;
y = (y as f32 / 10.).round() as u32 * 10;
}
commands.spawn((
WallBlock::new(
x,
y,
x,
y,
ui_state.wall_reflection_factor,
component_ids.get_current_wall_id(),
),
WallResize::BottomRight,
Overlay,
));
}
}
}
WallBrush::CircleBrush => {
if let Some(position) = window.cursor_position() {
if let Some((x, y)) =
screen_to_grid(position.x, position.y, ui_state.image_rect, &ui_state)
{
for dx in -(ui_state.wall_brush_radius as i32)
..ui_state.wall_brush_radius as i32
{
for dy in -(ui_state.wall_brush_radius as i32)
..ui_state.wall_brush_radius as i32
{
if dx * dx + dy * dy
<= ui_state.wall_brush_radius as i32
* ui_state.wall_brush_radius as i32
{
let x = (x as i32 + dx)
.clamp(0, SIMULATION_WIDTH as i32 - 1)
as u32;
let y = (y as i32 + dy)
.clamp(0, SIMULATION_HEIGHT as i32 - 1)
as u32;
commands.spawn((WallCell::new(
x,
y,
ui_state.wall_reflection_factor,
),));
}
}
}
}
}
}
},
ToolType::MoveWall => {
if let Some(position) = window.cursor_position() {
if let Some((x, y)) =
screen_to_grid(position.x, position.y, ui_state.image_rect, &ui_state)
{
for (entity, wall) in wallblocks.iter() {
let center = wall.rect.center();
if (center.x as u32).abs_diff(x) <= 10
&& (center.y as u32).abs_diff(y) <= 10
{
commands.entity(entity).insert((Drag, Selected));
break;
}
}
}
}
}
ToolType::ResizeWall => {
if let Some(position) = window.cursor_position() {
if let Some((x, y)) =
screen_to_grid(position.x, position.y, ui_state.image_rect, &ui_state)
{
for (entity, wall) in wallblocks.iter() {
let max = wall.rect.max;
if (max.x as u32).abs_diff(x) <= 10 && (max.y as u32).abs_diff(y) <= 10
{
commands
.entity(entity)
.insert((WallResize::BottomRight, Overlay));
break;
}
}
}
}
}
ToolType::PlaceMic => {
if let Some(position) = window.cursor_position() {
if let Some((x, y)) =
screen_to_grid(position.x, position.y, ui_state.image_rect, &ui_state)
{
commands.spawn(Microphone::new(x, y, component_ids.get_current_mic_id()));
}
}
}
ToolType::MoveMic => {
if let Some(position) = window.cursor_position() {
if let Some((x, y)) = screen_to_nearest_grid(
position.x,
position.y,
ui_state.image_rect,
&ui_state,
) {
for (entity, mic) in microphones.iter() {
let (m_x, m_y) = (mic.x, mic.y);
if m_x.abs_diff(x) <= 10 && m_y.abs_diff(y) <= 10 {
//values should change depending on image size (smaller image -> greater radius)
commands.entity(entity).insert((Drag, Selected));
break; // only drag one at a time
}
}
}
}
}
}
}
if mouse_buttons.just_released(MouseButton::Left) {
drag_sources.iter_mut().for_each(|(entity, _)| {
commands.entity(entity).remove::<Drag>();
});
drag_microphones.iter_mut().for_each(|(entity, _)| {
commands.entity(entity).remove::<Drag>();
});
resize_wallblocks
.iter_mut()
.for_each(|(entity, _, wallblock)| {
if wallblock.rect.width() == 0. || wallblock.rect.height() == 0. {
commands.entity(entity).despawn();
}
commands.entity(entity).remove::<(WallResize, Overlay)>();
});
drag_wallblocks.iter_mut().for_each(|(entity, _)| {
commands.entity(entity).remove::<Drag>();
});
}
if mouse_buttons.pressed(MouseButton::Left) && ui_state.tools_enabled {
let window = q_windows.single();
match ui_state.current_tool {
ToolType::MoveSource => {
if let Some(position) = window.cursor_position() {
if let Some((x, y)) = screen_to_nearest_grid(
position.x,
position.y,
ui_state.image_rect,
&ui_state,
) {
drag_sources.iter_mut().for_each(|(_, mut source)| {
source.x = x;
source.y = y;
});
}
}
}
ToolType::DrawWall | ToolType::ResizeWall => {
if let Some(position) = window.cursor_position() {
if let Some((mut x, mut y)) = screen_to_nearest_grid(
position.x,
position.y,
ui_state.image_rect,
&ui_state,
) {
if keys.pressed(KeyCode::ControlLeft) {
x = (x as f32 / 10.).round() as u32 * 10;
y = (y as f32 / 10.).round() as u32 * 10;
}
resize_wallblocks
.iter_mut()
.for_each(|(_, wall_resize, mut wall)| {
match wall_resize {
WallResize::BottomRight => {
wall.rect.max.x = x as f32;
wall.rect.max.y = y as f32;
}
_ => {}
}
wall.update_calc_rect(ui_state.e_al);
});
}
}
}
ToolType::MoveWall => {
if let Some(position) = window.cursor_position() {
if let Some((mut x, mut y)) = screen_to_nearest_grid(
position.x,
position.y,
ui_state.image_rect,
&ui_state,
) {
if keys.pressed(KeyCode::ControlLeft) {
x = (x as f32 / 10.).round() as u32 * 10;
y = (y as f32 / 10.).round() as u32 * 10;
}
drag_wallblocks.iter_mut().for_each(|(_, mut wall)| {
let width = wall.rect.width() / 2.;
let height = wall.rect.height() / 2.;
wall.rect.min = Pos2::new(x as f32 - width, y as f32 - height);
wall.rect.max = Pos2::new(x as f32 + width, y as f32 + height);
wall.update_calc_rect(ui_state.e_al);
});
}
}
}
ToolType::MoveMic => {
if let Some(position) = window.cursor_position() {
if let Some((x, y)) = screen_to_nearest_grid(
position.x,
position.y,
ui_state.image_rect,
&ui_state,
) {
drag_microphones.iter_mut().for_each(|(_, mut mic)| {
mic.x = x;
mic.y = y;
});
}
}
}
ToolType::PlaceSource => {}
ToolType::PlaceMic => {}
}
}
if keys.just_pressed(KeyCode::Space) {
ui_state.is_running = !ui_state.is_running;
}
if keys.just_pressed(KeyCode::Delete) {
selected.iter_mut().for_each(|entity| {
commands.entity(entity).despawn();
});
}
}