Skip to content

Commit

Permalink
Ability to fetch list of selected nodes.
Browse files Browse the repository at this point in the history
Keep order of selection.

Fixes a few issues with removed nodes
  • Loading branch information
zakarumych committed Sep 17, 2024
1 parent 067ccbd commit 43a78e8
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 43 deletions.
52 changes: 49 additions & 3 deletions examples/demo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use eframe::{App, CreationContext};
use egui::{Color32, Ui};
use egui::{Color32, Id, Ui};
use egui_snarl::{
ui::{AnyPins, PinInfo, SnarlStyle, SnarlViewer, WireStyle},
InPin, InPinId, NodeId, OutPin, OutPinId, Snarl,
Expand Down Expand Up @@ -34,6 +34,16 @@ enum DemoNode {
}

impl DemoNode {
fn name(&self) -> &str {
match self {
DemoNode::Sink => "Sink",
DemoNode::Number(_) => "Number",
DemoNode::String(_) => "String",
DemoNode::ShowImage(_) => "ShowImage",
DemoNode::ExprNode(_) => "ExprNode",
}
}

fn number_out(&self) -> f64 {
match self {
DemoNode::Number(value) => *value,
Expand Down Expand Up @@ -927,6 +937,7 @@ impl Expr {
pub struct DemoApp {
snarl: Snarl<DemoNode>,
style: SnarlStyle,
snarl_ui_id: Option<Id>,
}

impl DemoApp {
Expand All @@ -953,7 +964,7 @@ impl DemoApp {
};
// let style = SnarlStyle::new();

DemoApp { snarl, style }
DemoApp { snarl, style, snarl_ui_id: None }
}
}

Expand Down Expand Up @@ -987,9 +998,44 @@ impl App for DemoApp {
});
});

if let Some(snarl_ui_id) = self.snarl_ui_id {
egui::SidePanel::right("selected-list").show(ctx, |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
ui.strong("Selected nodes");

let selected = Snarl::<DemoNode>::get_selected_nodes_at("snarl", snarl_ui_id, ui.ctx());
let mut selected = selected
.into_iter()
.map(|id| (id, &self.snarl[id]))
.collect::<Vec<_>>();

selected.sort_by_key(|(id, _)| *id);

let mut remove = None;

for (id, node) in selected {
ui.horizontal(|ui| {
ui.label(format!("{:?}", id));
ui.label(node.name());
ui.add_space(ui.spacing().item_spacing.x);
if ui.button("Remove").clicked() {
remove = Some(id);
}
});
}

if let Some(id) = remove {
self.snarl.remove_node(id);
}
});
});
}

egui::CentralPanel::default().show(ctx, |ui| {
self.snarl_ui_id = Some(ui.id());

self.snarl
.show(&mut DemoViewer, &self.style, egui::Id::new("snarl"), ui);
.show(&mut DemoViewer, &self.style, "snarl", ui);
});
}

Expand Down
26 changes: 17 additions & 9 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,10 @@ impl<T> Snarl<T> {
let mut node_rects = Vec::new();

for node_idx in draw_order {
if !self.nodes.contains(node_idx.0) {
continue;
}

// show_node(node_idx);
let response = self.draw_node(
ui,
Expand Down Expand Up @@ -941,24 +945,28 @@ impl<T> Snarl<T> {
ui.advance_cursor_after_rect(Rect::from_min_size(viewport.min, Vec2::ZERO));

if let Some(node) = node_to_top {
ui.ctx().request_repaint();
snarl_state.node_to_top(node);
if self.nodes.contains(node.0) {
ui.ctx().request_repaint();
snarl_state.node_to_top(node);
}
}

if let Some((node, delta)) = node_moved {
ui.ctx().request_repaint();
if snarl_state.selected_nodes().contains(&node) {
for node in snarl_state.selected_nodes() {
if self.nodes.contains(node.0) {
ui.ctx().request_repaint();
if snarl_state.selected_nodes().contains(&node) {
for node in snarl_state.selected_nodes() {
let node = &mut self.nodes[node.0];
node.pos += delta;
}
} else {
let node = &mut self.nodes[node.0];
node.pos += delta;
}
} else {
let node = &mut self.nodes[node.0];
node.pos += delta;
}
}

snarl_state.store(ui.ctx());
snarl_state.store(self, ui.ctx());
});
}

Expand Down
Loading

0 comments on commit 43a78e8

Please sign in to comment.