This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
695 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ members = [ | |
"egui_node_graph", | ||
"egui_node_graph_example", | ||
] | ||
resolver = "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}), | ||
); | ||
} |
Oops, something went wrong.