- Android back: application/config/quit_on_go_back=false; Main.go_back() (from NOTIFICATION_WM_GO_BACK_REQUEST, phones only) closes the shop or pouch panel or leaves build mode, else pauses; paused it closes the question, then settings, then resumes (shared with the pause action); at the main menu it closes the question or settings, else quits. - Phones hide the main menu's Quit and the pause menu's Quit the game. - ScreenLabels.ensure() makes the one ScreenLabels (ToonLook's on desktop; Main's on phones, which watch()es the tree for Label3Ds). Label sizes grow with the window's content_scale_factor (1 on desktop). - FollowCamera starts at PHONE_DISTANCE (12 m) on phones. - MobilePort.forced for tests; game/tests/mobile/platform_test.gd. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
145 lines
6 KiB
GDScript
145 lines
6 KiB
GDScript
class_name ScreenLabels
|
|
extends Control
|
|
## Draws the farm's Label3Ds as flat text over the 3D view, so they stay sharp and readable when
|
|
## the 3D view is pixelated (GameSettings.pixel_size) and at a phone's interface scale. There's one
|
|
## per game (ensure()): ToonLook's on desktop, which tracks every Label3D, and on phones (no
|
|
## ToonLook, MobilePort) Main's, which watch()es the tree for them itself. A tracked
|
|
## label stops rendering in 3D (layers 0) and is drawn here instead, centered on where it projects,
|
|
## with its text, colors, and outline (in the HUD's handwritten font, Sketch, unless it has its own
|
|
## font), at the size it would have on screen (times the interface scale, the window's
|
|
## content_scale_factor: phones', MobilePort; 1 on desktop) but never smaller than
|
|
## MIN_SIZE (far-off ones, under HIDE_BELOW, aren't drawn at all). Labels that would overlap are
|
|
## nudged apart: the lowest on screen keeps its place and the ones over it move up clear of it.
|
|
## The game's labels are all
|
|
## billboards drawn over the scene (no_depth_test), so nothing is lost by drawing them flat, and
|
|
## game code keeps using the Label3Ds as before (text, modulate, visible, position). Sits on a
|
|
## CanvasLayer under the HUD and the menus.
|
|
|
|
const MIN_SIZE := 16.0 # canvas pixels (1080 high): smaller labels are drawn at this size...
|
|
const HIDE_BELOW := 6.0 # ...unless they'd be this small on screen, when they're not drawn
|
|
const GAP := 2.0 # canvas pixels between labels nudged apart
|
|
const GROUP := &"screen_labels"
|
|
|
|
var _labels: Array = [] # Label3D; untyped, so freed ones can be dropped
|
|
|
|
|
|
## The game's ScreenLabels: the one in the tree already, else a new one under host (which must
|
|
## be in the tree) on its own CanvasLayer, "Labels", over the 3D view and under the HUD (1) and
|
|
## the menus (10). ToonLook and Main (on phones) both get theirs here, so there's never two.
|
|
static func ensure(host: Node) -> ScreenLabels:
|
|
for n in host.get_tree().get_nodes_in_group(GROUP):
|
|
if not n.is_queued_for_deletion():
|
|
return n as ScreenLabels
|
|
var layer := CanvasLayer.new()
|
|
layer.name = "Labels"
|
|
layer.layer = -1
|
|
var labels := ScreenLabels.new()
|
|
labels.name = "ScreenLabels"
|
|
layer.add_child(labels)
|
|
host.add_child(layer)
|
|
return labels
|
|
|
|
|
|
func _ready() -> void:
|
|
add_to_group(GROUP)
|
|
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
|
mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
|
|
|
|
func track(label: Label3D) -> void:
|
|
if _labels.has(label):
|
|
return
|
|
label.layers = 0 # drawn here instead
|
|
_labels.append(label)
|
|
|
|
|
|
## Without ToonLook to hand them over (phones): tracks every Label3D under root, and from now on
|
|
## each one as it enters the tree.
|
|
func watch(root: Node) -> void:
|
|
for l in root.find_children("*", "Label3D", true, false):
|
|
track(l as Label3D)
|
|
if not get_tree().node_added.is_connected(_on_node_added):
|
|
get_tree().node_added.connect(_on_node_added)
|
|
|
|
|
|
func _on_node_added(node: Node) -> void:
|
|
if node is Label3D:
|
|
track(node as Label3D)
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
queue_redraw()
|
|
|
|
|
|
## What to draw this frame, one entry per shown label in front of the camera: label, text, font,
|
|
## font_size, outline_size, color, outline_color, and rect (canvas: the text and its outline,
|
|
## after nudging overlapping labels apart).
|
|
func entries() -> Array[Dictionary]:
|
|
var out: Array[Dictionary] = []
|
|
_labels = _labels.filter(func(l: Variant) -> bool: return is_instance_valid(l))
|
|
var cam := get_viewport().get_camera_3d()
|
|
if cam == null:
|
|
return out
|
|
var up := cam.global_basis.y
|
|
var ui := get_window().content_scale_factor # the interface scale (phones; 1 on desktop): labels grow with the HUD
|
|
var fallback := Sketch.font()
|
|
for l: Label3D in _labels:
|
|
if l.text == "" or not l.is_visible_in_tree():
|
|
continue
|
|
var world := l.get_global_transform_interpolated().origin
|
|
if cam.is_position_behind(world):
|
|
continue
|
|
var at := cam.unproject_position(world) # already in canvas units (the stretched 1080-high view)
|
|
var tall := at.distance_to(cam.unproject_position(world + up * l.font_size * l.pixel_size)) * ui
|
|
if tall < HIDE_BELOW:
|
|
continue
|
|
var scale := maxf(tall, MIN_SIZE) / l.font_size
|
|
var outline := l.outline_modulate
|
|
outline.a *= l.modulate.a
|
|
var font: Font = l.font if l.font != null else fallback
|
|
var size := roundi(l.font_size * scale)
|
|
var outline_size := roundi(l.outline_size * scale)
|
|
var box := font.get_multiline_string_size(l.text, HORIZONTAL_ALIGNMENT_CENTER, -1, size) + Vector2.ONE * outline_size
|
|
var center := at + Vector2(l.offset.x, -l.offset.y) * scale
|
|
out.append({"label": l, "text": l.text, "font": font, "font_size": size, "outline_size": outline_size,
|
|
"color": l.modulate, "outline_color": outline, "rect": Rect2(center - box * 0.5, box)})
|
|
_separate(out)
|
|
return out
|
|
|
|
|
|
## Nudges overlapping labels apart: from the lowest on screen up, each label that overlaps one
|
|
## already placed moves up until it clears it.
|
|
static func _separate(list: Array[Dictionary]) -> void:
|
|
list.sort_custom(func(a: Dictionary, b: Dictionary) -> bool:
|
|
var ya: float = (a["rect"] as Rect2).get_center().y
|
|
var yb: float = (b["rect"] as Rect2).get_center().y
|
|
if ya != yb:
|
|
return ya > yb
|
|
return (a["label"] as Object).get_instance_id() < (b["label"] as Object).get_instance_id())
|
|
var placed: Array[Rect2] = []
|
|
for e in list:
|
|
var r: Rect2 = e["rect"]
|
|
var moved := true
|
|
while moved:
|
|
moved = false
|
|
for p in placed:
|
|
if r.intersects(p):
|
|
r.position.y = p.position.y - r.size.y - GAP
|
|
moved = true
|
|
placed.append(r)
|
|
e["rect"] = r
|
|
|
|
|
|
func _draw() -> void:
|
|
for e in entries():
|
|
var font: Font = e["font"]
|
|
var text: String = e["text"]
|
|
var size: int = e["font_size"]
|
|
var outline_size: int = e["outline_size"]
|
|
var rect: Rect2 = e["rect"]
|
|
var pos := rect.position + Vector2(outline_size * 0.5, outline_size * 0.5 + font.get_ascent(size))
|
|
var width := rect.size.x - outline_size
|
|
if outline_size > 0:
|
|
draw_multiline_string_outline(font, pos, text, HORIZONTAL_ALIGNMENT_CENTER, width, size, -1,
|
|
outline_size, e["outline_color"])
|
|
draw_multiline_string(font, pos, text, HORIZONTAL_ALIGNMENT_CENTER, width, size, -1, e["color"])
|