From 502a15273f8159d1e51e9ee9b349e66afea0afb4 Mon Sep 17 00:00:00 2001 From: Rendo Date: Mon, 10 Aug 2026 04:44:11 +0500 Subject: [PATCH] feat: Updating and socket logic --- src/node.rs | 32 +++++++++++++++++++++++++++++--- src/socket.rs | 21 ++++++++++++++++++++- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/node.rs b/src/node.rs index 0cc027c..51681ba 100644 --- a/src/node.rs +++ b/src/node.rs @@ -1,20 +1,46 @@ use iced::{Element, Point}; -use crate::socket::{Socket,SocketData}; +use crate::socket::{Socket, SocketConnection, SocketData}; #[derive(Default)] pub struct Nodes { pub nodes: Vec, - pub sockets: Vec + pub sockets: Vec, + pub connections: Vec, + pub current_drag_socket: Option, } impl Nodes { pub fn update(&mut self, message: NodeMessage) { - todo!(); + match message { + NodeMessage::MoveNode(node, point) => self.nodes[node].position = point, + NodeMessage::SocketDragStart(socket) => { + self.current_drag_socket = Some(socket); + }, + NodeMessage::SocketDragEnd(socket) => { + if let Some(first_socket) = self.current_drag_socket { + if self.sockets[first_socket].compatable(&self.sockets[socket]){ + self.toggle_connection(first_socket, socket); + } + self.current_drag_socket = None; + } + }, + } } pub fn view(&self) -> Element<'_, crate::Message> { todo!(); } + + pub fn toggle_connection(&mut self, from: usize, to: usize) { + let connection = SocketConnection(from, to); + let found = self.connections.iter().position(|el| *el == connection); + if let Some(found_index) = found { + self.connections.remove(found_index); + } + else { + self.connections.push(connection); + } + } } pub enum NodeMessage { diff --git a/src/socket.rs b/src/socket.rs index d162f74..c9d5dbe 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -4,12 +4,31 @@ pub struct Socket { pub data: SocketData } +impl Socket { + pub fn compatable(&self, to: &Socket) -> bool { + self.data.compatable(&to.data) + } +} + pub enum SocketType { Input, Output, } pub enum SocketData { - F32(f32) + F32(f32), + Bool(bool), } +impl SocketData { + pub fn compatable(&self, to: &SocketData) -> bool { + match (self, to) { + (SocketData::F32(_), SocketData::F32(_)) => true, + (SocketData::Bool(_), SocketData::Bool(_)) => true, + _ => false, + } + } +} + +#[derive(Clone,Copy, PartialEq, Eq)] +pub struct SocketConnection(pub usize, pub usize);