NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/UI_NoticeController.cs

474 lines
12 KiB
C#

using cfg.ActorCfg;
using cfg.UIInterfaceBannerGoToCfg;
using Cysharp.Threading.Tasks;
using Framework;
using Gameplay;
using Gameplay.Net;
using PhxhSDK;
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 公告UI
/// </summary>
public class UI_NoticeController : UIWindow
{
/// <summary>
/// 打开公告界面
/// </summary>
/// <returns></returns>
public static async UniTask<UIWindow> Open(E_NoticeType noticeType = E_NoticeType.Activity)
{
CommonUtils.CaptureScreenshot(); // 截取当前屏幕截图
return await UIManager.Instance.OpenWindow(UINameConst.UI_Notice, noticeType);
}
#region Temp 临时
/// <summary>
/// 公告类型
/// </summary>
public enum E_NoticeType
{
None = 0,
/// <summary>
/// 活动
/// </summary>
Activity = 1,
/// <summary>
/// 系统
/// </summary>
System = 2,
}
#endregion
#region 类
/// <summary>
/// 公告类型页签
/// </summary>
private class NoticeTypeTab : UIGameObjectWrapper
{
/// <summary>
/// 选中按钮
/// </summary>
private Button buttonChoose;
/// <summary>
/// 未选中按钮
/// </summary>
private Button buttonNormal;
/// <summary>
/// 公告类型
/// </summary>
private readonly E_NoticeType noticeType;
/// <summary>
/// 点击回调
/// </summary>
private readonly Action<E_NoticeType> clickAction;
/// <summary>
/// 是否选中
/// </summary>
public bool IsSelect
{
set
{
buttonChoose.gameObject.IsScaleShow(value);
buttonNormal.gameObject.IsScaleShow(!value);
}
}
/// <summary>
/// 公告类型
/// </summary>
public E_NoticeType NoticeType
{
get { return noticeType; }
}
public NoticeTypeTab(GameObject root, E_NoticeType noticeType, Action<E_NoticeType> click) : base(root, true)
{
this.noticeType = noticeType;
clickAction = click;
buttonChoose = GetComponent<Button>("ButtonChoose");
buttonNormal = GetComponent<Button>("ButtonNormal");
buttonChoose.onClick.AddListener(OnClick);
buttonNormal.onClick.AddListener(OnClick);
}
private void OnClick()
{
clickAction?.Invoke(noticeType);
}
}
/// <summary>
/// 公告页签
/// </summary>
private class NoticeTab : UIGameObjectWrapper
{
#region UI
/// <summary>
/// 选中状态按钮
/// </summary>
private Button buttonChoose;
/// <summary>
/// 非选中状态按钮
/// </summary>
private Button buttonNormal;
/// <summary>
/// 选中状态标题
/// </summary>
private TMP_Text textSelectTitle;
/// <summary>
/// 选中状态日
/// </summary>
private TMP_Text textSelectDay;
/// <summary>
/// 选中状态月
/// </summary>
private TMP_Text textSelectWeek;
/// <summary>
/// 选中状态星期
/// </summary>
private TMP_Text textSelectMonth;
/// <summary>
/// 未选中状态标题
/// </summary>
private TMP_Text textTitle;
/// <summary>
/// 未选中状态日
/// </summary>
private TMP_Text textDay;
/// <summary>
/// 未选中状态月
/// </summary>
private TMP_Text textWeek;
/// <summary>
/// 未选中状态星期
/// </summary>
private TMP_Text textMonth;
#endregion
/// <summary>
/// 选中回调
/// </summary>
private readonly Action<NoticeTab> selectAction;
private DataUIBanner uiBanner;
private NoticeInfo noticeInfo;
/// <summary>
/// 是否选中
/// </summary>
public bool IsSelect
{
set
{
buttonChoose.gameObject.IsScaleShow(value);
buttonNormal.gameObject.IsScaleShow(!value);
}
}
public int ID
{
get { return uiBanner.Index; }
}
public DataUIBanner UIBanner
{
get { return uiBanner; }
}
public NoticeInfo NoticeInfo
{
get { return noticeInfo; }
}
public NoticeTab(GameObject root, Action<NoticeTab> select) : base(root)
{
selectAction = select;
buttonChoose = GetComponent<Button>("ButtonChoose");
buttonNormal = GetComponent<Button>("ButtonNormal");
textSelectTitle = GetComponent<TMP_Text>("ButtonChoose/TextTitle");
textSelectDay = GetComponent<TMP_Text>("ButtonChoose/TextDay");
textSelectWeek = GetComponent<TMP_Text>("ButtonChoose/TextWeek");
textSelectMonth = GetComponent<TMP_Text>("ButtonChoose/TextMonth");
textTitle = GetComponent<TMP_Text>("ButtonNormal/TextTitle");
textDay = GetComponent<TMP_Text>("ButtonNormal/TextDay");
textWeek = GetComponent<TMP_Text>("ButtonNormal/TextWeek");
textMonth = GetComponent<TMP_Text>("ButtonNormal/TextMonth");
BindButton(buttonChoose, OnClick);
BindButton(buttonNormal, OnClick);
}
public void SetInfo(DataUIBanner dataUIBanner)
{
uiBanner = dataUIBanner;
IsScaleShow = true;
}
public void SetInfo(NoticeInfo noticeInfo)
{
this.noticeInfo = noticeInfo;
IsScaleShow = true;
}
private void OnClick()
{
selectAction?.Invoke(this);
}
}
/// <summary>
/// 公告内容
/// </summary>
private class NoticeContent : UIGameObjectWrapper
{
/// <summary>
/// 活动名
/// </summary>
private TMP_Text textActivityName;
/// <summary>
/// 内容
/// </summary>
private TMP_Text textContent;
/// <summary>
/// 横幅
/// </summary>
private Image imageBanner;
public NoticeContent(GameObject root) : base(root)
{
textActivityName = GetComponent<TMP_Text>("ImageTitle/TextActivityName");
textContent = GetComponent<TMP_Text>("TextContent");
imageBanner = GetComponent<Image>("ImageBanner");
}
public async void SetInfo(DataUIBanner dataUIBanner)
{
imageBanner.sprite = await AssetManager.Instance.LoadAssetAsync<Sprite>(dataUIBanner.BannerImgPath);
IsScaleShow = true;
}
public void SetInfo(NoticeInfo noticeInfo)
{
textActivityName.text = noticeInfo.Title;
textContent.text = noticeInfo.Content;
IsScaleShow = true;
}
}
#endregion
/// <summary>
/// 关闭按钮
/// </summary>
private Button buttonClose;
/// <summary>
/// 页签循环列表
/// </summary>
private RecycleView tabRecycleView;
/// <summary>
/// 高斯模糊遮罩
/// </summary>
private RawImage rawImageGaussianBlurMask;
/// <summary>
/// 活动公告数据
/// </summary>
private readonly List<DataUIBanner> listUIBanner = TableManager.Instance.Tables.bannerCfg.DataList;
/// <summary>
/// 系统公告数据
/// </summary>
private readonly List<NoticeInfo> listNoticeInfo = AppConfig.Instance.NoticeInfos;
/// <summary>
/// 公告类型页签字典
/// </summary>
private readonly Dictionary<E_NoticeType, NoticeTypeTab> dicNoticeTypeTab = new();
/// <summary>
/// 页签字典
/// </summary>
private readonly Dictionary<int, NoticeTab> dicNoticeTap = new();
/// <summary>
/// 页签内容
/// </summary>
private NoticeContent noticeContent;
/// <summary>
/// 当前选中的公告类型
/// </summary>
private E_NoticeType curSelectNoticeType = E_NoticeType.None;
/// <summary>
/// 当前选择的页签
/// </summary>
private NoticeTab curSelectTab;
public override void OnInit()
{
buttonClose = GetComponent<Button>("Image_NoticeBG/Button_Close");
tabRecycleView = GetComponent<RecycleView>("Image_NoticeBG/Tabs");
rawImageGaussianBlurMask = GetComponent<RawImage>("GaussianBlurMask");
dicNoticeTypeTab.Add(E_NoticeType.Activity, new NoticeTypeTab(FindObj("Image_NoticeBG/Notices/ActivityNotice"), E_NoticeType.Activity, OnNoticeTypeTab));
dicNoticeTypeTab.Add(E_NoticeType.System, new NoticeTypeTab(FindObj("Image_NoticeBG/Notices/SystemNotice"), E_NoticeType.System, OnNoticeTypeTab));
noticeContent = new(FindObj("Image_NoticeBG/ContentRoot/Viewport/Content"));
rawImageGaussianBlurMask.texture = CommonUtils.screenTexture;
tabRecycleView.Init(OnRefreshTab);
BindButton(buttonClose, OnClose);
}
protected override void OnShowWindow(object data = null)
{
if (data is E_NoticeType noticeType)
OnNoticeTypeTab(noticeType);
else
OnNoticeTypeTab(E_NoticeType.Activity); // 默认选中活动公告
}
/// <summary>
/// 获取要显示的页签数量
/// </summary>
/// <returns></returns>
private int GetShowTabCount()
{
return curSelectNoticeType switch
{
E_NoticeType.Activity => listUIBanner.Count,
E_NoticeType.System => listNoticeInfo.Count,
_ => 0,
};
}
#region 回调方法
/// <summary>
/// 刷新页签
/// </summary>
/// <param name="cell"></param>
/// <param name="index"></param>
private void OnRefreshTab(GameObject cell, int index)
{
if (!dicNoticeTap.TryGetValue(cell.GetInstanceID(), out NoticeTab noticeTab))
{
noticeTab = new NoticeTab(cell, OnNoticeTab);
dicNoticeTap[cell.GetInstanceID()] = noticeTab;
}
switch (curSelectNoticeType)
{
case E_NoticeType.Activity:
{
if (listUIBanner == null)
return;
if (listUIBanner.Count < 0 || index >= listUIBanner.Count)
{
DebugUtil.LogWarning("索引错误");
return;
}
noticeTab.SetInfo(listUIBanner[index]);
if (curSelectTab == null) // 默认选中一个
OnNoticeTab(noticeTab);
else if (noticeTab.ID == curSelectTab.ID)
OnNoticeTab(noticeTab);
else
noticeTab.IsSelect = false;
}
break;
case E_NoticeType.System:
{
if (listNoticeInfo == null)
return;
if (listNoticeInfo.Count < 0 || index >= listNoticeInfo.Count)
{
DebugUtil.LogWarning("索引错误");
return;
}
noticeTab.SetInfo(listNoticeInfo[index]);
if (curSelectTab == null) // 默认选中一个
OnNoticeTab(noticeTab);
else if (noticeTab.ID == curSelectTab.ID)
OnNoticeTab(noticeTab);
else
noticeTab.IsSelect = false;
}
break;
default:
{
noticeTab.IsSelect = false;
}
break;
}
}
/// <summary>
/// 关闭
/// </summary>
private void OnClose()
{
CloseWindow();
}
/// <summary>
/// 公告类型页签
/// </summary>
private void OnNoticeTypeTab(E_NoticeType noticeType)
{
if (curSelectNoticeType == noticeType)
return;
if (dicNoticeTypeTab.TryGetValue(curSelectNoticeType, out NoticeTypeTab tab))
tab.IsSelect = false;
curSelectNoticeType = noticeType;
if (dicNoticeTypeTab.TryGetValue(curSelectNoticeType, out tab))
tab.IsSelect = true;
tabRecycleView.ShowList(GetShowTabCount());
}
/// <summary>
/// 公告页签
/// </summary>
private void OnNoticeTab(NoticeTab tab)
{
if (curSelectTab != null)
{
if (curSelectTab.ID == tab.ID)
return;
curSelectTab.IsSelect = false;
}
curSelectTab = tab;
curSelectTab.IsSelect = true;
switch (curSelectNoticeType)
{
case E_NoticeType.Activity:
noticeContent.SetInfo(curSelectTab.UIBanner);
break;
case E_NoticeType.System:
noticeContent.SetInfo(curSelectTab.NoticeInfo);
break;
default:
break;
}
}
#endregion
}