From 47297817f0c19c1c8a58520fa268f156cd2a4321 Mon Sep 17 00:00:00 2001 From: starlord Date: Thu, 18 Jul 2024 21:35:08 +0300 Subject: [PATCH] Default label value + demo refactor (#191) - default label value changed from `id` for nodes and edges to `node ` or `edge ` - extracted drawers and settings into separate modules --- examples/demo/src/drawers.rs | 134 +++++++++++ examples/demo/src/main.rs | 436 +++++++++++++--------------------- examples/demo/src/settings.rs | 20 +- src/graph.rs | 6 +- src/transform.rs | 12 +- 5 files changed, 328 insertions(+), 280 deletions(-) create mode 100644 examples/demo/src/drawers.rs diff --git a/examples/demo/src/drawers.rs b/examples/demo/src/drawers.rs new file mode 100644 index 0000000..bbaca98 --- /dev/null +++ b/examples/demo/src/drawers.rs @@ -0,0 +1,134 @@ +use egui::Ui; + +pub struct ValuesConfigButtonsStartReset { + pub simulation_stopped: bool, +} + +pub fn draw_start_reset_buttons( + ui: &mut egui::Ui, + mut values: ValuesConfigButtonsStartReset, + mut on_change: impl FnMut(bool, bool), +) { + ui.vertical(|ui| { + ui.label("Stop or start simulation again or reset to default settings."); + ui.horizontal(|ui| { + let start_simulation_stopped = values.simulation_stopped; + if ui + .button(match values.simulation_stopped { + true => "start", + false => "stop", + }) + .clicked() + { + values.simulation_stopped = !values.simulation_stopped; + }; + + let mut reset_pressed = false; + if ui.button("reset").clicked() { + reset_pressed = true; + } + + if start_simulation_stopped != values.simulation_stopped || reset_pressed { + on_change(values.simulation_stopped, reset_pressed); + } + }); + }); +} + +pub struct ValuesSectionDebug { + pub zoom: f32, + pub pan: [f32; 2], + pub fps: f32, +} + +pub fn draw_section_debug(ui: &mut egui::Ui, values: ValuesSectionDebug) { + ui.label(format!("zoom: {:.5}", values.zoom)); + ui.label(format!("pan: [{:.5}, {:.5}]", values.pan[0], values.pan[1])); + ui.label(format!("FPS: {:.1}", values.fps)); +} + +pub struct ValuesConfigSlidersGraph { + pub node_cnt: usize, + pub edge_cnt: usize, +} + +pub fn draw_counts_sliders( + ui: &mut egui::Ui, + mut values: ValuesConfigSlidersGraph, + mut on_change: impl FnMut(i32, i32), +) { + let start_node_cnt = values.node_cnt; + let mut delta_node_cnt = 0; + ui.horizontal(|ui| { + if ui + .add(egui::Slider::new(&mut values.node_cnt, 1..=2500).text("nodes")) + .changed() + { + delta_node_cnt = values.node_cnt as i32 - start_node_cnt as i32; + }; + }); + + let start = values.edge_cnt; + let mut delta_edge_cnt = 0; + ui.horizontal(|ui| { + if ui + .add(egui::Slider::new(&mut values.edge_cnt, 1..=2500).text("edges")) + .changed() + { + delta_edge_cnt = values.edge_cnt as i32 - start as i32; + }; + }); + + if delta_node_cnt != 0 || delta_edge_cnt != 0 { + on_change(delta_node_cnt, delta_edge_cnt) + }; +} + +pub struct ValuesConfigSlidersSimulation { + pub dt: f32, + pub cooloff_factor: f32, + pub scale: f32, +} + +pub fn draw_simulation_config_sliders( + ui: &mut Ui, + mut values: ValuesConfigSlidersSimulation, + mut on_change: impl FnMut(f32, f32, f32), +) { + let start_dt = values.dt; + let mut delta_dt = 0.; + ui.horizontal(|ui| { + if ui + .add(egui::Slider::new(&mut values.dt, 0.00..=1.).text("dt")) + .changed() + { + delta_dt = values.dt - start_dt; + }; + }); + + let start_cooloff_factor = values.cooloff_factor; + let mut delta_cooloff_factor = 0.; + ui.horizontal(|ui| { + if ui + .add(egui::Slider::new(&mut values.cooloff_factor, 0.00..=1.).text("cooloff_factor")) + .changed() + { + delta_cooloff_factor = values.cooloff_factor - start_cooloff_factor; + }; + }); + + let start_scale = values.scale; + let mut delta_scale = 0.; + ui.horizontal(|ui| { + if ui + .add(egui::Slider::new(&mut values.scale, 1.0..=1000.).text("scale")) + .changed() + { + delta_scale = values.scale - start_scale; + }; + }); + + if delta_dt != 0. || delta_cooloff_factor != 0. || delta_scale != 0. { + on_change(delta_dt, delta_cooloff_factor, delta_scale); + } +} diff --git a/examples/demo/src/main.rs b/examples/demo/src/main.rs index 9de42ed..fc91d5d 100644 --- a/examples/demo/src/main.rs +++ b/examples/demo/src/main.rs @@ -1,8 +1,9 @@ use std::time::Instant; use crossbeam::channel::{unbounded, Receiver, Sender}; +use drawers::ValuesSectionDebug; use eframe::{run_native, App, CreationContext}; -use egui::{CollapsingHeader, Context, Pos2, ScrollArea, Slider, Ui, Vec2}; +use egui::{CollapsingHeader, Context, Pos2, ScrollArea, Ui, Vec2}; use egui_graphs::events::Event; use egui_graphs::{to_graph, DefaultEdgeShape, DefaultNodeShape, Edge, Graph, GraphView, Node}; use fdg::fruchterman_reingold::{FruchtermanReingold, FruchtermanReingoldConfiguration}; @@ -12,6 +13,9 @@ use petgraph::stable_graph::{DefaultIx, EdgeIndex, NodeIndex, StableGraph}; use petgraph::Directed; use rand::Rng; +mod drawers; +mod settings; + const EVENTS_LIMIT: usize = 100; pub struct DemoApp { @@ -19,43 +23,36 @@ pub struct DemoApp { sim: ForceGraph, Edge<(), ()>>, force: FruchtermanReingold, - settings_simulation: SettingsSimulation, + settings_simulation: settings::SettingsSimulation, - settings_graph: SettingsGraph, - settings_interaction: SettingsInteraction, - settings_navigation: SettingsNavigation, - settings_style: SettingsStyle, + settings_graph: settings::SettingsGraph, + settings_interaction: settings::SettingsInteraction, + settings_navigation: settings::SettingsNavigation, + settings_style: settings::SettingsStyle, last_events: Vec, simulation_stopped: bool, - fps: f64, + fps: f32, last_update_time: Instant, frames_last_time_span: usize, event_publisher: Sender, event_consumer: Receiver, - pan: Option<[f32; 2]>, - zoom: Option, + pan: [f32; 2], + zoom: f32, } impl DemoApp { fn new(_: &CreationContext<'_>) -> Self { - let settings_graph = SettingsGraph::default(); - let settings_simulation = SettingsSimulation::default(); + let settings_graph = settings::SettingsGraph::default(); + let settings_simulation = settings::SettingsSimulation::default(); let mut g = generate_random_graph(settings_graph.count_node, settings_graph.count_edge); - let mut force = FruchtermanReingold { - conf: FruchtermanReingoldConfiguration { - dt: settings_simulation.dt, - cooloff_factor: settings_simulation.cooloff_factor, - scale: settings_simulation.scale, - }, - ..Default::default() - }; + let mut force = init_force(&settings_simulation); let mut sim = fdg::init_force_graph_uniform(g.g.clone(), 1.0); force.apply(&mut sim); g.g.node_weights_mut().for_each(|node| { @@ -77,9 +74,9 @@ impl DemoApp { settings_graph, settings_simulation, - settings_interaction: SettingsInteraction::default(), - settings_navigation: SettingsNavigation::default(), - settings_style: SettingsStyle::default(), + settings_interaction: settings::SettingsInteraction::default(), + settings_navigation: settings::SettingsNavigation::default(), + settings_style: settings::SettingsStyle::default(), last_events: Vec::default(), @@ -89,8 +86,8 @@ impl DemoApp { last_update_time: Instant::now(), frames_last_time_span: 0, - pan: Option::default(), - zoom: Option::default(), + pan: [0., 0.], + zoom: 0., } } @@ -104,7 +101,7 @@ impl DemoApp { } /// sync locations computed by the simulation with egui_graphs::Graph nodes. - fn sync_graph_with_simulation(&mut self) { + fn sync(&mut self) { self.g.g.node_weights_mut().for_each(|node| { let sim_computed_point: OPoint> = self.sim.node_weight(node.id()).unwrap().1; @@ -121,7 +118,7 @@ impl DemoApp { let elapsed = now.duration_since(self.last_update_time); if elapsed.as_secs() >= 1 { self.last_update_time = now; - self.fps = self.frames_last_time_span as f64 / elapsed.as_secs_f64(); + self.fps = self.frames_last_time_span as f32 / elapsed.as_secs_f32(); self.frames_last_time_span = 0; } } @@ -134,24 +131,8 @@ impl DemoApp { self.last_events.push(serde_json::to_string(&e).unwrap()); match e { - Event::Pan(payload) => match self.pan { - Some(pan) => { - self.pan = Some([pan[0] + payload.diff[0], pan[1] + payload.diff[1]]); - } - None => { - self.pan = Some(payload.diff); - } - }, - Event::Zoom(z) => { - match self.zoom { - Some(zoom) => { - self.zoom = Some(zoom + z.diff); - } - None => { - self.zoom = Some(z.diff); - } - }; - } + Event::Pan(payload) => self.pan = payload.new_pan, + Event::Zoom(payload) => self.zoom = payload.new_zoom, Event::NodeMove(payload) => { let node_id = NodeIndex::new(payload.id); @@ -254,56 +235,85 @@ impl DemoApp { } fn draw_section_simulation(&mut self, ui: &mut Ui) { - CollapsingHeader::new("Simulation") - .default_open(true) - .show(ui, |ui| { - ui.horizontal_wrapped(|ui| { - ui.style_mut().spacing.item_spacing = Vec2::new(0., 0.); - ui.label("Force-Directed Simulation is done with "); - ui.hyperlink_to("fdg project", "https://github.com/grantshandy/fdg"); - }); + ui.horizontal_wrapped(|ui| { + ui.style_mut().spacing.item_spacing = Vec2::new(0., 0.); + ui.label("Force-Directed Simulation is done with "); + ui.hyperlink_to("fdg project", "https://github.com/grantshandy/fdg"); + }); - ui.separator(); - ui.add_space(10.); - - ui.label("Config"); - ui.separator(); - - ui.horizontal(|ui| { - if ui - .button(match self.simulation_stopped { - true => "start", - false => "stop", - }) - .clicked() - { - self.simulation_stopped = !self.simulation_stopped; - }; - if ui.button("reset").clicked() { - self.reset(); - } - }); + ui.separator(); - ui.add_space(10.); + drawers::draw_start_reset_buttons( + ui, + drawers::ValuesConfigButtonsStartReset { + simulation_stopped: self.simulation_stopped, + }, + |simulation_stopped: bool, reset_pressed: bool| { + self.simulation_stopped = simulation_stopped; + if reset_pressed { + self.reset() + }; + }, + ); - self.draw_simulation_config_sliders(ui); - ui.add_space(10.); - ui.separator(); - self.draw_counts_sliders(ui); + ui.add_space(10.); - ui.add_space(10.); + drawers::draw_simulation_config_sliders( + ui, + drawers::ValuesConfigSlidersSimulation { + dt: self.settings_simulation.dt, + cooloff_factor: self.settings_simulation.cooloff_factor, + scale: self.settings_simulation.scale, + }, + |delta_dt: f32, delta_cooloff_factor: f32, delta_scale: f32| { + self.settings_simulation.dt += delta_dt; + self.settings_simulation.cooloff_factor += delta_cooloff_factor; + self.settings_simulation.scale += delta_scale; - ui.separator(); - }); + self.force = init_force(&self.settings_simulation); + }, + ); + + ui.add_space(10.); + + drawers::draw_counts_sliders( + ui, + drawers::ValuesConfigSlidersGraph { + node_cnt: self.settings_graph.count_node, + edge_cnt: self.settings_graph.count_edge, + }, + |delta_nodes, delta_edges| { + self.settings_graph.count_node += delta_nodes as usize; + self.settings_graph.count_edge += delta_edges as usize; + + if delta_nodes != 0 { + if delta_nodes > 0 { + (0..delta_nodes).for_each(|_| self.add_random_node()); + } else { + (0..delta_nodes.abs()).for_each(|_| self.remove_random_node()); + } + } + + if delta_edges != 0 { + if delta_edges > 0 { + (0..delta_edges).for_each(|_| self.add_random_edge()); + } else { + (0..delta_edges.abs()).for_each(|_| self.remove_random_edge()); + } + } + }, + ); } fn draw_section_widget(&mut self, ui: &mut Ui) { - CollapsingHeader::new("Widget") - .default_open(true) - .show(ui, |ui| { - CollapsingHeader::new("Navigation").default_open(true).show(ui, |ui|{ + CollapsingHeader::new("Navigation") + .default_open(true) + .show(ui, |ui| { if ui - .checkbox(&mut self.settings_navigation.fit_to_screen_enabled, "fit_to_screen") + .checkbox( + &mut self.settings_navigation.fit_to_screen_enabled, + "fit_to_screen", + ) .changed() && self.settings_navigation.fit_to_screen_enabled { @@ -315,18 +325,23 @@ impl DemoApp { ui.add_enabled_ui(!self.settings_navigation.fit_to_screen_enabled, |ui| { ui.vertical(|ui| { - ui.checkbox(&mut self.settings_navigation.zoom_and_pan_enabled, "zoom_and_pan"); + ui.checkbox( + &mut self.settings_navigation.zoom_and_pan_enabled, + "zoom_and_pan", + ); ui.label("Zoom with ctrl + mouse wheel, pan with middle mouse drag."); - }).response.on_disabled_hover_text("disable fit_to_screen to enable zoom_and_pan"); + }) + .response + .on_disabled_hover_text("disable fit_to_screen to enable zoom_and_pan"); }); }); - CollapsingHeader::new("Style").show(ui, |ui| { - ui.checkbox(&mut self.settings_style.labels_always, "labels_always"); - ui.label("Wheter to show labels always or when interacted only."); - }); + CollapsingHeader::new("Style").show(ui, |ui| { + ui.checkbox(&mut self.settings_style.labels_always, "labels_always"); + ui.label("Wheter to show labels always or when interacted only."); + }); - CollapsingHeader::new("Interaction").show(ui, |ui| { + CollapsingHeader::new("Interaction").show(ui, |ui| { if ui.checkbox(&mut self.settings_interaction.dragging_enabled, "dragging_enabled").clicked() && self.settings_interaction.dragging_enabled { self.settings_interaction.node_clicking_enabled = true; }; @@ -385,129 +400,56 @@ impl DemoApp { ui.label("Enable multiselect to select multiple edges."); }); - CollapsingHeader::new("Selected").default_open(true).show(ui, |ui| { - ScrollArea::vertical().auto_shrink([false, true]).max_height(200.).show(ui, |ui| { - self.g.selected_nodes().iter().for_each(|node| { - ui.label(format!("{node:?}")); - }); - self.g.selected_edges().iter().for_each(|edge| { - ui.label(format!("{edge:?}")); + CollapsingHeader::new("Selected") + .default_open(true) + .show(ui, |ui| { + ScrollArea::vertical() + .auto_shrink([false, true]) + .max_height(200.) + .show(ui, |ui| { + self.g.selected_nodes().iter().for_each(|node| { + ui.label(format!("{node:?}")); + }); + self.g.selected_edges().iter().for_each(|edge| { + ui.label(format!("{edge:?}")); + }); }); - }); }); - CollapsingHeader::new("Last Events").default_open(true).show(ui, |ui| { + CollapsingHeader::new("Last Events") + .default_open(true) + .show(ui, |ui| { if ui.button("clear").clicked() { self.last_events.clear(); } - ScrollArea::vertical().auto_shrink([false, true]).show(ui, |ui| { - self.last_events.iter().rev().for_each(|event| { - ui.label(event); + ScrollArea::vertical() + .auto_shrink([false, true]) + .show(ui, |ui| { + self.last_events.iter().rev().for_each(|event| { + ui.label(event); + }); }); - }); - }); - }); - } - - fn draw_section_debug(&mut self, ui: &mut Ui) { - CollapsingHeader::new("Debug") - .default_open(true) - .show(ui, |ui| { - if let Some(zoom) = self.zoom { - ui.label(format!("zoom: {:.5}", zoom)); - }; - if let Some(pan) = self.pan { - ui.label(format!("pan: [{:.5}, {:.5}]", pan[0], pan[1])); - }; - - ui.label(format!("FPS: {:.1}", self.fps)); - }); - } - - fn draw_counts_sliders(&mut self, ui: &mut Ui) { - ui.horizontal(|ui| { - let before = self.settings_graph.count_node as i32; - - ui.add(Slider::new(&mut self.settings_graph.count_node, 1..=2500).text("nodes")); - - let delta = self.settings_graph.count_node as i32 - before; - (0..delta.abs()).for_each(|_| { - if delta > 0 { - self.add_random_node(); - return; - }; - self.remove_random_node(); - }); - }); - - ui.horizontal(|ui| { - let before = self.settings_graph.count_edge as i32; - - ui.add(Slider::new(&mut self.settings_graph.count_edge, 0..=5000).text("edges")); - - let delta = self.settings_graph.count_edge as i32 - before; - (0..delta.abs()).for_each(|_| { - if delta > 0 { - self.add_random_edge(); - return; - }; - self.remove_random_edge(); }); - }); } - fn draw_simulation_config_sliders(&mut self, ui: &mut Ui) { - let mut changed = false; - - ui.horizontal(|ui| { - let resp = ui.add(Slider::new(&mut self.settings_simulation.dt, 0.00..=1.).text("dt")); - if resp.changed() { - changed = true - } - }); - ui.horizontal(|ui| { - let resp = ui.add( - Slider::new(&mut self.settings_simulation.cooloff_factor, 0.0..=1.) - .text("cooloff_factor"), - ); - if resp.changed() { - changed = true - } - }); - ui.horizontal(|ui| { - let resp = - ui.add(Slider::new(&mut self.settings_simulation.scale, 0.0..=300.).text("scale")); - if resp.changed() { - changed = true - } - }); - - if changed { - self.force = FruchtermanReingold { - conf: FruchtermanReingoldConfiguration { - dt: self.settings_simulation.dt, - cooloff_factor: self.settings_simulation.cooloff_factor, - scale: self.settings_simulation.scale, - }, - ..Default::default() - }; - } + fn draw_section_debug(&self, ui: &mut Ui) { + drawers::draw_section_debug( + ui, + ValuesSectionDebug { + zoom: self.zoom, + pan: self.pan, + fps: self.fps, + }, + ); } fn reset(&mut self) { - let settings_graph = SettingsGraph::default(); - let settings_simulation = SettingsSimulation::default(); + let settings_graph = settings::SettingsGraph::default(); + let settings_simulation = settings::SettingsSimulation::default(); let mut g = generate_random_graph(settings_graph.count_node, settings_graph.count_edge); - let mut force = FruchtermanReingold { - conf: FruchtermanReingoldConfiguration { - dt: settings_simulation.dt, - cooloff_factor: settings_simulation.cooloff_factor, - scale: settings_simulation.scale, - }, - ..Default::default() - }; + let mut force = init_force(&self.settings_simulation); let mut sim = fdg::init_force_graph_uniform(g.g.clone(), 1.0); force.apply(&mut sim); g.g.node_weights_mut().for_each(|node| { @@ -518,6 +460,7 @@ impl DemoApp { self.settings_simulation = settings_simulation; self.settings_graph = settings_graph; + self.sim = sim; self.g = g; self.force = force; @@ -530,11 +473,21 @@ impl App for DemoApp { .min_width(250.) .show(ctx, |ui| { ScrollArea::vertical().show(ui, |ui| { - self.draw_section_simulation(ui); + CollapsingHeader::new("Simulation") + .default_open(true) + .show(ui, |ui| self.draw_section_simulation(ui)); + ui.add_space(10.); - self.draw_section_debug(ui); + + egui::CollapsingHeader::new("Debug") + .default_open(true) + .show(ui, |ui| self.draw_section_debug(ui)); + ui.add_space(10.); - self.draw_section_widget(ui); + + CollapsingHeader::new("Widget") + .default_open(true) + .show(ui, |ui| self.draw_section_widget(ui)); }); }); @@ -567,7 +520,7 @@ impl App for DemoApp { }); self.handle_events(); - self.sync_graph_with_simulation(); + self.sync(); self.update_simulation(); self.update_fps(); } @@ -577,12 +530,10 @@ fn generate_random_graph(node_count: usize, edge_count: usize) -> Graph<(), ()> let mut rng = rand::thread_rng(); let mut graph = StableGraph::new(); - // add nodes for _ in 0..node_count { graph.add_node(()); } - // add random edges for _ in 0..edge_count { let source = rng.gen_range(0..node_count); let target = rng.gen_range(0..node_count); @@ -593,6 +544,17 @@ fn generate_random_graph(node_count: usize, edge_count: usize) -> Graph<(), ()> to_graph(&graph) } +fn init_force(settings: &settings::SettingsSimulation) -> FruchtermanReingold { + FruchtermanReingold { + conf: FruchtermanReingoldConfiguration { + dt: settings.dt, + cooloff_factor: settings.cooloff_factor, + scale: settings.scale, + }, + ..Default::default() + } +} + fn main() { let native_options = eframe::NativeOptions::default(); run_native( @@ -602,67 +564,3 @@ fn main() { ) .unwrap(); } - -struct SettingsSimulation { - dt: f32, - cooloff_factor: f32, - scale: f32, -} - -impl Default for SettingsSimulation { - fn default() -> Self { - Self { - dt: 0.03, - cooloff_factor: 0.7, - scale: 100., - } - } -} - -struct SettingsGraph { - pub count_node: usize, - pub count_edge: usize, -} - -impl Default for SettingsGraph { - fn default() -> Self { - Self { - count_node: 25, - count_edge: 50, - } - } -} - -#[derive(Default)] -struct SettingsInteraction { - pub dragging_enabled: bool, - pub node_clicking_enabled: bool, - pub node_selection_enabled: bool, - pub node_selection_multi_enabled: bool, - pub edge_clicking_enabled: bool, - pub edge_selection_enabled: bool, - pub edge_selection_multi_enabled: bool, -} - -struct SettingsNavigation { - pub fit_to_screen_enabled: bool, - pub zoom_and_pan_enabled: bool, - pub screen_padding: f32, - pub zoom_speed: f32, -} - -impl Default for SettingsNavigation { - fn default() -> Self { - Self { - screen_padding: 0.3, - zoom_speed: 0.1, - fit_to_screen_enabled: true, - zoom_and_pan_enabled: false, - } - } -} - -#[derive(Default)] -struct SettingsStyle { - pub labels_always: bool, -} diff --git a/examples/demo/src/settings.rs b/examples/demo/src/settings.rs index 863ee0a..0322ba6 100644 --- a/examples/demo/src/settings.rs +++ b/examples/demo/src/settings.rs @@ -6,8 +6,8 @@ pub struct SettingsGraph { impl Default for SettingsGraph { fn default() -> Self { Self { - count_node: 300, - count_edge: 500, + count_node: 25, + count_edge: 50, } } } @@ -45,3 +45,19 @@ impl Default for SettingsNavigation { pub struct SettingsStyle { pub labels_always: bool, } + +pub struct SettingsSimulation { + pub dt: f32, + pub cooloff_factor: f32, + pub scale: f32, +} + +impl Default for SettingsSimulation { + fn default() -> Self { + Self { + dt: 0.03, + cooloff_factor: 0.85, + scale: 100., + } + } +} diff --git a/src/graph.rs b/src/graph.rs index c98c5f8..927719b 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -118,7 +118,7 @@ impl< let graph_node = self.g.node_weight_mut(idx).unwrap(); graph_node.bind(idx, location); - graph_node.set_label(idx.index().to_string()); + graph_node.set_label(format!("node {}", idx.index())); idx } @@ -178,7 +178,7 @@ impl< removed } - /// Adds edge between start and end node with default label setting correct order. + /// Adds edge between start and end node with default label. pub fn add_edge( &mut self, start: NodeIndex, @@ -191,7 +191,7 @@ impl< let e = self.g.edge_weight_mut(idx).unwrap(); e.bind(idx, order); - e.set_label(e.id().index().to_string()); + e.set_label(format!("edge {}", e.id().index())); idx } diff --git a/src/transform.rs b/src/transform.rs index e765f2c..522196e 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -125,8 +125,8 @@ pub fn add_edge_custom< /// /// assert_eq!(*input_graph.g.edge_weight(input_graph.g.edge_indices().next().unwrap()).unwrap().payload(), "edge1"); /// -/// assert_eq!(*input_graph.g.node_weight(input_node_1).unwrap().label().clone(), input_node_1.index().to_string()); -/// assert_eq!(*input_graph.g.node_weight(input_node_2).unwrap().label().clone(), input_node_2.index().to_string()); +/// assert_eq!(*input_graph.g.node_weight(input_node_1).unwrap().label().clone(), format!("node {}", input_node_1.index())); +/// assert_eq!(*input_graph.g.node_weight(input_node_2).unwrap().label().clone(), format!("node {}", input_node_2.index())); /// /// let loc_1 = input_graph.g.node_weight(input_node_1).unwrap().location(); /// let loc_2 = input_graph.g.node_weight(input_node_2).unwrap().location(); @@ -175,7 +175,7 @@ pub fn default_node_transform< payload: &N, ) -> Node { let mut n = Node::new(payload.clone()); - n.set_label(idx.index().to_string()); + n.set_label(format!("node {}", idx.index())); let loc = random_location(DEFAULT_SPAWN_SIZE); n.bind(idx, loc); @@ -197,7 +197,7 @@ pub fn default_edge_transform< ) -> Edge { let mut e = Edge::new(payload.clone()); e.bind(idx, order); - e.set_label(e.id().index().to_string()); + e.set_label(format!("edge {}", e.id().index())); e } @@ -280,7 +280,7 @@ mod tests { assert!(input_n.location().x >= 0.0 && input_n.location().x <= DEFAULT_SPAWN_SIZE); assert!(input_n.location().y >= 0.0 && input_n.location().y <= DEFAULT_SPAWN_SIZE); - assert_eq!(*input_n.label(), user_idx.index().to_string()); + assert_eq!(*input_n.label(), format!("node {}", user_idx.index())); assert!(!input_n.selected()); assert!(!input_n.dragged()); @@ -309,7 +309,7 @@ mod tests { assert!(input_n.location().x >= 0.0 && input_n.location().x <= DEFAULT_SPAWN_SIZE); assert!(input_n.location().y >= 0.0 && input_n.location().y <= DEFAULT_SPAWN_SIZE); - assert_eq!(*input_n.label(), user_idx.index().to_string()); + assert_eq!(*input_n.label(), format!("node {}", user_idx.index())); assert!(!input_n.selected()); assert!(!input_n.dragged());