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

632 lines
16 KiB
C#

using cfg.UIInterfaceBannerGoToCfg;
using Cysharp.Threading.Tasks;
using Gameplay;
using Gameplay.Net;
using PhxhSDK;
using System;
using System.Collections.Generic;
using System.Linq;
using Framework;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 公告UI
/// </summary>
public class UI_NoticeController : UIWindow
{
/// <summary>
/// 打开公告界面
/// </summary>
public static async UniTask<UIWindow> Open(int index = 0, E_NoticeType noticeType = E_NoticeType.Activity)
{
CommonUtils.CaptureScreenshot(); // 截取当前屏幕截图
InputData inputData = new()
{
OpenType = noticeType,
Idx = index,
};
return await UIManager.Instance.CreateAndOpenWindow(UINameConst.UI_Notice, inputData);
}
private struct InputData
{
public E_NoticeType OpenType;
public int Idx;
}
#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 int index;
/// <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");
IsSelect = false;
CommonUtils.BindButton(buttonChoose, OnClick);
CommonUtils.BindButton(buttonNormal, 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 int ID;
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");
IsSelect = false;
CommonUtils.BindButton(buttonChoose, OnClick);
CommonUtils.BindButton(buttonNormal, OnClick);
}
/*public void SetInfo(DataUIBanner dataUIBanner)
{
uiBanner = dataUIBanner;
IsScaleShow = true;
}*/
public void SetInfo(NoticeInfo noticeInfo, int id)
{
ID = id;
if (noticeInfo == null)
{
IsScaleShow = false;
return;
}
this.noticeInfo = noticeInfo;
textSelectTitle.text = noticeInfo.Title;
textTitle.text = noticeInfo.Title;
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;
/// <summary>
/// 超链接文本
/// </summary>
private LinkText linkText;
public NoticeContent(GameObject root) : base(root)
{
textActivityName = GetComponent<TMP_Text>("ImageTitle/TextActivityName");
textContent = GetComponent<TMP_Text>("TextContent");
imageBanner = GetComponent<Image>("ImageBanner");
linkText = GetComponent<LinkText>("TextContent");
}
//TODO 暂不走表
/*public async void SetInfo(DataUIBanner dataUIBanner)
{
var sprite = await LoadAssetAsync<Sprite>(dataUIBanner.BannerImgPath);
if (sprite != null)
imageBanner.sprite = sprite;
IsScaleShow = true;
}*/
public async void SetInfo(NoticeInfo noticeInfo, int index)
{
if (noticeInfo == null)
{
IsScaleShow = false;
return;
}
linkText.SetText(noticeInfo.Content);
textActivityName.text = noticeInfo.Title;
var bannerList = TableManager.Instance.Tables.bannerCfg.DataList;
IsScaleShow = true;
if (index >= bannerList.Count)
return;
var sprite = await AssetManager.Instance.LoadAssetAsync<Sprite>(bannerList[index].NoticeImgPath);
if (sprite != null)
{
imageBanner.sprite = sprite;
}
}
}
#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 Dictionary<int, NoticeInfo> noticeInfo;
// TODO 公告索引列表 <格子索引,公告索引>
private Dictionary<int, int> noticeIndexDic = new();
/// <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;
private ScrollRect contentScrollRect;
private InputData inputData;
public override void OnInit()
{
var notices = AppConfig.Instance.NoticeInfos;
noticeInfo = new Dictionary<int, NoticeInfo>();
var index = 0;
for (var i = 0; i < notices.Count; i++)
{
if (!string.IsNullOrEmpty(notices[i].Title))
{
noticeInfo.Add(i, notices[i]);
noticeIndexDic.Add(index, i);
index++;
}
}
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"));
contentScrollRect = GetComponent<ScrollRect>("Image_NoticeBG/ContentRoot");
rawImageGaussianBlurMask.texture = CommonUtils.GetScreenTexture2D();
tabRecycleView.Init(OnRefreshTab);
BindButton(buttonClose, OnClose);
}
protected override void OnShowWindow(object data = null)
{
if (data is InputData)
{
inputData = (InputData)data;
}
if (!noticeInfo.TryGetValue(inputData.Idx, out var info))
{
inputData.Idx = noticeInfo.First().Key;
}
OnNoticeTypeTab(inputData.OpenType); // 默认选中活动公告
}
public override void OnRelease()
{
noticeContent.Dispose();
noticeContent = null;
base.OnRelease();
}
/// <summary>
/// 获取要显示的页签数量
/// </summary>
/// <returns></returns>
private int GetShowTabCount()
{
return curSelectNoticeType switch
{
/*E_NoticeType.Activity => listUIBanner.Count,*/
E_NoticeType.Activity => noticeInfo.Count,
//TODO 暂时做空
/*E_NoticeType.System => listNoticeInfo.Count,*/
E_NoticeType.System => 1,
_ => 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 (noticeInfo == null)
return;
if (noticeInfo.Count < 0 || index >= noticeInfo.Count)
{
DebugUtil.LogWarning("索引错误");
return;
}
if (!noticeIndexDic.TryGetValue(index, out var noticeIndex)) return;
noticeTab.SetInfo(noticeInfo[noticeIndex], noticeIndex);
if (inputData.Idx == noticeTab.ID)
{
curSelectTab = noticeTab;
OnNoticeTab(noticeTab);
}
else
{
noticeTab.IsSelect = false;
}
/*if (curSelectTab == null) // 默认选中一个
{
OnNoticeTab(noticeTab);
}
else if (noticeTab.ID == curSelectTab.ID)
OnNoticeTab(noticeTab);
else
noticeTab.IsSelect = false;*/
}
break;
case E_NoticeType.System:
{
noticeTab.SetInfo(null, index);
OnNoticeTab(noticeTab);
/*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;
curSelectTab = null;
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;
contentScrollRect.verticalNormalizedPosition = 1f;
switch (curSelectNoticeType)
{
case E_NoticeType.Activity:
/*noticeContent.SetInfo(curSelectTab.UIBanner);*/
noticeContent.SetInfo(curSelectTab.NoticeInfo, tab.ID);
break;
case E_NoticeType.System:
{
// TODO 暂时做空
noticeContent.SetInfo(null, 0);
break;
}
default:
break;
}
}
#endregion
}