using ET;
using FairyGUI;
using System;
using System.Collections.Generic;
using UI.ActivityWanShiLi;
using UnityEngine;
namespace GFGGame
{
public class ActivityWanShiLiView : BaseWindow
{
private UI_ActivityWanShiLiUI _ui;
private int _minProgressOffset = 11;
private int _maxProgressOffset = -11;
private ActivityInfo _activityInfo;
private ActivityRecharge2Cfg[] _rechargeCfgs;
public override void Dispose()
{
if (_ui != null)
{
_ui.Dispose();
_ui = null;
}
base.Dispose();
}
protected override void OnInit()
{
base.OnInit();
packageName = UI_ActivityWanShiLiUI.PACKAGE_NAME;
_ui = UI_ActivityWanShiLiUI.Create();
this.viewCom = _ui.target;
this.viewCom.Center();
this.modal = true;
viewAnimationType = EnumViewAnimationType.ZOOM_CENTER;
_rechargeCfgs = ActivityRecharge2CfgArray.Instance.dataArray;
InitProgressValueList();
InitReward();
_ui.m_btnBack.onClick.Add(Hide);
}
protected override void AddEventListener()
{
base.AddEventListener();
EventAgent.AddEventListener(ConstMessage.ACTIVITY_REWARD_ADD, UpdateView);
EventAgent.AddEventListener(ConstMessage.ACTIVITY_COUNT_VALUE_CHANGE, UpdateView);
}
protected override void RemoveEventListener()
{
base.RemoveEventListener();
EventAgent.RemoveEventListener(ConstMessage.ACTIVITY_REWARD_ADD, UpdateView);
EventAgent.RemoveEventListener(ConstMessage.ACTIVITY_COUNT_VALUE_CHANGE, UpdateView);
}
protected override void OnShown()
{
base.OnShown();
UpdateView();
UpdateTime(null);
Timers.inst.Add(1, 0, UpdateTime);
}
protected override void OnHide()
{
base.OnHide();
Timers.inst.Remove(UpdateTime);
}
private int[] _progressValuePart;
private int _partNum = 4;
///
/// 计算奖励的分段列表
///
private void InitProgressValueList()
{
_progressValuePart = new int[_partNum];
_ui.m_progress.target.max = _rechargeCfgs[_rechargeCfgs.Length - 1].value;
int max = (int)_ui.m_progress.target.max;
int average = (int)Mathf.Ceil((float)(_ui.m_progress.target.max / (_partNum - 1)));
_progressValuePart[0] = 0;
_progressValuePart[_partNum - 1] = max;
for (int i = 1; i < _partNum - 1; i++)
{
_progressValuePart[i] = i * average;
}
}
///
/// 初始化奖励的UI
///
private void InitReward()
{
for (int i = 0; i < _partNum; i++)
{
GObject gift = _ui.target.GetChild("DBGiftItem" + i);
// 大奖icon
UI_item item = UI_item.Proxy(gift);
item.m_icon.url = ResPathUtil.GetIconPath(ItemCfgArray.Instance.GetCfg(_rechargeCfgs[i].bonusArr[0][0]));
gift.data = _rechargeCfgs[i].bonusArr[0][0];
UI_item.ProxyEnd();
// 进度条分段值Text
GObject progressItem = _ui.m_progress.target.GetChild("comProBonus" + i);
UI_ComProBonus2 bonus = UI_ComProBonus2.Proxy(progressItem);
bonus.m_count.text = _rechargeCfgs[i].value.ToString();
UI_ComProBonus2.ProxyEnd();
}
}
private void UpdateProgress()
{
int max = (int)_ui.m_progress.target.max;
long count = _activityInfo.CountValue;
int index = 1;
// 确定count的区间
if (count == 0 || count >= max)
{
_ui.m_progress.target.value = Mathf.Min(count, max);
index = (count == 0 ? 0 : _partNum - 1);
}
else
{
for (; index < _progressValuePart.Length; index++)
{
if (count < _progressValuePart[index])
{
--index;
break;
}
}
// 计算进度条最小取值和最大取值,避免因为图标遮挡造成视觉错误
int minProgressValue = _progressValuePart[index] + _minProgressOffset;
int maxProgressValue = _progressValuePart[index + 1] + _maxProgressOffset;
if (count == _progressValuePart[index] || count == _progressValuePart[index + 1])
{
_ui.m_progress.target.value = count;
}
else
{
_ui.m_progress.target.value = Mathf.Clamp(count, minProgressValue, maxProgressValue);
}
}
// 检测玩家是否领取奖励
UpdateRewardState(index);
}
private void UpdateTime(object param)
{
long curTime = TimeHelper.ServerNow();
long endTime = _activityInfo.EndTime;
_ui.m_txtTime.text = TimeUtil.FormattingTimeTo_DDHHmm(endTime - curTime);
}
private void UpdateView()
{
_activityInfo = ActivityGlobalDataManager.Instance.GetActivityInfoOneByType(ActivityType.WanShiLi);
UpdateProgress();
}
private struct GetState
{
public int rewardID;
public bool canGet;
}
///
/// 更新奖励状态
///
/// 玩家可领取的最大奖励index
private void UpdateRewardState(int index)
{
for (int i = 0; i < _partNum; i++)
{
GObject gift = _ui.target.GetChild("DBGiftItem" + i);
bool arrive = (i <= index);
GObject progressItem = _ui.m_progress.target.GetChild("comProBonus" + i);
UI_ComProBonus2 bonus = UI_ComProBonus2.Proxy(progressItem);
// 下方礼物icon加上data
bonus.target.data = new GetState
{
rewardID = _rechargeCfgs[i].id,
canGet = arrive
};
UI_item item = UI_item.Proxy(gift);
item.m_c1.SetSelectedIndex(0);
// 大奖icon加上data
item.target.data = new GetState
{
rewardID = _rechargeCfgs[i].id,
canGet = arrive
};
// 玩家已经达到的奖励
if (arrive)
{
// 已领
if (_activityInfo.GetRewards.IndexOf(_rechargeCfgs[i].id) >= 0)
{
item.m_c1.SetSelectedIndex(1);
bonus.m_state.SetSelectedIndex(2);
bonus.target.touchable = false;
item.target.touchable = false;
// 隐藏红点
RedDotController.Instance.SetComRedDot(bonus.m_loaIcon, false);
RedDotController.Instance.SetComRedDot(item.target, false);
}
// 未领
else
{
bonus.m_state.SetSelectedIndex(0);
bonus.target.touchable = true;
item.target.touchable = true;
// 显示红点
RedDotController.Instance.SetComRedDot(bonus.m_loaIcon, true);
RedDotController.Instance.SetComRedDot(item.target, true);
}
}
else
{
bonus.target.touchable = true;
item.target.touchable = true;
bonus.m_state.SetSelectedIndex(1);
// 隐藏红点
RedDotController.Instance.SetComRedDot(bonus.m_loaIcon, false);
RedDotController.Instance.SetComRedDot(item.target, false);
}
progressItem.onClick.Set(OnGiftClick);
item.target.onClick.Set(OnGiftClick);
UI_ComProBonus2.ProxyEnd();
UI_item.ProxyEnd();
}
}
///
/// 点击最下方的礼物icon
///
///
private void OnGiftClick(EventContext eventContext)
{
GObject gObject = eventContext.sender as GObject;
GetState getState = (GetState)(gObject.data);
if (getState.canGet)
{
ActivityGlobalSProxy.ReqGetActivityBonus(_activityInfo.ActivityId, getState.rewardID).Coroutine();
}
else
{
// 显示奖励详细列表
ViewManager.Show(getState.rewardID, ViewManager.GetGoBackDatas(typeof(ActivityWanShiLiView).FullName));
}
}
}
}