2022-10-19 13:51:46 +00:00
using System.Collections ;
using System.Collections.Generic ;
using UnityEngine ;
using UnityEngine.UI ;
using UnityEngine.EventSystems ;
using static SimpleInventorySystem . GridUI ;
using System ;
namespace SimpleInventorySystem
{
[RequireComponent(typeof(RectTransform), typeof(CanvasGroup))]
2022-10-29 19:56:00 +00:00
public class ItemUI : MonoBehaviour , IPointerEnterHandler , IPointerExitHandler
2022-10-19 13:51:46 +00:00
{
private RectTransform rectTransform ;
private Image image ;
public Canvas canvas ;
private CanvasGroup canvasGroup ;
private float scale = 1f ;
2022-10-29 19:56:00 +00:00
public SlotUI previousSlot { get ; private set ; }
2022-10-19 13:51:46 +00:00
private SlotUI slot ;
private Cell [ ] cells ;
private bool isDragging ;
public int width ;
public int height ;
private Size imageSizeOnGrid ;
private Orientation imageOrientation = Orientation . Portrait ;
public ItemTags ItemTag = ItemTags . Any ;
2022-11-06 02:28:29 +00:00
public List < ItemUI > ContainedItems = new List < ItemUI > ( ) ;
public ItemUI ParentContainer ;
2022-10-29 19:56:00 +00:00
public GameObject container ;
public GameObject containerUIPrefab ;
2022-10-19 13:51:46 +00:00
public Item itemPrefab ;
2022-10-29 19:56:00 +00:00
public bool Equipped ;
public bool Stackable
{
get { return StackLimit > 1 ; }
}
private int count ;
public int StackLimit = 1 ;
public int Count
{
get { return count ; }
set
{
if ( ! Stackable )
{
count = 1 ;
}
else
{
count = value ;
}
SetRenderItemUIPropertyValue ( ) ;
}
}
private RenderItemUIProperty renderProp ;
[HideInInspector] public bool PickedUpOnFrame ;
[HideInInspector] public List < GameObject > ContextWindows = new List < GameObject > ( ) ;
[HideInInspector] public bool IsLinkedSlotClone ;
2022-11-06 02:28:29 +00:00
public bool PointerIsHoveredOver { get { return InventorySystem . IsMouseOverItemUITop ( this ) ; } }
public LimitStackSlotCapacity limitStackSlotCapacity ;
2022-10-19 13:51:46 +00:00
//public Item item;
private void Awake ( )
{
rectTransform = GetComponent < RectTransform > ( ) ;
image = transform . GetComponentInChildren < Image > ( ) ;
canvasGroup = GetComponent < CanvasGroup > ( ) ;
2022-10-29 19:56:00 +00:00
renderProp = GetComponent < RenderItemUIProperty > ( ) ;
2022-11-06 02:28:29 +00:00
limitStackSlotCapacity = GetComponent < LimitStackSlotCapacity > ( ) ;
2022-10-19 13:51:46 +00:00
2022-10-29 19:56:00 +00:00
if ( width < = 0 | | height < = 0 )
2022-10-19 13:51:46 +00:00
{
2022-11-06 02:28:29 +00:00
Debug . LogError ( "Inventory ItemUI must contain a positive integer value for Width and Height: " + name ) ;
2022-10-19 13:51:46 +00:00
}
Transform handler = transform . Find ( "Handler" ) ;
int eulerAngle = Mathf . Abs ( Mathf . CeilToInt ( handler . rotation . eulerAngles . z ) ) ;
switch ( eulerAngle )
{
case 270 :
imageOrientation = ( height > width ) ? Orientation . Portrait : Orientation . Landscape ;
break ;
case 180 :
imageOrientation = ( width > height ) ? Orientation . Landscape : Orientation . Portrait ;
break ;
case 90 :
imageOrientation = ( height > width ) ? Orientation . Portrait : Orientation . Landscape ;
break ;
default :
imageOrientation = ( width > height ) ? Orientation . Landscape : Orientation . Portrait ;
break ;
}
//InstantiateItem();
}
2022-11-06 02:28:29 +00:00
/// <summary>
/// Returns an integer number if it has a container with a GridUI. the number returned is either -1 if no gridUI exists, or the capacity in terms of 1x1 cells.
/// </summary>
/// <returns></returns>
public int ContainerCapacity ( )
{
GridUI gridUI = null ;
if ( container ! = null & & container . TryGetComponent < GridUI > ( out gridUI ) )
{
return gridUI . gridSize . Width * gridUI . gridSize . Height ;
}
return - 1 ;
}
2022-10-29 19:56:00 +00:00
private void SetRenderItemUIPropertyValue ( )
{
if ( renderProp ! = null )
{
renderProp . value = Count . ToString ( ) ;
}
}
2022-10-19 13:51:46 +00:00
public void InstantiateItem ( )
{
//if (item == null && itemPrefab != null)
//{
// item = Instantiate(itemPrefab);
//}
}
2022-11-06 02:28:29 +00:00
public SlotUI GetSlotUI ( )
{
return slot ;
}
2022-10-19 13:51:46 +00:00
public void OnBeginDrag ( PointerEventData eventData )
{
}
public void OnDrag ( PointerEventData eventData )
{
}
public void OnEndDrag ( PointerEventData eventData )
2022-10-29 19:56:00 +00:00
{
//rectTransform.localPosition = new Vector3(0, 0);
}
public void Drop ( )
2022-10-19 13:51:46 +00:00
{
//Debug.Log("OnEndDrag");
if ( image ! = null )
{
canvasGroup . alpha = 1f ;
image . maskable = true ;
}
canvasGroup . blocksRaycasts = true ;
isDragging = false ;
2022-10-29 19:56:00 +00:00
InventorySystem . instance . DraggedItem = null ;
2022-10-19 13:51:46 +00:00
2022-10-29 19:56:00 +00:00
if ( slot = = null )
2022-10-19 13:51:46 +00:00
{
Debug . Log ( "ItemUI: OnEndDrag: Returning to parent" ) ;
ReturnToSlot ( ) ;
}
2022-10-29 19:56:00 +00:00
else
{
// TODO: this need improvement
Equipped = slot . EquipSlot ;
}
}
public void GrabAndDragItem ( )
{
if ( ! isDragging )
{
2022-11-06 02:28:29 +00:00
Debug . Log ( "GRAB AND DRAG" ) ;
2022-10-29 19:56:00 +00:00
//Debug.Log("OnPointerDown");
//Debug.Log("OnBeginDrag");
canvasGroup . alpha = . 7f ;
canvasGroup . blocksRaycasts = false ;
SetImageScale ( 1 ) ;
if ( slot ! = null )
previousSlot = slot ;
transform . SetParent ( canvas . transform ) ;
//rectTransform.pivot = new Vector2(.5f, .5f);
//rectTransform.position += new Vector3(50, -50, 0);
if ( slot ! = null )
{
slot . RemoveDragDropItem ( ) ;
slot = null ;
}
if ( cells ! = null )
{
foreach ( Cell c in cells )
{
if ( c ! = null )
{
c . inUse = false ;
//c.overlapped = false;
//s.ShowImage();
if ( c . GetSlot ( ) ! = null & & c . GetSlot ( ) ! = this . slot )
{
2022-11-06 02:28:29 +00:00
c . GetSlot ( ) . UnsetCell ( ) ; // RemoveDragDropItem(true);
2022-10-29 19:56:00 +00:00
}
}
}
cells = null ;
}
isDragging = true ;
InventorySystem . instance . DraggedItem = this ;
PickedUpOnFrame = true ;
// Destroy each associated window, since we won't rely on windows if we are moving this item
for ( int i = ContextWindows . Count - 1 ; i > = 0 ; i - - )
{
ContextWindows [ i ] . SetActive ( true ) ;
}
ContextWindows . Clear ( ) ;
}
2022-10-19 13:51:46 +00:00
}
2022-11-06 02:28:29 +00:00
public void AddToContainedItems ( ItemUI item )
{
ContainedItems . Add ( item ) ;
item . ParentContainer = this ;
}
public void RemoveFromContainedItems ( ItemUI item )
{
ContainedItems . Remove ( item ) ;
item . ParentContainer = null ;
}
2022-10-19 13:51:46 +00:00
public void OnPointerDown ( PointerEventData eventData )
{
2022-10-29 19:56:00 +00:00
if ( eventData . button = = PointerEventData . InputButton . Left )
{
GrabAndDragItem ( ) ;
}
}
public void OnPointerClick ( PointerEventData eventData )
{
if ( eventData . button = = PointerEventData . InputButton . Left )
{
GrabAndDragItem ( ) ;
}
else if ( eventData . button = = PointerEventData . InputButton . Right )
{
InventorySystem . instance . OpenContextMenu ( this ) ;
}
/ * else if ( eventData . button = = PointerEventData . InputButton . Right )
{
InventorySystem . instance . OpenContextMenu ( this ) ;
} * /
2022-10-19 13:51:46 +00:00
}
2022-10-29 19:56:00 +00:00
2022-10-19 13:51:46 +00:00
public void SetImageScale ( float scale )
{
image . transform . localScale / = this . scale ;
this . scale = scale ;
image . transform . localScale * = scale ;
}
public void SetSlot ( SlotUI slot )
{
this . slot = slot ;
}
// TODO: Make sure it was dropped somewhere, if not, return it to the original parent
public void ReturnToSlot ( )
{
2022-10-29 19:56:00 +00:00
if ( previousSlot ! = null )
2022-10-19 13:51:46 +00:00
{
2022-11-06 02:28:29 +00:00
Debug . Log ( "RETURNED: " + previousSlot . gameObject . name ) ;
2022-10-19 13:51:46 +00:00
previousSlot . DropOntoSlot ( this . gameObject ) ;
}
}
// Start is called\ before the first frame update
void Start ( )
{
imageSizeOnGrid = new Size ( width * InventorySystem . instance . DefaultSizeOnGrid . Width , height * InventorySystem . instance . DefaultSizeOnGrid . Height ) ;
if ( canvas = = null )
{
canvas = SimpleInventorySystem . InventorySystem . instance . inventoryUI ;
}
2022-10-29 19:56:00 +00:00
if ( Count = = 0 )
{
Count = 1 ;
}
SetRenderItemUIPropertyValue ( ) ;
2022-10-19 13:51:46 +00:00
}
public Orientation ImageOrientation ( )
{
return imageOrientation ;
}
public Size SizeAfterOrientation ( )
{
Debug . Log ( imageOrientation ) ;
2022-10-29 19:56:00 +00:00
if ( imageOrientation = = Orientation . Landscape )
2022-10-19 13:51:46 +00:00
{
if ( width > = height )
{
return new Size ( width , height ) ;
}
else
{
return new Size ( height , width ) ;
}
}
else
{
if ( height > = width )
{
return new Size ( width , height ) ;
}
else
{
return new Size ( height , width ) ;
}
}
}
public void HideSlots ( )
{
2022-10-29 19:56:00 +00:00
if ( cells ! = null )
2022-10-19 13:51:46 +00:00
{
foreach ( Cell cell in cells )
{
2022-10-29 19:56:00 +00:00
if ( cell ! = null )
2022-10-19 13:51:46 +00:00
{
SlotUI slot = cell . GetSlot ( ) ;
if ( slot ! = null )
slot . HideImage ( ) ;
}
}
}
}
public void SetSlots ( List < Cell > cellList )
{
cells = new Cell [ cellList . Count ] ;
2022-10-29 19:56:00 +00:00
for ( int i = 0 ; i < cellList . Count ; i + + )
{
if ( cellList [ i ] ! = null )
2022-10-19 13:51:46 +00:00
{
2022-10-29 19:56:00 +00:00
cells [ i ] = cellList [ i ] ;
cells [ i ] . inUse = true ;
2022-10-19 13:51:46 +00:00
}
2022-10-29 19:56:00 +00:00
}
2022-10-19 13:51:46 +00:00
}
public bool IsSingleCellItem ( )
{
return width = = 1 & & height = = 1 ;
}
2022-10-29 19:56:00 +00:00
public bool AddToStack ( int value = 1 )
{
if ( Stackable & & Count < StackLimit )
{
Count + = value ;
return true ;
}
return false ;
}
public bool RemoveFromStack ( int value = 1 )
{
if ( Stackable & & Count > 0 )
{
Count - = value ;
return true ;
}
return false ;
}
2022-10-19 13:51:46 +00:00
public void UnsetSlots ( )
{
cells = null ;
}
2022-10-29 19:56:00 +00:00
void FixedUpdate ( )
{
if ( isDragging )
{
// TODO: fix this
rectTransform . position = InventorySystem . instance . MouseInCanvasPosition ;
}
}
2022-10-19 13:51:46 +00:00
// Update is called once per frame
void Update ( )
{
2022-11-06 02:28:29 +00:00
if ( PointerIsHoveredOver & & InventorySystem . instance . DraggedItem = = null )
2022-10-19 13:51:46 +00:00
{
2022-11-06 02:28:29 +00:00
if ( Input . GetMouseButtonDown ( 0 ) & & ! isDragging & & InventorySystem . instance . DraggedItem = = null & & ! slot . DroppedOnFrame )
2022-10-29 19:56:00 +00:00
{
GrabAndDragItem ( ) ;
}
if ( Input . GetMouseButtonDown ( 1 ) & & InventorySystem . instance . DraggedItem = = null )
2022-10-19 13:51:46 +00:00
{
2022-10-29 19:56:00 +00:00
InventorySystem . instance . OpenContextMenu ( this ) ;
2022-10-19 13:51:46 +00:00
}
}
2022-10-29 19:56:00 +00:00
if ( Input . GetMouseButtonUp ( 0 ) )
{
PickedUpOnFrame = false ;
}
if ( isDragging )
{
if ( image ! = null )
{
image . maskable = false ;
}
if ( InventorySystem . instance . PressedKeyRotation ( true ) )
{
//Debug.Log("Rotate");
Transform handler = transform . Find ( "Handler" ) ;
handler . Rotate ( new Vector3 ( 0 , 0 , 1 ) , - 90 ) ;
handler . localPosition = new Vector3 ( 0 , 0 , 0 ) ;
int eulerAngle = Mathf . Abs ( Mathf . CeilToInt ( handler . rotation . eulerAngles . z ) ) ;
//Debug.Log(imageSizeOnGrid.width + ", " + imageSizeOnGrid.height);
switch ( eulerAngle )
{
case 270 :
handler . localPosition = new Vector3 ( imageSizeOnGrid . Width / 4 , 0 , 0 ) ;
imageOrientation = ( width > height ) ? Orientation . Portrait : Orientation . Landscape ;
break ;
case 180 :
handler . localPosition = new Vector3 ( imageSizeOnGrid . Width / 2 , - imageSizeOnGrid . Height / 2 , 0 ) ;
imageOrientation = ( width > height ) ? Orientation . Landscape : Orientation . Portrait ;
break ;
case 90 :
handler . localPosition = new Vector3 ( 0 , - imageSizeOnGrid . Height , 0 ) ;
imageOrientation = ( width > height ) ? Orientation . Portrait : Orientation . Landscape ;
break ;
default :
handler . localPosition = new Vector3 ( 0 , 0 , 0 ) ;
imageOrientation = ( width > height ) ? Orientation . Landscape : Orientation . Portrait ;
break ;
}
}
}
}
public void OnPointerExit ( PointerEventData eventData )
{
2022-11-06 02:28:29 +00:00
//PointerIsHoveredOver = false;
2022-10-29 19:56:00 +00:00
}
public void OnPointerEnter ( PointerEventData eventData )
{
2022-11-06 02:28:29 +00:00
//PointerIsHoveredOver = true;
2022-10-19 13:51:46 +00:00
}
}
}