using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[RequireComponent(typeof(CanvasRenderer))]
[RequireComponent(typeof(RectTransform))]
public class UGuiArea: UnityEngine.UI.Graphic,IBeginDragHandler, IEndDragHandler, IDragHandler, IScrollHandler
{
    ScrollRect _scrollRect;
    protected override void Start()
    {
        Transform curr = this.gameObject.transform;
        while (curr != null) {
            ScrollRect scrollRect = curr.GetComponent<ScrollRect>();
            if(scrollRect != null)
            {
                _scrollRect = scrollRect;
                break;
            }
            curr = curr.parent;
        }
        base.Start();
    }
    public override bool Raycast(Vector2 sp, Camera eventCamera) {
        return RectTransformUtility.RectangleContainsScreenPoint(GetComponent<RectTransform>(),sp,eventCamera);
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        if (_scrollRect != null) { 
            _scrollRect.OnBeginDrag(eventData);
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        if (_scrollRect != null) { 
            _scrollRect.OnEndDrag(eventData);
        }
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (_scrollRect != null) { 
            _scrollRect.OnDrag(eventData);
        }
    }

    public void OnScroll(PointerEventData eventData)
    {
        if (_scrollRect != null) { 
            _scrollRect.OnScroll(eventData);
        }
    }

    public override Material material { 
        get { return null; }
    }
}
