|
| 1 | +extends Control |
| 2 | +## The credits screen. It shows the hand-written sections in credits.json first (original music |
| 3 | +## and anything else people add there as data), then the asset credits that |
| 4 | +## tools/credits/build_credits.py generates from ASSETS_LICENSES.md. The list scrolls by itself; |
| 5 | +## Up and Down (or the mouse wheel) scroll it by hand, and Back or Esc / B return to the hub. |
| 6 | +const Style = preload("res://scenes/main_menu/menu_style.gd") |
| 7 | +const Prompts := preload("res://app/controller_prompts.gd") |
| 8 | +## Hand-written sections: {"title", "lines": [String], "tracks": [{"title", "used_by"}]}. |
| 9 | +const CREDITS := "res://scenes/credits/credits.json" |
| 10 | +## Generated; do not edit. Sections of credits: |
| 11 | +## {"credit", "license", "used_by", "source_url", "license_url"}. |
| 12 | +const ASSET_CREDITS := "res://scenes/credits/asset_credits.json" |
| 13 | +const SCROLL_SPEED := 36.0 |
| 14 | +## After a manual scroll, the list waits this long before scrolling by itself again. |
| 15 | +const MANUAL_HOLD := 4.0 |
| 16 | +## One press of Up or Down moves the list this far; holding it scrolls this fast. Held D-pad |
| 17 | +## buttons send no repeat events, so holding is polled each frame. |
| 18 | +const STEP := 90.0 |
| 19 | +const HELD_SPEED := 540.0 |
| 20 | +const HELD_AFTER := 0.3 |
| 21 | +## At the bottom, the list waits this long, then starts again from the top. |
| 22 | +const END_HOLD := 3.0 |
| 23 | +const MUTED := Color("afbdc9") |
| 24 | +var scroll: ScrollContainer |
| 25 | +var list: VBoxContainer |
| 26 | +var back: Button |
| 27 | +var offset := 0.0 |
| 28 | +var hold := 1.5 |
| 29 | +var leaving := false |
| 30 | +## How long Up or Down has been held, to start continuous scrolling after HELD_AFTER. |
| 31 | +var held := 0.0 |
| 32 | + |
| 33 | +func _ready() -> void: |
| 34 | + theme = Style.theme() |
| 35 | + var background := ColorRect.new() |
| 36 | + background.color = Color("101d2d") |
| 37 | + background.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT) |
| 38 | + add_child(background) |
| 39 | + var margin := MarginContainer.new() |
| 40 | + margin.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT) |
| 41 | + for side in ["left", "right"]: |
| 42 | + margin.add_theme_constant_override("margin_" + side, 76) |
| 43 | + for side in ["top", "bottom"]: |
| 44 | + margin.add_theme_constant_override("margin_" + side, 36) |
| 45 | + add_child(margin) |
| 46 | + var column := VBoxContainer.new() |
| 47 | + column.add_theme_constant_override("separation", 14) |
| 48 | + margin.add_child(column) |
| 49 | + column.add_child(Style.label("HOPPY'S ADVENTURE ARCADE", 16, Color("ff9a43"))) |
| 50 | + column.add_child(Style.label("CREDITS", 38)) |
| 51 | + scroll = ScrollContainer.new() |
| 52 | + scroll.name = "Scroll" |
| 53 | + scroll.size_flags_vertical = Control.SIZE_EXPAND_FILL |
| 54 | + scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED |
| 55 | + scroll.vertical_scroll_mode = ScrollContainer.SCROLL_MODE_SHOW_NEVER |
| 56 | + column.add_child(scroll) |
| 57 | + list = VBoxContainer.new() |
| 58 | + list.name = "List" |
| 59 | + list.size_flags_horizontal = Control.SIZE_EXPAND_FILL |
| 60 | + list.add_theme_constant_override("separation", 6) |
| 61 | + scroll.add_child(list) |
| 62 | + for section in sections(): |
| 63 | + _add_section(section) |
| 64 | + _add_gap(18) |
| 65 | + _add_text("Source code: MIT License. CodeRabbit and the Hoppy mascot are trademarks of" |
| 66 | + + " CodeRabbit, Inc.", 15, MUTED) |
| 67 | + var footer := HBoxContainer.new() |
| 68 | + footer.add_theme_constant_override("separation", 24) |
| 69 | + column.add_child(footer) |
| 70 | + back = Style.button("Return to Main Menu", 48) |
| 71 | + back.name = "BackButton" |
| 72 | + footer.add_child(back) |
| 73 | + back.pressed.connect(leave) |
| 74 | + var hint := Style.label("", 14, MUTED) |
| 75 | + hint.size_flags_vertical = Control.SIZE_SHRINK_CENTER |
| 76 | + Prompts.track(hint, func() -> String: |
| 77 | + return "%s: scroll %s: back" % [Prompts.keys_or_pad("↑ ↓", Prompts.if_pad("D-pad")), |
| 78 | + Prompts.either("Esc", &"ui_cancel")]) |
| 79 | + footer.add_child(hint) |
| 80 | + back.grab_focus() |
| 81 | + |
| 82 | +## Every section in display order: the hand-written ones, then the generated asset credits. |
| 83 | +static func sections() -> Array[Dictionary]: |
| 84 | + var result: Array[Dictionary] = [] |
| 85 | + result.append_array(read_sections(CREDITS)) |
| 86 | + result.append_array(read_sections(ASSET_CREDITS)) |
| 87 | + return result |
| 88 | + |
| 89 | +## The "sections" array of a credits file, keeping only dictionaries with a title. |
| 90 | +static func read_sections(path: String) -> Array[Dictionary]: |
| 91 | + var result: Array[Dictionary] = [] |
| 92 | + var data: Variant = JSON.parse_string(FileAccess.get_file_as_string(path)) |
| 93 | + if not (data is Dictionary and data.get("sections") is Array): |
| 94 | + push_error("Credits: %s is missing or has no sections array" % path) |
| 95 | + return result |
| 96 | + for section: Variant in data.sections: |
| 97 | + if section is Dictionary and str(section.get("title", "")) != "": |
| 98 | + result.append(section) |
| 99 | + return result |
| 100 | + |
| 101 | +func _add_section(section: Dictionary) -> void: |
| 102 | + _add_gap(18) |
| 103 | + _add_text(str(section.title).to_upper(), 22, Style.MINT) |
| 104 | + for line: Variant in section.get("lines", []): |
| 105 | + _add_text(str(line), 19) |
| 106 | + for track: Variant in section.get("tracks", []): |
| 107 | + if track is Dictionary: |
| 108 | + var used_by := str(track.get("used_by", "")) |
| 109 | + _add_text(str(track.get("title", "")) + (" · " + used_by if used_by != "" else ""), 17) |
| 110 | + for entry: Variant in section.get("credits", []): |
| 111 | + if not entry is Dictionary: |
| 112 | + continue |
| 113 | + _add_gap(4) |
| 114 | + _add_text(str(entry.get("credit", "")), 18) |
| 115 | + var details := PackedStringArray() |
| 116 | + for key in ["license", "used_by"]: |
| 117 | + if str(entry.get(key, "")) != "": |
| 118 | + details.append(str(entry[key])) |
| 119 | + _add_text(" · ".join(details), 14, MUTED) |
| 120 | + var links := PackedStringArray() |
| 121 | + for key in ["source_url", "license_url"]: |
| 122 | + if str(entry.get(key, "")) != "": |
| 123 | + links.append(str(entry[key])) |
| 124 | + if not links.is_empty(): |
| 125 | + _add_text(" ".join(links), 13, MUTED) |
| 126 | + |
| 127 | +func _add_text(text: String, font_size: int, color: Color = Style.CREAM) -> void: |
| 128 | + var label := Style.label(text, font_size, color) |
| 129 | + label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART |
| 130 | + label.size_flags_horizontal = Control.SIZE_EXPAND_FILL |
| 131 | + list.add_child(label) |
| 132 | + |
| 133 | +func _add_gap(height: float) -> void: |
| 134 | + var gap := Control.new() |
| 135 | + gap.custom_minimum_size.y = height |
| 136 | + list.add_child(gap) |
| 137 | + |
| 138 | +func _process(delta: float) -> void: |
| 139 | + # A scroll that didn't come from here (the mouse wheel) counts as a manual one. |
| 140 | + if scroll.scroll_vertical != int(offset): |
| 141 | + offset = scroll.scroll_vertical |
| 142 | + hold = MANUAL_HOLD |
| 143 | + var direction := Input.get_axis(&"ui_up", &"ui_down") |
| 144 | + held = held + delta if absf(direction) > 0.5 else 0.0 |
| 145 | + if held > HELD_AFTER: |
| 146 | + _scroll_by(signf(direction) * HELD_SPEED * delta) |
| 147 | + return |
| 148 | + if hold > 0.0: |
| 149 | + hold -= delta |
| 150 | + return |
| 151 | + var bottom := maxf(0.0, list.size.y - scroll.size.y) |
| 152 | + if offset >= bottom: |
| 153 | + offset = 0.0 |
| 154 | + hold = END_HOLD |
| 155 | + else: |
| 156 | + offset = minf(bottom, offset + SCROLL_SPEED * delta) |
| 157 | + if offset >= bottom: |
| 158 | + hold = END_HOLD |
| 159 | + scroll.scroll_vertical = int(offset) |
| 160 | + |
| 161 | +func _input(event: InputEvent) -> void: |
| 162 | + # One step per press; keyboard echo is ignored, since holding is handled in _process(). |
| 163 | + for action in [&"ui_up", &"ui_down"]: |
| 164 | + if event.is_action_pressed(action): |
| 165 | + _scroll_by(STEP if action == &"ui_down" else -STEP) |
| 166 | + get_viewport().set_input_as_handled() |
| 167 | + return |
| 168 | + if event.is_action_pressed("ui_cancel"): |
| 169 | + leave() |
| 170 | + get_viewport().set_input_as_handled() |
| 171 | + |
| 172 | +## Move the list by `pixels` within its bounds; a manual scroll pauses the automatic one. |
| 173 | +func _scroll_by(pixels: float) -> void: |
| 174 | + offset = clampf(offset + pixels, 0.0, maxf(0.0, list.size.y - scroll.size.y)) |
| 175 | + scroll.scroll_vertical = int(offset) |
| 176 | + hold = MANUAL_HOLD |
| 177 | + |
| 178 | +## AppRouter ignores go() mid-transition, so a press then is dropped rather than latched, and a |
| 179 | +## route change that fails lets the player try again. The autoload is looked up by path: suites |
| 180 | +## preload this script before autoloads exist. |
| 181 | +func leave() -> void: |
| 182 | + var router := get_node(^"/root/AppRouter") |
| 183 | + if leaving or router.busy: |
| 184 | + return |
| 185 | + leaving = true |
| 186 | + if not await router.go("hub"): |
| 187 | + leaving = false |
0 commit comments