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



[RequireComponent(typeof(Image))]
public class GuideBase : MonoBehaviour
{
    protected Material material; // 材质

    protected Vector3 center;    // 镂空区域的中心

    protected RectTransform target; // 要显示的目标

    protected Vector3[] targetCorners = new Vector3[4]; // 要引导的目标的边界

    public Vector3 Center 
    {
        get
        {
            if (material == null) { return Vector3.zero; }

            return material.GetVector("_Center");
        }
    }



    // 引导
    public virtual void Guide(Canvas canvas, RectTransform target)
    {
        // 初始化材质
        material = transform.GetComponent<Image>().material;

        this.target = target;

        if (target != null)
        {
            // 获取中心点 
            target.GetWorldCorners(targetCorners);

            // 把世界坐标 转成屏幕坐标
            for (int i = 0; i < targetCorners.Length; i++)
            {
                targetCorners[i] = WorldToScreenPoint(canvas, targetCorners[i]);
            }
            // 计算中心点
            center.x = targetCorners[0].x + (targetCorners[3].x - targetCorners[0].x) / 2;
            center.y = targetCorners[0].y + (targetCorners[1].y - targetCorners[0].y) / 2;

            material.SetVector("_Center", center);
        }
        else 
        {
            center = Vector3.zero;
            targetCorners[0] = new Vector3(-2000,-2000,0);
            targetCorners[1] = new Vector3(-2000, 2000, 0);
            targetCorners[2] = new Vector3(2000, 2000, 0);
            targetCorners[3] = new Vector3(2000, -2000, 0);
        }
         
    }


    protected Vector2 WorldToScreenPoint(Canvas canvas, Vector3 world)
    {
        // 把世界坐标转成 屏幕坐标
        Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(canvas.worldCamera, world);
        Vector2 localPoint;
        // 把屏幕坐标 转成 局部坐标
        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.GetComponent<RectTransform>(), screenPoint, canvas.worldCamera, out localPoint);
        return localPoint;
    }

}
