139 lines
4.1 KiB
C#
139 lines
4.1 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace RND
|
|
{
|
|
|
|
public class MarketUI : Window
|
|
{
|
|
private int SectionsCount => _sections.Count;
|
|
|
|
private Transform _shopParent;
|
|
private Transform _tabsParent;
|
|
private GameObject _previewContainer;
|
|
|
|
private ShopTab _tabUITemplate;
|
|
|
|
private readonly List<ShopTab> _tabs = new List<ShopTab>();
|
|
private readonly List<BaseMarketSectionUI> _sections = new List<BaseMarketSectionUI>();
|
|
|
|
private Market _market;
|
|
private int _activeSection = -1;
|
|
|
|
private Button _closeButton;
|
|
|
|
protected override void OnInit()
|
|
{
|
|
_shopParent = transform.FindRecursive("SubWindowsContainer");
|
|
_tabsParent = transform.FindRecursive("TabsContainer");
|
|
|
|
_tabUITemplate = _tabsParent.GetChild(0).GetComponent<ShopTab>();
|
|
_tabUITemplate.gameObject.SetActive(false);
|
|
|
|
_previewContainer = Assets.Create<GameObject>("UI/ItemPreviews/ItemPreviewContainer");
|
|
OnClose += AfterCloseAction;
|
|
|
|
_closeButton = transform.FindRecursive("CloseButton").GetComponent<Button>();
|
|
_closeButton.onClick.AddListener(Close);
|
|
}
|
|
|
|
private void AfterCloseAction(Window _)
|
|
{
|
|
OnClose -= AfterCloseAction;
|
|
foreach (var sectionUI in _sections)
|
|
sectionUI.Close();
|
|
|
|
Destroy(_previewContainer);
|
|
}
|
|
|
|
public void SetMarket(Market market)
|
|
{
|
|
_market = market;
|
|
|
|
Clear();
|
|
CreateSections();
|
|
SetPreviewContainerForSections();
|
|
HideAllSections();
|
|
SetActiveSection(0);
|
|
}
|
|
|
|
private void Clear()
|
|
{
|
|
_sections.Clear();
|
|
_tabs.Clear();
|
|
}
|
|
|
|
private void CreateSections()
|
|
{
|
|
foreach (Market.MarketSection section in _market.Sections)
|
|
CreateSection(section);
|
|
}
|
|
|
|
private void CreateSection(Market.MarketSection section)
|
|
{
|
|
_tabs.Add(CreateTabUI(section.PreviewPath));
|
|
_sections.Add(CreateShopUI(section.Shop));
|
|
}
|
|
|
|
private ShopTab CreateTabUI(string previewPath)
|
|
{
|
|
ShopTab tabUI = Instantiate(_tabUITemplate, _tabsParent);
|
|
tabUI.Init();
|
|
tabUI.SetClickAction(OpenSection);
|
|
tabUI.gameObject.SetActive(true);
|
|
tabUI.SetSpriteFromPath(previewPath);
|
|
_tabs.Add(tabUI);
|
|
|
|
return tabUI;
|
|
}
|
|
|
|
private BaseMarketSectionUI CreateShopUI(BaseShop shop)
|
|
{
|
|
BaseMarketSectionUI baseMarketSectionUI = MarketSectionFactory.CreateSection(shop, _shopParent);
|
|
baseMarketSectionUI.NeedPurchaseConfirmation = _market.ConfirmationForPurchase;
|
|
baseMarketSectionUI.gameObject.SetActive(true);
|
|
_sections.Add(baseMarketSectionUI);
|
|
|
|
return baseMarketSectionUI;
|
|
}
|
|
|
|
private void SetActiveSection(int index)
|
|
{
|
|
if (index == _activeSection)
|
|
return;
|
|
|
|
if (_activeSection != -1)
|
|
HideSection(_activeSection);
|
|
|
|
OpenSection(index);
|
|
_activeSection = index;
|
|
}
|
|
|
|
private void HideAllSections()
|
|
{
|
|
for (int i = 0; i < SectionsCount; i++)
|
|
HideSection(i);
|
|
}
|
|
|
|
private void HideSection(int index)
|
|
{
|
|
_tabs[index].SetActiveState(false);
|
|
_sections[index].Hide();
|
|
}
|
|
|
|
private void OpenSection(int index)
|
|
{
|
|
_tabs[index].SetActiveState(true);
|
|
_sections[index].Open();
|
|
}
|
|
|
|
private void OpenSection(ShopTab tab) => SetActiveSection(_tabs.IndexOf(tab));
|
|
|
|
private void SetPreviewContainerForSections()
|
|
{
|
|
foreach (var section in _sections)
|
|
section.SetPreviewContainer(_previewContainer.transform);
|
|
}
|
|
}
|
|
} |