AxibugEmuOnline/AxibugEmuOnline.Client/Assets/Script/AppMain/UI/GamesUI/RomItem.cs

145 lines
3.8 KiB
C#
Raw Normal View History

using AxibugEmuOnline.Client.ClientCore;
using AxibugEmuOnline.Client.Event;
2024-12-11 21:21:27 +08:00
using AxibugEmuOnline.Client.UI;
using Coffee.UIExtensions;
using System;
2024-12-11 21:21:27 +08:00
using UnityEngine;
using UnityEngine.UI;
namespace AxibugEmuOnline.Client
{
public class RomItem : MenuItem, IVirtualItem
{
[SerializeField]
Image m_romImage;
[SerializeField]
GameObject DownloadingFlag;
[SerializeField]
Slider DownProgress;
[SerializeField]
GameObject FileReadyFlag;
[SerializeField]
UIShiny DownloadComplete;
2024-12-11 21:21:27 +08:00
public int Index { get; set; }
private RomLib m_romlib => App.nesRomLib;
private RomFile m_romfile;
protected override void OnEnable()
{
Eventer.Instance.RegisterEvent<int>(EEvent.OnRomFileDownloaded, OnRomDownloaded);
}
protected override void OnDisable()
{
Eventer.Instance.UnregisterEvent<int>(EEvent.OnRomFileDownloaded, OnRomDownloaded);
}
private void OnRomDownloaded(int romID)
{
if (m_romfile == null || m_romfile.ID != romID) return;
DownloadComplete.Play();
}
2024-12-11 21:21:27 +08:00
public void SetData(object data)
{
Reset();
m_romfile = (RomFile)data;
m_romfile.OnInfoFilled += OnRomInfoFilled;
m_romImage.sprite = null;
UpdateView();
if (!m_romfile.InfoReady)
{
m_romlib.BeginFetchRomInfo(m_romfile);
}
SetSelectState(data is ThirdMenuRoot && ((ThirdMenuRoot)data).SelectIndex == Index);
2024-12-11 21:21:27 +08:00
}
public void SetDependencyProperty(object data)
{
SetSelectState(data is ThirdMenuRoot && ((ThirdMenuRoot)data).SelectIndex == Index);
2024-12-11 21:21:27 +08:00
}
protected override void OnSelected(float progress)
{
base.OnSelected(progress);
if (m_romImage.sprite == null) LaunchUI.Instance.HideRomPreview();
else LaunchUI.Instance.SetRomPreview(m_romImage.sprite);
}
2024-12-11 21:21:27 +08:00
public void Release()
{
m_romfile.OnInfoFilled -= OnRomInfoFilled;
}
private void OnRomInfoFilled()
{
UpdateView();
}
private void UpdateView()
{
if (!m_romfile.InfoReady)
{
SetBaseInfo("正在拉取", "---", "---");
2024-12-11 21:21:27 +08:00
}
else
{
SetBaseInfo(m_romfile.Alias, m_romfile.Descript, m_romfile.GameTypeDes);
App.CacheMgr.GetSpriteCache(m_romfile.ImageURL, (img, url) =>
{
if (!m_romfile.InfoReady || url != m_romfile.ImageURL) return;
2024-12-11 21:21:27 +08:00
m_romImage.sprite = img;
if (m_select) LaunchUI.Instance.SetRomPreview(img);
2024-12-11 21:21:27 +08:00
});
}
}
public override bool OnEnterItem()
{
if (!m_romfile.RomReady)
{
m_romfile.BeginDownload();
return false;
}
else
{
App.emu.BeginGame(m_romfile);
return false;
}
}
protected override void Update()
{
DownloadingFlag.SetActiveEx(false);
FileReadyFlag.SetActiveEx(false);
if (m_romfile == null) return;
if (!m_romfile.InfoReady) return;
if (m_romfile.IsDownloading)
{
DownloadingFlag.SetActiveEx(true);
DownProgress.value = m_romfile.Progress;
}
else if (m_romfile.RomReady)
{
FileReadyFlag.SetActiveEx(true);
}
base.Update();
}
}
}