From 1b09d7a2d2bb0fbfe5e7ad0523c7dd434708c5af Mon Sep 17 00:00:00 2001 From: Mikail Khan Date: Thu, 15 Apr 2021 14:17:38 -0400 Subject: [PATCH] compound node concept --- notes.md | 12 ++++++++++++ src/components.rs | 11 +++++++++-- src/main.rs | 22 ++++++++++++++++------ src/resources.rs | 5 +++++ src/scripting.rs | 34 +++++++++++++++++----------------- src/ui/top_panel.rs | 18 +++++++++++++++++- 6 files changed, 76 insertions(+), 26 deletions(-) diff --git a/notes.md b/notes.md index 266f195..12f3670 100644 --- a/notes.md +++ b/notes.md @@ -1,3 +1,15 @@ +# Compound Nodes + +Outer compound node component + +Inner compound node component + +Eventually wanna have a stack of what compound node you're editing/viewing + +Only iterate the nodes that are at the current level except for simulation, where we simulate everything lower + +Switches should be replaced by input connections + # Connection rework Right now, wire inputs are pushed to by a node, and then the outputs pull from the input, and then the next node pulls from the output. diff --git a/src/components.rs b/src/components.rs index 2fc688c..b88fc43 100644 --- a/src/components.rs +++ b/src/components.rs @@ -1,3 +1,5 @@ +use std::collections::HashSet; + use macroquad::prelude::Vec2; use specs::{prelude::*, Component}; @@ -82,7 +84,12 @@ pub struct Connection { pub index: usize, } -#[derive(Component)] +#[derive(Default, Clone, Component)] pub struct CompoundNode { - pub world: World, + pub inner: HashSet, +} + +#[derive(Clone, Component)] +pub struct InnerNode { + pub parent: Entity, } diff --git a/src/main.rs b/src/main.rs index 9d91e61..c5d1ea2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use specs::prelude::*; mod components; mod resources; -mod scripting; +// mod scripting; mod svg; mod systems; mod ui; @@ -72,6 +72,7 @@ async fn main() { world.insert(resources::CameraRes::default()); world.insert(resources::RhaiEngine::default()); world.insert(resources::RhaiScope::default()); + world.insert(resources::CreatingCompoundNode::default()); let mut prev_mouse_pos = { let (mx, my) = mouse_position(); @@ -80,12 +81,12 @@ async fn main() { let mut last_fps = [60i32; 256]; - let script: String = macroquad::file::load_string("test_scripts/basic_circuit.rhai") - .await - .unwrap(); + // let script: String = macroquad::file::load_string("test_scripts/basic_circuit.rhai") + // .await + // .unwrap(); - scripting::run_circuit_create_sys(script, &world); - world.maintain(); + // scripting::run_circuit_create_sys(script, &world); + // world.maintain(); loop { clear_background(BLACK); @@ -116,6 +117,15 @@ 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), + UiSignal::CreateNode => { + world.insert(resources::UIState::Nothing); + let compound_node = world + .create_entity() + .with(components::CompoundNode::default()) + .build(); + world.insert(resources::CreatingCompoundNode(Some(compound_node))); + } + UiSignal::SaveCompoundNode => todo!(), }); world.insert(resources::UiSignals(Vec::new())); } diff --git a/src/resources.rs b/src/resources.rs index c7e626b..57c6e3a 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -14,6 +14,9 @@ pub struct TickProgress(pub f64); #[derive(Default)] pub struct Textures(pub std::collections::BTreeMap); +#[derive(Default)] +pub struct CreatingCompoundNode(pub Option); + #[derive(Clone)] pub enum UIState { AddingNode(NodeTy), @@ -35,6 +38,8 @@ impl Default for UIState { pub enum UiSignal { AddNode(NodeTy), Delete, + CreateNode, + SaveCompoundNode, } #[derive(Default)] diff --git a/src/scripting.rs b/src/scripting.rs index 6896b47..20e6376 100644 --- a/src/scripting.rs +++ b/src/scripting.rs @@ -161,24 +161,24 @@ pub fn create_circuit(circuit: Array, world: &World) { }); } -pub fn run_circuit_create_sys(script: String, world: &World) { - use crate::all_nodes; - use crate::nodes::*; +// pub fn run_circuit_create_sys(script: String, world: &World) { +// use crate::all_nodes; +// use crate::nodes::*; - { - let engine = &world.fetch::().0; - let scope = &mut world.fetch_mut::().0; +// { +// let engine = &world.fetch::().0; +// let scope = &mut world.fetch_mut::().0; - engine.eval_with_scope::<()>(scope, &script).unwrap(); - } +// engine.eval_with_scope::<()>(scope, &script).unwrap(); +// } - macro_rules! run_sys { - ( $([$node:ident, $i:expr, $o:expr]),* $(,)? ) => { - $( - CreateScriptedCircuitSys { node: PhantomData::<$node>, node_name: stringify!($node).to_string() }.run_now(world); - )* - }; - } +// macro_rules! run_sys { +// ( $([$node:ident, $i:expr, $o:expr]),* $(,)? ) => { +// $( +// CreateScriptedCircuitSys { node: PhantomData::<$node>, node_name: stringify!($node).to_string() }.run_now(world); +// )* +// }; +// } - all_nodes!(run_sys); -} +// all_nodes!(run_sys); +// } diff --git a/src/ui/top_panel.rs b/src/ui/top_panel.rs index 6ad3afe..639c52c 100644 --- a/src/ui/top_panel.rs +++ b/src/ui/top_panel.rs @@ -1,4 +1,4 @@ -use crate::resources::{self, GridMode}; +use crate::resources::{self, CreatingCompoundNode, GridMode}; use crate::resources::{CurrentModeText, UiSignals}; use crate::ResetSys; use crate::{components::nodes, UiSignal}; @@ -56,6 +56,22 @@ pub fn render_top_panel(ui: &mut egui::Ui, world: &mut World) { if ui.button("Remove All").clicked() { world.delete_all(); } + + match world.fetch::().0 { + Some(_) => { + if ui.button("Save Compound Node").clicked() { + world + .fetch_mut::() + .0 + .push(UiSignal::SaveCompoundNode); + } + } + None => { + if ui.button("Create Node").clicked() { + world.fetch_mut::().0.push(UiSignal::CreateNode); + } + } + } }); ui.with_layout(Layout::right_to_left(), |ui| {