ProjectDDD/Assets/_DDD/_Scripts/GameUi/UiManager.cs

155 lines
4.8 KiB
C#
Raw Normal View History

2025-07-21 07:53:39 +00:00
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace DDD
{
public class UiManager : Singleton<UiManager>, IManager, IEventHandler<OpenScreenUiEvent>, IEventHandler<CloseScreenUiEvent>,
IEventHandler<OpenPopupUiEvent>, IEventHandler<ClosePopupUiEvent>
{
private readonly Dictionary<Type, ScreenUi> _screenUIs = new();
private readonly Dictionary<Type, PopupUi> _popupUIs = new();
private readonly object _uiPauseRequester = new();
public void PreInit()
{
EventBus.Register<OpenScreenUiEvent>(this);
EventBus.Register<CloseScreenUiEvent>(this);
EventBus.Register<OpenPopupUiEvent>(this);
EventBus.Register<ClosePopupUiEvent>(this);
}
public Task Init()
{
return Task.CompletedTask;
}
public void PostInit()
{
}
private void OnDestroy()
{
EventBus.Unregister<OpenScreenUiEvent>(this);
EventBus.Unregister<CloseScreenUiEvent>(this);
EventBus.Unregister<OpenPopupUiEvent>(this);
EventBus.Unregister<ClosePopupUiEvent>(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)
{
2025-07-22 03:55:04 +00:00
var timeScaleChangeEvent = GameEvents.RequestTimeScaleChangeEvent;
timeScaleChangeEvent.Requester = _uiPauseRequester;
timeScaleChangeEvent.NewTimeScale = 1f;
EventBus.Broadcast(timeScaleChangeEvent);
2025-07-21 07:53:39 +00:00
}
}
}
}
public void Invoke(OpenScreenUiEvent evt)
{
if (_screenUIs.TryGetValue(evt.UiType, out var screen))
{
CloseAllScreenUIs();
screen.Open();
if (screen.IsBlockingTime)
{
2025-07-22 03:55:04 +00:00
var timeScaleChangeEvent = GameEvents.RequestTimeScaleChangeEvent;
timeScaleChangeEvent.Requester = screen;
timeScaleChangeEvent.NewTimeScale = 0f;
EventBus.Broadcast(timeScaleChangeEvent);
2025-07-21 07:53:39 +00:00
}
}
}
public void Invoke(CloseScreenUiEvent evt)
{
if (_screenUIs.TryGetValue(evt.UiType, out var screen))
{
screen.Close();
if (screen.IsBlockingTime)
{
2025-07-22 03:55:04 +00:00
var timeScaleChangeEvent = GameEvents.RequestTimeScaleChangeEvent;
timeScaleChangeEvent.Requester = screen;
timeScaleChangeEvent.NewTimeScale = 1f;
EventBus.Broadcast(timeScaleChangeEvent);
2025-07-21 07:53:39 +00:00
}
}
}
public void Invoke(OpenPopupUiEvent evt)
{
if (_popupUIs.TryGetValue(evt.UiType, out var popup))
{
popup.Open();
if (popup.IsBlockingTime)
{
2025-07-22 03:55:04 +00:00
var timeScaleChangeEvent = GameEvents.RequestTimeScaleChangeEvent;
timeScaleChangeEvent.Requester = popup;
timeScaleChangeEvent.NewTimeScale = 0f;
EventBus.Broadcast(timeScaleChangeEvent);
2025-07-21 07:53:39 +00:00
}
}
}
public void Invoke(ClosePopupUiEvent evt)
{
2025-07-22 03:55:04 +00:00
if (_screenUIs.TryGetValue(evt.UiType, out var popup))
2025-07-21 07:53:39 +00:00
{
2025-07-22 03:55:04 +00:00
popup.Close();
2025-07-21 07:53:39 +00:00
2025-07-22 03:55:04 +00:00
if (popup.IsBlockingTime)
2025-07-21 07:53:39 +00:00
{
2025-07-22 03:55:04 +00:00
var timeScaleChangeEvent = GameEvents.RequestTimeScaleChangeEvent;
timeScaleChangeEvent.Requester = popup;
timeScaleChangeEvent.NewTimeScale = 1f;
EventBus.Broadcast(timeScaleChangeEvent);
2025-07-21 07:53:39 +00:00
}
}
}
}
}