initial upload
This commit is contained in:
193
Assets/Convai/Scripts/Runtime/UI/SettingsPanel/FadeCanvas.cs
Normal file
193
Assets/Convai/Scripts/Runtime/UI/SettingsPanel/FadeCanvas.cs
Normal file
@ -0,0 +1,193 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Convai.Scripts.Runtime.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// This class is used to control the fade in and fade out animations of a CanvasGroup.
|
||||
/// </summary>
|
||||
public class FadeCanvas : MonoBehaviour
|
||||
{
|
||||
// Current alpha value of the CanvasGroup
|
||||
private float _currentAlpha;
|
||||
|
||||
// Event called when the Active Fade is completed.
|
||||
public Action OnCurrentFadeCompleted;
|
||||
|
||||
/// <summary>
|
||||
/// Starts the fade in animation for the given CanvasGroup.
|
||||
/// </summary>
|
||||
/// <param name="canvasGroup">The CanvasGroup to fade in.</param>
|
||||
/// <param name="duration">The duration of the fade in animation.</param>
|
||||
public void StartFadeIn(CanvasGroup canvasGroup, float duration)
|
||||
{
|
||||
StopAllCoroutines();
|
||||
StartCoroutine(FadeIn(canvasGroup, duration));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the fade out animation for the given CanvasGroup.
|
||||
/// </summary>
|
||||
/// <param name="canvasGroup">The CanvasGroup to fade out.</param>
|
||||
/// <param name="duration">The duration of the fade out animation.</param>
|
||||
public void StartFadeOut(CanvasGroup canvasGroup, float duration)
|
||||
{
|
||||
StopAllCoroutines();
|
||||
StartCoroutine(FadeOut(canvasGroup, duration));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts a sequence of fade in and fade out animations with a gap in between for the given CanvasGroup.
|
||||
/// </summary>
|
||||
/// <param name="canvasGroup">The CanvasGroup to animate.</param>
|
||||
/// <param name="fadeInDuration">The duration of the fade in animation.</param>
|
||||
/// <param name="fadeOutDuration">The duration of the fade out animation.</param>
|
||||
/// <param name="gapDuration">The duration of the gap between the fade in and fade out animations.</param>
|
||||
public void StartFadeInFadeOutWithGap(CanvasGroup canvasGroup, float fadeInDuration, float fadeOutDuration,
|
||||
float gapDuration)
|
||||
{
|
||||
StopAllCoroutines();
|
||||
StartCoroutine(FadeInFadeOutWithGap(canvasGroup, fadeInDuration, fadeOutDuration, gapDuration));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts a sequence of fade out and fade in animations with a gap in between for the given CanvasGroup.
|
||||
/// </summary>
|
||||
/// <param name="canvasGroup">The CanvasGroup to animate.</param>
|
||||
/// <param name="fadeInDuration">The duration of the fade in animation.</param>
|
||||
/// <param name="fadeOutDuration">The duration of the fade out animation.</param>
|
||||
/// <param name="gapDuration">The duration of the gap between the fade out and fade in animations.</param>
|
||||
public void StartFadeOutFadeInWithGap(CanvasGroup canvasGroup, float fadeInDuration, float fadeOutDuration,
|
||||
float gapDuration)
|
||||
{
|
||||
StopAllCoroutines();
|
||||
StartCoroutine(FadeOutFadeInWithGap(canvasGroup, fadeInDuration, fadeOutDuration, gapDuration));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the alpha value of the given CanvasGroup.
|
||||
/// </summary>
|
||||
/// <param name="canvasGroup">The CanvasGroup to set the alpha value for.</param>
|
||||
/// <param name="value">The alpha value to set.</param>
|
||||
private void SetAlpha(CanvasGroup canvasGroup, float value)
|
||||
{
|
||||
_currentAlpha = value;
|
||||
canvasGroup.alpha = _currentAlpha;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine for the fade in animation.
|
||||
/// </summary>
|
||||
/// <param name="canvasGroup">The CanvasGroup to fade in.</param>
|
||||
/// <param name="duration">The duration of the fade in animation.</param>
|
||||
private IEnumerator FadeIn(CanvasGroup canvasGroup, float duration)
|
||||
{
|
||||
float elapsedTime = 0.0f;
|
||||
// Gradually increase alpha from 0 to 1
|
||||
while (_currentAlpha <= 1.0f)
|
||||
{
|
||||
SetAlpha(canvasGroup, elapsedTime / duration);
|
||||
elapsedTime += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
canvasGroup.blocksRaycasts = true;
|
||||
OnCurrentFadeCompleted?.Invoke();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine for the fade out animation.
|
||||
/// </summary>
|
||||
/// <param name="canvasGroup">The CanvasGroup to fade out.</param>
|
||||
/// <param name="duration">The duration of the fade out animation.</param>
|
||||
private IEnumerator FadeOut(CanvasGroup canvasGroup, float duration)
|
||||
{
|
||||
float elapsedTime = 0.0f;
|
||||
|
||||
// Gradually decrease alpha from 1 to 0
|
||||
while (_currentAlpha >= 0.0f)
|
||||
{
|
||||
SetAlpha(canvasGroup, 1 - elapsedTime / duration);
|
||||
elapsedTime += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
canvasGroup.blocksRaycasts = false;
|
||||
|
||||
OnCurrentFadeCompleted?.Invoke();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine for a sequence of fade in and fade out animations with a gap in between.
|
||||
/// </summary>
|
||||
/// <param name="canvasGroup">The CanvasGroup to animate.</param>
|
||||
/// <param name="fadeInDuration">The duration of the fade in animation.</param>
|
||||
/// <param name="fadeOutDuration">The duration of the fade out animation.</param>
|
||||
/// <param name="gapDuration">The duration of the gap between the fade in and fade out animations.</param>
|
||||
private IEnumerator FadeInFadeOutWithGap(CanvasGroup canvasGroup, float fadeInDuration, float fadeOutDuration,
|
||||
float gapDuration)
|
||||
{
|
||||
float elapsedTime = 0.0f;
|
||||
|
||||
// Gradually increase alpha from 0 to 1
|
||||
while (_currentAlpha <= 1.0f)
|
||||
{
|
||||
SetAlpha(canvasGroup, elapsedTime / fadeInDuration);
|
||||
elapsedTime += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// Pause for a specified gap duration
|
||||
yield return new WaitForSeconds(gapDuration);
|
||||
|
||||
elapsedTime = 0.0f;
|
||||
|
||||
// Gradually decrease alpha from 1 to 0
|
||||
while (_currentAlpha >= 0.0f)
|
||||
{
|
||||
SetAlpha(canvasGroup, 1 - elapsedTime / fadeOutDuration);
|
||||
elapsedTime += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
OnCurrentFadeCompleted?.Invoke();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine for a sequence of fade out and fade in animations with a gap in between.
|
||||
/// </summary>
|
||||
/// <param name="canvasGroup">The CanvasGroup to animate.</param>
|
||||
/// <param name="fadeInDuration">The duration of the fade in animation.</param>
|
||||
/// <param name="fadeOutDuration">The duration of the fade out animation.</param>
|
||||
/// <param name="gapDuration">The duration of the gap between the fade out and fade in animations.</param>
|
||||
private IEnumerator FadeOutFadeInWithGap(CanvasGroup canvasGroup, float fadeInDuration, float fadeOutDuration,
|
||||
float gapDuration)
|
||||
{
|
||||
float elapsedTime = 0.0f;
|
||||
|
||||
// Gradually decrease alpha from 1 to 0
|
||||
while (_currentAlpha >= 0.0f)
|
||||
{
|
||||
SetAlpha(canvasGroup, 1 - elapsedTime / fadeOutDuration);
|
||||
elapsedTime += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// Pause for a specified gap duration
|
||||
yield return new WaitForSeconds(gapDuration);
|
||||
|
||||
elapsedTime = 0.0f;
|
||||
|
||||
// Gradually increase alpha from 0 to 1
|
||||
while (_currentAlpha <= 1.0f)
|
||||
{
|
||||
SetAlpha(canvasGroup, elapsedTime / fadeInDuration);
|
||||
elapsedTime += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
OnCurrentFadeCompleted?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3e8b13f4d5b1d4742a6a02493b450f0c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Convai.Scripts.Runtime.UI
|
||||
{
|
||||
public class MicrophoneManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Private Instance of the Singleton
|
||||
/// </summary>
|
||||
private static MicrophoneManager _instance;
|
||||
|
||||
/// <summary>
|
||||
/// Keeps track on the selected microphone device index
|
||||
/// </summary>
|
||||
private int _selectedMicrophoneIndex;
|
||||
|
||||
private MicrophoneManager()
|
||||
{
|
||||
_selectedMicrophoneIndex = UISaveLoadSystem.Instance.SelectedMicrophoneDeviceNumber;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Singleton instance of the MicrophoneTestController.
|
||||
/// </summary>
|
||||
public static MicrophoneManager Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null) _instance = new MicrophoneManager();
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Public Getter for Selected Microphone Name
|
||||
/// </summary>
|
||||
public string SelectedMicrophoneName
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_selectedMicrophoneIndex < 0 || _selectedMicrophoneIndex >= Microphone.devices.Length) return string.Empty;
|
||||
return Microphone.devices[_selectedMicrophoneIndex];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event indicating that the selected Microphone has changed.
|
||||
/// </summary>
|
||||
public event Action<string> OnMicrophoneDeviceChanged;
|
||||
|
||||
/// <summary>
|
||||
/// Called when the selected microphone device is changed.
|
||||
/// </summary>
|
||||
public void SetSelectedMicrophoneIndex(int selectedMicrophoneDeviceValue)
|
||||
{
|
||||
_selectedMicrophoneIndex = selectedMicrophoneDeviceValue;
|
||||
OnMicrophoneDeviceChanged?.Invoke(SelectedMicrophoneName);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether any microphone is present in the system or not
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool HasAnyMicrophoneDevices()
|
||||
{
|
||||
return Microphone.devices.Length != 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 139f8e0a80b8c38428e24202d3bb9ae4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,137 @@
|
||||
using System.Collections;
|
||||
using Convai.Scripts.Runtime.LoggerSystem;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Convai.Scripts.Runtime.UI
|
||||
{
|
||||
[RequireComponent(typeof(MicrophoneTestController))]
|
||||
public class MicrophoneTestAudioWaveform : MonoBehaviour
|
||||
{
|
||||
[Tooltip("The UI element to display the audio waveform.")] [SerializeField]
|
||||
private RectTransform waveVisualizerUI;
|
||||
|
||||
/// <summary>
|
||||
/// Array to hold audio sample data.
|
||||
/// </summary>
|
||||
private readonly float[] _clipSampleData = new float[1024];
|
||||
|
||||
/// <summary>
|
||||
/// Multiplier for adjusting the amplitude of the waveform.
|
||||
/// </summary>
|
||||
private readonly float _waveMultiplier = 75f;
|
||||
|
||||
/// <summary>
|
||||
/// AudioSource to fetch and play audio from.
|
||||
/// </summary>
|
||||
private AudioSource _audioSource;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the MicrophoneManager component.
|
||||
/// </summary>
|
||||
private MicrophoneTestController _microphoneTestController;
|
||||
|
||||
/// <summary>
|
||||
/// The initial size of the wave visualizer UI.
|
||||
/// </summary>
|
||||
private Vector2 _startSizeDelta;
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine reference for stopping the waveform display.
|
||||
/// </summary>
|
||||
private Coroutine _waveformCoroutine;
|
||||
|
||||
/// <summary>
|
||||
/// Get the components on Awake.
|
||||
/// </summary>
|
||||
private void Awake()
|
||||
{
|
||||
_microphoneTestController = GetComponent<MicrophoneTestController>();
|
||||
_audioSource = GetComponent<AudioSource>();
|
||||
_startSizeDelta = waveVisualizerUI.sizeDelta;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subscribing to events when enabled.
|
||||
/// </summary>
|
||||
private void OnEnable()
|
||||
{
|
||||
_microphoneTestController.OnRecordStarted += MicrophoneTestControllerOnRecordStarted;
|
||||
_microphoneTestController.OnRecordCompleted += MicrophoneTestControllerOnRecordCompleted;
|
||||
_microphoneTestController.OnAudioClipCompleted += MicrophoneTestControllerOnAudioClipCompleted;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unsubscribing from events when disabled.
|
||||
/// </summary>
|
||||
private void OnDisable()
|
||||
{
|
||||
_microphoneTestController.OnRecordStarted -= MicrophoneTestControllerOnRecordStarted;
|
||||
_microphoneTestController.OnRecordCompleted -= MicrophoneTestControllerOnRecordCompleted;
|
||||
_microphoneTestController.OnAudioClipCompleted -= MicrophoneTestControllerOnAudioClipCompleted;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when recording starts.
|
||||
/// </summary>
|
||||
private void MicrophoneTestControllerOnRecordStarted()
|
||||
{
|
||||
ConvaiLogger.DebugLog("<color=green> Record Started! </color>", ConvaiLogger.LogCategory.UI);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when recording is completed.
|
||||
/// </summary>
|
||||
private void MicrophoneTestControllerOnRecordCompleted()
|
||||
{
|
||||
ConvaiLogger.DebugLog("<color=green> Record Completed! </color>", ConvaiLogger.LogCategory.UI);
|
||||
_waveformCoroutine = StartCoroutine(DisplayWaveformUntilAudioComplete());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the audio clip playback is completed.
|
||||
/// </summary>
|
||||
private void MicrophoneTestControllerOnAudioClipCompleted()
|
||||
{
|
||||
_audioSource.clip = null;
|
||||
StopCoroutine(_waveformCoroutine);
|
||||
waveVisualizerUI.sizeDelta = _startSizeDelta;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetch and display audio waves from the audio source.
|
||||
/// </summary>
|
||||
private void ShowAudioSourceAudioWaves()
|
||||
{
|
||||
_audioSource.GetSpectrumData(_clipSampleData, 0, FFTWindow.Rectangular);
|
||||
float currentAverageVolume = SampleAverageCalculator() * _waveMultiplier;
|
||||
Vector2 size = waveVisualizerUI.sizeDelta;
|
||||
size.x = currentAverageVolume;
|
||||
size.x = Mathf.Clamp(size.x, 1, 145);
|
||||
waveVisualizerUI.sizeDelta = size;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine to display the waveform while the audio is playing.
|
||||
/// </summary>
|
||||
private IEnumerator DisplayWaveformUntilAudioComplete()
|
||||
{
|
||||
while (_audioSource.isPlaying)
|
||||
{
|
||||
ShowAudioSourceAudioWaves();
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculate the average volume from the sampled audio data.
|
||||
/// </summary>
|
||||
private float SampleAverageCalculator()
|
||||
{
|
||||
float sum = 0f;
|
||||
foreach (float t in _clipSampleData)
|
||||
sum += t;
|
||||
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e673a03d46af90446baa34d979119284
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,213 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Convai.Scripts.Runtime.Addons;
|
||||
using Convai.Scripts.Runtime.LoggerSystem;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Convai.Scripts.Runtime.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// This class is used to control the microphone test.
|
||||
/// It requires a UIMicrophoneSettings, AudioSource, and MicrophoneInputChecker components to work.
|
||||
/// </summary>
|
||||
[DefaultExecutionOrder(-100)]
|
||||
[RequireComponent(typeof(UIMicrophoneSettings), typeof(AudioSource), typeof(MicrophoneInputChecker))]
|
||||
public class MicrophoneTestController : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// Constants for frequency and maximum recording time.
|
||||
/// </summary>
|
||||
private const int FREQUENCY = 44100;
|
||||
|
||||
private const int MAX_RECORD_TIME = 10;
|
||||
|
||||
/// <summary>
|
||||
/// The AudioSource component attached to this GameObject.
|
||||
/// </summary>
|
||||
private AudioSource _audioSource;
|
||||
|
||||
/// <summary>
|
||||
/// The Audio Playing Status.
|
||||
/// </summary>
|
||||
private bool _isAudioPlaying;
|
||||
|
||||
/// <summary>
|
||||
/// The Recording Status.
|
||||
/// </summary>
|
||||
private bool _isRecording;
|
||||
|
||||
/// <summary>
|
||||
/// The MicrophoneInputChecker component attached to this GameObject.
|
||||
/// </summary>
|
||||
private MicrophoneInputChecker _microphoneInputChecker;
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine reference for stopping the recording.
|
||||
/// </summary>
|
||||
private Coroutine _recordTimeCounterCoroutine;
|
||||
|
||||
/// <summary>
|
||||
/// The currently selected microphone device.
|
||||
/// </summary>
|
||||
private string _selectedDevice;
|
||||
|
||||
/// <summary>
|
||||
/// The UIMicrophoneSettings component attached to this GameObject.
|
||||
/// </summary>
|
||||
private UIMicrophoneSettings _uiMicrophoneSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Singleton instance of the MicrophoneTestController.
|
||||
/// </summary>
|
||||
public static MicrophoneTestController Instance { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Get the components on Awake.
|
||||
/// </summary>
|
||||
private void Awake()
|
||||
{
|
||||
// Ensure there's only one instance of MicrophoneTestController
|
||||
if (Instance != null)
|
||||
{
|
||||
ConvaiLogger.DebugLog("<color=red> There's More Than One MicrophoneTestController </color> " + transform + " - " + Instance, ConvaiLogger.LogCategory.UI);
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
Instance = this;
|
||||
|
||||
// Get the components
|
||||
_uiMicrophoneSettings = GetComponent<UIMicrophoneSettings>();
|
||||
_audioSource = GetComponent<AudioSource>();
|
||||
_microphoneInputChecker = GetComponent<MicrophoneInputChecker>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add click event listeners for record and stop buttons on Start.
|
||||
/// </summary>
|
||||
private void Start()
|
||||
{
|
||||
_uiMicrophoneSettings.GetRecordControllerButton().onClick.AddListener(RecordController);
|
||||
_selectedDevice = MicrophoneManager.Instance.SelectedMicrophoneName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Events for different states of recording.
|
||||
/// </summary>
|
||||
public event Action OnRecordStarted;
|
||||
|
||||
public event Action OnRecordCompleted;
|
||||
public event Action OnAudioClipCompleted;
|
||||
|
||||
/// <summary>
|
||||
/// Check if the selected microphone device is working.
|
||||
/// </summary>
|
||||
public void CheckMicrophoneDeviceWorkingStatus(AudioClip audioClip)
|
||||
{
|
||||
_microphoneInputChecker.IsMicrophoneWorking(audioClip);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle the Record/Stop button click.
|
||||
/// </summary>
|
||||
private void RecordController()
|
||||
{
|
||||
if (_isRecording)
|
||||
StopMicrophoneTestRecording();
|
||||
else if (!_isAudioPlaying) StartMicrophoneTestRecording();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start recording from the selected microphone device.
|
||||
/// </summary>
|
||||
private void StartMicrophoneTestRecording()
|
||||
{
|
||||
if (_uiMicrophoneSettings.GetMicrophoneSelectDropdown().options.Count > 0)
|
||||
{
|
||||
_selectedDevice = MicrophoneManager.Instance.SelectedMicrophoneName;
|
||||
AudioClip recordedClip = Microphone.Start(_selectedDevice, false, MAX_RECORD_TIME, FREQUENCY);
|
||||
_audioSource.clip = recordedClip;
|
||||
CheckMicrophoneDeviceWorkingStatus(recordedClip);
|
||||
|
||||
OnRecordStarted?.Invoke();
|
||||
_isRecording = true;
|
||||
|
||||
_recordTimeCounterCoroutine = StartCoroutine(RecordTimeCounter());
|
||||
}
|
||||
else
|
||||
{
|
||||
ConvaiLogger.Error("No microphone devices found.", ConvaiLogger.LogCategory.UI);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop recording from the selected microphone device.
|
||||
/// </summary>
|
||||
private void StopMicrophoneTestRecording()
|
||||
{
|
||||
if (Microphone.IsRecording(_selectedDevice))
|
||||
{
|
||||
StopCoroutine(_recordTimeCounterCoroutine);
|
||||
int position = Microphone.GetPosition(_selectedDevice);
|
||||
|
||||
Microphone.End(_selectedDevice);
|
||||
|
||||
TrimAudio(position);
|
||||
_audioSource.Play();
|
||||
_isAudioPlaying = true;
|
||||
|
||||
OnRecordCompleted?.Invoke();
|
||||
_isRecording = false;
|
||||
|
||||
StartCoroutine(AudioClipTimeCounter(_audioSource.clip.length));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine to automatically stop recording after MAX_RECORD_TIME.
|
||||
/// </summary>
|
||||
private IEnumerator RecordTimeCounter()
|
||||
{
|
||||
yield return new WaitForSeconds(MAX_RECORD_TIME);
|
||||
StopMicrophoneTestRecording();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine to invoke OnAudioClipCompleted after the audio clip duration.
|
||||
/// </summary>
|
||||
private IEnumerator AudioClipTimeCounter(float length)
|
||||
{
|
||||
yield return new WaitForSeconds(length);
|
||||
|
||||
OnAudioClipCompleted?.Invoke();
|
||||
_isAudioPlaying = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trim the audio based on the last recorded position.
|
||||
/// </summary>
|
||||
private void TrimAudio(int micRecordLastPosition)
|
||||
{
|
||||
if (_audioSource.clip == null)
|
||||
{
|
||||
ConvaiLogger.Error("AudioSource clip is null.", ConvaiLogger.LogCategory.UI);
|
||||
return;
|
||||
}
|
||||
|
||||
if (micRecordLastPosition <= 0)
|
||||
{
|
||||
ConvaiLogger.Error("Microphone position is zero or negative. Cannot trim audio.", ConvaiLogger.LogCategory.UI);
|
||||
return;
|
||||
}
|
||||
|
||||
AudioClip tempAudioClip = _audioSource.clip;
|
||||
int channels = tempAudioClip.channels;
|
||||
int position = micRecordLastPosition;
|
||||
float[] samplesArray = new float[position * channels];
|
||||
tempAudioClip.GetData(samplesArray, 0);
|
||||
AudioClip newClip = AudioClip.Create("RecordedSound", position * channels, channels, FREQUENCY, false);
|
||||
newClip.SetData(samplesArray, 0);
|
||||
_audioSource.clip = newClip;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 518be0891425e7543bee2e69fb5ffc86
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,34 @@
|
||||
namespace Convai.Scripts.Runtime.UI
|
||||
{
|
||||
// Handles the activation status of the transcript UI based on Settings Panel Toggle.
|
||||
public class TranscriptUIActiveStatusHandler : ActiveStatusHandler
|
||||
{
|
||||
protected override void UISaveLoadSystem_OnLoad()
|
||||
{
|
||||
// Retrieve the saved notification system activation status.
|
||||
bool newValue = UISaveLoadSystem.Instance.TranscriptUIActiveStatus;
|
||||
|
||||
// Update the UI and internal status based on the loaded value.
|
||||
OnStatusChange(newValue);
|
||||
_activeStatusToggle.isOn = newValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the activation status of the notification system.
|
||||
/// </summary>
|
||||
/// <param name="value"> The new activation status. </param>
|
||||
public override void OnStatusChange(bool value)
|
||||
{
|
||||
// Save the current transcript UI activation status.
|
||||
UISaveLoadSystem.Instance.TranscriptUIActiveStatus = _activeStatusToggle.isOn;
|
||||
|
||||
IChatUI currentImplementation = ConvaiChatUIHandler.Instance.GetCurrentUI();
|
||||
|
||||
// Activate or Deactivate Toggle Values based on the toggle value
|
||||
if (value)
|
||||
currentImplementation.ActivateUI();
|
||||
else
|
||||
currentImplementation.DeactivateUI();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 02b1fa8555b04926b453b3ef98d8026f
|
||||
timeCreated: 1718274575
|
||||
@ -0,0 +1,211 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Convai.Scripts.Runtime.Core;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Convai.Scripts.Runtime.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// This class is used to control the appearance settings of the UI.
|
||||
/// It requires a UIMicrophoneSettings, AudioSource, and MicrophoneInputChecker components to work.
|
||||
/// </summary>
|
||||
public class UIAppearanceSettings : MonoBehaviour
|
||||
{
|
||||
public static Action<bool> UIStatusChange;
|
||||
|
||||
[Header("Settings Panel Animation Values")] [SerializeField] [Tooltip("Duration of the appearance preview animation.")]
|
||||
private float _appearancePreviewDuration = 1f;
|
||||
|
||||
[Header("References")] [Tooltip("Dropdown for selecting the appearance.")] [SerializeField]
|
||||
private TMP_Dropdown _appearanceDropdown;
|
||||
|
||||
/// <summary>
|
||||
/// FadeCanvas for handling appearance transitions.
|
||||
/// </summary>
|
||||
[SerializeField] private FadeCanvas _fadeCanvas;
|
||||
|
||||
/// <summary>
|
||||
/// Duration for fade in animation.
|
||||
/// </summary>
|
||||
private readonly float _fadeInDuration = 0.3f;
|
||||
|
||||
/// <summary>
|
||||
/// Duration for fade out animation.
|
||||
/// </summary>
|
||||
private readonly float _fadeOutDuration = 0.45f;
|
||||
|
||||
/// <summary>
|
||||
/// Currently active appearance.
|
||||
/// </summary>
|
||||
private IChatUI _currentActiveAppearance;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the UISettingsPanel for coordinating with appearance changes.
|
||||
/// </summary>
|
||||
private UISettingsPanel _uiSettingsPanel;
|
||||
|
||||
/// <summary>
|
||||
/// Action notifying the change of appearance.
|
||||
/// </summary>
|
||||
public Action OnAppearanceChanged;
|
||||
|
||||
/// <summary>
|
||||
/// Initialization when the script is loaded.
|
||||
/// </summary>
|
||||
private void Awake()
|
||||
{
|
||||
// Clear existing options in the dropdown.
|
||||
_appearanceDropdown.ClearOptions();
|
||||
|
||||
// Get reference to the UISettingsPanel.
|
||||
_uiSettingsPanel = GetComponent<UISettingsPanel>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
InitializeAppearanceDropdown();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subscribe to events when this component is enabled.
|
||||
/// </summary>
|
||||
private void OnEnable()
|
||||
{
|
||||
// Subscribe to the event when saved data is loaded.
|
||||
UISaveLoadSystem.Instance.OnLoad += UISaveLoadSystem_OnLoad;
|
||||
|
||||
// Subscribe to the event when data is saved.
|
||||
UISaveLoadSystem.Instance.OnSave += UISaveLoadSystem_OnSave;
|
||||
|
||||
ConvaiNPCManager.Instance.OnActiveNPCChanged += ConvaiNPCManager_OnActiveNPCChanged;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unsubscribe from events when this component is disabled.
|
||||
/// </summary>
|
||||
private void OnDisable()
|
||||
{
|
||||
// Unsubscribe from the event when saved data is loaded.
|
||||
UISaveLoadSystem.Instance.OnLoad -= UISaveLoadSystem_OnLoad;
|
||||
|
||||
// Unsubscribe from the event when data is saved.
|
||||
UISaveLoadSystem.Instance.OnSave -= UISaveLoadSystem_OnSave;
|
||||
|
||||
ConvaiNPCManager.Instance.OnActiveNPCChanged -= ConvaiNPCManager_OnActiveNPCChanged;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the appearance dropdown with available options.
|
||||
/// </summary>
|
||||
private void InitializeAppearanceDropdown()
|
||||
{
|
||||
List<string> appearanceNames = new();
|
||||
foreach (KeyValuePair<ConvaiChatUIHandler.UIType, IChatUI> appearance in ConvaiChatUIHandler.Instance
|
||||
.GetUIAppearances) appearanceNames.Add(appearance.Key.ToString());
|
||||
|
||||
_appearanceDropdown.AddOptions(appearanceNames);
|
||||
int value = (int)UISaveLoadSystem.Instance.UIType;
|
||||
_appearanceDropdown.value = value;
|
||||
// Subscribe to the appearance change event.
|
||||
_appearanceDropdown.onValueChanged.AddListener(ChangeAppearance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event handler for when the active NPC changes.
|
||||
/// </summary>
|
||||
private void ConvaiNPCManager_OnActiveNPCChanged(ConvaiNPC newActiveNPC)
|
||||
{
|
||||
if (newActiveNPC != null)
|
||||
{
|
||||
if (!UISaveLoadSystem.Instance.TranscriptUIActiveStatus) return;
|
||||
_fadeCanvas.StartFadeIn(_currentActiveAppearance.GetCanvasGroup(), _fadeInDuration);
|
||||
UIStatusChange?.Invoke(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
_fadeCanvas.OnCurrentFadeCompleted += FadeOutChat;
|
||||
_fadeCanvas.StartFadeOut(_currentActiveAppearance.GetCanvasGroup(), _fadeOutDuration);
|
||||
}
|
||||
}
|
||||
|
||||
private void FadeOutChat()
|
||||
{
|
||||
UIStatusChange?.Invoke(false);
|
||||
_fadeCanvas.OnCurrentFadeCompleted -= FadeOutChat;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event handler when saved data is loaded.
|
||||
/// </summary>
|
||||
private void UISaveLoadSystem_OnLoad()
|
||||
{
|
||||
// Set the current active appearance based on loaded data.
|
||||
_currentActiveAppearance = ConvaiChatUIHandler.Instance.GetCurrentUI();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event handler when data is saved.
|
||||
/// </summary>
|
||||
private void UISaveLoadSystem_OnSave()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event handler for appearance change.
|
||||
/// </summary>
|
||||
private void ChangeAppearance(int selectedOptionNumber)
|
||||
{
|
||||
// Initiate Settings Panel transition.
|
||||
_uiSettingsPanel.FadeOutFadeinWithGap(_fadeInDuration, _fadeOutDuration, _appearancePreviewDuration);
|
||||
|
||||
// Deactivate the current appearance.
|
||||
_currentActiveAppearance.DeactivateUI();
|
||||
|
||||
ConvaiChatUIHandler.UIType newUIType = (ConvaiChatUIHandler.UIType)_appearanceDropdown.value;
|
||||
ConvaiChatUIHandler.Instance.SetUIType(newUIType);
|
||||
_currentActiveAppearance = ConvaiChatUIHandler.Instance.GetChatUIByUIType(newUIType);
|
||||
|
||||
// Update the current active appearance reference.
|
||||
_currentActiveAppearance.ActivateUI();
|
||||
|
||||
// Initiate appearance transition.
|
||||
FadeInFadeOutWithGap();
|
||||
|
||||
OnAppearanceChanged?.Invoke();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fade out the currently active appearance.
|
||||
/// </summary>
|
||||
public void FadeOutCurrentAppearance()
|
||||
{
|
||||
_fadeCanvas.StartFadeOut(_currentActiveAppearance.GetCanvasGroup(), _fadeOutDuration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fade in the currently active appearance.
|
||||
/// </summary>
|
||||
public void FadeInCurrentAppearance()
|
||||
{
|
||||
_fadeCanvas.StartFadeIn(_currentActiveAppearance.GetCanvasGroup(), _fadeInDuration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Perform a fade-in, fade-out transition with a gap in between.
|
||||
/// </summary>
|
||||
private void FadeInFadeOutWithGap()
|
||||
{
|
||||
_fadeCanvas.StartFadeInFadeOutWithGap(_currentActiveAppearance.GetCanvasGroup(), _fadeInDuration,
|
||||
_fadeOutDuration, _appearancePreviewDuration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the currently active appearance.
|
||||
/// </summary>
|
||||
public IChatUI GetCurrentActiveAppearance()
|
||||
{
|
||||
return _currentActiveAppearance;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e22ed8432ed28324891d428b6a8fa2dd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,73 @@
|
||||
using System.Collections;
|
||||
using Convai.Scripts.Runtime.PlayerStats;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Convai.Scripts.Runtime.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// This class is used to manage the display name settings in the UI.
|
||||
/// </summary>
|
||||
public class UIDisplayNameSettings : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// Reference to the TextMeshPro input field for entering/displaying the display name.
|
||||
/// </summary>
|
||||
[SerializeField] private TMP_InputField playerNameInputField;
|
||||
|
||||
private ConvaiPlayerDataSO _convaiPlayerData;
|
||||
private bool _hasPlayerNameBeenSaved;
|
||||
private string _originalPlayerName;
|
||||
private UISettingsPanel _uiSettingsPanel;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_originalPlayerName = string.Empty;
|
||||
_uiSettingsPanel = GetComponentInParent<UISettingsPanel>();
|
||||
|
||||
_uiSettingsPanel.SaveChangesButton.onClick.AddListener(() =>
|
||||
{
|
||||
_hasPlayerNameBeenSaved = true;
|
||||
_originalPlayerName = string.Empty;
|
||||
});
|
||||
|
||||
_uiSettingsPanel.SettingsPanelExitButton.onClick.AddListener(() =>
|
||||
{
|
||||
if (_hasPlayerNameBeenSaved || string.IsNullOrEmpty(_originalPlayerName)) return;
|
||||
playerNameInputField.text = _originalPlayerName;
|
||||
_originalPlayerName = string.Empty;
|
||||
});
|
||||
}
|
||||
|
||||
private IEnumerator Start()
|
||||
{
|
||||
yield return new WaitForSeconds(0.1f);
|
||||
if (ConvaiPlayerDataSO.GetPlayerData(out _convaiPlayerData))
|
||||
playerNameInputField.text = string.IsNullOrEmpty(_convaiPlayerData.PlayerName) ? _convaiPlayerData.DefaultPlayerName : _convaiPlayerData.PlayerName;
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
UISaveLoadSystem.Instance.OnSave += UISaveLoadSystem_OnSave;
|
||||
playerNameInputField.onSelect.AddListener(PlayerNameInputField_OnSelect);
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
UISaveLoadSystem.Instance.OnSave -= UISaveLoadSystem_OnSave;
|
||||
playerNameInputField.onSelect.RemoveListener(PlayerNameInputField_OnSelect);
|
||||
}
|
||||
|
||||
private void PlayerNameInputField_OnSelect(string value)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_originalPlayerName)) return;
|
||||
_originalPlayerName = value;
|
||||
}
|
||||
|
||||
private void UISaveLoadSystem_OnSave()
|
||||
{
|
||||
if (_convaiPlayerData == null || !_hasPlayerNameBeenSaved || string.IsNullOrEmpty(playerNameInputField.text)) return;
|
||||
_convaiPlayerData.PlayerName = playerNameInputField.text;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6995c132b7884398811c9d7df7806a59
|
||||
timeCreated: 1697567638
|
||||
@ -0,0 +1,254 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Convai.Scripts.Runtime.Addons;
|
||||
using Convai.Scripts.Runtime.LoggerSystem;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
#if UNITY_ANDROID
|
||||
using UnityEngine.Android;
|
||||
#endif
|
||||
|
||||
namespace Convai.Scripts.Runtime.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// This class is used to manage the microphone settings in the UI.
|
||||
/// </summary>
|
||||
public class UIMicrophoneSettings : MonoBehaviour
|
||||
{
|
||||
[Tooltip("Dropdown to select the microphone device to use.")] [SerializeField]
|
||||
private TMP_Dropdown _microphoneSelectDropdown;
|
||||
|
||||
[Tooltip("Button to control the recording.")] [SerializeField]
|
||||
private Button _recordControllerButton;
|
||||
|
||||
[Tooltip("Text to display the status of the recording system.")] [SerializeField]
|
||||
private TextMeshProUGUI _recordSystemStatusText;
|
||||
|
||||
/// <summary>
|
||||
/// Image component of the recording control button.
|
||||
/// </summary>
|
||||
private Image _buttonImage;
|
||||
|
||||
/// <summary>
|
||||
/// Text component of the recording control button.
|
||||
/// </summary>
|
||||
private TextMeshProUGUI _buttonText;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the MicrophoneManager to subscribe to its events.
|
||||
/// </summary>
|
||||
private MicrophoneTestController _microphoneTestController;
|
||||
|
||||
/// <summary>
|
||||
/// Index of the selected microphone device.
|
||||
/// </summary>
|
||||
private int _selectedMicrophoneDeviceNumber;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize references.
|
||||
/// </summary>
|
||||
private void Awake()
|
||||
{
|
||||
_microphoneTestController = GetComponent<MicrophoneTestController>();
|
||||
_buttonImage = _recordControllerButton.GetComponent<Image>();
|
||||
_buttonText = _recordControllerButton.GetComponentInChildren<TextMeshProUGUI>();
|
||||
|
||||
RequestMicrophonePermissions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subscribe to events when this component is enabled.
|
||||
/// </summary>
|
||||
private void OnEnable()
|
||||
{
|
||||
_microphoneTestController.OnRecordStarted += MicrophoneTestControllerOnRecordStarted;
|
||||
_microphoneTestController.OnRecordCompleted += MicrophoneTestControllerOnRecordCompleted;
|
||||
_microphoneTestController.OnAudioClipCompleted += MicrophoneTestControllerOnAudioClipCompleted;
|
||||
|
||||
UISaveLoadSystem.Instance.OnLoad += UISaveLoadSystem_OnLoad;
|
||||
UISaveLoadSystem.Instance.OnSave += UISaveLoadSystem_OnSave;
|
||||
|
||||
#if !UNITY_ANDROID && !UNITY_IOS
|
||||
InitializeMicrophoneDevices();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unsubscribe from events when this component is disabled.
|
||||
/// </summary>
|
||||
private void OnDisable()
|
||||
{
|
||||
_microphoneTestController.OnRecordStarted -= MicrophoneTestControllerOnRecordStarted;
|
||||
_microphoneTestController.OnRecordCompleted -= MicrophoneTestControllerOnRecordCompleted;
|
||||
_microphoneTestController.OnAudioClipCompleted -= MicrophoneTestControllerOnAudioClipCompleted;
|
||||
UISaveLoadSystem.Instance.OnLoad -= UISaveLoadSystem_OnLoad;
|
||||
UISaveLoadSystem.Instance.OnSave -= UISaveLoadSystem_OnSave;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Request Microphone permissions on Android or iOS.
|
||||
/// </summary>
|
||||
private void RequestMicrophonePermissions()
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
if (Permission.HasUserAuthorizedPermission(Permission.Microphone))
|
||||
{
|
||||
// Initialize Microphone devices if permission is already granted.
|
||||
InitializeMicrophoneDevices();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Request Microphone permission with callback
|
||||
PermissionCallbacks callbacks = new();
|
||||
callbacks.PermissionGranted += PermissionCallbacks_PermissionGranted;
|
||||
callbacks.PermissionDenied += s => ShowNoMicrophoneDetectedNotification();
|
||||
callbacks.PermissionDeniedAndDontAskAgain += s => ShowNoMicrophoneDetectedNotification();
|
||||
Permission.RequestUserPermission(Permission.Microphone, callbacks);
|
||||
}
|
||||
|
||||
#elif UNITY_IOS
|
||||
// Check Microphone permission on iOS and start coroutine to request permission if not granted.
|
||||
StartCoroutine(iOSTryToAccessMicrophone());
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Show notification when no microphone is detected.
|
||||
/// </summary>
|
||||
private void ShowNoMicrophoneDetectedNotification()
|
||||
{
|
||||
NotificationSystemHandler.Instance.NotificationRequest(NotificationType.NoMicrophoneDetected);
|
||||
_recordSystemStatusText.text = "No Microphone Detected...";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the dropdown with available microphone devices after obtaining permission.
|
||||
/// </summary>
|
||||
private void InitializeMicrophoneDevices()
|
||||
{
|
||||
_microphoneSelectDropdown.ClearOptions();
|
||||
_microphoneSelectDropdown.AddOptions(new List<string>(Microphone.devices));
|
||||
_microphoneSelectDropdown.onValueChanged.AddListener(ChangeSelectedDevice);
|
||||
_recordSystemStatusText.text = "Waiting For Record...";
|
||||
|
||||
// Checking if system has at-least one microphone to record the audio
|
||||
if (!MicrophoneManager.Instance.HasAnyMicrophoneDevices()) ShowNoMicrophoneDetectedNotification();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine to check and request Microphone permission on iOS.
|
||||
/// </summary>
|
||||
private IEnumerator iOSTryToAccessMicrophone()
|
||||
{
|
||||
if (Application.HasUserAuthorization(UserAuthorization.Microphone))
|
||||
{
|
||||
InitializeMicrophoneDevices();
|
||||
}
|
||||
else
|
||||
{
|
||||
yield return Application.RequestUserAuthorization(UserAuthorization.Microphone);
|
||||
|
||||
if (Application.HasUserAuthorization(UserAuthorization.Microphone))
|
||||
InitializeMicrophoneDevices();
|
||||
else
|
||||
ShowNoMicrophoneDetectedNotification();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Callback when Microphone permission is granted on Android.
|
||||
/// </summary>
|
||||
/// <param name="obj">Permission string</param>
|
||||
private void PermissionCallbacks_PermissionGranted(string obj)
|
||||
{
|
||||
InitializeMicrophoneDevices();
|
||||
ConvaiLogger.Info("Microphone Permission Granted.", ConvaiLogger.LogCategory.Character);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event handler when the selected microphone device is changed.
|
||||
/// </summary>
|
||||
private void ChangeSelectedDevice(int selectedDeviceNumber)
|
||||
{
|
||||
_selectedMicrophoneDeviceNumber = selectedDeviceNumber;
|
||||
MicrophoneManager.Instance.SetSelectedMicrophoneIndex(selectedDeviceNumber);
|
||||
UISaveLoadSystem.Instance.SelectedMicrophoneDeviceNumber = _selectedMicrophoneDeviceNumber;
|
||||
ConvaiLogger.Info("Microphone Device Updated.", ConvaiLogger.LogCategory.Character);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event handler when saved data is loaded.
|
||||
/// </summary>
|
||||
private void UISaveLoadSystem_OnLoad()
|
||||
{
|
||||
_microphoneSelectDropdown.value = UISaveLoadSystem.Instance.SelectedMicrophoneDeviceNumber;
|
||||
ConvaiLogger.Info("Loaded Microphone Device. ", ConvaiLogger.LogCategory.Character);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event handler when data is saved.
|
||||
/// </summary>
|
||||
private void UISaveLoadSystem_OnSave()
|
||||
{
|
||||
UISaveLoadSystem.Instance.SelectedMicrophoneDeviceNumber = _microphoneSelectDropdown.value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event handler when a recording is started.
|
||||
/// </summary>
|
||||
private void MicrophoneTestControllerOnRecordStarted()
|
||||
{
|
||||
_recordSystemStatusText.text = "Recording...";
|
||||
_buttonImage.color = Color.red;
|
||||
_buttonText.text = "Stop";
|
||||
_buttonText.color = new Color(1, 1, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event handler when a recording is completed.
|
||||
/// </summary>
|
||||
private void MicrophoneTestControllerOnRecordCompleted()
|
||||
{
|
||||
_recordSystemStatusText.text = "Playing...";
|
||||
_buttonImage.color = Color.green;
|
||||
_buttonText.text = "Rec";
|
||||
_buttonText.color = new Color(0.14f, 0.14f, 0.14f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event handler when the audio clip playback is completed.
|
||||
/// </summary>
|
||||
private void MicrophoneTestControllerOnAudioClipCompleted()
|
||||
{
|
||||
_recordSystemStatusText.text = "Waiting For Record...";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the microphone selection dropdown object.
|
||||
/// </summary>
|
||||
public TMP_Dropdown GetMicrophoneSelectDropdown()
|
||||
{
|
||||
return _microphoneSelectDropdown;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the record control button object.
|
||||
/// </summary>
|
||||
public Button GetRecordControllerButton()
|
||||
{
|
||||
return _recordControllerButton;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the selected microphone device name.
|
||||
/// </summary>
|
||||
public string GetSelectedMicrophoneDeviceName()
|
||||
{
|
||||
if (_selectedMicrophoneDeviceNumber < 0 || _selectedMicrophoneDeviceNumber >= _microphoneSelectDropdown.options.Count)
|
||||
return string.Empty;
|
||||
|
||||
return _microphoneSelectDropdown.options[_selectedMicrophoneDeviceNumber].text;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0f5afe5603f66984b88bd5f54bbd762e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,128 @@
|
||||
using System;
|
||||
using Convai.Scripts.Runtime.LoggerSystem;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Convai.Scripts.Runtime.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// The UISaveLoadSystem class manages the storage and retrieval of persistent data using PlayerPrefs.
|
||||
/// It also ensures that there is only one instance of the class.
|
||||
/// </summary>
|
||||
[DefaultExecutionOrder(-110)]
|
||||
public class UISaveLoadSystem : MonoBehaviour
|
||||
{
|
||||
private const string SELECTED_MICROPHONE_DEVICE_NUMBER = "SelectedMicrophoneDeviceNumber";
|
||||
private const string UI_TYPE = "UIType";
|
||||
private const string NOTIFICATION_SYSTEM_ACTIVE_STATUS = "NotificationSystemActiveStatus";
|
||||
private const string TRANSCRIPT_UI_ACTIVE_STATUS = "TranscriptUIActiveStatus";
|
||||
|
||||
/// <summary>
|
||||
/// Stores the UI type.
|
||||
/// </summary>
|
||||
[HideInInspector] public ConvaiChatUIHandler.UIType UIType;
|
||||
|
||||
/// <summary>
|
||||
/// Events triggered during load and save operations
|
||||
/// </summary>
|
||||
public Action OnLoad; // Event triggered after loading values from PlayerPrefs.
|
||||
|
||||
public Action OnSave; // Event triggered before saving values to PlayerPrefs.
|
||||
|
||||
/// <summary>
|
||||
/// Singleton instance of the UISaveLoadSystem
|
||||
/// </summary>
|
||||
public static UISaveLoadSystem Instance { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Stores the selected microphone device number.
|
||||
/// </summary>
|
||||
public int SelectedMicrophoneDeviceNumber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Stores the status of the notification system.
|
||||
/// </summary>
|
||||
public bool NotificationSystemActiveStatus { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Stores the status of the transcript UI.
|
||||
/// </summary>
|
||||
public bool TranscriptUIActiveStatus { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Ensure there is only one instance of UISaveLoadSystem
|
||||
/// </summary>
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null)
|
||||
{
|
||||
// Log a warning and destroy the duplicate instance
|
||||
ConvaiLogger.Warn("There's More Than One UISaveLoadSystem", ConvaiLogger.LogCategory.UI);
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the singleton instance
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start is called before the first frame update
|
||||
/// </summary>
|
||||
private void Start()
|
||||
{
|
||||
// Load values from PlayerPrefs at the start of the application
|
||||
LoadValues();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the script instance is being destroyed
|
||||
/// </summary>
|
||||
private void OnDestroy()
|
||||
{
|
||||
// Save values to PlayerPrefs when the script is destroyed (e.g., when the scene changes)
|
||||
SaveValues();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load values from PlayerPrefs
|
||||
/// </summary>
|
||||
private void LoadValues()
|
||||
{
|
||||
// Retrieve selected microphone device number from PlayerPrefs, use 0 as default if not found
|
||||
SelectedMicrophoneDeviceNumber = PlayerPrefs.GetInt(SELECTED_MICROPHONE_DEVICE_NUMBER, 0);
|
||||
|
||||
// Retrieve UI type from PlayerPrefs, use 0 as default if not found
|
||||
UIType = (ConvaiChatUIHandler.UIType)PlayerPrefs.GetInt(UI_TYPE, 0);
|
||||
|
||||
// Retrieve notification system status from PlayerPrefs, default to true if not found
|
||||
NotificationSystemActiveStatus = PlayerPrefs.GetInt(NOTIFICATION_SYSTEM_ACTIVE_STATUS, 1) == 1;
|
||||
|
||||
// Retrieve transcript UI status from PlayerPrefs, default to true if not found
|
||||
TranscriptUIActiveStatus = PlayerPrefs.GetInt(TRANSCRIPT_UI_ACTIVE_STATUS, 1) == 1;
|
||||
|
||||
// Invoke the OnLoad event to notify listeners that loading has completed
|
||||
OnLoad?.Invoke();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save values to PlayerPrefs
|
||||
/// </summary>
|
||||
public void SaveValues()
|
||||
{
|
||||
// Invoke the OnSave event to notify listeners that saving is about to occur
|
||||
OnSave?.Invoke();
|
||||
|
||||
// Save selected microphone device number to PlayerPrefs
|
||||
PlayerPrefs.SetInt(SELECTED_MICROPHONE_DEVICE_NUMBER, SelectedMicrophoneDeviceNumber);
|
||||
|
||||
// Save UI type to PlayerPrefs as an integer representation
|
||||
PlayerPrefs.SetInt(UI_TYPE, (int)UIType);
|
||||
|
||||
// Save notification system status to PlayerPrefs as 1 or 0 (true or false)
|
||||
PlayerPrefs.SetInt(NOTIFICATION_SYSTEM_ACTIVE_STATUS, NotificationSystemActiveStatus ? 1 : 0);
|
||||
|
||||
// Save transcript UI status to PlayerPrefs as 1 or 0 (true or false)
|
||||
PlayerPrefs.SetInt(TRANSCRIPT_UI_ACTIVE_STATUS, TranscriptUIActiveStatus ? 1 : 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c6b5b7a24f2267c4893e5ca390ff8a7f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,161 @@
|
||||
using Convai.Scripts.Runtime.Core;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Convai.Scripts.Runtime.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// The UISettingsPanel class manages the settings panel UI, including animations and user interactions.
|
||||
/// </summary>
|
||||
public class UISettingsPanel : MonoBehaviour
|
||||
{
|
||||
[field: Header("References")]
|
||||
[field: SerializeField]
|
||||
public Button SaveChangesButton { get; private set; }
|
||||
|
||||
[field: SerializeField] public Button SettingsPanelExitButton { get; private set; }
|
||||
[SerializeField] private CanvasGroup _panelCanvasGroup;
|
||||
|
||||
/// <summary>
|
||||
/// Animation durations for fade in, fade out, and gap between animations
|
||||
/// </summary>
|
||||
[Header("Settings Panel Animation Values")] [SerializeField] [Tooltip("Duration for fade in animation")]
|
||||
private float _fadeInDuration = 0.35f;
|
||||
|
||||
[SerializeField] [Tooltip("Duration for fade out animation")]
|
||||
private float _fadeOutDuration = 0.35f;
|
||||
|
||||
private FadeCanvas _fadeCanvas;
|
||||
|
||||
/// <summary>
|
||||
/// References to other scripts and components
|
||||
/// </summary>
|
||||
private UIAppearanceSettings _uiAppearanceSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize references to other scripts and components and populate the dropdown with available microphone devices.
|
||||
/// </summary>
|
||||
private void Awake()
|
||||
{
|
||||
// Initialize references to other scripts and components
|
||||
_fadeCanvas = GetComponent<FadeCanvas>();
|
||||
_uiAppearanceSettings = GetComponent<UIAppearanceSettings>();
|
||||
|
||||
// Attach event listeners to UI buttons
|
||||
SaveChangesButton.onClick.AddListener(SaveChanges);
|
||||
SettingsPanelExitButton.onClick.AddListener(delegate { ToggleSettingsPanel(false); });
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
ConvaiInputManager.Instance.toggleSettings += ToggleSettings;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
ConvaiInputManager.Instance.toggleSettings -= ToggleSettings;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update is called once per frame
|
||||
/// </summary>
|
||||
private void ToggleSettings()
|
||||
{
|
||||
if (_panelCanvasGroup.alpha == 0)
|
||||
{
|
||||
ToggleSettingsPanel(true);
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Cursor.visible = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ToggleSettingsPanel(false);
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Toggle the settings panel on or off
|
||||
/// </summary>
|
||||
public void ToggleSettingsPanel(bool value)
|
||||
{
|
||||
if (value)
|
||||
{
|
||||
// Fade in the settings panel
|
||||
ActivatePanel();
|
||||
FadeInSettingsPanel();
|
||||
|
||||
// Check if the alpha of the appearance canvas is at its maximum
|
||||
const float MAX_ALPHA = 1;
|
||||
if (_uiAppearanceSettings.GetCurrentActiveAppearance().GetCanvasGroup().alpha >= MAX_ALPHA)
|
||||
// If true, fade out the current appearance canvas
|
||||
_uiAppearanceSettings.FadeOutCurrentAppearance();
|
||||
|
||||
// Set the cursor lock state to none
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Fade out the settings panel
|
||||
_fadeCanvas.OnCurrentFadeCompleted += DeactivatePanel;
|
||||
FadeOutSettingsPanel();
|
||||
|
||||
// Check if there is an active Convai NPC
|
||||
if (ConvaiNPCManager.Instance.GetActiveConvaiNPC() != null)
|
||||
// If true, fade in the current appearance canvas
|
||||
_uiAppearanceSettings.FadeInCurrentAppearance();
|
||||
|
||||
// Set the cursor lock state to locked
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
|
||||
// Save values when the settings panel is closed
|
||||
UISaveLoadSystem.Instance.SaveValues();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save changes and close the settings panel
|
||||
/// </summary>
|
||||
private void SaveChanges()
|
||||
{
|
||||
UISaveLoadSystem.Instance.SaveValues();
|
||||
ToggleSettingsPanel(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trigger fade in animation for the settings panel
|
||||
/// </summary>
|
||||
private void FadeInSettingsPanel()
|
||||
{
|
||||
_fadeCanvas.StartFadeIn(_panelCanvasGroup, _fadeInDuration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trigger fade out animation for the settings panel
|
||||
/// </summary>
|
||||
private void FadeOutSettingsPanel()
|
||||
{
|
||||
_fadeCanvas.StartFadeOut(_panelCanvasGroup, _fadeOutDuration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trigger fade out and fade in animation with a gap in between
|
||||
/// </summary>
|
||||
public void FadeOutFadeinWithGap(float fadeInDuration, float fadeOutDuration, float previewDuration)
|
||||
{
|
||||
_fadeCanvas.StartFadeOutFadeInWithGap(_panelCanvasGroup, fadeInDuration, fadeOutDuration, previewDuration);
|
||||
}
|
||||
|
||||
private void DeactivatePanel()
|
||||
{
|
||||
_panelCanvasGroup.gameObject.SetActive(false);
|
||||
_fadeCanvas.OnCurrentFadeCompleted -= DeactivatePanel;
|
||||
}
|
||||
|
||||
private void ActivatePanel()
|
||||
{
|
||||
_panelCanvasGroup.gameObject.SetActive(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d2c69b63f88551e49990c3b9f70f833f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,24 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Convai.Scripts.Runtime.UI
|
||||
{
|
||||
public class UISettingsPanelOpenButton : MonoBehaviour
|
||||
{
|
||||
private Button _openButton;
|
||||
private UISettingsPanel _uiSettingsPanel;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_uiSettingsPanel = FindObjectOfType<UISettingsPanel>();
|
||||
_openButton = GetComponent<Button>();
|
||||
|
||||
_openButton.onClick.AddListener(OpenSettingsPanel);
|
||||
}
|
||||
|
||||
private void OpenSettingsPanel()
|
||||
{
|
||||
_uiSettingsPanel.ToggleSettingsPanel(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bdbf93e7ba3ee774b9258ae70cdb88ef
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user