harvest/game/scripts/confirm_box.gd
Forrest 0c401171f3 #41: menus and settings at phone scale
On phones the main menu, pause menu, question box, and settings fit the
scaled canvas inside the safe area, with buttons and rows at least 44 pt
tall (19.5:9 at 1.6x, the iPhone SE at the 1.7 cap, 4:3 iPads). The
pause menu puts its buttons in a column beside the rest.

Phone settings: Audio, Touch (Interface size, Swap sticks, Camera
distance, Haptics, and a short touch-controls guide), Display (3D
resolution scale), and Gameplay. The list scrolls by dragging, and a
slider only moves after a mostly sideways drag. GameSettings saves the
touch settings under [touch] and applies them live (&"touch_controls",
the follow camera on phones). A hit buzzes the phone with Haptics on.
Desktop menus are unchanged. Test: game/tests/mobile/menus_test.gd.

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

109 lines
3.3 KiB
GDScript

class_name ConfirmBox
extends CenterContainer
## A yes / no question over the menus (Main): the message, the confirming button, and Cancel
## (focused, so a stray Enter / A never throws progress away). Escape / B cancels. Hand-drawn like
## the pause menu it mostly opens from (UiTheme.sketch). On a phone it sits in the middle of the
## safe area with touch-sized buttons (fit).
const BUTTON_HEIGHT := 54.0
var message: Label
var yes_button: Button
var no_button: Button
var is_open := false
var _on_yes: Callable
var _phone := false
func _ready() -> void:
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
theme = UiTheme.sketch()
var panel := PanelContainer.new()
panel.theme_type_variation = &"SolidPanel" # over the pause menu: none of it shows through
add_child(panel)
var margin := MarginContainer.new()
for side in ["left", "right", "top", "bottom"]:
margin.add_theme_constant_override("margin_" + side, 14)
panel.add_child(margin)
var box := VBoxContainer.new()
box.add_theme_constant_override("separation", 22)
margin.add_child(box)
message = Label.new()
message.custom_minimum_size = Vector2(560.0, 0.0)
message.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
message.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
message.add_theme_font_size_override("font_size", 30)
box.add_child(message)
var row := HBoxContainer.new()
row.alignment = BoxContainer.ALIGNMENT_CENTER
row.add_theme_constant_override("separation", 24)
box.add_child(row)
yes_button = Button.new()
yes_button.custom_minimum_size = Vector2(240.0, BUTTON_HEIGHT)
yes_button.add_theme_font_size_override("font_size", 30)
yes_button.pressed.connect(_yes)
row.add_child(yes_button)
no_button = Button.new()
no_button.text = "Cancel"
no_button.custom_minimum_size = Vector2(200.0, BUTTON_HEIGHT)
no_button.add_theme_font_size_override("font_size", 30)
no_button.pressed.connect(close)
row.add_child(no_button)
visible = false
mouse_filter = Control.MOUSE_FILTER_IGNORE
_phone = MobilePort.active()
if _phone:
get_viewport().size_changed.connect(fit)
fit()
## A phone: inside the safe area, the buttons at least the minimum touch target tall. (Nothing on
## desktop.)
func fit() -> void:
if not _phone or not is_inside_tree():
return
UiTheme.inset_to_safe_area(self)
for b: Button in [yes_button, no_button]:
b.custom_minimum_size.y = UiTheme.touch_height(self, BUTTON_HEIGHT)
queue_redraw() # the dim covers the screen from wherever the inset put this
func ask(text: String, confirm_text: String, on_yes: Callable) -> void:
message.text = text
yes_button.text = confirm_text
_on_yes = on_yes
is_open = true
visible = true
mouse_filter = Control.MOUSE_FILTER_STOP
fit()
no_button.grab_focus.call_deferred()
func close() -> void:
if not is_open:
return
is_open = false
visible = false
mouse_filter = Control.MOUSE_FILTER_IGNORE
func _draw() -> void:
draw_rect(UiTheme.overlay_rect(self), Color(0.0, 0.0, 0.0, 0.45)) # dims what's behind
func _has_point(point: Vector2) -> bool:
return UiTheme.overlay_rect(self).has_point(point) # all of the screen, also outside the safe area
func _yes() -> void:
var then := _on_yes
close()
if then.is_valid():
then.call()
func _unhandled_input(event: InputEvent) -> void:
if is_open and event.is_action_pressed("ui_cancel"):
close()
get_viewport().set_input_as_handled()