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-06 11:16:38 +01:00
|
|
|
#static var decks: Array[Deck]
|
|
|
|
static var decks: Dictionary # Dictionary[String -> id, (Deck|Dictionary[String -> instance_id, Deck])]
|
2023-11-22 05:26:11 +01:00
|
|
|
|
|
|
|
|
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-06 11:16:38 +01:00
|
|
|
decks[uuid] = deck
|
2023-11-22 05:26:11 +01:00
|
|
|
deck.id = uuid
|
|
|
|
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-06 11:16:38 +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-06 11:16:38 +01:00
|
|
|
static func open_deck_from_dict(data: Dictionary, path := "") -> Deck:
|
|
|
|
var deck := Deck.from_dict(data, path)
|
|
|
|
decks[deck.id] = deck
|
2023-11-22 05:26:11 +01:00
|
|
|
return deck
|
|
|
|
|
|
|
|
|
2023-12-06 11:16:38 +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
|
|
|
|
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}
|
|
|
|
|
|
|
|
return group
|
|
|
|
|
|
|
|
|
|
|
|
static func get_deck(id: String) -> Deck:
|
|
|
|
if !decks.has(id):
|
|
|
|
return null
|
|
|
|
|
|
|
|
if !(decks[id] is Dictionary):
|
|
|
|
return decks[id]
|
|
|
|
else:
|
|
|
|
return (decks[id] as Dictionary).values()[0]
|
|
|
|
|
|
|
|
|
|
|
|
static func get_group_instance(group_id: String, instance_id: String) -> Deck:
|
|
|
|
if !decks.has(group_id):
|
|
|
|
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:
|
|
|
|
var group_instances: Dictionary = decks.get(group_id, {}) as Dictionary
|
|
|
|
group_instances.erase(instance_id)
|
|
|
|
if group_instances.is_empty():
|
|
|
|
decks.erase(group_id)
|
|
|
|
|
|
|
|
|
2023-11-25 11:40:53 +01:00
|
|
|
## Unloads a deck.
|
2023-12-06 11:16:38 +01:00
|
|
|
static func close_deck(deck_id: String) -> void:
|
|
|
|
if decks.get(deck_id) is Deck:
|
|
|
|
decks.erase(deck_id)
|
2023-12-01 10:30:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
static func send_event(event_name: StringName, event_data: Dictionary = {}) -> void:
|
2023-12-06 11:16:38 +01:00
|
|
|
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)
|