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

76 lines
2.0 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using System.Collections;
public class MenuPanelsScroll : MenuPanel
{
[SerializeField] List<ClosablePanel> panels;
[SerializeField] CanvasGroup canvasGroup;
[SerializeField] SegmentedScrollbar scrollbar;
private int verticalIndex = 0;
private ClosablePanel currentPanel => panels[verticalIndex];
private bool m_enabled = true;
private bool isTransitioning = false;
public override void Initialize()
{
scrollbar.Init(panels.Count);
scrollbar.SetActiveSegment(verticalIndex);
foreach (var panel in panels)
{
panel.Disable(1);
}
currentPanel.Enable(-1, 0);
}
private void Update()
{
if (!m_enabled || isTransitioning) return;
int dir_v = 0;
dir_v += In.GetButtonDownAnyPlayer(PlayerAction.up) ? -1 : 0;
dir_v += In.GetButtonDownAnyPlayer(PlayerAction.down) ? +1 : 0;
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;
}
public override void Interact() => currentPanel.Interact();
}