class_name SpeechBubbles extends CanvasLayer signal bubble_dismissed(bubble: Bubble) @export var max_width := 160.0 # max text width in px (excludes padding) @export var chars_per_sec := 0.0 # 0 = show text instantly @export var tail_size := Vector2(10, 8) # width, height @export var pad := 6 @export var corner := 6 @export var screen_margin := 4.0 @export var fade_time := 0.12 @export var bg_color := Color.WHITE @export var text_color := Color(0.1, 0.1, 0.15) var _bubbles: Array[Bubble] = [] # ── Public API ──────────────────────────────────────────────── ## Show a bubble over `who`. duration 0 = stay until dismissed. ## Replaces any bubble already on `who`. Returns the bubble handle. ## Wait for it with: await speech.say(...).dismissed func say(text: String, who: Node2D, duration := 0.0) -> Bubble: var old := get_bubble(who) if old: old.dismiss(true) var b := Bubble.new() b.cfg = self b.target = who b.duration = duration b.dismissed.connect(_on_dismissed.bind(b)) _bubbles.append(b) add_child(b) b.start(text) return b ## Dismiss one speaker's bubble, or all bubbles if `who` is null. func dismiss(who: Node2D = null, instant := false) -> void: for b in _bubbles.duplicate(): if who == null or b.target == who: b.dismiss(instant) func get_bubble(who: Node2D) -> Bubble: for b in _bubbles: if b.target == who: return b return null ## True if `who` has a bubble, or if any bubble exists when `who` is null. func is_speaking(who: Node2D = null) -> bool: return not _bubbles.is_empty() if who == null else get_bubble(who) != null func _on_dismissed(b: Bubble) -> void: _bubbles.erase(b) bubble_dismissed.emit(b) # ── One bubble ──────────────────────────────────────────────── class Bubble extends RichTextLabel: signal shown # laid out and visible signal dismissed # fade-out starts (or instant removal) var cfg: SpeechBubbles var target: Node2D var duration := 0.0 var is_shown := false var _dead := false var _below := false var _tail_x := 0.0 var _tween: Tween func _ready() -> void: bbcode_enabled = true fit_content = false # we size the box ourselves scroll_active = false clip_contents = false # keeps the tail and shadow from being clipped mouse_filter = Control.MOUSE_FILTER_IGNORE add_theme_color_override("default_color", cfg.text_color) var sb := StyleBoxFlat.new() sb.bg_color = cfg.bg_color sb.set_corner_radius_all(cfg.corner) sb.set_content_margin_all(cfg.pad) sb.shadow_color = Color(0, 0, 0, 0.25) sb.shadow_size = 3 sb.shadow_offset = Vector2(0, 2) add_theme_stylebox_override("normal", sb) modulate.a = 0.0 set_process(false) func start(t: String) -> void: var pad2 := cfg.pad * 2.0 autowrap_mode = TextServer.AUTOWRAP_OFF custom_minimum_size = Vector2.ZERO size = Vector2(cfg.max_width + pad2, 100) # sane width while the text is parsed text = t await get_tree().process_frame if _dead: return var font := get_theme_font("normal_font") var fs := get_theme_font_size("normal_font_size") var plain := get_parsed_text() # Width: natural text width, capped var natural := font.get_string_size(plain, HORIZONTAL_ALIGNMENT_LEFT, -1, fs).x var text_w := minf(natural + 2.0, cfg.max_width) # Height: measured at the final wrap width var text_h := font.get_multiline_string_size( plain, HORIZONTAL_ALIGNMENT_LEFT, text_w, fs).y autowrap_mode = TextServer.AUTOWRAP_WORD_SMART size = Vector2(text_w + pad2, ceilf(text_h) + pad2) is_shown = true _place() set_process(true) # Pop-in scale = Vector2(0.6, 0.6) var t_in := create_tween().set_parallel() t_in.tween_property(self, "scale", Vector2.ONE, 0.2).set_trans(Tween.TRANS_BACK).set_ease(Tween.EASE_OUT) t_in.tween_property(self, "modulate:a", 1.0, 0.1) # Typewriter (optional) var type_time := 0.0 if cfg.chars_per_sec > 0.0: type_time = get_total_character_count() / cfg.chars_per_sec visible_ratio = 0.0 _tween = create_tween() _tween.tween_property(self, "visible_ratio", 1.0, type_time) shown.emit() # Auto-dismiss: duration counts after typing finishes if duration > 0.0: await get_tree().create_timer(type_time + duration).timeout if not _dead: dismiss() ## Finish typing instantly. func complete() -> void: if _tween: _tween.kill() visible_ratio = 1.0 func dismiss(instant := false) -> void: if _dead: return _dead = true set_process(false) if _tween: _tween.kill() dismissed.emit() if instant or not is_shown: queue_free() return var t := create_tween().set_parallel() t.tween_property(self, "modulate:a", 0.0, cfg.fade_time) t.tween_property(self, "scale", Vector2(0.8, 0.8), cfg.fade_time) t.finished.connect(queue_free) func _process(_dt: float) -> void: if not is_instance_valid(target): # speaker was freed dismiss(true) return _place() func _place() -> void: var vp := get_viewport().get_visible_rect().size var a := target.get_global_transform_with_canvas().origin var m := cfg.screen_margin var ts := cfg.tail_size _below = a.y - size.y - ts.y < m var x := clampf(a.x - size.x * 0.5, m, maxf(m, vp.x - size.x - m)) var y := a.y + ts.y if _below else a.y - size.y - ts.y position = Vector2(x, y) var tx := clampf(a.x - x, cfg.corner + ts.x, size.x - cfg.corner - ts.x) if not is_equal_approx(tx, _tail_x): _tail_x = tx queue_redraw() pivot_offset = Vector2(_tail_x, 0.0 if _below else size.y + ts.y) func _draw() -> void: if not is_shown: return var hw := cfg.tail_size.x * 0.5 var y0 := 0.0 if _below else size.y var d := -1.0 if _below else 1.0 # base starts 1px inside the box so there's no seam draw_colored_polygon(PackedVector2Array([ Vector2(_tail_x - hw, y0 - d), Vector2(_tail_x + hw, y0 - d), Vector2(_tail_x, y0 + d * cfg.tail_size.y)]), cfg.bg_color)