feat: Updating and socket logic
This commit is contained in:
parent
7de9ce4a5e
commit
502a15273f
2 changed files with 49 additions and 4 deletions
32
src/node.rs
32
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<Node>,
|
||||
pub sockets: Vec<Socket>
|
||||
pub sockets: Vec<Socket>,
|
||||
pub connections: Vec<SocketConnection>,
|
||||
pub current_drag_socket: Option<usize>,
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue