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.
164 lines
3.7 KiB
164 lines
3.7 KiB
using System; |
|
using DG.Tweening; |
|
using UnityEngine; |
|
using Utils; |
|
|
|
namespace Ui.Screens |
|
{ |
|
[RequireComponent(typeof(RectTransform))] |
|
public class ScreenContainer : MonoBehaviour |
|
{ |
|
private static ScreenContainer _instance; |
|
|
|
public static ScreenContainer Instance |
|
{ |
|
get { return _instance; } |
|
} |
|
|
|
[SerializeField] private ScreenSelector _selector; |
|
[SerializeField] private RectTransform _container; |
|
[SerializeField] private int _rectWidth; |
|
[SerializeField] private float _threshold; |
|
[SerializeField] private int _leftScreens; |
|
[SerializeField] private int _rightScreens; |
|
private bool _dragStart; |
|
private static bool _hold; |
|
public static bool Hold |
|
{ |
|
get { return _hold; } |
|
set |
|
{ |
|
_hold = value; |
|
if (_hold) |
|
Debug.Log("Hold true!"); |
|
else |
|
Debug.Log("Hold false!"); |
|
} |
|
} |
|
private int _scene; |
|
|
|
private float _position; |
|
private float _containerPosition; |
|
private float _currentPosition; |
|
|
|
private float ContainerPosition |
|
{ |
|
get { return _containerPosition; } |
|
set |
|
{ |
|
var targetValue = value < 0 && value < -_leftScreens * _rectWidth ? -_leftScreens * _rectWidth : |
|
value > 0 && value > _rightScreens * _rectWidth ? _rightScreens * _rectWidth : value; |
|
_container.SetRight(-targetValue); |
|
_container.SetLeft(targetValue); |
|
_containerPosition = targetValue; |
|
} |
|
} |
|
|
|
private void Awake() |
|
{ |
|
_instance = this; |
|
} |
|
|
|
private void OnDisable() |
|
{ |
|
_hold = false; |
|
} |
|
|
|
public static event Action<bool> DragEvent; |
|
|
|
private void Drag() |
|
{ |
|
if (GlobalsVar.gBoard.DialogsCount > 0) |
|
return; |
|
|
|
if (_dragStart) return; |
|
_dragStart = true; |
|
_position = Input.mousePosition.x; |
|
} |
|
|
|
private void Update() |
|
{ |
|
if (GlobalsVar.gSkipClickTime > 0f) |
|
{ |
|
if (_dragStart) |
|
_dragStart = false; |
|
|
|
return; |
|
} |
|
|
|
if (_hold) |
|
{ |
|
if (_dragStart) |
|
{ |
|
GlobalsVar.gOnMenuDrag = true; |
|
SetNewPosition(); |
|
} |
|
return; |
|
} |
|
|
|
if (Input.GetMouseButton(0)) |
|
{ |
|
Drag(); |
|
} |
|
|
|
if (!_dragStart) |
|
return; |
|
|
|
if (!Input.GetMouseButton(0)) |
|
{ |
|
SetNewPosition(); |
|
return; |
|
} |
|
|
|
ChangeDragPosition(); |
|
} |
|
|
|
private void SetNewPosition() |
|
{ |
|
_dragStart = false; |
|
var targetScene = Input.mousePosition.x - _position < -(_threshold * Screen.width) ? _scene - 1 : |
|
Input.mousePosition.x - _position > (_threshold * Screen.width) ? _scene + 1 : _scene; |
|
SetNewPosition(targetScene); |
|
} |
|
|
|
public void SetNewPosition(int scene) |
|
{ |
|
_scene = scene > _rightScreens ? _rightScreens : scene < -_leftScreens ? -_leftScreens : scene; |
|
_hold = true; |
|
Debug.Log("Hold true!"); |
|
_currentPosition = _rectWidth * _scene; |
|
DOTween.To(() => ContainerPosition, x => ContainerPosition = x, _rectWidth * _scene, 0.5f).OnComplete(() => |
|
{ |
|
GlobalsVar.gOnMenuDrag = false; |
|
_hold = false; |
|
Debug.Log("Hold false!"); |
|
_dragEvent = false; |
|
if (DragEvent != null) |
|
DragEvent(false); |
|
}); |
|
|
|
if (_selector != null) |
|
_selector.SetButton(_scene + 2); |
|
} |
|
|
|
private bool _dragEvent; |
|
|
|
private void ChangeDragPosition() |
|
{ |
|
var currentPoint = _currentPosition + (Input.mousePosition.x - _position) * (_rectWidth / (float)Screen.width); |
|
if (Mathf.Abs(Input.mousePosition.x - _position) > 0.05 * Screen.width && !_dragEvent) |
|
{ |
|
_dragEvent = true; |
|
if (DragEvent != null) |
|
DragEvent(true); |
|
} |
|
ContainerPosition = currentPoint; |
|
} |
|
|
|
private void OnValidate() |
|
{ |
|
if (_container == null) |
|
_container = gameObject.GetComponent<RectTransform>(); |
|
} |
|
} |
|
} |