// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Microsoft.MixedReality.Toolkit.UI
{
///
/// Theme Engine to play particular audio files based on state changes.
/// Add AudioSource component if none is found on initialized GameObject or in children
///
public class InteractableAudioTheme : InteractableThemeBase
{
///
public override bool IsEasingSupported => false;
private AudioSource audioSource;
public InteractableAudioTheme()
{
Types = new Type[] { typeof(Transform) };
Name = "Audio Theme";
}
///
public override ThemeDefinition GetDefaultThemeDefinition()
{
return new ThemeDefinition()
{
ThemeType = GetType(),
StateProperties = new List()
{
new ThemeStateProperty()
{
Name = "Audio",
Type = ThemePropertyTypes.AudioClip,
Values = new List(),
Default = new ThemePropertyValue() { AudioClip = null }
},
},
CustomProperties = new List(),
};
}
public override void Init(GameObject host, ThemeDefinition settings)
{
audioSource = host.GetComponentInChildren();
base.Init(host, settings);
}
public override ThemePropertyValue GetProperty(ThemeStateProperty property)
{
ThemePropertyValue start = new ThemePropertyValue();
if (audioSource == null)
{
audioSource = Host.GetComponentInChildren();
}
if (audioSource != null)
{
start.AudioClip = audioSource.clip;
}
return start;
}
///
public override void SetValue(ThemeStateProperty property, int index, float percentage)
{
SetValue(property, property.Values[index]);
}
///
protected override void SetValue(ThemeStateProperty property, ThemePropertyValue value)
{
if (audioSource == null)
{
audioSource = Host.AddComponent();
}
audioSource.clip = value.AudioClip;
audioSource.Play();
}
}
}