Files
beyond/Assets/Scripts/InvectorDerivatives/bItemWindowDisplay.cs
2024-11-20 15:21:28 +01:00

150 lines
5.0 KiB
C#

using Invector.vItemManager;
using UnityEngine;
using UnityEngine.EventSystems;
namespace Beyond
{
public class bItemWindowDisplay : MonoBehaviour
{
public bInventory inventory;
public bItemWindow itemWindow;
public bItemOptionWindow optionWindow;
[HideInInspector]
public bItemSlot currentSelectedSlot;
[HideInInspector]
public int amount;
public virtual void OnEnable()
{
Debug.Log("item window");
if (inventory == null)
inventory = GetComponentInParent<bInventory>();
if (inventory && itemWindow)
{
inventory.onDestroyItem.RemoveListener(OnDestroyItem);
inventory.onDestroyItem.AddListener(OnDestroyItem);
itemWindow.CreateEquipmentWindow(inventory.items, OnSubmit, OnSelectSlot);
inventory.OnUpdateInventory -= CheckItemExits;
inventory.OnUpdateInventory += CheckItemExits;
}
}
public void OnDisable()
{
if (inventory)
inventory.OnUpdateInventory -= CheckItemExits;
}
public virtual void OnDestroyItem(bItem item, int amount)
{
var _slot = itemWindow.slots.Find(slot => slot.item.Equals(item));
if (_slot != null && (_slot.item == null || _slot.item.amount == 0))
{
itemWindow.slots.Remove(_slot);
Destroy(_slot.gameObject);
}
}
public virtual void OnSubmit(bItemSlot slot)
{
currentSelectedSlot = slot;
if (slot.item)
{
var rect = slot.GetComponent<RectTransform>();
if (optionWindow.CanOpenOptions(slot.item))
{
//optionWindow.transform.position = rect.position;
optionWindow.gameObject.SetActive(true);
optionWindow.EnableOptions(slot);
// currentSelectedSlot = slot;
}
}
}
public virtual void OnSelectSlot(bItemSlot slot)
{
currentSelectedSlot = slot;
}
public virtual void DropItem()
{
if (amount > 0)
{
inventory.OnDropItem(currentSelectedSlot.item, amount);
if (currentSelectedSlot != null && (currentSelectedSlot.item == null || currentSelectedSlot.item.amount <= 0))
{
if (itemWindow.slots.Contains(currentSelectedSlot))
itemWindow.slots.Remove(currentSelectedSlot);
Destroy(currentSelectedSlot.gameObject);
if (itemWindow.slots.Count > 0)
SetSelectable(itemWindow.slots[0].gameObject);
}
}
}
public virtual void LeaveItem()
{
if (amount > 0)
{
inventory.OnDestroyItem(currentSelectedSlot.item, amount);
if (currentSelectedSlot != null && (currentSelectedSlot.item == null || currentSelectedSlot.item.amount <= 0))
{
if (itemWindow.slots.Contains(currentSelectedSlot))
itemWindow.slots.Remove(currentSelectedSlot);
Destroy(currentSelectedSlot.gameObject);
if (itemWindow.slots.Count > 0)
SetSelectable(itemWindow.slots[0].gameObject);
}
}
}
public virtual void UseItem()
{
inventory.OnUseItem(currentSelectedSlot.item);
}
private void CheckItemExits()
{
itemWindow.ReloadItems(inventory.items);
//var slotsToDestroy =itemWindow.slots.FindAll(slot => slot.item == null || slot.item.amount == 0);
//for(int i =0;i<slotsToDestroy.Count;i++)
//{
// itemWindow.slots.Remove(slotsToDestroy[i]);
// Destroy(slotsToDestroy[i].gameObject);
//}
}
public virtual void SetOldSelectable()
{
try
{
if (currentSelectedSlot != null)
SetSelectable(currentSelectedSlot.gameObject);
else if (itemWindow.slots.Count > 0 && itemWindow.slots[0] != null)
{
SetSelectable(itemWindow.slots[0].gameObject);
}
}
catch
{
}
}
public virtual void SetSelectable(GameObject target)
{
try
{
var pointer = new PointerEventData(EventSystem.current);
ExecuteEvents.Execute(EventSystem.current.currentSelectedGameObject, pointer, ExecuteEvents.pointerExitHandler);
EventSystem.current.SetSelectedGameObject(target, new BaseEventData(EventSystem.current));
ExecuteEvents.Execute(target, pointer, ExecuteEvents.selectHandler);
}
catch { }
}
}
}