Files
back_cantanBuilding/Assets/Scripts/UI/Social/SocialLeadboard.cs
2026-05-26 16:15:54 +08:00

141 lines
4.2 KiB
C#

using asap.core;
using game;
using GameCore;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UniRx;
public class SocialLeadboard : MonoBehaviour
{
LeadboardData _leadboardData;
List<Toggle> toggleList = new List<Toggle>();
List<GameObject> selected_tab = new List<GameObject>();
public GameObject item;
public PanelScroll scrollRect;
float itemHigh;
public int curSelfRank;
SocialLeadboardItem selfItem;
IDisposable disposable;
private void Awake()
{
_leadboardData = GContext.container.Resolve<LeadboardData>();
scrollRect = transform.Find("rank/ScrollView").GetComponent<PanelScroll>();
item = transform.Find("rank/ScrollView/Viewport/Content/Item").gameObject;
selfItem = transform.Find("rank/ScrollView/Viewport/Item").GetComponent<SocialLeadboardItem>();
for (int i = 1; i <= 2; i++)
{
toggleList.Add(transform.Find($"tab/text_tab{i}").GetComponent<Toggle>());
selected_tab.Add(transform.Find($"tab/selected_tab{i}").gameObject);
}
itemHigh = item.GetComponent<RectTransform>().sizeDelta.y;
toggleList[0].isOn = true;
toggleList[1].isOn = false;
scrollRect.loop = false;
}
private void Start()
{
toggleList[0].onValueChanged.AddListener(Global);
toggleList[1].onValueChanged.AddListener(Locals);
//SetScroll();
}
private void OnEnable()
{
disposable = GContext.OnEvent<PFRankDataEvent>().Subscribe((data) =>
{
if (!data.isLocal && toggleList[0].isOn)
{
SetScroll();
}
else if (data.isLocal && toggleList[1].isOn)
{
SetLocalScroll();
}
});
selected_tab[0].SetActive(toggleList[0].isOn);
selected_tab[1].SetActive(toggleList[1].isOn);
if (toggleList[0].isOn)
{
Global(true);
}
else
{
Locals(true);
}
}
//void OnClickItem(SocialLeadboardItem item)
//{
//}
void SetScroll()
{
curSelfRank = -1;
for (int i = 0; i < _leadboardData.LeadboardDataList.Count; i++)
{
if (_leadboardData.LeadboardDataList[i].playFabId == GContext.container.Resolve<IUserService>().UserId)
{
curSelfRank = i;
selfItem.data = _leadboardData.LeadboardDataList[i];
break;
}
}
scrollRect.Init<SocialLeadboardItem, LeadboardItemData>(itemHigh, item, null, _leadboardData.LeadboardDataList);
SetSelfItem();
}
void SetSelfItem()
{
if (curSelfRank >= 0)
{
float selfPos = itemHigh * curSelfRank - scrollRect.viewport.rect.height + itemHigh;
selfItem.gameObject.SetActive(scrollRect.content.anchoredPosition.y < selfPos);
selfItem.OnInit();
scrollRect.onValueChanged.AddListener((Vector2 v2) =>
{
selfItem.gameObject.SetActive(scrollRect.content.anchoredPosition.y < selfPos);
});
}
else
{
selfItem.gameObject.SetActive(false);
}
}
void Global(bool isOn)
{
selected_tab[0].SetActive(isOn);
if (isOn)
{
SetScroll();
}
}
void Locals(bool isOn)
{
selected_tab[1].SetActive(isOn);
if (isOn)
{
SetLocalScroll();
}
}
void SetLocalScroll()
{
curSelfRank = -1;
for (int i = 0; i < _leadboardData.LeadboardContinentDataList.Count; i++)
{
if (_leadboardData.LeadboardContinentDataList[i].playFabId == GContext.container.Resolve<IUserService>().UserId)
{
curSelfRank = i;
selfItem.data = _leadboardData.LeadboardContinentDataList[i];
break;
}
}
scrollRect.Init<SocialLeadboardItem, LeadboardItemData>(itemHigh, item, null, _leadboardData.LeadboardContinentDataList);
SetSelfItem();
}
private void OnDisable()
{
disposable?.Dispose();
disposable = null;
}
}