479 lines
14 KiB
GDScript
479 lines
14 KiB
GDScript
extends CanvasLayer
|
|
|
|
## Stash UI
|
|
## VIP feature for secure item storage between dungeon runs
|
|
|
|
@onready var close_button = $PanelContainer/VBoxContainer/Header/CloseButton
|
|
@onready var title_label = $PanelContainer/VBoxContainer/Header/Title
|
|
@onready var vip_message = $PanelContainer/VBoxContainer/VIPMessage
|
|
@onready var content = $PanelContainer/VBoxContainer/Content
|
|
@onready var inventory_grid = $PanelContainer/VBoxContainer/Content/InventorySection/InventoryGrid
|
|
@onready var stash_grid = $PanelContainer/VBoxContainer/Content/StashSection/StashGrid
|
|
|
|
const INVENTORY_DISPLAY_SLOTS = 30
|
|
const STASH_SLOTS = 50
|
|
|
|
const RARITY_COLORS = {
|
|
"Common": Color(0.7, 0.7, 0.7),
|
|
"Uncommon": Color(0.18, 0.8, 0.44),
|
|
"Rare": Color(0.2, 0.6, 0.86),
|
|
"Epic": Color(0.61, 0.35, 0.71),
|
|
"Legendary": Color(0.95, 0.61, 0.07)
|
|
}
|
|
|
|
var inventory_slots: Array = []
|
|
var stash_slots: Array = []
|
|
var tooltip: PanelContainer = null
|
|
var quantity_dialog: Window = null
|
|
var is_vip: bool = false
|
|
|
|
# Pending transfer info
|
|
var _pending_action: String = "" # "deposit" or "withdraw"
|
|
var _pending_slot: int = -1
|
|
var _pending_max_quantity: int = 1
|
|
|
|
func _ready():
|
|
visible = false
|
|
|
|
if close_button:
|
|
close_button.pressed.connect(_on_close)
|
|
|
|
_setup_inventory_slots()
|
|
_setup_stash_slots()
|
|
_create_tooltip()
|
|
_create_quantity_dialog()
|
|
|
|
call_deferred("_connect_gamestate")
|
|
|
|
func _connect_gamestate():
|
|
if GameState:
|
|
GameState.inventory_updated.connect(_refresh_inventory)
|
|
GameState.stash_updated.connect(_on_stash_updated)
|
|
GameState.stash_result.connect(_on_stash_result)
|
|
|
|
func _setup_inventory_slots():
|
|
if not inventory_grid:
|
|
return
|
|
for i in range(INVENTORY_DISPLAY_SLOTS):
|
|
var slot = _create_slot_button()
|
|
slot.set_meta("slot_type", "inventory")
|
|
slot.set_meta("slot_index", i)
|
|
slot.set_meta("item", null)
|
|
slot.gui_input.connect(_on_inventory_slot_input.bind(i))
|
|
slot.mouse_entered.connect(_on_slot_hover.bind(slot))
|
|
slot.mouse_exited.connect(_hide_tooltip)
|
|
inventory_grid.add_child(slot)
|
|
inventory_slots.append(slot)
|
|
|
|
func _setup_stash_slots():
|
|
if not stash_grid:
|
|
return
|
|
for i in range(STASH_SLOTS):
|
|
var slot = _create_slot_button()
|
|
slot.set_meta("slot_type", "stash")
|
|
slot.set_meta("slot_index", i)
|
|
slot.set_meta("item", null)
|
|
slot.gui_input.connect(_on_stash_slot_input.bind(i))
|
|
slot.mouse_entered.connect(_on_slot_hover.bind(slot))
|
|
slot.mouse_exited.connect(_hide_tooltip)
|
|
stash_grid.add_child(slot)
|
|
stash_slots.append(slot)
|
|
|
|
func _create_slot_button() -> Button:
|
|
var btn = Button.new()
|
|
btn.custom_minimum_size = Vector2(40, 40)
|
|
btn.add_theme_stylebox_override("normal", _create_slot_style(Color(0.15, 0.15, 0.2)))
|
|
btn.add_theme_stylebox_override("hover", _create_slot_style(Color(0.25, 0.25, 0.35)))
|
|
return btn
|
|
|
|
func _create_slot_style(color: Color) -> StyleBoxFlat:
|
|
var style = StyleBoxFlat.new()
|
|
style.bg_color = color
|
|
style.border_color = Color(0.3, 0.3, 0.4)
|
|
style.set_border_width_all(2)
|
|
style.set_corner_radius_all(4)
|
|
return style
|
|
|
|
func _create_tooltip():
|
|
tooltip = PanelContainer.new()
|
|
tooltip.z_index = 100
|
|
|
|
var style = StyleBoxFlat.new()
|
|
style.bg_color = Color(0.1, 0.1, 0.15, 0.95)
|
|
style.border_color = Color(0.4, 0.4, 0.5)
|
|
style.set_border_width_all(1)
|
|
style.set_corner_radius_all(4)
|
|
style.set_content_margin_all(8)
|
|
tooltip.add_theme_stylebox_override("panel", style)
|
|
|
|
var vbox = VBoxContainer.new()
|
|
vbox.name = "VBox"
|
|
|
|
var name_label = Label.new()
|
|
name_label.name = "ItemName"
|
|
name_label.add_theme_font_size_override("font_size", 14)
|
|
vbox.add_child(name_label)
|
|
|
|
var type_label = Label.new()
|
|
type_label.name = "ItemType"
|
|
type_label.add_theme_font_size_override("font_size", 11)
|
|
type_label.modulate = Color(0.7, 0.7, 0.7)
|
|
vbox.add_child(type_label)
|
|
|
|
var stats_label = Label.new()
|
|
stats_label.name = "ItemStats"
|
|
stats_label.add_theme_font_size_override("font_size", 12)
|
|
vbox.add_child(stats_label)
|
|
|
|
tooltip.add_child(vbox)
|
|
tooltip.visible = false
|
|
add_child(tooltip)
|
|
|
|
func _create_quantity_dialog():
|
|
quantity_dialog = Window.new()
|
|
quantity_dialog.title = "Select Quantity"
|
|
quantity_dialog.size = Vector2i(250, 150)
|
|
quantity_dialog.unresizable = true
|
|
quantity_dialog.transient = true
|
|
quantity_dialog.exclusive = true
|
|
quantity_dialog.visible = false
|
|
|
|
var panel = PanelContainer.new()
|
|
panel.anchors_preset = Control.PRESET_FULL_RECT
|
|
|
|
var vbox = VBoxContainer.new()
|
|
vbox.add_theme_constant_override("separation", 10)
|
|
|
|
var label = Label.new()
|
|
label.name = "QuantityLabel"
|
|
label.text = "How many?"
|
|
label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
|
vbox.add_child(label)
|
|
|
|
var hbox = HBoxContainer.new()
|
|
hbox.alignment = BoxContainer.ALIGNMENT_CENTER
|
|
|
|
var spin = SpinBox.new()
|
|
spin.name = "QuantitySpinner"
|
|
spin.min_value = 1
|
|
spin.max_value = 99
|
|
spin.value = 1
|
|
spin.custom_minimum_size = Vector2(100, 30)
|
|
hbox.add_child(spin)
|
|
|
|
var max_btn = Button.new()
|
|
max_btn.name = "MaxButton"
|
|
max_btn.text = "Max"
|
|
max_btn.pressed.connect(_on_quantity_max)
|
|
hbox.add_child(max_btn)
|
|
|
|
vbox.add_child(hbox)
|
|
|
|
var btn_row = HBoxContainer.new()
|
|
btn_row.alignment = BoxContainer.ALIGNMENT_CENTER
|
|
btn_row.add_theme_constant_override("separation", 20)
|
|
|
|
var ok_btn = Button.new()
|
|
ok_btn.name = "OkButton"
|
|
ok_btn.text = "Transfer"
|
|
ok_btn.custom_minimum_size = Vector2(80, 30)
|
|
ok_btn.pressed.connect(_on_quantity_confirm)
|
|
btn_row.add_child(ok_btn)
|
|
|
|
var cancel_btn = Button.new()
|
|
cancel_btn.name = "CancelButton"
|
|
cancel_btn.text = "Cancel"
|
|
cancel_btn.custom_minimum_size = Vector2(80, 30)
|
|
cancel_btn.pressed.connect(_on_quantity_cancel)
|
|
btn_row.add_child(cancel_btn)
|
|
|
|
vbox.add_child(btn_row)
|
|
|
|
panel.add_child(vbox)
|
|
quantity_dialog.add_child(panel)
|
|
quantity_dialog.close_requested.connect(_on_quantity_cancel)
|
|
add_child(quantity_dialog)
|
|
|
|
func show_stash():
|
|
visible = true
|
|
# Request inventory first, then stash
|
|
if GameState:
|
|
GameState.request_inventory()
|
|
# Small delay before requesting stash
|
|
await get_tree().create_timer(0.1).timeout
|
|
GameState.request_stash()
|
|
|
|
func _on_stash_updated(stash_data: Dictionary):
|
|
is_vip = true
|
|
_update_vip_state(true)
|
|
_refresh_stash(stash_data)
|
|
|
|
func _on_stash_result(result: Dictionary):
|
|
var success = result.get("success", false)
|
|
var error = result.get("error", "")
|
|
var message = result.get("message", "")
|
|
|
|
if error == "vip_required":
|
|
is_vip = false
|
|
_update_vip_state(false)
|
|
|
|
func _update_vip_state(is_vip_status: bool):
|
|
if vip_message:
|
|
vip_message.visible = not is_vip_status
|
|
if content:
|
|
content.visible = is_vip_status
|
|
if title_label:
|
|
title_label.text = "🔐 Stash (VIP Feature)" if is_vip_status else "🔒 Stash (VIP Required)"
|
|
|
|
func _refresh_inventory():
|
|
for i in range(INVENTORY_DISPLAY_SLOTS):
|
|
if i >= inventory_slots.size():
|
|
break
|
|
var item = GameState.inventory[i] if i < GameState.inventory.size() else null
|
|
_update_slot(inventory_slots[i], item)
|
|
|
|
func _refresh_stash(stash_data: Dictionary):
|
|
var items = stash_data.get("items", [])
|
|
|
|
# Clear all stash slots first
|
|
for slot in stash_slots:
|
|
_update_slot(slot, null)
|
|
|
|
# Fill in items at their slots
|
|
for item_data in items:
|
|
var slot_index = int(item_data.get("slotIndex", item_data.get("SlotIndex", -1)))
|
|
if slot_index >= 0 and slot_index < stash_slots.size():
|
|
# Merge definition data into item_data for display
|
|
var definition = item_data.get("definition", item_data.get("Definition", {}))
|
|
var merged_item = item_data.duplicate()
|
|
if definition:
|
|
for key in definition:
|
|
merged_item[key] = definition[key]
|
|
_update_slot(stash_slots[slot_index], merged_item)
|
|
|
|
func _update_slot(slot: Button, item):
|
|
# Clear any existing children
|
|
for child in slot.get_children():
|
|
child.queue_free()
|
|
|
|
if item == null or (item is Dictionary and item.is_empty()):
|
|
slot.text = ""
|
|
slot.set_meta("item", null)
|
|
slot.add_theme_stylebox_override("normal", _create_slot_style(Color(0.15, 0.15, 0.2)))
|
|
else:
|
|
slot.text = ""
|
|
slot.set_meta("item", item)
|
|
|
|
# Try to load sprite from CDN
|
|
var sprite_path = item.get("spritePath", item.get("SpritePath", ""))
|
|
var has_sprite = false
|
|
|
|
if sprite_path and not sprite_path.is_empty() and UpdateManager:
|
|
var texture = UpdateManager.load_texture(sprite_path)
|
|
if texture:
|
|
var sprite = TextureRect.new()
|
|
sprite.texture = texture
|
|
sprite.expand_mode = TextureRect.EXPAND_FIT_WIDTH_PROPORTIONAL
|
|
sprite.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
|
|
sprite.custom_minimum_size = Vector2(28, 28)
|
|
sprite.position = Vector2(6, 6)
|
|
slot.add_child(sprite)
|
|
has_sprite = true
|
|
|
|
if not has_sprite:
|
|
slot.text = _get_item_emoji(item)
|
|
|
|
# Add stack count label if quantity > 1
|
|
var quantity = item.get("quantity", item.get("Quantity", 1))
|
|
if quantity > 1:
|
|
var count_label = Label.new()
|
|
count_label.text = str(quantity)
|
|
count_label.add_theme_font_size_override("font_size", 9)
|
|
count_label.add_theme_color_override("font_color", Color.WHITE)
|
|
count_label.add_theme_color_override("font_shadow_color", Color.BLACK)
|
|
count_label.position = Vector2(slot.custom_minimum_size.x - 16, slot.custom_minimum_size.y - 14)
|
|
slot.add_child(count_label)
|
|
|
|
# Style based on rarity
|
|
var rarity = item.get("rarity", item.get("Rarity", "Common"))
|
|
var style = _create_slot_style(Color(0.15, 0.15, 0.2))
|
|
style.border_color = RARITY_COLORS.get(rarity, Color.GRAY)
|
|
style.border_width_bottom = 3
|
|
slot.add_theme_stylebox_override("normal", style)
|
|
|
|
func _get_item_emoji(item: Dictionary) -> String:
|
|
var item_type = item.get("type", item.get("Type", "")).to_lower()
|
|
var item_name = item.get("name", item.get("Name", "")).to_lower()
|
|
|
|
match item_type:
|
|
"consumable":
|
|
if "health" in item_name or "potion" in item_name: return "🧪"
|
|
if "mana" in item_name: return "💙"
|
|
return "🧪"
|
|
"material":
|
|
if "bone" in item_name: return "🦴"
|
|
if "hide" in item_name or "leather" in item_name: return "🥩"
|
|
if "essence" in item_name or "soul" in item_name: return "👻"
|
|
return "📦"
|
|
"weapon": return "⚔️"
|
|
"armor": return "🛡️"
|
|
"accessory": return "💍"
|
|
_: return "📦"
|
|
|
|
func _on_inventory_slot_input(event: InputEvent, slot_index: int):
|
|
if not event is InputEventMouseButton:
|
|
return
|
|
if not event.pressed:
|
|
return
|
|
|
|
var item = GameState.inventory[slot_index] if slot_index < GameState.inventory.size() else null
|
|
if item == null or (item is Dictionary and item.is_empty()):
|
|
return
|
|
|
|
if event.button_index == MOUSE_BUTTON_RIGHT:
|
|
# Right-click to deposit to stash - show quantity dialog
|
|
var quantity = item.get("quantity", item.get("Quantity", 1))
|
|
if quantity > 1:
|
|
_show_quantity_dialog("deposit", slot_index, quantity)
|
|
else:
|
|
GameState.stash_deposit(slot_index, 1)
|
|
|
|
func _on_stash_slot_input(event: InputEvent, slot_index: int):
|
|
if not event is InputEventMouseButton:
|
|
return
|
|
if not event.pressed:
|
|
return
|
|
|
|
var slot = stash_slots[slot_index] if slot_index < stash_slots.size() else null
|
|
if slot == null:
|
|
return
|
|
|
|
var item = slot.get_meta("item")
|
|
if item == null:
|
|
return
|
|
|
|
if event.button_index == MOUSE_BUTTON_RIGHT:
|
|
# Right-click to withdraw from stash - show quantity dialog
|
|
var quantity = item.get("quantity", item.get("Quantity", 1))
|
|
if quantity > 1:
|
|
_show_quantity_dialog("withdraw", slot_index, quantity)
|
|
else:
|
|
GameState.stash_withdraw(slot_index, 1)
|
|
|
|
func _show_quantity_dialog(action: String, slot: int, max_qty: int):
|
|
_pending_action = action
|
|
_pending_slot = slot
|
|
_pending_max_quantity = max_qty
|
|
|
|
var spinner = quantity_dialog.get_node("PanelContainer/VBoxContainer/HBoxContainer/QuantitySpinner")
|
|
if spinner:
|
|
spinner.max_value = max_qty
|
|
spinner.value = max_qty
|
|
|
|
var label = quantity_dialog.get_node("PanelContainer/VBoxContainer/QuantityLabel")
|
|
if label:
|
|
if action == "deposit":
|
|
label.text = "Deposit how many?"
|
|
else:
|
|
label.text = "Withdraw how many?"
|
|
|
|
# Center dialog
|
|
var screen_size = get_viewport().get_visible_rect().size
|
|
quantity_dialog.position = Vector2i(
|
|
int((screen_size.x - quantity_dialog.size.x) / 2),
|
|
int((screen_size.y - quantity_dialog.size.y) / 2)
|
|
)
|
|
quantity_dialog.visible = true
|
|
quantity_dialog.grab_focus()
|
|
|
|
func _on_quantity_max():
|
|
var spinner = quantity_dialog.get_node("PanelContainer/VBoxContainer/HBoxContainer/QuantitySpinner")
|
|
if spinner:
|
|
spinner.value = _pending_max_quantity
|
|
|
|
func _on_quantity_confirm():
|
|
var spinner = quantity_dialog.get_node("PanelContainer/VBoxContainer/HBoxContainer/QuantitySpinner")
|
|
var qty = int(spinner.value) if spinner else 1
|
|
|
|
if _pending_action == "deposit":
|
|
GameState.stash_deposit(_pending_slot, qty)
|
|
elif _pending_action == "withdraw":
|
|
GameState.stash_withdraw(_pending_slot, qty)
|
|
|
|
quantity_dialog.visible = false
|
|
_pending_action = ""
|
|
_pending_slot = -1
|
|
|
|
func _on_quantity_cancel():
|
|
quantity_dialog.visible = false
|
|
_pending_action = ""
|
|
_pending_slot = -1
|
|
|
|
func _on_slot_hover(slot: Button):
|
|
if not slot.has_meta("item"):
|
|
_hide_tooltip()
|
|
return
|
|
var item = slot.get_meta("item")
|
|
if item == null:
|
|
_hide_tooltip()
|
|
return
|
|
|
|
_show_tooltip(item, slot)
|
|
|
|
func _show_tooltip(item: Dictionary, slot: Button):
|
|
if not tooltip:
|
|
return
|
|
|
|
var vbox = tooltip.get_node_or_null("VBox")
|
|
if not vbox:
|
|
return
|
|
|
|
var name_label = vbox.get_node_or_null("ItemName")
|
|
var type_label = vbox.get_node_or_null("ItemType")
|
|
var stats_label = vbox.get_node_or_null("ItemStats")
|
|
|
|
if not name_label:
|
|
return
|
|
|
|
var rarity = item.get("rarity", item.get("Rarity", "Common"))
|
|
var item_name = item.get("name", item.get("Name", "Unknown"))
|
|
var item_type = item.get("type", item.get("Type", ""))
|
|
var quantity = item.get("quantity", item.get("Quantity", 1))
|
|
|
|
if quantity > 1:
|
|
name_label.text = "%s (x%d)" % [item_name, quantity]
|
|
else:
|
|
name_label.text = item_name
|
|
name_label.modulate = RARITY_COLORS.get(rarity, Color.WHITE)
|
|
|
|
if type_label:
|
|
type_label.text = "%s %s" % [rarity, item_type]
|
|
|
|
if stats_label:
|
|
stats_label.text = ""
|
|
|
|
# Position tooltip
|
|
await get_tree().process_frame
|
|
var screen_size = get_viewport().get_visible_rect().size
|
|
var pos = slot.global_position + Vector2(slot.size.x + 10, 0)
|
|
|
|
if pos.x + tooltip.size.x > screen_size.x:
|
|
pos.x = slot.global_position.x - tooltip.size.x - 10
|
|
if pos.y + tooltip.size.y > screen_size.y:
|
|
pos.y = screen_size.y - tooltip.size.y - 10
|
|
|
|
tooltip.global_position = pos
|
|
tooltip.visible = true
|
|
|
|
func _hide_tooltip():
|
|
if tooltip:
|
|
tooltip.visible = false
|
|
|
|
func _on_close():
|
|
visible = false
|
|
|
|
func _input(event):
|
|
if visible and event.is_action_pressed("escape_menu"):
|
|
visible = false
|
|
get_viewport().set_input_as_handled()
|