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 ;
2023-02-07 13:11:46 +00:00
using TMPro ;
2022-10-19 13:51:46 +00:00
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
{
2022-12-23 03:50:16 +00:00
[Header("Item Attributes")]
2022-12-11 03:03:30 +00:00
public string itemName ;
2023-02-02 02:58:50 +00:00
public string displayName ;
2022-11-15 22:58:54 +00:00
public Image image ;
2022-12-23 03:50:16 +00:00
public RarityType rarity ;
public ItemTags ItemTag = ItemTags . Any ;
public int StackLimit = 1 ;
public int width ;
public int height ;
2023-01-27 02:16:31 +00:00
public float weight ;
2023-01-21 06:02:58 +00:00
public float monetaryValue ;
2022-12-23 03:50:16 +00:00
[Header("Container Prefab")]
public GameObject containerUIPrefab ;
[Header("Required Component Links")]
2022-12-11 03:03:30 +00:00
public Image backdropImage ;
2022-12-23 03:50:16 +00:00
public RectTransform emptyGridContainer ;
2023-02-07 13:11:46 +00:00
public TextMeshProUGUI titleText ;
public TextMeshProUGUI stackText ;
2022-12-23 03:50:16 +00:00
// Hidden public variables
[HideInInspector] public bool interactable = true ;
[HideInInspector] public Canvas overrideSortingCanvas ;
[HideInInspector] public bool Equipped ;
[HideInInspector] public List < ItemUI > ContainedItems = new List < ItemUI > ( ) ;
[HideInInspector] public ItemUI ParentContainer ;
[HideInInspector] public GameObject container ;
2022-11-17 13:19:28 +00:00
[HideInInspector] public Canvas canvas ;
2022-12-23 03:50:16 +00:00
[HideInInspector] public bool PickedUpOnFrame ;
[HideInInspector] public List < GameObject > ContextWindows = new List < GameObject > ( ) ;
2023-01-15 11:39:47 +00:00
[HideInInspector] public StorageWindowUI StorageWindow ;
2022-12-23 03:50:16 +00:00
[HideInInspector] public LimitStackSlotCapacity limitStackSlotCapacity ;
2023-01-15 11:39:47 +00:00
internal void ResetTransform ( )
{
RectTransform rect = GetComponent < RectTransform > ( ) ;
rect . anchorMin = rect . anchorMax = rect . pivot = new Vector2 ( 0 , 1 ) ;
rect . anchoredPosition = new Vector2 ( ) ;
imageSizeOnGrid = new Size ( width * InventorySystem . instance . DefaultSizeOnGrid . Width , height * InventorySystem . instance . DefaultSizeOnGrid . Height ) ;
rect . sizeDelta = new Vector2 ( imageSizeOnGrid . Width , imageSizeOnGrid . Height ) ;
}
2022-12-23 03:50:16 +00:00
[HideInInspector] public PickUp pickUp ;
[HideInInspector] public TargetAttachment targetAttachment ;
// private variables
2023-02-14 11:55:20 +00:00
private float eulerAngle ;
private Vector3 pivot ;
2022-12-23 03:50:16 +00:00
private Vector2 oldImageSizeDelta ;
private Vector2 oldSizeDelta ;
private RectTransform rectTransform ;
2022-10-19 13:51:46 +00:00
private CanvasGroup canvasGroup ;
private float scale = 1f ;
private SlotUI slot ;
private Cell [ ] cells ;
private bool isDragging ;
private Size imageSizeOnGrid ;
private Orientation imageOrientation = Orientation . Portrait ;
2023-02-07 13:11:46 +00:00
[HideInInspector] public Inventory Inventory ;
2022-12-23 03:50:16 +00:00
// archived variables
//public Item itemPrefab;
2022-11-15 22:58:54 +00:00
2022-12-23 03:50:16 +00:00
//public variables
public SlotUI previousSlot { get ; private set ; }
2022-10-29 19:56:00 +00:00
public bool Stackable
{
get { return StackLimit > 1 ; }
}
private int count ;
public int Count
{
get { return count ; }
set
{
if ( ! Stackable )
{
count = 1 ;
}
else
{
count = value ;
}
SetRenderItemUIPropertyValue ( ) ;
}
}
2023-02-14 11:55:20 +00:00
2022-11-06 02:28:29 +00:00
public bool PointerIsHoveredOver { get { return InventorySystem . IsMouseOverItemUITop ( this ) ; } }
2022-10-19 13:51:46 +00:00
//public Item item;
2022-12-11 03:03:30 +00:00
2022-10-19 13:51:46 +00:00
private void Awake ( )
{
rectTransform = GetComponent < RectTransform > ( ) ;
2022-11-15 22:58:54 +00:00
2022-12-11 03:03:30 +00:00
if ( image = = null )
2022-11-15 22:58:54 +00:00
{
Image [ ] images = transform . GetComponentsInChildren < Image > ( ) ;
2023-01-27 02:16:31 +00:00
for ( int i = 0 ; i < images . Length ; i + + )
2022-11-15 22:58:54 +00:00
{
2023-01-27 02:16:31 +00:00
if ( images [ i ] = = GetComponent < Image > ( ) )
2022-11-15 22:58:54 +00:00
{
continue ;
}
image = transform . GetComponentInChildren < Image > ( ) ;
break ;
}
}
2023-01-27 02:16:31 +00:00
if ( image = = null )
2022-11-15 22:58:54 +00:00
{
2022-12-11 03:03:30 +00:00
Debug . LogError ( "ItemUI: Item needs a child with an Image component." ) ;
2022-11-15 22:58:54 +00:00
}
2022-10-19 13:51:46 +00:00
canvasGroup = GetComponent < CanvasGroup > ( ) ;
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
}
2022-11-15 22:58:54 +00:00
int eulerAngle = Mathf . Abs ( Mathf . CeilToInt ( transform . rotation . eulerAngles . z ) ) ;
2022-10-19 13:51:46 +00:00
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 ;
}
2023-02-14 11:55:20 +00:00
//rectTransform.pivot = pivot;
2022-10-19 13:51:46 +00:00
//InstantiateItem();
}
2022-12-11 03:03:30 +00:00
public Rarity GetRarity ( )
{
2023-01-31 02:12:08 +00:00
//Debug.Log(rarity);
2022-12-11 03:03:30 +00:00
return InventorySystem . instance . GetRarityFromList ( rarity ) ;
}
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 ;
2023-01-27 02:16:31 +00:00
if ( container ! = null & & container . TryGetComponent < GridUI > ( out gridUI ) )
2022-11-06 02:28:29 +00:00
{
return gridUI . gridSize . Width * gridUI . gridSize . Height ;
}
return - 1 ;
}
2023-02-07 13:11:46 +00:00
public void FindTextMeshProUGUI ( )
{
if ( titleText = = null )
{
Transform titleGameObject = transform . Find ( "Title" ) ;
if ( titleGameObject ! = null )
{
titleText = titleGameObject . GetComponent < TextMeshProUGUI > ( ) ;
}
}
if ( stackText = = null )
{
Transform stackTextGameObject = transform . Find ( "StackCount" ) ;
if ( stackTextGameObject ! = null )
{
stackText = stackTextGameObject . GetComponent < TextMeshProUGUI > ( ) ;
}
}
}
2022-10-29 19:56:00 +00:00
private void SetRenderItemUIPropertyValue ( )
{
2023-02-07 13:11:46 +00:00
if ( titleText ! = null )
{
Debug . Log ( $"Set Item name: {itemName}" ) ;
titleText . text = ( displayName ! = null & & ! string . IsNullOrEmpty ( displayName ) ) ? displayName : itemName ;
}
if ( stackText ! = null )
2022-10-29 19:56:00 +00:00
{
2023-02-07 13:11:46 +00:00
stackText . text = Count . ToString ( ) ;
2022-10-29 19:56:00 +00:00
}
}
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 OnEndDrag ( PointerEventData eventData )
2022-10-29 19:56:00 +00:00
{
//rectTransform.localPosition = new Vector3(0, 0);
}
2023-02-09 11:53:30 +00:00
public bool QuickMove ( )
{
bool moved = false ;
if ( Equip ( ) )
{
//Debug.Log("Equipped");
moved = true ;
}
else if ( Store ( ) )
{
//Debug.Log("Stored");
moved = true ;
}
return moved ;
}
2022-12-11 03:03:30 +00:00
public bool Equip ( )
{
2023-02-09 11:53:30 +00:00
bool equipped = false ;
2022-12-11 03:03:30 +00:00
foreach ( TagSlotPairing tsp in InventorySystem . instance . TagSlotPairings )
{
2023-02-09 11:53:30 +00:00
//Debug.Log($"Equip: {tsp.itemTag}, {tsp.slot.gameObject.name}");
if ( tsp . slot = = slot )
{
//Debug.Log("Could not equip, storing");
continue ;
}
2023-01-25 07:14:16 +00:00
if ( tsp . CanEquip ( this ) )
2023-01-15 11:39:47 +00:00
{
UnsetSlot ( ) ;
2023-02-09 11:53:30 +00:00
equipped = tsp . DropOntoSlot ( this ) ;
2023-01-31 02:12:08 +00:00
//Debug.Log(tsp.slot.gameObject.name);
2023-02-09 11:53:30 +00:00
if ( equipped )
2023-01-15 11:39:47 +00:00
{
2023-02-09 11:53:30 +00:00
break ;
2023-01-15 11:39:47 +00:00
}
else
{
2023-02-09 11:53:30 +00:00
//Debug.Log("Returning to slot");
ReturnToSlot ( ) ;
2023-01-15 11:39:47 +00:00
}
}
}
2023-02-09 11:53:30 +00:00
return equipped ;
2023-01-15 11:39:47 +00:00
}
public bool Store ( )
{
2023-02-09 11:53:30 +00:00
bool stored = false ;
foreach ( TagSlotGridPairing tsgp in InventorySystem . instance . TagSlotGridPairings )
2023-01-15 11:39:47 +00:00
{
2023-02-09 11:53:30 +00:00
//Debug.Log($"Store: {tsgp.itemTag}, {tsgp.ReferencedGrid}");
2023-01-27 02:16:31 +00:00
if ( tsgp . CanStore ( this ) )
2022-12-11 03:03:30 +00:00
{
2023-02-09 11:53:30 +00:00
GrabAndDragItem ( ) ; // TODO: See if this conflicts with anything
//UnsetSlot();
stored = tsgp . Drop ( this ) ;
if ( ! stored )
2022-12-11 03:03:30 +00:00
{
ReturnToSlot ( ) ;
}
2022-12-23 03:50:16 +00:00
else
{
2023-01-29 20:36:11 +00:00
// TODO: see if this is needed
if ( container ! = null )
container . SetActive ( false ) ;
2023-02-09 11:53:30 +00:00
break ;
2022-12-23 03:50:16 +00:00
}
2022-12-11 03:03:30 +00:00
}
}
2023-02-09 11:53:30 +00:00
return stored ;
2022-12-11 03:03:30 +00:00
}
2023-01-27 02:16:31 +00:00
public bool DropItemInStorageContainer ( ItemUI droppedItem )
{
2023-01-31 02:12:08 +00:00
//Debug.Log("Checking");
2023-01-27 02:16:31 +00:00
if ( IsStorageContainer ( ) )
{
2023-01-31 02:12:08 +00:00
//Debug.Log("Storage container found");
2023-01-27 02:16:31 +00:00
return container . GetComponent < GridUI > ( ) . DropItemOnGrid ( droppedItem . gameObject ) ;
}
return false ;
}
2023-01-25 07:14:16 +00:00
public void DropItemOntoSlot ( bool fitImageInSlot = false )
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-12-23 03:50:16 +00:00
if ( ! slot . useImageAsSlotHolder )
ShowEmptyGrid ( ) ;
else
HideEmptyGrid ( ) ;
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
{
2023-01-31 02:12:08 +00:00
//Debug.Log("ItemUI: OnEndDrag: Returning to parent");
2022-11-15 22:58:54 +00:00
//ReturnToSlot();
2022-10-19 13:51:46 +00:00
}
2022-10-29 19:56:00 +00:00
else
{
// TODO: this need improvement
Equipped = slot . EquipSlot ;
}
2023-01-15 11:39:47 +00:00
if ( StorageWindow ! = null & & StorageWindow . gameObject . activeSelf )
{
StorageWindow . dragWindowUI . CloseWindow ( ) ;
}
if ( Inventory = = null )
{
InventorySystem . instance . player . inventory . AddOwnership ( this , null ) ;
}
else if ( Inventory ! = InventorySystem . instance . player . inventory )
{
Inventory . RemoveOwnership ( this , Inventory ) ;
}
2023-01-25 07:14:16 +00:00
if ( ! fitImageInSlot )
{
2023-02-14 11:55:20 +00:00
ApplyRectTransformResize ( ) ; // TODO: Fix this so it maintains rotation later
transform . localRotation = Quaternion . Euler ( new Vector3 ( 0 , 0 , eulerAngle ) ) ;
rectTransform . pivot = pivot ;
2023-01-25 07:14:16 +00:00
}
}
public void ApplyRectTransformResize ( )
{
ResetTransform ( ) ;
2022-10-29 19:56:00 +00:00
}
public void GrabAndDragItem ( )
{
2022-12-11 03:03:30 +00:00
if ( ! isDragging & & interactable )
2022-10-29 19:56:00 +00:00
{
2023-01-31 02:12:08 +00:00
//Debug.Log("GRAB AND DRAG");
2022-10-29 19:56:00 +00:00
//Debug.Log("OnPointerDown");
//Debug.Log("OnBeginDrag");
2022-11-15 22:58:54 +00:00
2023-02-09 11:53:30 +00:00
RemoveItemFromSlot ( ) ;
2022-12-23 03:50:16 +00:00
2023-02-09 11:53:30 +00:00
isDragging = true ;
InventorySystem . instance . DraggedItem = this ;
PickedUpOnFrame = true ;
}
}
2022-12-23 03:50:16 +00:00
2023-02-09 11:53:30 +00:00
public void RemoveItemFromSlot ( )
{
canvasGroup . alpha = . 7f ;
canvasGroup . blocksRaycasts = false ;
2022-12-11 03:03:30 +00:00
2023-02-09 11:53:30 +00:00
transform . SetParent ( canvas . transform ) ;
2022-12-11 03:03:30 +00:00
2023-02-09 11:53:30 +00:00
rectTransform . anchorMin = new Vector2 ( 0 , 1 ) ;
rectTransform . anchorMax = new Vector2 ( 0 , 1 ) ;
2023-02-14 11:55:20 +00:00
//rectTransform.pivot = new Vector2(0, 1);
rectTransform . pivot = pivot ;
2022-12-23 03:50:16 +00:00
2023-02-09 11:53:30 +00:00
image . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Horizontal , oldImageSizeDelta . x ) ;
image . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , oldImageSizeDelta . y ) ;
rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Horizontal , oldSizeDelta . x ) ;
rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , oldSizeDelta . y ) ;
emptyGridContainer . SetSizeWithCurrentAnchors ( RectTransform . Axis . Horizontal , oldSizeDelta . x ) ;
emptyGridContainer . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , oldSizeDelta . y ) ;
backdropImage . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Horizontal , oldSizeDelta . x ) ;
backdropImage . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , oldSizeDelta . y ) ;
2022-10-29 19:56:00 +00:00
2023-02-09 11:53:30 +00:00
UnsetSlot ( ) ;
HideEmptyGrid ( ) ;
// 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 ) ;
2022-10-29 19:56:00 +00:00
}
2023-02-09 11:53:30 +00:00
ContextWindows . Clear ( ) ;
2022-10-19 13:51:46 +00:00
}
2022-12-11 03:03:30 +00:00
public void UnsetSlot ( )
{
if ( slot ! = null )
{
2023-02-09 11:53:30 +00:00
if ( slot . grid ! = null )
{
slot . grid . RemoveItemFromGrid ( this ) ;
}
2022-12-11 03:03:30 +00:00
previousSlot = slot ;
slot . RemoveDragDropItem ( ) ;
slot = null ;
}
if ( cells ! = null )
{
foreach ( Cell c in cells )
{
if ( c ! = null )
{
c . inUse = false ;
2023-01-27 02:16:31 +00:00
c . item = null ;
2022-12-11 03:03:30 +00:00
//c.overlapped = false;
//s.ShowImage();
if ( c . GetSlot ( ) ! = null & & c . GetSlot ( ) ! = this . slot )
{
c . GetSlot ( ) . UnsetCell ( ) ; // RemoveDragDropItem(true);
}
}
}
cells = null ;
}
}
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
2022-11-15 22:58:54 +00:00
public void FitImageInSlot ( Vector2 size )
2022-10-19 13:51:46 +00:00
{
2023-01-31 02:12:08 +00:00
//Debug.Log("FitImageInSlot: " + image.transform.name);
2022-11-15 22:58:54 +00:00
float ratio = size . y / image . rectTransform . sizeDelta . y ;
rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Horizontal , size . x ) ;
rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , size . y ) ;
2022-12-23 03:50:16 +00:00
image . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Horizontal , size . x ) ;
2022-11-15 22:58:54 +00:00
image . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , size . y ) ;
2022-12-23 03:50:16 +00:00
backdropImage . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Horizontal , size . x ) ;
backdropImage . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , size . y ) ;
emptyGridContainer . SetSizeWithCurrentAnchors ( RectTransform . Axis . Horizontal , size . x ) ;
emptyGridContainer . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , size . y ) ;
/ * image . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , size . y ) ; // = new Vector2(image.rectTransform.sizeDelta.x * ratio, size.y);
Debug . Log ( oldImageSizeDelta + ", " + oldSizeDelta ) ;
this . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , size . y ) ; ; * /
//image.transform.localScale *= ratio;
2022-11-15 22:58:54 +00:00
//this.scale = scale;
//image.transform.localScale *= scale;
2022-10-19 13:51:46 +00:00
}
public void SetSlot ( SlotUI slot )
{
this . slot = slot ;
}
// TODO: Make sure it was dropped somewhere, if not, return it to the original parent
2023-02-04 01:42:50 +00:00
public void ReturnToSlot ( bool forceDrop = false )
2022-10-19 13:51:46 +00:00
{
2023-02-04 01:42:50 +00:00
if ( previousSlot ! = null & & ( ! isDragging | | forceDrop ) )
2022-10-19 13:51:46 +00:00
{
2023-01-31 02:12:08 +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 ( )
{
2023-02-07 13:11:46 +00:00
FindTextMeshProUGUI ( ) ;
2023-01-15 11:39:47 +00:00
InitializeImageSize ( ) ;
2022-11-15 22:58:54 +00:00
// TODO: Possibly have item image scale into slot here.
2022-10-19 13:51:46 +00:00
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-12-11 03:03:30 +00:00
Rarity r = GetRarity ( ) ;
if ( backdropImage = = null )
{
backdropImage = transform . GetComponent < Image > ( ) ;
}
if ( backdropImage = = null )
{
Debug . LogError ( "ItemUI: Item needs at least an Image component." ) ;
}
else if ( r ! = null )
{
backdropImage . color = new Color ( r . color . r , r . color . g , r . color . b , InventorySystem . instance . ItemBackdropTransparency ) ;
}
2022-12-23 03:50:16 +00:00
2023-01-27 02:16:31 +00:00
CreateStorageContainer ( ) ;
2022-12-23 03:50:16 +00:00
//backdropImage.sprite.rect.
/ * overrideSortingCanvas = gameObject . AddComponent < Canvas > ( ) ;
if ( overrideSortingCanvas ! = null )
{
overrideSortingCanvas . overrideSorting = true ;
overrideSortingCanvas . sortingLayerID = 5 ;
//overrideSortingCanvas.sortingLayerName = "ItemUI";
} * /
2023-02-14 11:55:20 +00:00
eulerAngle = 0 ;
transform . localRotation = Quaternion . Euler ( 0 , 0 , eulerAngle ) ;
pivot = rectTransform . pivot = new Vector2 ( 0 , 1 ) ;
2022-10-19 13:51:46 +00:00
}
2023-01-15 11:39:47 +00:00
public void InitializeImageSize ( )
{
oldImageSizeDelta = image . rectTransform . sizeDelta ;
oldSizeDelta = this . rectTransform . sizeDelta ;
if ( oldImageSizeDelta = = Vector2 . zero )
{
oldImageSizeDelta = oldSizeDelta ;
}
if ( oldSizeDelta = = Vector2 . zero )
{
oldSizeDelta = oldImageSizeDelta ;
}
}
2022-10-19 13:51:46 +00:00
public Orientation ImageOrientation ( )
{
return imageOrientation ;
}
public Size SizeAfterOrientation ( )
{
2023-01-31 02:12:08 +00:00
//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
{
2023-01-15 11:39:47 +00:00
Cell previousCell = slot . cell ;
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
{
2023-01-15 11:39:47 +00:00
SlotUI cellSlot = cell . GetSlot ( ) ;
if ( cellSlot ! = null )
{
// && !slot.useImageAsSlotHolder)
cellSlot . HideImage ( ) ;
}
2022-10-19 13:51:46 +00:00
}
}
}
}
2022-12-23 03:50:16 +00:00
public void HideEmptyGrid ( )
{
if ( emptyGridContainer ! = null )
{
emptyGridContainer . gameObject . SetActive ( false ) ;
}
}
public void ShowEmptyGrid ( )
{
if ( emptyGridContainer ! = null )
{
emptyGridContainer . gameObject . SetActive ( true ) ;
}
}
2022-10-19 13:51:46 +00:00
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 ;
2023-01-27 02:16:31 +00:00
cells [ i ] . item = 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 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 ;
}
2023-01-27 02:16:31 +00:00
2022-10-19 13:51:46 +00:00
2022-10-29 19:56:00 +00:00
void FixedUpdate ( )
{
}
2022-10-19 13:51:46 +00:00
// Update is called once per frame
void Update ( )
{
2022-11-15 22:58:54 +00:00
if ( isDragging )
{
// TODO: fix this
rectTransform . position = InventorySystem . instance . MouseInCanvasPosition ;
}
2022-12-11 03:03:30 +00:00
if ( PointerIsHoveredOver & & InventorySystem . instance . PressedEquipItemKey ( false ) )
{
2023-02-09 11:53:30 +00:00
QuickMove ( ) ;
2022-12-11 03:03:30 +00:00
// TODO: see if more needs to be implemented here.
return ;
}
if ( PointerIsHoveredOver & & InventorySystem . instance . PressedDropItemKey ( false ) )
{
2023-01-31 02:12:08 +00:00
//Debug.Log("Should be throwing away...");
2022-12-11 03:03:30 +00:00
DropItemAway ( ) ;
return ;
}
2023-02-04 01:42:50 +00:00
if ( PointerIsHoveredOver & & ! InventorySystem . instance . IsDraggingItem )
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
{
2023-02-13 22:15:10 +00:00
Debug . Log ( "Grab and drag" ) ;
2022-10-29 19:56:00 +00:00
GrabAndDragItem ( ) ;
}
2023-02-13 22:15:10 +00:00
else if ( Input . GetMouseButtonDown ( 2 ) )
{
ShowStorageWindow ( ) ;
}
else if ( Input . GetMouseButtonDown ( 1 ) & & ! InventorySystem . instance . IsDraggingItem )
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-12-11 03:03:30 +00:00
2022-10-29 19:56:00 +00:00
if ( Input . GetMouseButtonUp ( 0 ) )
{
PickedUpOnFrame = false ;
}
if ( isDragging )
{
if ( image ! = null )
{
image . maskable = false ;
}
2023-02-04 01:42:50 +00:00
if ( Count < = 0 )
{
Destroy ( gameObject ) ;
}
2022-10-29 19:56:00 +00:00
if ( InventorySystem . instance . PressedKeyRotation ( true ) )
{
2023-01-31 02:12:08 +00:00
//Debug.Log("Rotate");
2022-11-15 22:58:54 +00:00
RectTransform handler = ( RectTransform ) transform ;
2023-02-14 11:55:20 +00:00
eulerAngle = Mathf . Abs ( Mathf . CeilToInt ( handler . rotation . eulerAngles . z ) ) - 90 ;
2023-01-31 02:12:08 +00:00
//Debug.Log("euler" + eulerAngle);
2022-10-29 19:56:00 +00:00
switch ( eulerAngle )
{
2022-11-15 22:58:54 +00:00
case - 90 :
2023-02-14 11:55:20 +00:00
Debug . Log ( - 90 ) ;
// Item rect transform
pivot = rectTransform . pivot = new Vector2 ( 0 , 0 ) ;
2022-11-15 22:58:54 +00:00
rectTransform . Rotate ( new Vector3 ( 0 , 0 , 1 ) , - 90 ) ;
2023-02-14 11:55:20 +00:00
// title rect transform
if ( titleText ! = null )
{
titleText . rectTransform . pivot = new Vector2 ( 1 , 0 ) ;
titleText . rectTransform . Rotate ( new Vector3 ( 0 , 0 , 1 ) , 90 ) ;
titleText . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , imageSizeOnGrid . Width ) ;
}
// stack count transform
if ( stackText ! = null )
{
stackText . rectTransform . pivot = new Vector2 ( 0 , 0 ) ;
stackText . rectTransform . Rotate ( new Vector3 ( 0 , 0 , 1 ) , 90 ) ;
stackText . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , imageSizeOnGrid . Height ) ;
}
// image Orientation
2022-10-29 19:56:00 +00:00
imageOrientation = ( width > height ) ? Orientation . Portrait : Orientation . Landscape ;
break ;
case 180 :
2023-02-14 11:55:20 +00:00
Debug . Log ( 180 ) ;
// Item rect transform
pivot = rectTransform . pivot = new Vector2 ( 1 , 0 ) ;
2022-11-15 22:58:54 +00:00
handler . Rotate ( new Vector3 ( 0 , 0 , 1 ) , - 90 ) ;
2023-02-14 11:55:20 +00:00
// title rect transform
if ( titleText ! = null )
{
titleText . rectTransform . pivot = new Vector2 ( 0 , 0 ) ;
titleText . rectTransform . Rotate ( new Vector3 ( 0 , 0 , 1 ) , 90 ) ;
titleText . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , imageSizeOnGrid . Height ) ;
}
// stack count transform
if ( stackText ! = null )
{
stackText . rectTransform . pivot = new Vector2 ( 0 , 1 ) ;
stackText . rectTransform . Rotate ( new Vector3 ( 0 , 0 , 1 ) , 90 ) ;
stackText . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , imageSizeOnGrid . Height ) ;
}
// image Orientation
2022-10-29 19:56:00 +00:00
imageOrientation = ( width > height ) ? Orientation . Landscape : Orientation . Portrait ;
break ;
case 90 :
2023-02-14 11:55:20 +00:00
Debug . Log ( 90 ) ;
// Item rect transform
pivot = rectTransform . pivot = new Vector2 ( 1 , 1 ) ;
2022-11-15 22:58:54 +00:00
handler . Rotate ( new Vector3 ( 0 , 0 , 1 ) , - 90 ) ;
2023-02-14 11:55:20 +00:00
// title rect transform
if ( titleText ! = null )
{
titleText . rectTransform . pivot = new Vector2 ( 0 , 1 ) ;
titleText . rectTransform . Rotate ( new Vector3 ( 0 , 0 , 1 ) , 90 ) ;
titleText . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Horizontal , imageSizeOnGrid . Height ) ;
}
// stack count transform
if ( stackText ! = null )
{
stackText . rectTransform . pivot = new Vector2 ( 1 , 1 ) ;
stackText . rectTransform . Rotate ( new Vector3 ( 0 , 0 , 1 ) , 90 ) ;
stackText . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , imageSizeOnGrid . Height ) ;
}
// image Orientation
2022-10-29 19:56:00 +00:00
imageOrientation = ( width > height ) ? Orientation . Portrait : Orientation . Landscape ;
break ;
default :
2023-02-14 11:55:20 +00:00
Debug . Log ( 0 ) ;
//handler.localPosition = new Vector3(0, 0, 0);
// Item rect transform
pivot = handler . pivot = new Vector2 ( 0 , 1 ) ;
2022-11-15 22:58:54 +00:00
handler . Rotate ( new Vector3 ( 0 , 0 , 1 ) , - 90 ) ;
2023-02-14 11:55:20 +00:00
// title rect transform
if ( titleText ! = null )
{
titleText . rectTransform . pivot = new Vector2 ( 1 , 1 ) ;
titleText . rectTransform . Rotate ( new Vector3 ( 0 , 0 , 1 ) , 90 ) ;
titleText . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Horizontal , imageSizeOnGrid . Width ) ;
}
// stack count transform
if ( stackText ! = null )
{
stackText . rectTransform . pivot = new Vector2 ( 1 , 0 ) ;
stackText . rectTransform . Rotate ( new Vector3 ( 0 , 0 , 1 ) , 90 ) ;
stackText . rectTransform . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , imageSizeOnGrid . Height ) ;
}
// image Orientation
2022-10-29 19:56:00 +00:00
imageOrientation = ( width > height ) ? Orientation . Landscape : Orientation . Portrait ;
break ;
}
}
2022-12-11 03:03:30 +00:00
}
2023-01-15 11:39:47 +00:00
}
2023-01-27 02:16:31 +00:00
public void ShowStorageWindow ( )
2023-01-15 11:39:47 +00:00
{
2023-01-27 02:16:31 +00:00
if ( IsStorageContainer ( ) & & ( StorageWindow = = null | | ! StorageWindow . gameObject . activeSelf ) )
2023-01-15 11:39:47 +00:00
{
2023-01-31 02:12:08 +00:00
//Debug.Log("Opening Window");
2023-01-15 11:39:47 +00:00
StorageWindow = InventorySystem . instance . OpenWindow ( ) . GetComponent < StorageWindowUI > ( ) ;
if ( StorageWindow ! = null & & containerUIPrefab ! = null & & ! Equipped )
{
2023-01-31 02:12:08 +00:00
//Debug.Log("Opening Window");
2023-01-27 02:16:31 +00:00
2023-01-15 11:39:47 +00:00
if ( container . transform . parent ! = StorageWindow . StorageContainer . transform )
container . transform . SetParent ( StorageWindow . StorageContainer . transform , false ) ;
container . SetActive ( true ) ;
StorageWindow . itemUI = this ;
StorageWindow . gameObject . SetActive ( true ) ;
}
}
2022-12-11 03:03:30 +00:00
}
2022-11-15 22:58:54 +00:00
2023-01-27 02:16:31 +00:00
public void CreateStorageContainer ( )
{
if ( container = = null & & IsStorageContainer ( ) )
{
2023-01-31 02:12:08 +00:00
//Debug.Log("Item:CreateStorageContainer:" + gameObject.name);
2023-01-27 02:16:31 +00:00
container = Instantiate ( containerUIPrefab , this . transform , false ) ; // StorageWindow.StorageContainer.transform, false);
//itemUI.container.name = itemUI.container.name + " " + inCount;
container . GetComponentInChildren < GridUI > ( ) . SetAssociatedItemUI ( this ) ;
2023-02-04 01:42:50 +00:00
//container.SetActive(true); // TODO: modify this
StartCoroutine ( coroutineA ( ) ) ;
2023-01-27 02:16:31 +00:00
}
}
2023-02-04 01:42:50 +00:00
IEnumerator coroutineA ( )
{
// wait for 1 second
Debug . Log ( "coroutineA created" ) ;
yield return new WaitForEndOfFrame ( ) ;
container . SetActive ( false ) ;
Debug . Log ( "coroutineA running again" ) ;
}
2022-12-11 03:03:30 +00:00
public void DropItemAway ( )
{
if ( pickUp = = null )
{
// TODO: Create a new pickup for items without a gameobject attached
Debug . LogError ( "No in-world gameobject previously attached with itemUI" ) ;
2022-10-29 19:56:00 +00:00
}
2022-12-11 03:03:30 +00:00
else
{
InventorySystem . instance . player . DetachItemGameObject ( pickUp . ItemGameObject , this ) ;
}
isDragging = false ;
2022-12-23 03:50:16 +00:00
SetOverrideSortingCanvasEnabled ( false ) ;
2022-12-11 03:03:30 +00:00
InventorySystem . instance . DraggedItem = null ;
UnsetSlot ( ) ;
}
2022-12-23 03:50:16 +00:00
public void SetOverrideSortingCanvasEnabled ( bool value )
{
/ * if ( overrideSortingCanvas ! = null )
{
overrideSortingCanvas . enabled = value ;
} * /
}
2022-12-11 03:03:30 +00:00
public void SetInteractable ( bool value )
{
interactable = value ;
2022-10-29 19:56:00 +00:00
}
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
}
2023-01-27 02:16:31 +00:00
public bool IsStorageContainer ( )
{
return containerUIPrefab ! = null & & containerUIPrefab . GetComponent < GridUI > ( ) ! = null ;
}
2022-10-19 13:51:46 +00:00
}
}