]> Untitled Git - wolf-seeking-sheep.git/blob - addons/dialogic/Modules/DefaultLayoutParts/Base_TextBubble/text_bubble_base.gd
Initial Godot project with Dialogic 2.0-Alpha-17
[wolf-seeking-sheep.git] / addons / dialogic / Modules / DefaultLayoutParts / Base_TextBubble / text_bubble_base.gd
1 @tool
2 extends DialogicLayoutBase
3
4 ## This layout won't do anything on its own
5
6 var bubbles: Array = []
7 var registered_characters: Dictionary = {}
8
9 @export_group("Main")
10 @export_range(1, 25, 1) var bubble_count: int = 2
11
12
13 func _ready() -> void:
14         if Engine.is_editor_hint():
15                 return
16
17         DialogicUtil.autoload().Text.about_to_show_text.connect(_on_dialogic_text_event)
18         $Example/CRT.position = $Example.get_viewport_rect().size/2
19
20         if not has_node('TextBubbleLayer'):
21                 return
22
23         if len(bubbles) < bubble_count:
24                 add_bubble()
25
26
27 func register_character(character:Variant, node:Node):
28         if typeof(character) == TYPE_STRING:
29                 var character_string: String = character
30                 if character.begins_with("res://"):
31                         character = load(character)
32                 else:
33                         character = DialogicResourceUtil.get_character_resource(character)
34                 if not character:
35                         printerr("[Dialogic] Textbubble: Tried registering character from invalid string '", character_string, "'.")
36
37         registered_characters[character] = node
38         if len(registered_characters) > len(bubbles) and len(bubbles) < bubble_count:
39                 add_bubble()
40
41
42 func _get_persistent_info() -> Dictionary:
43         return {"textbubble_registers": registered_characters}
44
45
46 func _load_persistent_info(info: Dictionary) -> void:
47         var register_info: Dictionary = info.get("textbubble_registers", {})
48         for character in register_info:
49                 if is_instance_valid(register_info[character]):
50                         register_character(character, register_info[character])
51
52
53 func add_bubble() -> void:
54         if not has_node('TextBubbleLayer'):
55                 return
56
57         var new_bubble: Control = get_node("TextBubbleLayer").add_bubble()
58         bubbles.append(new_bubble)
59
60
61 func _on_dialogic_text_event(info:Dictionary):
62         var bubble_to_use: Node
63         for bubble in bubbles:
64                 if bubble.current_character == info.character:
65                         bubble_to_use = bubble
66
67         if bubble_to_use == null:
68                 for bubble in bubbles:
69                         if bubble.current_character == null:
70                                 bubble_to_use = bubble
71
72         if bubble_to_use == null:
73                 bubble_to_use = bubbles[0]
74
75         var node_to_point_at: Node
76         if info.character in registered_characters:
77                 node_to_point_at = registered_characters[info.character]
78                 $Example.hide()
79         else:
80                 node_to_point_at = $Example/CRT/Marker
81                 $Example.show()
82
83         bubble_to_use.current_character = info.character
84         bubble_to_use.node_to_point_at = node_to_point_at
85         bubble_to_use.reset()
86         if has_node('TextBubbleLayer'):
87                 get_node("TextBubbleLayer").bubble_apply_overrides(bubble_to_use)
88         bubble_to_use.open()
89
90         ## Now close other bubbles
91         for bubble in bubbles:
92                 if bubble != bubble_to_use:
93                         bubble.close()
94                         bubble.current_character = null