2023-12-15 22:44:25 +01:00
|
|
|
# (c) 2023-present Eroax
|
|
|
|
# (c) 2023-present Yagich
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2023-11-22 05:26:11 +01:00
|
|
|
class_name DeckHolder
|
2023-11-25 11:40:53 +01:00
|
|
|
## @experimental
|
|
|
|
## A static class holding references to all decks opened in the current session.
|
2023-11-22 05:26:11 +01:00
|
|
|
|
2023-11-25 11:40:53 +01:00
|
|
|
## List of decks opened this session.
|
2023-12-15 22:44:25 +01:00
|
|
|
#static var decks: Array[Deck]
|
|
|
|
static var decks: Dictionary # Dictionary[String -> id, (Deck|Dictionary[String -> instance_id, Deck])]
|
|
|
|
|
2024-03-14 06:54:25 +01:00
|
|
|
static var groups_emitted: Array[String]
|
|
|
|
|
2023-12-15 22:44:25 +01:00
|
|
|
static var logger := Logger.new()
|
2024-03-05 08:09:36 +01:00
|
|
|
static var signals := Signals.new()
|
2023-11-22 05:26:11 +01:00
|
|
|
|
|
|
|
|
2024-03-14 06:54:25 +01:00
|
|
|
static func _static_init() -> void:
|
|
|
|
signals.deck_added.connect(RPCSignalLayer._on_deck_added)
|
|
|
|
signals.deck_closed.connect(RPCSignalLayer._on_deck_closed)
|
|
|
|
|
|
|
|
|
2023-11-25 11:40:53 +01:00
|
|
|
## Returns a new empty deck and assigns a new random ID to it.
|
2023-11-22 05:26:11 +01:00
|
|
|
static func add_empty_deck() -> Deck:
|
|
|
|
var deck := Deck.new()
|
|
|
|
var uuid := UUID.v4()
|
2023-12-15 22:44:25 +01:00
|
|
|
decks[uuid] = deck
|
2023-11-22 05:26:11 +01:00
|
|
|
deck.id = uuid
|
2024-03-14 06:54:25 +01:00
|
|
|
deck.connect_rpc_signals()
|
2024-03-15 07:02:04 +01:00
|
|
|
signals.deck_added.emit(uuid)
|
2023-11-22 05:26:11 +01:00
|
|
|
return deck
|
|
|
|
|
|
|
|
|
2023-11-25 11:40:53 +01:00
|
|
|
## Opens a deck from the [param path].
|
2023-11-22 05:26:11 +01:00
|
|
|
static func open_deck_from_file(path: String) -> Deck:
|
|
|
|
var f := FileAccess.open(path, FileAccess.READ)
|
|
|
|
if f.get_error() != OK:
|
|
|
|
return null
|
|
|
|
|
2023-12-15 22:44:25 +01:00
|
|
|
var deck := open_deck_from_dict(JSON.parse_string(f.get_as_text()), path)
|
|
|
|
return deck
|
|
|
|
|
2023-11-22 05:26:11 +01:00
|
|
|
|
2023-12-15 22:44:25 +01:00
|
|
|
static func open_deck_from_dict(data: Dictionary, path := "") -> Deck:
|
|
|
|
var deck := Deck.from_dict(data, path)
|
|
|
|
decks[deck.id] = deck
|
2024-03-14 06:54:25 +01:00
|
|
|
deck.connect_rpc_signals()
|
2024-03-15 07:02:04 +01:00
|
|
|
signals.deck_added.emit(deck.id)
|
2023-11-22 05:26:11 +01:00
|
|
|
return deck
|
|
|
|
|
|
|
|
|
2023-12-15 22:44:25 +01:00
|
|
|
static func add_group_from_dict(data: Dictionary, deck_id: String, instance_id: String) -> Deck:
|
|
|
|
var group := Deck.from_dict(data)
|
|
|
|
group.instance_id = instance_id
|
|
|
|
group.is_group = true
|
|
|
|
group.group_input_node = data.deck.group_input_node
|
|
|
|
group.group_output_node = data.deck.group_output_node
|
|
|
|
var instances: Dictionary = decks.get(deck_id, {})
|
|
|
|
instances[instance_id] = group
|
|
|
|
decks[deck_id] = instances
|
|
|
|
connect_group_signals(group)
|
2024-03-14 06:54:25 +01:00
|
|
|
|
|
|
|
if deck_id not in groups_emitted:
|
2024-03-15 07:02:04 +01:00
|
|
|
group.connect_rpc_signals()
|
2024-03-14 06:54:25 +01:00
|
|
|
signals.deck_added.emit(deck_id)
|
|
|
|
groups_emitted.append(deck_id)
|
|
|
|
|
2023-12-15 22:44:25 +01:00
|
|
|
return group
|
|
|
|
|
|
|
|
|
|
|
|
static func make_new_group_instance(group_id: String) -> Deck:
|
|
|
|
var group := get_deck(group_id)
|
|
|
|
var data := group.to_dict()
|
|
|
|
return add_group_from_dict(data, group_id, UUID.v4())
|
|
|
|
|
|
|
|
|
|
|
|
static func add_empty_group() -> Deck:
|
|
|
|
var group := Deck.new()
|
|
|
|
group.is_group = true
|
|
|
|
group.id = UUID.v4()
|
|
|
|
group.instance_id = UUID.v4()
|
|
|
|
decks[group.id] = {group.instance_id: group}
|
|
|
|
connect_group_signals(group)
|
2024-03-14 06:54:25 +01:00
|
|
|
if group.id not in groups_emitted:
|
2024-03-15 07:02:04 +01:00
|
|
|
group.connect_rpc_signals()
|
2024-03-14 06:54:25 +01:00
|
|
|
signals.deck_added.emit(group.id)
|
|
|
|
groups_emitted.append(group.id)
|
2023-12-15 22:44:25 +01:00
|
|
|
return group
|
|
|
|
|
|
|
|
|
|
|
|
static func connect_group_signals(group: Deck) -> void:
|
|
|
|
group.node_added_to_group.connect(DeckHolder._on_node_added_to_group)
|
|
|
|
group.node_removed_from_group.connect(DeckHolder._on_node_removed_from_group)
|
|
|
|
group.nodes_connected_in_group.connect(DeckHolder._on_nodes_connected_in_group)
|
|
|
|
group.nodes_disconnected_in_group.connect(DeckHolder._on_nodes_disconnected_in_group)
|
|
|
|
group.node_port_value_updated.connect(DeckHolder._on_node_port_value_updated)
|
|
|
|
group.node_renamed.connect(DeckHolder._on_node_renamed)
|
|
|
|
group.node_moved.connect(DeckHolder._on_node_moved)
|
|
|
|
|
|
|
|
|
|
|
|
static func get_deck(id: String) -> Deck:
|
2024-02-21 07:11:29 +01:00
|
|
|
if not decks.has(id):
|
2023-12-15 22:44:25 +01:00
|
|
|
return null
|
|
|
|
|
2024-02-21 07:11:29 +01:00
|
|
|
if not decks[id] is Dictionary:
|
2023-12-15 22:44:25 +01:00
|
|
|
return decks[id]
|
|
|
|
else:
|
|
|
|
return (decks[id] as Dictionary).values()[0]
|
|
|
|
|
|
|
|
|
|
|
|
static func get_group_instance(group_id: String, instance_id: String) -> Deck:
|
2024-02-21 07:11:29 +01:00
|
|
|
if not decks.has(group_id):
|
2023-12-15 22:44:25 +01:00
|
|
|
return null
|
|
|
|
|
|
|
|
if decks[group_id] is Dictionary:
|
|
|
|
return (decks[group_id] as Dictionary).get(instance_id)
|
|
|
|
else:
|
|
|
|
return null
|
|
|
|
|
|
|
|
|
|
|
|
static func close_group_instance(group_id: String, instance_id: String) -> void:
|
|
|
|
# this is kinda dumb, but to close groups that may be dangling
|
|
|
|
# when all instances are closed, we have to get that list
|
|
|
|
# *before* we close the instance
|
|
|
|
var dangling_groups := get_deck(group_id).get_referenced_groups()
|
|
|
|
|
|
|
|
var group_instances: Dictionary = decks.get(group_id, {}) as Dictionary
|
|
|
|
group_instances.erase(instance_id)
|
|
|
|
if group_instances.is_empty():
|
|
|
|
for group in dangling_groups:
|
|
|
|
close_all_group_instances(group)
|
2024-03-14 06:54:25 +01:00
|
|
|
signals.deck_closed.emit(group_id)
|
|
|
|
groups_emitted.erase(group_id)
|
2023-12-15 22:44:25 +01:00
|
|
|
decks.erase(group_id)
|
|
|
|
|
|
|
|
|
|
|
|
static func close_all_group_instances(group_id: String) -> void:
|
|
|
|
if decks.get(group_id) is Dictionary:
|
|
|
|
decks.erase(group_id)
|
|
|
|
|
|
|
|
|
|
|
|
## Unloads a deck. Returns a list of groups that are closed as a result of
|
|
|
|
## closing this deck.
|
|
|
|
static func close_deck(deck_id: String) -> Array:
|
|
|
|
if decks.get(deck_id) is Deck:
|
|
|
|
var deck: Deck = decks[deck_id] as Deck
|
|
|
|
var groups := deck.get_referenced_groups()
|
|
|
|
for group in groups:
|
|
|
|
close_all_group_instances(group)
|
2024-03-14 06:54:25 +01:00
|
|
|
signals.deck_closed.emit(deck_id)
|
2023-12-15 22:44:25 +01:00
|
|
|
decks.erase(deck_id)
|
|
|
|
return groups
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
static func send_event(event_name: StringName, event_data: Dictionary = {}) -> void:
|
|
|
|
for deck_id: String in decks:
|
|
|
|
if decks[deck_id] is Deck:
|
|
|
|
(decks[deck_id] as Deck).send_event(event_name, event_data)
|
|
|
|
else:
|
|
|
|
for deck_instance_id: String in decks[deck_id]:
|
|
|
|
(decks[deck_id][deck_instance_id] as Deck).send_event(event_name, event_data)
|
|
|
|
|
|
|
|
|
|
|
|
#region group signal callbacks
|
|
|
|
static func _on_node_added_to_group(node: DeckNode, assign_id: String, assign_to_self: bool, deck: Deck) -> void:
|
|
|
|
var group_id := deck.id
|
|
|
|
for instance_id: String in decks[group_id]:
|
|
|
|
if instance_id == deck.instance_id:
|
|
|
|
continue
|
|
|
|
|
|
|
|
var instance: Deck = get_group_instance(group_id, instance_id)
|
|
|
|
instance.emit_group_signals = false
|
|
|
|
var node_duplicate := DeckNode.from_dict(node.to_dict())
|
|
|
|
instance.add_node_inst(node_duplicate, assign_id, assign_to_self)
|
|
|
|
instance.emit_group_signals = true
|
|
|
|
|
|
|
|
|
|
|
|
static func _on_node_removed_from_group(node_id: String, remove_connections: bool, deck: Deck) -> void:
|
|
|
|
var group_id := deck.id
|
|
|
|
for instance_id: String in decks[group_id]:
|
|
|
|
if instance_id == deck.instance_id:
|
|
|
|
continue
|
|
|
|
|
|
|
|
var instance: Deck = get_group_instance(group_id, instance_id)
|
|
|
|
instance.emit_group_signals = false
|
|
|
|
instance.remove_node(node_id, remove_connections)
|
|
|
|
instance.emit_group_signals = true
|
|
|
|
|
|
|
|
|
|
|
|
static func _on_nodes_connected_in_group(from_node_id: String, to_node_id: String, from_output_port: int, to_input_port: int, deck: Deck) -> void:
|
|
|
|
var group_id := deck.id
|
|
|
|
for instance_id: String in decks[group_id]:
|
|
|
|
if instance_id == deck.instance_id:
|
|
|
|
continue
|
|
|
|
|
|
|
|
var instance: Deck = get_group_instance(group_id, instance_id)
|
|
|
|
instance.emit_group_signals = false
|
|
|
|
instance.connect_nodes(from_node_id, to_node_id, from_output_port, to_input_port)
|
|
|
|
instance.emit_group_signals = true
|
|
|
|
|
|
|
|
|
|
|
|
static func _on_nodes_disconnected_in_group(from_node_id: String, to_node_id: String, from_output_port: int, to_input_port: int, deck: Deck) -> void:
|
|
|
|
var group_id := deck.id
|
|
|
|
for instance_id: String in decks[group_id]:
|
|
|
|
if instance_id == deck.instance_id:
|
|
|
|
continue
|
|
|
|
|
|
|
|
var instance: Deck = get_group_instance(group_id, instance_id)
|
|
|
|
instance.emit_group_signals = false
|
|
|
|
instance.disconnect_nodes(from_node_id, to_node_id, from_output_port, to_input_port)
|
|
|
|
instance.emit_group_signals = true
|
|
|
|
|
|
|
|
|
|
|
|
static func _on_node_port_value_updated(node_id: String, port_idx: int, new_value: Variant, deck: Deck) -> void:
|
|
|
|
var group_id := deck.id
|
|
|
|
for instance_id: String in decks[group_id]:
|
|
|
|
if instance_id == deck.instance_id:
|
|
|
|
continue
|
|
|
|
|
|
|
|
var instance: Deck = get_group_instance(group_id, instance_id)
|
|
|
|
instance.emit_group_signals = false
|
|
|
|
instance.get_node(node_id).get_all_ports()[port_idx].set_value_no_signal(new_value)
|
|
|
|
instance.emit_group_signals = true
|
|
|
|
|
|
|
|
|
|
|
|
static func _on_node_renamed(node_id: String, new_name: String, deck: Deck) -> void:
|
|
|
|
var group_id := deck.id
|
|
|
|
for instance_id: String in decks[group_id]:
|
|
|
|
if instance_id == deck.instance_id:
|
|
|
|
continue
|
|
|
|
|
|
|
|
var instance: Deck = get_group_instance(group_id, instance_id)
|
|
|
|
instance.emit_group_signals = false
|
|
|
|
instance.get_node(node_id).name = new_name
|
|
|
|
instance.emit_group_signals = true
|
|
|
|
|
|
|
|
|
|
|
|
static func _on_node_moved(node_id: String, new_position: Dictionary, deck: Deck) -> void:
|
|
|
|
var group_id := deck.id
|
|
|
|
for instance_id: String in decks[group_id]:
|
|
|
|
if instance_id == deck.instance_id:
|
|
|
|
continue
|
|
|
|
|
|
|
|
var instance: Deck = get_group_instance(group_id, instance_id)
|
|
|
|
instance.emit_group_signals = false
|
|
|
|
instance.get_node(node_id).position = new_position.duplicate()
|
|
|
|
instance.emit_group_signals = true
|
|
|
|
#endregion
|
2024-03-05 08:09:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Signals:
|
|
|
|
signal deck_added(deck_id: String)
|
|
|
|
signal deck_closed(deck_id: String)
|