AxibugEmuOnline/AxibugEmuOnline.Client/Assets/Script/UI/RoomItem.cs

127 lines
3.5 KiB
C#
Raw Normal View History

2024-09-18 15:53:58 +08:00
using AxibugEmuOnline.Client.ClientCore;
2024-11-07 19:38:48 +08:00
using AxibugEmuOnline.Client.Event;
2024-09-18 15:53:58 +08:00
using AxibugEmuOnline.Client.UI;
using AxibugProtobuf;
2024-11-08 11:31:12 +08:00
using System;
2024-09-18 15:53:58 +08:00
using UnityEngine;
using UnityEngine.UI;
namespace AxibugEmuOnline.Client
{
public class RoomItem : MenuItem, IVirtualItem
{
[SerializeField]
Image m_roomPreview;
[SerializeField]
Slider m_downloadProgress;
[SerializeField]
GameObject m_downloadingFlag;
[SerializeField]
GameObject m_romReadyFlag;
private RomFile m_romFile;
public int Index { get; set; }
2024-11-07 19:38:48 +08:00
public int roomID { get; private set; }
2024-09-18 15:53:58 +08:00
2024-11-08 11:31:12 +08:00
protected override void Awake()
{
base.Awake();
Eventer.Instance.RegisterEvent<int>(EEvent.OnRoomListSingleUpdate, OnRoomSignelUpdate);
}
private void OnRoomSignelUpdate(int roomID)
{
if (this.roomID != roomID) return;
if (App.roomMgr.GetRoomListMiniInfo(roomID, out var roomInfo))
UpdateUI(roomInfo);
}
2024-09-18 15:53:58 +08:00
2024-11-07 19:38:48 +08:00
public void SetData(object data)
{
var roomInfo = data as Protobuf_Room_MiniInfo;
roomID = roomInfo.RoomID;
2024-09-18 15:53:58 +08:00
2024-11-07 19:38:48 +08:00
UpdateUI(roomInfo);
}
2024-09-18 15:53:58 +08:00
2024-11-07 20:33:44 +08:00
public override bool OnEnterItem()
{
if (!m_romFile.RomReady)
{
m_romFile.BeginDownload();
return false;
}
else
{
App.roomMgr.SendJoinRoom(roomID, 1);
return true;
}
}
2024-11-07 19:38:48 +08:00
private void UpdateUI(Protobuf_Room_MiniInfo roomInfo)
{
var hostNick = roomInfo.GetHostNickName();
roomInfo.GetRoomPlayers(out var cur, out var max);
SetBaseInfo(string.Empty, $"<b>{hostNick}</b><3E>ķ<EFBFBD><C4B7><EFBFBD> - {cur}/{max}");
SetIcon(null);
2024-11-08 11:31:12 +08:00
roomInfo.FetchRomFileInRoomInfo(EnumPlatform.NES, (room, romFile) =>
2024-11-07 19:38:48 +08:00
{
2024-11-08 11:31:12 +08:00
if (room.RoomID != roomID) return;
2024-11-07 19:38:48 +08:00
2024-11-08 11:31:12 +08:00
m_romFile = romFile;
Txt.text = romFile.Alias;
2024-11-07 19:38:48 +08:00
UpdateRomInfoView();
App.CacheMgr.GetSpriteCache(romFile.ImageURL, OnGetRomImage);
});
2024-09-18 15:53:58 +08:00
}
private void Update()
{
UpdateRomInfoView();
}
private void UpdateRomInfoView()
{
float? downloadingProgress = null;
bool romReady = false;
if (m_romFile != null)
{
if (m_romFile.IsDownloading)
downloadingProgress = m_romFile.Progress;
if (m_romFile.RomReady)
romReady = true;
}
m_downloadingFlag.SetActiveEx(downloadingProgress.HasValue);
if (downloadingProgress.HasValue)
m_downloadProgress.value = downloadingProgress.Value;
m_romReadyFlag.SetActiveEx(romReady);
}
private void OnGetRomImage(Sprite sprite, string url)
{
if (m_romFile == null) return;
if (m_romFile.ImageURL != url) return;
SetIcon(sprite);
}
public void SetDependencyProperty(object data)
{
SetSelectState(data is ThirdMenuRoot tr && tr.SelectIndex == Index);
}
public void Release()
{
Reset();
}
}
}