Skip to content

Commit

Permalink
feat: renamed and refactored custom_draw
Browse files Browse the repository at this point in the history
  • Loading branch information
blitzarx1 committed Sep 12, 2024
1 parent b075df2 commit 27052b9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 22 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "custom_draw"
name = "animated_nodes"
version = "0.1.0"
authors = ["Dmitrii Samsonov <[email protected]>"]
license = "MIT"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Custom draw
# Animated nodes
Example demonstrates how to use custom drawing functions nodes, handle animation state and access `node` payload in the drawing function.

Nodes are drawn as squares with labels in their center. Node is rotating when it is in the state of dragging. It is rotating clockwise if the `node` payload has `clockwise` field setting set to `true` and vice-verse.

## run
```bash
cargo run --release -p custom_draw
cargo run --release -p animated_nodes
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use eframe::{run_native, App, CreationContext};
use egui::Context;
use egui_graphs::{DefaultEdgeShape, Graph, GraphView, SettingsInteraction, SettingsNavigation};
use egui_graphs::{
default_edge_transform, default_node_transform, to_graph_custom, DefaultEdgeShape, Graph,
GraphView, SettingsInteraction, SettingsNavigation,
};
use node::NodeShapeAnimated;
use petgraph::{
stable_graph::{DefaultIx, StableGraph},
Expand All @@ -9,18 +12,33 @@ use petgraph::{

mod node;

pub struct CustomDrawApp {
const GLYPH_CLOCKWISE: &str = "↻";
const GLYPH_ANTICLOCKWISE: &str = "↺";

pub struct AnimatedNodesApp {
g: Graph<node::NodeData, (), Directed, DefaultIx, NodeShapeAnimated>,
}

impl CustomDrawApp {
impl AnimatedNodesApp {
fn new(_: &CreationContext<'_>) -> Self {
let g = generate_graph();
Self { g: Graph::from(&g) }
Self {
g: to_graph_custom(
&g,
|idx, n| {
if n.clockwise {
default_node_transform(idx, n).with_label(GLYPH_CLOCKWISE.to_string())
} else {
default_node_transform(idx, n).with_label(GLYPH_ANTICLOCKWISE.to_string())
}
},
default_edge_transform,
),
}
}
}

impl App for CustomDrawApp {
impl App for AnimatedNodesApp {
fn update(&mut self, ctx: &Context, _: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.add(
Expand Down Expand Up @@ -58,9 +76,9 @@ fn generate_graph() -> StableGraph<node::NodeData, ()> {
fn main() {
let native_options = eframe::NativeOptions::default();
run_native(
"egui_graphs_custom_draw_demo",
"egui_graphs_animated_nodes_demo",
native_options,
Box::new(|cc| Ok(Box::new(CustomDrawApp::new(cc)))),
Box::new(|cc| Ok(Box::new(AnimatedNodesApp::new(cc)))),
)
.unwrap();
}
File renamed without changes.
4 changes: 2 additions & 2 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ impl<
De: DisplayEdge<N, E, Ty, Ix, Dn>,
> From<&StableGraph<N, E, Ty, Ix>> for Graph<N, E, Ty, Ix, Dn, De>
{
fn from(value: &StableGraph<N, E, Ty, Ix>) -> Self {
transform::to_graph(value)
fn from(g: &StableGraph<N, E, Ty, Ix>) -> Self {
transform::to_graph(g)
}
}

Expand Down

0 comments on commit 27052b9

Please sign in to comment.