class_name ResultTray extends PanelContainer ## The strip along the bottom: the current roll as a chain of result chips ## joined by brass pipe pieces, and a short list of earlier rolls. var main: Main var _title: Label var _again: Button var _flow: HFlowContainer var _empty: Label var _earlier: VBoxContainer var _earlier_empty: Label var _shown_for: Dictionary = {} var _shown_count := 0 func setup(p_main: Main) -> void: main = p_main var sb := Style.box(Style.FELT_DEEP, 0, [16, 14, 16, 16]) sb.border_width_top = 1 sb.border_color = Color(1, 1, 1, 0.08) add_theme_stylebox_override("panel", sb) var h := HBoxContainer.new() h.add_theme_constant_override("separation", 28) add_child(h) var now := VBoxContainer.new() now.add_theme_constant_override("separation", 10) now.size_flags_horizontal = SIZE_EXPAND_FILL h.add_child(now) var head := HBoxContainer.new() head.add_theme_constant_override("separation", 12) now.add_child(head) _title = Style.make_label("RESULT", Style.caps(), 12, Style.ON_FELT_MUTED) _title.size_flags_vertical = SIZE_SHRINK_CENTER head.add_child(_title) _again = Style.bar_button("Roll again") _again.pressed.connect(func() -> void: if not main.current.is_empty(): main.start_roll(main.current["root"]) ) head.add_child(_again) _flow = HFlowContainer.new() _flow.add_theme_constant_override("h_separation", 0) _flow.add_theme_constant_override("v_separation", 8) # Keep the tray's height steady while a new roll's chips arrive. _flow.custom_minimum_size = Vector2(0, 58) now.add_child(_flow) _empty = Style.make_label("Click a die on any table to roll.", Style.body(), 16, Style.ON_FELT_MUTED) now.add_child(_empty) var earlier := VBoxContainer.new() earlier.add_theme_constant_override("separation", 8) earlier.custom_minimum_size = Vector2(300, 0) h.add_child(earlier) earlier.add_child(Style.make_label("EARLIER", Style.caps(), 12, Style.ON_FELT_MUTED)) _earlier = VBoxContainer.new() _earlier.add_theme_constant_override("separation", 6) earlier.add_child(_earlier) _earlier_empty = Style.make_label("Older rolls show here.", Style.body(), 15, Style.ON_FELT_MUTED) earlier.add_child(_earlier_empty) func render() -> void: var current := main.current var steps: Array = current.get("steps", []) var fresh_from := 0 if is_same(current, _shown_for): fresh_from = _shown_count _shown_for = current _shown_count = steps.size() for c in _flow.get_children(): _flow.remove_child(c) c.queue_free() _empty.visible = current.is_empty() if current.is_empty(): _title.text = "RESULT" _again.hide() else: var root_name: String = current["root_name"] _title.text = ("Result · %s" % root_name).to_upper() _again.text = "Roll %s again" % root_name _again.visible = main.data.get_table(current["root"]) != null var done: bool = current.get("done", false) for i in steps.size(): if i > 0: _flow.add_child(_link()) var chip := _chip(steps[i], done and i == steps.size() - 1) _flow.add_child(chip) if i >= fresh_from: chip.modulate.a = 0.3 create_tween().tween_property(chip, "modulate:a", 1.0, 0.25) for c in _earlier.get_children(): _earlier.remove_child(c) c.queue_free() _earlier_empty.visible = main.history.is_empty() for rec in main.history: var row := HBoxContainer.new() row.add_theme_constant_override("separation", 8) var time := Style.make_label(clock(rec["time"]), Style.body(), 14, Style.ON_FELT_MUTED) time.custom_minimum_size = Vector2(44, 0) time.size_flags_vertical = SIZE_SHRINK_BEGIN row.add_child(time) var texts := PackedStringArray() for s in rec["steps"]: texts.append(s["text"]) var line := RichTextLabel.new() line.bbcode_enabled = true line.fit_content = true line.scroll_active = false line.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART line.size_flags_horizontal = SIZE_EXPAND_FILL line.add_theme_font_override("normal_font", Style.body()) line.add_theme_font_size_override("normal_font_size", 14) line.add_theme_color_override("default_color", Style.ON_FELT) line.text = "[color=#%s]%s:[/color] %s" % [Style.ON_FELT_MUTED.to_html(false), _bb(rec["root_name"]), _bb(" → ".join(texts))] row.add_child(line) _earlier.add_child(row) func _chip(step: Dictionary, last: bool) -> Control: var p := PanelContainer.new() var sb := Style.box(Style.LIT if last else Style.CARD, 9, [12, 7, 12, 8]) sb.shadow_color = Color(0, 0, 0, 0.25) sb.shadow_size = 1 sb.shadow_offset = Vector2(0, 3) if last: Style.bordered(sb, 2, Style.BRASS) sb.set_expand_margin_all(2) p.add_theme_stylebox_override("panel", sb) var v := VBoxContainer.new() v.add_theme_constant_override("separation", 1) p.add_child(v) var num: String = step["num"] var caption: String = step["table"] if num != "": caption += " · rolled %s on d%d" % [num, step["die"]] v.add_child(Style.make_label(caption, Style.body(), 12, Style.MUTED)) v.add_child(Style.make_label(step["text"], Style.display(), 20, Style.INK)) return p func _link() -> Control: var link := Panel.new() var sb := Style.box(Style.BRASS) sb.border_width_top = 2 sb.border_width_bottom = 2 sb.border_color = Style.BRASS_DARK link.add_theme_stylebox_override("panel", sb) link.custom_minimum_size = Vector2(22, 8) link.size_flags_vertical = SIZE_SHRINK_CENTER link.mouse_filter = MOUSE_FILTER_IGNORE return link static func _bb(text: String) -> String: return text.replace("[", "[lb]") static func clock(unix: float) -> String: var bias: int = Time.get_time_zone_from_system().get("bias", 0) var d := Time.get_datetime_dict_from_unix_time(int(unix) + bias * 60) return "%02d:%02d" % [d["hour"], d["minute"]]