Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bevy support? #124

Closed
dmyyy opened this issue Nov 19, 2023 · 6 comments · Fixed by #126
Closed

Bevy support? #124

dmyyy opened this issue Nov 19, 2023 · 6 comments · Fixed by #126
Labels
question Further information is requested

Comments

@dmyyy
Copy link
Contributor

dmyyy commented Nov 19, 2023

Is eframe required for GraphView to work? I'm trying to get this to work via bevy_egui - a central panel is created but I don't see a graph. I'll do some digging on my own but any additional info would be appreciated.

@dmyyy
Copy link
Contributor Author

dmyyy commented Nov 19, 2023

Seems to work on the basic example - problem must be on my end. Made a pr for an example

#125

@dmyyy
Copy link
Contributor Author

dmyyy commented Nov 19, 2023

Would appreciate some input on a very odd issue.

In the sample code I wrote above (that works) - the StableGraph is

generate_graph: StableGraph { Ty: "Directed", node_count: 2, edge_count: 1, edges: (0, 1), node weights: {0: Properties(Null), 1: Properties(Null)}, edge weights: {0: Properties(Null)}, free_node: NodeIndex(65535), free_edge: EdgeIndex(65535) }

In my personal code I create the stable graph async. Before creating the egui_graphs::Graph it looks like this:

on_graph_created: StableGraph { Ty: "Directed", node_count: 2, edge_count: 1, edges: (0, 1), node weights: {0: Properties(Null), 1: Properties(Null)}, edge weights: {0: Properties(Null)}, free_node: NodeIndex(65535), free_edge: EdgeIndex(65535) }

The two stable graphs are identical. After adding them to egui_graphs they're also identical

Sample code egui_graphs::Graph

graph: Graph { g: StableGraph { Ty: "Directed", node_count: 2, edge_count: 1, edges: (0, 1), node weights: {0: Node { id: Some(NodeIndex(0)), location: Some([61.0 168.6]), payload: Properties(Null), label: "0", selected: false, dragged: false }, 1: Node { id: Some(NodeIndex(1)), location: Some([87.7 239.0]), payload: Properties(Null), label: "1", selected: false, dragged: false }}, edge weights: {0: Edge { id: Some(EdgeID { idx: EdgeIndex(0), order: 0 }), payload: Properties(Null), selected: false }}, free_node: NodeIndex(65535), free_edge: EdgeIndex(65535) } }

My code that doesn't work egui_graphs::Graph

egui_graph: Graph { g: StableGraph { Ty: "Directed", node_count: 2, edge_count: 1, edges: (0, 1), node weights: {0: Node { id: Some(NodeIndex(0)), location: Some([37.3 145.3]), payload: Properties(Null), label: "0", selected: false, dragged: false }, 1: Node { id: Some(NodeIndex(1)), location: Some([220.9 31.1]), payload: Properties(Null), label: "1", selected: false, dragged: false }}, edge weights: {0: Edge { id: Some(EdgeID { idx: EdgeIndex(0), order: 0 }), payload: Properties(Null), selected: false }}, free_node: NodeIndex(65535), free_edge: EdgeIndex(65535) } }

However for my code a central panel appears but no graph appears. GraphView::new() is running every frame.

#[derive(Component)]
pub struct EguiGraph(egui_graphs::Graph<Properties, Properties, Directed, u16>);

impl Default for EguiGraph {
    fn default() -> Self {
        Self(egui_graphs::Graph::new(StableGraph::default()))
    }
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(EntropyPlugin::<WyRand>::new())
        .add_plugins(ProcGenPlugin)
        .add_plugins(EguiPlugin)
        .add_systems(Update, (on_gen_map_button, graph_update))
        .add_systems(Update, on_graph_created)
        .run();
}

fn on_gen_map_button(
    mut commands: Commands,
    mut contexts: EguiContexts,
    mut gen_event_ew: EventWriter<GenEvent>,
) {
    let ctx = contexts.ctx_mut();

    egui::Window::new("grew_ui").show(ctx, |ui| {
        if ui.button("Generate Map").clicked() {
            let entity = commands.spawn(EguiGraph::default());

            let map_config = MapConfig { difficulty: 1 };

            gen_event_ew.send(GenEvent {
                entity: entity.id(),
                kind: GenKind::Map(map_config),
            });
        }
    });
}

fn graph_update(mut contexts: EguiContexts, mut q_egui_graph: Query<&mut EguiGraph>) {
    let ctx = contexts.ctx_mut();

    if let Ok(mut egui_graph) = q_egui_graph.get_single_mut() {
        println!("egui_graph: {:?}", egui_graph.0);

        egui::CentralPanel::default().show(ctx, |ui| {
            ui.add(&mut GraphView::<
                _,
                _,
                _,
                _,
                egui_graphs::DefaultNodeShape,
                egui_graphs::DefaultEdgeShape<_>,
            >::new(&mut egui_graph.0));
        });
    }
}

fn on_graph_created(mut q_graphs: Query<(&mut EguiGraph, &DiGraph), Added<DiGraph>>) {
    for (mut egui_graph, digraph) in q_graphs.iter_mut() {
        println!("on_graph_created: {:?}", digraph.0);

        egui_graph.0 = egui_graphs::Graph::from(&digraph.0);
    }
}

Any ideas?

@blitzarx1 blitzarx1 added the question Further information is requested label Nov 19, 2023
@blitzarx1
Copy link
Owner

blitzarx1 commented Nov 19, 2023

Try initializing GraphView with navigation settings and set fit_to_screen_enabled to false.

        ui.add(
            &mut GraphView::<_, _, _, _, DefaultNodeShape, DefaultEdgeShape<_>>::new(
                &mut graph_provider.g,
            )
            .with_navigations(
                &SettingsNavigation::default()
                    .with_fit_to_screen_enabled(false)
            ),
        )

There is a know issue #116 which can cause automatic fit_to_screen to perform bad if the root ui is relative.

@blitzarx1 blitzarx1 added bug Something isn't working and removed bug Something isn't working labels Nov 19, 2023
@blitzarx1
Copy link
Owner

blitzarx1 commented Nov 19, 2023

I also added example with multiple panels #125, you can play with it to check the behavior. Let me finish with #123 and I ll deail with #116 to fix this.

@blitzarx1 blitzarx1 linked a pull request Nov 19, 2023 that will close this issue
@dmyyy
Copy link
Contributor Author

dmyyy commented Nov 19, 2023

Issue is particularly bad if you initialize egui_graphs::Graph to StableGraph::default() and then update it to another value afterwards. I wasn't able to find where the graph was no matter how much zooming/panning I did - even though the position values look reasonable which is the real bug here I think.

Take your time - setting the initial value to some simple graph works okay as a workaround for now.

@dmyyy
Copy link
Contributor Author

dmyyy commented Nov 19, 2023

Bevy support seems to work - closing this and opening: #127 for the specific bug I ran into

@dmyyy dmyyy closed this as completed Nov 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants