using System; using System.Collections.Generic; using System.Threading.Tasks; namespace DDD { public class UiManager : Singleton, IManager, IEventHandler, IEventHandler { private readonly Dictionary _popupUIs = new(); private readonly object _uiPauseRequester = new(); public void PreInit() { EventBus.Register(this); EventBus.Register(this); } public Task Init() { return Task.CompletedTask; } public void PostInit() { } private void OnDestroy() { EventBus.Unregister(this); EventBus.Unregister(this); } public void RegisterPopupUI(PopupUi ui) { var type = ui.GetType(); _popupUIs.TryAdd(type, ui); } public void UnregisterPopupUI(PopupUi ui) { var type = ui.GetType(); if (_popupUIs.TryGetValue(type, out var registered) && registered == ui) { _popupUIs.Remove(type); } } public void Invoke(OpenPopupUiEvent evt) { if (_popupUIs.TryGetValue(evt.UiType, out var popup)) { popup.Open(); if (popup.IsBlockingTime) { var timeScaleChangeEvent = GameEvents.RequestTimeScaleChangeEvent; timeScaleChangeEvent.Requester = popup; timeScaleChangeEvent.NewTimeScale = 0f; EventBus.Broadcast(timeScaleChangeEvent); } } } public void Invoke(ClosePopupUiEvent evt) { if (_popupUIs.TryGetValue(evt.UiType, out var popup)) { popup.Close(); if (popup.IsBlockingTime) { var timeScaleChangeEvent = GameEvents.RequestTimeScaleChangeEvent; timeScaleChangeEvent.Requester = popup; timeScaleChangeEvent.NewTimeScale = 1f; EventBus.Broadcast(timeScaleChangeEvent); } } } } }