private void ReactIfChangedMemberIsUnitType(ElementSave parentElement, string changedMember, object oldValueAsObject) { bool wasAnythingSet = false; string variableToSet = null; StateSave stateSave = SelectedState.Self.SelectedStateSave; float valueToSet = 0; var wereUnitValuesChanged = changedMember == "X Units" || changedMember == "Y Units" || changedMember == "Width Units" || changedMember == "Height Units"; var shouldAttemptValueChange = wereUnitValuesChanged && ProjectManager.Self.GumProjectSave?.ConvertVariablesOnUnitTypeChange == true; if (shouldAttemptValueChange) { GeneralUnitType oldValue; if (UnitConverter.TryConvertToGeneralUnit(oldValueAsObject, out oldValue)) { IRenderableIpso currentIpso = WireframeObjectManager.Self.GetSelectedRepresentation(); float parentWidth = ObjectFinder.Self.GumProjectSave.DefaultCanvasWidth; float parentHeight = ObjectFinder.Self.GumProjectSave.DefaultCanvasHeight; float fileWidth = 0; float fileHeight = 0; float thisWidth = 0; float thisHeight = 0; if (currentIpso != null) { currentIpso.GetFileWidthAndHeightOrDefault(out fileWidth, out fileHeight); if (currentIpso.Parent != null) { parentWidth = currentIpso.Parent.Width; parentHeight = currentIpso.Parent.Height; } thisWidth = currentIpso.Width; thisHeight = currentIpso.Height; } float outX = 0; float outY = 0; bool isWidthOrHeight = false; object unitTypeAsObject = EditingManager.GetCurrentValueForVariable(changedMember, SelectedState.Self.SelectedInstance); GeneralUnitType unitType = UnitConverter.ConvertToGeneralUnit(unitTypeAsObject); XOrY xOrY = XOrY.X; if (changedMember == "X Units") { variableToSet = "X"; xOrY = XOrY.X; } else if (changedMember == "Y Units") { variableToSet = "Y"; xOrY = XOrY.Y; } else if (changedMember == "Width Units") { variableToSet = "Width"; isWidthOrHeight = true; xOrY = XOrY.X; } else if (changedMember == "Height Units") { variableToSet = "Height"; isWidthOrHeight = true; xOrY = XOrY.Y; } float valueOnObject = 0; if (AttemptToPersistPositionsOnUnitChanges && stateSave.TryGetValue <float>(GetQualifiedName(variableToSet), out valueOnObject)) { var defaultUnitType = GeneralUnitType.PixelsFromSmall; if (xOrY == XOrY.X) { UnitConverter.Self.ConvertToPixelCoordinates( valueOnObject, 0, oldValue, defaultUnitType, parentWidth, parentHeight, fileWidth, fileHeight, out outX, out outY); UnitConverter.Self.ConvertToUnitTypeCoordinates( outX, outY, unitType, defaultUnitType, thisWidth, thisHeight, parentWidth, parentHeight, fileWidth, fileHeight, out valueToSet, out outY); } else { UnitConverter.Self.ConvertToPixelCoordinates( 0, valueOnObject, defaultUnitType, oldValue, parentWidth, parentHeight, fileWidth, fileHeight, out outX, out outY); UnitConverter.Self.ConvertToUnitTypeCoordinates( outX, outY, defaultUnitType, unitType, thisWidth, thisHeight, parentWidth, parentHeight, fileWidth, fileHeight, out outX, out valueToSet); } wasAnythingSet = true; } } } if (wasAnythingSet && AttemptToPersistPositionsOnUnitChanges && !float.IsPositiveInfinity(valueToSet)) { InstanceSave instanceSave = SelectedState.Self.SelectedInstance; string unqualifiedVariableToSet = variableToSet; if (SelectedState.Self.SelectedInstance != null) { variableToSet = SelectedState.Self.SelectedInstance.Name + "." + variableToSet; } stateSave.SetValue(variableToSet, valueToSet, instanceSave); // Force update everything on the spot. We know we can just set this value instead of forcing a full refresh: var gue = WireframeObjectManager.Self.GetSelectedRepresentation(); if (gue != null) { gue.SetProperty(unqualifiedVariableToSet, valueToSet); } GumCommands.Self.GuiCommands.RefreshPropertyGrid(force: true); } }
private void ReactIfChangedMemberIsSourceFile(ElementSave parentElement, InstanceSave instance, string changedMember, object oldValue) { ////////////Early Out ///////////////////////////// string variableFullName; var instancePrefix = instance != null ? $"{instance.Name}." : ""; variableFullName = $"{instancePrefix}{changedMember}"; VariableSave variable = SelectedState.Self.SelectedStateSave?.GetVariableSave(variableFullName); bool isSourcefile = variable?.GetRootName() == "SourceFile"; if (!isSourcefile) { return; } string errorMessage = GetWhySourcefileIsInvalid(variable.Value as string); if (!string.IsNullOrEmpty(errorMessage)) { MessageBox.Show(errorMessage); variable.Value = oldValue; } else { string value; value = variable.Value as string; StateSave stateSave = SelectedState.Self.SelectedStateSave; if (!string.IsNullOrEmpty(value)) { var filePath = new FilePath(ProjectState.Self.ProjectDirectory + value); // See if this is relative to the project var shouldAskToCopy = !FileManager.IsRelativeTo( filePath.FullPath, ProjectState.Self.ProjectDirectory); if (shouldAskToCopy && !string.IsNullOrEmpty(ProjectState.Self.GumProjectSave?.ParentProjectRoot) && FileManager.IsRelativeTo(filePath.FullPath, ProjectState.Self.ProjectDirectory + ProjectState.Self.GumProjectSave.ParentProjectRoot)) { shouldAskToCopy = false; } if (shouldAskToCopy) { bool shouldCopy = AskIfShouldCopy(variable, value); if (shouldCopy) { PerformCopy(variable, value); } } if (filePath.Extension == "achx") { stateSave.SetValue($"{instancePrefix}Texture Address", Gum.Managers.TextureAddress.Custom); GumCommands.Self.GuiCommands.RefreshPropertyGrid(force: true); } } stateSave.SetValue($"{instancePrefix}AnimationFrames", new List <string>()); } }
private void ReactIfChangedMemberIsSourceFile(ElementSave parentElement, InstanceSave instance, string changedMember, object oldValue) { string variableFullName; if (instance != null) { variableFullName = $"{instance.Name}.{changedMember}"; } else { variableFullName = changedMember; } VariableSave variable = SelectedState.Self.SelectedStateSave?.GetVariableSave(variableFullName); bool isSourcefile = variable?.GetRootName() == "SourceFile"; string errorMessage = null; if (isSourcefile) { errorMessage = GetWhySourcefileIsInvalid(variable.Value as string); if (!string.IsNullOrEmpty(errorMessage)) { MessageBox.Show(errorMessage); variable.Value = oldValue; } else { string value; value = variable.Value as string; if (!string.IsNullOrEmpty(value)) { var fullValue = new FilePath(ProjectState.Self.ProjectDirectory + value); // See if this is relative to the project var isRelativeToProject = FileManager.IsRelativeTo( fullValue.FullPath, ProjectState.Self.ProjectDirectory); if (!isRelativeToProject) { bool shouldCopy = AskIfShouldCopy(variable, value); if (shouldCopy) { PerformCopy(variable, value); } } } StateSave stateSave = SelectedState.Self.SelectedStateSave; RecursiveVariableFinder rvf = new RecursiveVariableFinder(stateSave); stateSave.SetValue("AnimationFrames", new List <string>()); } } }
private void ReactIfChangedMemberIsUnitType(ElementSave parentElement, string changedMember, object oldValueAsObject) { bool wasAnythingSet = false; string variableToSet = null; StateSave stateSave = SelectedState.Self.SelectedStateSave; float valueToSet = 0; if (changedMember == "X Units" || changedMember == "Y Units" || changedMember == "Width Units" || changedMember == "Height Units") { GeneralUnitType oldValue; if (UnitConverter.TryConvertToGeneralUnit(oldValueAsObject, out oldValue)) { IRenderableIpso currentIpso = WireframeObjectManager.Self.GetSelectedRepresentation(); float parentWidth = ObjectFinder.Self.GumProjectSave.DefaultCanvasWidth; float parentHeight = ObjectFinder.Self.GumProjectSave.DefaultCanvasHeight; float fileWidth = 0; float fileHeight = 0; float thisWidth = 0; float thisHeight = 0; if (currentIpso != null) { currentIpso.GetFileWidthAndHeightOrDefault(out fileWidth, out fileHeight); if (currentIpso.Parent != null) { parentWidth = currentIpso.Parent.Width; parentHeight = currentIpso.Parent.Height; } thisWidth = currentIpso.Width; thisHeight = currentIpso.Height; } float outX = 0; float outY = 0; bool isWidthOrHeight = false; object unitTypeAsObject = EditingManager.GetCurrentValueForVariable(changedMember, SelectedState.Self.SelectedInstance); GeneralUnitType unitType = UnitConverter.ConvertToGeneralUnit(unitTypeAsObject); XOrY xOrY = XOrY.X; if (changedMember == "X Units") { variableToSet = "X"; xOrY = XOrY.X; } else if (changedMember == "Y Units") { variableToSet = "Y"; xOrY = XOrY.Y; } else if (changedMember == "Width Units") { variableToSet = "Width"; isWidthOrHeight = true; xOrY = XOrY.X; } else if (changedMember == "Height Units") { variableToSet = "Height"; isWidthOrHeight = true; xOrY = XOrY.Y; } float valueOnObject = 0; if (stateSave.TryGetValue <float>(GetQualifiedName(variableToSet), out valueOnObject)) { var defaultUnitType = GeneralUnitType.PixelsFromSmall; if (xOrY == XOrY.X) { UnitConverter.Self.ConvertToPixelCoordinates( valueOnObject, 0, oldValue, defaultUnitType, parentWidth, parentHeight, fileWidth, fileHeight, out outX, out outY); UnitConverter.Self.ConvertToUnitTypeCoordinates( outX, outY, unitType, defaultUnitType, thisWidth, thisHeight, parentWidth, parentHeight, fileWidth, fileHeight, out valueToSet, out outY); } else { UnitConverter.Self.ConvertToPixelCoordinates( 0, valueOnObject, defaultUnitType, oldValue, parentWidth, parentHeight, fileWidth, fileHeight, out outX, out outY); UnitConverter.Self.ConvertToUnitTypeCoordinates( outX, outY, defaultUnitType, unitType, thisWidth, thisHeight, parentWidth, parentHeight, fileWidth, fileHeight, out outX, out valueToSet); } wasAnythingSet = true; } } } if (wasAnythingSet) { InstanceSave instanceSave = SelectedState.Self.SelectedInstance; if (SelectedState.Self.SelectedInstance != null) { variableToSet = SelectedState.Self.SelectedInstance.Name + "." + variableToSet; } stateSave.SetValue(variableToSet, valueToSet, instanceSave); } }
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; } StateSave currentStateSave = SelectedState.Self.SelectedStateSave; VariableSave variableSave = this.VariableSave; bool tempVariable = false; 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); if (variableInDefault != null) { string variableType = variableInDefault.Type; currentStateSave.SetValue(variableName, null, instanceSave, variableType); // Now the variable should be created so we can access it variableSave = VariableSave; tempVariable = true; } } 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; tempVariable = false; GumCommands.Self.FileCommands.TryAutoSaveCurrentElement(); GumCommands.Self.GuiCommands.RefreshPropertyGrid(force: true); } } } if (tempVariable) { currentStateSave.Variables.Remove(variableSave); } }
private void ReactIfChangedMemberIsTexture(ElementSave parentElement, string changedMember, object oldValue) { VariableSave variable = SelectedState.Self.SelectedVariableSave; // Eventually need to handle tunneled variables if (variable != null && variable.GetRootName() == "SourceFile") { string value = variable.Value as string; if (!string.IsNullOrEmpty(value)) { // See if this is relative to the project bool isRelativeToProject = !value.StartsWith("../") && !value.StartsWith("..\\"); if (!isRelativeToProject) { // Ask the user what to do - make it relative? MultiButtonMessageBox mbmb = new MultiButtonMessageBox(); mbmb.MessageText = "The file\n" + value + "\nis not relative to the project. What would you like to do?"; mbmb.AddButton("Reference the file in its current location", DialogResult.OK); mbmb.AddButton("Copy the file relative to the Gum project and reference the copy", DialogResult.Yes); var dialogResult = mbmb.ShowDialog(); bool shouldCopy = false; string directory = FileManager.GetDirectory(ProjectManager.Self.GumProjectSave.FullFileName); string targetAbsoluteFile = directory + FileManager.RemovePath(value); if (dialogResult == DialogResult.Yes) { shouldCopy = true; // If the destination already exists, we gotta ask the user what they want to do. if (System.IO.File.Exists(targetAbsoluteFile)) { mbmb = new MultiButtonMessageBox(); mbmb.MessageText = "The destination file already exists. Would you like to overwrite it?"; mbmb.AddButton("Yes", DialogResult.Yes); mbmb.AddButton("No, use the original file", DialogResult.No); shouldCopy = mbmb.ShowDialog() == DialogResult.Yes; } } if (shouldCopy) { try { string sourceAbsoluteFile = directory + value; sourceAbsoluteFile = FileManager.RemoveDotDotSlash(sourceAbsoluteFile); System.IO.File.Copy(sourceAbsoluteFile, targetAbsoluteFile, overwrite: true); variable.Value = FileManager.RemovePath(value); } catch (Exception e) { MessageBox.Show("Error copying file:\n" + e.ToString()); } } } } StateSave stateSave = SelectedState.Self.SelectedStateSave; RecursiveVariableFinder rvf = new RecursiveVariableFinder(stateSave); stateSave.SetValue("AnimationFrames", new List <string>()); } }
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 { var elementSave = SelectedState.Self.SelectedElement; // if there is an inactive variable, // we should get rid of it: var existingVariable = SelectedState.Self.SelectedElement.GetVariableFromThisOrBase(tiw.Result); // there's a variable but we shouldn't consider it // unless it's "Active" - inactive variables may be // leftovers from a type change if (existingVariable != null) { var isActive = VariableSaveLogic.GetIfVariableIsActive(existingVariable, elementSave, null); if (isActive == false) { // gotta remove the variable: if (elementSave.DefaultState.Variables.Contains(existingVariable)) { // We may need to worry about inheritance...eventually elementSave.DefaultState.Variables.Remove(existingVariable); } } } variableSave.ExposedAsName = tiw.Result; GumCommands.Self.FileCommands.TryAutoSaveCurrentElement(); GumCommands.Self.GuiCommands.RefreshPropertyGrid(force: true); } } } }