AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
CompoundButtonSpeech.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
6 using HoloToolkit.Unity;
7 
8 namespace HoloToolkit.Unity.Buttons
9 {
14  [RequireComponent (typeof(CompoundButton))]
15  public class CompoundButtonSpeech : MonoBehaviour, ISpeechHandler
16  {
17  public enum KeywordSourceEnum
18  {
19  None,
20  LocalOverride,
21  ButtonText,
22  }
23 
29  public KeywordSourceEnum KeywordSource = KeywordSourceEnum.ButtonText;
30 
35  public string Keyword = string.Empty;
36 
40  private string prevButtonText;
41 
45  private string keyWord;
46 
50  private CompoundButton m_button;
51 
55  private CompoundButtonText m_button_text;
56 
57  public void Start ()
58  {
59  // Disable if no microphone devices are found
60  if (Microphone.devices.Length == 0) {
61  enabled = false;
62  return;
63  }
64 
65  if (KeywordSource == KeywordSourceEnum.None)
66  return;
67 
68  keyWord = string.Empty;
69 
70  // Assign internal cached components
71  m_button = GetComponent<CompoundButton>();
72  m_button_text = GetComponent<CompoundButtonText>();
73 
74  switch (KeywordSource)
75  {
76  case KeywordSourceEnum.ButtonText:
77  default:
78  keyWord = prevButtonText = m_button_text.Text;
79  break;
80 
81  case KeywordSourceEnum.LocalOverride:
82  keyWord = Keyword;
83  break;
84  }
85 
86 
87  }
88 
89  public void Update()
90  {
91  // Check if Button text has changed. If so, remove previous keyword and add new button text
92  if (KeywordSource == KeywordSourceEnum.ButtonText &&
93  prevButtonText != null &&
94  m_button_text.Text != prevButtonText)
95  {
96  prevButtonText = m_button_text.Text;
97  }
98  }
99 
100  private void OnDestroy()
101  {
102  if (string.IsNullOrEmpty(this.keyWord))
103  return;
104 
105  }
106 
112  {
113  if (!gameObject.activeSelf || !enabled)
114  return;
115 
116  if(eventData.RecognizedText.Equals(keyWord))
117  {
118  // Send a pressed message to the button through the InputManager
119  m_button.TriggerClicked();
120  }
121  }
122 
123 #if UNITY_EDITOR
124  [UnityEditor.CustomEditor(typeof(CompoundButtonSpeech))]
125  public class CustomEditor : MRTKEditor
126  {
127  protected override void DrawCustomFooter() {
128  CompoundButtonSpeech speechButton = (CompoundButtonSpeech)target;
129 
130  bool microphoneEnabled = UnityEditor.PlayerSettings.WSA.GetCapability(UnityEditor.PlayerSettings.WSACapability.Microphone);
131  if (!microphoneEnabled) {
132  DrawWarning("Microphone capability not present. Speech recognition will be disabled.");
133  return;
134  }
135 
136  UnityEditor.EditorGUILayout.LabelField("Keyword source", UnityEditor.EditorStyles.miniBoldLabel);
137  speechButton.KeywordSource = (CompoundButtonSpeech.KeywordSourceEnum)UnityEditor.EditorGUILayout.EnumPopup(speechButton.KeywordSource);
138  CompoundButtonText text = speechButton.GetComponent<CompoundButtonText>();
139  switch (speechButton.KeywordSource) {
140  case CompoundButtonSpeech.KeywordSourceEnum.ButtonText:
141  default:
142  if (text == null) {
143  DrawError("No CompoundButtonText component found.");
144  } else if (string.IsNullOrEmpty(text.Text)) {
145  DrawWarning("No keyword found in button text.");
146  } else {
147  UnityEditor.EditorGUILayout.LabelField("Keyword: " + text.Text);
148  }
149  break;
150 
151  case CompoundButtonSpeech.KeywordSourceEnum.LocalOverride:
152  speechButton.Keyword = UnityEditor.EditorGUILayout.TextField(speechButton.Keyword);
153  break;
154 
156  UnityEditor.EditorGUILayout.LabelField("(Speech control disabled)", UnityEditor.EditorStyles.miniBoldLabel);
157  break;
158  }
159  }
160 
161  private void EnableMicrophone() {
162  UnityEditor.PlayerSettings.WSA.SetCapability(UnityEditor.PlayerSettings.WSACapability.Microphone, true);
163  }
164 
165  private void AddText() {
166  CompoundButtonSpeech speechButton = (CompoundButtonSpeech)target;
167  speechButton.gameObject.AddComponent<CompoundButtonText>();
168  }
169  }
170 #endif
171  }
172 }
string Keyword
Keyword used when KeywordSource is set to LocalOverride
KeywordSourceEnum KeywordSource
Source of the keyword to be used By default the text in a CompoundButtonText component will be used ...
void TriggerClicked()
Public function to force a clicked event on a button.
Definition: Button.cs:417
Concrete version of Button class used with other CompoundButton scripts (e.g., CompoundButtonMesh) Al...
void OnSpeechKeywordRecognized(SpeechEventData eventData)
On Speech keyword recognizer handle speech event
string RecognizedText
The text that was recognized.
This class will automatically link buttons to speech keywords (Currently disabled) ...
Describes an input event that involves keyword recognition.
Interface to implement to react to speech recognition.
Useful for releasing external override. See CursorStateEnum.Contextual