using UnityEngine;
using UnityEngine.EventSystems;

public class Draggable : MonoBehaviour, IPointerDownHandler, IEventSystemHandler, IDragHandler
{
	private RectTransform rectTransform;

	private CanvasGroup canvasGroup;

	private Vector2 offset;

	private void Awake()
	{
		rectTransform = GetComponent<RectTransform>();
		canvasGroup = GetComponent<CanvasGroup>();
	}

	public void OnPointerDown(PointerEventData eventData)
	{
		RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out offset);
	}

	public void OnDrag(PointerEventData eventData)
	{
		if (canvasGroup != null)
		{
			canvasGroup.blocksRaycasts = false;
		}
		if (RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out var localPoint))
		{
			rectTransform.anchoredPosition += localPoint - offset;
		}
	}

	public void OnEndDrag(PointerEventData eventData)
	{
		if (canvasGroup != null)
		{
			canvasGroup.blocksRaycasts = true;
		}
	}
}
