Skip to content

Commit

Permalink
remove item display dependency on InventoryDisplayElement
Browse files Browse the repository at this point in the history
  • Loading branch information
stilnat committed Dec 22, 2024
1 parent 61d832e commit fe6e755
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Assets/Scripts/SS3D/Systems/Inventory/UI/ItemDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace SS3D.Systems.Inventory.UI
/// </summary>
public class ItemDisplay : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerDownHandler, IPointerClickHandler
{
public event EventHandler OnDragOutOfUI;

[SerializeField]
private Image _itemImage;

Expand Down Expand Up @@ -99,7 +101,7 @@ public void OnEndDrag(PointerEventData eventData)
GameObject o = eventData.pointerCurrentRaycast.gameObject;
if (o == null)
{
GetComponentInParent<InventoryDisplayElement>().DropItemOutside(Item);
OnDragOutOfUI?.Invoke(this, EventArgs.Empty);
}
}

Expand Down
7 changes: 7 additions & 0 deletions Assets/Scripts/SS3D/Systems/Inventory/UI/ItemGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public void CreateItemDisplay(Item item, Vector2Int position, bool itemMovedInsi

GameObject o = Instantiate(_itemDisplayPrefab, transform);
ItemGridItem itemSpriteOnGrid = o.GetComponent<ItemGridItem>();
itemSpriteOnGrid.OnDragOutOfUI += HandleDragOutOfUI;

Vector2 cellSize = _gridLayout.cellSize;
o.GetComponent<RectTransform>().sizeDelta = new Vector2(cellSize.x, cellSize.y);
Expand All @@ -147,6 +148,7 @@ public void RemoveItemDisplay(Item item)
continue;
}

gridItem.OnDragOutOfUI -= HandleDragOutOfUI;
_gridItems.Remove(gridItem);
gridItem.gameObject.Dispose(true);
return;
Expand Down Expand Up @@ -197,6 +199,11 @@ protected override void OnItemDisplayDrop(ItemDisplay display)
// display.ShouldDrop = true;
}

private void HandleDragOutOfUI(object sender, EventArgs e)
{
DropItemOutside(((ItemDisplay)sender).Item);
}

/// <summary>
/// Finds an item at a position
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using SS3D.Systems.Inventory.Containers;
using SS3D.Systems.Inventory.Interfaces;
using SS3D.Systems.Inventory.Items;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
Expand Down Expand Up @@ -72,6 +73,7 @@ protected override void OnItemDisplayDrop(ItemDisplay display)
protected void Start()
{
Assert.IsNotNull(_itemDisplay);
_itemDisplay.OnDragOutOfUI += HandleDragOutOfUI;
if (Container != null)
{
UpdateContainer(Container);
Expand All @@ -88,6 +90,11 @@ protected void OnDestroy()
Destroy(_itemDisplay);
}

private void HandleDragOutOfUI(object sender, EventArgs e)
{
DropItemOutside(_itemDisplay.Item);
}

/// <summary>
/// Change the displayed sprite inside the slot.
/// </summary>
Expand Down

0 comments on commit fe6e755

Please sign in to comment.