Skip to content

Commit 0e11182

Browse files
Stephen HodgsonStephen Hodgson
authored andcommitted
Merge branch 'HTK-master' into HTK-upgrade
2 parents a7ee95c + 6bf5202 commit 0e11182

File tree

8 files changed

+437
-7
lines changed

8 files changed

+437
-7
lines changed

Assets/HoloToolkit/Input/Scripts/Voice/Editor/KeywordAndKeyCodeDrawer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using UnityEngine;
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;
25
using UnityEditor;
36

47
namespace HoloToolkit.Unity.InputModule
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 System.Collections.Generic;
5+
using System.Linq;
6+
using UnityEditor;
7+
using UnityEngine;
8+
9+
namespace HoloToolkit.Unity.InputModule
10+
{
11+
[CustomEditor(typeof(SpeechInputHandler))]
12+
public class SpeechInputHandlerEditor : Editor
13+
{
14+
private SerializedProperty keywordsProperty;
15+
private string[] registeredKeywords;
16+
17+
private void OnEnable()
18+
{
19+
keywordsProperty = serializedObject.FindProperty("keywords");
20+
registeredKeywords = RegisteredKeywords().Distinct().ToArray();
21+
}
22+
23+
public override void OnInspectorGUI()
24+
{
25+
serializedObject.Update();
26+
ShowList(keywordsProperty);
27+
serializedObject.ApplyModifiedProperties();
28+
29+
// error and warning messages
30+
if (keywordsProperty.arraySize == 0)
31+
{
32+
EditorGUILayout.HelpBox("No keywords have been assigned!", MessageType.Warning);
33+
}
34+
else
35+
{
36+
SpeechInputHandler handler = target as SpeechInputHandler;
37+
string duplicateKeyword = handler.keywords.GroupBy(keyword => keyword.Keyword.ToLower()).Where(group => group.Count() > 1).Select(group => group.Key).FirstOrDefault();
38+
if (duplicateKeyword != null)
39+
{
40+
EditorGUILayout.HelpBox("Keyword '" + duplicateKeyword + "' is assigned more than once!", MessageType.Warning);
41+
}
42+
}
43+
}
44+
45+
private static GUIContent removeButtonContent = new GUIContent("-", "Remove keyword");
46+
private static GUIContent addButtonContent = new GUIContent("+", "Add keyword");
47+
private static GUILayoutOption miniButtonWidth = GUILayout.Width(20.0f);
48+
49+
private void ShowList(SerializedProperty list)
50+
{
51+
EditorGUI.indentLevel++;
52+
53+
// remove the keywords already assigned from the registered list
54+
SpeechInputHandler handler = target as SpeechInputHandler;
55+
string[] availableKeywords = registeredKeywords.Except(handler.keywords.Select(keywordAndResponse => keywordAndResponse.Keyword)).ToArray();
56+
57+
// keyword rows
58+
for (int index = 0; index < list.arraySize; index++)
59+
{
60+
// the element
61+
SerializedProperty elementProperty = list.GetArrayElementAtIndex(index);
62+
EditorGUILayout.BeginHorizontal();
63+
bool elementExpanded = EditorGUILayout.PropertyField(elementProperty);
64+
GUILayout.FlexibleSpace();
65+
// the remove element button
66+
bool elementRemoved = GUILayout.Button(removeButtonContent, EditorStyles.miniButton, miniButtonWidth);
67+
if (elementRemoved)
68+
{
69+
list.DeleteArrayElementAtIndex(index);
70+
}
71+
EditorGUILayout.EndHorizontal();
72+
73+
if (!elementRemoved && elementExpanded)
74+
{
75+
SerializedProperty keywordProperty = elementProperty.FindPropertyRelative("Keyword");
76+
string[] keywords = availableKeywords.Concat(new[] { keywordProperty.stringValue }).OrderBy(keyword => keyword).ToArray();
77+
int previousSelection = ArrayUtility.IndexOf(keywords, keywordProperty.stringValue);
78+
int currentSelection = EditorGUILayout.Popup("Keyword", previousSelection, keywords);
79+
if (currentSelection != previousSelection)
80+
{
81+
keywordProperty.stringValue = keywords[currentSelection];
82+
}
83+
84+
SerializedProperty responseProperty = elementProperty.FindPropertyRelative("Response");
85+
EditorGUILayout.PropertyField(responseProperty, true);
86+
}
87+
}
88+
89+
// add button row
90+
EditorGUILayout.BeginHorizontal();
91+
GUILayout.FlexibleSpace();
92+
// the add element button
93+
if (GUILayout.Button(addButtonContent, EditorStyles.miniButton, miniButtonWidth))
94+
{
95+
list.InsertArrayElementAtIndex(list.arraySize);
96+
}
97+
EditorGUILayout.EndHorizontal();
98+
99+
EditorGUI.indentLevel--;
100+
}
101+
102+
private IEnumerable<string> RegisteredKeywords()
103+
{
104+
foreach(SpeechInputSource source in Object.FindObjectsOfType<SpeechInputSource>())
105+
{
106+
foreach(SpeechInputSource.KeywordAndKeyCode keywordAndKeyCode in source.Keywords)
107+
{
108+
yield return keywordAndKeyCode.Keyword;
109+
}
110+
}
111+
}
112+
}
113+
}

Assets/HoloToolkit/Input/Scripts/Voice/Editor/SpeechInputHandlerEditor.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/HoloToolkit/Input/Scripts/Voice/Editor/SpeechInputSourceEditor.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
24
using UnityEditor;
35
using UnityEngine;
46

@@ -7,11 +9,17 @@ namespace HoloToolkit.Unity.InputModule
79
[CustomEditor(typeof(SpeechInputSource))]
810
public class SpeechInputSourceEditor : Editor
911
{
10-
public override void OnInspectorGUI()
12+
private SerializedProperty recognizerStart;
13+
private SerializedProperty keywordsAndKeys;
14+
15+
private void OnEnable()
1116
{
12-
SerializedProperty recognizerStart = serializedObject.FindProperty("RecognizerStart");
13-
SerializedProperty keywordsAndKeys = serializedObject.FindProperty("Keywords");
17+
recognizerStart = serializedObject.FindProperty("RecognizerStart");
18+
keywordsAndKeys = serializedObject.FindProperty("Keywords");
19+
}
1420

21+
public override void OnInspectorGUI()
22+
{
1523
serializedObject.Update();
1624
// the RecognizerStart field
1725
EditorGUILayout.PropertyField(recognizerStart);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 System;
5+
using System.Collections.Generic;
6+
using UnityEngine;
7+
using UnityEngine.Events;
8+
9+
namespace HoloToolkit.Unity.InputModule
10+
{
11+
public class SpeechInputHandler : MonoBehaviour, ISpeechHandler
12+
{
13+
[System.Serializable]
14+
public struct KeywordAndResponse
15+
{
16+
[Tooltip("The keyword to handle.")]
17+
public string Keyword;
18+
[Tooltip("The handler to be invoked.")]
19+
public UnityEvent Response;
20+
}
21+
22+
[Tooltip("The keywords to be recognized and optional keyboard shortcuts.")]
23+
public KeywordAndResponse[] keywords;
24+
25+
[NonSerialized]
26+
private readonly Dictionary<string, UnityEvent> responses = new Dictionary<string, UnityEvent>();
27+
28+
// Use this for initialization
29+
protected virtual void Start()
30+
{
31+
// Convert the struct array into a dictionary, with the keywords and the methods as the values.
32+
// This helps easily link the keyword recognized to the UnityEvent to be invoked.
33+
int keywordCount = keywords.Length;
34+
for (int index = 0; index < keywordCount; index++)
35+
{
36+
KeywordAndResponse keywordAndResponse = keywords[index];
37+
string keyword = keywordAndResponse.Keyword.ToLower();
38+
if (responses.ContainsKey(keyword))
39+
{
40+
Debug.LogError("Duplicate keyword '" + keyword + "' specified in '" + gameObject.name + "'.");
41+
}
42+
else
43+
{
44+
responses.Add(keyword, keywordAndResponse.Response);
45+
}
46+
}
47+
}
48+
49+
void ISpeechHandler.OnSpeechKeywordRecognized(SpeechKeywordRecognizedEventData eventData)
50+
{
51+
UnityEvent keywordResponse;
52+
53+
// Check to make sure the recognized keyword exists in the methods dictionary, then invoke the corresponding method.
54+
if (enabled && responses.TryGetValue(eventData.RecognizedText.ToLower(), out keywordResponse))
55+
{
56+
keywordResponse.Invoke();
57+
}
58+
}
59+
}
60+
}

Assets/HoloToolkit/Input/Scripts/Voice/SpeechInputHandler.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)