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>
183 lines
7.5 KiB
GDScript
183 lines
7.5 KiB
GDScript
class_name MainMenu
|
|
extends Control
|
|
## The title screen over the farm (Main slowly circles a camera around it): the title, then
|
|
## Continue (with which morning it resumes at, or why the save can't be continued), New
|
|
## vacation, Play with a friend (co-op, CoopMenu; not on phones), Settings, and Quit (not on
|
|
## phones either), and a notice line (e.g.
|
|
## why a co-op game ended). Mouse, keyboard, and gamepad work through focus.
|
|
## On a phone the column and the credits sit inside the safe area (clear of the notch and the home
|
|
## indicator), the buttons at least the minimum touch target tall, with smaller type and gaps
|
|
## when the canvas is too short for the desktop's (fit).
|
|
|
|
signal continue_requested
|
|
signal new_requested
|
|
signal settings_requested
|
|
signal coop_requested
|
|
signal quit_requested
|
|
|
|
const BUTTON_SIZE := Vector2(380.0, 62.0)
|
|
## Phones: the layouts to try, roomiest first (the first is the desktop's): the title's,
|
|
## subtitle's, and credits' font sizes, the gap above the buttons, the column's gaps, and its
|
|
## distance from the safe area's top-left corner (the credits line up with it, BOTTOM_PAD above
|
|
## the safe area's bottom).
|
|
const LAYOUTS: Array[Dictionary] = [
|
|
{"title": 92, "sub": 28, "credits": 16, "gap": 50.0, "separation": 16, "pad": Vector2(110.0, 150.0)},
|
|
{"title": 72, "sub": 26, "credits": 16, "gap": 12.0, "separation": 10, "pad": Vector2(56.0, 28.0)},
|
|
{"title": 60, "sub": 22, "credits": 14, "gap": 0.0, "separation": 6, "pad": Vector2(40.0, 10.0)},
|
|
]
|
|
const BOTTOM_PAD := 14.0
|
|
|
|
var continue_button: Button
|
|
var new_button: Button
|
|
var settings_button: Button
|
|
var coop_button: Button
|
|
var notice: Label
|
|
var quit_button: Button
|
|
var save_note: Label
|
|
|
|
var _phone := false
|
|
var _shade: ColorRect
|
|
var _box: VBoxContainer
|
|
var _title: Label
|
|
var _sub: Label
|
|
var _gap: Control
|
|
var _credits: Label
|
|
|
|
|
|
func _ready() -> void:
|
|
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
|
var shade := ColorRect.new() # darkens the left side behind the buttons
|
|
shade.color = Color(0.03, 0.04, 0.03, 0.62)
|
|
shade.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
add_child(shade)
|
|
shade.set_anchors_and_offsets_preset(Control.PRESET_LEFT_WIDE)
|
|
shade.offset_right = 700.0
|
|
_shade = shade
|
|
var box := VBoxContainer.new()
|
|
_box = box
|
|
box.position = Vector2(110.0, 150.0)
|
|
box.add_theme_constant_override("separation", 16)
|
|
add_child(box)
|
|
var title := Label.new()
|
|
title.text = "Harvest Season"
|
|
title.add_theme_font_size_override("font_size", 92)
|
|
title.add_theme_color_override("font_color", UiTheme.GOLD)
|
|
title.add_theme_color_override("font_outline_color", Color(0.15, 0.1, 0.05))
|
|
title.add_theme_constant_override("outline_size", 14)
|
|
box.add_child(title)
|
|
_title = title
|
|
var sub := Label.new()
|
|
sub.text = "The farmers are away. You're the scarecrow."
|
|
sub.add_theme_font_size_override("font_size", 28)
|
|
sub.add_theme_color_override("font_color", UiTheme.TEXT)
|
|
box.add_child(sub)
|
|
_sub = sub
|
|
var gap := Control.new()
|
|
gap.custom_minimum_size = Vector2(0.0, 50.0)
|
|
box.add_child(gap)
|
|
_gap = gap
|
|
continue_button = _button(box, "Continue")
|
|
continue_button.pressed.connect(func() -> void: continue_requested.emit())
|
|
save_note = Label.new()
|
|
save_note.add_theme_font_size_override("font_size", 19)
|
|
save_note.add_theme_color_override("font_color", Color(0.75, 0.77, 0.7))
|
|
box.add_child(save_note)
|
|
new_button = _button(box, "New vacation")
|
|
new_button.pressed.connect(func() -> void: new_requested.emit())
|
|
coop_button = _button(box, "Play with a friend")
|
|
coop_button.pressed.connect(func() -> void: coop_requested.emit())
|
|
coop_button.visible = not MobilePort.active() # no co-op on phones
|
|
settings_button = _button(box, "Settings")
|
|
settings_button.pressed.connect(func() -> void: settings_requested.emit())
|
|
quit_button = _button(box, "Quit")
|
|
quit_button.pressed.connect(func() -> void: quit_requested.emit())
|
|
quit_button.visible = not MobilePort.active() # phones: the system closes apps (Android's back here, Main)
|
|
notice = Label.new()
|
|
notice.add_theme_font_size_override("font_size", 22)
|
|
notice.add_theme_color_override("font_color", Color(1.0, 0.75, 0.5))
|
|
box.add_child(notice)
|
|
var credits := Label.new()
|
|
credits.text = "Art: KayKit and ithappy · Sounds: Kenney (CC0) · Music: \"On the Farm\"; DavidKBD, Purgatory · Co-op: GD-Sync · Made with Godot"
|
|
credits.add_theme_font_size_override("font_size", 16)
|
|
credits.add_theme_color_override("font_color", Color(0.6, 0.62, 0.56))
|
|
add_child(credits)
|
|
credits.set_anchors_and_offsets_preset(Control.PRESET_BOTTOM_LEFT)
|
|
credits.offset_top = -60.0
|
|
credits.offset_left = 110.0
|
|
_credits = credits
|
|
visible = false
|
|
_phone = MobilePort.active()
|
|
if _phone:
|
|
get_viewport().size_changed.connect(fit)
|
|
fit()
|
|
|
|
|
|
## A phone: the column and the credits (wrapped to the column's width, so they stay on the shade)
|
|
## inside the safe area, the buttons at least the minimum touch target tall, in the roomiest of
|
|
## LAYOUTS that fits. (Nothing on desktop.)
|
|
func fit() -> void:
|
|
if not _phone or not is_inside_tree():
|
|
return
|
|
var safe := MobilePort.safe_rect(self)
|
|
var view := get_viewport_rect().size
|
|
for b: Button in [continue_button, new_button, coop_button, settings_button, quit_button]:
|
|
b.custom_minimum_size.y = UiTheme.touch_height(self, BUTTON_SIZE.y)
|
|
for layout in LAYOUTS:
|
|
var pad: Vector2 = layout["pad"]
|
|
_title.add_theme_font_size_override("font_size", layout["title"])
|
|
_sub.add_theme_font_size_override("font_size", layout["sub"])
|
|
_gap.custom_minimum_size.y = layout["gap"]
|
|
_box.add_theme_constant_override("separation", layout["separation"])
|
|
var column := _box.get_combined_minimum_size()
|
|
_credits.add_theme_font_size_override("font_size", layout["credits"])
|
|
_credits.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART # under the column, on its shade
|
|
_credits.custom_minimum_size.x = column.x
|
|
_credits.size = Vector2(column.x, 0.0)
|
|
var credits_h := _credits.get_combined_minimum_size().y
|
|
_box.position = safe.position + pad
|
|
_box.size = column
|
|
_credits.offset_left = safe.position.x + pad.x
|
|
_credits.offset_right = _credits.offset_left + _credits.custom_minimum_size.x
|
|
_credits.offset_bottom = safe.end.y - view.y - BOTTOM_PAD
|
|
_credits.offset_top = _credits.offset_bottom - credits_h
|
|
_shade.offset_right = _box.position.x + column.x + pad.x
|
|
if pad.y + column.y + BOTTOM_PAD + credits_h + BOTTOM_PAD <= safe.size.y and pad.x + column.x <= safe.size.x:
|
|
break
|
|
|
|
|
|
## Shows the menu for the save as read (SaveGame.read): Continue says where it resumes, or is
|
|
## off with the reason.
|
|
func open(saved: SaveGame.Loaded, nights: int) -> void:
|
|
visible = true
|
|
var ok := saved != null and saved.status == SaveGame.Status.OK
|
|
continue_button.disabled = not ok
|
|
continue_button.focus_mode = Control.FOCUS_ALL if ok else Control.FOCUS_NONE
|
|
if ok:
|
|
save_note.text = "Morning of day %d of %d%s" % [saved.state.night, nights,
|
|
(" · saved " + saved.saved_at.replace("T", " ").left(16)) if saved.saved_at != "" else ""]
|
|
else:
|
|
save_note.text = saved.message if saved != null else "No saved vacation"
|
|
fit()
|
|
_focus_first.call_deferred()
|
|
|
|
|
|
func close() -> void:
|
|
visible = false
|
|
|
|
|
|
## Continue when it can be used, else New vacation (checked when it runs: open() may have been
|
|
## called again since).
|
|
func _focus_first() -> void:
|
|
if visible:
|
|
(new_button if continue_button.disabled else continue_button).grab_focus()
|
|
|
|
|
|
func _button(parent: Control, text: String) -> Button:
|
|
var b := Button.new()
|
|
b.text = text
|
|
b.alignment = HORIZONTAL_ALIGNMENT_LEFT
|
|
b.custom_minimum_size = BUTTON_SIZE
|
|
b.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN
|
|
b.add_theme_font_size_override("font_size", 28)
|
|
parent.add_child(b)
|
|
return b
|