138 lines
4.0 KiB
C#
138 lines
4.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
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 Stack<PopupUi> _popupStack = new();
|
|
private InputActionMaps _previousActionMap = InputActionMaps.None;
|
|
|
|
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))
|
|
{
|
|
if (!popup.IsOpen)
|
|
{
|
|
popup.Open();
|
|
PushPopup(popup);
|
|
|
|
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))
|
|
{
|
|
if (popup.IsOpen)
|
|
{
|
|
popup.Close();
|
|
PopPopup(popup);
|
|
|
|
if (popup.IsBlockingTime)
|
|
{
|
|
var timeScaleChangeEvent = GameEvents.RequestTimeScaleChangeEvent;
|
|
timeScaleChangeEvent.Requester = popup;
|
|
timeScaleChangeEvent.NewTimeScale = 1f;
|
|
EventBus.Broadcast(timeScaleChangeEvent);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool IsTopPopup(PopupUi popup)
|
|
{
|
|
return _popupStack.Count > 0 && _popupStack.Peek() == popup;
|
|
}
|
|
|
|
public void PushPopup(PopupUi popup)
|
|
{
|
|
if (_popupStack.Contains(popup)) return;
|
|
|
|
if (_popupStack.Count == 0)
|
|
{
|
|
_previousActionMap = InputManager.Instance.GetCurrentActionMap();
|
|
}
|
|
|
|
_popupStack.Push(popup);
|
|
}
|
|
|
|
public void PopPopup(PopupUi popup)
|
|
{
|
|
if (_popupStack.Count == 0) return;
|
|
|
|
if (_popupStack.Peek() == popup)
|
|
{
|
|
_popupStack.Pop();
|
|
}
|
|
else
|
|
{
|
|
var temp = _popupStack.Reverse().Where(p => p != popup).Reverse().ToList();
|
|
_popupStack.Clear();
|
|
foreach (var p in temp)
|
|
{
|
|
_popupStack.Push(p);
|
|
}
|
|
}
|
|
|
|
if (_popupStack.TryPeek(out var topPopup) && topPopup.IsOpen)
|
|
{
|
|
InputManager.Instance.SwitchCurrentActionMap(topPopup.GetInputActionMaps());
|
|
}
|
|
else
|
|
{
|
|
InputManager.Instance.SwitchCurrentActionMap(_previousActionMap);
|
|
}
|
|
}
|
|
}
|
|
} |