use crate::controller::NodeEditorController;
use std::rc::Rc;
pub struct NodeEditorSetup<F>
where
F: Fn(i32, f32, f32) + 'static,
{
controller: Rc<NodeEditorController>,
on_drag_committed: Rc<F>,
}
impl<F> NodeEditorSetup<F>
where
F: Fn(i32, f32, f32) + 'static,
{
pub fn new(on_drag_committed: F) -> Self {
Self {
controller: Rc::new(NodeEditorController::new()),
on_drag_committed: Rc::new(on_drag_committed),
}
}
pub fn controller(&self) -> &Rc<NodeEditorController> {
&self.controller
}
pub fn report_node_rect(&self) -> impl Fn(i32, f32, f32, f32, f32) + 'static {
let ctrl = self.controller.clone();
move |id, x, y, w, h| {
ctrl.handle_node_rect(id, x, y, w, h);
}
}
pub fn report_pin_position(&self) -> impl Fn(i32, i32, i32, f32, f32, bool) + 'static {
let ctrl = self.controller.clone();
move |pin_id, node_id, pin_type, x, y, hit_testable| {
ctrl.handle_pin_position_with_hit_testable(
pin_id,
node_id,
pin_type,
x,
y,
hit_testable,
);
}
}
pub fn remove_node(&self) -> impl Fn(i32) + 'static {
let ctrl = self.controller.clone();
move |node_id| {
ctrl.remove_node(node_id);
}
}
pub fn remove_pin(&self) -> impl Fn(i32) + 'static {
let ctrl = self.controller.clone();
move |pin_id| {
ctrl.remove_pin(pin_id);
}
}
pub fn reset_graph(&self) -> impl Fn() + 'static {
let ctrl = self.controller.clone();
move || ctrl.reset_graph()
}
pub fn start_node_drag(&self) -> impl Fn(i32, bool, f32, f32) + 'static {
let ctrl = self.controller.clone();
move |node_id, _already_selected, _world_x, _world_y| {
ctrl.handle_node_drag_started(node_id);
}
}
pub fn cancel_node_drag(&self) -> impl Fn() + 'static {
let ctrl = self.controller.clone();
move || ctrl.handle_node_drag_finished()
}
pub fn end_node_drag(&self) -> impl Fn(i32, f32, f32) + 'static {
let ctrl = self.controller.clone();
let on_committed = self.on_drag_committed.clone();
move |node_id, delta_x, delta_y| {
ctrl.handle_node_drag_finished();
on_committed(node_id, delta_x, delta_y);
}
}
}