Net.Like.Xue.Tokyo/Assets/Artists/Scripts/UX/UXHud.cs

101 lines
3.3 KiB
C#
Raw Normal View History

2024-11-03 16:42:23 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using BITKit;
using BITKit.UX;
using BITKit.WorldNode;
using Cysharp.Threading.Tasks;
using Net.Project.B.Interaction;
using Project.B.Map;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace Net.Like.Xue.Tokyo.UX
{
public class UXHud : UIToolKitPanel
{
private readonly IGameMapService _gameMapService;
private readonly IWorldInteractionService _interactionService;
private readonly WorldInfoNodeService _worldInfoNodeService;
public UXHud(IUXService uxService, IGameMapService gameMapService, IWorldInteractionService interactionService, WorldInfoNodeService worldInfoNodeService) : base(uxService)
{
_gameMapService = gameMapService;
_interactionService = interactionService;
_worldInfoNodeService = worldInfoNodeService;
_gameMapService.OnMapChanged += OnMapChanged;
_interactionService.OnInteraction += OnInteraction;
}
private void OnInteraction(object arg1, IWorldInteractable arg2, WorldInteractionProcess arg3, object arg4)
{
switch (arg3)
{
case WorldInteractionProcess.Hover:
_interactionTips.SetActive(true);
_interactionTips.text = _worldInfoNodeService.WorldInfoNodes.TryGetValue(arg2.Id, out var node) ? node.Name : "互动";
break;
default:
_interactionTips.SetActive(false);
break;
}
}
private void OnMapChanged(Guid arg1, string arg2)
{
UXService.Entry<UXHud>();
}
protected override string DocumentPath => "ux_hud";
public override bool AllowInput => true;
public override bool AllowCursor => false;
[UXBindPath("minimap-container")]
private VisualElement _minimapContainer;
[UXBindPath("minimap-player")]
private VisualElement _miniPlayer;
[UXBindPath("interaction-tips")]
private Label _interactionTips;
private Camera _minimapCamera;
private Camera _mainCamera;
public override async UniTask EntryAsync()
{
await base.EntryAsync();
UXUtils.Inject(this);
_interactionTips.SetActive(false);
_minimapContainer.RegisterCallback<MouseDownEvent>(x =>
{
UXService.Entry<UXMap>();
});
}
public override void OnTick(float deltaTime)
{
base.OnTick(deltaTime);
if(RootVisualElement is null)return;
if (!_minimapCamera)
{
_minimapCamera = GameObject.Find("minimap_camera")?.GetComponent<Camera>();
return;
}
if (!_mainCamera)
{
_mainCamera = Camera.main;
return;
}
var point = _minimapCamera.WorldToViewportPoint(_mainCamera.transform.position);
_miniPlayer.style.left = _minimapContainer.layout.width * point.x - _miniPlayer.layout.width / 2;
_miniPlayer.style.top = _minimapContainer.layout.height * point.y - _miniPlayer.layout.height / 2;
}
}
}