blitzarx1 / egui_graphs

Interactive graph visualization widget for rust powered by egui and petgraph
https://docs.rs/crate/egui_graphs
MIT License
388 stars 29 forks source link

What is the best way access the Node payload in DisplayNode.update()? #159

Closed RobertoMaurizzi closed 7 months ago

RobertoMaurizzi commented 7 months ago

Hi! I'm trying to implement something to work with graphs so I can see/debug a "cyclic dungeon generation" algorithm I'd like to write for a Bevy game I'm planning. I'm still quite new to Rust so I might be missing something very obvious 😅 I created a "MapNode" type containing a few fields to represent what the Node is supposed to be (i.e. the entrance, a regular room, the treasure room the boss room, etc.) then created a Graph<MapNode, MapEdge, , , MapNodeShape> to display (with MapNodeShape based on your rotating node example).

However, to display a useful node with for example room type, room difficulty, how can I access these data in a DisplayNode trait implementation for a NodeShape renderer implementation? I've seen they're in the payload (adding Debug to N allow me to print my MapNode struct) but then how do I access it from code? payload inside NodeProps.state is a generic type N implementing Clone and I get a type mismatch.

What's the best way access the Node payload when drawing a Node?

blitzarx1 commented 7 months ago

Hi! I tired to demonstrate access to node payload inside custom drawing example (#160). I suppose this is the easiest way to have access to it and stay generic.

RobertoMaurizzi commented 7 months ago

Wow, thank you so much!

I actually went on by myself this afternoon (UTC+8) and got to understand all the things I had to do so that Rust can make sense of the typing and it was VERY instructive!

I ended up doing practically the same (I implemented a trait that access the payload and returns it with the proper type) but I had the ui part and the node part in 2 different files part of a separate module and I got to fight with rustc --explain E0116 and had to wrap my original type in the file that manages the animation, declare the trait, implement it for my original type in its own file and the new wrapped type... and it worked! (and I did learn a lot about Rust in the process, so double win! 🎉)

image

Thanks again!

blitzarx1 commented 7 months ago

Glad you get it done!