harvest/game/scripts/ui_theme.gd
Forrest 0c401171f3 #41: menus and settings at phone scale
On phones the main menu, pause menu, question box, and settings fit the
scaled canvas inside the safe area, with buttons and rows at least 44 pt
tall (19.5:9 at 1.6x, the iPhone SE at the 1.7 cap, 4:3 iPads). The
pause menu puts its buttons in a column beside the rest.

Phone settings: Audio, Touch (Interface size, Swap sticks, Camera
distance, Haptics, and a short touch-controls guide), Display (3D
resolution scale), and Gameplay. The list scrolls by dragging, and a
slider only moves after a mostly sideways drag. GameSettings saves the
touch settings under [touch] and applies them live (&"touch_controls",
the follow camera on phones). A hit buzzes the phone with Haptics on.
Desktop menus are unchanged. Test: game/tests/mobile/menus_test.gd.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 15:20:15 -04:00

206 lines
9.5 KiB
GDScript

class_name UiTheme
extends RefCounted
## The game's UI theme (the project's default theme, gui/theme/custom =
## res://game/resources/ui_theme.tres, written by game/tools/build_ui_theme.gd from build()):
## dark straw-trimmed panels, buttons that light up gold on hover and show a thick gold ring
## when focused (keyboard and gamepad players always see where they are), sliders, check
## boxes, and tabs to match. Labels keep the default look; the HUD sets its own sizes.
## sketch() is the hand-drawn theme the in-game panels use instead (the pause menu and its
## questions, the workbench and shed shops), to match the HUD's pouch panel (Sketch).
## touch_height, inset_to_safe_area, and overlay_rect fit the menus on a phone.
const TEXT := Color(0.95, 0.93, 0.85)
const GOLD := Color(0.95, 0.82, 0.45)
const DIM := Color(0.5, 0.5, 0.45)
static var _sketch: Theme
## The hand-drawn theme (Sketch, SketchBox), set as a panel's theme: the handwritten font with a
## dark outline on every label and button; panels in the pouch panel's dark wash and sketched ink
## frame; buttons as hotbar slots that light up on hover, with the hotbar's gold frame when
## focused; HSeparators as a pen rule; and SolidPanel, a PanelContainer variation without the
## see-through wash. Anything it leaves out (sliders, check boxes) falls back
## to the project's theme.
static func sketch() -> Theme:
if _sketch != null:
return _sketch
var t := Theme.new()
t.default_font = Sketch.font()
t.default_font_size = 28
var panel := SketchBox.new(Sketch.PANEL, Sketch.INK, Sketch.PANEL_INK)
_margins(panel, 26.0, 20.0)
t.set_stylebox("panel", "PanelContainer", panel)
t.set_stylebox("panel", "Panel", panel)
# The same panel without the see-through wash, for a question over another panel.
var solid := SketchBox.new(Color(Sketch.PANEL, 1.0), Sketch.INK, Sketch.PANEL_INK)
_margins(solid, 26.0, 20.0)
t.set_type_variation(&"SolidPanel", &"PanelContainer")
t.set_stylebox("panel", "SolidPanel", solid)
t.set_color("font_color", "Label", Sketch.TEXT)
t.set_color("font_outline_color", "Label", Sketch.INK)
t.set_constant("outline_size", "Label", 6)
var hover := Color(Sketch.SLOT.lightened(0.12), 0.8)
var styles := {
"normal": SketchBox.new(Sketch.SLOT, Sketch.INK, 2.5),
"hover": SketchBox.new(hover, Sketch.INK, 2.5),
"pressed": SketchBox.new(Color(Sketch.SLOT.darkened(0.3), 0.8), Sketch.GOLD, 2.5),
"disabled": SketchBox.new(Color(0.12, 0.12, 0.11, 0.5), Color(Sketch.INK, 0.6), 2.0),
"focus": SketchBox.new(Color(0, 0, 0, 0), Sketch.GOLD, 3.5),
}
for key: String in styles:
var b: SketchBox = styles[key]
_margins(b, 18.0, 6.0)
if key == "focus":
b.grow = 3.0
t.set_stylebox(key, "Button", b)
t.set_color("font_color", "Button", Sketch.TEXT)
t.set_color("font_hover_color", "Button", Color(1.0, 0.95, 0.7))
t.set_color("font_focus_color", "Button", Color(1.0, 0.95, 0.7))
t.set_color("font_hover_pressed_color", "Button", Sketch.GOLD)
t.set_color("font_pressed_color", "Button", Sketch.GOLD)
t.set_color("font_disabled_color", "Button", Sketch.DIM)
t.set_color("font_outline_color", "Button", Sketch.INK)
t.set_constant("outline_size", "Button", 6)
t.set_stylebox("separator", "HSeparator", SketchBox.new(Color(0, 0, 0, 0), Sketch.RULE, 2.0, true))
t.set_constant("separation", "HSeparator", 12)
_sketch = t
return t
## --- Menus on a phone (MobilePort): touch-sized buttons, inside the safe area ---
## The menus (MainMenu, PauseMenu, ConfirmBox, SettingsMenu) fit themselves on a phone when they
## open and whenever the canvas changes (the window, or the Interface size); desktop keeps the
## layout they're built with.
## A canvas item's height on a phone: at least the minimum touch target (44 pt,
## MobilePort.minimum_target). On desktop, h.
static func touch_height(ci: CanvasItem, h: float) -> float:
return maxf(h, ceilf(MobilePort.minimum_target(ci))) if MobilePort.active() else h
## A full-screen overlay (a CenterContainer menu, a child of a CanvasLayer) inset to the safe area,
## so what it centers sits clear of the notch, the rounded corners, and the home indicator. Its
## dim and its input still cover the whole screen (overlay_rect).
static func inset_to_safe_area(c: Control) -> void:
var safe := MobilePort.safe_rect(c)
var view := c.get_viewport_rect().size
c.offset_left = safe.position.x
c.offset_top = safe.position.y
c.offset_right = safe.end.x - view.x
c.offset_bottom = safe.end.y - view.y
## The whole screen in an overlay's coordinates (its own rect unless inset_to_safe_area moved it):
## what it dims, and where it takes the pointer so nothing behind it does.
static func overlay_rect(c: Control) -> Rect2:
return Rect2(-c.position, c.get_viewport_rect().size)
static func _margins(s: StyleBox, x: float, y: float) -> void:
s.content_margin_left = x
s.content_margin_right = x
s.content_margin_top = y
s.content_margin_bottom = y
static func build() -> Theme:
var t := Theme.new()
t.default_font_size = 22
t.set_stylebox("panel", "PanelContainer", ReviewPanel.backing())
t.set_stylebox("panel", "Panel", ReviewPanel.backing())
for type in ["Button", "OptionButton"]:
t.set_stylebox("normal", type, _box(Color(0.2, 0.24, 0.18), Color(0.45, 0.42, 0.3), 2))
t.set_stylebox("hover", type, _box(Color(0.3, 0.35, 0.24), GOLD, 2))
t.set_stylebox("pressed", type, _box(Color(0.15, 0.19, 0.13), GOLD, 2))
t.set_stylebox("disabled", type, _box(Color(0.14, 0.15, 0.13), Color(0.28, 0.28, 0.25), 2))
var focus := _box(Color(0, 0, 0, 0), GOLD, 4)
focus.draw_center = false
focus.set_expand_margin_all(3.0)
t.set_stylebox("focus", type, focus)
t.set_color("font_color", type, TEXT)
t.set_color("font_hover_color", type, Color(1.0, 0.95, 0.7))
t.set_color("font_focus_color", type, Color(1.0, 0.95, 0.7))
t.set_color("font_pressed_color", type, GOLD)
t.set_color("font_disabled_color", type, DIM)
for type in ["CheckBox", "CheckButton"]:
var none := StyleBoxEmpty.new()
t.set_stylebox("normal", type, none)
t.set_stylebox("pressed", type, none)
t.set_stylebox("hover", type, _box(Color(1, 1, 1, 0.05), Color(0, 0, 0, 0), 0))
t.set_stylebox("hover_pressed", type, _box(Color(1, 1, 1, 0.05), Color(0, 0, 0, 0), 0))
var focus := _box(Color(0, 0, 0, 0), GOLD, 3)
focus.draw_center = false
t.set_stylebox("focus", type, focus)
t.set_color("font_color", type, TEXT)
t.set_color("font_hover_color", type, Color(1.0, 0.95, 0.7))
t.set_color("font_focus_color", type, Color(1.0, 0.95, 0.7))
t.set_color("font_pressed_color", type, TEXT)
t.set_icon("checked", type, check_icon(true, false))
t.set_icon("unchecked", type, check_icon(false, false))
t.set_icon("checked_disabled", type, check_icon(true, true))
t.set_icon("unchecked_disabled", type, check_icon(false, true))
t.set_constant("h_separation", type, 12)
var track := _box(Color(0.22, 0.24, 0.2), Color(0, 0, 0, 0), 0)
track.content_margin_top = 4.0
track.content_margin_bottom = 4.0
t.set_stylebox("slider", "HSlider", track)
var filled := _box(Color(0.55, 0.78, 0.4), Color(0, 0, 0, 0), 0)
t.set_stylebox("grabber_area", "HSlider", filled)
t.set_stylebox("grabber_area_highlight", "HSlider", _box(Color(0.7, 0.9, 0.5), Color(0, 0, 0, 0), 0))
var slider_focus := _box(Color(0, 0, 0, 0), GOLD, 3)
slider_focus.draw_center = false
slider_focus.set_expand_margin_all(4.0)
t.set_stylebox("focus", "HSlider", slider_focus)
t.set_stylebox("tab_selected", "TabContainer", _box(Color(0.3, 0.35, 0.24), GOLD, 2))
t.set_stylebox("tab_unselected", "TabContainer", _box(Color(0.16, 0.19, 0.14), Color(0.35, 0.33, 0.25), 2))
t.set_stylebox("tab_hovered", "TabContainer", _box(Color(0.24, 0.28, 0.2), GOLD, 2))
var tab_focus := _box(Color(0, 0, 0, 0), GOLD, 3)
tab_focus.draw_center = false
t.set_stylebox("tab_focus", "TabContainer", tab_focus)
t.set_stylebox("panel", "TabContainer", _box(Color(0.12, 0.14, 0.11), Color(0.35, 0.33, 0.25), 2))
t.set_color("font_selected_color", "TabContainer", GOLD)
t.set_color("font_unselected_color", "TabContainer", TEXT)
t.set_color("font_hovered_color", "TabContainer", Color(1.0, 0.95, 0.7))
return t
## A 30 px check box: a dark rounded square with a light border, filled gold with a dark tick
## when checked.
static func check_icon(checked: bool, disabled: bool) -> ImageTexture:
const N := 30
var img := Image.create(N, N, false, Image.FORMAT_RGBA8)
var border := Color(0.78, 0.74, 0.6) if not disabled else Color(0.4, 0.4, 0.36)
var fill := (GOLD if not disabled else Color(0.45, 0.42, 0.32)) if checked else Color(0.14, 0.16, 0.12)
var tick := Color(0.12, 0.1, 0.05)
for y in N:
for x in N:
var p := Vector2(x + 0.5, y + 0.5)
var q := (p - Vector2(N, N) * 0.5).abs() - Vector2(N * 0.5 - 5.0, N * 0.5 - 5.0)
var d := Vector2(maxf(q.x, 0.0), maxf(q.y, 0.0)).length() + minf(maxf(q.x, q.y), 0.0) - 4.0 # rounded square
var c := Color(0, 0, 0, 0)
if d <= 0.0:
c = fill if d < -2.0 else border
if checked and d < -2.0 and minf(_segment(p, Vector2(8, 15.5), Vector2(13, 21)), _segment(p, Vector2(13, 21), Vector2(22.5, 9))) < 2.0:
c = tick
img.set_pixel(x, y, c)
return ImageTexture.create_from_image(img)
static func _segment(p: Vector2, a: Vector2, b: Vector2) -> float:
var ab := b - a
var t := clampf((p - a).dot(ab) / ab.length_squared(), 0.0, 1.0)
return p.distance_to(a + ab * t)
static func _box(bg: Color, border: Color, width: int) -> StyleBoxFlat:
var s := StyleBoxFlat.new()
s.bg_color = bg
s.border_color = border
s.set_border_width_all(width)
s.set_corner_radius_all(6)
s.content_margin_left = 16.0
s.content_margin_right = 16.0
s.content_margin_top = 8.0
s.content_margin_bottom = 8.0
return s