Skip to content

Commit

Permalink
added incomplete delete
Browse files Browse the repository at this point in the history
WASM
  • Loading branch information
mkhan45 committed Apr 11, 2021
1 parent fd04f2e commit 497d40c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 1 deletion.
Binary file modified docs/simple_electronics.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ async fn main() {

ui_signals.iter().for_each(|signal| match signal {
UiSignal::AddNode(ty) => world.insert(resources::UIState::AddingNode(*ty)),
UiSignal::Delete => world.insert(resources::UIState::Deleting),
});
world.insert(resources::UiSignals(Vec::new()));
}
Expand Down
2 changes: 2 additions & 0 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum UIState {
x_pos: Option<f32>,
y_pos: Option<f32>,
},
Deleting,
Nothing,
}

Expand All @@ -30,6 +31,7 @@ impl Default for UIState {
#[derive(Clone)]
pub enum UiSignal {
AddNode(NodeTy),
Delete,
}

#[derive(Default)]
Expand Down
3 changes: 3 additions & 0 deletions src/systems/ui_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ impl<'a> System<'a> for CurrentModeSys {
current_mode.0 =
"Right click a node to set wire output or left click to cancel".to_string();
}
UIState::Deleting => {
current_mode.0 = "Click a node or wire focus to delete it".to_string();
}
_ => {
*current_mode = CurrentModeText::default();
}
Expand Down
20 changes: 20 additions & 0 deletions src/ui/mouse_click.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ pub fn handle_mouse_click(world: &mut World) {

world.insert(UIState::Nothing);
}
UIState::Deleting => {
let positions = world.read_storage::<Pos>();
let entities = world.entities();
let mouse_pos = {
let (mx, my) = mouse_position();
Vec2::new(mx, my)
};

let target = (&positions, &entities)
.join()
.find(|(pos, _)| (pos.pos - mouse_pos).length() < 35.0);

if let Some((_, entity)) = target {
entities.delete(entity).unwrap();
std::mem::drop(positions);
std::mem::drop(entities);
world.maintain();
crate::systems::cleanup_sys::run_cleanup_sys(world);
}
}
UIState::Nothing => {}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/ui/top_panel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::resources::CurrentModeText;
use crate::resources::{self, GridMode};
use crate::resources::{CurrentModeText, UiSignals};
use crate::ResetSys;
use crate::{components::nodes, UiSignal};
use egui::menu;
Expand Down Expand Up @@ -49,6 +49,10 @@ pub fn render_top_panel(ui: &mut egui::Ui, world: &mut World) {
});
world.insert(grid_mode);

if ui.button("Delete").clicked() {
world.fetch_mut::<UiSignals>().0.push(UiSignal::Delete);
}

if ui.button("Remove All").clicked() {
world.delete_all();
}
Expand Down

0 comments on commit 497d40c

Please sign in to comment.