using System; using System.Collections.Generic; using System.Threading.Tasks; namespace DDD { public class UiManager : Singleton, IManager, IEventHandler, IEventHandler, IEventHandler, IEventHandler { private readonly Dictionary _screenUIs = new(); private readonly Dictionary _popupUIs = new(); private readonly object _uiPauseRequester = new(); public void PreInit() { EventBus.Register(this); EventBus.Register(this); EventBus.Register(this); EventBus.Register(this); } public Task Init() { return Task.CompletedTask; } public void PostInit() { } private void OnDestroy() { EventBus.Unregister(this); EventBus.Unregister(this); EventBus.Unregister(this); EventBus.Unregister(this); } public void RegisterScreenUI(ScreenUi ui) { var type = ui.GetType(); _screenUIs.TryAdd(type, ui); } public void UnregisterScreenUI(ScreenUi ui) { var type = ui.GetType(); if (_screenUIs.TryGetValue(type, out var value) && value == ui) { _screenUIs.Remove(type); } } 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); } } private void CloseAllScreenUIs() { foreach (var screen in _screenUIs.Values) { if (screen.IsOpen) { screen.Close(); if (screen.IsBlockingTime) { EventBus.Broadcast(new TimeScaleChangeEvent(_uiPauseRequester, 1f)); } } } } public void Invoke(OpenScreenUiEvent evt) { if (_screenUIs.TryGetValue(evt.UiType, out var screen)) { CloseAllScreenUIs(); screen.Open(); if (screen.IsBlockingTime) { EventBus.Broadcast(new TimeScaleChangeEvent(screen, 0f)); } } } public void Invoke(CloseScreenUiEvent evt) { if (_screenUIs.TryGetValue(evt.UiType, out var screen)) { screen.Close(); if (screen.IsBlockingTime) { EventBus.Broadcast(new TimeScaleChangeEvent(screen, 1f)); } } } public void Invoke(OpenPopupUiEvent evt) { if (_popupUIs.TryGetValue(evt.UiType, out var popup)) { popup.Open(); if (popup.IsBlockingTime) { EventBus.Broadcast(new TimeScaleChangeEvent(popup, 0f)); } } } public void Invoke(ClosePopupUiEvent evt) { if (_screenUIs.TryGetValue(evt.UiType, out var popUp)) { popUp.Close(); if (popUp.IsBlockingTime) { EventBus.Broadcast(new TimeScaleChangeEvent(popUp, 1f)); } } } } }