|
| 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 | +} |
0 commit comments