Files
Ratdoku/scripts/UI.gd
T

173 lines
5.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
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.add_theme_font_size_override("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.add_theme_font_override("font", heading_font())
b.add_theme_font_size_override("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 = false
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
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
s.shadow_color = Color(0.29, 0.20, 0.14, 0.12)
s.shadow_size = 4
s.shadow_offset = Vector2(0, 2)
return s