2023-06-10 19:13:16 +02:00
|
|
|
class_name Deck
|
|
|
|
|
|
|
|
var nodes: Dictionary
|
|
|
|
|
|
|
|
enum Types{
|
|
|
|
ERROR = -1,
|
|
|
|
BOOL,
|
|
|
|
NUMERIC,
|
|
|
|
STRING,
|
|
|
|
ARRAY,
|
|
|
|
DICTIONARY,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static var type_assoc: Dictionary = {
|
|
|
|
Types.ERROR: DeckType.DeckTypeError,
|
|
|
|
Types.BOOL: DeckType.DeckTypeBool,
|
|
|
|
Types.NUMERIC: DeckType.DeckTypeNumeric,
|
|
|
|
Types.STRING: DeckType.DeckTypeString,
|
|
|
|
Types.ARRAY: DeckType.DeckTypeArray,
|
|
|
|
Types.DICTIONARY: DeckType.DeckTypeDictionary,
|
|
|
|
}
|
|
|
|
|
2023-06-13 17:23:10 +02:00
|
|
|
var variable_stack: Dictionary = {}
|
|
|
|
|
2023-06-10 19:13:16 +02:00
|
|
|
|
|
|
|
func add_node(node: GDScript, meta: Dictionary = {}) -> DeckNode:
|
|
|
|
# TODO: accept instances of DeckNode instead of instancing here?
|
|
|
|
var uuid := UUID.v4()
|
|
|
|
var node_inst: DeckNode = node.new() as DeckNode
|
|
|
|
nodes[uuid] = node_inst
|
|
|
|
node_inst._belonging_to = self
|
|
|
|
node_inst._id = uuid
|
|
|
|
|
|
|
|
if !meta.is_empty():
|
|
|
|
for k in meta:
|
|
|
|
node_inst.set_meta(k, meta[k])
|
|
|
|
|
|
|
|
return node_inst
|
|
|
|
|
|
|
|
|
2023-07-21 10:10:24 +02:00
|
|
|
func add_node_inst(node: DeckNode, assign_id: String = "", assign_to_self: bool = true) -> DeckNode:
|
|
|
|
if assign_to_self:
|
|
|
|
node._belonging_to = self
|
|
|
|
|
|
|
|
if assign_id == "":
|
|
|
|
var uuid := UUID.v4()
|
|
|
|
nodes[uuid] = node
|
|
|
|
node._id = uuid
|
|
|
|
else:
|
|
|
|
nodes[assign_id] = node
|
|
|
|
|
2023-06-24 05:39:50 +02:00
|
|
|
|
|
|
|
# if !meta.is_empty():
|
|
|
|
# for k in meta:
|
|
|
|
# node.set_meta(k, meta[k])
|
|
|
|
|
|
|
|
return node
|
|
|
|
|
|
|
|
|
2023-06-10 19:13:16 +02:00
|
|
|
func get_node(uuid: String) -> DeckNode:
|
|
|
|
return nodes.get(uuid)
|
|
|
|
|
|
|
|
|
|
|
|
func connect_nodes(from_node: DeckNode, to_node: DeckNode, from_port: int, to_port: int) -> bool:
|
|
|
|
# first, check that we can do the type conversion.
|
2023-06-12 17:59:30 +02:00
|
|
|
var type_a: Types = from_node.ports[from_port].type
|
|
|
|
var type_b: Types = to_node.ports[to_port].type
|
2023-06-11 17:39:11 +02:00
|
|
|
var err: DeckType = (type_assoc[type_b]).from(type_assoc[type_a].new())
|
2023-06-10 19:13:16 +02:00
|
|
|
if err is DeckType.DeckTypeError:
|
|
|
|
print(err.error_message)
|
|
|
|
return false
|
|
|
|
|
|
|
|
# TODO: prevent duplicate connections
|
|
|
|
|
|
|
|
from_node.add_outgoing_connection(from_port, to_node._id, to_port)
|
|
|
|
return true
|
2023-06-11 18:26:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
func disconnect_nodes(from_node: DeckNode, to_node: DeckNode, from_port: int, to_port: int) -> void:
|
|
|
|
var hash = {to_node._id: to_port}.hash()
|
2023-07-21 11:12:20 +02:00
|
|
|
# prints({to_node._id: to_port}, "-", {to_node._id: to_port}.hash())
|
|
|
|
# prints(from_node.outgoing_connections[0][0], "-", from_node.outgoing_connections[0][0].hash())
|
|
|
|
#
|
|
|
|
# var a = {to_node._id: to_port}
|
|
|
|
# var b = from_node.outgoing_connections[0][0]
|
|
|
|
#
|
|
|
|
# print(typeof(a.values()[0]))
|
|
|
|
# print(typeof(b.values()[0]))
|
|
|
|
#
|
|
|
|
# prints(a, b)
|
|
|
|
|
2023-06-11 18:26:12 +02:00
|
|
|
from_node.remove_outgoing_connection(from_port, hash)
|
2023-07-21 11:12:20 +02:00
|
|
|
to_node.remove_incoming_connection(to_port)
|
2023-06-24 05:39:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
func to_json(with_meta: bool = true) -> String:
|
2023-07-21 10:10:24 +02:00
|
|
|
var inner := {"nodes": {}, "variable_stack": variable_stack}
|
2023-06-24 05:39:50 +02:00
|
|
|
|
|
|
|
for id in nodes.keys():
|
|
|
|
inner["nodes"][id] = nodes[id].to_dict(with_meta)
|
|
|
|
|
|
|
|
var d := {"deck": inner}
|
|
|
|
|
|
|
|
if with_meta:
|
|
|
|
d["meta"] = {}
|
|
|
|
for meta in get_meta_list():
|
2023-08-08 08:54:17 +02:00
|
|
|
d["meta"][meta] = var_to_str(get_meta(meta))
|
2023-06-24 05:39:50 +02:00
|
|
|
return JSON.stringify(d, "\t", false)
|
2023-07-21 10:10:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
static func from_json(json: String) -> Deck:
|
|
|
|
var data: Dictionary = JSON.parse_string(json) as Dictionary
|
|
|
|
var deck := Deck.new()
|
|
|
|
|
|
|
|
deck.variable_stack = data.deck.variable_stack
|
|
|
|
|
|
|
|
for key in data.meta:
|
2023-08-08 08:54:17 +02:00
|
|
|
deck.set_meta(key, str_to_var(data.meta[key]))
|
2023-07-21 10:10:24 +02:00
|
|
|
|
|
|
|
var nodes_data: Dictionary = data.deck.nodes as Dictionary
|
|
|
|
|
|
|
|
for node_id in nodes_data:
|
|
|
|
var node := deck.add_node_inst(NodeDB.instance_node(nodes_data[node_id].node_type), node_id, false)
|
|
|
|
node._id = node_id
|
|
|
|
node.name = nodes_data[node_id].name
|
|
|
|
node._belonging_to = deck
|
|
|
|
# node.outgoing_connections = nodes_data[node_id].outgoing_connections
|
|
|
|
# node.incoming_connections = nodes_data[node_id].incoming_connections
|
|
|
|
for connection_id in nodes_data[node_id].outgoing_connections:
|
|
|
|
var connection_data = nodes_data[node_id].outgoing_connections[connection_id]
|
2023-07-21 11:12:20 +02:00
|
|
|
for connection in connection_data:
|
|
|
|
connection[connection.keys()[0]] = int(connection.values()[0])
|
2023-07-21 10:10:24 +02:00
|
|
|
node.outgoing_connections[int(connection_id)] = connection_data
|
|
|
|
|
|
|
|
for connection_id in nodes_data[node_id].incoming_connections:
|
|
|
|
var connection_data = nodes_data[node_id].incoming_connections[connection_id]
|
2023-07-21 11:12:20 +02:00
|
|
|
for connection in connection_data:
|
|
|
|
connection_data[connection] = int(connection_data[connection])
|
2023-07-21 10:10:24 +02:00
|
|
|
node.incoming_connections[int(connection_id)] = connection_data
|
|
|
|
|
2023-07-21 10:26:43 +02:00
|
|
|
for i in nodes_data[node_id].port_values.size():
|
|
|
|
var port_value = nodes_data[node_id].port_values[i]
|
|
|
|
node.ports[i].value = port_value
|
|
|
|
|
2023-07-21 10:10:24 +02:00
|
|
|
for key in nodes_data[node_id].meta:
|
2023-08-08 08:54:17 +02:00
|
|
|
node.set_meta(key, str_to_var(nodes_data[node_id].meta[key]))
|
2023-07-21 10:10:24 +02:00
|
|
|
|
|
|
|
for prop in nodes_data[node_id].props:
|
|
|
|
node.set(prop, nodes_data[node_id].props[prop])
|
|
|
|
|
|
|
|
return deck
|