68 lines
3.3 KiB
GDScript
68 lines
3.3 KiB
GDScript
extends Button
|
|
|
|
var player: CharacterBody2D
|
|
var _texture: Texture2D
|
|
var _frame_texture: Texture2D
|
|
var _frame_region := Rect2()
|
|
var _display := Vector3.ONE
|
|
const TEXT := Color("dce8e7")
|
|
const MUTED := Color("88a7ae")
|
|
|
|
func _ready() -> void:
|
|
focus_mode = Control.FOCUS_NONE
|
|
text = ""
|
|
for state in ["normal", "hover", "pressed", "focus"]:
|
|
add_theme_stylebox_override(state, StyleBoxEmpty.new())
|
|
var ship := ShipCatalog.for_path(GameState.selected_ship_path)
|
|
_texture = load(ShipCatalog.texture_path(ship)) as Texture2D
|
|
_display = _ratios()
|
|
_frame_texture = load("res://assets/ui/frames/ship_ring_bars.png") as Texture2D
|
|
_frame_region = Rect2(Vector2.ZERO, _frame_texture.get_size())
|
|
|
|
func _ratios() -> Vector3:
|
|
if not is_instance_valid(player): return Vector3.ZERO
|
|
return Vector3(player.hull / maxf(1, player.max_hull), player.shields / maxf(1, player.max_shields), player.energy / maxf(1, player.max_energy)).clamp(Vector3.ZERO, Vector3.ONE)
|
|
|
|
func _process(delta: float) -> void:
|
|
_display = _display.lerp(_ratios(), 1.0 - exp(-delta * 12.0))
|
|
queue_redraw()
|
|
|
|
func _draw() -> void:
|
|
if not is_instance_valid(player): return
|
|
var portrait := Vector2(185, 35.5)
|
|
draw_circle(portrait, 26, Color("0a141b66"))
|
|
if _texture != null:
|
|
var dimensions := _texture.get_size()
|
|
var extent := dimensions * (44.0 / maxf(dimensions.x, dimensions.y))
|
|
draw_texture_rect(_texture, Rect2(portrait - extent * 0.5, extent), false)
|
|
_bar(Rect2(12, 15, 126, 7), player.hull, player.max_hull, _display.x, Color("e88972"), "KADŁUB")
|
|
_bar(Rect2(12, 32, 126, 7), player.shields, player.max_shields, _display.y, Color("79bde5"), "TARCZE")
|
|
_bar(Rect2(12, 49, 126, 7), player.energy, player.max_energy, _display.z, Color("87cfb1"), "ENERGIA")
|
|
if _frame_texture != null:
|
|
draw_texture_rect_region(_frame_texture, Rect2(Vector2.ZERO, size), _frame_region, Color(1, 1, 1, 1.0 if is_hovered() else 0.88))
|
|
_bar_caption(Vector2(13, 21), player.hull, player.max_hull, "KADŁUB")
|
|
_bar_caption(Vector2(13, 38), player.shields, player.max_shields, "TARCZE")
|
|
_bar_caption(Vector2(13, 55), player.energy, player.max_energy, "ENERGIA")
|
|
|
|
func _bar_caption(point: Vector2, value: float, maximum: float, caption: String) -> void:
|
|
var font := ThemeDB.fallback_font
|
|
var values := "%.0f / %.0f" % [value, maximum]
|
|
draw_string_outline(font, point, caption, HORIZONTAL_ALIGNMENT_LEFT, 58, 7, 2, Color("071117"))
|
|
draw_string_outline(font, point, values, HORIZONTAL_ALIGNMENT_RIGHT, 123, 7, 2, Color("071117"))
|
|
draw_string(font, point, caption, HORIZONTAL_ALIGNMENT_LEFT, 58, 7, TEXT)
|
|
draw_string(font, point, values, HORIZONTAL_ALIGNMENT_RIGHT, 123, 7, TEXT)
|
|
|
|
func _bar(rect: Rect2, value: float, maximum: float, ratio: float, color: Color, _caption: String) -> void:
|
|
var track := StyleBoxFlat.new()
|
|
track.bg_color = Color("071117")
|
|
track.set_corner_radius_all(2)
|
|
draw_style_box(track, rect)
|
|
var fill := track.duplicate() as StyleBoxFlat
|
|
fill.bg_color = color
|
|
if ratio > 0.001:
|
|
draw_style_box(fill, Rect2(rect.position, Vector2(rect.size.x * ratio, rect.size.y)))
|
|
draw_line(rect.position + Vector2(1, 1), rect.position + Vector2(maxf(1, rect.size.x * ratio - 1), 1), Color(color.lightened(0.3), 0.6), 1)
|
|
for i in range(1, 10):
|
|
var x := rect.position.x + rect.size.x * float(i) / 10.0
|
|
draw_line(Vector2(x, rect.position.y), Vector2(x, rect.end.y), Color("101d2466"), 1)
|