//--------------------------------------------------------
|
// [Author]: 第二世界
|
// [ Date ]: Saturday, September 02, 2017
|
//--------------------------------------------------------
|
using UnityEngine;
|
using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine.UI;
|
|
using UnityEngine.EventSystems;
|
using UnityEngine.Events;
|
|
namespace Snxxz.UI
|
{
|
|
public class SkillButtonInterface : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
|
{
|
|
public virtual void OnPointerDown(PointerEventData eventData)
|
{
|
}
|
|
public virtual void OnPointerUp(PointerEventData eventData)
|
{
|
}
|
|
}
|
|
public class SkillButton : SkillButtonInterface, IPointerExitHandler
|
{
|
[SerializeField] SkillTurnplate m_SkillTurnplate;
|
[SerializeField] Image m_Icon;
|
public Image icon { get { return m_Icon; } }
|
[SerializeField] Transform m_Locked;
|
public Transform locked
|
{
|
get
|
{
|
return m_Locked;
|
}
|
}
|
|
[SerializeField] Text m_CoolDown;
|
[SerializeField] Image m_CoolDownMask;
|
[SerializeField] ScaleTween m_ScaleTween;
|
|
[Header("遮罩是否反向")]
|
public bool Isreverse = false;
|
UIEvent skillEvent1 = new UIEvent();
|
UIEvent skillEvent2 = new UIEvent();
|
|
bool isExit = false;
|
|
float m_TotalCoolDown = 0f;
|
public float totalCoolDown
|
{
|
get
|
{
|
return m_TotalCoolDown;
|
}
|
set
|
{
|
m_TotalCoolDown = value;
|
}
|
}
|
|
float m_CoolDownCompleteTime = 0f;
|
public float coolDownCompleteTime
|
{
|
get
|
{
|
return m_CoolDownCompleteTime;
|
}
|
set
|
{
|
m_CoolDownCompleteTime = value;
|
}
|
}
|
|
float timer = 0f;
|
PlayerSkillData m_SkillData = null;
|
Skill skillBuf = null;
|
float fillAmountRevise = 1f;
|
|
public void SetSkillData(PlayerSkillData _skillData)
|
{
|
m_SkillData = _skillData;
|
if (m_SkillData == null)
|
{
|
locked.gameObject.SetActive(true);
|
m_Icon.gameObject.SetActive(false);
|
}
|
else
|
{
|
if (PreFightMission.Instance.IsFinished()
|
|| PreFightMission.Instance.s_MissionDict.ContainsKey(1002))
|
{
|
locked.gameObject.SetActive(false);
|
m_Icon.gameObject.SetActive(true);
|
}
|
var skillConfig = m_SkillData.skillCfg;
|
m_Icon.SetSprite(skillConfig.IconName);
|
|
if (skillBuf != null)
|
{
|
skillBuf.RefreshCD -= OnSkillCDRefresh;
|
}
|
|
if (PlayerDatas.Instance.hero != null)
|
{
|
skillBuf = PlayerDatas.Instance.hero.SkillMgr.Get(m_SkillData.id);
|
OnSkillCDRefresh(skillBuf.cd, skillBuf.skillInfo.config.CoolDownTime * Constants.F_GAMMA);
|
skillBuf.RefreshCD += OnSkillCDRefresh;
|
}
|
}
|
|
}
|
|
public void Dispose()
|
{
|
if (skillBuf != null)
|
{
|
skillBuf.RefreshCD -= OnSkillCDRefresh;
|
}
|
}
|
|
public void AddDownListener(UnityAction _action)
|
{
|
skillEvent1.AddListener(_action);
|
}
|
|
public void AddUpListener(UnityAction _action)
|
{
|
skillEvent2.AddListener(_action);
|
}
|
|
public void RemoveAllListeners()
|
{
|
skillEvent1.RemoveAllListeners();
|
skillEvent2.RemoveAllListeners();
|
}
|
|
/// <summary>
|
/// 剩余CD和总CD都是毫秒
|
/// </summary>
|
/// <param name="_cd"></param>
|
/// <param name="_totalCD"></param>
|
private void OnSkillCDRefresh(float _cd, float _totalCD)
|
{
|
totalCoolDown = _totalCD;
|
coolDownCompleteTime = Time.time + _cd;
|
timer = 0f;
|
}
|
|
private void LateUpdate()
|
{
|
if (m_CoolDownCompleteTime > Time.time)
|
{
|
var cd = m_CoolDownCompleteTime - Time.time;
|
if (!m_CoolDown.gameObject.activeInHierarchy)
|
{
|
m_CoolDown.gameObject.SetActive(true);
|
}
|
|
if (m_CoolDownMask != null && !m_CoolDownMask.gameObject.activeInHierarchy)
|
{
|
m_CoolDownMask.gameObject.SetActive(true);
|
}
|
|
if (m_CoolDownMask != null)
|
{
|
if (Isreverse)
|
{
|
float flo = cd / totalCoolDown;
|
m_CoolDownMask.fillAmount = 1.0f - flo;
|
}
|
else
|
{
|
m_CoolDownMask.fillAmount = cd / totalCoolDown;
|
}
|
}
|
|
timer -= Time.deltaTime;
|
if (timer < 0f)
|
{
|
m_CoolDown.text = cd.ToString("f0");
|
timer += 1f;
|
}
|
}
|
else
|
{
|
if (m_CoolDown.gameObject.activeInHierarchy)
|
{
|
m_CoolDown.gameObject.SetActive(false);
|
}
|
|
if (m_CoolDownMask != null)
|
{
|
if (Isreverse)
|
{
|
m_CoolDownMask.fillAmount = 1;
|
}
|
else
|
{
|
if (m_CoolDownMask.gameObject.activeInHierarchy)
|
{
|
m_CoolDownMask.gameObject.SetActive(false);
|
}
|
}
|
}
|
|
}
|
|
}
|
|
public override void OnPointerDown(PointerEventData eventData)
|
{
|
base.OnPointerDown(eventData);
|
|
PassEvent(eventData, ExecuteEvents.pointerDownHandler);
|
|
if (m_SkillData == null)
|
{
|
return;
|
}
|
if (m_ScaleTween != null)
|
{
|
m_ScaleTween.Play();
|
}
|
|
skillEvent1.Invoke();
|
isExit = false;
|
|
}
|
|
public override void OnPointerUp(PointerEventData eventData)
|
{
|
base.OnPointerUp(eventData);
|
|
PassEvent(eventData, ExecuteEvents.pointerUpHandler);
|
|
if (m_SkillData == null)
|
{
|
return;
|
}
|
if (m_ScaleTween != null)
|
{
|
m_ScaleTween.Play(true);
|
}
|
|
if (isExit == false)
|
{
|
skillEvent2.Invoke();
|
}
|
}
|
|
public void OnPointerExit(PointerEventData eventData)
|
{
|
isExit = true;
|
}
|
|
public void PassEvent<T>(PointerEventData data, ExecuteEvents.EventFunction<T> function) where T : IEventSystemHandler
|
{
|
List<RaycastResult> results = new List<RaycastResult>();
|
EventSystem.current.RaycastAll(data, results);
|
GameObject current = data.pointerCurrentRaycast.gameObject;
|
if (current != null && current == m_SkillTurnplate.gameObject)
|
{
|
ExecuteEvents.Execute(current, data, function);
|
}
|
else
|
{
|
for (int i = 0; i < results.Count; i++)
|
{
|
if (current != results[i].gameObject)
|
{
|
if (ExecuteEvents.Execute(results[i].gameObject, data, function))
|
{
|
break;
|
}
|
}
|
}
|
}
|
|
}
|
}
|
|
}
|