Files
bansonic_beta_main/Assets/playerInfoDisplay/uRecentGameHistory.cs
T
2026-07-20 22:19:25 +08:00

218 lines
5.8 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class uRecentGameHistory : MonoBehaviour
{
private const string SongResourcesPath = "song_songIndex";
private const int MaxVisibleRecordCount = 5;
private const float DefaultPrefabScaleRatio = 1.25f;
[Header("gameobjects")]
public GameObject gameHistoryPrefab;
public Transform gameHistoryParent;
public Transform dGameHistoryParent;
public GameObject more_gameHistory;
[Header("layout")]
public float prefabScaleRatio = DefaultPrefabScaleRatio;
[Header("buttons")]
public Button more_gameHistoryButton;
public Button shut_gameHistoryButton;
private SongData[] cachedSongs;
private readonly List<GameObject> previewEntries = new List<GameObject>();
private readonly List<GameObject> detailEntries = new List<GameObject>();
private void Start()
{
BindButtons();
if (more_gameHistory != null)
{
more_gameHistory.SetActive(false);
}
RebuildHistory();
}
private void OnDestroy()
{
if (more_gameHistoryButton != null)
{
more_gameHistoryButton.onClick.RemoveListener(OpenDetailedHistory);
}
if (shut_gameHistoryButton != null)
{
shut_gameHistoryButton.onClick.RemoveListener(CloseDetailedHistory);
}
}
public void RebuildHistory()
{
if (gameHistoryParent == null || gameHistoryPrefab == null)
{
return;
}
ClearEntries(previewEntries, gameHistoryParent);
cachedSongs = RuntimeResourcesCache.LoadSongsFromPath(SongResourcesPath);
var records = RecentPlayHistoryStore.GetRecords();
if (records == null)
{
return;
}
int visibleCount = Mathf.Min(records.Count, MaxVisibleRecordCount);
for (int i = 0; i < visibleCount; i++)
{
RecentPlayRecord record = records[i];
if (record == null)
{
continue;
}
GameObject instance = Instantiate(gameHistoryPrefab, gameHistoryParent);
instance.transform.localScale *= prefabScaleRatio;
previewEntries.Add(instance);
UI_OrderedEntryAnimator.PlaySingle(instance, i, 0.18f, 0.015f, -14f, 0.97f, true);
ghPrefab controller = instance.GetComponent<ghPrefab>();
if (controller == null)
{
continue;
}
controller.Setup(record, FindSongById(record.songID));
}
}
private void OpenDetailedHistory()
{
if (more_gameHistory != null)
{
more_gameHistory.SetActive(true);
UI_OrderedEntryAnimator.PlayRefresh(more_gameHistory, 0.18f, -10f, 0.985f, true);
}
if (dGameHistoryParent == null || gameHistoryPrefab == null)
{
return;
}
ClearEntries(detailEntries, dGameHistoryParent);
EnsureSongCache();
var records = RecentPlayHistoryStore.GetRecords();
if (records == null)
{
return;
}
for (int i = 0; i < records.Count; i++)
{
RecentPlayRecord record = records[i];
if (record == null)
{
continue;
}
GameObject instance = Instantiate(gameHistoryPrefab, dGameHistoryParent);
detailEntries.Add(instance);
UI_OrderedEntryAnimator.PlaySingle(instance, i, 0.16f, 0.012f, -12f, 0.97f, true);
ghPrefab controller = instance.GetComponent<ghPrefab>();
if (controller == null)
{
continue;
}
controller.Setup(record, FindSongById(record.songID));
}
}
private void CloseDetailedHistory()
{
if (more_gameHistory != null)
{
UI_PanelExitUtility.PlayExitThen(more_gameHistory, () =>
{
ClearEntries(detailEntries, dGameHistoryParent);
if (more_gameHistory != null)
{
more_gameHistory.SetActive(false);
}
});
return;
}
ClearEntries(detailEntries, dGameHistoryParent);
}
private SongData FindSongById(int songId)
{
if (songId <= 0 || cachedSongs == null)
{
return null;
}
for (int i = 0; i < cachedSongs.Length; i++)
{
SongData song = cachedSongs[i];
if (song != null && song.songID == songId)
{
return song;
}
}
return null;
}
private void BindButtons()
{
if (more_gameHistoryButton != null)
{
more_gameHistoryButton.onClick.RemoveListener(OpenDetailedHistory);
more_gameHistoryButton.onClick.AddListener(OpenDetailedHistory);
}
if (shut_gameHistoryButton != null)
{
shut_gameHistoryButton.onClick.RemoveListener(CloseDetailedHistory);
shut_gameHistoryButton.onClick.AddListener(CloseDetailedHistory);
}
}
private void EnsureSongCache()
{
if (cachedSongs == null || cachedSongs.Length == 0)
{
cachedSongs = RuntimeResourcesCache.LoadSongsFromPath(SongResourcesPath);
}
}
private static void ClearEntries(List<GameObject> entries, Transform parent)
{
for (int i = entries.Count - 1; i >= 0; i--)
{
if (entries[i] != null)
{
Destroy(entries[i]);
}
}
entries.Clear();
if (parent == null)
{
return;
}
for (int i = parent.childCount - 1; i >= 0; i--)
{
Destroy(parent.GetChild(i).gameObject);
}
}
}