files-game/ui/widgets.gd
2026-10-01 23:01:56 -04:00

201 lines
7 KiB
GDScript

class_name Widgets
## Small builders shared by the HUD and the menu, so both make buttons, labels and forms the same
## way from Style's theme.
const SCANLINES := preload("res://ui/scanlines.gdshader")
static func button(text: String, fn: Callable, variation := "") -> Button:
var b := Button.new()
b.text = text
b.pressed.connect(fn)
b.focus_mode = Control.FOCUS_NONE
if variation != "":
b.theme_type_variation = variation
return b
static func label(text: String, variation := "") -> Label:
var l := Label.new()
l.text = text
if variation != "":
l.theme_type_variation = variation
return l
static func title(text: String) -> Label:
return label(text, "Title")
static func heading(text: String) -> Label:
return label(text, "Heading")
static func muted(text: String) -> Label:
var l := label(text, "Muted")
l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
return l
static func rich() -> RichTextLabel:
var rt := RichTextLabel.new()
rt.bbcode_enabled = true
rt.fit_content = true
rt.scroll_active = false
rt.selection_enabled = false
return rt
## A filled accent chip with dark text, e.g. "SALVAGE +4 Gb".
static func chip(text: String, accent := Style.ACCENT) -> PanelContainer:
var pc := PanelContainer.new()
pc.add_theme_stylebox_override("panel", Style.chip_box(accent))
var l := Label.new()
l.text = text
l.add_theme_color_override("font_color", Style.BG)
l.add_theme_font_override("font", Style.body_font(800))
l.add_theme_font_size_override("font_size", 12)
pc.add_child(l)
return pc
## A clickable chip: an accent-filled button.
static func chip_button(text: String, fn: Callable, accent := Style.ACCENT) -> Button:
var b := button(text, fn)
var box := Style.chip_box(accent)
b.add_theme_stylebox_override("normal", box)
var hover := box.tinted(accent.lightened(0.25), Color.WHITE)
hover.glow = 10.0
b.add_theme_stylebox_override("hover", hover)
b.add_theme_stylebox_override("pressed", box.tinted(Color.WHITE, Color.WHITE))
for c in ["font_color", "font_hover_color", "font_pressed_color"]:
b.add_theme_color_override(c, Style.BG)
b.add_theme_font_override("font", Style.body_font(800))
b.add_theme_font_size_override("font_size", 12)
return b
## The CRT lines over a panel: add as the panel's last child.
static func scanlines(strength := 0.16) -> ColorRect:
var r := ColorRect.new()
r.mouse_filter = Control.MOUSE_FILTER_IGNORE
r.set_anchors_preset(Control.PRESET_FULL_RECT)
var m := ShaderMaterial.new()
m.shader = SCANLINES
m.set_shader_parameter("strength", strength)
r.material = m
return r
## A small swatch in player `p`'s color and pattern: stripes for South, dots for North (the second
## cue beside the color, as on the tiles).
static func owner_badge(p: int, size := 14.0) -> Control:
var c := Control.new()
c.custom_minimum_size = Vector2(size, size)
c.mouse_filter = Control.MOUSE_FILTER_IGNORE
c.draw.connect(func():
var col: Color = Style.OWNER[p]
var r := Rect2(Vector2.ZERO, c.size)
c.draw_rect(r, Color(col, 0.18))
if p == 0:
var n := 4
for i in range(-n, n + 1):
var x := i * r.size.x / n * 0.75
c.draw_line(Vector2(x, r.size.y), Vector2(x + r.size.y, 0), col, 1.5, true)
else:
for yy in 3:
for xx in 3:
c.draw_circle(Vector2((xx + 0.5) * r.size.x / 3, (yy + 0.5) * r.size.y / 3), r.size.x / 10, col)
c.draw_rect(r, col, false, 1.0))
c.clip_contents = true
return c
# ---------------------------------------------------------------------------------------------
# Forms
## The new-game form into `box`: seed, side and bot style. `on_start` gets {seed, human, style}.
## With `on_cancel`, there's a Cancel button too.
static func start_form(box: Container, defaults: Dictionary, on_start: Callable, on_cancel := Callable()) -> void:
var grid := GridContainer.new()
grid.columns = 2
grid.add_theme_constant_override("h_separation", 14)
grid.add_theme_constant_override("v_separation", 8)
box.add_child(grid)
var seed_edit := LineEdit.new()
seed_edit.text = str(defaults.get("seed", randi_range(1, 999999)))
seed_edit.custom_minimum_size = Vector2(220, 0)
grid.add_child(label("SEED", "Muted"))
grid.add_child(seed_edit)
var side := OptionButton.new()
side.add_item("South (S1)")
side.add_item("North (S2)")
side.selected = defaults.get("human", 0)
side.focus_mode = Control.FOCUS_NONE
grid.add_child(label("YOU PLAY", "Muted"))
grid.add_child(side)
var style := OptionButton.new()
style.focus_mode = Control.FOCUS_NONE
var styles: Dictionary = GreedyBot.STYLES
var keys := styles.keys()
for k in keys:
style.add_item(styles[k].label)
style.selected = maxi(0, keys.find(defaults.get("style", "greedy")))
grid.add_child(label("OPPONENT", "Muted"))
grid.add_child(style)
var about := muted(styles[keys[style.selected]].about)
box.add_child(about)
style.item_selected.connect(func(i): about.text = styles[keys[i]].about)
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 10)
row.add_child(button("DEAL ▸", func():
on_start.call({"seed": int(seed_edit.text) if seed_edit.text.is_valid_int() else randi_range(1, 999999),
"human": side.selected, "style": keys[style.selected]}), "PrimaryButton"))
if on_cancel.is_valid():
row.add_child(button("Cancel", on_cancel))
box.add_child(row)
## The settings form into `box`. `on_done` gets the new settings.
static func settings_form(box: Container, settings: Dictionary, on_done: Callable) -> void:
box.add_child(label("WHEN THE GAME STOPS FOR YOU TO RESPOND", "Muted"))
var stops := OptionButton.new()
stops.focus_mode = Control.FOCUS_NONE
var stop_ids := ["combat", "all", "never"]
for text in ["In combat only", "Every opponent move", "Never"]:
stops.add_item(text)
stops.selected = maxi(0, stop_ids.find(settings.stops))
box.add_child(stops)
box.add_child(muted("Hold Shift to stop at the next window anyway."))
var skip := CheckBox.new()
skip.text = "Skip steps where End and Salvage are the only moves"
skip.button_pressed = settings.autoSkip
skip.focus_mode = Control.FOCUS_NONE
box.add_child(skip)
var check := CheckBox.new()
check.text = "Check the rule invariants after every move"
check.button_pressed = settings.checkInvariants
check.focus_mode = Control.FOCUS_NONE
box.add_child(check)
box.add_child(label("ANIMATION", "Muted"))
var anim := OptionButton.new()
anim.focus_mode = Control.FOCUS_NONE
var anim_ids: Array = GameSettings.ANIM_SPEEDS.keys()
for k in anim_ids:
anim.add_item(GameSettings.ANIM_SPEEDS[k].label)
anim.selected = maxi(0, anim_ids.find(settings.get("anim", "normal")))
box.add_child(anim)
var speed_label := label("")
box.add_child(speed_label)
var speed := HSlider.new()
speed.min_value = 0.0
speed.max_value = 1.5
speed.step = 0.05
speed.value = settings.botDelay
var show_speed := func(x): speed_label.text = "Bot pause between moves: %.2fs" % x
show_speed.call(speed.value)
speed.value_changed.connect(show_speed)
box.add_child(speed)
box.add_child(button("Done", func():
on_done.call({"stops": stop_ids[stops.selected], "autoSkip": skip.button_pressed,
"checkInvariants": check.button_pressed, "botDelay": speed.value, "anim": anim_ids[anim.selected]}), "PrimaryButton"))