v0.9.0-alpha local build. Adds per-guest story vignettes, a scrollable Guest Book story section, live text scaling, Android haptics, a difficulty picker with descriptions, six repeating cosmetic hotel events, and isolated free play using verified symmetry variants. Save format v7 with automatic migration. QA is 9/9 including the free-play uniqueness suite.
255 lines
8.9 KiB
GDScript
255 lines
8.9 KiB
GDScript
class_name UI
|
|
extends RefCounted
|
|
|
|
## 360dp design tokens. All sizes here are dp-like canvas units.
|
|
|
|
const MARGIN := 16
|
|
const GAP := 12
|
|
const BTN_H := 54
|
|
const BTN_FONT := 19
|
|
const BODY := 16
|
|
const SMALL := 14
|
|
const CAPTION := 12
|
|
const H1 := 44
|
|
const H2 := 26
|
|
const H3 := 21
|
|
const RADIUS := 22
|
|
|
|
const FONT_HEADING_BASE := preload("res://assets/fonts/Fredoka-Variable.ttf")
|
|
const FONT_BODY_BASE := preload("res://assets/fonts/Nunito-Variable.ttf")
|
|
|
|
static var _heading_font: FontVariation
|
|
static var _body_font: FontVariation
|
|
|
|
|
|
static func heading_font() -> Font:
|
|
if _heading_font == null:
|
|
_heading_font = FontVariation.new()
|
|
_heading_font.base_font = FONT_HEADING_BASE
|
|
var ts := TextServerManager.get_primary_interface()
|
|
_heading_font.variation_opentype = { ts.name_to_tag("wght"): 600 }
|
|
return _heading_font
|
|
|
|
|
|
static func body_font() -> Font:
|
|
if _body_font == null:
|
|
_body_font = FontVariation.new()
|
|
_body_font.base_font = FONT_BODY_BASE
|
|
var ts := TextServerManager.get_primary_interface()
|
|
_body_font.variation_opentype = { ts.name_to_tag("wght"): 400 }
|
|
return _body_font
|
|
|
|
|
|
## The SaveGame autoload, fetched dynamically so helper scripts can compile
|
|
## before autoloads are registered (headless --script runs do exactly that).
|
|
static func _save_node() -> Node:
|
|
var loop := Engine.get_main_loop()
|
|
if loop is SceneTree:
|
|
return (loop as SceneTree).root.get_node_or_null("SaveGame")
|
|
return null
|
|
|
|
|
|
## User-facing UI scale (1.0 / 1.15 / 1.3), defaulting to 1.0 if missing or bad.
|
|
static func ui_scale() -> float:
|
|
var save := _save_node()
|
|
if save == null:
|
|
return 1.0
|
|
var value: Variant = save.get_setting("ui_scale")
|
|
var type := typeof(value)
|
|
if type == TYPE_FLOAT or type == TYPE_INT or type == TYPE_STRING:
|
|
var scale := float(value)
|
|
if scale > 0.0 and is_finite(scale):
|
|
return scale
|
|
return 1.0
|
|
|
|
|
|
## Font size with the UI scale applied, never below the 8 px minimum.
|
|
static func _scaled_font_size(base_size: int) -> int:
|
|
return int(max(8, round(base_size * ui_scale())))
|
|
|
|
|
|
## Light haptic feedback on Android. Strong is used for wins and unlocks.
|
|
static func haptic(strong: bool = false) -> void:
|
|
var save := _save_node()
|
|
if save == null or not bool(save.get_setting("haptics")):
|
|
return
|
|
if OS.get_name() == "Android":
|
|
Input.vibrate_handheld(55 if strong else 18)
|
|
|
|
|
|
## Sets a per-control font size while keeping it eligible for live text scaling.
|
|
static func set_font_size(control: Control, base_size: int) -> void:
|
|
control.set_meta("base_font_size", base_size)
|
|
control.add_theme_font_size_override("font_size", _scaled_font_size(base_size))
|
|
|
|
|
|
## Re-applies the saved text scale to controls that are already on screen.
|
|
static func apply_scale(root: Node) -> void:
|
|
if root is Control and root.has_meta("base_font_size"):
|
|
var base := int(root.get_meta("base_font_size"))
|
|
root.add_theme_font_size_override("font_size", _scaled_font_size(base))
|
|
for child in root.get_children():
|
|
apply_scale(child)
|
|
|
|
|
|
static func label(text: String, font_size: int = BODY, color: Color = Palette.INK, heading: bool = false) -> Label:
|
|
var l := Label.new()
|
|
l.text = text
|
|
l.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
|
l.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
|
|
l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
|
l.set_meta("base_font_size", font_size)
|
|
l.add_theme_font_size_override("font_size", _scaled_font_size(font_size))
|
|
l.add_theme_color_override("font_color", color)
|
|
l.add_theme_font_override("font", heading_font() if (heading or font_size >= 28) else body_font())
|
|
return l
|
|
|
|
|
|
static func heading(text: String, font_size: int = H2, color: Color = Palette.INK) -> Label:
|
|
return label(text, font_size, color, true)
|
|
|
|
|
|
static func body(text: String, color: Color = Palette.INK, font_size: int = BODY) -> Label:
|
|
return label(text, font_size, color, false)
|
|
|
|
|
|
static func caption(text: String, color: Color = Palette.MUTED) -> Label:
|
|
return label(text, CAPTION, color, false)
|
|
|
|
|
|
static func button(text: String, primary: bool = false, min_height: int = BTN_H) -> Button:
|
|
var b := Button.new()
|
|
b.text = text
|
|
b.custom_minimum_size = Vector2(0, min_height)
|
|
b.set_meta("base_font_size", BTN_FONT)
|
|
b.add_theme_font_override("font", heading_font())
|
|
b.add_theme_font_size_override("font_size", _scaled_font_size(BTN_FONT))
|
|
b.add_theme_color_override("font_color", Palette.WHITE if primary else Palette.INK)
|
|
b.add_theme_color_override("font_hover_color", Palette.WHITE if primary else Palette.INK)
|
|
b.add_theme_color_override("font_pressed_color", Palette.WHITE if primary else Palette.INK)
|
|
b.add_theme_color_override("font_focus_color", Palette.WHITE if primary else Palette.INK)
|
|
b.add_theme_stylebox_override("normal", _button_style(Palette.ORANGE if primary else Palette.PAPER))
|
|
b.add_theme_stylebox_override("hover", _button_style((Palette.ORANGE if primary else Palette.PAPER).lightened(0.05)))
|
|
b.add_theme_stylebox_override("pressed", _button_style((Palette.ORANGE if primary else Palette.PAPER).darkened(0.07)))
|
|
b.add_theme_stylebox_override("focus", _button_style((Palette.ORANGE if primary else Palette.PAPER).lightened(0.05)))
|
|
b.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
|
|
return b
|
|
|
|
|
|
static func round_button(icon_path: String, size: int = 48) -> Button:
|
|
var b := Button.new()
|
|
b.custom_minimum_size = Vector2(size, size)
|
|
b.icon = load(icon_path)
|
|
b.expand_icon = true
|
|
b.icon_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
|
b.alignment = HORIZONTAL_ALIGNMENT_CENTER
|
|
b.add_theme_stylebox_override("normal", _round_style(Palette.PAPER))
|
|
b.add_theme_stylebox_override("hover", _round_style(Palette.PAPER.lightened(0.04)))
|
|
b.add_theme_stylebox_override("pressed", _round_style(Palette.PAPER.darkened(0.06)))
|
|
b.add_theme_stylebox_override("focus", _round_style(Palette.PAPER.lightened(0.04)))
|
|
b.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
|
|
return b
|
|
|
|
|
|
static func icon(icon_path: String, size: int = 20, color: Color = Palette.MUTED) -> TextureRect:
|
|
var t := TextureRect.new()
|
|
t.texture = load(icon_path)
|
|
t.custom_minimum_size = Vector2(size, size)
|
|
t.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
|
|
t.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
|
|
t.modulate = color
|
|
t.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
return t
|
|
|
|
|
|
static func panel(color: Color = Palette.PAPER, radius: int = RADIUS, margin: int = 16) -> PanelContainer:
|
|
var p := PanelContainer.new()
|
|
var s := StyleBoxFlat.new()
|
|
s.bg_color = color
|
|
s.corner_radius_top_left = radius
|
|
s.corner_radius_top_right = radius
|
|
s.corner_radius_bottom_left = radius
|
|
s.corner_radius_bottom_right = radius
|
|
s.content_margin_left = margin
|
|
s.content_margin_right = margin
|
|
s.content_margin_top = margin
|
|
s.content_margin_bottom = margin
|
|
s.shadow_color = Color(0.29, 0.20, 0.14, 0.12)
|
|
s.shadow_size = 5
|
|
s.shadow_offset = Vector2(0, 3)
|
|
p.add_theme_stylebox_override("panel", s)
|
|
return p
|
|
|
|
|
|
static func spacer(height: int = GAP) -> Control:
|
|
var c := Control.new()
|
|
c.custom_minimum_size = Vector2(0, height)
|
|
c.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
return c
|
|
|
|
|
|
static func safe_top() -> int:
|
|
return 30 if OS.has_feature("mobile") else 20
|
|
|
|
|
|
static func safe_bottom() -> int:
|
|
return 24 if OS.has_feature("mobile") else 14
|
|
|
|
|
|
## Godot's ScrollContainer only sees a touch drag when the controls under the
|
|
## finger let the event through. PASS keeps buttons clickable while allowing
|
|
## the parent ScrollContainer to handle the same gesture. `skip` protects
|
|
## controls that must own their input, such as the tutorial Board.
|
|
static func pass_scroll_input(root: Node, skip: Array = []) -> void:
|
|
if root is Control and not skip.has(root):
|
|
root.mouse_filter = Control.MOUSE_FILTER_PASS
|
|
for child in root.get_children():
|
|
if skip.has(child):
|
|
continue
|
|
pass_scroll_input(child, skip)
|
|
|
|
|
|
## Short control hint that follows the saved placement mode.
|
|
static func place_hint() -> String:
|
|
var save := _save_node()
|
|
var double_tap := true if save == null else bool(save.get_setting("double_tap_place"))
|
|
if double_tap:
|
|
return "Tap = X · Double-tap or right-click = check in"
|
|
return "Tap = X · Hold or right-click = check in"
|
|
|
|
|
|
static func _button_style(color: Color) -> StyleBoxFlat:
|
|
var s := StyleBoxFlat.new()
|
|
s.bg_color = color
|
|
s.corner_radius_top_left = 26
|
|
s.corner_radius_top_right = 26
|
|
s.corner_radius_bottom_left = 26
|
|
s.corner_radius_bottom_right = 26
|
|
s.content_margin_left = 18
|
|
s.content_margin_right = 18
|
|
s.content_margin_top = 10
|
|
s.content_margin_bottom = 10
|
|
s.shadow_color = Color(0.29, 0.20, 0.14, 0.16)
|
|
s.shadow_size = 5
|
|
s.shadow_offset = Vector2(0, 3)
|
|
return s
|
|
|
|
|
|
static func _round_style(color: Color) -> StyleBoxFlat:
|
|
var s := StyleBoxFlat.new()
|
|
s.bg_color = color
|
|
s.corner_radius_top_left = 26
|
|
s.corner_radius_top_right = 26
|
|
s.corner_radius_bottom_left = 26
|
|
s.corner_radius_bottom_right = 26
|
|
# Keep high-resolution icons near their original visual size inside the
|
|
# round button instead of letting expand_icon fill the whole control.
|
|
s.content_margin_left = 10
|
|
s.content_margin_right = 10
|
|
s.content_margin_top = 10
|
|
s.content_margin_bottom = 10
|
|
s.shadow_color = Color(0.29, 0.20, 0.14, 0.12)
|
|
s.shadow_size = 4
|
|
s.shadow_offset = Vector2(0, 2)
|
|
return s
|