2022-11-06 21:34:17 +01:00
using System ;
using UnityEditor ;
using UnityEngine ;
using UnityEngine.UI ;
public class DraggedSlotController : MonoBehaviour
{
public static DraggedSlotController Instance { get ; private set ; }
[Header("Dragged Image")]
[SerializeField] protected Image DraggedSlotImageTemplate ;
[SerializeField] protected Image DraggedSlotImage ;
[Space]
[Header("Dragged Slot")]
2022-11-19 17:02:31 +01:00
[SerializeField] protected ItemSlot _draggedSlot ;
public ItemSlot DraggedSlot // in Drag & Drop functionality for Warehouse set of panels I will temporary delegate dragged item to outside static object instance in order to remember it during dragging object from one slot to anothe one
2022-11-06 21:34:17 +01:00
{
get { return _draggedSlot ; }
set
{
_draggedSlot = value ;
}
}
public void Awake ( )
{
if ( Instance = = null )
{
Instance = this ;
}
else
{
Destroy ( gameObject ) ;
}
}
public bool IsDragged ( )
{
return DraggedSlot ! = null ;
}
public void CreateDraggedSlot ( ItemSlot itemSlot , Vector3 beginingPosition )
{
DraggedSlot = itemSlot ;
MakeDraggableItem ( beginingPosition ) ;
}
2022-11-24 03:03:30 +01:00
private void MakeDraggableItem ( Vector3 beginingPosition )
2022-11-06 21:34:17 +01:00
{
if ( DraggedSlotImage )
{
DraggedSlotImage . enabled = true ;
return ;
}
GameObject globalGUI = GameObject . FindGameObjectWithTag ( "GUI" ) ;
if ( globalGUI )
{
DraggedSlotImage = MonoBehaviour . Instantiate ( DraggedSlotImageTemplate , DraggedSlotImageTemplate . transform . position , Quaternion . identity , globalGUI . transform ) ;
DraggedSlotImage . transform . localPosition = beginingPosition ;
DraggedSlotImage . sprite = DraggedSlot . Item . Image ;
DraggedSlotImage . transform . position = Input . mousePosition ;
DraggedSlotImage . enabled = true ;
}
else
{
Debug . Log ( "Can't find global GUI object!!!" ) ;
}
}
public void UpdatePosition ( Vector3 newSlotPosition )
{
if ( DraggedSlotImage ! = null )
DraggedSlotImage . transform . position = newSlotPosition ;
}
public bool RemoveDraggedSlot ( )
{
try
{
DestroyImmediate ( DraggedSlotImage . gameObject , true ) ;
DraggedSlotImage = null ;
DraggedSlot = null ;
} catch ( Exception e )
{
Debug . LogError ( e . Message ) ;
return false ;
}
return true ;
}
2022-11-19 17:02:31 +01:00
public void UpdateItem ( Item item )
{
DraggedSlot . Item = ( EquippableItem ) item ;
}
2022-11-06 21:34:17 +01:00
}