You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

136 lines
4.1 KiB

using System.Collections.Generic;
using JetBrains.Annotations;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Ui.Screens
{
public class ScreenSelector : MonoBehaviour
{
[SerializeField] private Image[] _buttons;
[SerializeField] private Sprite _inactive;
[SerializeField] private float _inactiveSize;
[SerializeField] private Sprite _active;
[SerializeField] private float _activeSize;
[SerializeField] private ScreenContainer _screenContainer;
private Image _activeButton;
private Image _oldButton;
private Vector3 _activeIconScale = new Vector3( 1.45f, 1.45f, 1f);
List<float> m_iconsActiveShift = new List<float> { 78, 76, 71, 76, 76};
private void Awake()
{
_oldButton = _buttons[2];
InitBtnText();
SetIconSizeAndPos(_oldButton, true, false);
GlobalsVar.gScreenSelector = this;
}
private void InitBtnText()
{
for (int i = 0; i < _buttons.Length; ++i)
{
TextMeshProUGUI text = _buttons[i].transform.Find("text").GetComponent<TextMeshProUGUI>();
text.text = GlobalsVar.gGameTextMng.GetGameText(150 + i);
CanvasGroup cg = text.GetComponent<CanvasGroup>();
cg.alpha = 0f;
}
}
[UsedImplicitly]
public void SetScreen(int index)
{
if (GlobalsVar.gSkipClickTime > 0)
return;
_screenContainer.SetNewPosition(index);
}
public void SetButton(int index)
{
var newButton = _buttons[index];
if (newButton == _oldButton)
return;
if (_oldButton != null)
{
SetIconSizeAndPos(_oldButton, false, true);
_oldButton.sprite = _inactive;
var size = _oldButton.rectTransform.sizeDelta;
size.x = _inactiveSize;
_oldButton.rectTransform.sizeDelta = size;
}
_oldButton = newButton;
_oldButton.sprite = _active;
var newSize = _oldButton.rectTransform.sizeDelta;
newSize.x = _activeSize;
_oldButton.rectTransform.sizeDelta = newSize;
SetIconSizeAndPos(_oldButton, true, true);
}
private int GetBtnIndex(Image btn)
{
for (int i = 0; i < _buttons.Length; ++i)
{
if (btn == _buttons[i])
return i;
}
CommonFunctions.myassert(false);
return 0;
}
private void SetIconSizeAndPos(Image btn, bool active, bool needTween)
{
Transform iconTrans = btn.transform.Find("icon");
Transform textTrans = btn.transform.Find("text");
int index = GetBtnIndex(btn);
CanvasGroup cg = textTrans.GetComponent<CanvasGroup>();
if (active)
{
if (btn == _activeButton)
return;
_activeButton = btn;
Vector2 posToMove = iconTrans.localPosition;
posToMove.y += m_iconsActiveShift[index];
if (needTween)
{
LeanTween.alphaCanvas(cg, 1f, 0.5f);
LeanTween.moveLocalY(iconTrans.gameObject, posToMove.y, 0.5f);
LeanTween.scale(iconTrans.gameObject, _activeIconScale, 0.5f);
}
else
{
cg.alpha = 1f;
iconTrans.localPosition = posToMove;
iconTrans.localScale = _activeIconScale;
}
}
else
{
if (needTween)
{
LeanTween.alphaCanvas(cg, 0f, 0.5f);
LeanTween.moveLocalY(iconTrans.gameObject, 0, 0.5f);
LeanTween.scale(iconTrans.gameObject, Vector3.one, 0.5f);
}
else
{
cg.alpha = 0f;
iconTrans.localPosition = Vector3.zero;
iconTrans.localScale = Vector3.one;
}
}
}
}
}