Randoloot/scripts/ui/equipment_slot_view.gd

95 lines
3.0 KiB
GDScript

extends PanelContainer
@export var slot_kind: String = "cargo"
@export var slot_index: int = 0
@onready var slot_label: Label = $SlotLabel
var _inventory: ShipInventory
func _ready() -> void:
_inventory = get_tree().get_first_node_in_group("ship_inventory") as ShipInventory
if _inventory:
_inventory.inventory_changed.connect(_refresh)
custom_minimum_size = Vector2(108, 52) if slot_kind != "cargo" else Vector2(86, 62)
_refresh()
func _refresh() -> void:
if not is_node_ready() or not _inventory:
return
var item: ItemInstance = _inventory.get_item(slot_kind, slot_index)
if item == null or item.definition == null:
slot_label.text = _empty_slot_text()
return
var definition := item.definition
var category_name := _category_name(definition.category)
var value_text := _stat_text(item)
var slot_title := category_name
if slot_kind == "weapon":
var weapon_binding := "LPM" if slot_index == 0 else "PPM"
slot_title = "BROŃ %d [%s]" % [slot_index + 1, weapon_binding]
elif slot_kind == "module":
slot_title = "MODUŁ %d [%d]" % [slot_index + 1, slot_index + 1]
slot_label.text = "%s\n%s\n%s" % [slot_title, definition.display_name, value_text]
func _get_drag_data(_at_position: Vector2) -> Variant:
if not _inventory:
return null
var item: ItemInstance = _inventory.get_item(slot_kind, slot_index)
if item == null:
return null
var preview := Label.new()
preview.text = item.definition.display_name
preview.add_theme_color_override("font_color", Color(0.85, 0.94, 1.0))
set_drag_preview(preview)
return {"from_kind": slot_kind, "from_index": slot_index}
func _can_drop_data(_at_position: Vector2, data: Variant) -> bool:
return (
_inventory != null
and data is Dictionary
and _inventory.can_move_item(
str(data.get("from_kind", "")),
int(data.get("from_index", -1)),
slot_kind,
slot_index
)
)
func _drop_data(_at_position: Vector2, data: Variant) -> void:
_inventory.move_item(
str(data.get("from_kind", "")),
int(data.get("from_index", -1)),
slot_kind,
slot_index
)
func _empty_slot_text() -> String:
if slot_kind == "weapon":
var weapon_binding := "LPM" if slot_index == 0 else "PPM"
return "BROŃ %d [%s]\nPUSTY" % [slot_index + 1, weapon_binding]
if slot_kind == "module":
return "MODUŁ %d [%d]\nPUSTY" % [slot_index + 1, slot_index + 1]
return "PUSTY"
func _category_name(category: int) -> String:
match category:
ItemDefinition.Category.WEAPON:
return "BROŃ"
ItemDefinition.Category.SHIELD:
return "TARCZA"
ItemDefinition.Category.HULL:
return "KADŁUB"
ItemDefinition.Category.GENERATOR:
return "GENERATOR"
ItemDefinition.Category.EXTENSION:
return "ROZSZERZENIE"
return "PRZEDMIOT"
func _stat_text(item: ItemInstance) -> String:
var definition := item.definition
if definition.primary_stat.is_empty():
return "Poz. %d" % item.item_level
var sign_text := "+" if definition.primary_value >= 0.0 else ""
return "%s%.0f%s · Poz. %d" % [sign_text, definition.primary_value, definition.stat_suffix, item.item_level]