Randoloot/scripts/ui/ship_selection.gd
2026-10-27 22:20:01 +01:00

107 lines
4.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.

extends Control
signal ship_selected
signal close_requested
const CATALOG := preload("res://scripts/game/ship_catalog.gd")
@onready var ship_grid: GridContainer = %ShipGrid
@onready var close_button: Button = %CloseButton
var button_group := ButtonGroup.new()
func _ready() -> void:
button_group.allow_unpress = false
preload("res://scripts/ui/menu_modal.gd").style_button(close_button)
$Layout/Heading.add_theme_font_override("font",preload("res://assets/fonts/font2.ttf"))
$Layout/Instructions.add_theme_font_override("font",ThemeDB.fallback_font)
$Layout/Instructions.add_theme_font_size_override("font_size",14)
close_button.pressed.connect(func() -> void: close_requested.emit())
_load_ships()
func _frame(accent: Color, highlighted: bool) -> StyleBoxFlat:
var style := StyleBoxFlat.new()
style.bg_color = Color("142b42") if highlighted else Color("0b192a")
style.bg_color.a = 0.96
style.border_color = accent if highlighted else Color("29435a")
style.set_border_width_all(2 if highlighted else 1)
style.set_corner_radius_all(10)
style.shadow_color = Color(0, 0, 0, 0.3)
style.shadow_size = 5
return style
func _label(text: String, font_size: int, color: Color) -> Label:
var label := Label.new()
label.text = text
label.mouse_filter = Control.MOUSE_FILTER_IGNORE
label.add_theme_font_override("font",ThemeDB.fallback_font)
label.add_theme_font_size_override("font_size", clampi(font_size, 10, 20))
label.add_theme_color_override("font_color", color)
return label
func _load_ships() -> void:
var selected_ship := CATALOG.for_path(GameState.selected_ship_path)
for chassis in CATALOG.SHIPS:
var texture := load(CATALOG.texture_path(chassis)) as Texture2D
if texture == null:
continue
var accent: Color = chassis["accent"]
var button := Button.new()
button.name = "Ship%d" % int(chassis["number"])
button.custom_minimum_size = Vector2(240, 238)
button.size_flags_horizontal = Control.SIZE_EXPAND_FILL
button.toggle_mode = true
button.button_group = button_group
button.add_theme_stylebox_override("normal", _frame(accent, false))
for state in ["hover", "pressed", "hover_pressed", "focus"]:
button.add_theme_stylebox_override(state, _frame(accent, true))
var contents := VBoxContainer.new()
contents.mouse_filter = Control.MOUSE_FILTER_IGNORE
button.add_child(contents)
contents.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
contents.offset_left = 16
contents.offset_right = -16
contents.offset_top = 12
contents.offset_bottom = -12
contents.add_theme_constant_override("separation", 5)
contents.add_child(_label(chassis["name"].to_upper(), 20, Color("edf6ff")))
contents.add_child(_label(chassis["role"], 11, accent))
var preview := TextureRect.new()
preview.custom_minimum_size.y = 110
preview.size_flags_vertical = Control.SIZE_EXPAND_FILL
preview.texture = texture
preview.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
preview.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
preview.mouse_filter = Control.MOUSE_FILTER_IGNORE
preview.texture_filter = CanvasItem.TEXTURE_FILTER_LINEAR
contents.add_child(preview)
var stats := GridContainer.new()
stats.columns = 2
stats.mouse_filter = Control.MOUSE_FILTER_IGNORE
stats.add_theme_constant_override("h_separation", 14)
stats.add_theme_constant_override("v_separation", 4)
for stat in [["Kadłub", "hull", ""], ["Tarcze", "shields", ""], ["Energia", "energy", ""], ["Prędkość", "speed", " px/s"]]:
var value := _label("%s %d%s" % [stat[0], chassis[stat[1]], stat[2]], 13, Color("bfd4e5"))
value.size_flags_horizontal = Control.SIZE_EXPAND_FILL
stats.add_child(value)
contents.add_child(stats)
var launch := _label("WYBIERZ I WYSTARTUJ ›", 11, accent)
launch.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
contents.add_child(launch)
button.tooltip_text = "%s\nStatystyki bazowe przed bonusami modułów.\n%s" % [chassis["name"], CATALOG.stats_text(chassis)]
button.pressed.connect(_choose_ship.bind(chassis, button))
ship_grid.add_child(button)
if chassis["number"] == selected_ship["number"]:
_select_ship(chassis, button)
func _select_ship(chassis: Dictionary, button: Button) -> void:
GameState.selected_ship_path = CATALOG.texture_path(chassis)
button.button_pressed = true
func _choose_ship(chassis: Dictionary, button: Button) -> void:
_select_ship(chassis, button)
ship_selected.emit()
func _unhandled_input(event: InputEvent) -> void:
if visible and event is InputEventKey and event.pressed and event.keycode == KEY_ESCAPE:
close_requested.emit()
get_viewport().set_input_as_handled()