84 lines
2.6 KiB
C#
84 lines
2.6 KiB
C#
using DG.Tweening;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class MenuLayoutController : MonoBehaviour, IControlable
|
|
{
|
|
[Header("Панели")]
|
|
[SerializeField] List<MenuPanel> _panels;
|
|
int _panelIndex = 0;
|
|
|
|
[Header("Анимация открытия/закрытия")]
|
|
[SerializeField] private RectTransform _leftPanel;
|
|
[SerializeField] private RectTransform _rightPanel;
|
|
[SerializeField] private float _transitionDuration;
|
|
[SerializeField] private Ease _ease;
|
|
private float _leftOpenedPosition;
|
|
private float _rightOpenedPosition;
|
|
|
|
public bool ControlsEnabled { get; set; }
|
|
|
|
public void Initialize()
|
|
{
|
|
foreach (var panel in _panels)
|
|
{
|
|
panel.Initialize();
|
|
panel.Disable(0);
|
|
}
|
|
|
|
_panels[_panelIndex].Enable(0, 0);
|
|
ControlsEnabled = true;
|
|
|
|
_leftOpenedPosition = _leftPanel.anchoredPosition.x;
|
|
_rightOpenedPosition = _rightPanel.anchoredPosition.x;
|
|
}
|
|
|
|
public void UpdateControls()
|
|
{
|
|
int direction = In.GetAxis(Axis.Horizontal, JoystickSide.Both);
|
|
|
|
int newPanelIndex = Mathf.Clamp(_panelIndex + direction, 0, 2);
|
|
bool indexChanged = newPanelIndex != _panelIndex;
|
|
var currentPanel = _panels[_panelIndex];
|
|
|
|
if (indexChanged)
|
|
{
|
|
var nextPanel = _panels[newPanelIndex];
|
|
currentPanel.Disable(0);
|
|
nextPanel.Enable(0, 0);
|
|
|
|
_panelIndex = newPanelIndex;
|
|
}
|
|
|
|
if (In.GetButtonDownAnyPlayer(PlayerAction.action))
|
|
currentPanel.Interact();
|
|
}
|
|
|
|
public Sequence GetOpenSequence() => GetSequenceByState(true);
|
|
public Sequence GetCloseSequence() => GetSequenceByState(false);
|
|
|
|
private Sequence GetSequenceByState(bool open)
|
|
{
|
|
float leftTarget = open ? _leftOpenedPosition : 0;
|
|
float rightTarget = open ? _rightOpenedPosition : 0;
|
|
return GetTransitionSequence(leftTarget, rightTarget);
|
|
}
|
|
|
|
private Sequence GetTransitionSequence(float targetLeftX, float targetRightX)
|
|
{
|
|
if (_leftPanel == null || _rightPanel == null)
|
|
throw new Exception("Панели не назначены!");
|
|
|
|
var sequence = DOTween.Sequence();
|
|
Tween left = _leftPanel.DOAnchorPosX(targetLeftX, _transitionDuration)
|
|
.SetEase(_ease);
|
|
|
|
Tween right = _rightPanel.DOAnchorPosX(targetRightX, _transitionDuration)
|
|
.SetEase(_ease);
|
|
|
|
sequence.Join(left);
|
|
sequence.Join(right);
|
|
return sequence;
|
|
}
|
|
} |