miggor-StreamGraph/classes/deck/deck.gd
2023-06-24 06:39:50 +03:00

91 lines
2.1 KiB
GDScript

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,
}
var variable_stack: Dictionary = {}
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
func add_node_inst(node: DeckNode) -> DeckNode:
var uuid := UUID.v4()
nodes[uuid] = node
node._belonging_to = self
node._id = uuid
# if !meta.is_empty():
# for k in meta:
# node.set_meta(k, meta[k])
return node
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.
var type_a: Types = from_node.ports[from_port].type
var type_b: Types = to_node.ports[to_port].type
var err: DeckType = (type_assoc[type_b]).from(type_assoc[type_a].new())
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
func disconnect_nodes(from_node: DeckNode, to_node: DeckNode, from_port: int, to_port: int) -> void:
var hash = {to_node._id: to_port}.hash()
from_node.remove_outgoing_connection(from_port, hash)
func to_json(with_meta: bool = true) -> String:
var inner := {"nodes": {}}
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():
d["meta"][meta] = get_meta(meta)
return JSON.stringify(d, "\t", false)