74 lines
2.5 KiB
C#
74 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Code.Infrastructure.EventBus;
|
|
using Reflex.Core;
|
|
using Reflex.Extensions;
|
|
using Reflex.Injectors;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
namespace Code.Infrastructure.UI
|
|
{
|
|
public interface IUIService
|
|
{
|
|
void OpenPanel(UIPanelType uiPanelType);
|
|
void ClosePanel(UIPanelType uiPanelType);
|
|
void TryLoadPanel(UIPanelType uiPanelType);
|
|
void TryRemovePanel(UIPanelType panelType);
|
|
}
|
|
|
|
public class UIService : IUIPanelLoadedHandle, IUIService
|
|
{
|
|
private Dictionary<UIPanelType, UIPanel> _loadedPanels = new();
|
|
|
|
private EventBus.EventBus _eventBus;
|
|
private IUIFactory _factory;
|
|
|
|
public UIService(EventBus.EventBus eventBus, IUIFactory factory)
|
|
{
|
|
_eventBus = eventBus;
|
|
_factory = factory;
|
|
eventBus.Subscribe(this);
|
|
}
|
|
|
|
public void OpenPanel(UIPanelType uiPanelType)
|
|
{
|
|
_loadedPanels[uiPanelType].gameObject.SetActive(true); // TODO Братуха реально реши надо вкл\выкл или это очередная хренооптимизация
|
|
_loadedPanels[uiPanelType].Open();
|
|
}
|
|
|
|
public void ClosePanel(UIPanelType uiPanelType)
|
|
{
|
|
_loadedPanels[uiPanelType].gameObject.SetActive(false);
|
|
_loadedPanels[uiPanelType].Close();
|
|
}
|
|
|
|
public void TryLoadPanel(UIPanelType uiPanelType) => _factory.LoadPanel(uiPanelType);
|
|
|
|
public void UIPanelLoaded(UIPanel panel, UIPanelType panelType)
|
|
{
|
|
_loadedPanels.Add(panelType, panel);
|
|
|
|
if(panel.InjectMeSenpai)
|
|
GameObjectInjector.InjectRecursive(panel.gameObject, Container.RootContainer);
|
|
// GameObjectInjector.InjectRecursive(panel.gameObject, SceneManager.GetActiveScene().GetSceneContainer()); // Скорее всего будут нужны компоненты текущей сцены
|
|
|
|
_eventBus.RaiseEvent<IUIPanelLoaded>(ui=> ui.UILoaded(panelType));
|
|
}
|
|
|
|
public void TryRemovePanel(UIPanelType panelType)
|
|
{
|
|
// TODO Close it
|
|
RemovePanel(panelType);
|
|
}
|
|
|
|
private void RemovePanel(UIPanelType panelType)
|
|
{
|
|
_factory.ReleasePanel(_loadedPanels[panelType]);
|
|
_loadedPanels.Remove(panelType);
|
|
}
|
|
}
|
|
public interface IUIPanelLoaded : IGlobalSubscriber
|
|
{
|
|
void UILoaded(UIPanelType uiPanelType);
|
|
}
|
|
} |