public void ReactToCustomVariableChangedValue(string changedMember, CustomVariable customVariable, object oldValue)
        {
            #region Name

            if (changedMember == "Name")
            {
                ReactToChangedCustomVariableName((string)oldValue, customVariable);
            }
            #endregion

            #region SetByDerived
            if (changedMember == "SetByDerived")
            {
                bool didErrorOccur = false;

                if (customVariable.SetByDerived && customVariable.IsShared)
                {
                    MessageBox.Show("Variables that are IsShared cannot be SetByDerived");
                    didErrorOccur = true;
                }

                if (didErrorOccur)
                {
                    customVariable.SetByDerived = (bool)oldValue;
                }
                else
                {
                    ProjectManager.UpdateAllDerivedElementFromBaseValues(false, true);
                }
            }
            #endregion

            #region IsShared

            else if (changedMember == "IsShared")
            {
                HandleIsSharedVariableSet(customVariable, oldValue);
            }
            #endregion

            #region SouceObjectProperty

            else if (changedMember == "SourceObjectProperty")
            {
                // See if there is already a NOS that uses this SourceObject/SourceObjectProperty combo
                IElement       currentElement  = EditorLogic.CurrentElement;
                CustomVariable currentVariable = customVariable;

                if (!string.IsNullOrEmpty(currentVariable.SourceObject) && !string.IsNullOrEmpty(currentVariable.SourceObjectProperty))
                {
                    foreach (CustomVariable variableInLoop in currentElement.CustomVariables)
                    {
                        if (variableInLoop != currentVariable &&
                            !string.IsNullOrEmpty(variableInLoop.SourceObject) && currentVariable.SourceObject == variableInLoop.SourceObject &&
                            !string.IsNullOrEmpty(variableInLoop.SourceObjectProperty) && currentVariable.SourceObjectProperty == variableInLoop.SourceObjectProperty)
                        {
                            MessageBox.Show("There is already a variable that is modifying " + currentVariable.SourceObjectProperty + " on " + currentVariable.SourceObject);

                            currentVariable.SourceObjectProperty = (string)oldValue;
                        }
                    }
                }
            }

            #endregion

            #region DefaultValue

            else if (changedMember == "DefaultValue")
            {
                customVariable.FixEnumerationTypes();

                var currentElement = GlueState.Self.CurrentElement;

                if (!string.IsNullOrEmpty(customVariable.SourceObject))
                {
                    // See if the source NamedObjectSave has
                    // this variable exposed, and if so, set that
                    // variable too so the two mirror each other...
                    // or make it null if this is a recasted variable.
                    NamedObjectSave nos = currentElement.GetNamedObjectRecursively(customVariable.SourceObject);

                    if (nos != null)
                    {
                        CustomVariableInNamedObject cvino = nos.GetCustomVariable(customVariable.SourceObjectProperty);

                        // If the cvino is null, that means that the NOS doesn't have this exposed, so we don't
                        // need to do anything.
                        if (cvino != null)
                        {
                            if (string.IsNullOrEmpty(customVariable.OverridingPropertyType))
                            {
                                cvino.Value = customVariable.DefaultValue;
                            }
                            else
                            {
                                cvino.Value = null;
                            }
                        }
                    }
                }

                Plugins.PluginManager.ReactToElementVariableChange(currentElement, customVariable);
            }

            #endregion

            #region HasAccompanyingVelocityProperty
            else if (changedMember == "HasAccompanyingVelocityProperty")
            {
                ReactToChangedHasAccompanyingVelocityProperty(customVariable);
            }
            #endregion

            #region OverridingPropertyType

            else if (changedMember == "OverridingPropertyType")
            {
                if (customVariable.OverridingPropertyType != null)
                {
                    customVariable.SetDefaultValueAccordingToType(customVariable.OverridingPropertyType);
                }
                GlueCommands.Self.RefreshCommands.RefreshPropertyGrid();
            }

            #endregion

            #region Type
            else if (changedMember == "Type")
            {
                customVariable.SetDefaultValueAccordingToType(customVariable.Type);
            }
            #endregion
        }
Example #2
0
        public void ReactToCustomVariableChangedValue(string changedMember, CustomVariable customVariable, object oldValue)
        {
            #region Name

            if (changedMember == nameof(CustomVariable.Name))
            {
                ReactToChangedCustomVariableName((string)oldValue, customVariable);
            }
            #endregion

            #region SetByDerived
            if (changedMember == nameof(CustomVariable.SetByDerived))
            {
                bool didErrorOccur = false;

                if (customVariable.SetByDerived && customVariable.IsShared)
                {
                    MessageBox.Show("Variables that are IsShared cannot be SetByDerived");
                    didErrorOccur = true;
                }

                if (didErrorOccur)
                {
                    customVariable.SetByDerived = (bool)oldValue;
                }
                else
                {
                    ProjectManager.UpdateAllDerivedElementFromBaseValues(true);
                }
            }
            #endregion

            #region IsShared

            else if (changedMember == nameof(CustomVariable.IsShared))
            {
                HandleIsSharedVariableSet(customVariable, oldValue);
            }
            #endregion

            #region Scope

            else if (changedMember == nameof(CustomVariable.Scope))
            {
                HandleScopeSet(customVariable, oldValue);
            }

            #endregion

            #region SouceObjectProperty

            else if (changedMember == nameof(CustomVariable.SourceObjectProperty))
            {
                // See if there is already a NOS that uses this SourceObject/SourceObjectProperty combo
                IElement       currentElement  = EditorLogic.CurrentElement;
                CustomVariable currentVariable = customVariable;

                if (!string.IsNullOrEmpty(currentVariable.SourceObject) && !string.IsNullOrEmpty(currentVariable.SourceObjectProperty))
                {
                    foreach (CustomVariable variableInLoop in currentElement.CustomVariables)
                    {
                        if (variableInLoop != currentVariable &&
                            !string.IsNullOrEmpty(variableInLoop.SourceObject) && currentVariable.SourceObject == variableInLoop.SourceObject &&
                            !string.IsNullOrEmpty(variableInLoop.SourceObjectProperty) && currentVariable.SourceObjectProperty == variableInLoop.SourceObjectProperty)
                        {
                            MessageBox.Show("There is already a variable that is modifying " + currentVariable.SourceObjectProperty + " on " + currentVariable.SourceObject);

                            currentVariable.SourceObjectProperty = (string)oldValue;
                        }
                    }
                }
            }

            #endregion

            #region DefaultValue

            else if (changedMember == nameof(CustomVariable.DefaultValue))
            {
                customVariable.FixEnumerationTypes();

                var currentElement = GlueState.Self.CurrentElement;

                if (!string.IsNullOrEmpty(customVariable.SourceObject))
                {
                    // See if the source NamedObjectSave has
                    // this variable exposed, and if so, set that
                    // variable too so the two mirror each other...
                    // or make it null if this is a recasted variable.
                    NamedObjectSave nos = currentElement.GetNamedObjectRecursively(customVariable.SourceObject);

                    if (nos != null)
                    {
                        CustomVariableInNamedObject cvino = nos.GetCustomVariable(customVariable.SourceObjectProperty);

                        // If the cvino is null, that means that the NOS doesn't have this exposed, so we don't
                        // need to do anything.
                        if (cvino != null)
                        {
                            if (string.IsNullOrEmpty(customVariable.OverridingPropertyType))
                            {
                                cvino.Value = customVariable.DefaultValue;
                            }
                            else
                            {
                                cvino.Value = null;
                            }
                        }
                    }
                }

                Plugins.PluginManager.ReactToElementVariableChange(currentElement, customVariable);
            }

            #endregion

            #region HasAccompanyingVelocityProperty
            else if (changedMember == nameof(CustomVariable.HasAccompanyingVelocityProperty))
            {
                ReactToChangedHasAccompanyingVelocityProperty(customVariable);
            }
            #endregion

            #region OverridingPropertyType

            else if (changedMember == nameof(CustomVariable.OverridingPropertyType))
            {
                if (customVariable.OverridingPropertyType != null)
                {
                    customVariable.SetDefaultValueAccordingToType(customVariable.OverridingPropertyType);
                }
                GlueCommands.Self.RefreshCommands.RefreshPropertyGrid();
            }

            #endregion

            #region Type
            else if (changedMember == nameof(CustomVariable.Type))
            {
                var currentValue = customVariable.DefaultValue;
                var oldType      = (string)oldValue;

                bool wasAbleToConvert = false;

                if (currentValue != null)
                {
                    if (oldType == "int")
                    {
                        var valueAsInt = (int)currentValue;

                        switch (customVariable.Type)
                        {
                        case "float":
                            customVariable.DefaultValue = (float)valueAsInt;
                            wasAbleToConvert            = true;
                            break;

                        case "double":
                            customVariable.DefaultValue = (double)valueAsInt;
                            wasAbleToConvert            = true;
                            break;

                        case "string":
                            customVariable.DefaultValue = valueAsInt.ToString();
                            wasAbleToConvert            = true;
                            break;
                        }
                    }
                    else if (oldType == "float")
                    {
                        var valueAsFloat = (float)currentValue;

                        switch (customVariable.Type)
                        {
                        case "int":
                            customVariable.DefaultValue = (int)valueAsFloat;
                            wasAbleToConvert            = true;
                            break;

                        case "double":
                            customVariable.DefaultValue = (double)valueAsFloat;
                            wasAbleToConvert            = true;
                            break;

                        case "string":
                            customVariable.DefaultValue = valueAsFloat.ToString();
                            wasAbleToConvert            = true;
                            break;
                        }
                    }
                    else if (oldType == "double")
                    {
                        var valueAsDouble = (double)currentValue;

                        switch (customVariable.Type)
                        {
                        case "int":
                            customVariable.DefaultValue = (int)valueAsDouble;
                            wasAbleToConvert            = true;
                            break;

                        case "float":
                            customVariable.DefaultValue = (float)valueAsDouble;
                            wasAbleToConvert            = true;
                            break;

                        case "string":
                            customVariable.DefaultValue = valueAsDouble.ToString();
                            wasAbleToConvert            = true;
                            break;
                        }
                    }
                    else if (oldType == "string")
                    {
                        var valueAsString = (string)currentValue;

                        switch (customVariable.Type)
                        {
                        case "int":
                        {
                            if (int.TryParse(valueAsString, out int result))
                            {
                                customVariable.DefaultValue = result;
                            }
                        }
                        break;

                        case "float":
                        {
                            if (float.TryParse(valueAsString, out float result))
                            {
                                customVariable.DefaultValue = result;
                            }
                        }
                        break;

                        case "double":
                        {
                            if (double.TryParse(valueAsString, out double result))
                            {
                                customVariable.DefaultValue = result;
                            }
                        }
                        break;
                        }
                    }
                }

                if (wasAbleToConvert == false)
                {
                    customVariable.SetDefaultValueAccordingToType(customVariable.Type);
                }


                // If the type changed, the Property Grid needs to be re-made so that the new
                // grid will have the right type for the DefaultValue cell:
                PropertyGridHelper.UpdateDisplayedPropertyGridProperties();
            }
            #endregion
        }