use crate::controller::NodeEditorController;
use crate::groups::Dragged;
use std::cell::RefCell;
use std::rc::Rc;
type GroupDragCommit = Rc<dyn Fn(i32, f32, f32)>;
pub struct NodeEditorSetup<F>
where
F: Fn(i32, f32, f32) + 'static,
{
controller: Rc<NodeEditorController>,
on_drag_committed: Rc<F>,
on_group_drag_committed: Rc<RefCell<Option<GroupDragCommit>>>,
}
impl NodeEditorSetup<Box<dyn Fn(i32, f32, f32)>> {
pub fn with_drag_commit(on_committed: impl Fn(Dragged, f32, f32) + 'static) -> Self {
let on_committed = Rc::new(on_committed);
let setup = Self::new({
let on_committed = on_committed.clone();
Box::new(move |node_id, dx, dy| on_committed(Dragged::Node(node_id), dx, dy))
as Box<dyn Fn(i32, f32, f32)>
});
setup.on_group_drag_committed(move |group_id, dx, dy| {
on_committed(Dragged::Group(group_id), dx, dy)
});
setup
}
}
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),
on_group_drag_committed: Rc::new(RefCell::new(None)),
}
}
pub fn on_group_drag_committed(&self, on_committed: impl Fn(i32, f32, f32) + 'static) {
*self.on_group_drag_committed.borrow_mut() = Some(Rc::new(on_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);
}
}
pub fn report_group_rect(&self) -> impl Fn(i32, f32, f32, f32, f32) + 'static {
let ctrl = self.controller.clone();
move |id, x, y, w, h| {
ctrl.handle_group_rect(id, x, y, w, h);
}
}
pub fn remove_group(&self) -> impl Fn(i32) + 'static {
let ctrl = self.controller.clone();
move |group_id| {
ctrl.remove_group(group_id);
}
}
pub fn set_node_hidden(&self) -> impl Fn(i32, bool) + 'static {
let ctrl = self.controller.clone();
move |node_id, hidden| {
ctrl.set_node_hidden(node_id, hidden);
}
}
pub fn start_group_drag(&self) -> impl Fn(i32, f32, f32) + 'static {
let ctrl = self.controller.clone();
move |group_id, _world_x, _world_y| {
ctrl.handle_group_drag_started(group_id);
}
}
pub fn end_group_drag(&self) -> impl Fn(i32, f32, f32) + 'static {
let ctrl = self.controller.clone();
let on_committed = self.on_group_drag_committed.clone();
move |group_id, delta_x, delta_y| {
ctrl.handle_node_drag_finished();
let commit = on_committed.borrow().clone();
if let Some(commit) = commit {
commit(group_id, delta_x, delta_y);
}
}
}
}