using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using XFramework.Objects;
///
/// 一个可以逐页展示数据的窗口控制器 需要和group 配合使用 只关注子物体数量 初始化方法、更新方法 其他不做关注
///
public class ShowMsgByPageBase : MonoBehaviour
{
public GameObject showMessPrefab; //子物体预制件
List showPool = new List();//当前显示或隐藏的所有子物体
int Page = 1;//默认显示第一页
int msgCount;//数据数量
int allPage;//总页数
public int onePageCount = 10;//单页显示的数量
Text showPage;
UnityAction initChildEvent;//初始化子物体 由外部注入确定UpdataChildFun 子物体首次创建时调用 只执行一次
UnityAction updateChildEvent;//更新子物体 由外部注入确定UpdataChildFun 子物体每次更新时调用
// Use this for initialization
void Awake()
{
InitComponent();
}
void InitComponent()
{
this.transform.parent.Find("Button_nextPage").GetComponent().onClick.AddListener(NextPage);
this.transform.parent.Find("Button_lastPage").GetComponent().onClick.AddListener(LastPage);
showPage = this.transform.parent.Find("Text_page").GetComponent();
}
/////
///// 注入数据前需要先注册界面 指定待创建的子物体 和子物体初始化方法 未确定此思路 待定
/////
///// 初始化子物体的方法 int 数据索引 GameObject 带初始化的子物体
///// 子物体预制件
//public void RegistePanel(UnityAction initChildFun,GameObject childPrefab)
//{
// if (showMessPrefab!=null&& childPrefab != showMessPrefab)//指定了新的预制件 清空界面
// {
// for (int i = 0; i < showPool.Count; i++)//
// {
// Destroy(showPool[i]);
// }
// showPool = new List();
// }
//}
///
/// 注入待展示的所有数据 覆盖此前数据
///
/// 待展示的数据总数
/// 子物体创建时的初始化方法
/// 更新子物体的方法
public virtual void InitPanel(int count, UnityAction initChildFun, UnityAction UpdataChildFun)
{
initChildEvent = initChildFun;
updateChildEvent = UpdataChildFun;
ShowData(count);
}
///
/// 初始化后可以直接 传入数据数量 更新界面
///
public void ShowData(int count)
{
msgCount = count;
GetMaxPage();
Page = 1;
ShowOnePageMessage(Page);
}
///
/// 添加数据 当前在最大页数 计算是否需要增加页数 并更新界面
///
///
public virtual void AddChild(int count)
{
if (msgCount == 0)
{
Debug.LogError("未注入原始数据前不能添加");
}
else
{
msgCount += count;
if (Page == allPage)//
{
GetMaxPage();
ShowOnePageMessage(Page);
}
}
}
///
/// 减少数据 并更新界面
///
///
public virtual void RemoveChild(int count)
{
if (msgCount == 0)
Debug.LogError("当前无数据");
else if (msgCount > count)
{
msgCount -= count;
GetMaxPage();
if (Page > allPage)//减少到当前页码超出最大页码时 更新当前页码
Page = allPage;
ShowOnePageMessage(Page);
}
}
//显示一页数据
void ShowOnePageMessage(int pageNumber)
{
HideAllMessage();
UpdatePageText(Page, allPage);
int maxIndex;//计算当前页数 数据的最大索引
maxIndex = pageNumber * onePageCount > msgCount ? msgCount : pageNumber * onePageCount;
for (int i = (pageNumber - 1) * onePageCount; i < maxIndex; i++)
{
//CreatShowMessageGO().GetComponent().SetMessage(recordClasses[i]);
updateChildEvent?.Invoke(i, CreatShowMessageGO());
}
}
///
/// 翻页
///
///
/// 当前页码
int TurnPage(TurnPageType turn)
{
switch (turn)
{
case TurnPageType.Up:
if (Page > 1)
{
Page -= 1;
ShowOnePageMessage(Page);
}
break;
case TurnPageType.Down:
if (Page * onePageCount < msgCount)
{
Page += 1;
ShowOnePageMessage(Page);
}
break;
default:
break;
}
return Page;
}
//计算最大页数
private void GetMaxPage()
{
allPage = msgCount / onePageCount;
if (msgCount % onePageCount > 0)
{
allPage += 1;
}
}
///
/// 隐藏所有信息
///
void HideAllMessage()
{
for (int i = 0; i < showPool.Count; i++)
{
showPool[i].SetActive(false);
}
UpdatePageText(0, 0);
}
void NextPage()
{
TurnPage(TurnPageType.Down);
}
void LastPage()
{
TurnPage(TurnPageType.Up);
}
//更新页码显示
void UpdatePageText(int currentPage, int AllPage)
{
if (showPage == null) return;
LocalizationManager.SetFormatted(showPage, "第{0}页/共{1}页", currentPage, AllPage);
}
//展示一页数据时
GameObject CreatShowMessageGO()
{
GameObject returnGO = null;
for (int i = 0; i < showPool.Count; i++)
{
if (!showPool[i].gameObject.activeSelf)
{
returnGO = showPool[i];
returnGO.SetActive(true);
return returnGO;
}
}
returnGO = Instantiate(showMessPrefab);
returnGO.transform.SetParent(this.transform);
returnGO.transform.localScale = Vector3.one;
initChildEvent?.Invoke(returnGO);//新建物体需要初始化
showPool.Add(returnGO);
return returnGO;
}
void OnDisable()
{
HideAllMessage();
}
}