Files
unity-application/Assets/Courses/Scripts/Feedback.cs
2023-03-18 19:53:17 +00:00

123 lines
4.7 KiB
C#

//using Mediapipe.Unity.Tutorial;
using Mediapipe.Unity.Tutorial;
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
// for your own scripts make sure to add the following line:
using DigitalRuby.Tween;
using UnityEngine.SceneManagement;
namespace Assets.Courses.Scripts
{
public class Feedback : MonoBehaviour
{
/// <summary>
/// Reference to the feedback field
/// </summary>
public TMP_Text feedback;
/// <summary>
/// Reference to the sign predictor
/// </summary>
public Wesign_extractor signPredictor;
/// <summary>
/// Reference to the TemplateCourse
/// </summary>
public TemplateCourse templateCourse;
/// <summary>
/// Reference to the progress bar
/// </summary>
public GameObject progress;
/// <summary>
/// Start is called before the first frame update
/// </summary>
void Start()
{
// Start the coroutine to update the scale every 200 milliseconds
StartCoroutine(UpdateFeedback());
}
/// <summary>
/// UpdateScale updates the progress bar every 200ms, updated the feedback text, and progress bar color
/// If a high enough accuracy is detected, it will go to the next sign
/// </summary>
/// <returns></returns>
IEnumerator UpdateFeedback()
{
while (true)
{
// Get current sign
char currentSign = (char)(65 + templateCourse.GetWordIndex());
//Debug.Log(currentSign);
// Get the predicted sign
if (signPredictor != null && signPredictor.letterProbabilities != null && signPredictor.letterProbabilities.ContainsKey(currentSign))
{
int accuracy = (int)(signPredictor.letterProbabilities[currentSign] * 100);
if (accuracy > 98)
{
feedback.text = "Perfect!!!";
feedback.color = Color.green;
progress.GetComponent<Image>().color = Color.green;
}
else if (accuracy > 95)
{
feedback.text = "Super!";
feedback.color = Color.green;
progress.GetComponent<Image>().color = Color.green;
}
else if (accuracy > 90)
{
feedback.text = "Goed";
feedback.color = Color.green;
progress.GetComponent<Image>().color = Color.green;
}
else if (accuracy > 80)
{
feedback.text = "Bijna...";
feedback.color = new Color(0xFF, 0xE5, 0x00);
progress.GetComponent<Image>().color = new Color(0xFF, 0xE5, 0x00);
}
else
{
feedback.text = "Detecteren ...";
feedback.color = Color.red;
progress.GetComponent<Image>().color = Color.red;
}
// use an exponential scale
float newScale = Mathf.Exp(((float)accuracy / 24.5f) - 4);
Vector3 newScaleVector = new Vector3(newScale,
progress.transform.localScale.y,
progress.transform.localScale.z);
System.Action<ITween<Vector3>> updateProgressScale = (t) =>
{
if (progress != null)
{
progress.transform.localScale = t.CurrentValue;
}
};
progress.Tween("ScaleProgress", progress.transform.localScale, newScaleVector, 0.2f, TweenScaleFunctions.CubicEaseInOut, updateProgressScale);
if (accuracy > 90)
{
// Wait and go to next sign
yield return new WaitForSeconds(1);
templateCourse.NextSign();
}
}
else
{
progress.transform.localScale = new Vector3(0f, progress.transform.localScale.y, progress.transform.localScale.z);
//Debug.Log("doesn't contain A");
}
// Wait for 200 milliseconds before updating the scale again
yield return new WaitForSeconds(0.2f);
}
}
}
}