4 using System.Collections.Generic;
11 [CustomEditor(typeof(SpeechInputHandler))]
14 private SerializedProperty keywordsProperty;
15 private string[] registeredKeywords;
16 private SerializedProperty isGlobalListenerProperty;
17 private SerializedProperty persistentKeywordsProperty;
19 private void OnEnable()
21 keywordsProperty = serializedObject.FindProperty(
"Keywords");
22 isGlobalListenerProperty = serializedObject.FindProperty(
"IsGlobalListener");
23 persistentKeywordsProperty = serializedObject.FindProperty(
"PersistentKeywords");
28 serializedObject.Update();
29 EditorGUILayout.PropertyField(isGlobalListenerProperty);
30 EditorGUILayout.PropertyField(persistentKeywordsProperty);
32 registeredKeywords = RegisteredKeywords().Distinct().ToArray();
33 ShowList(keywordsProperty);
34 serializedObject.ApplyModifiedProperties();
37 if (keywordsProperty.arraySize == 0)
39 EditorGUILayout.HelpBox(
"No keywords have been assigned!", MessageType.Warning);
44 string duplicateKeyword = handler.Keywords
45 .GroupBy(keyword => keyword.Keyword.ToLower())
46 .Where(group => group.Count() > 1)
47 .
Select(group => group.Key).FirstOrDefault();
49 if (duplicateKeyword != null)
51 EditorGUILayout.HelpBox(
"Keyword '" + duplicateKeyword +
"' is assigned more than once!", MessageType.Warning);
56 private static readonly GUIContent RemoveButtonContent =
new GUIContent(
"-",
"Remove keyword");
57 private static readonly GUIContent AddButtonContent =
new GUIContent(
"+",
"Add keyword");
58 private static readonly GUILayoutOption MiniButtonWidth = GUILayout.Width(20.0f);
60 private void ShowList(SerializedProperty list)
62 EditorGUI.indentLevel++;
66 var availableKeywords =
new string[0];
68 if (handler.Keywords != null)
70 availableKeywords = registeredKeywords.Except(handler.Keywords.Select(keywordAndResponse => keywordAndResponse.Keyword)).ToArray();
74 for (
int index = 0; index < list.arraySize; index++)
77 SerializedProperty elementProperty = list.GetArrayElementAtIndex(index);
78 EditorGUILayout.BeginHorizontal();
79 bool elementExpanded = EditorGUILayout.PropertyField(elementProperty);
80 GUILayout.FlexibleSpace();
82 bool elementRemoved = GUILayout.Button(RemoveButtonContent, EditorStyles.miniButton, MiniButtonWidth);
86 list.DeleteArrayElementAtIndex(index);
89 EditorGUILayout.EndHorizontal();
91 if (!elementRemoved && elementExpanded)
93 SerializedProperty keywordProperty = elementProperty.FindPropertyRelative(
"Keyword");
94 string[] keywords = availableKeywords.Concat(
new[] { keywordProperty.stringValue }).OrderBy(keyword => keyword).ToArray();
95 int previousSelection = ArrayUtility.IndexOf(keywords, keywordProperty.stringValue);
96 int currentSelection = EditorGUILayout.Popup(
"Keyword", previousSelection, keywords);
98 if (currentSelection != previousSelection)
100 keywordProperty.stringValue = keywords[currentSelection];
103 SerializedProperty responseProperty = elementProperty.FindPropertyRelative(
"Response");
104 EditorGUILayout.PropertyField(responseProperty,
true);
109 EditorGUILayout.BeginHorizontal();
110 GUILayout.FlexibleSpace();
113 if (GUILayout.Button(AddButtonContent, EditorStyles.miniButton, MiniButtonWidth))
115 list.InsertArrayElementAtIndex(list.arraySize);
118 EditorGUILayout.EndHorizontal();
120 EditorGUI.indentLevel--;
123 private static IEnumerable<string> RegisteredKeywords()
127 for (var i = 0; i < source.
Keywords.Length; i++)