public static SerializedProperty ChangeThemeProperty()

in Assets/MixedRealityToolkit.SDK/Inspectors/UX/Interactable/ThemeInspector.cs [238:440]


        public static SerializedProperty ChangeThemeProperty(int index, SerializedProperty themeSettings, SerializedProperty target, State[] states, bool isNew = false)
        {
            SerializedProperty settingsItem = themeSettings.GetArrayElementAtIndex(index);

            SerializedProperty className = settingsItem.FindPropertyRelative("Name");

            InteractableTypesContainer themeTypes = InteractableProfileItem.GetThemeTypes();

            // get class value types
            if (!String.IsNullOrEmpty(className.stringValue))
            {
                int propIndex = InspectorUIUtility.ReverseLookup(className.stringValue, themeTypes.ClassNames);
                GameObject renderHost = null;
                if (target != null)
                {
                    renderHost = (GameObject)target.objectReferenceValue;
                }

                InteractableThemeBase themeBase = (InteractableThemeBase)Activator.CreateInstance(themeTypes.Types[propIndex], renderHost);

                // does this object have the right component types
                SerializedProperty isValid = settingsItem.FindPropertyRelative("IsValid");
                SerializedProperty noEasing = settingsItem.FindPropertyRelative("NoEasing");
                noEasing.boolValue = themeBase.NoEasing;

                bool valid = false;

                bool hasText = false;
                bool hasRenderer = false;

                if (renderHost != null)
                {
                    for (int i = 0; i < themeBase.Types.Length; i++)
                    {
                        Type type = themeBase.Types[i];
                        if (renderHost.gameObject.GetComponent(type))
                        {
                            if (type == typeof(TextMesh) || type == typeof(Text))
                            {
                                hasText = true;
                            }

                            if (type == typeof(Renderer))
                            {
                                hasRenderer = true;
                            }

                            valid = true;
                        }
                    }
                }

                isValid.boolValue = valid;

                // setup the values
                // get the state names

                List<InteractableThemeProperty> properties = themeBase.ThemeProperties;

                SerializedProperty sProps = settingsItem.FindPropertyRelative("Properties");
                SerializedProperty history = settingsItem.FindPropertyRelative("History");

                if (isNew)
                {
                    sProps.ClearArray();
                }
                else
                {
                    // stick the copy in the new format into sProps.
                    sProps = CopyPropertiesFromHistory(sProps, properties, history, out history);
                }

                for (int i = 0; i < properties.Count; i++)
                {
                    bool newItem = false;
                    if (isNew)
                    {
                        sProps.InsertArrayElementAtIndex(sProps.arraySize);
                        newItem = true;
                    }

                    SerializedProperty item = sProps.GetArrayElementAtIndex(i);
                    SerializedProperty name = item.FindPropertyRelative("Name");
                    SerializedProperty type = item.FindPropertyRelative("Type");
                    SerializedProperty values = item.FindPropertyRelative("Values");

                    name.stringValue = properties[i].Name;
                    type.intValue = (int)properties[i].Type;

                    int valueCount = states.Length;

                    for (int j = 0; j < valueCount; j++)
                    {
                        if (values.arraySize <= j)
                        {
                            values.InsertArrayElementAtIndex(values.arraySize);
                            newItem = true;
                        }

                        SerializedProperty valueItem = values.GetArrayElementAtIndex(j);
                        SerializedProperty valueName = valueItem.FindPropertyRelative("Name");
                        valueName.stringValue = states[j].Name;

                        if (newItem && properties[i].Default != null)
                        {
                            if ((InteractableThemePropertyValueTypes)type.intValue == InteractableThemePropertyValueTypes.AnimatorTrigger)
                            {
                                InteractableThemePropertyValue propValue = new InteractableThemePropertyValue();
                                propValue.Name = valueName.stringValue;
                                propValue.String = states[j].Name;

                                SerializeThemeValues(propValue, valueItem, type.intValue);
                            }
                            else
                            {
                                // assign default values if new item
                                SerializeThemeValues(properties[i].Default, valueItem, type.intValue);
                            }
                        }
                    }

                    //TODO: make sure shader has currently selected property

                    List<ShaderPropertyType> shaderPropFilter = new List<ShaderPropertyType>();
                    // do we need a propId?
                    if (properties[i].Type == InteractableThemePropertyValueTypes.Color)
                    {
                        if ((!hasText && hasRenderer) || (!hasText && target == null))
                        {
                            shaderPropFilter.Add(ShaderPropertyType.Color);
                        }
                        else if (!hasText && !hasRenderer)
                        {
                            valid = false;
                        }
                    }

                    if (properties[i].Type == InteractableThemePropertyValueTypes.ShaderFloat || properties[i].Type == InteractableThemePropertyValueTypes.shaderRange)
                    {
                        if (hasRenderer || target == null)
                        {
                            shaderPropFilter.Add(ShaderPropertyType.Float);
                            shaderPropFilter.Add(ShaderPropertyType.Range);
                        }
                        else
                        {
                            valid = false;
                        }
                    }

                    SerializedProperty propId = item.FindPropertyRelative("PropId");
                    if (newItem)
                    {
                        propId.intValue = 0;
                    }

                    SerializedProperty shaderList = item.FindPropertyRelative("ShaderOptions");
                    SerializedProperty shaderNames = item.FindPropertyRelative("ShaderOptionNames");
                    SerializedProperty shaderName = item.FindPropertyRelative("ShaderName");

                    shaderList.ClearArray();
                    shaderNames.ClearArray();

                    if (valid && shaderPropFilter.Count > 0)
                    {
                        Renderer renderer = null;
                        if (renderHost != null)
                        {
                            renderer = renderHost.gameObject.GetComponent<Renderer>();
                        }

                        ShaderInfo info = GetShaderProperties(renderer, shaderPropFilter.ToArray());
                        ShaderProperties[] shaderProps = info.ShaderOptions;
                        shaderName.stringValue = info.Name;
                        for (int n = 0; n < shaderProps.Length; n++)
                        {
                            shaderList.InsertArrayElementAtIndex(shaderList.arraySize);
                            SerializedProperty shaderListItem = shaderList.GetArrayElementAtIndex(shaderList.arraySize - 1);
                            SerializedProperty shaderListName = shaderListItem.FindPropertyRelative("Name");
                            SerializedProperty shaderListType = shaderListItem.FindPropertyRelative("Type");
                            SerializedProperty shaderListRange = shaderListItem.FindPropertyRelative("Range");

                            shaderListName.stringValue = shaderProps[n].Name;
                            shaderListType.intValue = (int)shaderProps[n].Type;
                            shaderListRange.vector2Value = shaderProps[n].Range;

                            shaderNames.InsertArrayElementAtIndex(shaderNames.arraySize);
                            SerializedProperty names = shaderNames.GetArrayElementAtIndex(shaderNames.arraySize - 1);
                            names.stringValue = shaderProps[n].Name;
                            
                        }
                        Debug.Log(shaderNames.arraySize + " / " + propId.intValue);
                    }
                }

                if (!valid)
                {
                    isValid.boolValue = false;
                }
            }

            return themeSettings;
        }