diff --git a/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs b/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs index 4ff7e82d9ef7913f1783ecb7d878ca5f373fd5bc..9022161ab828343980222442d711f869c399a813 100644 --- a/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs +++ b/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs @@ -4,10 +4,11 @@ using UnityEngine; using UnityEngine.UI; public class BuildRoom : MonoBehaviour { - public LabelledInputValidator[] labelledInputValidators; - public Button buildButton; + public LabelledInputValidator[] labelledInputValidators; // All the input validators and their respective labels + public Button buildButton; // The submit/build room button void Update() { + // Check if all inputs have been entered and enable/disable the button foreach (LabelledInputValidator labelledInputValidator in labelledInputValidators) { InputValidator inputValidator = labelledInputValidator.inputValidator; if (!inputValidator.IsValid()) { @@ -19,7 +20,41 @@ public class BuildRoom : MonoBehaviour { buildButton.interactable = true; } + // allData is in the form: + // { + // "obj": { + // "path": "C:\filepath\file.obj", + // }, + // "mtl": { + // "path": "C:\filepath\file.mtl", + // }, + // "roomDimensions": { + // "x": 1, + // "y": 2, + // "z": 3, + // }, + // "startPosition": { + // "x": 4, + // "y": 5, + // "z": 6, + // }, + // "speakers": { + // "speaker1": { + // "path": C:\filepath\audio.mp3, + // "x": 7, + // "y": 8, + // "z": 9, + // }, + // "speaker2": { + // "path": C:\filepath\audio.mp3, + // "x": 10, + // "y": 11, + // "z": 12, + // } + // } + // } public void OnButtonPress() { + // Compile all the inputs into one dictionary Dictionary<string, object> allData = new Dictionary<string, object>(); foreach (LabelledInputValidator labelledInputValidator in labelledInputValidators) { allData[labelledInputValidator.name] = labelledInputValidator.inputValidator.GetData(); @@ -49,6 +84,8 @@ public class BuildRoom : MonoBehaviour { } } +// A class to label the data from an input validator +// Also allows for viewing in the inspector [Serializable] public class LabelledInputValidator { public string name; diff --git a/AVVR/Assets/_Scripts/MenuInterface/DimensionInputValidator.cs b/AVVR/Assets/_Scripts/MenuInterface/DimensionInputValidator.cs index 37433fb7193731cf398a941ce2c36b21e9132c01..f8d3ecd4f9972e3c753c82a5076367720052a27e 100644 --- a/AVVR/Assets/_Scripts/MenuInterface/DimensionInputValidator.cs +++ b/AVVR/Assets/_Scripts/MenuInterface/DimensionInputValidator.cs @@ -2,14 +2,14 @@ using System.Collections.Generic; using TMPro; public class DimensionInputValidator : InputValidator { - public bool canBeNegative; + public bool canBeNegative; // True if the input can be negative public TMP_InputField xInput; public TMP_InputField yInput; public TMP_InputField zInput; - public TMP_Text error; + public TMP_Text error; // The textmeshpro error output - // Update is called once per frame void Update() { + // Validate input and update the error message accordingly if (xInput.text == "-") { error.text = "Invalid x value"; return; @@ -38,10 +38,12 @@ public class DimensionInputValidator : InputValidator { error.text = ""; } + // If there is no error text, the data is valid public override bool IsValid() { return error.text == ""; } + // Return the x, y, z inputs public override Dictionary<string, object> GetData() { if (!IsValid()) { return null; diff --git a/AVVR/Assets/_Scripts/MenuInterface/FileInputValidator.cs b/AVVR/Assets/_Scripts/MenuInterface/FileInputValidator.cs index e0ff559a23f14bf259284a3ffeede32e0dfa2edc..bd3f7ce04029557e64975efbf86992391fdb3de6 100644 --- a/AVVR/Assets/_Scripts/MenuInterface/FileInputValidator.cs +++ b/AVVR/Assets/_Scripts/MenuInterface/FileInputValidator.cs @@ -7,13 +7,14 @@ using UnityEngine.Events; using UnityEngine.UI; public class FileInputValidator : InputValidator { - public Button selectFileButton; - public TMP_Text outputText; - public string[] fileExtensions; - public UnityEvent<string> onFileSelected; + public Button selectFileButton; // The button to select the file + public TMP_Text outputText; // The textmeshpro error output + public string[] fileExtensions; // A list of all the allowed file extensions in the form .* (eg: .txt, .exe ...) + public UnityEvent<string> onFileSelected; // A list of events to occur when a file is selected (currently unused) - string path = null; + string path = null; // The path to return when data is requested + // The function to call when the button is pressed public void OnSelectFileButtonPressed() { // Don't open the file browser if one is open if (FileBrowser.IsOpen) { @@ -25,6 +26,7 @@ public class FileInputValidator : InputValidator { FileBrowser.ShowLoadDialog(OnSuccess, OnCancel, FileBrowser.PickMode.Files, loadButtonText: "Select"); } + // When a file is successfully picked void OnSuccess(string[] paths) { path = paths[0]; string filename = path.Split("\\").Last(); // Get filename from directory @@ -35,14 +37,17 @@ public class FileInputValidator : InputValidator { onFileSelected?.Invoke($"Selected {filename}"); } + // When a file is not successfully picked (intentionally empty) void OnCancel() { } + // If there is a selected path, it is assumed to be valid public override bool IsValid() { return path != null; } + // Return the path public override Dictionary<string, object> GetData() { if (!IsValid()) { return null; diff --git a/AVVR/Assets/_Scripts/MenuInterface/InputValidator.cs b/AVVR/Assets/_Scripts/MenuInterface/InputValidator.cs index fc883b4cf47dfbc1267df7862648713eb7f3869f..674008713a8a7d6f33c76fa3a33a350c408b700a 100644 --- a/AVVR/Assets/_Scripts/MenuInterface/InputValidator.cs +++ b/AVVR/Assets/_Scripts/MenuInterface/InputValidator.cs @@ -1,7 +1,9 @@ using System.Collections.Generic; using UnityEngine; + +// The abstract class for input validation public abstract class InputValidator : MonoBehaviour { - public abstract bool IsValid(); - public abstract Dictionary<string, object> GetData(); + public abstract bool IsValid(); // Return true if the data is correctly inputted + public abstract Dictionary<string, object> GetData(); // Return the data, null if data is not inputted } \ No newline at end of file diff --git a/AVVR/Assets/_Scripts/MenuInterface/SpeakerInputValidator.cs b/AVVR/Assets/_Scripts/MenuInterface/SpeakerInputValidator.cs index dae98b488104b0dede9db8a1252a3907e3713712..d55fca2f7afda5f6b4e6e24691c2766c8256eeb7 100644 --- a/AVVR/Assets/_Scripts/MenuInterface/SpeakerInputValidator.cs +++ b/AVVR/Assets/_Scripts/MenuInterface/SpeakerInputValidator.cs @@ -2,14 +2,16 @@ using System.Collections.Generic; using TMPro; public class SpeakerInputValidator : InputValidator { - public TMP_Text speakerText; - public FileInputValidator fileInputValidator; - public DimensionInputValidator dimensionInputValidator; + public TMP_Text speakerText; // The title of the speaker + public FileInputValidator fileInputValidator; // The fileInputValidator of the speaker + public DimensionInputValidator dimensionInputValidator; // The dimensionInputValidator of the speaker + // A speaker's input is valid if both the file and position is entered public override bool IsValid() { return fileInputValidator.IsValid() && dimensionInputValidator.IsValid(); } + // Returns a dictionary of the filename and position public override Dictionary<string, object> GetData() { if (!IsValid()) { return null; @@ -19,6 +21,7 @@ public class SpeakerInputValidator : InputValidator { Dictionary<string, object> fileDict = fileInputValidator.GetData(); Dictionary<string, object> dimensionDict = dimensionInputValidator.GetData(); + // Add all entries from fileDict foreach (var entry in fileDict) { output.Add(entry.Key, entry.Value); } @@ -31,6 +34,7 @@ public class SpeakerInputValidator : InputValidator { return output; } + // Changes the title of the speaker (used by speakersInputValidator) public void SetSpeakerNumber(int speakerNumber) { speakerText.text = $"Speaker {speakerNumber}"; } diff --git a/AVVR/Assets/_Scripts/MenuInterface/SpeakersInputValidator.cs b/AVVR/Assets/_Scripts/MenuInterface/SpeakersInputValidator.cs index 78f380a2eb3a0c8de089dc20df72626bb6cfda0b..2f3c0e67a7f5a9ce771c762a67e9ec2a3db7489a 100644 --- a/AVVR/Assets/_Scripts/MenuInterface/SpeakersInputValidator.cs +++ b/AVVR/Assets/_Scripts/MenuInterface/SpeakersInputValidator.cs @@ -3,41 +3,46 @@ using UnityEngine; using UnityEngine.UI; public class SpeakersInputValidator : InputValidator { - public GameObject speakerPrefab; - public GameObject speakerParent; - List<SpeakerInputValidator> allSpeakerInputValidators = new List<SpeakerInputValidator>{}; + public GameObject speakerPrefab; // The prefab to be instantiated for new speakers + public GameObject speakerParent; // The parent for the speaker prefabs to be instantiated + List<SpeakerInputValidator> allSpeakerInputValidators = new List<SpeakerInputValidator>{}; // A list of all the speakerInputValidators that have been added + // The menu starts with no speakers and at least one is required in the scene public void Start() { AddSpeaker(); } + // Add a speaker into the scene public void AddSpeaker() { - GameObject instance = Instantiate(speakerPrefab, speakerParent.transform); + GameObject instance = Instantiate(speakerPrefab, speakerParent.transform); // Instantiate and position the speaker correctly instance.transform.SetSiblingIndex(transform.parent.childCount - 1); - allSpeakerInputValidators.Add(instance.GetComponent<SpeakerInputValidator>()); + allSpeakerInputValidators.Add(instance.GetComponent<SpeakerInputValidator>()); // Add the speaker to the list and set its title correctly instance.GetComponent<SpeakerInputValidator>().SetSpeakerNumber(allSpeakerInputValidators.Count); UpdateContentSizeFitter(); } + // Remove a speaker from the scene public void RemoveSpeaker() { - if (allSpeakerInputValidators.Count <= 1) { + if (allSpeakerInputValidators.Count <= 1) { // Don't remove if it is the only one return; } - SpeakerInputValidator speakerInputValidator = allSpeakerInputValidators[allSpeakerInputValidators.Count - 1]; + SpeakerInputValidator speakerInputValidator = allSpeakerInputValidators[allSpeakerInputValidators.Count - 1]; // Remove the speaker from the list allSpeakerInputValidators.RemoveAt(allSpeakerInputValidators.Count - 1); - Destroy(speakerInputValidator.gameObject); + Destroy(speakerInputValidator.gameObject); // Destroy the object UpdateContentSizeFitter(); } + // Fixes a bug where the content size fitter does not update correctly void UpdateContentSizeFitter() { transform.parent.GetComponent<VerticalLayoutGroup>().enabled = false; Canvas.ForceUpdateCanvases(); transform.parent.GetComponent<VerticalLayoutGroup>().enabled = true; } + // Checks if every speaker's input is valid public override bool IsValid() { foreach (SpeakerInputValidator speakerInputValidator in allSpeakerInputValidators) { if (!speakerInputValidator.IsValid()) { @@ -48,6 +53,7 @@ public class SpeakersInputValidator : InputValidator { return true; } + // Retrieve the data from all the speakers and put them in a dictionary public override Dictionary<string, object> GetData() { if (!IsValid()) { return null;