Example #1
0
        // Cast to voice data
        public static TTSWitVoiceData AsTTSWitVoiceData(this WitResponseNode responseNode)
        {
            // Get result
            object result    = new TTSWitVoiceData();
            Type   voiceType = typeof(TTSWitVoiceData);

            // Get root & field names
            WitResponseClass voiceRoot = responseNode.AsObject;

            string[] voiceFieldNames = voiceRoot.ChildNodeNames;
            foreach (var voiceFieldName in voiceFieldNames)
            {
                FieldInfo field = voiceType.GetField(voiceFieldName);
                if (field != null && field.IsPublic && !field.IsStatic)
                {
                    // Get value
                    object val = null;
                    // String
                    if (field.FieldType == typeof(string))
                    {
                        val = voiceRoot[voiceFieldName].Value;
                    }
                    // String[]
                    else if (field.FieldType == typeof(string[]))
                    {
                        val = voiceRoot[voiceFieldName].AsStringArray;
                    }
                    // Set value
                    if (val != null)
                    {
                        field.SetValue(result, val);
                    }
                }
                else
                {
                    Log($"Decode Warning\nUnknown field: {voiceFieldName}", true);
                }
            }

            // Return result
            return((TTSWitVoiceData)result);
        }
        // Handles gui layout
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            // On gui
            float  y         = position.y;
            string voiceName = property.FindPropertyRelative(VAR_SETTINGS).stringValue;

            property.isExpanded =
                EditorGUI.Foldout(new Rect(position.x, y, position.width, VAR_HEIGHT), property.isExpanded, voiceName);
            if (!property.isExpanded)
            {
                return;
            }
            y += VAR_HEIGHT + VAR_MARGIN;

            // Increment
            EditorGUI.indentLevel++;

            // Get voice index
            int voiceIndex = GetVoiceIndex(property);

            // Iterate subfields
            UpdateFields();
            for (int s = 0; s < _fields.Count; s++)
            {
                FieldInfo          subfield         = _fields[s];
                SerializedProperty subfieldProperty = property.FindPropertyRelative(subfield.Name);
                Rect subfieldRect = new Rect(position.x, y, position.width, VAR_HEIGHT);
                if (string.Equals(subfield.Name, VAR_VOICE) && voiceIndex != -1)
                {
                    int newVoiceIndex = EditorGUI.Popup(subfieldRect, subfieldProperty.displayName, voiceIndex,
                                                        TTSWitVoiceUtility.VoiceNames.ToArray());
                    newVoiceIndex = Mathf.Clamp(newVoiceIndex, 0, TTSWitVoiceUtility.VoiceNames.Count);
                    if (voiceIndex != newVoiceIndex)
                    {
                        voiceIndex = newVoiceIndex;
                        subfieldProperty.stringValue = TTSWitVoiceUtility.VoiceNames[voiceIndex];
                        GUI.FocusControl(null);
                    }
                    y += VAR_HEIGHT + VAR_MARGIN;
                    continue;
                }
                if (string.Equals(subfield.Name, VAR_STYLE) && voiceIndex >= 0 && voiceIndex < TTSWitVoiceUtility.Voices.Length)
                {
                    // Get voice data
                    TTSWitVoiceData voiceData = TTSWitVoiceUtility.Voices[voiceIndex];
                    EditorGUI.indentLevel++;

                    // Locale layout
                    EditorGUI.LabelField(subfieldRect, "Locale", voiceData.locale);
                    y += VAR_HEIGHT + VAR_MARGIN;

                    // Gender layout
                    subfieldRect = new Rect(position.x, y, position.width, VAR_HEIGHT);
                    EditorGUI.LabelField(subfieldRect, "Gender", voiceData.gender);
                    y += VAR_HEIGHT + VAR_MARGIN;

                    // Style layout/select
                    subfieldRect = new Rect(position.x, y, position.width, VAR_HEIGHT);
                    if (voiceData.styles != null && voiceData.styles.Length > 0)
                    {
                        // Get style index
                        string style      = subfieldProperty.stringValue;
                        int    styleIndex = new List <string>(voiceData.styles).IndexOf(style);

                        // Show style select
                        int newStyleIndex = EditorGUI.Popup(subfieldRect, subfieldProperty.displayName, styleIndex,
                                                            voiceData.styles);
                        newStyleIndex = Mathf.Clamp(newStyleIndex, 0, voiceData.styles.Length);
                        if (styleIndex != newStyleIndex)
                        {
                            // Apply style
                            styleIndex = newStyleIndex;
                            subfieldProperty.stringValue = voiceData.styles[styleIndex];
                            GUI.FocusControl(null);
                        }

                        // Move down
                        y += VAR_HEIGHT + VAR_MARGIN;
                        EditorGUI.indentLevel--;
                        continue;
                    }

                    // Undent
                    EditorGUI.indentLevel--;
                }

                // Default layout
                EditorGUI.PropertyField(subfieldRect, subfieldProperty, new GUIContent(subfieldProperty.displayName));

                // Clamp in between range
                RangeAttribute range = subfield.GetCustomAttribute <RangeAttribute>();
                if (range != null)
                {
                    int newValue = Mathf.Clamp(subfieldProperty.intValue, (int)range.min, (int)range.max);
                    if (subfieldProperty.intValue != newValue)
                    {
                        subfieldProperty.intValue = newValue;
                    }
                }

                // Increment
                y += VAR_HEIGHT + VAR_MARGIN;
            }

            // Undent
            EditorGUI.indentLevel--;
        }