Skip to content

Commit 4a7f89a

Browse files
committed
The SpeechInputHandler shows in inspector the keywords as a dropdown.
1 parent 0538707 commit 4a7f89a

File tree

5 files changed

+101
-136
lines changed

5 files changed

+101
-136
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: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
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+
4+
using System.Collections.Generic;
5+
using System.Linq;
26
using UnityEditor;
37
using UnityEngine;
48

@@ -7,19 +11,88 @@ namespace HoloToolkit.Unity.InputModule
711
[CustomEditor(typeof(SpeechInputHandler))]
812
public class SpeechInputHandlerEditor : Editor
913
{
10-
public override void OnInspectorGUI()
14+
private SerializedProperty keywordsProperty;
15+
private string[] registeredKeywords;
16+
17+
private void OnEnable()
1118
{
12-
SerializedProperty keywordsAndResponses = serializedObject.FindProperty("KeywordsAndResponses");
19+
keywordsProperty = serializedObject.FindProperty("keywords");
20+
registeredKeywords = RegisteredKeywords().Distinct().ToArray();
21+
}
1322

23+
public override void OnInspectorGUI()
24+
{
1425
serializedObject.Update();
15-
EditorGUILayout.PropertyField(keywordsAndResponses, true);
26+
ShowList(keywordsProperty);
1627
serializedObject.ApplyModifiedProperties();
1728

1829
// error and warning messages
19-
if (keywordsAndResponses.arraySize == 0)
30+
if (keywordsProperty.arraySize == 0)
2031
{
2132
EditorGUILayout.HelpBox("No keywords have been assigned!", MessageType.Warning);
2233
}
2334
}
35+
36+
private static GUIContent removeButtonContent = new GUIContent("-", "Remove keyword");
37+
private static GUIContent addButtonContent = new GUIContent("+", "Add keyword");
38+
private static GUILayoutOption miniButtonWidth = GUILayout.Width(20.0f);
39+
40+
private void ShowList(SerializedProperty list)
41+
{
42+
EditorGUI.indentLevel++;
43+
44+
// keyword rows
45+
for (int index = 0; index < list.arraySize; index++)
46+
{
47+
// the element
48+
SerializedProperty elementProperty = list.GetArrayElementAtIndex(index);
49+
EditorGUILayout.BeginHorizontal();
50+
bool elementExpanded = EditorGUILayout.PropertyField(elementProperty);
51+
GUILayout.FlexibleSpace();
52+
// the remove element button
53+
if (GUILayout.Button(removeButtonContent, EditorStyles.miniButton, miniButtonWidth))
54+
{
55+
list.DeleteArrayElementAtIndex(index);
56+
}
57+
EditorGUILayout.EndHorizontal();
58+
59+
if (elementExpanded)
60+
{
61+
SerializedProperty keywordProperty = elementProperty.FindPropertyRelative("Keyword");
62+
int previousSelection = ArrayUtility.IndexOf(registeredKeywords, keywordProperty.stringValue);
63+
int currentSelection = EditorGUILayout.Popup("Keyword", previousSelection, registeredKeywords);
64+
if (currentSelection != previousSelection)
65+
{
66+
keywordProperty.stringValue = registeredKeywords[currentSelection];
67+
}
68+
69+
SerializedProperty responseProperty = elementProperty.FindPropertyRelative("Response");
70+
EditorGUILayout.PropertyField(responseProperty, true);
71+
}
72+
}
73+
74+
// add button row
75+
EditorGUILayout.BeginHorizontal();
76+
GUILayout.FlexibleSpace();
77+
// the add element button
78+
if (GUILayout.Button(addButtonContent, EditorStyles.miniButton, miniButtonWidth))
79+
{
80+
list.InsertArrayElementAtIndex(list.arraySize);
81+
}
82+
EditorGUILayout.EndHorizontal();
83+
84+
EditorGUI.indentLevel--;
85+
}
86+
87+
private IEnumerable<string> RegisteredKeywords()
88+
{
89+
foreach(SpeechInputSource source in Object.FindObjectsOfType<SpeechInputSource>())
90+
{
91+
foreach(SpeechInputSource.KeywordAndKeyCode keywordAndKeyCode in source.Keywords)
92+
{
93+
yield return keywordAndKeyCode.Keyword;
94+
}
95+
}
96+
}
2497
}
2598
}

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);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ public struct KeywordAndResponse
2020
}
2121

2222
[Tooltip("An array of string keywords and UnityEvents, to be set in the Inspector.")]
23-
public KeywordAndResponse[] KeywordsAndResponses;
23+
public KeywordAndResponse[] keywords;
2424

2525
private readonly Dictionary<string, UnityEvent> responses = new Dictionary<string, UnityEvent>();
2626

2727
// Use this for initialization
2828
protected virtual void Start()
2929
{
30-
int keywordCount = KeywordsAndResponses.Length;
30+
int keywordCount = keywords.Length;
3131
if (keywordCount > 0)
3232
{
3333
try
@@ -36,7 +36,7 @@ protected virtual void Start()
3636
// This helps easily link the keyword recognized to the UnityEvent to be invoked.
3737
for (int index = 0; index < keywordCount; index++)
3838
{
39-
KeywordAndResponse keywordAndResponse = KeywordsAndResponses[index];
39+
KeywordAndResponse keywordAndResponse = keywords[index];
4040
responses[keywordAndResponse.Keyword.ToLower()] = keywordAndResponse.Response;
4141
}
4242
}

Assets/HoloToolkit/Input/Tests/Scenes/SpeechInputSource.unity

Lines changed: 4 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -323,58 +323,7 @@ MonoBehaviour:
323323
m_Script: {fileID: 11500000, guid: e7d6513c2cdf97f409654a2a4114d9b1, type: 3}
324324
m_Name:
325325
m_EditorClassIdentifier:
326-
KeywordsAndResponses:
327-
- Keyword: Red
328-
Response:
329-
m_PersistentCalls:
330-
m_Calls:
331-
- m_Target: {fileID: 1071413364}
332-
m_MethodName: ChangeColor
333-
m_Mode: 5
334-
m_Arguments:
335-
m_ObjectArgument: {fileID: 0}
336-
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
337-
m_IntArgument: 0
338-
m_FloatArgument: 0
339-
m_StringArgument: Red
340-
m_BoolArgument: 0
341-
m_CallState: 2
342-
m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine, Version=0.0.0.0, Culture=neutral,
343-
PublicKeyToken=null
344-
- Keyword: Green
345-
Response:
346-
m_PersistentCalls:
347-
m_Calls:
348-
- m_Target: {fileID: 1071413364}
349-
m_MethodName: ChangeColor
350-
m_Mode: 5
351-
m_Arguments:
352-
m_ObjectArgument: {fileID: 0}
353-
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
354-
m_IntArgument: 0
355-
m_FloatArgument: 0
356-
m_StringArgument: Green
357-
m_BoolArgument: 0
358-
m_CallState: 2
359-
m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine, Version=0.0.0.0, Culture=neutral,
360-
PublicKeyToken=null
361-
- Keyword: Blue
362-
Response:
363-
m_PersistentCalls:
364-
m_Calls:
365-
- m_Target: {fileID: 1071413364}
366-
m_MethodName: ChangeColor
367-
m_Mode: 5
368-
m_Arguments:
369-
m_ObjectArgument: {fileID: 0}
370-
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
371-
m_IntArgument: 0
372-
m_FloatArgument: 0
373-
m_StringArgument: Blue
374-
m_BoolArgument: 0
375-
m_CallState: 2
376-
m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine, Version=0.0.0.0, Culture=neutral,
377-
PublicKeyToken=null
326+
keywords: []
378327
--- !u!1 &1073894170
379328
GameObject:
380329
m_ObjectHideFlags: 0
@@ -473,24 +422,7 @@ MonoBehaviour:
473422
m_Script: {fileID: 11500000, guid: e7d6513c2cdf97f409654a2a4114d9b1, type: 3}
474423
m_Name:
475424
m_EditorClassIdentifier:
476-
KeywordsAndResponses:
477-
- Keyword: Reset All
478-
Response:
479-
m_PersistentCalls:
480-
m_Calls:
481-
- m_Target: {fileID: 1117903548}
482-
m_MethodName: ResetAll
483-
m_Mode: 1
484-
m_Arguments:
485-
m_ObjectArgument: {fileID: 0}
486-
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
487-
m_IntArgument: 0
488-
m_FloatArgument: 0
489-
m_StringArgument:
490-
m_BoolArgument: 0
491-
m_CallState: 2
492-
m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine, Version=0.0.0.0, Culture=neutral,
493-
PublicKeyToken=null
425+
keywords: []
494426
--- !u!1 &1525392731
495427
GameObject:
496428
m_ObjectHideFlags: 0
@@ -596,58 +528,7 @@ MonoBehaviour:
596528
m_Script: {fileID: 11500000, guid: e7d6513c2cdf97f409654a2a4114d9b1, type: 3}
597529
m_Name:
598530
m_EditorClassIdentifier:
599-
KeywordsAndResponses:
600-
- Keyword: Red
601-
Response:
602-
m_PersistentCalls:
603-
m_Calls:
604-
- m_Target: {fileID: 1525392733}
605-
m_MethodName: ChangeColor
606-
m_Mode: 5
607-
m_Arguments:
608-
m_ObjectArgument: {fileID: 0}
609-
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
610-
m_IntArgument: 0
611-
m_FloatArgument: 0
612-
m_StringArgument: Red
613-
m_BoolArgument: 0
614-
m_CallState: 2
615-
m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine, Version=0.0.0.0, Culture=neutral,
616-
PublicKeyToken=null
617-
- Keyword: Green
618-
Response:
619-
m_PersistentCalls:
620-
m_Calls:
621-
- m_Target: {fileID: 1525392733}
622-
m_MethodName: ChangeColor
623-
m_Mode: 5
624-
m_Arguments:
625-
m_ObjectArgument: {fileID: 0}
626-
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
627-
m_IntArgument: 0
628-
m_FloatArgument: 0
629-
m_StringArgument: Green
630-
m_BoolArgument: 0
631-
m_CallState: 2
632-
m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine, Version=0.0.0.0, Culture=neutral,
633-
PublicKeyToken=null
634-
- Keyword: Blue
635-
Response:
636-
m_PersistentCalls:
637-
m_Calls:
638-
- m_Target: {fileID: 1525392733}
639-
m_MethodName: ChangeColor
640-
m_Mode: 5
641-
m_Arguments:
642-
m_ObjectArgument: {fileID: 0}
643-
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
644-
m_IntArgument: 0
645-
m_FloatArgument: 0
646-
m_StringArgument: Blue
647-
m_BoolArgument: 0
648-
m_CallState: 2
649-
m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine, Version=0.0.0.0, Culture=neutral,
650-
PublicKeyToken=null
531+
keywords: []
651532
--- !u!1 &1590076548
652533
GameObject:
653534
m_ObjectHideFlags: 0
@@ -1058,7 +939,7 @@ MonoBehaviour:
1058939
m_Script: {fileID: 11500000, guid: e7d6513c2cdf97f409654a2a4114d9b1, type: 3}
1059940
m_Name:
1060941
m_EditorClassIdentifier:
1061-
KeywordsAndResponses:
942+
keywords:
1062943
- Keyword: Red
1063944
Response:
1064945
m_PersistentCalls:

0 commit comments

Comments
 (0)