Crafting v3

This commit is contained in:
dingo
2025-12-23 15:09:05 -05:00
parent 2a5f4b9b78
commit 16be3a58cd
3 changed files with 59 additions and 30 deletions
+57 -30
View File
@@ -357,34 +357,33 @@ func _on_inventory_slot_input(event: InputEvent, slot_index: int):
if not event is InputEventMouseButton:
return
if event.button_index != MOUSE_BUTTON_LEFT:
return
var slot = inventory_slots[slot_index]
var item = slot.get_meta("item")
if event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
# Start drag if has item
if item and not item.is_empty():
_start_drag(item, slot_index, "inventory")
else:
# End drag
if is_dragging:
_end_drag()
if event.pressed:
# Start drag if has item
if item and not item.is_empty():
_start_drag(item, slot_index, "inventory")
print("[Crafting] Started drag: %s" % item.get("name", "?"))
func _on_crafting_slot_input(event: InputEvent, slot_index: int):
if not event is InputEventMouseButton:
return
var slot = crafting_slots[slot_index]
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
var slot = crafting_slots[slot_index]
if is_dragging:
# Drop item here
# Drop the dragged item here
_drop_on_crafting_slot(slot_index)
print("[Crafting] Dropped on slot %d" % slot_index)
else:
# Pick up from crafting slot
# Click to clear the slot
var item = slot.get_meta("item")
if item and not item.is_empty():
# Just clear the slot (item is still in inventory)
_clear_crafting_slot(slot_index)
elif event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
@@ -404,6 +403,7 @@ func _start_drag(item: Dictionary, source_slot: int, source_type: String):
if label:
label.text = item.get("name", "Item")
drag_preview.visible = true
drag_preview.global_position = get_viewport().get_mouse_position() + Vector2(15, 15)
func _drop_on_crafting_slot(slot_index: int):
if not is_dragging or drag_data.is_empty():
@@ -412,11 +412,11 @@ func _drop_on_crafting_slot(slot_index: int):
var slot = crafting_slots[slot_index]
var item = drag_data.get("item", {})
# Place in crafting slot
# Place in crafting slot (visual only - items stay in inventory)
slot.set_meta("item", item.duplicate())
_update_slot_display(slot, item)
# Green border to show it's in crafting
# Green border to show it's in crafting grid
var style = _create_slot_style(Color(0.15, 0.2, 0.15), Color(0.3, 0.6, 0.3))
slot.add_theme_stylebox_override("normal", style)
@@ -440,6 +440,36 @@ func _clear_crafting_grid():
for i in range(crafting_slots.size()):
_clear_crafting_slot(i)
func _input(event):
if not visible:
return
# Handle drag release anywhere
if is_dragging and event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and not event.pressed:
# Check if we're over a crafting slot
var dominated = false
for i in range(crafting_slots.size()):
var slot = crafting_slots[i]
var rect = slot.get_global_rect()
if rect.has_point(get_viewport().get_mouse_position()):
_drop_on_crafting_slot(i)
dominated = true
break
if not dominated:
# Dropped elsewhere, cancel
_end_drag()
# Cancel drag on right click
if is_dragging and event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
_end_drag()
if event.is_action_pressed("escape_menu"):
_on_close()
get_viewport().set_input_as_handled()
func _process(_delta):
if is_dragging and drag_preview:
drag_preview.global_position = get_viewport().get_mouse_position() + Vector2(15, 15)
@@ -604,7 +634,17 @@ func _on_crafting_result(result: Dictionary):
_refresh_recipe_list()
_clear_crafting_grid()
else:
_show_result(result.get("error", "Failed!"), Color.RED)
var error_text = result.get("error", "Failed!")
var materials_lost = result.get("materialsLost", false)
if materials_lost:
_show_result("[color=red]%s[/color]" % error_text, Color.RED)
# Refresh inventory since materials were lost
GameState.request_inventory()
else:
_show_result(error_text, Color.YELLOW)
_clear_crafting_grid()
func _show_result(text: String, color: Color):
if result_label:
@@ -613,16 +653,3 @@ func _show_result(text: String, color: Color):
result_label.text = text
else:
result_label.text = "[color=#%s]%s[/color]" % [color.to_html(false), text]
func _input(event):
if not visible:
return
if event.is_action_pressed("escape_menu"):
_on_close()
get_viewport().set_input_as_handled()
# Cancel drag on escape or right click
if is_dragging:
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_RIGHT:
_end_drag()