Merge #42: panels at phone scale
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
5d14fed3ec
6 changed files with 836 additions and 35 deletions
|
|
@ -7,7 +7,8 @@ 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).
|
||||
## touch_height, inset_to_safe_area, and overlay_rect fit the menus on a phone.
|
||||
## touch_height, inset_to_safe_area, and overlay_rect fit the menus on a phone; 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)
|
||||
|
|
@ -204,3 +205,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))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue