Big fixes

This commit is contained in:
dingo
2026-01-26 17:42:23 -05:00
parent c1a97b219e
commit b434eb6031
10 changed files with 303 additions and 76 deletions
+216 -48
View File
@@ -49,8 +49,11 @@ var inventory_slots: Array = []
var equipment_slots: Dictionary = {}
var tooltip: PanelContainer = null
var selected_hotbar_slot: int = -1 # For hotbar assignment
var right_click_start_time: int = 0 # Track right-click duration
var right_click_slot: int = -1
var left_click_start_time: int = 0 # Track left-click duration for drag detection
var left_click_slot: int = -1
var left_click_start_pos: Vector2 = Vector2.ZERO
const DRAG_THRESHOLD_TIME: int = 150 # ms before drag starts
const DRAG_THRESHOLD_DISTANCE: float = 5.0 # pixels before drag starts
func _ready():
visible = false
@@ -71,6 +74,23 @@ func _connect_gamestate():
UpdateManager.file_downloaded.connect(_on_file_downloaded)
UpdateManager.update_completed.connect(_on_updates_completed)
func _process(_delta):
if not visible:
return
# Check for drag initiation while holding left click
if left_click_slot >= 0 and not DragManager.is_dragging:
if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
var current_pos = get_viewport().get_mouse_position()
var distance = current_pos.distance_to(left_click_start_pos)
var elapsed = Time.get_ticks_msec() - left_click_start_time
# Start drag if held long enough OR moved far enough
if elapsed >= DRAG_THRESHOLD_TIME or distance >= DRAG_THRESHOLD_DISTANCE:
var item = GameState.inventory[left_click_slot] if left_click_slot < GameState.inventory.size() else null
if item and not item.is_empty():
DragManager.start_drag(item, "inventory", left_click_slot)
func _on_file_downloaded(file_path: String):
# If an item texture was downloaded, refresh inventory to show it
if file_path.begins_with("textures/items/"):
@@ -114,6 +134,11 @@ func _create_slot_button() -> Button:
btn.custom_minimum_size = Vector2(48, 48)
btn.add_theme_stylebox_override("normal", _create_slot_style(Color(0.408, 0.408, 0.322, 1.0)))
btn.add_theme_stylebox_override("hover", _create_slot_style(Color(0.408, 0.408, 0.322, 0.902)))
btn.add_theme_stylebox_override("pressed", _create_slot_style(Color(0.408, 0.408, 0.322, 0.902)))
btn.add_theme_stylebox_override("focus", _create_slot_style(Color(0.408, 0.408, 0.322, 1.0)))
# Disable built-in button behavior
btn.disabled = false
btn.focus_mode = Control.FOCUS_NONE
return btn
func _create_slot_style(color: Color) -> StyleBoxFlat:
@@ -124,6 +149,7 @@ func _create_slot_style(color: Color) -> StyleBoxFlat:
style.set_corner_radius_all(4)
return style
func _create_tooltip():
tooltip = PanelContainer.new()
tooltip.z_index = 100
@@ -302,51 +328,58 @@ func _get_item_emoji(item: Dictionary) -> String:
_: return "❓"
func _on_inventory_gui_input(event: InputEvent, slot_index: int):
if not event is InputEventMouseButton:
return
var item = GameState.inventory[slot_index] if slot_index < GameState.inventory.size() else null
# Left click
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
if DragManager and DragManager.is_dragging:
# Drop item here
_drop_item_on_slot(slot_index, item)
elif item != null:
var item_type = item.get("type", item.get("Type", ""))
if item_type == "Consumable":
# Use consumable on left click
GameState.send_use_item(slot_index)
elif item_type in ["Weapon", "Armor", "Accessory"]:
# Equip on left click
GameState.send_equip_item(slot_index)
# Right click
elif event.button_index == MOUSE_BUTTON_RIGHT:
if event.pressed:
# Right click press - start drag if has item
if DragManager and DragManager.is_dragging:
# Already dragging, drop here
_drop_item_on_slot(slot_index, item)
elif item and not item.is_empty():
# Start drag
right_click_start_time = Time.get_ticks_msec()
right_click_slot = slot_index
DragManager.start_drag(item, "inventory", slot_index)
else:
# Right click release
if DragManager and DragManager.is_dragging:
var drag_time = Time.get_ticks_msec() - right_click_start_time
# If released quickly (< 200ms) without moving, it's a quick click
if drag_time < 200 and right_click_slot == slot_index and item:
DragManager.end_drag()
# Show hotbar menu for consumables
var item_type = item.get("type", item.get("Type", ""))
if item_type == "Consumable":
_show_hotbar_menu(slot_index)
else:
# It was a drag, drop here
# Handle mouse button events
if event is InputEventMouseButton:
# Left click
if event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
if DragManager and DragManager.is_dragging:
# Drop item here
_drop_item_on_slot(slot_index, item)
elif item != null and not item.is_empty():
# Start tracking for potential drag
left_click_start_time = Time.get_ticks_msec()
left_click_slot = slot_index
left_click_start_pos = get_viewport().get_mouse_position()
else:
# Left click release - handled by _input for drag, here for quick click
if not (DragManager and DragManager.is_dragging):
if left_click_slot == slot_index and item != null:
# Quick click (not a drag) - use or equip
var click_duration = Time.get_ticks_msec() - left_click_start_time
if click_duration < DRAG_THRESHOLD_TIME:
var item_type = item.get("type", item.get("Type", ""))
if item_type == "Consumable":
GameState.send_use_item(slot_index)
elif item_type in ["Weapon", "Armor", "Accessory"]:
GameState.send_equip_item(slot_index)
# Reset tracking
left_click_slot = -1
# Right click - assign consumable to hotbar
elif event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
if DragManager and DragManager.is_dragging:
# Cancel drag on right click
DragManager.end_drag()
elif item and not item.is_empty():
var item_type = item.get("type", item.get("Type", ""))
if item_type == "Consumable":
_show_hotbar_menu(slot_index)
# Handle mouse motion for drag detection
elif event is InputEventMouseMotion:
if left_click_slot == slot_index and left_click_slot >= 0:
if not DragManager.is_dragging:
var current_pos = get_viewport().get_mouse_position()
var distance = current_pos.distance_to(left_click_start_pos)
var elapsed = Time.get_ticks_msec() - left_click_start_time
# Start drag if held long enough OR moved far enough
if elapsed >= DRAG_THRESHOLD_TIME or distance >= DRAG_THRESHOLD_DISTANCE:
if item and not item.is_empty():
DragManager.start_drag(item, "inventory", slot_index)
func _drop_item_on_slot(slot_index: int, target_item):
if not DragManager or not DragManager.is_dragging:
@@ -376,10 +409,68 @@ func _drop_item_on_slot(slot_index: int, target_item):
DragManager.end_drag()
func _on_equipment_gui_input(event: InputEvent, slot_name: String):
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
var item = GameState.equipment.get(slot_name)
if item != null:
GameState.send_unequip_item(slot_name)
if not event is InputEventMouseButton:
return
if event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
if DragManager and DragManager.is_dragging:
# Drop dragged item onto equipment slot
_drop_item_on_equipment(slot_name)
else:
# Left click on equipped item - unequip it
var item = GameState.equipment.get(slot_name)
if item != null:
GameState.send_unequip_item(slot_name)
func _drop_item_on_equipment(slot_name: String):
if not DragManager or not DragManager.is_dragging:
return
var drag_item = DragManager.get_drag_item()
var source_type = DragManager.drag_data.get("source_type", "")
var source_slot = DragManager.drag_data.get("source_slot", -1)
# Only allow drops from inventory
if source_type != "inventory" or source_slot < 0:
DragManager.end_drag()
return
# Check if item can be equipped in this slot
var item_type = drag_item.get("type", drag_item.get("Type", ""))
var item_slot = drag_item.get("slot", drag_item.get("Slot", ""))
# Validate the item can go in this equipment slot
var valid_slot = false
match slot_name:
"weapon":
valid_slot = item_type == "Weapon" and item_slot in ["MainHand", "TwoHand", "Ranged"]
"offhand":
valid_slot = item_type == "Weapon" and item_slot == "OffHand" or item_type == "Armor" and item_slot == "Shield"
"helm":
valid_slot = item_type == "Armor" and item_slot == "Head"
"chest":
valid_slot = item_type == "Armor" and item_slot == "Chest"
"gloves":
valid_slot = item_type == "Armor" and item_slot == "Hands"
"legs":
valid_slot = item_type == "Armor" and item_slot == "Legs"
"boots":
valid_slot = item_type == "Armor" and item_slot == "Feet"
"necklace":
valid_slot = item_type == "Accessory" and item_slot == "Neck"
"ring1", "ring2":
valid_slot = item_type == "Accessory" and item_slot == "Ring"
"quiver":
valid_slot = item_slot == "Quiver" or item_slot == "Back"
if valid_slot:
# Equip the item from the source inventory slot
GameState.send_equip_item(source_slot)
else:
print("Cannot equip %s in %s slot" % [drag_item.get("name", "item"), slot_name])
DragManager.end_drag()
func _can_stack_items(item1: Dictionary, item2: Dictionary) -> bool:
# Check if both items are the same and stackable
@@ -559,6 +650,28 @@ func _input(event):
if not visible:
return
# Handle mouse release for drag operations - find what's under the mouse
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and not event.pressed:
if DragManager and DragManager.is_dragging:
# Find what slot is under the mouse and drop there
var drop_slot = _get_inventory_slot_at_mouse()
var equip_slot = _get_equipment_slot_at_mouse()
var hotbar_slot = _get_hotbar_slot_at_mouse()
if drop_slot >= 0:
var target_item = GameState.inventory[drop_slot] if drop_slot < GameState.inventory.size() else null
_drop_item_on_slot(drop_slot, target_item)
elif equip_slot != "":
_drop_item_on_equipment(equip_slot)
elif hotbar_slot >= 0:
_drop_item_on_hotbar(hotbar_slot)
else:
DragManager.end_drag()
left_click_slot = -1
get_viewport().set_input_as_handled()
return
if event.is_action_pressed("escape_menu"):
visible = false
get_viewport().set_input_as_handled()
@@ -567,3 +680,58 @@ func _input(event):
# Auto-stack inventory
GameState.send_auto_stack_inventory()
get_viewport().set_input_as_handled()
func _get_inventory_slot_at_mouse() -> int:
var mouse_pos = get_viewport().get_mouse_position()
for i in range(inventory_slots.size()):
var slot = inventory_slots[i]
var rect = Rect2(slot.global_position, slot.size)
if rect.has_point(mouse_pos):
return i
return -1
func _get_equipment_slot_at_mouse() -> String:
var mouse_pos = get_viewport().get_mouse_position()
for slot_name in equipment_slots:
var slot = equipment_slots[slot_name]
var rect = Rect2(slot.global_position, slot.size)
if rect.has_point(mouse_pos):
return slot_name
return ""
func _get_hotbar_slot_at_mouse() -> int:
var mouse_pos = get_viewport().get_mouse_position()
# Find the HUD and check hotbar slots
var hud = get_tree().get_first_node_in_group("hud")
if hud and hud.has_method("get_hotbar_slots"):
var hotbar_slots = hud.get_hotbar_slots()
for i in range(hotbar_slots.size()):
var slot = hotbar_slots[i]
var rect = Rect2(slot.global_position, slot.size)
if rect.has_point(mouse_pos):
return i
return -1
func _drop_item_on_hotbar(hotbar_slot: int):
if not DragManager or not DragManager.is_dragging:
return
var drag_item = DragManager.get_drag_item()
var source_type = DragManager.drag_data.get("source_type", "")
var source_slot = DragManager.drag_data.get("source_slot", -1)
# Only allow drops from inventory
if source_type != "inventory" or source_slot < 0:
DragManager.end_drag()
return
# Allow consumables and equipment in hotbar
var item_type = drag_item.get("type", drag_item.get("Type", ""))
print("[Type]" , item_type)
if item_type not in ["Consumable", "Weapon", "Armor", "Accessory"]:
DragManager.end_drag()
return
# Assign to hotbar
GameState.assign_hotbar(hotbar_slot, source_slot)
DragManager.end_drag()