JD2022-TU1/main/phone_apps/JDCompanionApp/Assets/Scripts/Misc/PauseSlider.cs

127 lines
4.2 KiB
C#

using LightJSON;
using UnityEngine;
using UnityEngine.EventSystems;
using DentedPixel;
using UnityEngine.UI;
namespace Misc
{
public class PauseSlider : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{
public delegate void SlideEventHandler(JSON command);
public static event SlideEventHandler OnSlide;
[Header("Container toggle and toggle to move ")]
public RectTransform containerToggle;
public RectTransform toggleToMove;
[Header("Text to disapear when sliding")]
public Text slideToPauseTxt;
[Header("Blocking the object at min distance percentage")]
[Range(0, 100)]
public float minDistancePercentage;
[Header("Blocking the object at max distance percentage")]
[Range(0, 100)]
public float maxDistancePercentage = 100f;
public JSON command;
[Header("How much value the toggle need to pass to pause")]
[Range(0, 100)]
public float percentageCompletionToPause = 90f;
private float minDistance;
private float maxDistance;
private float completionToPause;
public LeanTweenType tweenPauseType = LeanTweenType.linear;
public float tweenDuration = 0.2f;
private bool canDrag;
private float sizeHalfToggleToMove = 0f;
private float containerWidth = 0f;
private bool containerWidthSet = false;
private void Update()
{
if(containerWidthSet)
return;
if(containerWidth == 0) {
containerWidth = containerToggle.GetComponent<RectTransform>().rect.width;
return;
}
containerWidthSet = true;
CalculateDistance();
}
private void CalculateDistance()
{
minDistance = ((minDistancePercentage / 100) * containerWidth);
maxDistance = ((maxDistancePercentage / 100) * containerWidth);
completionToPause = ((percentageCompletionToPause / 100) * containerWidth);
sizeHalfToggleToMove = (toggleToMove.sizeDelta.x / 2);
toggleToMove.anchoredPosition = new Vector2(minDistance + sizeHalfToggleToMove, toggleToMove.anchoredPosition.y);
}
#region IDragHandlers
//IBeginDragHandler
public void OnBeginDrag(PointerEventData eventData)
{
canDrag = (eventData.pointerEnter.name == toggleToMove.name);
slideToPauseTxt.gameObject.SetActive(!canDrag);
}
//IDragHandler
public void OnDrag(PointerEventData eventData)
{
if(!canDrag)
return;
float x = eventData.delta.x + toggleToMove.anchoredPosition.x;
float halfWidthToggleToMove = (toggleToMove.sizeDelta.x / 2f);
x = Mathf.Clamp(x, minDistance + halfWidthToggleToMove, maxDistance - halfWidthToggleToMove);
toggleToMove.anchoredPosition = new Vector2(x, toggleToMove.anchoredPosition.y);
}
//IEndDragHandler
public void OnEndDrag(PointerEventData eventData)
{
if(!canDrag)
return;
canDrag = false;
// If the element has overcome the % value, we pause the game.
if((toggleToMove.anchoredPosition.x > completionToPause) && command != null && OnSlide != null) {
OnSlide(command);
}
else {
_TweenToStartPos();
}
}
private void _TweenToStartPos()
{
// Otherwise, we do some animation to tween out
float startValue = toggleToMove.anchoredPosition.x;
float finalValue = minDistance + sizeHalfToggleToMove;
LeanTween.value(gameObject, (val) => {
var intVal = (int)val;
toggleToMove.anchoredPosition = new Vector2(intVal, toggleToMove.anchoredPosition.y);
}, startValue, finalValue, tweenDuration)
.setEase(tweenPauseType)
.setOnComplete(() => {
slideToPauseTxt.gameObject.SetActive(!canDrag);
});
}
}
#endregion
} // namespace JDCompanionApp.Misc