]> Untitled Git - wolf-seeking-sheep.git/blob - addons/dialogic/Modules/DefaultLayoutParts/Layer_Textbubble/text_bubble.gd
Initial Godot project with Dialogic 2.0-Alpha-17
[wolf-seeking-sheep.git] / addons / dialogic / Modules / DefaultLayoutParts / Layer_Textbubble / text_bubble.gd
1 extends Control
2
3 @onready var tail: Line2D = ($Group/Tail as Line2D)
4 @onready var bubble: Control = ($Group/Background as Control)
5 @onready var text: DialogicNode_DialogText = (%DialogText as DialogicNode_DialogText)
6 # The choice container is added by the TextBubble layer
7 @onready var choice_container: Container = null
8 @onready var name_label: Label = (%NameLabel as Label)
9 @onready var name_label_box: PanelContainer = (%NameLabelPanel as PanelContainer)
10 @onready var name_label_holder: HBoxContainer = $DialogText/NameLabelPositioner
11
12 var node_to_point_at: Node = null
13 var current_character: DialogicCharacter = null
14
15 var max_width := 300
16
17 var bubble_rect: Rect2 = Rect2(0.0, 0.0, 2.0, 2.0)
18 var base_position := Vector2.ZERO
19
20 var base_direction := Vector2(1.0, -1.0).normalized()
21 var safe_zone := 50.0
22 var padding := Vector2()
23
24 var name_label_alignment := HBoxContainer.ALIGNMENT_BEGIN
25 var name_label_offset := Vector2()
26 var force_choices_on_separate_lines := false
27
28 # Sets the padding shader paramter.
29 # It's the amount of spacing around the background to allow some wobbeling.
30 var bg_padding := 30
31
32
33 func _ready() -> void:
34         reset()
35         DialogicUtil.autoload().Choices.question_shown.connect(_on_question_shown)
36
37
38 func reset() -> void:
39         scale = Vector2.ZERO
40         modulate.a = 0.0
41
42         tail.points = []
43         bubble_rect = Rect2(0,0,2,2)
44
45         base_position = get_speaker_canvas_position()
46         position = base_position
47
48
49 func _process(delta:float) -> void:
50         base_position = get_speaker_canvas_position()
51
52         var center := get_viewport_rect().size / 2.0
53
54         var dist_x := absf(base_position.x - center.x)
55         var dist_y := absf(base_position.y - center.y)
56         var x_e := center.x - bubble_rect.size.x
57         var y_e := center.y - bubble_rect.size.y
58         var influence_x := remap(clamp(dist_x, x_e, center.x), x_e, center.x * 0.8, 0.0, 1.0)
59         var influence_y := remap(clamp(dist_y, y_e, center.y), y_e, center.y * 0.8, 0.0, 1.0)
60         if base_position.x > center.x: influence_x = -influence_x
61         if base_position.y > center.y: influence_y = -influence_y
62         var edge_influence := Vector2(influence_x, influence_y)
63
64         var direction := (base_direction + edge_influence).normalized()
65
66         var p: Vector2 = base_position + direction * (
67                 safe_zone + lerp(bubble_rect.size.y, bubble_rect.size.x, abs(direction.x)) * 0.4
68                 )
69         p = p.clamp(bubble_rect.size / 2.0, get_viewport_rect().size - bubble_rect.size / 2.0)
70
71         position = position.lerp(p, 5 * delta)
72
73         var point_a: Vector2 = Vector2.ZERO
74         var point_b: Vector2 = (base_position - position) * 0.75
75
76         var offset: Vector2 = Vector2.from_angle(point_a.angle_to_point(point_b)) * bubble_rect.size * abs(direction.x) * 0.4
77
78         point_a += offset
79         point_b += offset * 0.5
80
81         var curve := Curve2D.new()
82         var direction_point := Vector2(0, (point_b.y - point_a.y))
83         curve.add_point(point_a, Vector2.ZERO, direction_point * 0.5)
84         curve.add_point(point_b)
85         tail.points = curve.tessellate(5)
86         tail.width = bubble_rect.size.x * 0.15
87
88
89 func open() -> void:
90         set_process(true)
91         show()
92         text.enabled = true
93         var open_tween := create_tween().set_parallel(true)
94         open_tween.tween_property(self, "scale", Vector2.ONE, 0.1).from(Vector2.ZERO)
95         open_tween.tween_property(self, "modulate:a", 1.0, 0.1).from(0.0)
96
97
98 func close() -> void:
99         text.enabled = false
100         var close_tween := create_tween().set_parallel(true)
101         close_tween.tween_property(self, "scale", Vector2.ONE * 0.8, 0.2)
102         close_tween.tween_property(self, "modulate:a", 0.0, 0.2)
103         await close_tween.finished
104         hide()
105         set_process(false)
106
107
108 func _on_dialog_text_started_revealing_text() -> void:
109         _resize_bubble(get_base_content_size(), true)
110
111
112 func _resize_bubble(content_size:Vector2, popup:=false) -> void:
113         var bubble_size: Vector2 = content_size+(padding*2)+Vector2.ONE*bg_padding
114         var half_size: Vector2= (bubble_size / 2.0)
115         bubble.pivot_offset = half_size
116         bubble_rect = Rect2(position, bubble_size * Vector2(1.1, 1.1))
117         bubble.position = -half_size
118         bubble.size = bubble_size
119
120         text.size = content_size
121         text.position = -(content_size/2.0)
122
123         if popup:
124                 var t := create_tween().set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_BACK)
125                 t.tween_property(bubble, "scale", Vector2.ONE, 0.2).from(Vector2.ZERO)
126         else:
127                 bubble.scale = Vector2.ONE
128
129         bubble.material.set(&"shader_parameter/box_size", bubble_size)
130         name_label_holder.position = Vector2(0, bubble.position.y - text.position.y - name_label_holder.size.y/2.0)
131         name_label_holder.position += name_label_offset
132         name_label_holder.alignment = name_label_alignment
133         name_label_holder.size.x = text.size.x
134
135
136 func _on_question_shown(info:Dictionary) -> void:
137         if !is_visible_in_tree():
138                 return
139
140         await get_tree().process_frame
141
142         var content_size := get_base_content_size()
143         content_size.y += choice_container.size.y
144         content_size.x = max(content_size.x, choice_container.size.x)
145         _resize_bubble(content_size)
146
147
148 func get_base_content_size() -> Vector2:
149         var font: Font = text.get_theme_font(&"normal_font")
150         return font.get_multiline_string_size(
151                 text.get_parsed_text(),
152                 HORIZONTAL_ALIGNMENT_LEFT,
153                 max_width,
154                 text.get_theme_font_size(&"normal_font_size")
155                 )
156
157
158 func add_choice_container(node:Container, alignment:=FlowContainer.ALIGNMENT_BEGIN) -> void:
159         if choice_container:
160                 choice_container.get_parent().remove_child(choice_container)
161                 choice_container.queue_free()
162
163         node.name = "ChoiceContainer"
164         choice_container = node
165         node.set_anchors_preset(LayoutPreset.PRESET_BOTTOM_WIDE)
166         node.grow_vertical = Control.GROW_DIRECTION_BEGIN
167         text.add_child(node)
168
169         if node is HFlowContainer:
170                 (node as HFlowContainer).alignment = alignment
171
172         for i:int in range(5):
173                 choice_container.add_child(DialogicNode_ChoiceButton.new())
174                 if node is HFlowContainer:
175                         continue
176                 match alignment:
177                         HBoxContainer.ALIGNMENT_BEGIN:
178                                 (choice_container.get_child(-1) as Control).size_flags_horizontal = SIZE_SHRINK_BEGIN
179                         HBoxContainer.ALIGNMENT_CENTER:
180                                 (choice_container.get_child(-1) as Control).size_flags_horizontal = SIZE_SHRINK_CENTER
181                         HBoxContainer.ALIGNMENT_END:
182                                 (choice_container.get_child(-1) as Control).size_flags_horizontal = SIZE_SHRINK_END
183
184         for child:Button in choice_container.get_children():
185                 var prev := child.get_parent().get_child(wrap(child.get_index()-1, 0, choice_container.get_child_count()-1)).get_path()
186                 var next := child.get_parent().get_child(wrap(child.get_index()+1, 0, choice_container.get_child_count()-1)).get_path()
187                 child.focus_next = next
188                 child.focus_previous = prev
189                 child.focus_neighbor_left = prev
190                 child.focus_neighbor_top = prev
191                 child.focus_neighbor_right = next
192                 child.focus_neighbor_bottom = next
193
194
195 func get_speaker_canvas_position() -> Vector2:
196         if is_instance_valid(node_to_point_at):
197                 if node_to_point_at is Node3D:
198                         base_position = get_viewport().get_camera_3d().unproject_position(
199                                 (node_to_point_at as Node3D).global_position)
200                 if node_to_point_at is CanvasItem:
201                         base_position = (node_to_point_at as CanvasItem).get_global_transform_with_canvas().origin
202         return base_position