AR_Celement/Assets/Scripts/SwipeScript.cs
2023-01-24 12:07:25 +01:00

138 lines
3.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class SwipeScript : MonoBehaviour
{
public TMP_Text header;
public TMP_Text textSteps;
public List<Text> lengths;
public int numberImage;
public int lengthTexts;
public GameObject arrowLeft;
public GameObject arrowRight;
public Button buttonleft;
public Button buttonright;
public Animator m_Animator;
public TMP_Text textNumber;
public List<int> images;
private Vector2 startTouchPosition;
private Vector2 endTouchPosition;
private GameObject image;
private Image imageUI;
// Start is called before the first frame update
void Start()
{
header.text = "Allumer la Speedy 100";
textSteps.text = lengths[0].text;
textNumber.text = "Step 1/" + (lengthTexts+1);
arrowLeft.gameObject.SetActive(false);
Button btnLeft = buttonleft.GetComponent<Button>();
Button btnRight = buttonright.GetComponent<Button>();
btnLeft.onClick.AddListener(PreviousText);
btnRight.onClick.AddListener(NextText);
}
// Update is called once per frame
void Update()
{
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began){
startTouchPosition = Input.GetTouch(0).position;
}
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended){
endTouchPosition = Input.GetTouch(0).position;
if(endTouchPosition.x < startTouchPosition.x){
NextText();
}else{
PreviousText();
}
if(endTouchPosition.y + 100 < startTouchPosition.y){
SwipeDown();
}
if(endTouchPosition.y - 100 > startTouchPosition.y){
SwipeUp();
}
}
}
private void NextText(){
if(numberImage < lengthTexts)
{
numberImage ++;
if(numberImage == lengthTexts){
arrowRight.gameObject.SetActive(false);
}else{
arrowRight.gameObject.SetActive(true);
}
if(numberImage != 0){
arrowLeft.gameObject.SetActive(true);
}
if(images.Contains(numberImage - 1)){
DisableImageStep();
}
if(images.Contains(numberImage)){
SetImageStep();
}else{
textSteps.text = lengths[numberImage].text;
textNumber.text = "Step" + (numberImage+1) + "/" + (lengthTexts+1);
}
}
}
private void PreviousText(){
if(numberImage > 0) {
numberImage --;
if(numberImage == 0){
arrowLeft.gameObject.SetActive(false);
}else{
arrowLeft.gameObject.SetActive(true);
}
if(numberImage != lengthTexts){
arrowRight.gameObject.SetActive(true);
}
if(images.Contains(numberImage + 1)){
DisableImageStep();
}
if(images.Contains(numberImage)){
SetImageStep();
}else{
textSteps.text = lengths[numberImage].text;
textNumber.text = "Step" + (numberImage+1) + "/" + (lengthTexts+1);
}
}
}
private void SwipeDown(){
m_Animator.ResetTrigger("SwipeUp");
m_Animator.SetTrigger("SwipeDown");
}
private void SwipeUp(){
m_Animator.ResetTrigger("SwipeDown");
m_Animator.SetTrigger("SwipeUp");
}
private void SetImageStep(){
textSteps.text = "";
textNumber.text = "Step" + (numberImage+1) + "/" + (lengthTexts+1);
image = GameObject.Find("Image"+ numberImage);
imageUI = image.GetComponent<Image>();
imageUI.enabled = true;
}
private void DisableImageStep(){
imageUI.enabled = false;
}
}