#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>
This commit is contained in:
parent
5392ae09af
commit
e8729b41e8
6 changed files with 835 additions and 34 deletions
|
|
@ -10,12 +10,19 @@ extends CenterContainer
|
|||
## * The final assessment, once the farmers' return (FarmersReturn) has played or been
|
||||
## skipped: the rating and score (Assessment), the share kept per category with a bar, and
|
||||
## the vacation's totals, with Main menu (or New vacation).
|
||||
## On a phone (UiTheme's panels) it sits in the safe area: the text wraps to the room's width
|
||||
## (up to PHONE_WIDTH), scrolls (drag it) if it's still too tall, and the buttons stay below it,
|
||||
## in view and at least the touch target tall.
|
||||
|
||||
signal menu_requested
|
||||
|
||||
const DELAY := 1.4
|
||||
const GOLD := Color(0.95, 0.82, 0.45)
|
||||
const GREY := Color(0.75, 0.77, 0.72)
|
||||
const MARGIN := 34
|
||||
const BUTTON := Vector2(230.0, 56.0)
|
||||
## The text's widest on a phone (an iPad's room is wider): about the totals' longest line.
|
||||
const PHONE_WIDTH := 1180.0
|
||||
|
||||
var campaign: CampaignController
|
||||
## Set by Main: the Main menu button shows instead of New vacation.
|
||||
|
|
@ -37,6 +44,11 @@ var _menu: Button
|
|||
var _new: Button
|
||||
var _wait: float = -1.0
|
||||
var _pending_mode := ""
|
||||
var _safe: MarginContainer
|
||||
var _panel: PanelContainer
|
||||
var _scroll: UiTheme.TouchScroll
|
||||
var _box: VBoxContainer
|
||||
var _fit_queued := false
|
||||
|
||||
|
||||
func setup(p_campaign: CampaignController) -> void:
|
||||
|
|
@ -49,16 +61,25 @@ func setup(p_campaign: CampaignController) -> void:
|
|||
|
||||
func _ready() -> void:
|
||||
set_anchors_preset(Control.PRESET_FULL_RECT)
|
||||
var panel := PanelContainer.new()
|
||||
panel.add_theme_stylebox_override("panel", ReviewPanel.backing())
|
||||
add_child(panel)
|
||||
_safe = MarginContainer.new() # the safe area's margins on a phone (none on desktop)
|
||||
add_child(_safe)
|
||||
_panel = PanelContainer.new()
|
||||
_panel.add_theme_stylebox_override("panel", ReviewPanel.backing())
|
||||
_safe.add_child(_panel)
|
||||
var margin := MarginContainer.new()
|
||||
for side in ["left", "right", "top", "bottom"]:
|
||||
margin.add_theme_constant_override("margin_" + side, 34)
|
||||
panel.add_child(margin)
|
||||
margin.add_theme_constant_override("margin_" + side, MARGIN)
|
||||
_panel.add_child(margin)
|
||||
var page := VBoxContainer.new() # the text (scrolling on a phone), then the buttons
|
||||
page.add_theme_constant_override("separation", 16)
|
||||
margin.add_child(page)
|
||||
_scroll = UiTheme.TouchScroll.new(UiTheme.TEXT)
|
||||
page.add_child(_scroll)
|
||||
var box := VBoxContainer.new()
|
||||
box.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||||
box.add_theme_constant_override("separation", 16)
|
||||
margin.add_child(box)
|
||||
_scroll.add_child(box)
|
||||
_box = box
|
||||
_title = _label(box, 46)
|
||||
_rating = _label(box, 30, GOLD)
|
||||
_detail = _label(box, 22)
|
||||
|
|
@ -92,7 +113,7 @@ func _ready() -> void:
|
|||
var buttons := HBoxContainer.new()
|
||||
buttons.alignment = BoxContainer.ALIGNMENT_CENTER
|
||||
buttons.add_theme_constant_override("separation", 24)
|
||||
box.add_child(buttons)
|
||||
page.add_child(buttons)
|
||||
_retry = _button(buttons, "Retry the night")
|
||||
_retry.pressed.connect(func() -> void: campaign.retry_night())
|
||||
_menu = _button(buttons, "Main menu")
|
||||
|
|
@ -100,6 +121,8 @@ func _ready() -> void:
|
|||
_new = _button(buttons, "New vacation")
|
||||
_new.pressed.connect(func() -> void: campaign.new_campaign())
|
||||
hide_panel()
|
||||
box.minimum_size_changed.connect(_queue_fit) # the text reflowed
|
||||
get_viewport().size_changed.connect(_queue_fit)
|
||||
|
||||
|
||||
func hide_panel() -> void:
|
||||
|
|
@ -230,9 +253,46 @@ func _show_buttons() -> void:
|
|||
func _open(focus: Button) -> void:
|
||||
visible = true
|
||||
mouse_filter = Control.MOUSE_FILTER_STOP
|
||||
_fit()
|
||||
_scroll.scroll_vertical = 0
|
||||
focus.grab_focus()
|
||||
|
||||
|
||||
## Lays the screen out: on desktop as it always was; on a phone (UiTheme's panels) inside the
|
||||
## safe area, the text wrapped to the room's width and scrolling past what its height leaves.
|
||||
func _fit() -> void:
|
||||
var phone := MobilePort.active()
|
||||
_scroll.set_phone(phone)
|
||||
for l: Label in [_title, _rating, _detail, _stats]:
|
||||
l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART if phone else TextServer.AUTOWRAP_OFF
|
||||
for b: Button in [_retry, _menu, _new]:
|
||||
b.custom_minimum_size = Vector2(BUTTON.x, maxf(BUTTON.y, MobilePort.minimum_target(self))) if phone else BUTTON
|
||||
if not phone:
|
||||
UiTheme.fit_margins(_safe, Rect2())
|
||||
_box.custom_minimum_size.x = 0.0
|
||||
return
|
||||
var room := UiTheme.panel_room(self)
|
||||
UiTheme.fit_margins(_safe, room)
|
||||
var frame := 2.0 * MARGIN + _panel.get_theme_stylebox("panel").get_minimum_size().x + _scroll.frame_width()
|
||||
_box.custom_minimum_size.x = floorf(minf(PHONE_WIDTH, room.size.x - frame))
|
||||
var others := _panel.get_combined_minimum_size().y - _scroll.get_combined_minimum_size().y
|
||||
_scroll.fit(room.size.y - others)
|
||||
|
||||
|
||||
## 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()
|
||||
|
||||
|
||||
func _label(parent: Node, font_size: int, color: Color = Color(0.92, 0.92, 0.88)) -> Label:
|
||||
var l := Label.new()
|
||||
l.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||||
|
|
@ -245,7 +305,7 @@ func _label(parent: Node, font_size: int, color: Color = Color(0.92, 0.92, 0.88)
|
|||
func _button(parent: Control, text: String) -> Button:
|
||||
var b := Button.new()
|
||||
b.text = text
|
||||
b.custom_minimum_size = Vector2(230.0, 56.0)
|
||||
b.custom_minimum_size = BUTTON
|
||||
b.add_theme_font_size_override("font_size", 24)
|
||||
parent.add_child(b)
|
||||
return b
|
||||
|
|
|
|||
|
|
@ -9,6 +9,10 @@ extends CenterContainer
|
|||
## 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]
|
||||
|
|
@ -18,13 +22,38 @@ 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
|
||||
|
|
@ -48,6 +77,8 @@ func setup(p_campaign: CampaignController) -> void:
|
|||
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
|
||||
|
|
@ -55,7 +86,7 @@ func _ready() -> void:
|
|||
margins.content_margin_bottom = BOTTOM
|
||||
_board.add_theme_stylebox_override("panel", margins)
|
||||
_board.draw.connect(_draw_board)
|
||||
add_child(_board)
|
||||
_safe.add_child(_board)
|
||||
var column := VBoxContainer.new()
|
||||
column.add_theme_constant_override("separation", 0)
|
||||
_board.add_child(column)
|
||||
|
|
@ -65,16 +96,23 @@ func _ready() -> void:
|
|||
_title.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
|
||||
_title.custom_minimum_size = Vector2(0.0, BEAM)
|
||||
column.add_child(_title)
|
||||
var sheet := MarginContainer.new()
|
||||
_sheet = MarginContainer.new()
|
||||
for side: String in ["left", "right"]:
|
||||
sheet.add_theme_constant_override("margin_" + side, 48)
|
||||
sheet.add_theme_constant_override("margin_top", 26)
|
||||
sheet.add_theme_constant_override("margin_bottom", 32)
|
||||
column.add_child(sheet)
|
||||
_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)
|
||||
sheet.add_child(box)
|
||||
_scroll.add_child(box)
|
||||
_box = box
|
||||
_last_head = _ink(box, 38)
|
||||
_last_head.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||||
_rule = HSeparator.new()
|
||||
|
|
@ -124,7 +162,7 @@ func _ready() -> void:
|
|||
_intro.add_theme_constant_override("paragraph_spacing", 14)
|
||||
_start = Button.new()
|
||||
_start.text = "Start the day"
|
||||
_start.custom_minimum_size = Vector2(300.0, 62.0)
|
||||
_start.custom_minimum_size = START
|
||||
_start.add_theme_font_size_override("font_size", 32)
|
||||
_start.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
|
||||
var wood := {
|
||||
|
|
@ -135,9 +173,12 @@ func _ready() -> void:
|
|||
for key: String in wood:
|
||||
_start.add_theme_stylebox_override(key, wood[key])
|
||||
_start.pressed.connect(func() -> void: campaign.begin_day())
|
||||
box.add_child(_spacer(6.0))
|
||||
box.add_child(_start)
|
||||
_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.
|
||||
|
|
@ -178,10 +219,53 @@ func refresh() -> void:
|
|||
_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:
|
||||
|
|
@ -289,9 +373,10 @@ func _draw_board() -> void:
|
|||
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 * 0.5 + 8.0), PI * 0.5)
|
||||
Sketch.plank(b, Rect2(-h * 0.5 - 44.0, -27.0, h + 88.0, 54.0), Sketch.WOOD.darkened(0.08), 601.0 + k)
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ extends RefCounted
|
|||
## 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).
|
||||
## The panels' phone layout (panel_room, fit_margins, TouchScroll) is at the end.
|
||||
|
||||
const TEXT := Color(0.95, 0.93, 0.85)
|
||||
const GOLD := Color(0.95, 0.82, 0.45)
|
||||
|
|
@ -174,3 +175,177 @@ static func _box(bg: Color, border: Color, width: int) -> StyleBoxFlat:
|
|||
s.content_margin_top = 8.0
|
||||
s.content_margin_bottom = 8.0
|
||||
return s
|
||||
|
||||
|
||||
## --- Panels on a phone (#42) ---
|
||||
## The morning sign (ReviewPanel), the end screens (EndPanel), and the workbench's and the shed's
|
||||
## shops (UpgradePanel) on a phone (MobilePort.active(); at any interface scale, iPads too):
|
||||
## * each sits in panel_room(), the safe area (MobilePort.safe_rect) less PANEL_GAP, through
|
||||
## fit_margins() on a MarginContainer between the panel and its full-screen CenterContainer
|
||||
## (which still dims and blocks the whole screen);
|
||||
## * its long part is a TouchScroll at most as tall as the room leaves, with its buttons below
|
||||
## it, always in view; long lines wrap to the room's width;
|
||||
## * its buttons and rows are at least MobilePort.minimum_target() tall.
|
||||
## On desktop the margins are 0 and a TouchScroll is a plain box, so each is laid out as before.
|
||||
|
||||
const PANEL_GAP := 8.0
|
||||
|
||||
|
||||
## Where a panel goes on a phone: the safe rect less PANEL_GAP on every side, in ci's canvas.
|
||||
static func panel_room(ci: CanvasItem) -> Rect2:
|
||||
return MobilePort.safe_rect(ci).grow(-PANEL_GAP)
|
||||
|
||||
|
||||
## Puts the panel inside `wrap` (a MarginContainer, the only child of a full-screen
|
||||
## CenterContainer) in `room` (canvas coordinates): margins out to the room's edges, plus
|
||||
## `extra` (left, top, right, bottom) kept clear for drawing past the panel's rect. An empty
|
||||
## room (desktop) is no margins at all.
|
||||
static func fit_margins(wrap: MarginContainer, room: Rect2, extra: Vector4 = Vector4.ZERO) -> void:
|
||||
var m := Vector4.ZERO
|
||||
if room.has_area():
|
||||
var full := (wrap.get_parent() as Control).get_global_rect()
|
||||
m = Vector4(room.position.x - full.position.x, room.position.y - full.position.y,
|
||||
full.end.x - room.end.x, full.end.y - room.end.y) + extra
|
||||
var sides: Array[String] = ["left", "top", "right", "bottom"]
|
||||
for i in 4:
|
||||
set_constant(wrap, "margin_" + sides[i], maxi(ceili(m[i] - 0.001), 0))
|
||||
|
||||
|
||||
## A theme constant override, set only when it changes (each set lays the control out again).
|
||||
static func set_constant(c: Control, constant: String, value: int) -> void:
|
||||
if c.get_theme_constant(constant) != value:
|
||||
c.add_theme_constant_override(constant, value)
|
||||
|
||||
|
||||
## A panel's long part (its only child is the content). On desktop (set_phone(false), the
|
||||
## default) it's a plain box as tall as its content: no scrolling, no clipping, no bar. On a
|
||||
## phone (set_phone(true)) fit() makes it at most as tall as the panel's room leaves; past that
|
||||
## it scrolls, with a thin bar, and a finger drags it: a press inside it that moves DRAG_PT
|
||||
## points (about Apple's touch slop) scrolls with the finger, and the button it started on lets
|
||||
## go (NOTIFICATION_SCROLL_BEGIN), so lifting the finger presses (buys) nothing; a press that
|
||||
## doesn't move that far is a tap for whatever's under it. Godot's own touch dragging (only on a
|
||||
## real touchscreen, and only from between the buttons) is off, so a phone, the desktop
|
||||
## preview, and the tests all take this one path. To check on a device: the slop feels right;
|
||||
## there's no fling (the list stops when the finger lifts).
|
||||
class TouchScroll extends ScrollContainer:
|
||||
const DRAG_PT := 10.0
|
||||
|
||||
## The press under way has turned into a scroll.
|
||||
var dragging := false
|
||||
var _bar: Color
|
||||
var _pad: float
|
||||
var _finger := -1
|
||||
var _moved := Vector2.ZERO
|
||||
|
||||
## bar: the scroll bar's colour; pad: room kept clear around the content on a phone (where
|
||||
## it clips), for its buttons' focus frames.
|
||||
func _init(bar: Color, pad: float = 0.0) -> void:
|
||||
_bar = bar
|
||||
_pad = pad
|
||||
horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
|
||||
scroll_deadzone = 1 << 30 # Godot's own touch dragging never starts: _input does it
|
||||
set_phone(false)
|
||||
|
||||
func _ready() -> void:
|
||||
get_viewport().gui_focus_changed.connect(_on_focus)
|
||||
|
||||
func set_phone(on: bool) -> void:
|
||||
vertical_scroll_mode = ScrollContainer.SCROLL_MODE_AUTO if on else ScrollContainer.SCROLL_MODE_DISABLED
|
||||
clip_contents = on
|
||||
if not on:
|
||||
custom_minimum_size = Vector2.ZERO
|
||||
remove_theme_stylebox_override("panel")
|
||||
_finger = -1
|
||||
dragging = false
|
||||
return
|
||||
if has_theme_stylebox_override("panel"):
|
||||
return
|
||||
var pad := StyleBoxEmpty.new()
|
||||
pad.set_content_margin_all(_pad)
|
||||
add_theme_stylebox_override("panel", pad)
|
||||
var track := StyleBoxFlat.new()
|
||||
track.bg_color = Color(_bar, 0.12)
|
||||
track.set_corner_radius_all(4)
|
||||
track.content_margin_left = 4.0
|
||||
track.content_margin_right = 4.0
|
||||
var grab := StyleBoxFlat.new()
|
||||
grab.bg_color = Color(_bar, 0.5)
|
||||
grab.set_corner_radius_all(4)
|
||||
var lit := grab.duplicate() as StyleBoxFlat
|
||||
lit.bg_color = Color(_bar, 0.75)
|
||||
var v := get_v_scroll_bar()
|
||||
for key: String in ["scroll", "scroll_focus"]:
|
||||
v.add_theme_stylebox_override(key, track)
|
||||
v.add_theme_stylebox_override("grabber", grab)
|
||||
for key: String in ["grabber_highlight", "grabber_pressed"]:
|
||||
v.add_theme_stylebox_override(key, lit)
|
||||
|
||||
## The width it adds around its content on a phone: the pad and the bar.
|
||||
func frame_width() -> float:
|
||||
return get_theme_stylebox("panel").get_minimum_size().x + get_v_scroll_bar().get_combined_minimum_size().x
|
||||
|
||||
## On a phone: as wide as its content and the bar, and as tall as its content up to `most`,
|
||||
## past which it scrolls.
|
||||
func fit(most: float) -> void:
|
||||
if get_child_count() == 0:
|
||||
return
|
||||
var content := (get_child(0) as Control).get_combined_minimum_size()
|
||||
var h := content.y + get_theme_stylebox("panel").get_minimum_size().y
|
||||
custom_minimum_size = Vector2(content.x + frame_width(), h if h <= most else floorf(maxf(most, 0.0)))
|
||||
|
||||
## Whether there's more than it shows.
|
||||
func scrolls() -> bool:
|
||||
var v := get_v_scroll_bar()
|
||||
return vertical_scroll_mode != ScrollContainer.SCROLL_MODE_DISABLED and v.max_value - v.page > 0.5
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
var touch := event as InputEventScreenTouch
|
||||
if touch != null:
|
||||
if touch.pressed:
|
||||
if _finger == -1 and is_visible_in_tree() and scrolls() and _grabs(touch.position):
|
||||
_finger = touch.index
|
||||
_moved = Vector2.ZERO
|
||||
dragging = false
|
||||
elif touch.index == _finger:
|
||||
_finger = -1
|
||||
if dragging:
|
||||
dragging = false
|
||||
scroll_ended.emit()
|
||||
return
|
||||
var drag := event as InputEventScreenDrag
|
||||
if drag == null or drag.index != _finger:
|
||||
return
|
||||
if not is_visible_in_tree():
|
||||
_finger = -1
|
||||
dragging = false
|
||||
return
|
||||
var v := get_v_scroll_bar()
|
||||
if not dragging:
|
||||
_moved += drag.relative
|
||||
if _moved.length() < DRAG_PT * MobilePort.minimum_target(self) / MobilePort.TARGET_PT:
|
||||
return
|
||||
dragging = true
|
||||
propagate_notification(Control.NOTIFICATION_SCROLL_BEGIN) # the button under the finger lets go
|
||||
scroll_started.emit()
|
||||
v.value -= _moved.y
|
||||
else:
|
||||
v.value -= drag.relative.y
|
||||
get_viewport().set_input_as_handled()
|
||||
|
||||
## On a phone, keys and a gamepad scroll the button they focus into view (once the layout's
|
||||
## in); a finger's press doesn't (follow_focus would: the list would jump under it).
|
||||
func _on_focus(c: Control) -> void:
|
||||
if vertical_scroll_mode == ScrollContainer.SCROLL_MODE_DISABLED or Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) or not is_ancestor_of(c):
|
||||
return
|
||||
_follow.call_deferred(c.get_instance_id())
|
||||
|
||||
func _follow(id: int) -> void:
|
||||
var c := instance_from_id(id) as Control
|
||||
if c != null and c.has_focus() and is_ancestor_of(c):
|
||||
ensure_control_visible(c)
|
||||
|
||||
## Whether a finger landing here (the viewport's canvas) drags it: inside it, off its bar.
|
||||
func _grabs(at: Vector2) -> bool:
|
||||
var local := get_global_transform_with_canvas().affine_inverse() * at
|
||||
var v := get_v_scroll_bar()
|
||||
return Rect2(Vector2.ZERO, size).has_point(local) and not (v.visible and v.get_rect().has_point(local))
|
||||
|
|
|
|||
|
|
@ -11,12 +11,22 @@ extends CenterContainer
|
|||
## Escape / B, interact again, by walking off, or at nightfall. The scarecrow stands still and
|
||||
## can't shoot while either is open. Hand-drawn like the HUD's pouch panel (UiTheme.sketch): the
|
||||
## title over a pen rule, the tracks' names dim like the pouch's column headings, a slot per row.
|
||||
## On a phone (UiTheme's panels) it sits in the safe area: the rows are a list that scrolls
|
||||
## (UiTheme.TouchScroll: a finger that drags the list scrolls it and buys nothing, a tap on a row
|
||||
## buys), each row at least the touch target tall with its text wrapped to the room's width (up
|
||||
## to PHONE_ROW), and Close stays below the list, always in view.
|
||||
|
||||
## A purchase's result text. Nothing shows it on screen since #23 (the rows say what's missing);
|
||||
## tests listen to it.
|
||||
signal message(text: String, color: Color)
|
||||
signal bought(id: String)
|
||||
|
||||
const ROW := Vector2(860.0, 46.0)
|
||||
const CLOSE := Vector2(200.0, 48.0)
|
||||
const MARGIN := 8
|
||||
## A row's widest on a phone (an iPad's room is wider): about the workbench's longest.
|
||||
const PHONE_ROW := 1150.0
|
||||
|
||||
var campaign: CampaignController
|
||||
var structures: StructureSystem
|
||||
var player: PlayerCharacter
|
||||
|
|
@ -33,6 +43,11 @@ var slot: int = 0
|
|||
var _rows := {} # upgrade id -> Button
|
||||
var _title: Label
|
||||
var _close: Button
|
||||
var _safe: MarginContainer
|
||||
var _panel: PanelContainer
|
||||
var _scroll: UiTheme.TouchScroll
|
||||
var _list: VBoxContainer
|
||||
var _fit_queued := false
|
||||
|
||||
|
||||
func setup(p_campaign: CampaignController, p_structures: StructureSystem, p_player: PlayerCharacter, p_shop: String = "workbench") -> void:
|
||||
|
|
@ -40,7 +55,7 @@ func setup(p_campaign: CampaignController, p_structures: StructureSystem, p_play
|
|||
structures = p_structures
|
||||
player = p_player
|
||||
shop = p_shop
|
||||
var box := get_node("Panel/Margin/Box") as VBoxContainer
|
||||
var box := _list
|
||||
var track := ""
|
||||
for id in Upgrades.ORDER:
|
||||
var u: Dictionary = campaign.config.upgrades.get(id, {})
|
||||
|
|
@ -55,31 +70,27 @@ func setup(p_campaign: CampaignController, p_structures: StructureSystem, p_play
|
|||
box.add_child(head)
|
||||
var b := Button.new()
|
||||
b.alignment = HORIZONTAL_ALIGNMENT_LEFT
|
||||
b.custom_minimum_size = Vector2(860.0, 46.0)
|
||||
b.custom_minimum_size = ROW
|
||||
b.add_theme_font_size_override("font_size", 25)
|
||||
b.pressed.connect(func() -> void: _buy(id))
|
||||
box.add_child(b)
|
||||
_rows[id] = b
|
||||
_close = Button.new()
|
||||
_close.text = "Close"
|
||||
_close.custom_minimum_size = Vector2(200.0, 48.0)
|
||||
_close.add_theme_font_size_override("font_size", 28)
|
||||
_close.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
|
||||
_close.pressed.connect(close)
|
||||
box.add_child(_close)
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
set_anchors_preset(Control.PRESET_FULL_RECT)
|
||||
theme = UiTheme.sketch()
|
||||
var panel := PanelContainer.new()
|
||||
panel.name = "Panel"
|
||||
add_child(panel)
|
||||
_safe = MarginContainer.new() # the safe area's margins on a phone (none on desktop)
|
||||
_safe.name = "Safe"
|
||||
add_child(_safe)
|
||||
_panel = PanelContainer.new()
|
||||
_panel.name = "Panel"
|
||||
_safe.add_child(_panel)
|
||||
var margin := MarginContainer.new()
|
||||
margin.name = "Margin"
|
||||
for side in ["left", "right", "top", "bottom"]:
|
||||
margin.add_theme_constant_override("margin_" + side, 8)
|
||||
panel.add_child(margin)
|
||||
margin.add_theme_constant_override("margin_" + side, MARGIN)
|
||||
_panel.add_child(margin)
|
||||
var box := VBoxContainer.new()
|
||||
box.name = "Box"
|
||||
box.add_theme_constant_override("separation", 6)
|
||||
|
|
@ -88,8 +99,24 @@ func _ready() -> void:
|
|||
_title.add_theme_font_size_override("font_size", 36)
|
||||
box.add_child(_title)
|
||||
box.add_child(HSeparator.new())
|
||||
_scroll = UiTheme.TouchScroll.new(Sketch.TEXT, 4.0) # the rows (setup), scrolling on a phone
|
||||
box.add_child(_scroll)
|
||||
_list = VBoxContainer.new()
|
||||
_list.name = "List"
|
||||
_list.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||||
_list.add_theme_constant_override("separation", 6)
|
||||
_scroll.add_child(_list)
|
||||
_close = Button.new()
|
||||
_close.text = "Close"
|
||||
_close.custom_minimum_size = CLOSE
|
||||
_close.add_theme_font_size_override("font_size", 28)
|
||||
_close.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
|
||||
_close.pressed.connect(close)
|
||||
box.add_child(_close)
|
||||
visible = false
|
||||
mouse_filter = Control.MOUSE_FILTER_IGNORE
|
||||
_list.minimum_size_changed.connect(_queue_fit) # rows rewritten or reflowed
|
||||
get_viewport().size_changed.connect(_queue_fit)
|
||||
|
||||
|
||||
func open() -> void:
|
||||
|
|
@ -100,6 +127,8 @@ func open() -> void:
|
|||
mouse_filter = Control.MOUSE_FILTER_STOP
|
||||
player.start_channel(&"Idle_A")
|
||||
refresh()
|
||||
_fit()
|
||||
_scroll.scroll_vertical = 0
|
||||
var first: Button = _close
|
||||
for id in Upgrades.ORDER:
|
||||
if _rows.has(id) and not (_rows[id] as Button).disabled:
|
||||
|
|
@ -117,6 +146,42 @@ func close() -> void:
|
|||
player.stop_channel()
|
||||
|
||||
|
||||
## Lays the menu out: on desktop as it always was; on a phone (UiTheme's panels) inside the safe
|
||||
## area, the rows wrapped to the room's width and at least the touch target tall, and the list
|
||||
## scrolling past what the height leaves for it.
|
||||
func _fit() -> void:
|
||||
var phone := MobilePort.active()
|
||||
_scroll.set_phone(phone)
|
||||
var target := MobilePort.minimum_target(self) if phone else 0.0
|
||||
_close.custom_minimum_size = Vector2(CLOSE.x, maxf(CLOSE.y, target))
|
||||
var room := UiTheme.panel_room(self) if phone else Rect2()
|
||||
UiTheme.fit_margins(_safe, room)
|
||||
var width := ROW.x
|
||||
if phone:
|
||||
var frame := _panel.get_theme_stylebox("panel").get_minimum_size().x + 2.0 * MARGIN + _scroll.frame_width()
|
||||
width = floorf(minf(PHONE_ROW, room.size.x - frame))
|
||||
for id: String in _rows:
|
||||
var b := _rows[id] as Button
|
||||
b.custom_minimum_size = Vector2(width, maxf(ROW.y, target))
|
||||
b.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART if phone else TextServer.AUTOWRAP_OFF
|
||||
if phone:
|
||||
_scroll.fit(room.size.y - (_panel.get_combined_minimum_size().y - _scroll.get_combined_minimum_size().y))
|
||||
|
||||
|
||||
## Fits again once this frame's changes are in (rows rewritten, the screen changed size).
|
||||
func _queue_fit() -> void:
|
||||
if _fit_queued or not is_open:
|
||||
return
|
||||
_fit_queued = true
|
||||
_refit.call_deferred()
|
||||
|
||||
|
||||
func _refit() -> void:
|
||||
_fit_queued = false
|
||||
if is_open:
|
||||
_fit()
|
||||
|
||||
|
||||
## Rewrites every row for what's owned and affordable now.
|
||||
func refresh() -> void:
|
||||
var cfg := campaign.config
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue