harvest/game/scripts/review_panel.gd
Forrest e8729b41e8 #42: panels at phone scale
On phones (MobilePort.active()) the morning sign, the end screens, and the
workbench's and shed's menus sit in the safe area; their long part scrolls
(UiTheme.TouchScroll: a finger drag scrolls and never presses, a tap does)
with Start the day / the end buttons / Close always in view below it; rows
and buttons are at least the touch target; long text wraps. Desktop layout
is unchanged (panels_test checks the 1920x1080 rects against the old ones).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 15:28:25 -04:00

421 lines
16 KiB
GDScript

class_name ReviewPanel
extends CenterContainer
## The morning screen (the untimed MORNING phase), between one night and the next day: a wooden
## sign, hand-drawn (Sketch) over the dimmed farm, the day on its top beam and a sheet of paper
## nailed under it with last night's results (from the night's log entry):
## * per category (crops, animals, supplies): what was taken, recovered, and lost;
## * the enemies defeated, by type;
## * the materials gathered over the day and night.
## Each row has its icon; a type or material with none isn't listed. "Start the day" (focused,
## so Enter / A work) ends it. The first morning explains the vacation instead of results. It
## stays down while the opening intro has the farm (intro.running).
## On a phone (UiTheme's panels) the sign stands in the safe area: the paper's text scrolls
## (drag it) and Start the day stays below it, in view and at least the touch target tall; the
## text is narrower if need be, and in a room shorter than TIGHT (a phone's, not an iPad's) the
## paper's margins and the posts' tops are a little shorter.
const CATEGORIES := ["Crops", "Animals", "Supplies"]
const ENEMY_TYPES := [EnemySystem.TYPE_CROW, EnemySystem.TYPE_THIEF, EnemySystem.TYPE_BREAKER, EnemySystem.TYPE_UFO]
## The sign's layout around its paper: the posts and beam ends either side of it, the top beam
## (the title is written on it), and the bottom beam.
const SIDE := 64.0
const BEAM := 92.0
const BOTTOM := 70.0
const WIDTH := 880.0
## The paper's margins around its text: either side, above, and below (PHONE_SHEET in a tight
## room, below).
const SHEET_SIDE := 48
const SHEET := Vector2i(26, 32)
const PHONE_SHEET := Vector2i(14, 16)
const START := Vector2(300.0, 62.0)
## How far the drawing reaches past the board: the top beam's ends either side (24, and their
## end grain), the posts above the top beam (POST_TOP, PHONE_POST_TOP in a tight room; their end
## grain END_GRAIN more), and the posts' feet below the board (into the ground: on a phone they
## may run past the safe area).
const BEAM_END := 40.0
const POST_TOP := 36.0
const PHONE_POST_TOP := 16.0
const END_GRAIN := 10.0
const FEET := 52.0
## A phone's room is shorter than this (an iPad's isn't): the paper's margins and the posts' tops
## shrink to give the text more of it.
const TIGHT := 1000.0
var campaign: CampaignController
## Set by FarmSim: the night plans (last night's title), and the opening intro.
var waves: WaveDirector
var intro: OpeningIntro
var _safe: MarginContainer
var _board: PanelContainer
var _sheet: MarginContainer
var _scroll: UiTheme.TouchScroll
var _box: VBoxContainer
var _start_gap: Control
var _post_top := POST_TOP
var _fit_queued := false
var _title: Label
var _last_head: Label
var _rule: HSeparator
var _grid: GridContainer
var _cells := {} # "Crops/taken" -> Label
var _lists: HBoxContainer
var _enemies := {} # type -> [icon, name, count]
var _no_enemies: Label
var _materials := {} # material -> [icon, name, count]
var _no_materials: Label
var _intro: Label
var _start: Button
func setup(p_campaign: CampaignController) -> void:
campaign = p_campaign
campaign.phase_changed.connect(func(_p: CampaignController.Phase, _prev: CampaignController.Phase) -> void: refresh())
refresh()
func _ready() -> void:
set_anchors_preset(Control.PRESET_FULL_RECT)
theme = UiTheme.sketch()
_safe = MarginContainer.new() # the safe area's margins on a phone (none on desktop)
add_child(_safe)
_board = PanelContainer.new()
var margins := StyleBoxEmpty.new()
margins.content_margin_left = SIDE
margins.content_margin_right = SIDE
margins.content_margin_bottom = BOTTOM
_board.add_theme_stylebox_override("panel", margins)
_board.draw.connect(_draw_board)
_safe.add_child(_board)
var column := VBoxContainer.new()
column.add_theme_constant_override("separation", 0)
_board.add_child(column)
_title = Label.new()
Sketch.style(_title, 48)
_title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
_title.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
_title.custom_minimum_size = Vector2(0.0, BEAM)
column.add_child(_title)
_sheet = MarginContainer.new()
for side: String in ["left", "right"]:
_sheet.add_theme_constant_override("margin_" + side, SHEET_SIDE)
_sheet.add_theme_constant_override("margin_top", SHEET.x)
_sheet.add_theme_constant_override("margin_bottom", SHEET.y)
column.add_child(_sheet)
var page := VBoxContainer.new() # the paper: its text (scrolling on a phone), then Start the day
page.add_theme_constant_override("separation", 10)
_sheet.add_child(page)
_scroll = UiTheme.TouchScroll.new(Sketch.PAPER_INK)
page.add_child(_scroll)
var box := VBoxContainer.new()
box.custom_minimum_size = Vector2(WIDTH, 0.0)
box.size_flags_horizontal = Control.SIZE_EXPAND_FILL
box.add_theme_constant_override("separation", 10)
_scroll.add_child(box)
_box = box
_last_head = _ink(box, 38)
_last_head.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
_rule = HSeparator.new()
_rule.add_theme_stylebox_override("separator", SketchBox.new(Color(0, 0, 0, 0), Color(Sketch.PAPER_INK, 0.5), 2.0, true))
box.add_child(_rule)
_grid = GridContainer.new()
_grid.columns = 5
_grid.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
_grid.add_theme_constant_override("h_separation", 18)
_grid.add_theme_constant_override("v_separation", 4)
box.add_child(_grid)
_grid.add_child(Control.new())
_grid.add_child(Control.new())
for head: String in ["Taken", "Recovered", "Lost"]:
var l := _ink(_grid, 28, Sketch.PAPER_DIM)
l.text = head
l.custom_minimum_size = Vector2(150.0, 0.0)
l.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
for i in CATEGORIES.size():
var cat: String = CATEGORIES[i]
_grid.add_child(SketchIcon.new(LootSystem.CATEGORY_KEYS[i], 52.0))
var label := _ink(_grid, 32)
label.text = cat
label.custom_minimum_size = Vector2(150.0, 0.0)
for col: String in ["taken", "recovered", "lost"]:
var l := _ink(_grid, 34)
l.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
_cells["%s/%s" % [cat, col]] = l
box.add_child(_spacer(8.0))
_lists = HBoxContainer.new()
_lists.alignment = BoxContainer.ALIGNMENT_CENTER
_lists.add_theme_constant_override("separation", 90)
box.add_child(_lists)
var enemies := _list(_lists, "Enemies defeated")
for t: int in ENEMY_TYPES:
var plural := EnemySystem.PLURALS[t]
_enemies[t] = _list_row(enemies, EnemySystem.TYPE_NAMES[t], plural[0].to_upper() + plural.substr(1))
_no_enemies = _ink(enemies, 30, Sketch.PAPER_DIM)
_no_enemies.text = "None"
var materials := _list(_lists, "Materials gathered")
for m in ResourceStore.MATERIALS:
_materials[m] = _list_row(materials, m, m.capitalize())
_no_materials = _ink(materials, 30, Sketch.PAPER_DIM)
_no_materials.text = "Nothing"
_intro = _ink(box, 28)
_intro.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
_intro.add_theme_constant_override("paragraph_spacing", 14)
_start = Button.new()
_start.text = "Start the day"
_start.custom_minimum_size = START
_start.add_theme_font_size_override("font_size", 32)
_start.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
var wood := {
"normal": SketchBox.new(Sketch.WOOD, Sketch.INK, 2.5),
"hover": SketchBox.new(Sketch.WOOD.lightened(0.15), Sketch.INK, 2.5),
"pressed": SketchBox.new(Sketch.WOOD.darkened(0.2), Sketch.GOLD, 2.5),
}
for key: String in wood:
_start.add_theme_stylebox_override(key, wood[key])
_start.pressed.connect(func() -> void: campaign.begin_day())
_start_gap = _spacer(6.0)
page.add_child(_start_gap)
page.add_child(_start)
visible = false
box.minimum_size_changed.connect(_queue_fit) # the text reflowed (a new width, new results)
get_viewport().size_changed.connect(_queue_fit)
## The dark, nearly opaque panel behind the menus and the end screens.
static func backing() -> StyleBoxFlat:
var bg := StyleBoxFlat.new()
bg.bg_color = Color(0.1, 0.12, 0.1)
bg.border_color = Color(0.55, 0.5, 0.35)
bg.set_border_width_all(2)
bg.set_corner_radius_all(6)
return bg
func refresh() -> void:
if campaign == null:
return
visible = campaign.state.phase == CampaignController.Phase.MORNING and not (intro != null and intro.running)
mouse_filter = Control.MOUSE_FILTER_STOP if visible else Control.MOUSE_FILTER_IGNORE
if not visible:
return
var s := campaign.state
var nights := campaign.config.campaign_nights
var first := s.night_log.is_empty()
for n: Control in [_last_head, _rule, _grid, _lists]:
n.visible = not first
_intro.visible = first
if first:
_title.text = "The farmers are away"
_intro.text = "\n".join([
"Keep the farm safe for %d nights until they come home: the crops, the animals in the pen, and the supplies in the shed." % nights,
Prompts.sign_day(),
Prompts.sign_night(),
])
else:
var e: Dictionary = s.night_log[s.night_log.size() - 1]
_title.text = "Morning of day %d of %d" % [s.night, nights]
var plan := waves.plan_for(int(e["night"])) if waves != null else null
_last_head.text = "Night %d: %s" % [e["night"], plan.title] if plan != null and plan.title != "" else "Night %d" % e["night"]
_fill_grid(e)
_fill_list(_enemies, _no_enemies, func(t: int) -> int: return int(e.get("killed_" + EnemySystem.TYPE_NAMES[t], 0)))
_fill_list(_materials, _no_materials, func(m: String) -> int: return int(e.get("gathered_" + m, 0)))
_fit()
_scroll.scroll_vertical = 0 # each morning's sheet from its top
_board.queue_redraw()
_focus_start.call_deferred()
## Lays the sign out for the screen: on desktop as it always was; on a phone (UiTheme's panels)
## inside the safe area (its posts' tops and its top beam's ends too), as wide as that allows up
## to WIDTH, with the paper's text scrolling past what the height leaves for it.
func _fit() -> void:
var phone := MobilePort.active()
var room := UiTheme.panel_room(self) if phone else Rect2()
var tight := phone and room.size.y < TIGHT
_scroll.set_phone(phone)
_post_top = PHONE_POST_TOP if tight else POST_TOP
var sheet := PHONE_SHEET if tight else SHEET
UiTheme.set_constant(_sheet, "margin_top", sheet.x)
UiTheme.set_constant(_sheet, "margin_bottom", sheet.y)
_start_gap.visible = not tight
_start.custom_minimum_size = Vector2(START.x, maxf(START.y, MobilePort.minimum_target(self))) if phone else START
if not phone:
UiTheme.fit_margins(_safe, Rect2())
_box.custom_minimum_size.x = WIDTH
return
var top := _post_top + END_GRAIN
UiTheme.fit_margins(_safe, room, Vector4(BEAM_END, top, BEAM_END, 0.0))
var frame := 2.0 * (BEAM_END + SIDE + SHEET_SIDE) + _scroll.frame_width()
_box.custom_minimum_size.x = floorf(minf(WIDTH, room.size.x - frame))
var others := _board.get_combined_minimum_size().y - _scroll.get_combined_minimum_size().y
_scroll.fit(room.size.y - top - others)
_board.queue_redraw()
## Fits again once this frame's changes are in (the text reflowed, the screen changed size).
func _queue_fit() -> void:
if _fit_queued or not visible:
return
_fit_queued = true
_refit.call_deferred()
func _refit() -> void:
_fit_queued = false
if visible:
_fit()
## Start the day takes the focus (unless the sign went down again meanwhile: a hidden button
## would still take Enter).
func _focus_start() -> void:
if is_visible_in_tree():
_start.grab_focus()
## The results table as text, one row per category ("Crops 4 3 1"), for tests.
func grid_text() -> String:
var rows: Array[String] = []
for cat: String in CATEGORIES:
rows.append("%s %s %s %s" % [cat, _cells[cat + "/taken"].text, _cells[cat + "/recovered"].text, _cells[cat + "/lost"].text])
return "\n".join(rows)
## The enemies listed, "Crows 12, Aliens 3" (or "None"), for tests.
func enemies_text() -> String:
return _list_text(_enemies, _no_enemies)
## The materials listed, "Wood 8, Straw 4" (or "Nothing"), for tests.
func materials_text() -> String:
return _list_text(_materials, _no_materials)
## Per category: (taken, recovered, lost) in a night's log entry. Animals taken include those
## that got out of an open pen (loose, not lost).
static func category_counts(e: Dictionary) -> Dictionary:
return {
"Crops": [int(e["crops_stolen"]), int(e["crops_recovered"]), int(e["crops_lost"])],
"Animals": [int(e["animals_stolen"]) + int(e["animals_strayed"]), int(e["animals_recovered"]), int(e["animals_lost"])],
"Supplies": [int(e["supplies_stolen"]), int(e["supplies_recovered"]), int(e["supplies_lost"])],
}
func _fill_grid(e: Dictionary) -> void:
var counts := category_counts(e)
for cat: String in CATEGORIES:
var c: Array = counts[cat]
for k in 3:
var l: Label = _cells["%s/%s" % [cat, ["taken", "recovered", "lost"][k]]]
l.text = str(c[k])
var color := Sketch.PAPER_INK if int(c[k]) > 0 else Sketch.PAPER_DIM
l.add_theme_color_override("font_color", Sketch.PAPER_RED if k == 2 and int(c[k]) > 0 else color)
## Shows the rows of rows (key -> [icon, name, count]) with a count above 0 (count_of(key)), or
## none when there aren't any.
func _fill_list(rows: Dictionary, none: Label, count_of: Callable) -> void:
var any := false
for key: Variant in rows:
var n: int = count_of.call(key)
for c: Control in rows[key]:
c.visible = n > 0
(rows[key][2] as Label).text = str(n)
any = any or n > 0
none.visible = not any
func _list_text(rows: Dictionary, none: Label) -> String:
var parts := PackedStringArray()
for key: Variant in rows:
var cells: Array = rows[key]
if (cells[2] as Control).visible:
parts.append("%s %s" % [(cells[1] as Label).text, (cells[2] as Label).text])
return ", ".join(parts) if not parts.is_empty() else none.text
## A headed table of icon, name, and count; returns the grid the rows go in.
func _list(parent: Control, heading: String) -> GridContainer:
var column := VBoxContainer.new()
column.add_theme_constant_override("separation", 4)
parent.add_child(column)
var head := _ink(column, 30, Sketch.PAPER_DIM)
head.text = heading
var grid := GridContainer.new()
grid.columns = 3
grid.add_theme_constant_override("h_separation", 14)
grid.add_theme_constant_override("v_separation", 0)
column.add_child(grid)
return grid
func _list_row(grid: GridContainer, icon: String, text: String) -> Array:
var i := SketchIcon.new(icon, 46.0)
grid.add_child(i)
var l := _ink(grid, 30)
l.text = text
l.custom_minimum_size = Vector2(150.0, 0.0)
var n := _ink(grid, 32)
n.custom_minimum_size = Vector2(44.0, 0.0)
n.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
return [i, l, n]
func _draw() -> void:
draw_rect(Rect2(Vector2.ZERO, size), Color(0.0, 0.0, 0.0, 0.35)) # dims the farm behind
## The sign behind the board's contents: two posts, the paper between them, a beam across its top
## (under the title) and one across its bottom, nails, and leaves.
func _draw_board() -> void:
var b := _board
var w := b.size.x
var h := b.size.y
var paper := Rect2(SIDE - 14.0, BEAM - 26.0, w - 2.0 * SIDE + 28.0, h - BEAM - BOTTOM + 52.0)
var posts: Array[float] = [SIDE + 34.0, w - SIDE - 34.0]
var reach := _post_top + h + FEET # a post's length, from its top to its feet
for k in 2:
b.draw_set_transform(Vector2(posts[k], (h + FEET - _post_top) * 0.5), PI * 0.5)
Sketch.plank(b, Rect2(-reach * 0.5, -27.0, reach, 54.0), Sketch.WOOD.darkened(0.08), 601.0 + k)
b.draw_set_transform_matrix(Transform2D.IDENTITY)
Sketch.paper(b, paper, 611.0)
Sketch.plank(b, Rect2(0.0, h - BOTTOM - 4.0, w, 62.0), Sketch.WOOD.darkened(0.04), 621.0)
Sketch.plank(b, Rect2(-24.0, 0.0, w + 48.0, BEAM), Sketch.WOOD, 631.0)
for x: float in [paper.position.x + 26.0, paper.end.x - 26.0]:
Sketch.nail(b, Vector2(x, paper.position.y + 46.0))
for k in 2:
Sketch.nail(b, Vector2(posts[k], BEAM * 0.5), 6.0)
Sketch.nail(b, Vector2(posts[k], h - BOTTOM + 27.0), 6.0)
Sketch.leaf(b, Vector2(28.0, 18.0), 46.0, -2.5, 641.0)
Sketch.leaf(b, Vector2(34.0, 22.0), 40.0, -1.4, 642.0)
Sketch.leaf(b, Vector2(w - 26.0, 16.0), 50.0, -0.5, 643.0)
Sketch.leaf(b, Vector2(w - 30.0, 24.0), 38.0, 0.4, 644.0, Sketch.LEAF.lightened(0.12))
Sketch.leaf(b, Vector2(posts[0] - 20.0, h - BOTTOM + 40.0), 42.0, 2.6, 645.0, Sketch.LEAF.darkened(0.1))
static func _spacer(height: float) -> Control:
var c := Control.new()
c.custom_minimum_size = Vector2(0.0, height)
return c
## A Label in ink on the paper (no outline).
func _ink(parent: Node, font_size: int, color: Color = Sketch.PAPER_INK) -> Label:
var l := Label.new()
Sketch.style(l, font_size, color, 0)
parent.add_child(l)
return l
## An icon (Sketch.ICONS) drawn in a square, side long, centered in the control.
class SketchIcon extends Control:
var id := ""
func _init(p_id: String, side: float) -> void:
id = p_id
custom_minimum_size = Vector2(side, side)
mouse_filter = Control.MOUSE_FILTER_IGNORE
func _draw() -> void:
var side := minf(size.x, size.y)
Sketch.icon(self, id, Rect2((size - Vector2(side, side)) * 0.5, Vector2(side, side)))