Skip to content

Commit

Permalink
Changed elements api & fixed looped edge draw (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
blitzarx1 authored Jun 7, 2023
1 parent a84cbae commit 6b5bda1
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 118 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "egui_graphs"
version = "0.5.3"
version = "0.5.4-beta.0"
authors = ["Dmitrii Samsonov <[email protected]>"]
license = "MIT"
homepage = "https://github.com/blitzarx1/egui_graphs"
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "basic"
version = "0.5.3"
version = "0.5.4-beta.0"
authors = ["Dmitrii Samsonov <[email protected]>"]
license = "MIT"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion examples/basic_interactive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "basic_interactive"
version = "0.5.3"
version = "0.5.4-beta.0"
authors = ["Dmitrii Samsonov <[email protected]>"]
license = "MIT"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion examples/basic_undirected/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "basic_undirected"
version = "0.5.3"
version = "0.5.4-beta.0"
authors = ["Dmitrii Samsonov <[email protected]>"]
license = "MIT"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion examples/configurable/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "configurable"
version = "0.5.3"
version = "0.5.4-beta.0"
authors = ["Dmitrii Samsonov <[email protected]>"]
license = "MIT"
edition = "2021"
Expand Down
12 changes: 6 additions & 6 deletions examples/configurable/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,16 @@ impl ConfigurableApp {
let g_n = self.g.node_weight_mut(*g_n_idx).unwrap();
let sim_n = self.sim.get_graph_mut().node_weight_mut(*g_n_idx).unwrap();

if g_n.dragged {
let loc = g_n.location;
if g_n.dragged() {
let loc = g_n.location();
sim_n.location = Vec3::new(loc.x, loc.y, 0.);
return;
}

let loc = sim_n.location;
g_n.location = Vec2::new(loc.x, loc.y);
g_n.set_location(Vec2::new(loc.x, loc.y));

if g_n.selected {
if g_n.selected() {
self.selected_nodes.push(g_n.clone());
}
});
Expand Down Expand Up @@ -233,8 +233,8 @@ impl ConfigurableApp {
// location of new node is in surrounging of random existing node
let mut rng = rand::thread_rng();
let location = Vec2::new(
random_n.location.x + 10. + rng.gen_range(0. ..50.),
random_n.location.y + 10. + rng.gen_range(0. ..50.),
random_n.location().x + 10. + rng.gen_range(0. ..50.),
random_n.location().y + 10. + rng.gen_range(0. ..50.),
);

let idx = self.g.add_node(Node::new(location, ()));
Expand Down
96 changes: 51 additions & 45 deletions src/drawer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
collections::HashMap,
f32::{MAX, MIN},
f32::{consts::PI, MAX, MIN},
};

use egui::{
Expand Down Expand Up @@ -70,27 +70,27 @@ impl<'a, N: Clone, E: Clone, Ty: EdgeType> Drawer<'a, N, E, Ty> {
// we shall account for the node radius
// so that the node is fully visible

let x_minus_rad = n.location.x - comp_node.radius(self.meta);
let x_minus_rad = n.location().x - comp_node.radius(self.meta);
if x_minus_rad < min_x {
min_x = x_minus_rad;
};

let y_minus_rad = n.location.y - comp_node.radius(self.meta);
let y_minus_rad = n.location().y - comp_node.radius(self.meta);
if y_minus_rad < min_y {
min_y = y_minus_rad;
};

let x_plus_rad = n.location.x + comp_node.radius(self.meta);
let x_plus_rad = n.location().x + comp_node.radius(self.meta);
if x_plus_rad > max_x {
max_x = x_plus_rad;
};

let y_plus_rad = n.location.y + comp_node.radius(self.meta);
let y_plus_rad = n.location().y + comp_node.radius(self.meta);
if y_plus_rad > max_y {
max_y = y_plus_rad;
};

if n.dragged {
if n.dragged() {
new_dragged = Some(idx);
}

Expand Down Expand Up @@ -221,27 +221,33 @@ impl<'a, N: Clone, E: Clone, Ty: EdgeType> Drawer<'a, N, E, Ty> {
return vec![];
}

let pos_start_and_end = n.location.to_pos2();
let loop_size = comp_node.radius(self.meta) * (4. + 1. + order as f32);
let center_horizont_angle = PI / 4.;
let center = n.location();
let y_intersect = center.y - comp_node.radius(self.meta) * center_horizont_angle.sin();

let control_point1 = Pos2::new(
pos_start_and_end.x + loop_size,
pos_start_and_end.y - loop_size,
let left_intersect = Pos2::new(
center.x - comp_node.radius(self.meta) * center_horizont_angle.cos(),
y_intersect,
);
let control_point2 = Pos2::new(
pos_start_and_end.x - loop_size,
pos_start_and_end.y - loop_size,
let right_intersect = Pos2::new(
center.x + comp_node.radius(self.meta) * center_horizont_angle.cos(),
y_intersect,
);

let stroke = Stroke::new(e.width, self.settings_style.color_edge(self.p.ctx(), e));
let loop_size = comp_node.radius(self.meta) * (4. + 1. + order as f32);

let control_point1 = Pos2::new(center.x + loop_size, center.y - loop_size);
let control_point2 = Pos2::new(center.x - loop_size, center.y - loop_size);

let stroke = Stroke::new(e.width(), self.settings_style.color_edge(self.p.ctx(), e));
let shape_basic = CubicBezierShape::from_points_stroke(
[
pos_start_and_end,
right_intersect,
control_point1,
control_point2,
pos_start_and_end,
left_intersect,
],
true,
false,
Color32::TRANSPARENT,
stroke,
);
Expand All @@ -254,17 +260,17 @@ impl<'a, N: Clone, E: Clone, Ty: EdgeType> Drawer<'a, N, E, Ty> {
let mut shapes = vec![shape_basic];

let highlighted_stroke = Stroke::new(
e.width * 2.,
e.width() * 2.,
self.settings_style.color_edge_highlight(comp_edge).unwrap(),
);
shapes.push(CubicBezierShape::from_points_stroke(
[
pos_start_and_end,
right_intersect,
control_point1,
control_point2,
pos_start_and_end,
left_intersect,
],
true,
false,
Color32::TRANSPARENT,
highlighted_stroke,
));
Expand All @@ -286,7 +292,7 @@ impl<'a, N: Clone, E: Clone, Ty: EdgeType> Drawer<'a, N, E, Ty> {
let mut end_node = self.g.node(*end_idx).unwrap();
let mut transparent = false;

if (start_node.folded || comp_start.subfolded()) && comp_end.subfolded() {
if (start_node.folded() || comp_start.subfolded()) && comp_end.subfolded() {
return (vec![], vec![]);
}

Expand Down Expand Up @@ -320,8 +326,8 @@ impl<'a, N: Clone, E: Clone, Ty: EdgeType> Drawer<'a, N, E, Ty> {
transparent = true;
}

let pos_start = start_node.screen_transform(self.meta).location.to_pos2();
let pos_end = end_node.screen_transform(self.meta).location.to_pos2();
let pos_start = start_node.screen_transform(self.meta).location().to_pos2();
let pos_end = end_node.screen_transform(self.meta).location().to_pos2();

let vec = pos_end - pos_start;
let l = vec.length();
Expand All @@ -339,13 +345,13 @@ impl<'a, N: Clone, E: Clone, Ty: EdgeType> Drawer<'a, N, E, Ty> {
if transparent {
color = color.gamma_multiply(0.15);
}
let stroke = Stroke::new(e.width, color);
let stroke = Stroke::new(e.width(), color);

// draw straight edge
if order == 0 {
let mut shapes = vec![];
let head_point_1 = tip_point - e.tip_size * rotate_vector(dir, e.tip_angle);
let head_point_2 = tip_point - e.tip_size * rotate_vector(dir, -e.tip_angle);
let head_point_1 = tip_point - e.tip_size() * rotate_vector(dir, e.tip_angle());
let head_point_2 = tip_point - e.tip_size() * rotate_vector(dir, -e.tip_angle());

shapes.push(Shape::line_segment([start_point, tip_point], stroke));

Expand All @@ -364,7 +370,7 @@ impl<'a, N: Clone, E: Clone, Ty: EdgeType> Drawer<'a, N, E, Ty> {
}

let highlighted_stroke = Stroke::new(
e.width * 2.,
e.width() * 2.,
self.settings_style.color_edge_highlight(comp_edge).unwrap(),
);
shapes.push(Shape::line_segment(
Expand All @@ -391,14 +397,14 @@ impl<'a, N: Clone, E: Clone, Ty: EdgeType> Drawer<'a, N, E, Ty> {
let dir_perpendicular = Vec2::new(-dir.y, dir.x);
let center_point = (start_point + tip_point.to_vec2()).to_vec2() / 2.0;
let control_point =
(center_point + dir_perpendicular * e.curve_size * order as f32).to_pos2();
(center_point + dir_perpendicular * e.curve_size() * order as f32).to_pos2();

let tip_vec = control_point - tip_point;
let tip_dir = tip_vec / tip_vec.length();
let tip_size = e.tip_size;
let tip_size = e.tip_size();

let arrow_tip_dir_1 = rotate_vector(tip_dir, e.tip_angle) * tip_size;
let arrow_tip_dir_2 = rotate_vector(tip_dir, -e.tip_angle) * tip_size;
let arrow_tip_dir_1 = rotate_vector(tip_dir, e.tip_angle()) * tip_size;
let arrow_tip_dir_2 = rotate_vector(tip_dir, -e.tip_angle()) * tip_size;

let head_point_1 = tip_point + arrow_tip_dir_1;
let head_point_2 = tip_point + arrow_tip_dir_2;
Expand All @@ -424,7 +430,7 @@ impl<'a, N: Clone, E: Clone, Ty: EdgeType> Drawer<'a, N, E, Ty> {
}

let highlighted_stroke = Stroke::new(
e.width * 2.,
e.width() * 2.,
self.settings_style.color_edge_highlight(comp_edge).unwrap(),
);
quadratic_shapes.push(QuadraticBezierShape::from_points_stroke(
Expand All @@ -451,7 +457,7 @@ impl<'a, N: Clone, E: Clone, Ty: EdgeType> Drawer<'a, N, E, Ty> {
comp_node: &StateComputedNode,
) -> (Vec<CircleShape>, Vec<TextShape>) {
let node = &n.screen_transform(self.meta);
let loc = node.location.to_pos2();
let loc = node.location().to_pos2();

let (mut circles, mut texts) = self.draw_node_basic(loc, node, comp_node);
let (circles_interacted, texts_interacted) =
Expand All @@ -463,10 +469,10 @@ impl<'a, N: Clone, E: Clone, Ty: EdgeType> Drawer<'a, N, E, Ty> {

fn shape_label(&self, node_radius: f32, n: &Node<N>) -> Option<TextShape> {
let color_label = self.settings_style.color_label(self.p.ctx());
let label_pos = Pos2::new(n.location.x, n.location.y - node_radius * 2.);
let label_pos = Pos2::new(n.location().x, n.location().y - node_radius * 2.);
let label_size = node_radius;
let galley = self.p.layout_no_wrap(
n.label.as_ref()?.clone(),
n.label()?.clone(),
FontId::new(label_size, FontFamily::Monospace),
color_label,
);
Expand All @@ -482,10 +488,10 @@ impl<'a, N: Clone, E: Clone, Ty: EdgeType> Drawer<'a, N, E, Ty> {
) -> (Vec<CircleShape>, Vec<TextShape>) {
let color = self.settings_style.color_node(self.p.ctx(), node);
let mut nodes = vec![];
if !(node.selected
if !(node.selected()
|| comp_node.subselected()
|| node.dragged
|| node.folded
|| node.dragged()
|| node.folded()
|| comp_node.subfolded())
{
// draw not interacted nodes in place
Expand All @@ -507,7 +513,7 @@ impl<'a, N: Clone, E: Clone, Ty: EdgeType> Drawer<'a, N, E, Ty> {
return (nodes, vec![]);
}

if node.folded || comp_node.subfolded() {
if node.folded() || comp_node.subfolded() {
return (nodes, vec![]);
}

Expand All @@ -530,10 +536,10 @@ impl<'a, N: Clone, E: Clone, Ty: EdgeType> Drawer<'a, N, E, Ty> {
node: &Node<N>,
comp_node: &StateComputedNode,
) -> (Vec<CircleShape>, Vec<TextShape>) {
if !(node.selected
if !(node.selected()
|| comp_node.subselected()
|| node.dragged
|| node.folded
|| node.dragged()
|| node.folded()
|| comp_node.subfolded())
{
return (vec![], vec![]);
Expand All @@ -553,7 +559,7 @@ impl<'a, N: Clone, E: Clone, Ty: EdgeType> Drawer<'a, N, E, Ty> {
texts.push(label_shape);
};

if node.folded {
if node.folded() {
shape = CircleShape::stroke(
loc,
node_radius,
Expand Down
Loading

0 comments on commit 6b5bda1

Please sign in to comment.