Skip to content

Commit

Permalink
compound node concept
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhan45 committed Apr 15, 2021
1 parent 8ee22d7 commit 1b09d7a
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 26 deletions.
12 changes: 12 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
11 changes: 9 additions & 2 deletions src/components.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use macroquad::prelude::Vec2;
use specs::{prelude::*, Component};

Expand Down Expand Up @@ -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<Entity>,
}

#[derive(Clone, Component)]
pub struct InnerNode {
pub parent: Entity,
}
22 changes: 16 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use specs::prelude::*;

mod components;
mod resources;
mod scripting;
// mod scripting;
mod svg;
mod systems;
mod ui;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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()));
}
Expand Down
5 changes: 5 additions & 0 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub struct TickProgress(pub f64);
#[derive(Default)]
pub struct Textures(pub std::collections::BTreeMap<String, Texture2D>);

#[derive(Default)]
pub struct CreatingCompoundNode(pub Option<Entity>);

#[derive(Clone)]
pub enum UIState {
AddingNode(NodeTy),
Expand All @@ -35,6 +38,8 @@ impl Default for UIState {
pub enum UiSignal {
AddNode(NodeTy),
Delete,
CreateNode,
SaveCompoundNode,
}

#[derive(Default)]
Expand Down
34 changes: 17 additions & 17 deletions src/scripting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<RhaiEngine>().0;
let scope = &mut world.fetch_mut::<RhaiScope>().0;
// {
// let engine = &world.fetch::<RhaiEngine>().0;
// let scope = &mut world.fetch_mut::<RhaiScope>().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);
// }
18 changes: 17 additions & 1 deletion src/ui/top_panel.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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::<CreatingCompoundNode>().0 {
Some(_) => {
if ui.button("Save Compound Node").clicked() {
world
.fetch_mut::<UiSignals>()
.0
.push(UiSignal::SaveCompoundNode);
}
}
None => {
if ui.button("Create Node").clicked() {
world.fetch_mut::<UiSignals>().0.push(UiSignal::CreateNode);
}
}
}
});

ui.with_layout(Layout::right_to_left(), |ui| {
Expand Down

0 comments on commit 1b09d7a

Please sign in to comment.