Aimbot-ParallelEnv/Assets/Script/UI/LevelPanel.cs
2023-10-23 01:54:30 +09:00

96 lines
4.0 KiB
C#

using DG.Tweening;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UnityEngine;
using UnityEngine.UI;
public class LevelPanel : MonoBehaviour
{
private int levelNum = 0;
private float buttonHeight = 30;
[SerializeField] private TargetUIController.PrimaryButtonType primaryButtonType;
[SerializeField] private GameObject levelButtonPrefab;
[SerializeField] private GameObject hudObj;
private TargetUIController targetUIController;
public Vector2 defaultPosition = Vector2.zero;
public Vector2 targetPosition = Vector2.zero;
public Vector2 defaultSize = Vector2.zero;
public Vector2 targetSize = Vector2.zero;
public bool isFolding = false;
public bool isExpanding = false;
private List<Button> levelButtonList = new List<Button>();
public ReadOnlyCollection<Button> LevelButtonList
{
get { return levelButtonList.AsReadOnly(); }
}
/// <summary>
/// Initialize the level panel.
/// Set the level number, button height, default position and target position.
/// Create leve buttonInitialize the level buttons.
/// </summary>
public void InitLevelPanel(int levelNum, Button parentButton, float buttonHeight = 30)
{
targetUIController = hudObj.GetComponent<TargetUIController>();
// set level panel parameters
this.levelNum = levelNum;
this.buttonHeight = buttonHeight;
defaultSize = new Vector2(parentButton.GetComponent<RectTransform>().sizeDelta.x, 0); ;
defaultPosition = parentButton.GetComponent<RectTransform>().anchoredPosition;
targetSize = new Vector2(defaultSize.x, buttonHeight * levelNum);
targetPosition = new Vector2(-parentButton.GetComponent<RectTransform>().sizeDelta.x,
parentButton.GetComponent<RectTransform>().anchoredPosition.y + (levelNum - 1) * buttonHeight / 2);
// limit the target position is screen range
if (targetPosition.y > 0)
{
targetPosition.y = 0;
}
// create level buttons
for (int i = 0; i < levelNum; i++)
{
GameObject newButton = Instantiate(levelButtonPrefab, transform);
levelButtonList.Add(newButton.GetComponent<Button>());
newButton.GetComponent<LevelButton>().Initialization(i);
newButton.GetComponent<Button>().onClick.AddListener(delegate { targetUIController.LevelButtonPressed(primaryButtonType, newButton.GetComponent<LevelButton>().level); });
}
// fold panel to default position immediately
FoldLevelPanel();
}
/// <summary>
/// fold the level panel.
/// </summary>
/// <param name="animeTime">fold animation time,0 without animation</param>
public void FoldLevelPanel(float animeTime = 0)
{
isFolding = true;
DOTween.To(() => gameObject.GetComponent<RectTransform>().anchoredPosition,
x => gameObject.GetComponent<RectTransform>().anchoredPosition = x,
defaultPosition,
animeTime).SetEase(Ease.OutCirc).Play().OnComplete(() => isFolding = false);
DOTween.To(() => gameObject.GetComponent<RectTransform>().sizeDelta,
x => gameObject.GetComponent<RectTransform>().sizeDelta = x,
defaultSize,
animeTime).SetEase(Ease.OutCirc).Play();
}
/// <summary>
/// expand the level panel.
/// </summary>
/// <param name="animeTime">expand animation time,0 without animation</param>
public void ExpandLevelPanel(float animeTime = 0)
{
isExpanding = true;
DOTween.To(() => gameObject.GetComponent<RectTransform>().anchoredPosition,
x => gameObject.GetComponent<RectTransform>().anchoredPosition = x,
targetPosition,
animeTime).SetEase(Ease.OutCirc).Play().OnComplete(() => isExpanding = false);
DOTween.To(() => gameObject.GetComponent<RectTransform>().sizeDelta,
x => gameObject.GetComponent<RectTransform>().sizeDelta = x,
targetSize,
animeTime).SetEase(Ease.OutCirc).Play();
}
}