chip-gameV3/Assets/Scripts/Menu/Panels/MenuPanelsScroll.cs

105 lines
2.8 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using System.Collections;
public class MenuPanelsScroll : MenuPanel
{
[SerializeField] private Transform _panelsRoot;
[SerializeField] private List<ClosablePanel> _panelPrefabs;
[SerializeField] private PrizesPanel _prizesPanel;
[SerializeField] private CanvasGroup canvasGroup;
[SerializeField] private SegmentedScrollbar scrollbar;
private List<ClosablePanel> _panels;
private int verticalIndex = 0;
private ClosablePanel currentPanel => _panels[verticalIndex];
private bool m_enabled = true;
private bool isTransitioning = false;
public override void Initialize()
{
BuildLayout();
SettingsManager.OnSettingsChanged += BuildLayout;
}
private void BuildLayout()
{
Clear();
verticalIndex = 0;
bool prizing = SettingsManager.GetBool(SettingKey.PRIZING);
foreach (var panel in _panelPrefabs)
{
if (panel == _prizesPanel && prizing == false)
continue;
var panelInst = Instantiate(panel, _panelsRoot);
panelInst.GetComponent<RectTransform>().anchoredPosition = Vector3.up * 1000;
_panels.Add(panelInst);
panelInst.Disable(1);
}
currentPanel.Enable(-1, 0);
scrollbar.Init(_panels.Count);
scrollbar.SetActiveSegment(verticalIndex);
}
private void Update()
{
if (!m_enabled || isTransitioning) return;
int dir_v = -In.GetAxis(Axis.Vertical, JoystickSide.Both);
if (dir_v != 0)
StartCoroutine(SwitchPanelCoroutine(dir_v));
}
private IEnumerator SwitchPanelCoroutine(int direction)
{
isTransitioning = true;
float closeTime = currentPanel.closeDelay;
currentPanel.Disable(direction);
yield return new WaitForSeconds(closeTime);
int newVerticalIndex = (verticalIndex + direction + _panels.Count) % _panels.Count;
verticalIndex = newVerticalIndex;
currentPanel.Enable(direction, 0);
scrollbar.SetActiveSegment(verticalIndex);
yield return new WaitForSeconds(currentPanel.closeDelay * currentPanel.closeSpeedMultiplier/2);
isTransitioning = false;
}
public override void Disable(int _)
{
canvasGroup.alpha = .5f;
m_enabled = false;
//StopAllCoroutines();
}
public override void Enable(int _, float delay)
{
canvasGroup.alpha = 1f;
m_enabled = true;
}
private void Clear()
{
for (int i = _panelsRoot.childCount - 1; i >= 0; i--)
{
var child = _panelsRoot.GetChild(i);
Destroy(child.gameObject);
}
_panels = new();
verticalIndex = 0;
}
public override void Interact() => currentPanel.Interact();
}