Files
Net.Like.Xue.Tokyo/Packages-Local/Com.Project.B.Unity/UX/UXMapSelector.cs
2025-06-24 23:49:13 +08:00

110 lines
4.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BITKit;
using BITKit.Entities;
using BITKit.Mod;
using BITKit.UX;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Project.B.Map;
using Project.B.Player;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
namespace Net.Project.B.UX
{
public class UXMapSelector : UIToolKitPanel, IUXMapSelector
{
private readonly IEntitiesService _entitiesService;
private readonly IUXKeyMap<InputAction> _uxKeyMap;
private readonly ILogger<UXMapSelector> _logger;
private readonly IGameMapService _gameMapService;
protected override string DocumentPath => "ui_map_selector";
public override bool AllowCursor => true;
[UXBindPath("map-name")] private Label _mapNameLabel;
[UXBindPath("map-description")] private Label _mapDescriptionLabel;
[UXBindPath("map-container")] private VisualElement _mapContainer;
[UXBindPath("background-image")] private VisualElement _backgroundImage;
public UXMapSelector(IUXService uxService, IGameMapService gameMapService, ILogger<UXMapSelector> logger, IUXKeyMap<InputAction> uxKeyMap, IEntitiesService entitiesService) :
base(uxService)
{
_gameMapService = gameMapService;
_logger = logger;
_uxKeyMap = uxKeyMap;
_entitiesService = entitiesService;
}
protected override void OnPanelEntry()
{
base.OnPanelEntry();
_backgroundImage.transform.scale = new Vector3(1.2f, 1.2f, 1.2f);
InputActionGroup.RegisterCallback(_uxKeyMap.InventoryKey, OnReturn);
InputActionGroup.RegisterCallback(_uxKeyMap.CancelKey, OnReturn);
}
private void OnReturn(InputAction.CallbackContext obj)
{
if (obj is { performed: true })
{
UXService.Return();
}
}
protected override void OnPanelExit()
{
base.OnPanelExit();
InputActionGroup.UnRegisterCallback(_uxKeyMap.InventoryKey, OnReturn);
InputActionGroup.UnRegisterCallback(_uxKeyMap.CancelKey, OnReturn);
}
public override async UniTask EntryAsync()
{
await base.EntryAsync();
_mapContainer.Clear();
var maps = _entitiesService.QueryComponents<IGameMapData>().ToArray();
foreach (var gameMapData in maps.OrderBy(x=>x.MapName))
{
var button = _mapContainer.Create<Button>();
button.text = gameMapData.MapName;
button.clicked += () =>
{
_logger.LogInformation($"Loading Map:{gameMapData.MapName}@{gameMapData.MapAddress}");
_gameMapService.StartMapAsync(gameMapData.MapAddress).Forget();
};
if (gameMapData.LoadingScreen is Texture2D texture)
{
button.style.backgroundImage = new StyleBackground(texture);
button.RegisterCallback<PointerOverEvent>(OnPointerOver);
}else if (gameMapData.LoadingScreen is Sprite sprite)
{
button.style.backgroundImage = new StyleBackground(sprite);
button.RegisterCallback<PointerOverEvent>(OnPointerOver);
}
void OnPointerOver(PointerOverEvent evt)
{
_mapNameLabel.text = gameMapData.MapName;
_mapDescriptionLabel.text = gameMapData.Description;
_backgroundImage.style.backgroundImage = button.style.backgroundImage;
_backgroundImage.MarkDirtyRepaint();
}
}
}
public override void OnTick(float deltaTime)
{
base.OnTick(deltaTime);
_backgroundImage.transform.position = new Vector3(Mathf.PingPong(Time.time,8), 0, 0) * 8;
}
}
}