files-game/ui/hud.gd
2026-10-02 09:54:59 -04:00

1419 lines
53 KiB
GDScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name Hud
extends CanvasLayer
## The 2D layer over the table (godot-plan.md §4, "Anything with text is 2D"): the status bar, a
## status strip per player, the card inspector, the source menu and move list, the terminal log
## drawer, the combat readout, the hand-focus row and the dialogs. It draws what Game hands it in
## refresh() and reports clicks as signals; it never builds an action. Its look comes from Style
## (ui-plan.md §2–3).
signal move_chosen(move: Dictionary)
signal source_picked(key: String)
signal cancel_pressed
signal end_command_pressed
signal view_toggled
signal new_game_requested(opts: Dictionary)
signal settings_changed(settings: Dictionary)
## Back to the menu: "" for the title screen, "setup" for the new-game screen.
signal menu_requested(screen: String)
## The hand-focus row: the card under the mouse ({key, card}, or {} for none), and a click on one.
signal hand_hovered(info: Dictionary)
signal hand_picked(key: String, at: Vector2)
signal hand_focus_changed(open: bool)
const STAT_LABELS := {
"capturesUnowned": "Open locations captured", "capturesUndefended": "Undefended enemy locations captured",
"capturesInCombat": "Locations won in combat", "attacks": "Attacks", "attacksWon": "Rolls won attacking",
"damageDealt": "Damage dealt", "anomaliesDestroyed": "Anomalies lost", "recalls": "Recalls",
"firewallsPlaced": "Firewalls placed", "bandwidthFromCaptures": "Gb from captures",
"bandwidthFromSalvage": "Gb from Salvage", "bandwidthOnCards": "Gb spent on cards", "cardsSalvaged": "Cards salvaged",
"hardwareInstalled": "Hardware installed", "programsInstalled": "Programs installed",
"executionsPlayed": "Executions played", "abilitiesUsed": "Abilities used",
}
## The terminal drawer's height, at the base size.
const DRAWER_H := 290.0
## Seconds the combat readout stays after a combat ends and its show has played out.
const COMBAT_LINGER := 2.5
## Cards in the hand-focus row at the base size, and how much the one under the mouse grows.
const FOCUS_CARD := Vector2(192, 269)
const FOCUS_GROW := 1.5
## Pixels from the bottom edge that open the hand-focus row.
const FOCUS_EDGE := 6.0
var theme: Theme
var _root: Control
var _status: Label
var _stack: Label
## One per player strip, the opponent's first: {panel, box, title, side, turn, bar, gb, body, badge}.
var _strips: Array = []
var _right: VBoxContainer
var _source_menu: PanelContainer
var _source_box: VBoxContainer
var _popover: PanelContainer
var _popover_box: VBoxContainer
var _inspector_art: TextureRect
var _inspector_blank: Control
var _inspector_kind: PanelContainer
var _inspector_text: RichTextLabel
var _salvage_row: HBoxContainer
var _inspect_key := ""
var _moves_box: VBoxContainer
var _modal: Control
var _modal_box: VBoxContainer
var _log_count := -1
var _moves_version := -1
var _primary_move := {}
var _art_cache := {}
var _settings := GameSettings.DEFAULTS.duplicate()
var _ctx := {}
# The terminal drawer.
var _drawer: Control
var _drawer_handle: Button
var _drawer_open := false
var _drawer_tween: Tween
var _log: RichTextLabel
var _prompt: Label
var _unread := 0
# The combat readout.
var _combat_panel: PanelContainer
var _combat: RichTextLabel
var _combat_id := -1
## When the readout starts counting down to going away: the end of the show of the combat's end.
var _combat_done_at := -1.0
## Whether the readout is up or on its way up (false: gone or fading out).
var _combat_shown := false
var _combat_tween: Tween
# The turn banner.
var _banner: PanelContainer
var _banner_title: Label
var _banner_sub: Label
var _banner_tween: Tween
# The hand-focus row.
var _focus: Control
var _focus_shade: TextureRect
var _focus_open := false
var _focus_by_edge := false
var _focus_cards: Array = []
var _focus_hover := -1
var _focus_sig := ""
func _ready() -> void:
layer = 1
theme = Style.theme()
_root = Control.new()
_root.theme = theme
_root.set_anchors_preset(Control.PRESET_FULL_RECT)
_root.mouse_filter = Control.MOUSE_FILTER_IGNORE
add_child(_root)
_build_top()
_build_strips()
_build_right()
_build_combat()
_build_banner()
_build_focus()
_build_drawer()
_build_center()
_modal = Control.new()
_modal.set_anchors_preset(Control.PRESET_FULL_RECT)
_modal.visible = false
_root.add_child(_modal)
get_viewport().size_changed.connect(_fit_layout)
_fit_layout()
# ---------------------------------------------------------------------------------------------
# Building
func _build_top() -> void:
var bar := PanelContainer.new()
bar.set_anchors_preset(Control.PRESET_TOP_WIDE)
bar.offset_left = Style.MARGIN
bar.offset_right = -Style.MARGIN
bar.offset_top = 4
var box := Style.chamfer_box(Style.ACCENT_DK, Style.PANEL, 1.0, 0.0, 10.0)
box.content_margin_top = 4
box.content_margin_bottom = 4
box.corners = ChamferBox.BOTTOM_LEFT | ChamferBox.BOTTOM_RIGHT
bar.add_theme_stylebox_override("panel", box)
_root.add_child(bar)
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 14)
bar.add_child(row)
var brand := RichTextLabel.new()
brand.bbcode_enabled = true
brand.fit_content = true
brand.autowrap_mode = TextServer.AUTOWRAP_OFF
brand.scroll_active = false
brand.mouse_filter = Control.MOUSE_FILTER_IGNORE
brand.add_theme_font_override("normal_font", Style.display_font(800))
brand.add_theme_font_size_override("normal_font_size", 16)
brand.text = "[color=%s]FILES[/color][color=%s]//[/color][color=%s]TCG[/color]" % [
Style.hex(Style.ACCENT), Style.hex(Style.ACCENT_DK), Style.hex(Style.SCREEN)]
brand.custom_minimum_size = Vector2(118, 0)
brand.size_flags_vertical = Control.SIZE_SHRINK_CENTER
row.add_child(brand)
_status = Label.new()
_status.add_theme_color_override("font_color", Style.SECONDARY)
_status.add_theme_font_size_override("font_size", 13)
_status.size_flags_vertical = Control.SIZE_SHRINK_CENTER
row.add_child(_status)
_stack = Label.new()
_stack.add_theme_color_override("font_color", Style.WARN)
_stack.add_theme_font_size_override("font_size", 13)
_stack.size_flags_horizontal = Control.SIZE_EXPAND_FILL
_stack.size_flags_vertical = Control.SIZE_SHRINK_CENTER
_stack.clip_text = true
row.add_child(_stack)
for spec in [["TOP VIEW T", func(): view_toggled.emit()], ["TERMINAL `", toggle_drawer],
["SETTINGS", show_settings], ["MENU", _confirm_menu]]:
var b := Widgets.button(spec[0], spec[1])
b.add_theme_font_size_override("font_size", 12)
row.add_child(b)
## The player strips, side by side under the status bar, over the table.
func _build_strips() -> void:
var row := HBoxContainer.new()
row.set_anchors_preset(Control.PRESET_TOP_WIDE)
row.offset_left = Style.MARGIN
row.offset_right = -Style.PANEL_RIGHT
row.offset_top = Style.BAR_H + 6
row.add_theme_constant_override("separation", 8)
_root.add_child(row)
for i in 2:
row.add_child(_make_strip())
## A player's status strip, one line: name, the Bandwidth bar, the location track.
func _make_strip() -> PanelContainer:
var pc := PanelContainer.new()
pc.size_flags_horizontal = Control.SIZE_EXPAND_FILL
var sb := Style.chamfer_box(Style.ACCENT_DK, Style.PANEL, 1.0, 0.0, 9.0)
sb.left_bar = 3.0
sb.content_margin_left = 12
pc.add_theme_stylebox_override("panel", sb)
var head := HBoxContainer.new()
head.add_theme_constant_override("separation", 8)
pc.add_child(head)
var badge_slot := CenterContainer.new()
head.add_child(badge_slot)
var title := Label.new()
title.add_theme_font_override("font", Style.display_font(700))
title.add_theme_font_size_override("font_size", 13)
head.add_child(title)
var side := Label.new()
side.theme_type_variation = "Micro"
head.add_child(side)
var bar := BandwidthBar.new()
bar.size_flags_horizontal = Control.SIZE_EXPAND_FILL
bar.size_flags_vertical = Control.SIZE_SHRINK_CENTER
head.add_child(bar)
var gb := RichTextLabel.new()
gb.bbcode_enabled = true
gb.fit_content = true
gb.scroll_active = false
gb.autowrap_mode = TextServer.AUTOWRAP_OFF
gb.custom_minimum_size = Vector2(78, 0)
gb.size_flags_vertical = Control.SIZE_SHRINK_CENTER
gb.mouse_filter = Control.MOUSE_FILTER_IGNORE
head.add_child(gb)
var body := Widgets.rich()
body.autowrap_mode = TextServer.AUTOWRAP_OFF
body.size_flags_vertical = Control.SIZE_SHRINK_CENTER
body.add_theme_font_size_override("normal_font_size", 12)
body.add_theme_font_size_override("bold_font_size", 12)
head.add_child(body)
var turn := Label.new()
turn.add_theme_font_override("font", Style.body_font(800))
turn.add_theme_font_size_override("font_size", 11)
# Held wide when empty too, so the two strips' bars line up.
turn.custom_minimum_size = Vector2(74, 0)
turn.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
head.add_child(turn)
_strips.append({"panel": pc, "box": sb, "title": title, "side": side, "turn": turn, "bar": bar, "gb": gb,
"body": body, "badge_slot": badge_slot, "badge_for": -1})
return pc
func _build_right() -> void:
_right = VBoxContainer.new()
_right.anchor_left = 1.0
_right.anchor_right = 1.0
_right.anchor_bottom = 1.0
_right.offset_left = -Style.PANEL_RIGHT + Style.MARGIN
_right.offset_right = -Style.MARGIN
_right.offset_top = Style.BAR_H + 6
_right.offset_bottom = -Style.MARGIN
_right.add_theme_constant_override("separation", 8)
_root.add_child(_right)
# The inspector: the card large, then what it is and what it's doing.
var insp := PanelContainer.new()
var ibox := Style.chamfer_box(Style.ACCENT_DK, Style.PANEL, 1.0, 0.0, 12.0)
ibox.ticks = true
ibox.set_content_margin_all(10)
insp.add_theme_stylebox_override("panel", ibox)
_right.add_child(insp)
var iv := VBoxContainer.new()
iv.add_theme_constant_override("separation", 6)
insp.add_child(iv)
var head := HBoxContainer.new()
iv.add_child(head)
var lbl := Widgets.label("// INSPECT", "Micro")
lbl.size_flags_horizontal = Control.SIZE_EXPAND_FILL
head.add_child(lbl)
_inspector_kind = Widgets.chip("")
_inspector_kind.visible = false
head.add_child(_inspector_kind)
var art_slot := Control.new()
art_slot.size_flags_horizontal = Control.SIZE_EXPAND_FILL
iv.add_child(art_slot)
_inspector_art = TextureRect.new()
_inspector_art.set_anchors_preset(Control.PRESET_FULL_RECT)
_inspector_art.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
_inspector_art.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
_inspector_art.visible = false
art_slot.add_child(_inspector_art)
_inspector_blank = Control.new()
_inspector_blank.set_anchors_preset(Control.PRESET_FULL_RECT)
_inspector_blank.draw.connect(_draw_blank_card)
art_slot.add_child(_inspector_blank)
_salvage_row = HBoxContainer.new()
_salvage_row.alignment = BoxContainer.ALIGNMENT_CENTER
_salvage_row.visible = false
iv.add_child(_salvage_row)
_inspector_text = Widgets.rich()
_inspector_text.fit_content = false
_inspector_text.scroll_active = true
_inspector_text.custom_minimum_size = Vector2(0, 118)
_inspector_text.add_theme_font_size_override("normal_font_size", 12)
_inspector_text.add_theme_font_size_override("bold_font_size", 13)
_inspector_text.text = _muted_bb("Point at a card, an Anomaly, a location or a gateway for its details. Tab opens your hand.")
iv.add_child(_inspector_text)
# The picked source's moves, or your responses, between the inspector and the move list.
_source_menu = PanelContainer.new()
_source_menu.visible = false
var sbox := Style.chamfer_box(Style.ACCENT, Color("#04161af0"), 1.5, 8.0, 9.0)
sbox.set_content_margin_all(9)
_source_menu.add_theme_stylebox_override("panel", sbox)
_right.add_child(_source_menu)
_source_box = VBoxContainer.new()
_source_box.add_theme_constant_override("separation", 5)
_source_menu.add_child(_source_box)
# Every move, grouped: you can click any from here.
var moves := PanelContainer.new()
moves.size_flags_vertical = Control.SIZE_EXPAND_FILL
var mbox := Style.chamfer_box(Style.ACCENT_DK, Style.PANEL, 1.0, 0.0, 10.0)
mbox.set_content_margin_all(9)
moves.add_theme_stylebox_override("panel", mbox)
_right.add_child(moves)
var mv := VBoxContainer.new()
mv.add_theme_constant_override("separation", 4)
moves.add_child(mv)
mv.add_child(Widgets.label("// MOVES", "Micro"))
var scroll := ScrollContainer.new()
scroll.size_flags_vertical = Control.SIZE_EXPAND_FILL
scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
mv.add_child(scroll)
_moves_box = VBoxContainer.new()
_moves_box.size_flags_horizontal = Control.SIZE_EXPAND_FILL
_moves_box.add_theme_constant_override("separation", 4)
scroll.add_child(_moves_box)
## An empty card outline where the art goes, for things that aren't cards.
func _draw_blank_card() -> void:
var c := _inspector_blank
var h := c.size.y
var w := minf(c.size.x, h * 750.0 / 1050.0)
var r := Rect2((c.size.x - w) / 2.0, 0, w, h)
var box := Style.chamfer_box(Color(Style.ACCENT_DK, 0.6), Color(Style.ACCENT, 0.03), 1.0, 0.0, w * Style.CHAMFER_RATIO)
box.draw(c.get_canvas_item(), r)
var f := Style.body_font()
var t := "// NO CARD"
var tw := f.get_string_size(t, HORIZONTAL_ALIGNMENT_LEFT, -1, 11).x
c.draw_string(f, r.get_center() + Vector2(-tw / 2.0, 4), t, HORIZONTAL_ALIGNMENT_LEFT, -1, 11, Color(Style.MUTED, 0.7))
## The combat readout: a transient panel over the top of the table while a combat runs.
func _build_combat() -> void:
_combat_panel = PanelContainer.new()
_combat_panel.mouse_filter = Control.MOUSE_FILTER_IGNORE
var box := Style.chamfer_box(Style.ATTACK, Color("#0a0306e8"), 1.5, 12.0, 12.0)
box.ticks = true
box.set_content_margin_all(11)
_combat_panel.add_theme_stylebox_override("panel", box)
_combat_panel.visible = false
_root.add_child(_combat_panel)
_combat = Widgets.rich()
_combat.mouse_filter = Control.MOUSE_FILTER_IGNORE
_combat.add_theme_font_size_override("normal_font_size", 12)
_combat.add_theme_font_size_override("bold_font_size", 13)
_combat_panel.add_child(_combat)
## The banner that sweeps across the table when a command starts.
func _build_banner() -> void:
_banner = PanelContainer.new()
_banner.mouse_filter = Control.MOUSE_FILTER_IGNORE
_banner.visible = false
var box := Style.chamfer_box(Style.ACCENT, Color(Style.BG, 0.86), 1.5, 14.0, 14.0)
box.ticks = true
box.content_margin_left = 36
box.content_margin_right = 36
box.content_margin_top = 10
box.content_margin_bottom = 12
_banner.add_theme_stylebox_override("panel", box)
_root.add_child(_banner)
var v := VBoxContainer.new()
v.alignment = BoxContainer.ALIGNMENT_CENTER
v.add_theme_constant_override("separation", 2)
_banner.add_child(v)
_banner_sub = Widgets.label("", "Micro")
_banner_sub.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
v.add_child(_banner_sub)
_banner_title = Widgets.heading("")
_banner_title.add_theme_font_size_override("font_size", 26)
_banner_title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
v.add_child(_banner_title)
## Sweeps `title` across the table on the motion curve: it rushes in, drifts while you read it,
## and rushes out. After `delay` seconds.
func banner(title: String, sub: String, color: Color, delay := 0.0) -> void:
if Motion.instant():
return
if _banner_tween != null:
_banner_tween.kill()
_banner_title.text = title
_banner_sub.text = sub
_banner_title.add_theme_color_override("font_color", color)
var box: ChamferBox = _banner.get_theme_stylebox("panel")
box.border_color = color
_banner.reset_size()
var vp := _root.get_viewport_rect().size
var region := Style.table_region(vp)
var y := region.position.y + region.size.y * 0.36 - _banner.size.y / 2.0
var mid := region.position.x + (region.size.x - _banner.size.x) / 2.0
_banner.position = Vector2(mid - 260, y)
_banner.modulate.a = 0.0
_banner_tween = create_tween()
if delay > 0.0:
_banner_tween.tween_interval(Motion.secs(delay))
_banner_tween.tween_callback(func(): _banner.visible = true)
_banner_tween.set_parallel()
Motion.rush_in(_banner_tween, _banner, "position:x", mid - 12, 0.26)
Motion.rush_in(_banner_tween, _banner, "modulate:a", 1.0, 0.2)
_banner_tween.set_parallel(false)
Motion.drift(_banner_tween, _banner, "position:x", mid + 12, 0.55)
_banner_tween.set_parallel()
Motion.rush_out(_banner_tween, _banner, "position:x", mid + 340, 0.22)
Motion.rush_out(_banner_tween, _banner, "modulate:a", 0.0, 0.22)
_banner_tween.set_parallel(false)
_banner_tween.tween_callback(func(): _banner.visible = false)
func finish_banner() -> void:
if _banner_tween != null:
_banner_tween.kill()
_banner.visible = false
# A skipped show is over now, so a finished combat's readout counts down from here.
var now := Time.get_ticks_msec() / 1000.0
if _combat_done_at > now:
_combat_done_at = now
## "+4 Gb" rising off player `p`'s Bandwidth readout, after `delay` seconds.
func pulse_bandwidth(p: int, delta: int, delay := 0.0) -> void:
if Motion.instant() or _ctx.is_empty():
return
var st: Dictionary = _strips[1] if p == _ctx.match.human else _strips[0]
var gb: Control = st.gb
var l := Label.new()
l.text = "%+d Gb" % delta
l.mouse_filter = Control.MOUSE_FILTER_IGNORE
l.add_theme_font_override("font", Style.body_font(800))
l.add_theme_font_size_override("font_size", 14)
l.add_theme_color_override("font_color", Style.OWNER[p].lightened(0.3) if delta > 0 else Style.BAD)
l.add_theme_color_override("font_outline_color", Style.BG)
l.add_theme_constant_override("outline_size", 5)
l.modulate.a = 0.0
_root.add_child(l)
# Placed when it starts, over the readout, so the strip's layout is settled by then.
var y := [0.0]
var tw := l.create_tween()
if delay > 0.0:
tw.tween_interval(Motion.secs(delay))
tw.tween_callback(func():
l.reset_size()
var r := gb.get_global_rect()
l.position = Vector2(r.position.x + (r.size.x - l.size.x) / 2.0, r.position.y - 4)
y[0] = l.position.y)
tw.set_parallel()
Motion.rush_in(tw, l, "modulate:a", 1.0, 0.12)
tw.tween_method(func(k): l.position.y = y[0] - 14.0 * k, 0.0, 1.0, Motion.secs(0.25)).set_trans(Tween.TRANS_EXPO).set_ease(Tween.EASE_OUT)
tw.set_parallel(false)
tw.tween_method(func(k): l.position.y = y[0] - 14.0 - 6.0 * k, 0.0, 1.0, Motion.secs(0.45))
tw.set_parallel()
Motion.rush_out(tw, l, "modulate:a", 0.0, 0.2)
tw.tween_method(func(k): l.position.y = y[0] - 20.0 - 10.0 * k, 0.0, 1.0, Motion.secs(0.2)).set_trans(Tween.TRANS_EXPO).set_ease(Tween.EASE_IN)
tw.set_parallel(false)
tw.tween_callback(l.queue_free)
## The hand-focus row (gameplay-plan.md §B3): your hand as large 2D cards over the bottom of the
## table. Tab, or the mouse at the bottom edge, opens it.
func _build_focus() -> void:
_focus = Control.new()
_focus.set_anchors_preset(Control.PRESET_FULL_RECT)
_focus.mouse_filter = Control.MOUSE_FILTER_IGNORE
_focus.visible = false
_root.add_child(_focus)
_focus_shade = TextureRect.new()
_focus_shade.mouse_filter = Control.MOUSE_FILTER_IGNORE
var grad := Gradient.new()
grad.set_color(0, Color(Style.BG, 0.0))
grad.set_color(1, Color(Style.BG, 0.92))
grad.add_point(0.35, Color(Style.BG, 0.55))
var gt := GradientTexture2D.new()
gt.gradient = grad
gt.fill_from = Vector2(0, 0)
gt.fill_to = Vector2(0, 1)
gt.width = 4
gt.height = 128
_focus_shade.texture = gt
_focus_shade.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
_focus_shade.stretch_mode = TextureRect.STRETCH_SCALE
_focus.add_child(_focus_shade)
## The terminal: the log as a CRT console in a drawer at the bottom, hidden until ` or its handle.
func _build_drawer() -> void:
_drawer = Control.new()
_drawer.mouse_filter = Control.MOUSE_FILTER_IGNORE
_drawer.anchor_top = 1.0
_drawer.anchor_bottom = 1.0
_drawer.anchor_right = 1.0
_root.add_child(_drawer)
_drawer_handle = Widgets.button("▴ // TERMINAL `", toggle_drawer)
_drawer_handle.add_theme_font_size_override("font_size", 11)
var hb := Style.chamfer_box(Style.ACCENT_DK, Style.PANEL, 1.0, 0.0, 7.0)
hb.corners = ChamferBox.TOP_LEFT | ChamferBox.TOP_RIGHT
hb.content_margin_left = 12
hb.content_margin_right = 12
hb.content_margin_top = 3
hb.content_margin_bottom = 3
_drawer_handle.add_theme_stylebox_override("normal", hb)
_drawer_handle.add_theme_stylebox_override("hover", hb.tinted(Color("#0c2730f0"), Style.ACCENT))
_drawer_handle.add_theme_stylebox_override("pressed", hb.tinted(Color(Style.ACCENT, 0.3), Style.ACCENT))
# The handle stays in the bottom-left corner, open or shut.
_drawer_handle.anchor_top = 1.0
_drawer_handle.anchor_bottom = 1.0
_drawer_handle.offset_left = Style.MARGIN
_drawer_handle.offset_top = -26
_root.add_child(_drawer_handle)
var panel := PanelContainer.new()
panel.name = "Screen"
var box := Style.chamfer_box(Style.ACCENT, Color("#020809f4"), 1.5, 10.0, 12.0)
box.corners = ChamferBox.TOP_LEFT | ChamferBox.TOP_RIGHT
box.ticks = true
box.set_content_margin_all(12)
panel.add_theme_stylebox_override("panel", box)
_drawer.add_child(panel)
var v := VBoxContainer.new()
v.add_theme_constant_override("separation", 4)
panel.add_child(v)
var head := RichTextLabel.new()
head.bbcode_enabled = true
head.fit_content = true
head.scroll_active = false
head.mouse_filter = Control.MOUSE_FILTER_IGNORE
head.add_theme_font_size_override("normal_font_size", 12)
head.text = "[color=%s]round@files:~$[/color] [color=%s]tail -f /var/log/match.log[/color]" % [Style.hex(Style.ACCENT), Style.hex(Style.SECONDARY)]
v.add_child(head)
_log = Widgets.rich()
_log.fit_content = false
_log.scroll_following = true
_log.scroll_active = true
_log.size_flags_vertical = Control.SIZE_EXPAND_FILL
_log.add_theme_font_size_override("normal_font_size", 12)
_log.add_theme_font_size_override("bold_font_size", 12)
v.add_child(_log)
_prompt = Label.new()
_prompt.add_theme_color_override("font_color", Style.ACCENT)
_prompt.add_theme_font_size_override("font_size", 12)
_prompt.text = "round@files:~$ ▌"
v.add_child(_prompt)
panel.add_child(Widgets.scanlines(0.18))
var blink := create_tween().set_loops()
blink.tween_property(_prompt, "modulate:a", 0.35, 0.5)
blink.tween_property(_prompt, "modulate:a", 1.0, 0.5)
func _build_center() -> void:
_popover = PanelContainer.new()
_popover.visible = false
var box := Style.chamfer_box(Style.ACCENT, Color("#05080af6"), 1.5, 10.0, 9.0)
box.set_content_margin_all(9)
_popover.add_theme_stylebox_override("panel", box)
_root.add_child(_popover)
_popover_box = VBoxContainer.new()
_popover_box.add_theme_constant_override("separation", 5)
_popover.add_child(_popover_box)
## Sizes what depends on the screen: the inspector's card, the drawer, the combat readout.
func _fit_layout() -> void:
var vp := _root.get_viewport_rect().size
var art_h := clampf(vp.y * 0.46, 220.0, 560.0)
art_h = minf(art_h, (Style.PANEL_RIGHT - 2 * Style.MARGIN - 20) * 1050.0 / 750.0)
_inspector_art.get_parent().custom_minimum_size = Vector2(0, art_h)
var region := Style.table_region(vp)
_drawer.offset_left = region.position.x
_drawer.offset_right = region.end.x - vp.x
var panel: Control = _drawer.get_node("Screen")
panel.size = Vector2(region.size.x, DRAWER_H)
panel.position = Vector2(0, 0)
_place_drawer(true)
_place_combat()
_layout_focus(true)
# ---------------------------------------------------------------------------------------------
# Drawing
## Redraws from the game's state. `ctx` holds: match, view, moves, selected, marks, thinking, paused,
## preview.
func refresh(ctx: Dictionary) -> void:
_ctx = ctx
var m: LocalMatch = ctx.match
var v: Dictionary = ctx.view
var me: int = m.human
var my_turn := m.human_to_act()
var window = RulesEngine.open_window(v)
var in_window: bool = my_turn and window != null and v.choice == null
var my_choice: bool = my_turn and v.choice != null and v.choice.player == me
_status.text = status_line(v, m) + (" · paused" if ctx.paused and not v.over else "")
var items := []
for it in v.stack:
items.append("%s (%s%s)" % [it.name.trim_prefix("Execute: "), State.player_name(it.player), " → %s" % it.target if it.target else ""])
_stack.text = "WAITING: " + " ← ".join(items) if not items.is_empty() else ""
_fill_strip(_strips[0], v, m.bot_side, me, v.active == m.bot_side and not v.over, not my_turn and not v.over)
_fill_strip(_strips[1], v, me, me, v.active == me and not v.over, my_turn and not v.over)
# What Space plays (as Game's key handler picks it): a pass, or else ending the step.
_primary_move = {}
if v.choice == null:
for t in ["pass", "end"]:
var hit := (ctx.moves as Array).filter(func(mv): return mv.action.type == t)
if not hit.is_empty():
_primary_move = hit[0]
break
var can_end: bool = my_turn and v.choice == null and window == null and v.active == me
_draw_source_menu(ctx, in_window, my_choice)
_draw_log(m)
_draw_combat(m, v, ctx)
_draw_salvage()
if _moves_version != m.version:
_moves_version = m.version
_draw_moves(ctx.moves, v.over, can_end)
if _focus_open:
_sync_focus()
if my_choice:
_show_choice(v, ctx.moves)
elif _modal.visible and _modal.get_meta("kind", "") == "choice":
close_modal()
if v.over and not _modal.visible and not _modal.get_meta("closed_over", false):
_show_game_over(m)
static func status_line(v: Dictionary, m: LocalMatch) -> String:
var me := m.human
if v.over:
if v.winner == null:
return "Game over: a draw"
return "Game over: %s" % ("you win" if v.winner == me else "%s wins" % State.player_name(v.winner))
var round := "Round %d · command %d" % [State.round_of(v, v.turn), v.turn] if v.rules.commands.on else "Turn %d" % v.turn
var whose := "your command" if v.active == me else "%s's command" % State.player_name(v.active)
var deciding := "your decision" if m.human_to_act() else "%s deciding" % State.player_name(m.bot_side)
return "%s · %s · %s step · %s" % [round, whose, Describe.STEP_NAMES[v.step], deciding]
## One player's strip: `active` is whose command it is, `deciding` whose decision.
func _fill_strip(st: Dictionary, v: Dictionary, p: int, me: int, active: bool, deciding: bool) -> void:
var pl: Dictionary = v.players[p]
var col: Color = Style.OWNER[p]
var sb: ChamferBox = st.box
sb.border_color = Color(col, 0.85 if deciding else 0.4)
sb.glow = 7.0 if deciding else 0.0
st.side.text = "YOU" if p == me else "BOT"
st.turn.text = "◀ COMMAND" if active else ""
if st.badge_for != p:
st.title.text = State.player_name(p).to_upper()
st.title.add_theme_color_override("font_color", col)
st.turn.add_theme_color_override("font_color", col)
for c in st.badge_slot.get_children():
c.queue_free()
st.badge_slot.add_child(Widgets.owner_badge(p, 13))
st.badge_for = p
var bar: BandwidthBar = st.bar
bar.color = col
bar.ticks = v.rules.firewallCost
var reach := Query.reachable_bandwidth(v, p) if p == me else int(pl.bandwidth)
bar.set_bandwidth(pl.bandwidth, reach)
var gb := "[b]%d[/b] [color=%s]Gb[/color]" % [pl.bandwidth, Style.hex(Style.MUTED)]
if reach > pl.bandwidth:
gb += " [color=%s]/%d[/color]" % [Style.hex(Color(col, 0.6)), reach]
st.gb.text = gb
var held := State.grid_count(v, p)
var need: int = v.rules.winLocations
var c := Style.hex(col)
var mu := Style.hex(Style.MUTED)
var track := "■".repeat(held) + "[color=%s]%s[/color]" % [Style.hex(Color(col, 0.3)), "□".repeat(maxi(0, need - held))]
st.body.text = "[color=%s]%s[/color] %d/%d [color=%s]LOC[/color]" % [c, track, held, need, mu]
## What a response window is open on, e.g. "Combat at B2, before the roll".
static func _window_subject(m: LocalMatch, v: Dictionary) -> String:
var w: Dictionary = RulesEngine.open_window(v)
var c = v.combat if v.combat != null and not v.combat.ended else null
var top = v.stack.back() if not v.stack.is_empty() else null
if w.kind == "preRoll" and c != null:
return "Combat at %s, before the roll" % c.loc
if w.kind == "postRoll" and c != null:
return "Combat at %s, after the roll" % c.loc
if top != null and top.player != m.human:
return "%s %s %s%s" % [State.player_name(top.player), "plays" if top.kind == "execution" else "uses", top.name, " on %s" % top.target if top.target else ""]
var what := _last_by(m, m.bot_side).trim_suffix(".")
return what if what != "" else "A response window is open"
static func _last_by(m: LocalMatch, p: int) -> String:
var lines := m.log_lines()
for i in range(lines.size() - 1, maxi(-1, lines.size() - 13), -1):
if lines[i].get("player") == p and lines[i].get("tag") != "turn":
return lines[i].text
return ""
func _draw_source_menu(ctx: Dictionary, in_window: bool, my_choice: bool) -> void:
_clear(_source_box)
var v: Dictionary = ctx.view
var selected: String = ctx.selected
_source_menu.visible = false
if my_choice:
return
if selected != "":
var mine := (ctx.moves as Array).filter(func(mv): return mv.source == selected)
var untargeted := mine.filter(func(mv): return mv.target == "none")
var targeted := mine.filter(func(mv): return mv.target != "none")
_source_box.add_child(Widgets.title(source_title(v, selected, ctx.match.human)))
if not targeted.is_empty():
_source_box.add_child(Widgets.muted("Click a highlighted %s." % _target_words(targeted)))
if ctx.get("preview", "") != "":
_source_box.add_child(Widgets.muted("Attack odds: %s" % ctx.preview))
for mv in untargeted:
_source_box.add_child(_move_button(mv))
_source_box.add_child(Widgets.button("Cancel Esc", func(): cancel_pressed.emit()))
_source_menu.visible = true
elif in_window:
var by_source := {}
for mv in ctx.moves:
if mv.action.type == "pass" or mv.action.type == "salvage":
continue
if not by_source.has(mv.source):
by_source[mv.source] = []
by_source[mv.source].append(mv)
_source_box.add_child(Widgets.title("// YOUR RESPONSES"))
_source_box.add_child(Widgets.muted("%s. Pick a glowing card or Ability, or pass." % _window_subject(ctx.match, v)))
for src in by_source:
var list: Array = by_source[src]
if list.size() == 1:
_source_box.add_child(_move_button(list[0]))
else:
var key: String = src
_source_box.add_child(Widgets.button("%s · %d ways" % [source_title(v, key, ctx.match.human), list.size()], func(): source_picked.emit(key)))
if not _primary_move.is_empty():
_source_box.add_child(_move_button(_primary_move, "Pass Space"))
_source_menu.visible = true
func _move_button(mv: Dictionary, text := "") -> Button:
return _list_button(text if text != "" else mv.label, func(): move_chosen.emit(mv))
func _list_button(text: String, fn: Callable) -> Button:
var b := Widgets.button(text, fn)
b.alignment = HORIZONTAL_ALIGNMENT_LEFT
b.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
b.add_theme_font_size_override("font_size", 13)
return b
static func source_title(v: Dictionary, key: String, me: int) -> String:
var r = Moves.ref_of_key(key)
if r != null:
return Describe.ref_name(v, r, me)
if key.begins_with("hand:"):
return key.substr(5)
if key.begins_with("loc:"):
var loc := key.substr(4)
var server = v.locations[loc].server
return loc + (": %s" % State.name_of(v, server) if server != null else "")
if key.begins_with("gate:"):
return "Gateway %s" % Describe.line_name(key.substr(5))
return key
static func _target_words(moves: Array) -> String:
var words := []
for mv in moves:
var kind: String = mv.target.split(":")[0]
var w: String = {"loc": "location", "anomaly": "Anomaly", "gate": "gateway", "hand": "card"}.get(kind, kind)
if not words.has(w):
words.append(w)
return " or ".join(words)
## A menu of moves at the mouse, for several moves on one target.
func show_popover(moves: Array, at: Vector2) -> void:
_clear(_popover_box)
for mv in moves:
_popover_box.add_child(_move_button(mv))
_popover_box.add_child(Widgets.button("Cancel", hide_popover))
_popover.visible = true
_popover.reset_size()
var vp := _root.get_viewport_rect().size
_popover.position = Vector2(minf(at.x, vp.x - _popover.size.x - 8), clampf(at.y, 8, vp.y - _popover.size.y - 8))
func hide_popover() -> void:
_popover.visible = false
func popover_open() -> bool:
return _popover.visible
func _draw_log(m: LocalMatch) -> void:
var lines := m.log_lines()
if lines.size() == _log_count:
return
if lines.size() < _log_count:
_log.clear()
_log_count = 0
var added := 0
for i in range(maxi(0, _log_count), lines.size()):
var l: Dictionary = lines[i]
if l.text == "":
continue
added += 1
var text := _escape(l.text)
if l.get("tag") == "turn":
_log.append_text("\n[color=%s][b]// %s[/b][/color]\n" % [Style.hex(Style.ACCENT), text.strip_edges().trim_prefix("— ").trim_suffix(" —")])
elif l.get("player") != null:
_log.append_text("[color=%s]>[/color] [color=%s]%s[/color]\n" % [Style.hex(Style.ACCENT_DK), Style.hex(Style.OWNER[l.player]), text])
elif l.get("tag") == "combat":
_log.append_text("[color=%s]![/color] [color=%s]%s[/color]\n" % [Style.hex(Style.ATTACK), Style.hex(Style.ATTACK.lightened(0.35)), text])
else:
_log.append_text("[color=%s] %s[/color]\n" % [Style.hex(Style.MUTED), text])
if _log_count >= 0 and not _drawer_open:
_unread += added
_log_count = lines.size()
_prompt.text = "round %d@files:~$ ▌" % State.round_of(m.s, m.s.turn) if m.s.rules.commands.on else "turn %d@files:~$ ▌" % m.s.turn
_draw_handle()
static func _escape(t: String) -> String:
return t.replace("[", "[lb]")
static func _muted_bb(t: String) -> String:
return "[color=%s]%s[/color]" % [Style.hex(Style.MUTED), t]
func _draw_combat(m: LocalMatch, v: Dictionary, ctx: Dictionary) -> void:
var c = m.combat()
if c == null:
_set_combat_visible(false)
return
if c.id != _combat_id:
_combat_id = c.id
_combat_done_at = -1.0
if c.done and _combat_done_at < 0:
# Counted from when the show of the result is over, so the readout outlasts the dice.
_combat_done_at = Time.get_ticks_msec() / 1000.0 + ctx.get("show_left", 0.0)
# This combat's log lines: from its start up to the next combat.
var own := m.log_lines().slice(c.logFrom)
var mine := own
for i in range(1, own.size()):
if own[i].get("tag") == "combat" and own[i].text.begins_with("Combat at"):
mine = own.slice(0, i)
break
var mu := Style.hex(Style.MUTED)
var fighter := func(f: Dictionary, role: String) -> String:
var name := Describe.ref_name(v, f.ref, m.human)
var parts := []
for part in f.parts:
parts.append("%+d %s" % [part.cores, part.source] if part.kind != "anomaly" else "%d %s" % [part.cores, part.source])
var dice := " ".join(f.dice.map(func(d): return "[%d]" % d))
var col := Style.hex(Style.OWNER[f.ref.player])
var out := "[color=%s][b]%s[/b][/color] %s [b]%d CORES[/b]\n[color=%s] %s[/color]\n" % [col, role, _escape(name), f.cores, mu, _escape(", ".join(parts))]
if dice != "":
out += " [color=%s]dice[/color] %s = [b]%d[/b]\n" % [mu, _escape(dice), Util.sum(f.dice)]
var took := 0
var dead := false
for l in mine:
var r = l.get("ref")
if r != null and l.get("amount") != null and State.same_ref(r, f.ref):
took += l.amount
dead = dead or l.text.contains("destroyed")
if v.rules.health.on and f.health > 0:
out += " [color=%s]health[/color] %d/%d" % [mu, f.healthLeft, f.health]
if took > 0:
out += " [color=%s][b]−%d%s[/b][/color]" % [Style.hex(Style.BAD), took, " DESTROYED" if dead else ""]
out += "\n"
return out
var stage := "ended early" if c.done and c.ended else ("over" if c.done else ("after the roll" if c.rolled else "before the roll"))
var kind: String = "Trace Route" if c.kind == "traceRoute" else c.kind
var out := "[color=%s][b]// COMBAT · %s[/b][/color] [color=%s]%s · %s[/color]\n" % [Style.hex(Style.ATTACK), c.loc, mu, kind, stage]
out += fighter.call(c.attacker, "ATK")
for d in c.defenders:
out += fighter.call(d, "DEF")
if not c.rolled and not c.done and not ctx.thinking:
var atk: int = c.attacker.cores
var def := 0
for d in c.defenders:
def += d.cores
var o := Odds.roll_odds(atk, def)
out += "[color=%s]%d vs %d · win %d%% · tie %d%% · lose %d%% · avg dmg %.1f dealt, %.1f taken[/color]\n" % [
Style.hex(Style.SECONDARY), atk, def, roundi(o.win * 100), roundi(o.tie * 100), roundi(o.lose * 100), o.damageDealt, o.damageTaken]
# What happened: the result, damage, captures and cards played in it, after the opening line.
var result := []
for i in range(1, mine.size()):
var l: Dictionary = mine[i]
if l.text == "" or l.text.begins_with("Roll:"):
continue
if l.get("tag") == "combat" or l.get("tag") == "capture":
var color: String = Style.hex(Style.OWNER[l.player]) if l.get("player") != null else Style.hex(Style.SCREEN)
result.append("[color=%s]%s[/color]" % [color, _escape(l.text)])
if not result.is_empty():
out += "\n".join(result.slice(maxi(0, result.size() - 4)))
_combat.text = out.strip_edges()
if _combat_panel.visible:
_place_combat()
_set_combat_visible(not c.done or Time.get_ticks_msec() / 1000.0 - _combat_done_at < COMBAT_LINGER)
func _set_combat_visible(on: bool) -> void:
# Each refresh asks again, so only a change starts a fade (restarting one every frame would
# hold it at its slow start forever).
if on == _combat_shown:
return
_combat_shown = on
if _combat_tween != null:
_combat_tween.kill()
if on:
_combat_panel.visible = true
_place_combat()
if Motion.instant():
_combat_panel.modulate.a = 1.0
return
_combat_panel.modulate.a = 0.0
_combat_tween = create_tween()
Motion.rush_in(_combat_tween, _combat_panel, "modulate:a", 1.0, 0.25)
else:
if Motion.instant() or not _combat_panel.visible:
_combat_panel.visible = false
return
_combat_tween = create_tween()
Motion.rush_out(_combat_tween, _combat_panel, "modulate:a", 0.0, 0.3)
_combat_tween.tween_callback(func(): _combat_panel.visible = false)
func _place_combat() -> void:
var vp := _root.get_viewport_rect().size
var region := Style.table_region(vp)
var w := minf(560.0, region.size.x - 40)
# The text needs its width before it's measured, or it wraps at zero width and stands tall.
_combat.custom_minimum_size = Vector2(w - 22, 0)
_combat_panel.custom_minimum_size = Vector2(w, 0)
_combat_panel.size = Vector2(w, 0)
_combat_panel.reset_size()
_combat_panel.position = Vector2(region.position.x + (region.size.x - w) / 2.0, Style.PANEL_TOP + 8)
func _process(_delta: float) -> void:
if _combat_shown and _combat_done_at > 0 and Time.get_ticks_msec() / 1000.0 - _combat_done_at >= COMBAT_LINGER:
_set_combat_visible(false)
_watch_focus_edge()
## Every move, grouped, with the keys for Space's move and (when `can_end`) ending the command.
func _draw_moves(moves: Array, over: bool, can_end: bool) -> void:
_clear(_moves_box)
if moves.is_empty():
_moves_box.add_child(Widgets.muted("Game over." if over else "Nothing to do until the bot moves."))
return
for g in Moves.group_moves(moves):
var t := Widgets.label("// " + String(g[0]).to_upper(), "Micro")
t.add_theme_color_override("font_color", Style.ACCENT)
_moves_box.add_child(t)
for mv in g[1]:
_moves_box.add_child(_move_button(mv, "%s Space" % mv.label if mv == _primary_move else ""))
if g[0] == "Turn" and can_end:
_moves_box.add_child(_list_button("End command Enter", func(): end_command_pressed.emit()))
## Shows a card or board object in the inspector: `info` is {art: card name or "", text: bbcode,
## key: what's inspected}.
func inspect(info: Dictionary) -> void:
var art: String = info.get("art", "")
_inspector_art.texture = _art(art) if art != "" else null
_inspector_art.visible = _inspector_art.texture != null
_inspector_blank.visible = not _inspector_art.visible
_inspector_text.text = info.get("text", "")
_inspector_text.scroll_to_line(0)
_inspect_key = info.get("key", "")
var d: Dictionary = _ctx.view.defs.get(art, {}) if not _ctx.is_empty() and art != "" else {}
_inspector_kind.visible = not d.is_empty()
if not d.is_empty():
var kind_name: String = Style.CLASS_NAMES.get(d.type, "Anomaly") if d.kind == "anomaly" else d.kind.capitalize()
_inspector_kind.get_child(0).text = kind_name.to_upper()
_inspector_kind.add_theme_stylebox_override("panel", Style.chip_box(Style.card_accent(d)))
_draw_salvage()
## The salvage chip under the inspected card (ui-plan.md §A6): only for a card in your hand you
## may salvage now, so it's found by pointing at a card without lighting up the whole hand.
func _draw_salvage() -> void:
_clear(_salvage_row)
var mv := _salvage_move(_inspect_key)
_salvage_row.visible = not mv.is_empty()
if not mv.is_empty():
_salvage_row.add_child(_salvage_chip(mv))
func _salvage_move(key: String) -> Dictionary:
if _ctx.is_empty() or not key.begins_with("hand:"):
return {}
for mv in _ctx.moves:
if mv.action.type == "salvage" and mv.source == key:
return mv
return {}
## "SALVAGE +7 Gb": a payment, not a discard, so it reads as Bandwidth gained.
func _salvage_chip(mv: Dictionary) -> Button:
var name: String = mv.source.substr(5)
var gb: int = _ctx.view.defs.get(name, {}).get("bandwidth", 0)
var b := Widgets.chip_button("SALVAGE +%d Gb" % gb, func(): move_chosen.emit(mv), Style.TYPE_ACCENT.server)
b.tooltip_text = "Discard %s for its printed %d Gb." % [name, gb]
return b
func _art(card_name: String) -> Texture2D:
if not _art_cache.has(card_name):
var path := CardFaces.CARD_DIR + CardFaces.slug(card_name) + ".png"
_art_cache[card_name] = load(path) if ResourceLoader.exists(path) else null
return _art_cache[card_name]
# ---------------------------------------------------------------------------------------------
# The terminal drawer
func toggle_drawer() -> void:
set_drawer(not _drawer_open)
func set_drawer(open: bool) -> void:
if open == _drawer_open:
return
_drawer_open = open
if open:
_unread = 0
_draw_handle()
_place_drawer(false)
func drawer_open() -> bool:
return _drawer_open
func _draw_handle() -> void:
if _drawer_open:
_drawer_handle.text = "▾ // TERMINAL `"
elif _unread > 0:
_drawer_handle.text = "▴ // TERMINAL · %d new `" % _unread
else:
_drawer_handle.text = "▴ // TERMINAL `"
## Slides the drawer on the motion curve: it rushes up, and rushes back down.
func _place_drawer(instant: bool) -> void:
var to := -DRAWER_H if _drawer_open else 0.0
if _drawer_tween != null:
_drawer_tween.kill()
if instant or Motion.instant():
_drawer.offset_top = to
_drawer.offset_bottom = to
return
_drawer_tween = create_tween().set_parallel()
if _drawer_open:
Motion.rush_in(_drawer_tween, _drawer, "offset_top", to, 0.32)
Motion.rush_in(_drawer_tween, _drawer, "offset_bottom", to, 0.32)
else:
Motion.rush_out(_drawer_tween, _drawer, "offset_top", to, 0.24)
Motion.rush_out(_drawer_tween, _drawer, "offset_bottom", to, 0.24)
# ---------------------------------------------------------------------------------------------
# The hand-focus row
func toggle_hand_focus() -> void:
set_hand_focus(not _focus_open)
func set_hand_focus(open: bool, by_edge := false) -> void:
if open == _focus_open:
return
_focus_open = open
_focus_by_edge = by_edge and open
_focus.visible = open
_focus_sig = ""
if open:
_sync_focus()
else:
_focus_hover = -1
for c in _focus_cards:
c.node.queue_free()
_focus_cards = []
hand_hovered.emit({})
hand_focus_changed.emit(open)
func hand_focus_open() -> bool:
return _focus_open
## Opens the row when the mouse reaches the bottom edge over the table, and closes a row opened
## that way once the mouse leaves it.
func _watch_focus_edge() -> void:
if _ctx.is_empty() or _modal.visible or _drawer_open:
return
var vp := _root.get_viewport_rect().size
var mouse := _root.get_local_mouse_position()
var region := Style.table_region(vp)
var over_table := mouse.x > region.position.x and mouse.x < region.end.x
if not _focus_open:
if over_table and mouse.y >= vp.y - FOCUS_EDGE and mouse.y <= vp.y and DisplayServer.window_is_focused():
set_hand_focus(true, true)
elif _focus_by_edge:
var card_h := FOCUS_CARD.y * Style.scale_of(vp).y
if mouse.y < vp.y - card_h * FOCUS_GROW - 50 or not over_table:
set_hand_focus(false)
## Rebuilds the row's cards when the hand changes, and restyles them for the marks.
func _sync_focus() -> void:
if _ctx.is_empty():
return
var v: Dictionary = _ctx.view
var me: int = _ctx.match.human
var hand: Array = v.players[me].hand
var sig := ",".join(hand.map(func(id): return str(id)))
if sig != _focus_sig:
_focus_sig = sig
for c in _focus_cards:
c.node.queue_free()
_focus_cards = []
_focus_hover = -1
for i in hand.size():
var name := State.name_of(v, hand[i])
_focus_cards.append(_make_focus_card(i, name))
for c in _focus_cards:
var mark = _ctx.marks.get(c.key)
var frame: ChamferBox = c.frame
if mark != null:
frame.border_color = Style.MARK[mark]
frame.glow = 14.0
frame.border_width = 2.5
else:
frame.border_color = Color(Style.ACCENT_DK, 0.0)
frame.glow = 0.0
frame.border_width = 1.0
c.node.queue_redraw()
c.frame_node.queue_redraw()
var mv := _salvage_move(c.key)
c.salvage.visible = not mv.is_empty() and c.index == _focus_hover
if not mv.is_empty():
_clear(c.salvage)
c.salvage.add_child(_salvage_chip(mv))
_layout_focus(true)
func _make_focus_card(i: int, card_name: String) -> Dictionary:
var node := Control.new()
node.mouse_filter = Control.MOUSE_FILTER_STOP
node.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
_focus.add_child(node)
var frame := Style.chamfer_box(Color(0, 0, 0, 0), Color(0, 0, 0, 0), 1.0, 0.0, 8.0)
var frame_node := Panel.new()
frame_node.mouse_filter = Control.MOUSE_FILTER_IGNORE
frame_node.add_theme_stylebox_override("panel", frame)
frame_node.set_anchors_preset(Control.PRESET_FULL_RECT)
frame_node.offset_left = -3
frame_node.offset_top = -3
frame_node.offset_right = 3
frame_node.offset_bottom = 3
node.add_child(frame_node)
var art := TextureRect.new()
art.mouse_filter = Control.MOUSE_FILTER_IGNORE
art.set_anchors_preset(Control.PRESET_FULL_RECT)
art.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
art.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
art.texture = _art(card_name)
node.add_child(art)
var salvage := HBoxContainer.new()
salvage.alignment = BoxContainer.ALIGNMENT_CENTER
# Above the card, clear of its text: the card grows upward from its bottom edge.
salvage.anchor_left = 0.0
salvage.anchor_right = 1.0
salvage.offset_top = -30
salvage.offset_bottom = -6
salvage.visible = false
node.add_child(salvage)
var key := Moves.hand_key(card_name)
var entry := {"node": node, "frame": frame, "frame_node": frame_node, "key": key, "card": card_name, "index": i, "salvage": salvage, "tween": null}
node.mouse_entered.connect(func(): _hover_focus(i))
node.mouse_exited.connect(func():
if _focus_hover == i:
_hover_focus(-1))
node.gui_input.connect(func(e: InputEvent):
if e is InputEventMouseButton and e.pressed:
if e.button_index == MOUSE_BUTTON_LEFT:
var at: Vector2 = node.get_global_rect().get_center()
set_hand_focus(false)
hand_picked.emit(key, at)
elif e.button_index == MOUSE_BUTTON_RIGHT:
set_hand_focus(false)
cancel_pressed.emit()
node.accept_event())
return entry
func _hover_focus(i: int) -> void:
if i == _focus_hover:
return
_focus_hover = i
for c in _focus_cards:
c.salvage.visible = c.index == i and not _salvage_move(c.key).is_empty()
_layout_focus(false)
if i >= 0 and i < _focus_cards.size():
hand_hovered.emit({"key": _focus_cards[i].key, "card": _focus_cards[i].card})
else:
hand_hovered.emit({})
## Lays out the row across the table's part of the screen; the hovered card grows and rises.
func _layout_focus(instant: bool) -> void:
if not _focus_open:
return
var vp := _root.get_viewport_rect().size
var sc := Style.scale_of(vp).y
var region := Style.table_region(vp)
var cw := FOCUS_CARD.x * sc
var ch := FOCUS_CARD.y * sc
var n := _focus_cards.size()
_focus_shade.position = Vector2(region.position.x - 20, vp.y - ch * FOCUS_GROW - 90)
_focus_shade.size = Vector2(region.size.x + 40, ch * FOCUS_GROW + 90)
if n == 0:
return
var room := region.size.x - 24
var step := minf(cw + 12, (room - cw) / maxf(1, n - 1)) if n > 1 else 0.0
var x0 := region.position.x + (region.size.x - (cw + step * (n - 1))) / 2.0
var base_y := vp.y - ch - 14 * sc
for c in _focus_cards:
var node: Control = c.node
var hovered: bool = c.index == _focus_hover
node.size = Vector2(cw, ch)
node.pivot_offset = Vector2(cw / 2.0, ch)
node.z_index = 10 if hovered else c.index
var pos := Vector2(x0 + step * c.index, base_y - (24 * sc if hovered else 0.0))
var grow := Vector2.ONE * (FOCUS_GROW if hovered else 1.0)
if c.tween != null:
c.tween.kill()
if instant or Motion.instant():
node.position = pos
node.scale = grow
continue
var tw := create_tween().set_parallel()
Motion.rush_in(tw, node, "position", pos, 0.22)
Motion.rush_in(tw, node, "scale", grow, 0.22)
c.tween = tw
# ---------------------------------------------------------------------------------------------
# Dialogs
func _open_modal(kind: String, width := 520) -> VBoxContainer:
_clear(_modal)
_modal.set_meta("kind", kind)
var dim := ColorRect.new()
dim.color = Color(Style.BG, 0.6 if kind != "choice" else 0.25)
dim.set_anchors_preset(Control.PRESET_FULL_RECT)
_modal.add_child(dim)
if kind != "choice":
_modal.add_child(Widgets.scanlines(0.1))
var center := CenterContainer.new()
center.set_anchors_preset(Control.PRESET_FULL_RECT)
_modal.add_child(center)
var pc := PanelContainer.new()
pc.custom_minimum_size = Vector2(width, 0)
var box := Style.chamfer_box(Style.ACCENT, Color(Style.BG, 0.93), 1.5, 16.0, 16.0)
box.ticks = true
box.set_content_margin_all(20)
pc.add_theme_stylebox_override("panel", box)
center.add_child(pc)
_modal_box = VBoxContainer.new()
_modal_box.add_theme_constant_override("separation", 10)
pc.add_child(_modal_box)
_modal.visible = true
return _modal_box
func close_modal() -> void:
_modal.visible = false
_clear(_modal)
_modal.set_meta("kind", "")
func modal_open() -> bool:
return _modal.visible
func _show_choice(v: Dictionary, moves: Array) -> void:
var key := "%s:%d:%d" % [v.choice.kind, moves.size(), v.seq]
if _modal.visible and _modal.get_meta("choice_key", "") == key:
return
var box := _open_modal("choice", 560)
_modal.set_meta("choice_key", key)
box.add_child(Widgets.label("// CHOOSE", "Micro"))
var prompt := Widgets.title(v.choice.prompt)
prompt.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
box.add_child(prompt)
var options := moves.filter(func(mv): return mv.action.type == "choose")
for mv in options:
box.add_child(_move_button(mv))
var blocked := []
for i in v.choice.options.size():
if not options.any(func(mv): return mv.action.index == i):
var o: Dictionary = v.choice.options[i]
blocked.append("%s (%d Gb)" % [o.label, o.get("cost", 0)])
if not blocked.is_empty():
box.add_child(Widgets.muted("Not enough Gb for: %s. Salvage a card first." % ", ".join(blocked)))
var row := HFlowContainer.new()
row.add_theme_constant_override("h_separation", 6)
row.add_theme_constant_override("v_separation", 6)
box.add_child(row)
for mv in moves.filter(func(x): return x.action.type == "salvage"):
var name: String = mv.source.substr(5)
var gb: int = v.defs.get(name, {}).get("bandwidth", 0)
var mvv: Dictionary = mv
row.add_child(Widgets.chip_button("%s +%d Gb" % [name.to_upper(), gb], func(): move_chosen.emit(mvv), Style.TYPE_ACCENT.server))
func _show_game_over(m: LocalMatch) -> void:
var s := m.s
var me := m.human
var box := _open_modal("over", 580)
box.add_child(Widgets.label("// MATCH COMPLETE", "Micro"))
var title := "DRAW" if s.winner == null else ("VICTORY" if s.winner == me else "DEFEAT")
var h := Widgets.heading(title)
h.add_theme_font_size_override("font_size", 34)
if s.winner != null:
h.add_theme_color_override("font_color", Style.OWNER[s.winner])
box.add_child(h)
var reason := "%s holds %d locations." % [State.player_name(s.winner), s.rules.winLocations] if s.endReason == "win" else "The turn limit was reached."
box.add_child(Widgets.muted("%s Round %d, seed %d." % [reason, State.round_of(s, s.turn), m.setup.seed]))
var grid := GridContainer.new()
grid.columns = 3
grid.add_theme_constant_override("h_separation", 18)
for t in ["", "%s (you)" % State.player_name(me), "%s (bot)" % State.player_name(m.bot_side)]:
grid.add_child(Widgets.title(t))
for k in Types.STAT_KEYS:
if not STAT_LABELS.has(k) or (s.stats[k][0] == 0 and s.stats[k][1] == 0):
continue
for t in [STAT_LABELS[k], str(s.stats[k][me]), str(s.stats[k][m.bot_side])]:
grid.add_child(Widgets.label(t))
box.add_child(grid)
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 10)
row.add_child(Widgets.button("Rematch", func():
close_modal()
new_game_requested.emit({"seed": m.setup.seed, "human": m.human, "style": m.setup.get("style", "greedy")}), "PrimaryButton"))
row.add_child(Widgets.button("New game", func():
close_modal()
menu_requested.emit("setup")))
row.add_child(Widgets.button("Look at the board", func():
close_modal()
_modal.set_meta("closed_over", true)))
box.add_child(row)
func _confirm_menu() -> void:
var box := _open_modal("leave", 420)
box.add_child(Widgets.heading("LEAVE GAME?"))
box.add_child(Widgets.muted("This game ends and you go back to the menu."))
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 10)
row.add_child(Widgets.button("Leave", func():
close_modal()
menu_requested.emit(""), "PrimaryButton"))
row.add_child(Widgets.button("Stay", close_modal))
box.add_child(row)
## Settings: when the game stops for you, skipping, speeds, invariant checks.
func show_settings(settings := {}) -> void:
if settings.is_empty():
settings = _settings
var box := _open_modal("settings", 520)
box.add_child(Widgets.heading("SETTINGS"))
Widgets.settings_form(box, settings, func(out):
_settings = out
close_modal()
settings_changed.emit(out))
func set_settings(settings: Dictionary) -> void:
_settings = settings.duplicate()
func reset_for_new_game() -> void:
_log.clear()
_log_count = -1
_unread = 0
_draw_handle()
_moves_version = -1
_combat_id = -1
_combat_done_at = -1.0
_set_combat_visible(false)
_modal.set_meta("closed_over", false)
hide_popover()
set_hand_focus(false)
func _clear(box: Node) -> void:
for c in box.get_children():
box.remove_child(c)
c.queue_free()