Files
Net.Like.Xue.Tokyo/Packages-Local/Com.Project.B.Unity/UX/UXLoadingMap.cs

102 lines
3.2 KiB
C#
Raw Normal View History

2025-06-24 23:49:13 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using BITKit;
using BITKit.Entities;
using BITKit.Mod;
using BITKit.UX;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Net.Project.B.UX;
using Project.B.Map;
using UnityEngine;
using UnityEngine.UIElements;
namespace Project.B.UX
{
public class UXLoadingMap:UIToolKitPanel,IDisposable,ILogger<UXLoadingMap>, IUXLoadingMap
{
private readonly IGameMapService _gameMapService;
private readonly IEntitiesService _entitiesService;
public UXLoadingMap(IUXService uxService, IGameMapService gameMapService, IEntitiesService entitiesService) : base(uxService)
{
_gameMapService = gameMapService;
_entitiesService = entitiesService;
_gameMapService.OnMapChange += OnMapChange;
_gameMapService.OnMapLoadingProgress += OnMapLoadingProgress;
}
[UXBindPath("map-label")]
private Label _mapLabel;
[UXBindPath("load_step-label")]
private Label _loadStepLabel;
[UXBindPath("load-progress")]
private ProgressBar _loadProgress;
[UXBindPath("background-image")]
private VisualElement _backgroundImage;
private UniTaskCompletionSource _waitEntry = new();
private async UniTask OnMapChange(string arg)
{
await WaitUtilInitialized.Task;
_mapLabel.text = _gameMapService.CurrentMap;
foreach (var gameMapData in _entitiesService.QueryComponents<IGameMapData>().ToArray())
{
if(string.Equals(gameMapData.MapAddress,_gameMapService.CurrentMap) is false)continue;
if (gameMapData.LoadingScreen is Texture2D texture2D)
{
_backgroundImage.style.backgroundImage = new(texture2D);
}else if (gameMapData.LoadingScreen is Sprite sprite)
{
_backgroundImage.style.backgroundImage = new(sprite);
}
}
UXService.Entry(this);
await _waitEntry.Task;
_waitEntry = new();
}
private void OnMapLoadingProgress(Guid playerId, float arg1, string arg2)
{
if (VisualTreeAsset is null) return;
_loadProgress.value = arg1;
_loadStepLabel.text = arg2;
}
protected override string DocumentPath=>"ux_loading_map";
public override bool AllowCursor => true;
public override void Dispose()
{
base.Dispose();
_gameMapService.OnMapChange -= OnMapChange;
_gameMapService.OnMapLoadingProgress -= OnMapLoadingProgress;
}
protected override void OnPanelEntry()
{
base.OnPanelEntry();
_waitEntry.TrySetResult();
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
_loadStepLabel.text = state.ToString();
}
public bool IsEnabled(LogLevel logLevel) => IsEntered;
public IDisposable BeginScope<TState>(TState state) where TState : notnull
{
return null;
}
}
}