private void HandleExposeVariableClick(object sender, System.Windows.RoutedEventArgs e) { InstanceSave instanceSave = SelectedState.Self.SelectedInstance; if (instanceSave == null) { MessageBox.Show("Cannot expose variables on components or screens, only on instances"); return; } // Update June 1, 2017 // This code used to expose // a variable on whatever state // was selected; however, exposed // variables should be exposed on the // default state or else Gum doesn't properly //StateSave currentStateSave = SelectedState.Self.SelectedStateSave; StateSave stateToExposeOn = SelectedState.Self.SelectedElement.DefaultState; VariableSave variableSave = this.VariableSave; if (variableSave == null) { // This variable hasn't been assigned yet. Let's make a new variable with a null value string variableName = instanceSave.Name + "." + this.RootVariableName; string rawVariableName = this.RootVariableName; ElementSave elementForInstance = ObjectFinder.Self.GetElementSave(instanceSave.BaseType); var variableInDefault = elementForInstance.DefaultState.GetVariableSave(rawVariableName); while (variableInDefault == null && !string.IsNullOrEmpty(elementForInstance.BaseType)) { elementForInstance = ObjectFinder.Self.GetElementSave(elementForInstance.BaseType); if (elementForInstance?.DefaultState == null) { break; } variableInDefault = elementForInstance.DefaultState.GetVariableSave(rawVariableName); } if (variableInDefault != null) { string variableType = variableInDefault.Type; stateToExposeOn.SetValue(variableName, null, instanceSave, variableType); // Now the variable should be created so we can access it variableSave = stateToExposeOn.GetVariableSave(variableName); // Since it's newly-created, there is no value being set: variableSave.SetsValue = false; } } if (variableSave == null) { MessageBox.Show("This variable cannot be exposed."); } else { TextInputWindow tiw = new TextInputWindow(); tiw.Message = "Enter variable name:"; // We want to use the name without the dots. // So something like TextInstance.Text would be // TextInstanceText tiw.Result = variableSave.Name.Replace(".", ""); DialogResult result = tiw.ShowDialog(); if (result == DialogResult.OK) { string whyNot; if (!NameVerifier.Self.IsExposedVariableNameValid(tiw.Result, SelectedState.Self.SelectedElement, out whyNot)) { MessageBox.Show(whyNot); } else { variableSave.ExposedAsName = tiw.Result; GumCommands.Self.FileCommands.TryAutoSaveCurrentElement(); GumCommands.Self.GuiCommands.RefreshPropertyGrid(force: true); } } } }
//private void ReactIfChangedMemberIsAnimation(ElementSave parentElement, string changedMember, object oldValue, out bool saveProject) //{ // const string sourceFileString = "SourceFile"; // if (changedMember == sourceFileString) // { // StateSave stateSave = SelectedState.Self.SelectedStateSave; // string value = (string)stateSave.GetValueRecursive(sourceFileString); // if (!FileManager.IsRelative) // { // } // saveProject = true; // } // saveProject = false; //} /// <summary> /// Called when the user clicks the "Make Default" menu item /// </summary> /// <param name="variableName">The variable to make default.</param> private void ResetVariableToDefault(StateReferencingInstanceMember srim) { string variableName = srim.Name; bool shouldReset = false; bool affectsTreeView = false; var selectedElement = SelectedState.Self.SelectedElement; if (SelectedState.Self.SelectedInstance != null) { affectsTreeView = variableName == "Parent"; //variableName = SelectedState.Self.SelectedInstance.Name + "." + variableName; shouldReset = true; } else if (selectedElement != null) { shouldReset = // Don't let the user reset standard element variables, they have to have some actual value (selectedElement is StandardElementSave) == false || // ... unless it's not the default SelectedState.Self.SelectedStateSave != SelectedState.Self.SelectedElement.DefaultState; } // now we reset, but we don't remove the variable: //if(shouldReset) //{ // // If the variable is part of a category, then we don't allow setting the variable to default - they gotta do it through the cateory itself // if (isPartOfCategory) // { // var window = new DeletingVariablesInCategoriesMessageBox(); // window.ShowDialog(); // shouldReset = false; // } //} if (shouldReset) { bool isPartOfCategory = srim.StateSaveCategory != null; StateSave state = SelectedState.Self.SelectedStateSave; bool wasChangeMade = false; VariableSave variable = state.GetVariableSave(variableName); if (variable != null) { // Don't remove the variable if it's part of an element - we still want it there // so it can be set, we just don't want it to set a value // Update August 13, 2013 // Actually, we do want to remove it if it's part of an element but not the // default state // Update October 17, 2017 // Now that components do not // necessarily need to have all // of their variables, we can remove // the variable now. In fact, we should //bool shouldRemove = SelectedState.Self.SelectedInstance != null || // SelectedState.Self.SelectedStateSave != SelectedState.Self.SelectedElement.DefaultState; // Also, don't remove it if it's an exposed variable, this un-exposes things bool shouldRemove = string.IsNullOrEmpty(variable.ExposedAsName) && !isPartOfCategory; // Update October 7, 2019 // Actually, we can remove any variable so long as the current state isn't the "base definition" for it // For elements - no variables are the base variable definitions except for variables that are categorized // state variables for categories defined in this element if (shouldRemove) { var isState = variable.IsState(selectedElement, out ElementSave categoryContainer, out StateSaveCategory categoryForVariable); if (isState) { var isDefinedHere = categoryForVariable != null && categoryContainer == selectedElement; shouldRemove = !isDefinedHere; } } if (shouldRemove) { state.Variables.Remove(variable); } else if (isPartOfCategory) { var variableInDefault = SelectedState.Self.SelectedElement.DefaultState.GetVariableSave(variable.Name); if (variableInDefault != null) { GumCommands.Self.GuiCommands.PrintOutput( $"The variable {variable.Name} is part of the category {srim.StateSaveCategory.Name} so it cannot be removed. Instead, the value has been set to the value in the default state"); variable.Value = variableInDefault.Value; } else { GumCommands.Self.GuiCommands.PrintOutput("Could not set value to default because the default state doesn't set this value"); } } else { variable.Value = null; variable.SetsValue = false; } wasChangeMade = true; // We need to refresh the property grid and the wireframe display } else { // Maybe this is a variable list? VariableListSave variableList = state.GetVariableListSave(variableName); if (variableList != null) { state.VariableLists.Remove(variableList); // We don't support this yet: // variableList.SetsValue = false; // just to be safe wasChangeMade = true; } } if (wasChangeMade) { RefreshUI(force: true); WireframeObjectManager.Self.RefreshAll(true); SelectionManager.Self.Refresh(); if (affectsTreeView) { GumCommands.Self.GuiCommands.RefreshElementTreeView(SelectedState.Self.SelectedElement); } if (ProjectManager.Self.GeneralSettingsFile.AutoSave) { ProjectManager.Self.SaveElement(SelectedState.Self.SelectedElement); } } } else { srim.IsDefault = false; } }
/// <summary> /// Called when the user clicks the "Make Default" menu item /// </summary> /// <param name="variableName">The variable to make default.</param> private void ResetVariableToDefault(string variableName) { bool shouldReset = false; bool affectsTreeView = false; if (SelectedState.Self.SelectedInstance != null) { affectsTreeView = variableName == "Parent"; variableName = SelectedState.Self.SelectedInstance.Name + "." + variableName; shouldReset = true; } else if (SelectedState.Self.SelectedElement != null) { shouldReset = // Don't let the user reset standard element variables, they have to have some actual value (SelectedState.Self.SelectedElement is StandardElementSave) == false || // ... unless it's not the default SelectedState.Self.SelectedStateSave != SelectedState.Self.SelectedElement.DefaultState; } if (shouldReset) { StateSave state = SelectedState.Self.SelectedStateSave; bool wasChangeMade = false; VariableSave variable = state.GetVariableSave(variableName); if (variable != null) { // Don't remove the variable if it's part of an element - we still want it there // so it can be set, we just don't want it to set a value // Update August 13, 2013 // Actually, we do want to remove it if it's part of an element but not the // default state bool shouldRemove = SelectedState.Self.SelectedInstance != null || SelectedState.Self.SelectedStateSave != SelectedState.Self.SelectedElement.DefaultState; // Also, don't remove it if it's an exposed variable, this un-exposes things shouldRemove = shouldRemove && string.IsNullOrEmpty(variable.ExposedAsName); if (shouldRemove) { state.Variables.Remove(variable); } else { variable.Value = null; variable.SetsValue = false; } wasChangeMade = true; // We need to refresh the property grid and the wireframe display } else { // Maybe this is a variable list? VariableListSave variableList = state.GetVariableListSave(variableName); if (variableList != null) { state.VariableLists.Remove(variableList); // We don't support this yet: // variableList.SetsValue = false; // just to be safe wasChangeMade = true; } } if (wasChangeMade) { RefreshUI(); WireframeObjectManager.Self.RefreshAll(true); SelectionManager.Self.Refresh(); if (affectsTreeView) { GumCommands.Self.GuiCommands.RefreshElementTreeView(SelectedState.Self.SelectedElement); } if (ProjectManager.Self.GeneralSettingsFile.AutoSave) { ProjectManager.Self.SaveElement(SelectedState.Self.SelectedElement); } } } }
//private void ReactIfChangedMemberIsAnimation(ElementSave parentElement, string changedMember, object oldValue, out bool saveProject) //{ // const string sourceFileString = "SourceFile"; // if (changedMember == sourceFileString) // { // StateSave stateSave = SelectedState.Self.SelectedStateSave; // string value = (string)stateSave.GetValueRecursive(sourceFileString); // if (!FileManager.IsRelative) // { // } // saveProject = true; // } // saveProject = false; //} /// <summary> /// Called when the user clicks the "Make Default" menu item /// </summary> /// <param name="variableName">The variable to make default.</param> private void ResetVariableToDefault(StateReferencingInstanceMember srim) { string variableName = srim.Name; bool shouldReset = false; bool affectsTreeView = false; if (SelectedState.Self.SelectedInstance != null) { affectsTreeView = variableName == "Parent"; //variableName = SelectedState.Self.SelectedInstance.Name + "." + variableName; shouldReset = true; } else if (SelectedState.Self.SelectedElement != null) { shouldReset = // Don't let the user reset standard element variables, they have to have some actual value (SelectedState.Self.SelectedElement is StandardElementSave) == false || // ... unless it's not the default SelectedState.Self.SelectedStateSave != SelectedState.Self.SelectedElement.DefaultState; } if (shouldReset) { // If the variable is part of a category, then we don't allow setting the variable to default - they gotta do it through the cateory itself bool isPartOfCategory = srim.StateSaveCategory != null; if (isPartOfCategory) { var window = new DeletingVariablesInCategoriesMessageBox(); window.ShowDialog(); shouldReset = false; } } if (shouldReset) { StateSave state = SelectedState.Self.SelectedStateSave; bool wasChangeMade = false; VariableSave variable = state.GetVariableSave(variableName); if (variable != null) { // Don't remove the variable if it's part of an element - we still want it there // so it can be set, we just don't want it to set a value // Update August 13, 2013 // Actually, we do want to remove it if it's part of an element but not the // default state // Update October 17, 2017 // Now that components do not // necessarily need to have all // of their variables, we can remove // the variable now. In fact, we should //bool shouldRemove = SelectedState.Self.SelectedInstance != null || // SelectedState.Self.SelectedStateSave != SelectedState.Self.SelectedElement.DefaultState; // Also, don't remove it if it's an exposed variable, this un-exposes things bool shouldRemove = string.IsNullOrEmpty(variable.ExposedAsName); if (shouldRemove) { state.Variables.Remove(variable); } else { variable.Value = null; variable.SetsValue = false; } wasChangeMade = true; // We need to refresh the property grid and the wireframe display } else { // Maybe this is a variable list? VariableListSave variableList = state.GetVariableListSave(variableName); if (variableList != null) { state.VariableLists.Remove(variableList); // We don't support this yet: // variableList.SetsValue = false; // just to be safe wasChangeMade = true; } } if (wasChangeMade) { RefreshUI(); WireframeObjectManager.Self.RefreshAll(true); SelectionManager.Self.Refresh(); if (affectsTreeView) { GumCommands.Self.GuiCommands.RefreshElementTreeView(SelectedState.Self.SelectedElement); } if (ProjectManager.Self.GeneralSettingsFile.AutoSave) { ProjectManager.Self.SaveElement(SelectedState.Self.SelectedElement); } } } else { srim.IsDefault = false; } }