140 lines
3.8 KiB
C#
140 lines
3.8 KiB
C#
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)
|
|
{
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |