Scriptum/Assets/Scripts/REFACTORING/Application/Shared/Manager/UI/Panel/Draggable/DraggedSlotController.cs

101 lines
2.6 KiB
C#
Raw Normal View History

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")]
[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);
}
private void MakeDraggableItem( Vector3 beginingPosition)
{
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;
}
public void UpdateItem(Item item)
{
DraggedSlot.Item = (EquippableItem)item;
}
2022-11-06 21:34:17 +01:00
}