fixed so there isn't too many spam messages
This commit is contained in:
@@ -233,6 +233,7 @@ func _create_equipment_slots():
|
||||
button.size_flags_horizontal = 0
|
||||
button.size_flags_vertical = 0
|
||||
button.connect("pressed", _on_equipment_slot_pressed.bind(slot_name))
|
||||
button.connect("gui_input", _on_equipment_slot_gui_input.bind(slot_name))
|
||||
# Connect focus_entered like inspiration system (for keyboard navigation)
|
||||
if local_player and local_player.character_stats:
|
||||
var equipped_item = local_player.character_stats.equipment[slot_name]
|
||||
@@ -288,6 +289,19 @@ func _on_equipment_slot_pressed(slot_name: String):
|
||||
_update_selection_highlight()
|
||||
_update_selection_rectangle()
|
||||
|
||||
func _on_equipment_slot_gui_input(event: InputEvent, slot_name: String):
|
||||
# Handle double-click to unequip
|
||||
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
|
||||
if event.double_click and event.pressed:
|
||||
# Double-click detected - unequip the item
|
||||
selected_slot = slot_name
|
||||
selected_item = local_player.character_stats.equipment[slot_name] if local_player and local_player.character_stats else null
|
||||
selected_type = "equipment" if selected_item else ""
|
||||
|
||||
if selected_type == "equipment" and selected_slot != "":
|
||||
# Use the same logic as F key to unequip
|
||||
_handle_f_key()
|
||||
|
||||
func _update_selection_highlight():
|
||||
# This function is kept for compatibility but now uses _update_selection_rectangle()
|
||||
_update_selection_rectangle()
|
||||
@@ -488,6 +502,7 @@ func _update_ui():
|
||||
button.flat = false # Use styleboxes
|
||||
button.focus_mode = Control.FOCUS_ALL # Allow button to receive focus
|
||||
button.connect("pressed", _on_inventory_item_pressed.bind(item))
|
||||
button.connect("gui_input", _on_inventory_item_gui_input.bind(item))
|
||||
# Connect focus_entered like inspiration system (for keyboard navigation)
|
||||
# Note: focus_entered will trigger when we call grab_focus(), but _on_inventory_item_pressed
|
||||
# just updates selection state, so it should be safe
|
||||
@@ -540,12 +555,27 @@ func _update_ui():
|
||||
inventory_selection_col = max(0, row.get_child_count() - 1)
|
||||
|
||||
# Update selection only if selected_type is already set (don't auto-update during initialization)
|
||||
if selected_type != "":
|
||||
# Don't call _set_selection() here if we already have a valid selection - it will reset to 0,0
|
||||
# Only call it if selection is empty or invalid
|
||||
var should_reset_selection = false
|
||||
if selected_type == "":
|
||||
should_reset_selection = true
|
||||
elif selected_type == "item":
|
||||
# Check if current selection is still valid
|
||||
if inventory_selection_row < 0 or inventory_selection_row >= inventory_rows_list.size():
|
||||
should_reset_selection = true
|
||||
elif inventory_selection_row >= 0 and inventory_selection_row < inventory_rows_list.size():
|
||||
var row = inventory_rows_list[inventory_selection_row]
|
||||
if inventory_selection_col < 0 or inventory_selection_col >= row.get_child_count():
|
||||
should_reset_selection = true
|
||||
|
||||
if should_reset_selection:
|
||||
_set_selection()
|
||||
elif selected_type != "":
|
||||
# Selection is valid, just update it
|
||||
_update_selection_from_navigation()
|
||||
_update_selection_rectangle()
|
||||
_update_info_panel()
|
||||
|
||||
_set_selection()
|
||||
|
||||
# Reset update flag
|
||||
is_updating_ui = false
|
||||
@@ -553,15 +583,30 @@ func _update_ui():
|
||||
func _set_selection():
|
||||
# NOW check for items AFTER UI is updated
|
||||
# Initialize selection - prefer inventory, but if empty, check equipment
|
||||
# Only set initial selection if not already set, or if current selection is invalid
|
||||
var needs_initial_selection = false
|
||||
if selected_type == "":
|
||||
needs_initial_selection = true
|
||||
elif selected_type == "item":
|
||||
# Check if current selection is still valid
|
||||
if inventory_selection_row >= inventory_rows_list.size() or inventory_selection_row < 0:
|
||||
needs_initial_selection = true
|
||||
elif inventory_selection_row >= 0 and inventory_selection_row < inventory_rows_list.size():
|
||||
var row = inventory_rows_list[inventory_selection_row]
|
||||
if inventory_selection_col >= row.get_child_count() or inventory_selection_col < 0:
|
||||
needs_initial_selection = true
|
||||
|
||||
# Check if we have inventory items
|
||||
if inventory_rows_list.size() > 0 and inventory_items_list.size() > 0:
|
||||
selected_type = "item"
|
||||
inventory_selection_row = 0
|
||||
inventory_selection_col = 0
|
||||
# Ensure selection is set correctly
|
||||
if needs_initial_selection:
|
||||
# Only reset to 0 if we need initial selection
|
||||
selected_type = "item"
|
||||
inventory_selection_row = 0
|
||||
inventory_selection_col = 0
|
||||
# Ensure selection is set correctly (preserves existing selection if valid)
|
||||
_update_selection_from_navigation()
|
||||
# Debug: Print selection state
|
||||
print("InventoryUI: Initial selection - type: ", selected_type, " row: ", inventory_selection_row, " col: ", inventory_selection_col, " item: ", selected_item)
|
||||
print("InventoryUI: Selection - type: ", selected_type, " row: ", inventory_selection_row, " col: ", inventory_selection_col, " item: ", selected_item)
|
||||
# Now set focus - buttons should be ready
|
||||
await _update_selection_rectangle() # Await to ensure focus is set
|
||||
_update_info_panel()
|
||||
@@ -859,6 +904,40 @@ func _on_inventory_item_pressed(item: Item):
|
||||
_update_selection_highlight()
|
||||
_update_selection_rectangle()
|
||||
|
||||
func _on_inventory_item_gui_input(event: InputEvent, item: Item):
|
||||
# Handle double-click to equip/consume and right-click to drop
|
||||
if event is InputEventMouseButton:
|
||||
if event.button_index == MOUSE_BUTTON_LEFT and event.double_click and event.pressed:
|
||||
# Double-click detected - equip or consume the item
|
||||
selected_item = item
|
||||
selected_slot = ""
|
||||
selected_type = "item"
|
||||
|
||||
# Update navigation position first
|
||||
var item_index = inventory_items_list.find(item)
|
||||
if item_index >= 0:
|
||||
var items_per_row: int = 8
|
||||
inventory_selection_row = floor(item_index / float(items_per_row))
|
||||
inventory_selection_col = item_index % items_per_row
|
||||
|
||||
# Use the same logic as F key to equip/consume
|
||||
_handle_f_key()
|
||||
elif event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
|
||||
# Right-click detected - drop the item
|
||||
selected_item = item
|
||||
selected_slot = ""
|
||||
selected_type = "item"
|
||||
|
||||
# Update navigation position first
|
||||
var item_index = inventory_items_list.find(item)
|
||||
if item_index >= 0:
|
||||
var items_per_row: int = 8
|
||||
inventory_selection_row = floor(item_index / float(items_per_row))
|
||||
inventory_selection_col = item_index % items_per_row
|
||||
|
||||
# Use the same logic as E key to drop
|
||||
_handle_e_key()
|
||||
|
||||
func _on_character_changed(_char: CharacterStats):
|
||||
# Always update stats when character changes (even if inventory is closed)
|
||||
# Equipment changes affect max HP/MP which should be reflected everywhere
|
||||
@@ -984,25 +1063,90 @@ func _handle_f_key():
|
||||
Item.EquipmentType.ACCESSORY:
|
||||
target_slot_name = "accessory"
|
||||
|
||||
# Remember current item position before equipping
|
||||
var items_per_row = 8
|
||||
var current_item_index = inventory_selection_row * items_per_row + inventory_selection_col
|
||||
|
||||
# Check if target slot has an item (will be placed back in inventory)
|
||||
var slot_has_item = char_stats.equipment[target_slot_name] != null
|
||||
|
||||
# Check if this is the last item in inventory (before equipping)
|
||||
var was_last_item = char_stats.inventory.size() == 1
|
||||
|
||||
char_stats.equip_item(selected_item)
|
||||
# Equip the item, placing old item at the same position if slot had an item
|
||||
var insert_index = current_item_index if slot_has_item else -1
|
||||
char_stats.equip_item(selected_item, insert_index)
|
||||
|
||||
# Play armour sound when equipping
|
||||
if sfx_armour:
|
||||
sfx_armour.play()
|
||||
|
||||
# If this was the last item, set selection state BEFORE _update_ui()
|
||||
# so that _update_selection_from_navigation() works correctly
|
||||
if was_last_item and target_slot_name != "":
|
||||
# Update UI first
|
||||
_update_ui()
|
||||
|
||||
# If slot had an item, keep selection at the same position (old item is now there)
|
||||
if slot_has_item and current_item_index < char_stats.inventory.size():
|
||||
# Keep selection at the same position
|
||||
selected_type = "item"
|
||||
selected_slot = ""
|
||||
# Recalculate row/col from index (may have changed if rows shifted)
|
||||
inventory_selection_row = floor(current_item_index / float(items_per_row))
|
||||
inventory_selection_col = current_item_index % items_per_row
|
||||
# Ensure row/col are within bounds
|
||||
if inventory_selection_row >= inventory_rows_list.size():
|
||||
inventory_selection_row = max(0, inventory_rows_list.size() - 1)
|
||||
if inventory_selection_row >= 0 and inventory_selection_row < inventory_rows_list.size():
|
||||
var row = inventory_rows_list[inventory_selection_row]
|
||||
if inventory_selection_col >= row.get_child_count():
|
||||
inventory_selection_col = max(0, row.get_child_count() - 1)
|
||||
_update_selection_from_navigation()
|
||||
elif was_last_item and target_slot_name != "":
|
||||
# Last item was equipped, move selection to equipment slot
|
||||
if target_slot_name in equipment_slots_list:
|
||||
selected_type = "equipment"
|
||||
selected_slot = target_slot_name
|
||||
equipment_selection_index = equipment_slots_list.find(target_slot_name)
|
||||
selected_item = char_stats.equipment[target_slot_name]
|
||||
|
||||
_update_ui()
|
||||
_update_selection_from_navigation()
|
||||
else:
|
||||
# Item was removed, try to keep selection at same position if possible
|
||||
if current_item_index < char_stats.inventory.size():
|
||||
# Item at next position moved up, keep selection there
|
||||
selected_type = "item"
|
||||
selected_slot = ""
|
||||
inventory_selection_row = floor(current_item_index / float(items_per_row))
|
||||
inventory_selection_col = current_item_index % items_per_row
|
||||
if inventory_selection_row >= inventory_rows_list.size():
|
||||
inventory_selection_row = max(0, inventory_rows_list.size() - 1)
|
||||
if inventory_selection_row >= 0 and inventory_selection_row < inventory_rows_list.size():
|
||||
var row = inventory_rows_list[inventory_selection_row]
|
||||
if inventory_selection_col >= row.get_child_count():
|
||||
inventory_selection_col = max(0, row.get_child_count() - 1)
|
||||
_update_selection_from_navigation()
|
||||
elif current_item_index > 0:
|
||||
# Move to previous position (current_item_index - 1) if current is out of bounds
|
||||
var previous_index = current_item_index - 1
|
||||
inventory_selection_row = floor(previous_index / float(items_per_row))
|
||||
inventory_selection_col = previous_index % items_per_row
|
||||
if inventory_selection_row >= 0 and inventory_selection_row < inventory_rows_list.size():
|
||||
var row = inventory_rows_list[inventory_selection_row]
|
||||
if inventory_selection_col >= row.get_child_count():
|
||||
inventory_selection_col = max(0, row.get_child_count() - 1)
|
||||
selected_type = "item"
|
||||
selected_slot = ""
|
||||
_update_selection_from_navigation()
|
||||
else:
|
||||
# Previous position is also out of bounds, move to equipment if available
|
||||
selected_type = ""
|
||||
selected_slot = ""
|
||||
selected_item = null
|
||||
_update_selection_from_navigation()
|
||||
else:
|
||||
# No items left, move to equipment if available
|
||||
selected_type = ""
|
||||
selected_slot = ""
|
||||
selected_item = null
|
||||
_update_selection_from_navigation()
|
||||
|
||||
# Update selection rectangle and info panel
|
||||
_update_selection_rectangle()
|
||||
@@ -1017,6 +1161,10 @@ func _use_consumable_item(item: Item):
|
||||
|
||||
var char_stats = local_player.character_stats
|
||||
|
||||
# Remember current item position before consuming
|
||||
var items_per_row = 8
|
||||
var current_item_index = inventory_selection_row * items_per_row + inventory_selection_col
|
||||
|
||||
# Determine if it's a potion or food based on item name
|
||||
var is_potion = "potion" in item.item_name.to_lower()
|
||||
|
||||
@@ -1041,6 +1189,43 @@ func _use_consumable_item(item: Item):
|
||||
if index >= 0:
|
||||
char_stats.inventory.remove_at(index)
|
||||
char_stats.character_changed.emit(char_stats)
|
||||
|
||||
# Update UI first
|
||||
_update_ui()
|
||||
|
||||
# Try to keep selection at the same position if possible
|
||||
if current_item_index < char_stats.inventory.size():
|
||||
# Item at next position moved up, keep selection there
|
||||
selected_type = "item"
|
||||
selected_slot = ""
|
||||
inventory_selection_row = floor(current_item_index / float(items_per_row))
|
||||
inventory_selection_col = current_item_index % items_per_row
|
||||
if inventory_selection_row >= inventory_rows_list.size():
|
||||
inventory_selection_row = max(0, inventory_rows_list.size() - 1)
|
||||
if inventory_selection_row >= 0 and inventory_selection_row < inventory_rows_list.size():
|
||||
var row = inventory_rows_list[inventory_selection_row]
|
||||
if inventory_selection_col >= row.get_child_count():
|
||||
inventory_selection_col = max(0, row.get_child_count() - 1)
|
||||
_update_selection_from_navigation()
|
||||
_update_selection_rectangle()
|
||||
elif current_item_index > 0:
|
||||
# Move to previous position if current is out of bounds
|
||||
current_item_index = char_stats.inventory.size() - 1
|
||||
inventory_selection_row = floor(current_item_index / float(items_per_row))
|
||||
inventory_selection_col = current_item_index % items_per_row
|
||||
if inventory_selection_row >= 0 and inventory_selection_row < inventory_rows_list.size():
|
||||
var row = inventory_rows_list[inventory_selection_row]
|
||||
if inventory_selection_col >= row.get_child_count():
|
||||
inventory_selection_col = max(0, row.get_child_count() - 1)
|
||||
_update_selection_from_navigation()
|
||||
_update_selection_rectangle()
|
||||
else:
|
||||
# No items left, clear selection
|
||||
selected_type = ""
|
||||
selected_slot = ""
|
||||
selected_item = null
|
||||
_update_selection_from_navigation()
|
||||
_update_selection_rectangle()
|
||||
|
||||
print(local_player.name, " used item: ", item.item_name)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user