Files
unity-application/Assets/Courses/Scripts/PanelWithVideoAndImage.cs
2023-05-14 20:18:29 +00:00

132 lines
3.5 KiB
C#

using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
/// <summary>
/// Class to handle panel with image, video and webcam
/// </summary>
public class PanelWithVideoAndImage : MonoBehaviour
{
/// <summary>
/// Reference to the feedback progress bar
/// </summary>
public GameObject feedbackProgressObject;
/// <summary>
/// Reference to the object containing the message for when the course is loaded in preview mode
/// </summary>
public GameObject previewMessage;
/// <summary>
/// True if the course is loaded in preview mode, false otherwise
/// </summary>
public bool isPreview;
/// <summary>
/// Video 'play' sprite
/// </summary>
public Sprite playSprite;
/// <summary>
/// Video 'pause' sprite
/// </summary>
public Sprite pauseSprite;
/// <summary>
/// Reference to instructional video player
/// </summary>
public VideoPlayer videoPlayer;
/// <summary>
/// Refrence to the video play/pause button
/// </summary>
public Image playButton;
/// <summary>
/// Reference to the webcam
/// </summary>
public RawImage webcamScreen;
/// <summary>
/// Reference to the image for displaying the current words sprite
/// </summary>
public Transform signImageContainer;
/// <summary>
/// Reference to the prefab for displaying the image
/// </summary>
public GameObject signImagePrefab;
/// <summary>
/// Reference to the feedback field
/// </summary>
public TMP_Text feedbackText;
/// <summary>
/// Reference to the progress bar
/// </summary>
public Slider feedbackProgressBar;
/// <summary>
/// Reference to the progress bar image, so we can add fancy colors
/// </summary>
public Image feedbackProgressImage;
/// <summary>
/// Reference to the list of learnables
/// </summary>
public List<Learnable> signs;
/// <summary>
/// Index of the current learnable in the list with learnables
/// </summary>
public int currentSignIndex;
/// <summary>
/// Update the display of this panel
/// </summary>
public void Display()
{
Learnable currentSign = signs[currentSignIndex];
videoPlayer.aspectRatio = VideoAspectRatio.FitInside;
videoPlayer.clip = currentSign.clip;
videoPlayer.Play();
feedbackProgressObject.SetActive(!isPreview);
previewMessage.SetActive(isPreview);
List<Sprite> sprites = new List<Sprite>() { currentSign.image };
if (currentSign.handGuide != null)
sprites.Add(currentSign.handGuide);
foreach (Sprite s in sprites)
{
GameObject sprite = GameObject.Instantiate(signImagePrefab, signImageContainer);
sprite.GetComponent<Image>().sprite = s;
}
}
/// <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 TogglePlayPause()
{
if (!videoPlayer.isPlaying)
{
// Play video and switch sprite of button
playButton.sprite = pauseSprite;
videoPlayer.Play();
}
else
{
// Pause video and and switch sprite of button
playButton.sprite = playSprite;
videoPlayer.Pause();
}
}
}