Skip to content

Commit

Permalink
basic guides in UI
Browse files Browse the repository at this point in the history
wasm
  • Loading branch information
mkhan45 committed Apr 11, 2021
1 parent d48cec2 commit e4e6aa1
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 42 deletions.
Binary file modified docs/simple_electronics.wasm
Binary file not shown.
14 changes: 9 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ mod svg;
mod systems;
mod ui;

use components::{
nodes::{self, add_node_systems},
Orientation,
};
use components::nodes::{self, add_node_systems};
use components::{Connected, Pos};
use systems::draw_systems::add_draw_system;
use systems::simulation_systems::*;
Expand All @@ -32,7 +29,8 @@ async fn main() {
};

let mut draw_dispatcher = {
let mut builder = DispatcherBuilder::new();
let mut builder =
DispatcherBuilder::new().with_thread_local(systems::ui_systems::CurrentModeSys);
builder = add_draw_system(builder);
builder.build()
};
Expand Down Expand Up @@ -175,6 +173,12 @@ async fn main() {
}

egui_macroquad::ui(|egui_ctx| {
use egui::{FontDefinitions, TextStyle};
let mut fonts = FontDefinitions::default();
fonts.family_and_size.get_mut(&TextStyle::Button).unwrap().1 = 24.0;
fonts.family_and_size.get_mut(&TextStyle::Body).unwrap().1 = 28.0;
egui_ctx.set_fonts(fonts);

egui::TopPanel::top("SIMple Electronics").show(egui_ctx, |ui| {
ui::top_panel::render_top_panel(ui, &mut world);
});
Expand Down
13 changes: 13 additions & 0 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub struct Textures(pub std::collections::BTreeMap<String, Texture2D>);
#[derive(Default)]
pub struct AddingNode(pub Option<NodeTy>);

// should probably not be a tuple struct
// it goes
// input node entity, wire entity, x pos, y pos
#[derive(Default)]
pub struct AddingWire(pub Option<(Entity, Entity, Option<f32>, Option<f32>)>);

Expand Down Expand Up @@ -45,3 +48,13 @@ impl Default for GridMode {
GridMode::CrossHatches
}
}

pub struct CurrentModeText(pub String);

impl Default for CurrentModeText {
fn default() -> Self {
CurrentModeText(
"Right click a node to add a wire or click on the menu to choose a node".to_string(),
)
}
}
1 change: 1 addition & 0 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod draw_systems;
pub mod place_node_sys;
pub mod place_wire_sys;
pub mod simulation_systems;
pub mod ui_systems;
2 changes: 1 addition & 1 deletion src/systems/draw_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ impl<'a> System<'a> for DrawGridSys {

fn run(&mut self, grid_mode: Self::SystemData) {
use crate::components::SNAP;
let s = 3;
let s = 4;

match *grid_mode {
GridMode::Lines => {
Expand Down
37 changes: 37 additions & 0 deletions src/systems/ui_systems.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use specs::prelude::*;

use crate::resources::{AddingNode, AddingWire, CurrentModeText};

pub struct CurrentModeSys;
impl<'a> System<'a> for CurrentModeSys {
type SystemData = (
Write<'a, CurrentModeText>,
Read<'a, AddingNode>,
Read<'a, AddingWire>,
);

fn run(&mut self, (mut current_mode, adding_node, adding_wire): Self::SystemData) {
match adding_node.0 {
Some(_) => {
current_mode.0 = "Click to place node".to_string();
return;
}
_ => {}
};

match adding_wire.0 {
Some((_, _, None, Some(_))) => {
current_mode.0 = "Right click to set wire position".to_string();
return;
}
Some((_, _, Some(_), Some(_))) => {
current_mode.0 =
"Right click a node to set wire output or left click to cancel".to_string();
return;
}
_ => {}
};

*current_mode = CurrentModeText::default();
}
}
82 changes: 46 additions & 36 deletions src/ui/top_panel.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,62 @@
use crate::resources::CurrentModeText;
use crate::resources::{self, GridMode};
use crate::ResetSys;
use crate::{components::nodes, UiSignal};
use egui::menu;
use egui::Layout;
use macroquad::prelude::*;
use specs::prelude::*;

pub fn render_top_panel(ui: &mut egui::Ui, world: &mut World) {
ui.horizontal(|ui| {
menu::menu(ui, "Nodes", |ui| {
macro_rules! node_button {
( $name:expr, $node:ident ) => {
if ui.button($name).clicked() {
world
.fetch_mut::<resources::UiSignals>()
.0
.push(UiSignal::AddNode(nodes::NodeTy::$node));
}
};
}
ui.horizontal(|ui| {
menu::menu(ui, "Nodes", |ui| {
macro_rules! node_button {
( $name:expr, $node:ident ) => {
if ui.button($name).clicked() {
world
.fetch_mut::<resources::UiSignals>()
.0
.push(UiSignal::AddNode(nodes::NodeTy::$node));
}
};
}

node_button!("Connection Node", Wire);
node_button!("On Node", OnNode);
node_button!("Off Node", OffNode);
node_button!("Not Node", NotNode);
node_button!("And Node", AndNode);
node_button!("Or Node", OrNode);
node_button!("Nand Node", NandNode);
node_button!("Nor Node", NorNode);
node_button!("Xor Node", XorNode);
node_button!("Xnor Node", XnorNode);
});
node_button!("Connection Node", Wire);
node_button!("On Node", OnNode);
node_button!("Off Node", OffNode);
node_button!("Not Node", NotNode);
node_button!("And Node", AndNode);
node_button!("Or Node", OrNode);
node_button!("Nand Node", NandNode);
node_button!("Nor Node", NorNode);
node_button!("Xor Node", XorNode);
node_button!("Xnor Node", XnorNode);
});

if ui.button("Restart Sim").clicked() || is_key_pressed(KeyCode::Space) {
ResetSys.run_now(&world);
world.insert(resources::Tick(0));
}

if ui.button("Restart Sim").clicked() || is_key_pressed(KeyCode::Space) {
ResetSys.run_now(&world);
world.insert(resources::Tick(0));
}
let mut grid_mode = *world.fetch::<GridMode>();
menu::menu(ui, "Grid Mode", |ui| {
ui.radio_value(&mut grid_mode, GridMode::CrossHatches, "Cross Hatches");
ui.radio_value(&mut grid_mode, GridMode::Lines, "Lines");
ui.radio_value(&mut grid_mode, GridMode::Dots, "Dots");
ui.radio_value(&mut grid_mode, GridMode::Off, "Off");
});
world.insert(grid_mode);

let mut grid_mode = *world.fetch::<GridMode>();
menu::menu(ui, "Grid Mode", |ui| {
ui.radio_value(&mut grid_mode, GridMode::CrossHatches, "Cross Hatches");
ui.radio_value(&mut grid_mode, GridMode::Lines, "Lines");
ui.radio_value(&mut grid_mode, GridMode::Dots, "Dots");
ui.radio_value(&mut grid_mode, GridMode::Off, "Off");
if ui.button("Remove All").clicked() {
world.delete_all();
}
});
world.insert(grid_mode);

if ui.button("Remove All").clicked() {
world.delete_all();
}
ui.with_layout(Layout::right_to_left(), |ui| {
ui.label("\t\t");
let current_mode = world.fetch::<CurrentModeText>();
ui.label(&current_mode.0);
});
});
}

0 comments on commit e4e6aa1

Please sign in to comment.