Files
unity-application/Assets/Courses/Scripts/CoursesController.cs
2023-04-02 12:27:59 +00:00

405 lines
13 KiB
C#

using DigitalRuby.Tween;
using System;
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
/// <summary>
/// TemplateCourse scene manager
/// </summary>
public class CoursesController : AbstractFeedback
{
public GameObject feedbackProgressBar;
public GameObject previewMessage;
/// <summary>
/// Reference to instructional video player
/// </summary>
public VideoPlayer player;
/// <summary>
/// Reference to pause button
/// </summary>
public Button button;
/// <summary>
/// Reference to sprite for the pause button
/// </summary>
public Sprite pauseSprite;
/// <summary>
/// Reference to the image for displaying the current words sprite
/// </summary>
public Image wordImage;
/// <summary>
/// Reference to the text object for displaying the current word
/// </summary>
public TMP_Text title;
/// <summary>
/// Reference to user list to get current user
/// </summary>
public UserList userList;
/// <summary>
/// The current user
/// </summary>
private User user;
/// <summary>
/// Current user progress for this course
/// </summary>
private Progress progress = null;
/// <summary>
/// ScriptableObject with list of all courses
/// </summary>
public CourseList courselist;
/// <summary>
/// Reference to Course ScriptableObject
/// </summary>
private Course course;
/// <summary>
/// Index of the current word/letter in the course.learnables list
/// </summary>
private int currentWordIndex = 0;
/// <summary>
/// This holds the amount of words in the course
/// </summary>
private int maxWords;
/// <summary>
/// Number of correct words so far
/// (can be modified to a list or something like that to give better feedback)
/// </summary>
private int correctWords = 0;
/// <summary>
/// The "finished" screen
/// </summary>
public GameObject ResultPanel;
/// <summary>
/// Reference to the title on the results panel
/// </summary>
public TMP_Text ResultsTitle;
/// <summary>
/// Reference to the description on the results panel
/// </summary>
public TMP_Text ResultsDecription;
/// <summary>
/// Button to go back to courses list
/// </summary>
public Button CoursesButton;
/// <summary>
/// DateTime containint the start moment
/// </summary>
private DateTime startMoment;
/// <summary>
/// Reference to the timeSpent UI
/// </summary>
public TMP_Text timeSpent;
/// <summary>
/// Reference to the feedback field
/// </summary>
public TMP_Text feedbackText;
/// <summary>
/// Reference to the progress bar
/// </summary>
public Slider feedbackProgress;
/// <summary>
/// Reference to the progress bar image, so we can add fancy colors
/// </summary>
public Image feedbackProgressImage;
/// <summary>
/// Timer to keep track of how long a incorrect sign is performed
/// </summary>
protected DateTime timer;
/// <summary>
/// Current predicted sign
/// </summary>
protected string predictedSign = null;
/// <summary>
/// Previous incorrect sign, so we can keep track whether the user is wrong or the user is still changing signs
/// </summary>
protected string previousIncorrectSign = null;
/// <summary>
/// This function is called when the script is initialised.
/// It inactivatis the popup, finds a webcam to use and links it via the WebcamTexture to the display RawImage.
/// It takes the correct course from the courselist, using the courseIndex.
/// Then it checks whether or not the User has started the course yet, to possibly create a new progress atribute for the course.
/// Then it sets up the course-screen to display relevant information from the course-scriptable.
/// </summary>
void Start()
{
StartCourseController();
signPredictor.SetModel(course.theme.modelIndex);
AddSelfAsListener();
}
/// <summary>
/// Holds the course-specific logic to start the controller, it is seperated to allow the course to be reset (if that would become needed)
/// </summary>
public void StartCourseController()
{
// Setting up course
course = courselist.courses[courselist.currentCourseIndex];
maxWords = course.theme.learnables.Count;
// Show preview messages if there is no model
feedbackProgressBar.SetActive(course.theme.modelIndex != ModelIndex.NONE);
previewMessage.SetActive(course.theme.modelIndex == ModelIndex.NONE);
// Create entry in current user for keeping track of progress
userList.Load();
user = userList.GetCurrentUser();
progress = user.GetCourseProgress(course.index);
if (progress == null)
{
progress = new Progress();
progress.AddOrUpdate<CourseIndex>("courseIndex", course.index);
progress.AddOrUpdate<float>("courseProgress", -1.0f);
user.courses.Add(progress);
}
userList.Save();
// Force the videoplayer to add bars to preserve aspect ratio
player.aspectRatio = VideoAspectRatio.FitInside;
// Setup UI
button.image.sprite = pauseSprite;
title.text = course.title;
NextVideo();
NextImage();
// Hide the result panel
ResultPanel.SetActive(false);
// Set the startTime
startMoment = DateTime.Now;
}
/// <summary>
/// This function uses the word_i integer to grab the correct video from the course.learnabels.
/// When it has this video, it will load it into the videoplayer and set it to start.
/// </summary>
private void NextVideo()
{
player.clip = course.theme.learnables[currentWordIndex].clip;
// This loads first frame, so that it can be used as a sort-of preview for the video
player.Play();
// As the video will start playiing -> hide button
Color col = button.image.color;
col.a = 0;
button.image.color = col;
}
/// <summary>
/// This function uses the word_i integer to grab the correct image from the course.learnabels.
/// Then it simply loads it into wordImage so that it can be displayed.
/// </summary>
private void NextImage()
{
wordImage.sprite = course.theme.learnables[currentWordIndex].image;
}
/// <summary>
/// This function is called when the pause-button is pressed on the video.
/// It switches between playing and pausing the video.
/// It then makes the button invisible when the video is playing, or visible when it's paused.
/// </summary>
public void Pause()
{
if (!player.isPlaying)
{
// Play video and hide button
player.Play();
Color col = button.image.color;
col.a = 0;
button.image.color = col;
}
else
{
// Pause video and show button
player.Pause();
Color col = button.image.color;
col.a = 255;
button.image.color = col;
}
}
/// <summary>
/// This function is called when the next-sign button is pressed.
/// It increased the wordindex and fetches new videos/images if index<max, because then the coure is not fincished yet.
/// If the maximum is reached, finishcourse is called to save the "finished" progress to the user.
/// </summary>
public void NextSign()
{
// If the currentindex >= maxwords, it indicated that the course is already finished, running the next code is the meaningless.
if (currentWordIndex >= maxWords) { return; }
// Goto the next word/letter
currentWordIndex++;
// TODO: fix correct word count
correctWords++;
progress.AddOrUpdate<float>("courseProgress", (float)correctWords / (float)maxWords);
userList.Save();
// Update UI if course is not finished yet
if (currentWordIndex < maxWords)
{
NextVideo();
NextImage();
}
// Finish course and record progress
else
{
FinishCourse();
}
}
/// <summary>
/// finishcourse is called to save the "finished" progress to the user.
/// </summary>
public void FinishCourse()
{
// Show the "finished" screen
ResultPanel.SetActive(true);
// Set the correct title
ResultsTitle.text = course.title + " voltooid!";
// Set the correct description
ResultsDecription.text = "Goed gedaan! Je kan nu spelletjes spelen met " + course.title + " om verder te oefenen!";
// Set the total time spent UI
TimeSpan time = DateTime.Now - startMoment;
timeSpent.text = time.ToString(@"hh\:mm\:ss");
// Link button
CoursesButton.onClick.AddListener(() => { SystemController.GetInstance().BackToPreviousScene(); });
progress.AddOrUpdate<float>("courseProgress", 1f);
userList.Save();
}
/// <summary>
/// The updateFunction that is called when new probabilities become available
/// </summary>
/// <returns></returns>
protected override IEnumerator UpdateFeedback()
{
// Get current sign
string currentSign = course.theme.learnables[currentWordIndex].name;
// Get the predicted sign
if (signPredictor != null && signPredictor.learnableProbabilities != null &&
currentSign != null && signPredictor.learnableProbabilities.ContainsKey(currentSign))
{
float accuracy = signPredictor.learnableProbabilities[currentSign];
if (feedbackText != null && feedbackProgressImage != null)
{
if (accuracy > 0.90)
{
feedbackText.text = "Goed";
feedbackText.color = Color.green;
feedbackProgressImage.color = Color.green;
}
else if (accuracy > 0.80)
{
feedbackText.text = "Bijna...";
Color col = new Color(0xff / 255.0f, 0x66 / 255.0f, 0x00 / 255.0f);
feedbackText.color = col;
feedbackProgressImage.color = col;
}
else
{
feedbackText.text = "Detecteren...";
feedbackText.color = Color.red;
feedbackProgressImage.color = Color.red;
}
float oldValue = feedbackProgress.value;
// use an exponential scale
float newValue = Mathf.Exp(4 * (accuracy - 1.0f));
feedbackProgress.gameObject.Tween("FeedbackUpdate", oldValue, newValue, 0.2f, TweenScaleFunctions.CubicEaseInOut, (t) =>
{
if (feedbackProgress != null)
{
feedbackProgress.value = t.CurrentValue;
}
});
}
// Check whether (in)correct sign has high accuracy
foreach (var kv in signPredictor.learnableProbabilities)
{
if (kv.Value > 0.90)
{
predictedSign = kv.Key;
// Correct sign
if (predictedSign == currentSign)
{
yield return new WaitForSeconds(1.0f);
CheckEquality(predictedSign);
timer = DateTime.Now;
predictedSign = null;
previousIncorrectSign = null;
}
// Incorrect sign
else
{
if (previousIncorrectSign != predictedSign)
{
timer = DateTime.Now;
previousIncorrectSign = predictedSign;
}
else if (DateTime.Now - timer > TimeSpan.FromSeconds(2.0f))
{
CheckEquality(predictedSign);
timer = DateTime.Now;
predictedSign = null;
previousIncorrectSign = null;
}
}
break;
}
}
}
else if (feedbackProgress != null)
{
feedbackProgress.value = 0.0f;
}
}
/// <summary>
/// Function to check equality between the current sign and the sign that the model predicted, if they are equal then the next sign is fetched.
/// </summary>
/// <param name="predicted"></param>
private void CheckEquality(string predicted)
{
if (predicted == course.theme.learnables[currentWordIndex].name)
{
NextSign();
}
}
}