Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

add example for bevy-egui #67 #82

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ members = [
"egui_node_graph",
"egui_node_graph_example",
]
resolver = "2"
6 changes: 6 additions & 0 deletions egui_node_graph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ slotmap = { version = "1.0" }
smallvec = { version = "1.7.0" }
serde = { version = "1.0", optional = true, features = ["derive"] }
thiserror = "1.0"

[dev-dependencies]
anyhow = "1.0.68"
bevy = "0.9.1"
bevy_egui = "0.17"
eframe = "0.19.0"
67 changes: 67 additions & 0 deletions egui_node_graph/examples/bevy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
mod node_graph;

use std::collections::HashMap;

use bevy::prelude::*;
use bevy_egui::{EguiContext, EguiPlugin};
use egui::TextStyle;
use egui_node_graph::*;
use node_graph::*;

impl NodeGraphExample {
fn draw(&mut self, ctx: &egui::Context) {
egui::TopBottomPanel::top("top").show(ctx, |ui| {
egui::menu::bar(ui, |ui| {
egui::widgets::global_dark_light_mode_switch(ui);
});
});
let graph_response = egui::CentralPanel::default()
.show(ctx, |ui| {
self.state
.draw_graph_editor(ui, AllMyNodeTemplates, &mut self.user_state)
})
.inner;
for node_response in graph_response.node_responses {
// Here, we ignore all other graph events. But you may find
// some use for them. For example, by playing a sound when a new
// connection is created
if let NodeResponse::User(user_event) = node_response {
match user_event {
MyResponse::SetActiveNode(node) => self.user_state.active_node = Some(node),
MyResponse::ClearActiveNode => self.user_state.active_node = None,
}
}
}

if let Some(node) = self.user_state.active_node {
if self.state.graph.nodes.contains_key(node) {
let text = match evaluate_node(&self.state.graph, node, &mut HashMap::new()) {
Ok(value) => format!("The result is: {:?}", value),
Err(err) => format!("Execution error: {}", err),
};
ctx.debug_painter().text(
egui::pos2(10.0, 35.0),
egui::Align2::LEFT_TOP,
text,
TextStyle::Button.resolve(&ctx.style()),
egui::Color32::WHITE,
);
} else {
self.user_state.active_node = None;
}
}
}
}

fn draw_graph(mut context: ResMut<EguiContext>, mut graph: ResMut<NodeGraphExample>) {
graph.draw(context.ctx_mut());
}

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(EguiPlugin)
.init_resource::<NodeGraphExample>()
.add_system(draw_graph)
.run();
}
96 changes: 96 additions & 0 deletions egui_node_graph/examples/eframe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
mod node_graph;

use std::collections::HashMap;

use egui::TextStyle;
use egui_node_graph::NodeResponse;
use node_graph::*;

#[cfg(feature = "persistence")]
const PERSISTENCE_KEY: &str = "egui_node_graph";

#[cfg(feature = "persistence")]
impl NodeGraphExample {
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
let state = cc
.storage
.and_then(|storage| eframe::get_value(storage, PERSISTENCE_KEY))
.unwrap_or_default();

Self {
state,
user_state: MyGraphState::default(),
}
}
}

impl eframe::App for NodeGraphExample {
#[cfg(feature = "persistence")]
/// If the persistence function is enabled,
/// Called by the frame work to save state before shutdown.
fn save(&mut self, storage: &mut dyn eframe::Storage) {
eframe::set_value(storage, PERSISTENCE_KEY, &self.state);
}
/// Called each time the UI needs repainting, which may be many times per second.
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::TopBottomPanel::top("top").show(ctx, |ui| {
egui::menu::bar(ui, |ui| {
egui::widgets::global_dark_light_mode_switch(ui);
});
});
let graph_response = egui::CentralPanel::default()
.show(ctx, |ui| {
self.state
.draw_graph_editor(ui, AllMyNodeTemplates, &mut self.user_state)
})
.inner;
for node_response in graph_response.node_responses {
// Here, we ignore all other graph events. But you may find
// some use for them. For example, by playing a sound when a new
// connection is created
if let NodeResponse::User(user_event) = node_response {
match user_event {
MyResponse::SetActiveNode(node) => self.user_state.active_node = Some(node),
MyResponse::ClearActiveNode => self.user_state.active_node = None,
}
}
}

if let Some(node) = self.user_state.active_node {
if self.state.graph.nodes.contains_key(node) {
let text = match evaluate_node(&self.state.graph, node, &mut HashMap::new()) {
Ok(value) => format!("The result is: {:?}", value),
Err(err) => format!("Execution error: {}", err),
};
ctx.debug_painter().text(
egui::pos2(10.0, 35.0),
egui::Align2::LEFT_TOP,
text,
TextStyle::Button.resolve(&ctx.style()),
egui::Color32::WHITE,
);
} else {
self.user_state.active_node = None;
}
}
}
}

fn main() {
use eframe::egui::Visuals;

eframe::run_native(
"Egui node graph example",
eframe::NativeOptions::default(),
Box::new(|cc| {
cc.egui_ctx.set_visuals(Visuals::dark());
#[cfg(feature = "persistence")]
{
Box::new(NodeGraphExample::new(cc))
}
#[cfg(not(feature = "persistence"))]
Box::new(NodeGraphExample::default())
}),
);
}
Loading