Randoloot/scripts/ui/ship_selection.gd

71 lines
2.7 KiB
GDScript

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
close_button.pressed.connect(func() -> void: close_requested.emit())
_load_ships()
func _load_ships() -> void:
var selected_ship := CATALOG.for_path(GameState.selected_ship_path)
for chassis in CATALOG.SHIPS:
var ship_path := CATALOG.texture_path(chassis)
var texture := load(ship_path) as Texture2D
if texture == null:
continue
var button := Button.new()
button.name = "Ship%d" % int(chassis["number"])
button.custom_minimum_size = Vector2(250, 240)
button.size_flags_horizontal = Control.SIZE_EXPAND_FILL
button.toggle_mode = true
button.button_group = button_group
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 = 12
contents.offset_right = -12
contents.offset_top = 12
contents.offset_bottom = -12
contents.add_theme_constant_override("separation", 12)
var preview := TextureRect.new()
preview.custom_minimum_size.y = 100
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
contents.add_child(preview)
var description := Label.new()
description.mouse_filter = Control.MOUSE_FILTER_IGNORE
description.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
description.text = "%s\n%s\n\n%s" % [chassis["name"], chassis["role"], CATALOG.stats_text(chassis)]
description.add_theme_font_size_override("font_size", 13)
contents.add_child(description)
button.add_theme_font_size_override("font_size", 13)
button.tooltip_text = "%s — %s\nStatystyki bazowe przed bonusami modułów.\n%s" % [chassis["name"], chassis["role"], 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()