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>
178 lines
8.9 KiB
GDScript
178 lines
8.9 KiB
GDScript
class_name GameSettings
|
|
extends RefCounted
|
|
## The player's settings, kept in user://settings.cfg (ConfigFile) apart from the save game, and
|
|
## applied at start and on every change (SettingsMenu):
|
|
## * Audio: the volume of each audio bus (game/resources/audio_buses.tres).
|
|
## * Controls: the dead zones of the move stick (the move_* actions) and the aim stick
|
|
## (GameConfig.aim_dead_zone). Mouse aim follows the cursor directly, so it has no
|
|
## sensitivity to set.
|
|
## * Display: fullscreen, VSync, the pixel size (the 3D view renders that many times smaller
|
|
## and scales back up with sharp pixels, for ToonLook's pixel-art look; 1 = off), and the 3D
|
|
## resolution scale, used when the pixel size is 1 (the HUD stays sharp either way).
|
|
## * Gameplay: whether the tips show on the first day and night (Tutorial).
|
|
## * Co-op: your player name, and whether your microphone is sent in voice chat.
|
|
## * Touch (phones): the interface size, times the scale the screen's size calls for
|
|
## (MobilePort.ui_scale; it doesn't change desktop), the sticks swapped, the camera's
|
|
## distance, and a buzz when the scarecrow is hit. Saved on every platform; only phones
|
|
## apply them (and show them: SettingsMenu).
|
|
## Values missing from the file or out of range fall back to the defaults, so an old or
|
|
## hand-edited file never breaks the game.
|
|
|
|
const PATH := "user://settings.cfg"
|
|
const BUSES: Array[String] = ["Master", "Music", "SFX", "Ambience", "Voice"]
|
|
const BUS_NAMES := {"Master": "Master volume", "Music": "Music and stingers", "SFX": "Sound effects", "Ambience": "Ambience",
|
|
"Voice": "Voice chat (your friend)"}
|
|
const DEFAULT_VOLUME := {"Master": 0.8, "Music": 0.7, "SFX": 0.8, "Ambience": 0.6, "Voice": 1.0}
|
|
const MAX_PIXEL_SIZE := 8
|
|
## Phones: the Interface size's range (MobilePort.ui_scale clamps to the same) and the camera's.
|
|
const INTERFACE_SIZE_MIN := 0.8
|
|
const INTERFACE_SIZE_MAX := 1.3
|
|
const CAMERA_DISTANCE_MIN := 8.0
|
|
const CAMERA_DISTANCE_MAX := 20.0
|
|
|
|
var volume := DEFAULT_VOLUME.duplicate()
|
|
var move_dead_zone := 0.2
|
|
var aim_dead_zone := 0.3
|
|
var fullscreen := false
|
|
var vsync := true
|
|
## Phones (MobilePort) default to no pixel look, at three quarters of the screen's resolution.
|
|
var pixel_size := 1 if MobilePort.active() else 4
|
|
var resolution_scale := 0.75 if MobilePort.active() else 1.0
|
|
var show_tips := true
|
|
## Co-op: your name for the lobby and the name tag, and whether your microphone is sent.
|
|
var player_name := ""
|
|
var microphone := true
|
|
## Phones: the Interface size, 0.8-1.3 times the scale the screen calls for (MobilePort.ui_scale).
|
|
var interface_size := 1.0
|
|
## Phones: the sticks swapped (TouchSticks, TouchButtons: the move stick on the other half), the
|
|
## follow camera's distance (8-20 m, in place of FollowCamera.PHONE_DISTANCE), and a short buzz
|
|
## when the scarecrow is hit (Scarecrow.haptics).
|
|
var swap_sticks := false
|
|
var camera_distance := FollowCamera.PHONE_DISTANCE
|
|
var haptics := true
|
|
|
|
|
|
static func load_from(path: String = PATH) -> GameSettings:
|
|
var g := GameSettings.new()
|
|
var f := ConfigFile.new()
|
|
if f.load(path) != OK:
|
|
return g
|
|
for bus in BUSES:
|
|
g.volume[bus] = _number(f.get_value("audio", bus.to_lower(), g.volume[bus]), 0.0, 1.0, g.volume[bus])
|
|
g.move_dead_zone = _number(f.get_value("controls", "move_dead_zone", g.move_dead_zone), 0.05, 0.9, g.move_dead_zone)
|
|
g.aim_dead_zone = _number(f.get_value("controls", "aim_dead_zone", g.aim_dead_zone), 0.05, 0.9, g.aim_dead_zone)
|
|
g.fullscreen = _flag(f.get_value("display", "fullscreen", g.fullscreen), g.fullscreen)
|
|
g.vsync = _flag(f.get_value("display", "vsync", g.vsync), g.vsync)
|
|
g.pixel_size = roundi(_number(f.get_value("display", "pixel_size", g.pixel_size), 1.0, MAX_PIXEL_SIZE, g.pixel_size))
|
|
g.resolution_scale = _number(f.get_value("display", "resolution_scale", g.resolution_scale), 0.5, 1.0, g.resolution_scale)
|
|
g.show_tips = _flag(f.get_value("gameplay", "show_tips", g.show_tips), g.show_tips)
|
|
var n: Variant = f.get_value("coop", "player_name", "")
|
|
g.player_name = String(n).strip_edges().left(20) if n is String else ""
|
|
g.microphone = _flag(f.get_value("coop", "microphone", g.microphone), g.microphone)
|
|
g.interface_size = _number(f.get_value("touch", "interface_size", g.interface_size), INTERFACE_SIZE_MIN,
|
|
INTERFACE_SIZE_MAX, g.interface_size)
|
|
g.swap_sticks = _flag(f.get_value("touch", "swap_sticks", g.swap_sticks), g.swap_sticks)
|
|
g.camera_distance = _number(f.get_value("touch", "camera_distance", g.camera_distance), CAMERA_DISTANCE_MIN,
|
|
CAMERA_DISTANCE_MAX, g.camera_distance)
|
|
g.haptics = _flag(f.get_value("touch", "haptics", g.haptics), g.haptics)
|
|
return g
|
|
|
|
|
|
func save_to(path: String = PATH) -> Error:
|
|
var f := ConfigFile.new()
|
|
for bus in BUSES:
|
|
f.set_value("audio", bus.to_lower(), volume.get(bus, DEFAULT_VOLUME[bus]))
|
|
f.set_value("controls", "move_dead_zone", move_dead_zone)
|
|
f.set_value("controls", "aim_dead_zone", aim_dead_zone)
|
|
f.set_value("display", "fullscreen", fullscreen)
|
|
f.set_value("display", "vsync", vsync)
|
|
f.set_value("display", "pixel_size", pixel_size)
|
|
f.set_value("display", "resolution_scale", resolution_scale)
|
|
f.set_value("gameplay", "show_tips", show_tips)
|
|
f.set_value("coop", "player_name", player_name)
|
|
f.set_value("coop", "microphone", microphone)
|
|
f.set_value("touch", "interface_size", interface_size)
|
|
f.set_value("touch", "swap_sticks", swap_sticks)
|
|
f.set_value("touch", "camera_distance", camera_distance)
|
|
f.set_value("touch", "haptics", haptics)
|
|
return f.save(path)
|
|
|
|
|
|
## Applies everything: bus volumes, dead zones (into config too, when given), the interface
|
|
## scale, the touch settings (apply_touch), and the display (skipped without a real display, e.g.
|
|
## headless).
|
|
func apply(viewport: Viewport, config: GameConfig = null) -> void:
|
|
for bus in BUSES:
|
|
var i := AudioServer.get_bus_index(bus)
|
|
if i < 0:
|
|
continue
|
|
var v: float = volume.get(bus, DEFAULT_VOLUME[bus])
|
|
AudioServer.set_bus_volume_db(i, linear_to_db(maxf(v, 0.0001)))
|
|
AudioServer.set_bus_mute(i, v <= 0.001)
|
|
for action in ["move_left", "move_right", "move_forward", "move_back"]:
|
|
if InputMap.has_action(action):
|
|
InputMap.action_set_deadzone(action, move_dead_zone)
|
|
if config != null:
|
|
config.aim_dead_zone = aim_dead_zone
|
|
if viewport != null:
|
|
apply_pixels(viewport)
|
|
apply_scale(viewport)
|
|
apply_touch(viewport.get_tree() if viewport != null else Engine.get_main_loop() as SceneTree)
|
|
if DisplayServer.get_name() == "headless":
|
|
return
|
|
var mode := DisplayServer.window_get_mode()
|
|
var want_full := fullscreen
|
|
var is_full := mode == DisplayServer.WINDOW_MODE_FULLSCREEN or mode == DisplayServer.WINDOW_MODE_EXCLUSIVE_FULLSCREEN
|
|
if want_full != is_full:
|
|
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN if want_full else DisplayServer.WINDOW_MODE_WINDOWED)
|
|
DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_ENABLED if vsync else DisplayServer.VSYNC_DISABLED)
|
|
|
|
|
|
## The 3D view's size: pixel_size > 1 renders it that many times smaller with sharp (nearest)
|
|
## upscaling and no MSAA, so each art pixel is a clean block; 1 renders it at resolution_scale
|
|
## with the project's MSAA.
|
|
func apply_pixels(viewport: Viewport) -> void:
|
|
if pixel_size > 1:
|
|
viewport.scaling_3d_mode = Viewport.SCALING_3D_MODE_NEAREST
|
|
viewport.scaling_3d_scale = 1.0 / pixel_size
|
|
viewport.msaa_3d = Viewport.MSAA_DISABLED
|
|
else:
|
|
viewport.scaling_3d_mode = Viewport.SCALING_3D_MODE_BILINEAR
|
|
viewport.scaling_3d_scale = resolution_scale
|
|
viewport.msaa_3d = ProjectSettings.get_setting("rendering/anti_aliasing/quality/msaa_3d", Viewport.MSAA_DISABLED)
|
|
|
|
|
|
## The interface scale (MobilePort.ui_scale: on a phone, the screen's size times
|
|
## interface_size; always 1 on desktop) as the window's content_scale_factor, which scales every
|
|
## CanvasLayer (the HUD, the menus, the sticks) and leaves the 3D view alone.
|
|
func apply_scale(viewport: Viewport) -> void:
|
|
var window := viewport.get_window()
|
|
if window != null:
|
|
window.content_scale_factor = MobilePort.ui_scale(interface_size)
|
|
|
|
|
|
## The touch settings, live: every node in group &"touch_controls" (TouchSticks, TouchButtons)
|
|
## gets apply_settings(self) (the sticks' sides follow swap_sticks), and on a phone the follow
|
|
## camera (group FollowCamera.GROUP) moves to camera_distance and a hit buzzes when haptics is on
|
|
## (Scarecrow.haptics). Desktop's camera keeps its own distance (16, and the mouse wheel's zoom).
|
|
## Main applies the settings after the farm is ready, so on a phone this is what sets the
|
|
## camera's distance.
|
|
func apply_touch(tree: SceneTree) -> void:
|
|
Scarecrow.haptics = haptics and MobilePort.active()
|
|
if tree == null:
|
|
return
|
|
tree.call_group(&"touch_controls", &"apply_settings", self)
|
|
if MobilePort.active():
|
|
for cam in tree.get_nodes_in_group(FollowCamera.GROUP):
|
|
(cam as FollowCamera).distance = camera_distance
|
|
|
|
|
|
static func _number(v: Variant, lo: float, hi: float, fallback: float) -> float:
|
|
if not (v is float or v is int):
|
|
return fallback
|
|
var x := float(v)
|
|
return clampf(x, lo, hi) if is_finite(x) else fallback
|
|
|
|
|
|
static func _flag(v: Variant, fallback: bool) -> bool:
|
|
return bool(v) if v is bool else fallback
|