108 lines
4.5 KiB
GDScript
108 lines
4.5 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). ToonLook tracks every Label3D; 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 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
|
|
|
|
var _labels: Array = [] # Label3D; untyped, so freed ones can be dropped
|
|
|
|
|
|
func _ready() -> void:
|
|
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)
|
|
|
|
|
|
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 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))
|
|
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"])
|