152 lines
5.1 KiB
GDScript
152 lines
5.1 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
|
||
var _delete_button: Button
|
||
|
||
func _ready() -> void:
|
||
if slot_kind == "cargo":
|
||
var overlay := Control.new()
|
||
overlay.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
||
add_child(overlay)
|
||
_delete_button = Button.new()
|
||
_delete_button.text = "×"
|
||
_delete_button.tooltip_text = "Rozłóż przedmiot: +10 części"
|
||
_delete_button.flat = true
|
||
_delete_button.add_theme_font_size_override("font_size", 13)
|
||
_delete_button.add_theme_color_override("font_color", Color("ff8989"))
|
||
overlay.add_child(_delete_button)
|
||
_delete_button.set_anchors_and_offsets_preset(Control.PRESET_TOP_RIGHT)
|
||
_delete_button.offset_left = -20
|
||
_delete_button.offset_right = 0
|
||
_delete_button.offset_top = 0
|
||
_delete_button.offset_bottom = 18
|
||
_delete_button.pressed.connect(_delete_item)
|
||
_inventory = get_tree().get_first_node_in_group("ship_inventory") as ShipInventory
|
||
if _inventory:
|
||
_inventory.inventory_changed.connect(_refresh)
|
||
match slot_kind:
|
||
"weapon":
|
||
custom_minimum_size = Vector2(90, 38)
|
||
"module":
|
||
custom_minimum_size = Vector2(104, 36)
|
||
_:
|
||
custom_minimum_size = Vector2(84, 40)
|
||
_refresh()
|
||
|
||
func _refresh() -> void:
|
||
if not is_node_ready() or not _inventory:
|
||
return
|
||
var item: ItemInstance = _inventory.get_item(slot_kind, slot_index)
|
||
tooltip_text = item.get_tooltip_text() if item != null else ""
|
||
if _delete_button:
|
||
_delete_button.visible = item != null
|
||
if item == null or item.definition == null:
|
||
slot_label.add_theme_color_override("font_color", Color("82909e"))
|
||
slot_label.text = _empty_slot_text()
|
||
return
|
||
var definition := item.definition
|
||
slot_label.add_theme_color_override("font_color", item.get_rarity_color())
|
||
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 _delete_item() -> void:
|
||
if _inventory:
|
||
_inventory.delete_cargo_item(slot_index)
|
||
|
||
func _make_custom_tooltip(for_text: String) -> Object:
|
||
var panel := PanelContainer.new()
|
||
var background := StyleBoxFlat.new()
|
||
background.bg_color = Color("101925")
|
||
background.border_color = Color("52677e")
|
||
background.set_border_width_all(1)
|
||
background.set_content_margin_all(12)
|
||
panel.add_theme_stylebox_override("panel", background)
|
||
var label := Label.new()
|
||
label.text = for_text
|
||
label.custom_minimum_size.x = 330
|
||
label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
||
label.add_theme_font_size_override("font_size", 13)
|
||
var item := _inventory.get_item(slot_kind, slot_index)
|
||
if item:
|
||
label.add_theme_color_override("font_color", item.get_rarity_color())
|
||
panel.add_child(label)
|
||
return panel
|
||
|
||
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"
|
||
ItemDefinition.Category.ACTIVE_EXTENSION:
|
||
return "ROZSZERZENIE AKTYWNE"
|
||
return "PRZEDMIOT"
|
||
|
||
func _stat_text(item: ItemInstance) -> String:
|
||
var definition := item.definition
|
||
if definition.category == ItemDefinition.Category.WEAPON:
|
||
return "%s · Poz. %d" % [item.get_damage_range_text(), item.item_level]
|
||
if definition.primary_stat.is_empty():
|
||
return "Poz. %d" % item.item_level
|
||
var total_value := item.get_stat_total(definition.primary_stat)
|
||
var sign_text := "+" if total_value >= 0.0 else ""
|
||
return "%s%.0f%s · Poz. %d" % [sign_text, total_value, definition.stat_suffix, item.item_level]
|