Skip to content

Commit 361dc48

Browse files
committed
Add reset on change
1 parent 2d33566 commit 361dc48

File tree

4 files changed

+114
-45
lines changed

4 files changed

+114
-45
lines changed

src/events.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ pub struct EventPlugin;
88

99
impl Plugin for EventPlugin {
1010
fn build(&self, app: &mut App) {
11-
app.add_systems(PostUpdate, update_wall_event)
12-
.add_event::<UpdateWalls>();
11+
app.add_systems(PostUpdate, (update_wall_event, reset_event))
12+
.add_event::<UpdateWalls>()
13+
.add_event::<ResetEvent>();
1314
}
1415
}
1516

@@ -27,3 +28,18 @@ pub fn update_wall_event(
2728
grid.update_walls(&rect_walls, &circ_walls, ui_state.boundary_width);
2829
}
2930
}
31+
32+
#[derive(Event)]
33+
pub struct ResetEvent;
34+
35+
pub fn reset_event(
36+
mut reset_ev: EventReader<ResetEvent>,
37+
mut grid: ResMut<Grid>,
38+
ui_state: Res<UiState>,
39+
) {
40+
if ui_state.reset_on_change {
41+
for _ in reset_ev.read() {
42+
grid.reset_cells(ui_state.boundary_width);
43+
}
44+
}
45+
}

src/input/input.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::components::microphone::Microphone;
55
use crate::components::source::{Source, SourceType};
66
use crate::components::states::{Drag, Selected};
77
use crate::components::wall::{CircWall, RectWall, WResize, Wall};
8-
use crate::events::UpdateWalls;
8+
use crate::events::{ResetEvent, UpdateWalls};
99
use crate::grid::plugin::ComponentIDs;
1010
use crate::math::transformations::{screen_to_grid, screen_to_nearest_grid};
1111
use crate::ui::state::{ClipboardBuffer, ToolType, UiState, WallType};
@@ -62,6 +62,7 @@ pub fn copy_paste_system(
6262
pub fn button_input(
6363
mouse_buttons: Res<ButtonInput<MouseButton>>,
6464
mut wall_update_ev: EventWriter<UpdateWalls>,
65+
mut reset_ev: EventWriter<ResetEvent>,
6566
keys: Res<ButtonInput<KeyCode>>,
6667
q_windows: Query<&Window, With<PrimaryWindow>>,
6768
sources: Query<(Entity, &Source), Without<Drag>>,
@@ -267,11 +268,13 @@ pub fn button_input(
267268

268269
wall_update_ev.send(UpdateWalls);
269270
}
270-
271+
271272
if mouse_buttons.pressed(MouseButton::Left) && ui_state.tools_enabled {
272273
let window = q_windows.single();
273-
274+
274275
if let Some(position) = window.cursor_position() {
276+
//TODO: this triggers everytime we release left mouse (pls fix)
277+
reset_ev.send(ResetEvent);
275278
match ui_state.current_tool {
276279
ToolType::MoveSource => {
277280
if let Some((x, y)) =
@@ -341,6 +344,7 @@ pub fn button_input(
341344
selected.iter_mut().for_each(|entity| {
342345
commands.entity(entity).despawn();
343346
wall_update_ev.send(UpdateWalls);
347+
reset_ev.send(ResetEvent);
344348
});
345349
}
346350
}

src/ui/draw.rs

+87-40
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::components::microphone::*;
1212
use crate::components::source::*;
1313
use crate::components::states::{MenuSelected, Selected};
1414
use crate::components::wall::{CircWall, RectWall, Wall};
15-
use crate::events::UpdateWalls;
15+
use crate::events::{ResetEvent, UpdateWalls};
1616
use crate::grid::grid::Grid;
1717
use crate::math::constants::*;
1818
use crate::math::fft::calc_mic_spectrum;
@@ -29,6 +29,7 @@ pub fn draw_egui(
2929
mut grid: ResMut<Grid>,
3030
mut gradient: ResMut<GradientResource>,
3131
mut wall_update_ev: EventWriter<UpdateWalls>,
32+
mut reset_ev: EventWriter<ResetEvent>,
3233
mut rect_wall_set: ParamSet<(
3334
Query<(Entity, &mut RectWall)>,
3435
Query<(Entity, &mut RectWall), With<Selected>>,
@@ -236,34 +237,54 @@ pub fn draw_egui(
236237
let collapse = ui.collapsing(format!("Source {}", source.id), |ui| {
237238
ui.horizontal(|ui| {
238239
ui.label("x:");
239-
ui.add(
240-
egui::DragValue::new(&mut source.x)
241-
.speed(1)
242-
.clamp_range(0.0..=SIMULATION_WIDTH as f32 - 1.),
243-
);
240+
if ui
241+
.add(
242+
egui::DragValue::new(&mut source.x)
243+
.speed(1)
244+
.clamp_range(0.0..=SIMULATION_WIDTH as f32 - 1.),
245+
)
246+
.changed()
247+
{
248+
reset_ev.send(ResetEvent);
249+
}
244250
ui.add_space(10.);
245251
ui.label("y:");
246-
ui.add(
247-
egui::DragValue::new(&mut source.y)
248-
.speed(1)
249-
.clamp_range(0.0..=SIMULATION_HEIGHT as f32 - 1.),
250-
);
252+
if ui
253+
.add(
254+
egui::DragValue::new(&mut source.y)
255+
.speed(1)
256+
.clamp_range(0.0..=SIMULATION_HEIGHT as f32 - 1.),
257+
)
258+
.changed()
259+
{
260+
reset_ev.send(ResetEvent);
261+
}
251262
});
252263
if source.source_type != SourceType::WhiteNoise {
253-
ui.add(
254-
egui::Slider::new(&mut source.frequency, 0.0..=20000.0)
255-
.text("Frequency (Hz)"),
256-
);
264+
if ui
265+
.add(
266+
egui::Slider::new(&mut source.frequency, 0.0..=20000.0)
267+
.text("Frequency (Hz)"),
268+
)
269+
.changed()
270+
{
271+
reset_ev.send(ResetEvent);
272+
}
257273
}
258274
ui.add(
259275
egui::Slider::new(&mut source.amplitude, 0.0..=25.0)
260276
.text("Amplitude"),
261277
);
262278
if source.source_type == SourceType::Sin {
263-
ui.add(
264-
egui::Slider::new(&mut source.phase, 0.0..=360.0)
265-
.text("Phase (°)"),
266-
);
279+
if ui
280+
.add(
281+
egui::Slider::new(&mut source.phase, 0.0..=360.0)
282+
.text("Phase (°)"),
283+
)
284+
.changed()
285+
{
286+
reset_ev.send(ResetEvent);
287+
}
267288
}
268289

269290
egui::ComboBox::from_label("Waveform")
@@ -374,6 +395,7 @@ pub fn draw_egui(
374395
if wall.rect.min.x > wall.rect.max.x {
375396
wall.rect.min.x = wall.rect.max.x;
376397
}
398+
reset_ev.send(ResetEvent);
377399
}
378400
ui.add_space(10.);
379401
ui.label("Top Corner x:");
@@ -386,6 +408,7 @@ pub fn draw_egui(
386408
.changed()
387409
{
388410
// wall.update_calc_rect(ui_state.boundary_width);
411+
reset_ev.send(ResetEvent);
389412
}
390413
});
391414

@@ -400,6 +423,7 @@ pub fn draw_egui(
400423
.changed()
401424
{
402425
// wall.update_calc_rect(ui_state.boundary_width);
426+
reset_ev.send(ResetEvent);
403427
}
404428
ui.add_space(10.);
405429
ui.label("Bottom Corner y:");
@@ -412,6 +436,7 @@ pub fn draw_egui(
412436
.changed()
413437
{
414438
// wall.update_calc_rect(ui_state.boundary_width);
439+
reset_ev.send(ResetEvent);
415440
}
416441
});
417442

@@ -429,19 +454,26 @@ pub fn draw_egui(
429454
));
430455
});
431456

432-
ui.add(
433-
// 0.01 because rendering then draws white
434-
egui::Slider::new(&mut wall.reflection_factor, 0.01..=1.0)
435-
.text("Wall Reflection Factor"),
436-
);
457+
if ui
458+
.add(
459+
// 0.01 because rendering then draws white
460+
egui::Slider::new(&mut wall.reflection_factor, 0.01..=1.0)
461+
.text("Wall Reflection Factor"),
462+
)
463+
.changed()
464+
{
465+
reset_ev.send(ResetEvent);
466+
}
437467

438468
if ui.checkbox(&mut wall.is_hollow, "Hollow Wall").changed() {
439469
wall_update_ev.send(UpdateWalls);
470+
reset_ev.send(ResetEvent);
440471
};
441472

442473
if ui.add(egui::Button::new("Delete")).clicked() {
443474
commands.entity(*entity).despawn();
444475
wall_update_ev.send(UpdateWalls);
476+
reset_ev.send(ResetEvent);
445477
}
446478
});
447479

@@ -478,18 +510,25 @@ pub fn draw_egui(
478510
));
479511
});
480512

481-
ui.add(
482-
egui::Slider::new(&mut wall.reflection_factor, 0.01..=1.0)
483-
.text("Wall Reflection Factor"),
484-
);
513+
if ui
514+
.add(
515+
egui::Slider::new(&mut wall.reflection_factor, 0.01..=1.0)
516+
.text("Wall Reflection Factor"),
517+
)
518+
.changed()
519+
{
520+
reset_ev.send(ResetEvent);
521+
}
485522

486523
if ui.checkbox(&mut wall.is_hollow, "Hollow Wall").changed() {
487524
wall_update_ev.send(UpdateWalls);
525+
reset_ev.send(ResetEvent);
488526
};
489527

490528
if ui.add(egui::Button::new("Delete")).clicked() {
491529
commands.entity(*entity).despawn();
492530
wall_update_ev.send(UpdateWalls);
531+
reset_ev.send(ResetEvent);
493532
}
494533
});
495534

@@ -508,12 +547,6 @@ pub fn draw_egui(
508547
ui.heading("General Settings");
509548
ui.separator();
510549

511-
ui.horizontal(|ui| {
512-
ui.color_edit_button_srgba(&mut gradient.0);
513-
ui.color_edit_button_srgba(&mut gradient.1);
514-
});
515-
ui.separator();
516-
517550
ui.horizontal(|ui| {
518551
if ui
519552
.button(if ui_state.is_running { "Stop" } else { "Start" })
@@ -548,13 +581,25 @@ pub fn draw_egui(
548581
grid.reset_cells(ui_state.boundary_width);
549582
wall_update_ev.send(UpdateWalls);
550583
}
584+
585+
ui.checkbox(&mut ui_state.reset_on_change, "Reset on Change")
551586
});
552587

553-
ui.add(
554-
egui::Slider::new(&mut ui_state.delta_l, 0.0..=10.0)
555-
.text("Delta L")
556-
.logarithmic(true),
557-
);
588+
if ui
589+
.add(
590+
egui::Slider::new(&mut ui_state.delta_l, 0.0..=10.0)
591+
.text("Delta L")
592+
.logarithmic(true),
593+
)
594+
.changed()
595+
{
596+
reset_ev.send(ResetEvent);
597+
}
598+
599+
ui.horizontal(|ui| {
600+
ui.color_edit_button_srgba(&mut gradient.0);
601+
ui.color_edit_button_srgba(&mut gradient.1);
602+
});
558603

559604
if ui
560605
.checkbox(&mut ui_state.show_plots, "Show Plots")
@@ -810,7 +855,9 @@ pub fn draw_egui(
810855
.x_axis_label("Frequency (Hz)")
811856
.y_axis_label("Intensity (dB)")
812857
.x_grid_spacer(|input| {
813-
let mut marks = Vec::with_capacity(input.bounds.1 as usize - input.bounds.0 as usize + 1);
858+
let mut marks = Vec::with_capacity(
859+
input.bounds.1 as usize - input.bounds.0 as usize + 1,
860+
);
814861

815862
for i in input.bounds.0 as u32 + 1..=input.bounds.1 as u32 {
816863
marks.push(GridMark {

src/ui/state.rs

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub struct UiState {
8282
pub wall_type: WallType,
8383
pub wall_is_hollow: bool,
8484
pub tools_enabled: bool,
85+
pub reset_on_change: bool,
8586
}
8687

8788
impl Default for UiState {
@@ -104,6 +105,7 @@ impl Default for UiState {
104105
wall_type: WallType::Rectangle,
105106
wall_is_hollow: false,
106107
tools_enabled: true,
108+
reset_on_change: true,
107109
}
108110
}
109111
}

0 commit comments

Comments
 (0)