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 _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 _uxKeyMap; private readonly ConcurrentDictionary _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 uxKeyMap, IDialogueService dialogueService, ILocalizationService localizationService, IEntitiesService entitiesService, IPlayerFactory playerFactory, ILogger 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("ui_esc_menu_dialogue-template"); _dialogueContainer.Clear(); _timeSlider.RegisterValueChangedCallback(OnTime); } public override void OnStateEntry(IState old) { base.OnStateEntry(old); var enable = _entitiesService.QueryComponents().Length > 0; _timeSlider.SetEnabled(enable); _timeSlider.tooltip = enable ? string.Empty : "这个地图没有开启昼夜系统"; } private void OnTime(ChangeEvent evt) { var controllers = _entitiesService.QueryComponents(); 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()) { if (idComponent.Id == arg.ActorIdentity) { actorName = idComponent.Name; throw new OperationCanceledException(); } } } try { var players = _entitiesService.QueryComponents().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(isPlayer ? Align.FlexEnd : Align.FlexStart); container.Get