files-game/ui/bandwidth_bar.gd
2026-10-01 23:01:56 -04:00

115 lines
3.5 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name BandwidthBar
extends Control
## A player's Bandwidth as a neon loading bar (ui-plan.md §A5). Bandwidth has no maximum, so the
## scale is fixed at 0–20 Gb with ticks at the Firewall costs, and the bar glows past 20 instead of
## clipping. Behind the fill, a dimmer ghost runs to what you could reach by salvaging your hand
## (Query.reachable_bandwidth): the number that answers "can I afford this?".
const MAX_GB := 20
const CELLS := 20
var color := Style.ACCENT:
set(value):
color = value
queue_redraw()
## Tick marks, in Gb: the Firewall costs.
var ticks: Array = [2, 4, 8]:
set(value):
ticks = value
queue_redraw()
## Show the numbers under the ticks.
var show_scale := false:
set(value):
show_scale = value
custom_minimum_size.y = 22.0 if value else 10.0
queue_redraw()
## The shown fill, eased toward the real value.
var shown := 0.0:
set(value):
shown = value
queue_redraw()
var gb := 0
var reachable := 0
var _tween: Tween
var _pulse := 0.0
func _init() -> void:
custom_minimum_size = Vector2(120, 10)
mouse_filter = Control.MOUSE_FILTER_IGNORE
## Shows `value` Gb with the ghost at `reach`. Animates the change on the motion curve.
func set_bandwidth(value: int, reach: int, animate := true) -> void:
reachable = maxi(reach, value)
if value == gb and is_equal_approx(shown, value):
queue_redraw()
return
gb = value
if _tween != null:
_tween.kill()
if not animate or Motion.instant() or not is_inside_tree():
shown = value
return
_tween = create_tween()
Motion.rush_in(_tween, self, "shown", float(value), 0.55)
func _process(delta: float) -> void:
if gb > MAX_GB:
_pulse = fmod(_pulse + delta, 2.0)
queue_redraw()
func _draw() -> void:
var bar_h := minf(size.y, 10.0)
var w := size.x
var cell := w / CELLS
var gap := maxf(1.0, cell * 0.14)
var dark := Color(color, 0.1)
var ghost := Color(color, 0.28)
for i in CELLS:
var x := i * cell
var r := Rect2(x + gap / 2.0, 0, cell - gap, bar_h)
var c := dark
if i + 1 <= roundi(shown):
# Brighter toward the head of the fill.
c = color.lerp(Color.WHITE, 0.25 * float(i) / CELLS)
elif i + 1 <= reachable:
c = ghost
draw_rect(r, c)
# A partial cell while the fill moves.
var frac := shown - floorf(shown)
if frac > 0.02 and shown < CELLS:
var i := int(floorf(shown))
draw_rect(Rect2(i * cell + gap / 2.0, 0, (cell - gap) * frac, bar_h), color)
# The frame and the Firewall-cost ticks.
draw_rect(Rect2(0, -2, w, bar_h + 4), Color(color, 0.45), false, 1.0)
for t in ticks:
if t > 0 and t < MAX_GB:
var x: float = t * cell
draw_line(Vector2(x, -4), Vector2(x, bar_h + 4), Color(Style.SCREEN, 0.7), 1.0)
if show_scale:
_text(str(t), Vector2(x, bar_h + 14))
if show_scale:
_text("0", Vector2(0, bar_h + 14), HORIZONTAL_ALIGNMENT_LEFT)
_text("20", Vector2(w, bar_h + 14), HORIZONTAL_ALIGNMENT_RIGHT)
# Past the scale: the bar glows instead of clipping.
if gb > MAX_GB:
var k := 0.5 + 0.5 * sin(_pulse * PI)
for g in 3:
var grow := 2.0 + g * 2.0
draw_rect(Rect2(-grow, -2 - grow, w + grow * 2, bar_h + 4 + grow * 2), Color(color, (0.25 - g * 0.07) * (0.6 + 0.4 * k)), false, 2.0)
func _text(t: String, at: Vector2, align := HORIZONTAL_ALIGNMENT_CENTER) -> void:
var f := Style.body_font()
var fs := 10
var tw := f.get_string_size(t, HORIZONTAL_ALIGNMENT_LEFT, -1, fs).x
var x := at.x - tw / 2.0
if align == HORIZONTAL_ALIGNMENT_LEFT:
x = at.x
elif align == HORIZONTAL_ALIGNMENT_RIGHT:
x = at.x - tw
draw_string(f, Vector2(x, at.y), t, HORIZONTAL_ALIGNMENT_LEFT, -1, fs, Style.MUTED)