Skip to content
Snippets Groups Projects
Commit a2824e9d authored by ag15g21's avatar ag15g21
Browse files

Added comments for readability

parent 84aa9260
No related branches found
No related tags found
2 merge requests!5statup menu ui added and fixes: readded xr interaction toolkit thats accidentally deleted. and added fixed gitignore,!4Add menu UI to main
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......
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
......@@ -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}";
}
......
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment