private static float AdjustAmountAccordingToUnitType(string baseVariableName, float amount, object unitsVariableAsObject) { GeneralUnitType generalUnitType = UnitConverter.ConvertToGeneralUnit(unitsVariableAsObject); float xAmount; float yAmount; if (baseVariableName == "X" || baseVariableName == "Width") { xAmount = amount; yAmount = 0; } else { xAmount = 0; yAmount = amount; } if (generalUnitType == GeneralUnitType.PixelsFromMiddleInverted) { return(amount * -1); } else if (generalUnitType != GeneralUnitType.PixelsFromLarge && generalUnitType != GeneralUnitType.PixelsFromMiddle && generalUnitType != GeneralUnitType.PixelsFromSmall) { float parentWidth; float parentHeight; float fileWidth; float fileHeight; float outX; float outY; IRenderableIpso ipso = WireframeObjectManager.Self.GetSelectedRepresentation(); ipso.GetFileWidthAndHeightOrDefault(out fileWidth, out fileHeight); ipso.GetParentWidthAndHeight( ProjectManager.Self.GumProjectSave.DefaultCanvasWidth, ProjectManager.Self.GumProjectSave.DefaultCanvasHeight, out parentWidth, out parentHeight); var unitsVariable = UnitConverter.ConvertToGeneralUnit(unitsVariableAsObject); UnitConverter.Self.ConvertToUnitTypeCoordinates(xAmount, yAmount, unitsVariable, unitsVariable, ipso.Width, ipso.Height, parentWidth, parentHeight, fileWidth, fileHeight, out outX, out outY); if (baseVariableName == "X" || baseVariableName == "Width") { return(outX); } else { return(outY); } } else { return(amount); } }
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 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); } }