Resolve WES-133 "Multiple choice"
This commit is contained in:
committed by
Jelle De Geest
parent
04d9a4bf2b
commit
4e9d801e61
@@ -1,16 +1,41 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.Video;
|
||||
|
||||
|
||||
public class PanelMultipleChoice : MonoBehaviour
|
||||
{
|
||||
public GameObject previewMessage;
|
||||
/// <summary>
|
||||
/// Reference to the webcam screen
|
||||
/// </summary>
|
||||
public RawImage webcamScreen;
|
||||
|
||||
/// <summary>
|
||||
/// Video 'play' sprite
|
||||
/// </summary>
|
||||
public Sprite playSprite;
|
||||
|
||||
/// <summary>
|
||||
/// Video 'pause' sprite
|
||||
/// </summary>
|
||||
public Sprite pauseSprite;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to its course controller to be able to go to call NextSignMultipleChoice to go to the next panel
|
||||
/// </summary>
|
||||
public CoursesController courseController;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to instructional video player
|
||||
/// </summary>
|
||||
public VideoPlayer videoPlayer;
|
||||
|
||||
/// <summary>
|
||||
/// Refrence to the video play/pause button
|
||||
/// </summary>
|
||||
public Image playButton;
|
||||
|
||||
/// <summary>
|
||||
@@ -18,29 +43,152 @@ public class PanelMultipleChoice : MonoBehaviour
|
||||
/// </summary>
|
||||
public Transform optionContainer;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the image for displaying the current words sprite, for a fingerspelling courses
|
||||
/// </summary>
|
||||
public Transform optionFingerspellingContainer;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the option prefab
|
||||
/// </summary>
|
||||
public GameObject optionPrefab;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the option prefab, for a fingerspelling course
|
||||
/// </summary>
|
||||
public GameObject optionFingerspellingPrefab;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the saved progress
|
||||
/// </summary>
|
||||
public PersistentDataController.SavedCourseProgress progress;
|
||||
|
||||
/// <summary>
|
||||
/// The current sign that will be displayed
|
||||
/// </summary>
|
||||
private Learnable currentSign;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to all signs in this course
|
||||
/// </summary>
|
||||
public List<Learnable> signs;
|
||||
|
||||
/// <summary>
|
||||
/// Index of the current sign
|
||||
/// </summary>
|
||||
public int currentSignIndex;
|
||||
|
||||
/// <summary>
|
||||
/// Boolean used to check whether the current course is a fingerspelling course
|
||||
/// </summary>
|
||||
public bool isFingerSpelling;
|
||||
|
||||
/// <summary>
|
||||
/// Boolean used to check whether the user has already answered the question
|
||||
/// </summary>
|
||||
private bool hasAnswered = false;
|
||||
|
||||
/// <summary>
|
||||
/// Get a list of wrongs answers
|
||||
/// </summary>
|
||||
/// <param name="notThisIndex">The index of the correct sign</param>
|
||||
/// <returns></returns>
|
||||
public List<Learnable> GetWrongOptions(int notThisIndex)
|
||||
{
|
||||
List<Learnable> randomSigns = new List<Learnable>();
|
||||
// TODO: find more koosjer way to do this
|
||||
while (randomSigns.Count < 3)
|
||||
{
|
||||
int index = progress.GetRandomLearnable().index;
|
||||
if (index != notThisIndex && !randomSigns.Contains(signs[index]))
|
||||
{
|
||||
randomSigns.Add(signs[index]);
|
||||
}
|
||||
}
|
||||
|
||||
return randomSigns;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the display of this panel
|
||||
/// </summary>
|
||||
public void Display()
|
||||
{
|
||||
Learnable currentSign = signs[currentSignIndex];
|
||||
currentSign = signs[currentSignIndex];
|
||||
videoPlayer.aspectRatio = VideoAspectRatio.FitInside;
|
||||
videoPlayer.clip = currentSign.clip;
|
||||
videoPlayer.Play();
|
||||
// Gets three random selected signs from the list signs which are not equal to currentSign
|
||||
List<Learnable> allOptions = GetWrongOptions(currentSignIndex);
|
||||
|
||||
// TODO: @Tibe find 3 other random indexes and make buttons for mc question (and set images and callbacks)
|
||||
// Add the correct sign at a random position in the list of all options
|
||||
int randomIndex = UnityEngine.Random.Range(0, allOptions.Count + 1);
|
||||
allOptions.Insert(randomIndex, currentSign);
|
||||
|
||||
/**************************
|
||||
* TODO: @Tibe
|
||||
*
|
||||
* Voor het initialiseren van die multiplechoice opties, er bestaat een prefab van die button, moet maar eens kijken
|
||||
* Dus zelf gwn woorden kiezen voor de andere opties ad random.
|
||||
* Zie dat je geen dubbels hebt in je opties (logisch)
|
||||
*
|
||||
* *************************/
|
||||
var prefab = isFingerSpelling ? optionFingerspellingPrefab : optionPrefab;
|
||||
var container = isFingerSpelling ? optionFingerspellingContainer : optionContainer;
|
||||
foreach (Learnable option in allOptions)
|
||||
{
|
||||
GameObject multipleChoiceOption = GameObject.Instantiate(prefab, container);
|
||||
if (!isFingerSpelling)
|
||||
multipleChoiceOption.transform.Find("TextOption").GetComponent<TMP_Text>().text = option.name;
|
||||
multipleChoiceOption.transform.Find("ImageOption").GetComponent<Image>().sprite = option.image;
|
||||
|
||||
Button button = multipleChoiceOption.GetComponent<Button>();
|
||||
button.onClick.AddListener(() =>
|
||||
{
|
||||
if (!hasAnswered)
|
||||
{
|
||||
courseController.NextSignMultipleChoice(currentSign.name, option.name);
|
||||
ShowAnswers(option.name);
|
||||
hasAnswered = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Show the asnwers
|
||||
/// </summary>
|
||||
/// <param name="answerName">The name of the clicked sign</param>
|
||||
public void ShowAnswers(string answerName)
|
||||
{
|
||||
if (answerName == currentSign.name)
|
||||
{
|
||||
courseController.courseTitle.text = "Correct!";
|
||||
}
|
||||
else
|
||||
{
|
||||
courseController.courseTitle.text = "Spijtig, fout";
|
||||
}
|
||||
|
||||
var container = isFingerSpelling ? optionFingerspellingContainer : optionContainer;
|
||||
for (int i = 0; i < container.childCount; i++)
|
||||
{
|
||||
// Get the i-th child object
|
||||
Transform childTransform = container.GetChild(i);
|
||||
|
||||
// Change the background color of the Button
|
||||
Color col = new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f);
|
||||
if (childTransform.Find("ImageOption").GetComponent<Image>().sprite == currentSign.image)
|
||||
{
|
||||
col = new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f);
|
||||
}
|
||||
childTransform.GetComponent<Image>().color = col;
|
||||
}
|
||||
StartCoroutine(GoToNextScreen());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wait and go to the next sign
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private IEnumerator GoToNextScreen()
|
||||
{
|
||||
// Wait for 5 seconds
|
||||
yield return new WaitForSeconds(3.0f);
|
||||
courseController.NextSign();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -51,10 +199,18 @@ public class PanelMultipleChoice : MonoBehaviour
|
||||
public void TogglePlayPause()
|
||||
{
|
||||
if (!videoPlayer.isPlaying)
|
||||
// Play video and hide button
|
||||
{
|
||||
// Play video and switch sprite of button
|
||||
playButton.sprite = pauseSprite;
|
||||
videoPlayer.Play();
|
||||
}
|
||||
|
||||
else
|
||||
// Pause video and show button
|
||||
{
|
||||
// Pause video and and switch sprite of button
|
||||
playButton.sprite = playSprite;
|
||||
videoPlayer.Pause();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user