Skip to content

Commit

Permalink
refactored layers drawing mechanism (#180)
Browse files Browse the repository at this point in the history
Solves #179
  • Loading branch information
blitzarx1 authored Feb 19, 2024
1 parent 8278e8c commit 113570d
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions src/draw/drawer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

use egui::{Context, Id, LayerId, Order, Painter};
use egui::{Context, Painter, Shape};
use petgraph::graph::IndexType;
use petgraph::EdgeType;

Expand All @@ -26,10 +26,9 @@ where
Nd: DisplayNode<N, E, Ty, Ix>,
Ed: DisplayEdge<N, E, Ty, Ix, Nd>,
{
layer_top: Painter,
layer_bot: Painter,
ctx: &'a DrawContext<'a>,
g: &'a mut Graph<N, E, Ty, Ix, Nd, Ed>,
postponed: Vec<Shape>,
_marker: PhantomData<(Nd, Ed)>,
}

Expand All @@ -43,29 +42,27 @@ where
Ed: DisplayEdge<N, E, Ty, Ix, Nd>,
{
pub fn new(g: &'a mut Graph<N, E, Ty, Ix, Nd, Ed>, ctx: &'a DrawContext<'a>) -> Self {
let layer_top = ctx
.painter
.clone()
.with_layer_id(LayerId::new(Order::Foreground, Id::new("top")));
let layer_bot = ctx
.painter
.clone()
.with_layer_id(LayerId::new(Order::Middle, Id::new("bot")));
Drawer {
layer_top,
layer_bot,
ctx,
g,
postponed: Vec::new(),
_marker: PhantomData,
}
}

pub fn draw(mut self) {
self.draw_edges();
self.fill_layers_nodes();
self.draw_nodes();
self.draw_postponed();
}

fn fill_layers_nodes(&mut self) {
fn draw_postponed(&mut self) {
self.postponed.iter().for_each(|s| {
self.ctx.painter.add(s.clone());
});
}

fn draw_nodes(&mut self) {
self.g
.g
.node_indices()
Expand All @@ -81,11 +78,11 @@ where

if n.selected() || n.dragged() {
shapes.into_iter().for_each(|s| {
self.layer_top.add(s);
self.postponed.push(s);
});
} else {
shapes.into_iter().for_each(|s| {
self.layer_bot.add(s);
self.ctx.painter.add(s);
});
}
});
Expand Down Expand Up @@ -113,11 +110,11 @@ where

if e.selected() {
shapes.into_iter().for_each(|s| {
self.layer_top.add(s);
self.postponed.push(s);
});
} else {
shapes.into_iter().for_each(|s| {
self.layer_bot.add(s);
self.ctx.painter.add(s);
});
}
});
Expand Down

0 comments on commit 113570d

Please sign in to comment.