harvest/game/scripts/ui_theme.gd
2026-10-07 13:41:38 -04:00

176 lines
8 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).
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
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