setzer22 / egui_node_graph

Build your node graph applications in Rust, using egui
MIT License
715 stars 134 forks source link

Visual Enhancement: Bezier Curves #2

Closed DrRuhe closed 2 years ago

DrRuhe commented 2 years ago

Hi,

first of all, this library is a great starting point for anyone wanting to do nodegraphs. So thanks for releasing it!

The Editor currently uses straight lines to connect the nodes. But Bezier Curves look so much nicer :)

Luckily egui is adding support for cubic bezier curves. But since this is sadly only in their git, I am doing this writeup so once it ships, the implementation is straightforward.

Here I have linked a demo on how bezier curves can be set up to look like they do in other editors. You can move the endpoints to see how it feels. Notably, the control points are half the distance (s in the demo) away from the endpoints, but on the same y coordinate.

So, what has to be done here in the node graph?

In egui_node_graph/src/editor_ui.rs; Instead of:

painter.line_segment([src_pos, dst_pos], connection_stroke);

Something like:

let s = 0.5 * src_pos.distance(dst_pos);

let control_pos1 = pos2(src_pos.x + s, src_pos.y);
let control_pos2 = pos2(src_pos.x - s, src_pos.y);

painter.add(Shape::CubicBezier(CubicBezierShape {
    fill: Color32::TRANSPARENT,
    points:[src_pos,control_pos1,control_pos2,dst_pos],
    closed: false,
    stroke: connection_stroke,
}));
setzer22 commented 2 years ago

That's really great! I was thinking I would need to implement bezier curves myself. Very glad to know egui is adding support for them :)

You already seem to have most of it figured out, so a PR would be very much welcome! We can keep a "bezier" branch with a git dependency on egui.

If you don't have time for it, no worries! I will get around to doing this eventually 👍

gmorenz commented 2 years ago

I've implemented this in this commit: https://github.com/setzer22/egui_node_graph/commit/572c999c6465efea1869d63e948a61bd3b55cbf4.

Merging back to main is blocked on updating egui (#23).

setzer22 commented 2 years ago

Hi, many thanks for your work! I haven't had a lot of time to put into my open source work lately. I'll check this out as soon as I can :smile:

setzer22 commented 2 years ago

Hi again @gmorenz! Your changes look good. I took care of merging all the open PRs, including #23. Would you like to submit a PR for this? I can take your commit as a patch but I'd rather have you listed as the author of the commit :+1:

gmorenz commented 2 years ago

Opened a PR, fwiw I'm pretty sure if you just take a commit and merge it with git it will properly attribute authorship, but it's probably cleaner to do it via github anyways