ProjectDDD/Assets/_DDD/_Scripts/GameUi/UiManager.cs
2025-07-22 13:09:38 +09:00

82 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace DDD
{
public class UiManager : Singleton<UiManager>, IManager, IEventHandler<OpenPopupUiEvent>, IEventHandler<ClosePopupUiEvent>
{
private readonly Dictionary<Type, PopupUi> _popupUIs = new();
private readonly object _uiPauseRequester = new();
public void PreInit()
{
EventBus.Register<OpenPopupUiEvent>(this);
EventBus.Register<ClosePopupUiEvent>(this);
}
public Task Init()
{
return Task.CompletedTask;
}
public void PostInit()
{
}
private void OnDestroy()
{
EventBus.Unregister<OpenPopupUiEvent>(this);
EventBus.Unregister<ClosePopupUiEvent>(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);
}
}
}
}
}