harvest/game/scripts/ui_theme.gd
Forrest 5d14fed3ec Merge #42: panels at phone scale
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 15:31:15 -04:00

381 lines
17 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; the panels' phone
## layout (panel_room, fit_margins, TouchScroll) is at the end.
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
## --- Panels on a phone (#42) ---
## The morning sign (ReviewPanel), the end screens (EndPanel), and the workbench's and the shed's
## shops (UpgradePanel) on a phone (MobilePort.active(); at any interface scale, iPads too):
## * each sits in panel_room(), the safe area (MobilePort.safe_rect) less PANEL_GAP, through
## fit_margins() on a MarginContainer between the panel and its full-screen CenterContainer
## (which still dims and blocks the whole screen);
## * its long part is a TouchScroll at most as tall as the room leaves, with its buttons below
## it, always in view; long lines wrap to the room's width;
## * its buttons and rows are at least MobilePort.minimum_target() tall.
## On desktop the margins are 0 and a TouchScroll is a plain box, so each is laid out as before.
const PANEL_GAP := 8.0
## Where a panel goes on a phone: the safe rect less PANEL_GAP on every side, in ci's canvas.
static func panel_room(ci: CanvasItem) -> Rect2:
return MobilePort.safe_rect(ci).grow(-PANEL_GAP)
## Puts the panel inside `wrap` (a MarginContainer, the only child of a full-screen
## CenterContainer) in `room` (canvas coordinates): margins out to the room's edges, plus
## `extra` (left, top, right, bottom) kept clear for drawing past the panel's rect. An empty
## room (desktop) is no margins at all.
static func fit_margins(wrap: MarginContainer, room: Rect2, extra: Vector4 = Vector4.ZERO) -> void:
var m := Vector4.ZERO
if room.has_area():
var full := (wrap.get_parent() as Control).get_global_rect()
m = Vector4(room.position.x - full.position.x, room.position.y - full.position.y,
full.end.x - room.end.x, full.end.y - room.end.y) + extra
var sides: Array[String] = ["left", "top", "right", "bottom"]
for i in 4:
set_constant(wrap, "margin_" + sides[i], maxi(ceili(m[i] - 0.001), 0))
## A theme constant override, set only when it changes (each set lays the control out again).
static func set_constant(c: Control, constant: String, value: int) -> void:
if c.get_theme_constant(constant) != value:
c.add_theme_constant_override(constant, value)
## A panel's long part (its only child is the content). On desktop (set_phone(false), the
## default) it's a plain box as tall as its content: no scrolling, no clipping, no bar. On a
## phone (set_phone(true)) fit() makes it at most as tall as the panel's room leaves; past that
## it scrolls, with a thin bar, and a finger drags it: a press inside it that moves DRAG_PT
## points (about Apple's touch slop) scrolls with the finger, and the button it started on lets
## go (NOTIFICATION_SCROLL_BEGIN), so lifting the finger presses (buys) nothing; a press that
## doesn't move that far is a tap for whatever's under it. Godot's own touch dragging (only on a
## real touchscreen, and only from between the buttons) is off, so a phone, the desktop
## preview, and the tests all take this one path. To check on a device: the slop feels right;
## there's no fling (the list stops when the finger lifts).
class TouchScroll extends ScrollContainer:
const DRAG_PT := 10.0
## The press under way has turned into a scroll.
var dragging := false
var _bar: Color
var _pad: float
var _finger := -1
var _moved := Vector2.ZERO
## bar: the scroll bar's colour; pad: room kept clear around the content on a phone (where
## it clips), for its buttons' focus frames.
func _init(bar: Color, pad: float = 0.0) -> void:
_bar = bar
_pad = pad
horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
scroll_deadzone = 1 << 30 # Godot's own touch dragging never starts: _input does it
set_phone(false)
func _ready() -> void:
get_viewport().gui_focus_changed.connect(_on_focus)
func set_phone(on: bool) -> void:
vertical_scroll_mode = ScrollContainer.SCROLL_MODE_AUTO if on else ScrollContainer.SCROLL_MODE_DISABLED
clip_contents = on
if not on:
custom_minimum_size = Vector2.ZERO
remove_theme_stylebox_override("panel")
_finger = -1
dragging = false
return
if has_theme_stylebox_override("panel"):
return
var pad := StyleBoxEmpty.new()
pad.set_content_margin_all(_pad)
add_theme_stylebox_override("panel", pad)
var track := StyleBoxFlat.new()
track.bg_color = Color(_bar, 0.12)
track.set_corner_radius_all(4)
track.content_margin_left = 4.0
track.content_margin_right = 4.0
var grab := StyleBoxFlat.new()
grab.bg_color = Color(_bar, 0.5)
grab.set_corner_radius_all(4)
var lit := grab.duplicate() as StyleBoxFlat
lit.bg_color = Color(_bar, 0.75)
var v := get_v_scroll_bar()
for key: String in ["scroll", "scroll_focus"]:
v.add_theme_stylebox_override(key, track)
v.add_theme_stylebox_override("grabber", grab)
for key: String in ["grabber_highlight", "grabber_pressed"]:
v.add_theme_stylebox_override(key, lit)
## The width it adds around its content on a phone: the pad and the bar.
func frame_width() -> float:
return get_theme_stylebox("panel").get_minimum_size().x + get_v_scroll_bar().get_combined_minimum_size().x
## On a phone: as wide as its content and the bar, and as tall as its content up to `most`,
## past which it scrolls.
func fit(most: float) -> void:
if get_child_count() == 0:
return
var content := (get_child(0) as Control).get_combined_minimum_size()
var h := content.y + get_theme_stylebox("panel").get_minimum_size().y
custom_minimum_size = Vector2(content.x + frame_width(), h if h <= most else floorf(maxf(most, 0.0)))
## Whether there's more than it shows.
func scrolls() -> bool:
var v := get_v_scroll_bar()
return vertical_scroll_mode != ScrollContainer.SCROLL_MODE_DISABLED and v.max_value - v.page > 0.5
func _input(event: InputEvent) -> void:
var touch := event as InputEventScreenTouch
if touch != null:
if touch.pressed:
if _finger == -1 and is_visible_in_tree() and scrolls() and _grabs(touch.position):
_finger = touch.index
_moved = Vector2.ZERO
dragging = false
elif touch.index == _finger:
_finger = -1
if dragging:
dragging = false
scroll_ended.emit()
return
var drag := event as InputEventScreenDrag
if drag == null or drag.index != _finger:
return
if not is_visible_in_tree():
_finger = -1
dragging = false
return
var v := get_v_scroll_bar()
if not dragging:
_moved += drag.relative
if _moved.length() < DRAG_PT * MobilePort.minimum_target(self) / MobilePort.TARGET_PT:
return
dragging = true
propagate_notification(Control.NOTIFICATION_SCROLL_BEGIN) # the button under the finger lets go
scroll_started.emit()
v.value -= _moved.y
else:
v.value -= drag.relative.y
get_viewport().set_input_as_handled()
## On a phone, keys and a gamepad scroll the button they focus into view (once the layout's
## in); a finger's press doesn't (follow_focus would: the list would jump under it).
func _on_focus(c: Control) -> void:
if vertical_scroll_mode == ScrollContainer.SCROLL_MODE_DISABLED or Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) or not is_ancestor_of(c):
return
_follow.call_deferred(c.get_instance_id())
func _follow(id: int) -> void:
var c := instance_from_id(id) as Control
if c != null and c.has_focus() and is_ancestor_of(c):
ensure_control_visible(c)
## Whether a finger landing here (the viewport's canvas) drags it: inside it, off its bar.
func _grabs(at: Vector2) -> bool:
var local := get_global_transform_with_canvas().affine_inverse() * at
var v := get_v_scroll_bar()
return Rect2(Vector2.ZERO, size).has_point(local) and not (v.visible and v.get_rect().has_point(local))