252 lines
8.6 KiB
C#
252 lines
8.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using BITKit;
|
|
using BITKit.UX;
|
|
using Cysharp.Threading.Tasks;
|
|
using Net.Project.B.Dialogue;
|
|
using Net.Project.B.UX;
|
|
using Project.B.Map;
|
|
using Project.B.Player;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.UIElements;
|
|
using System.Linq;
|
|
using AHD2TimeOfDay;
|
|
using BITKit.Entities;
|
|
using BITKit.Mod;
|
|
using BITKit.StateMachine;
|
|
using Microsoft.Extensions.Logging;
|
|
using Net.BITKit.Localization;
|
|
using Project.B.CharacterController;
|
|
using Project.B.Entities;
|
|
using YooAsset;
|
|
using IUXDialogue = Net.Project.B.UX.IUXDialogue;
|
|
|
|
namespace Net.Like.Xue.Tokyo.UX
|
|
{
|
|
public class UXEscMenu : UIToolKitPanel,IDisposable
|
|
{
|
|
private readonly ILogger<UXEscMenu> _logger;
|
|
private readonly IPlayerFactory _playerFactory;
|
|
private readonly IEntitiesService _entitiesService;
|
|
private readonly ILocalizationService _localizationService;
|
|
private readonly IDialogueService _dialogueService;
|
|
private readonly IGameMapService _gameMapService;
|
|
private readonly IUXDialogue _uxDialogue;
|
|
protected override string DocumentPath => "ui_menu_esc";
|
|
public override bool AllowInput => false;
|
|
public override bool AllowCursor => true;
|
|
|
|
private readonly IUXKeyMap<InputAction> _uxKeyMap;
|
|
|
|
private readonly ConcurrentDictionary<int, IDialogueData> _dialogues = new();
|
|
[UXBindPath("dialogue-container")]
|
|
private VisualElement _dialogueContainer;
|
|
[UXBindPath("return-button")]
|
|
private Button _returnButton;
|
|
[UXBindPath("exit-button")]
|
|
private Button _exitButton;
|
|
[UXBindPath("enable-chs-toggle")]
|
|
private Toggle _enableChsToggle;
|
|
[UXBindPath("enable-tps-toggle")]
|
|
private Toggle _enableTpsToggle;
|
|
[UXBindPath("time-slider")]
|
|
private Slider _timeSlider;
|
|
|
|
private VisualTreeAsset _template;
|
|
|
|
[Inject]
|
|
private IPlayerCharacterController _playerCharacterController;
|
|
|
|
public UXEscMenu(IUXService uxService, IUXDialogue uxDialogue, IGameMapService gameMapService, IUXKeyMap<InputAction> uxKeyMap, IDialogueService dialogueService, ILocalizationService localizationService, IEntitiesService entitiesService, IPlayerFactory playerFactory, ILogger<UXEscMenu> logger) : base(uxService)
|
|
{
|
|
_uxDialogue = uxDialogue;
|
|
_gameMapService = gameMapService;
|
|
_uxKeyMap = uxKeyMap;
|
|
_dialogueService = dialogueService;
|
|
_localizationService = localizationService;
|
|
_entitiesService = entitiesService;
|
|
_playerFactory = playerFactory;
|
|
_logger = logger;
|
|
|
|
OnInitiated += Initiated;
|
|
OnInitiatedAsync += InitiatedAsync;
|
|
_uxDialogue.OnSubtitle += OnSubtitle;
|
|
|
|
_dialogueService.OnDialogueStart += OnDialogueStart;
|
|
|
|
playerFactory.OnEntityCreated += OnEntityCreated;
|
|
}
|
|
|
|
|
|
|
|
private UniTask OnEntityCreated(string arg1, IEntity arg2)
|
|
{
|
|
arg2.Inject(this);
|
|
|
|
if (_enableTpsToggle.value)
|
|
{
|
|
_playerCharacterController.AllowTpsCamera.AddElement(this);
|
|
}
|
|
|
|
|
|
return UniTask.CompletedTask;
|
|
}
|
|
|
|
private async UniTask InitiatedAsync()
|
|
{
|
|
_template =await ModService.LoadAsset<VisualTreeAsset>("ui_esc_menu_dialogue-template");
|
|
_dialogueContainer.Clear();
|
|
|
|
_timeSlider.RegisterValueChangedCallback(OnTime);
|
|
}
|
|
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
base.OnStateEntry(old);
|
|
var enable = _entitiesService.QueryComponents<TODController>().Length > 0;
|
|
|
|
_timeSlider.SetEnabled(enable);
|
|
_timeSlider.tooltip = enable ? string.Empty : "这个地图没有开启昼夜系统";
|
|
}
|
|
|
|
private void OnTime(ChangeEvent<float> evt)
|
|
{
|
|
var controllers = _entitiesService.QueryComponents<TODController>();
|
|
foreach (var todController in controllers)
|
|
{
|
|
var par = todController.todGlobalParameters;
|
|
par.CurrentTime = evt.newValue;
|
|
todController.todGlobalParameters = par;
|
|
todController.SetGlobalParameters();
|
|
}
|
|
}
|
|
|
|
private UniTask OnDialogueStart(IDialogueData arg)
|
|
{
|
|
_dialogues.TryAdd(arg.Identity, arg);
|
|
|
|
|
|
var actorName = arg.ActorIdentity.ToString();
|
|
|
|
var isPlayer = false;
|
|
|
|
try
|
|
{
|
|
if (arg.ActorIdentity != 0)
|
|
{
|
|
foreach (var idComponent in _entitiesService.QueryComponents<IdComponent>())
|
|
{
|
|
if (idComponent.Id == arg.ActorIdentity)
|
|
{
|
|
actorName = idComponent.Name;
|
|
throw new OperationCanceledException();
|
|
}
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
var players = _entitiesService.QueryComponents<IdComponent, LocalPlayerComponent>().ToArray();
|
|
if (players.Any())
|
|
{
|
|
isPlayer = true;
|
|
}
|
|
actorName = players.Any( )? players[0].Item1.Name
|
|
: "#Narrator";
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogException(e);
|
|
}
|
|
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
|
|
}
|
|
|
|
var container = _dialogueContainer.Create(_template);
|
|
|
|
container.style.alignSelf = new StyleEnum<Align>(isPlayer ? Align.FlexEnd : Align.FlexStart);
|
|
|
|
container.Get<Label>().text = actorName;
|
|
|
|
container = container.Get<VisualElement>();
|
|
|
|
container.Clear();
|
|
|
|
|
|
foreach (var dictionary in _localizationService.LocalizedStrings.Values)
|
|
{
|
|
if (dictionary.TryGetValue(arg.Text, out var text))
|
|
{
|
|
container.Create<Label>().text = text;
|
|
}
|
|
else
|
|
{
|
|
container.Create<Label>().text = arg.Text;
|
|
}
|
|
|
|
}
|
|
|
|
return UniTask.CompletedTask;
|
|
}
|
|
|
|
private void Initiated()
|
|
{
|
|
_returnButton.clicked += UXService.Return;
|
|
_exitButton.clicked +=()=> _gameMapService.StopMapAsync().Forget();
|
|
_exitButton.clicked += UXService.Entry<IUXLobby>;
|
|
|
|
_enableChsToggle.RegisterValueChangedCallback(x =>
|
|
{
|
|
_uxDialogue.SubtitleLanguages = x.newValue ? _localizationService.LocalizedStrings.Keys.ToArray() : null;
|
|
});
|
|
|
|
_enableTpsToggle.RegisterValueChangedCallback(x =>
|
|
{
|
|
if (_playerCharacterController is not null)
|
|
{
|
|
_playerCharacterController.AllowTpsCamera.SetElements(this,x.newValue);
|
|
}
|
|
});
|
|
}
|
|
|
|
private string OnSubtitle(string arg)
|
|
{
|
|
return arg;
|
|
}
|
|
|
|
protected override void OnPanelEntry()
|
|
{
|
|
base.OnPanelEntry();
|
|
InputActionGroup.RegisterCallback(_uxKeyMap.CancelKey, OnCancel);
|
|
}
|
|
|
|
private void OnCancel(InputAction.CallbackContext obj)
|
|
{
|
|
if(obj.JustPressed() is false)return;
|
|
UXService.Entry<IUXHud>();
|
|
}
|
|
|
|
protected override void OnPanelExit()
|
|
{
|
|
base.OnPanelExit();
|
|
InputActionGroup.UnRegisterCallback(_uxKeyMap.CancelKey, OnCancel);
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
base.Dispose();
|
|
_uxDialogue.OnSubtitle -= OnSubtitle;
|
|
_playerFactory.OnEntityCreated += OnEntityCreated;
|
|
_dialogueService.OnDialogueStart -= OnDialogueStart;
|
|
}
|
|
}
|
|
|
|
|
|
}
|