using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///
/// 所有关卡的排行榜信息
///
public class AllLevPanel : BasePanel
{
//总关卡左滑右滑长度变化
public RectTransform imgLev;
//关卡条长度
public float maxWidth=335f;
//左滑
public Button btnLeft;
//右滑
public Button btnRight;
//关闭按钮
public Button btnClose;
//关卡范围
public Text txtAllLev;
public Transform content;
//区间范围
private int beginIndex;
private int endIndex;
//用于存储关卡按钮们
public List itemList = new List();
private List levList;
private int num;
public override void Init()
{
UpdatePan();
btnClose.onClick.AddListener(() =>
{
UIManager.Instance.HidePanel();
});
btnLeft.onClick.AddListener(() =>
{
beginIndex -= 30;
endIndex -= 30;
if (beginIndex<1)
{
beginIndex = levList.Count / 30 * 30+1;
endIndex = levList.Count;
}
else if (endIndex%30!=0&&endIndex< levList.Count&& levList.Count>30)
{
endIndex = beginIndex / 30*30 + 30;
}
UpdataPanel(beginIndex, endIndex);
InitInfo(beginIndex, endIndex);
UpdateImg(num, beginIndex / 30 + 1);
});
btnRight.onClick.AddListener(() =>
{
beginIndex += 30;
endIndex += 30;
if (beginIndex > levList.Count)
{
beginIndex = 1;
if (levList.Count >= 30)
{
endIndex = 30;
}
else
{
endIndex = levList.Count;
}
}
else if (endIndex > levList.Count&&beginIndex < levList.Count)
{
endIndex = levList.Count;
}
UpdataPanel(beginIndex, endIndex);
InitInfo(beginIndex, endIndex);
UpdateImg(num, beginIndex / 30+1);
});
}
//一打开面板的显示数据
private void UpdatePan()
{
//获取关卡的状态信息
levList = GameDataMgr.Instance.levList;
//得到一共要循环创建多少个区间按钮
//由于向下取整 +1 代表 平均分成了num个按钮
if (levList.Count % 30 == 0)
{
num = levList.Count / 30;
}
else
{
num = levList.Count / 30 + 1;
}
beginIndex = 1;
if (levList.Count >= 30)
{
endIndex = 30;
}
else
{
endIndex = levList.Count;
}
UpdateImg(num, beginIndex / 30 + 1);
UpdataPanel(beginIndex, endIndex);
InitInfo(beginIndex, endIndex);
}
public void InitInfo(int beginIndex, int endIndex)
{
//记录当前区间按钮的 区间值
this.beginIndex = beginIndex;
this.endIndex = endIndex;
//把区间显示的内容 更新了
txtAllLev.text = "第"+ beginIndex + " - " + endIndex + "关";
}
///
/// 提供给其他地方 用于更新 当前选择区间的右侧按钮
///
///
///
public void UpdataPanel(int beginIndex, int endIndex)
{
//第一步:删除之前的单个按钮
for (int i = 0; i < itemList.Count; i++)
{
//删除之前的 对象
Destroy(itemList[i]);
}
//删除完成后 一定要清空列表
itemList.Clear();
//第二步:创建新的按钮
for (int i = beginIndex; i <= endIndex; i++)
{
//根据信息 更新按钮数据
GameObject levItem = Instantiate(Resources.Load("UI/LevItem1"));
levItem.transform.SetParent(content, false);
//根据信息 更新按钮数据
LevItem1 item = levItem.GetComponent();
item.InitInfo(levList[i-1]);
//创建成功后 把它记录到列表中
itemList.Add(levItem);
}
}
private void UpdateImg(float f,int lenght)
{
// 更新 Image 的宽度
float newWidth = maxWidth * (lenght / f);
imgLev.sizeDelta = new Vector2(newWidth, imgLev.sizeDelta.y);
}
}