private void GenerateSetter(string propertyName, VariableSave variable, ICodeBlock property, string variableName, string whatToGetOrSet, ElementSave elementSave) { var setter = property.Set(); bool wasHandled = TryHandleCustomSetter(variable, elementSave, setter); if (!wasHandled) { if (mStandardSetterReplacements.ContainsKey(variable.Name)) { mStandardSetterReplacements[variable.Name](setter); } else { string rightSide = "value"; rightSide = AdjustStandardElementVariableSetIfNecessary(variable, rightSide); setter.Line(whatToGetOrSet + " = " + rightSide + ";"); } if (variablesToCallLayoutAfter.Contains(variable.Name)) { setter.Line("UpdateLayout();"); } } }
public static void ConvertEnumerationValuesToInts(this VariableSave variableSave) { if (variableSave.Value != null) { switch (variableSave.Type) { case "DimensionUnitType": case "Gum.DataTypes.DimensionUnitType": case "VerticalAlignment": case "RenderingLibrary.Graphics.VerticalAlignment": case "HorizontalAlignment": case "RenderingLibrary.Graphics.HorizontalAlignment": case "PositionUnitType": case "Gum.Managers.PositionUnitType": case "GeneralUnitType": case "Gum.Converters.GeneralUnitType": case "Gum.RenderingLibrary.Blend": case "Blend": case "Gum.Managers.TextureAddress": case "TextureAddress": case "Gum.Managers.ChildrenLayout": case "ChildrenLayout": variableSave.Value = (int)variableSave.Value; break; } } }
private void AddAssignmentForInterpolationForVariable(ICodeBlock curBlock, VariableSave variable, ElementSave container) { string memberNameInCode = variable.MemberNameInCode(container, VariableNamesToReplaceForStates); ElementSave categoryContainer; StateSaveCategory stateSaveCategory; if (variable.IsState(container, out categoryContainer, out stateSaveCategory, recursive: false) && !string.IsNullOrEmpty(variable.SourceObject)) { string line = string.Format( "{0}.InterpolateBetween({1}FirstValue, {1}SecondValue, interpolationValue);", SaveObjectExtensionMethods.InstanceNameInCode(variable.SourceObject), memberNameInCode.Replace(".", "")); curBlock.Line(line); } else { switch (variable.Type) { case "int": curBlock.Line(string.Format("{0} = FlatRedBall.Math.MathFunctions.RoundToInt({1}FirstValue* (1 - interpolationValue) + {1}SecondValue * interpolationValue);", memberNameInCode, memberNameInCode.Replace(".", ""))); break; case "float": case "double": curBlock.Line(string.Format("{0} = {1}FirstValue * (1 - interpolationValue) + {1}SecondValue * interpolationValue;", memberNameInCode, memberNameInCode.Replace(".", ""))); break; } } }
private static bool AskIfShouldCopy(VariableSave variable, string value) { // 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; } } return(shouldCopy); }
private static VariableSave TryGetVariableFromStateOnInstance(InstanceSave instance, string variable, IEnumerable <StateSave> statesToPullFrom, string stateVariableName, StateSave fallbackState, List <StateSave> statesToLoopThrough) { VariableSave foundVariableSave = null; // Let's see if this is in a non-default state string thisState = null; foreach (var stateToPullFrom in statesToPullFrom) { var foundStateVariable = stateToPullFrom.GetVariableSave(instance.Name + "." + stateVariableName); if (foundStateVariable != null && foundStateVariable.SetsValue) { thisState = foundStateVariable.Value as string; } } StateSave instanceStateToPullFrom = fallbackState; // if thisState is not null, then the state is being explicitly set, so let's try to get that state if (!string.IsNullOrEmpty(thisState) && statesToLoopThrough.Any(item => item.Name == thisState)) { instanceStateToPullFrom = statesToLoopThrough.First(item => item.Name == thisState); } if (instanceStateToPullFrom != null) { // Eventually use the instanceBase's current state value foundVariableSave = instanceStateToPullFrom.GetVariableRecursive(variable); } return(foundVariableSave); }
public static object GetValueFromThisOrBase(this InstanceSave instance, List <ElementWithState> elementStack, string variable, bool forceDefault = false) { ElementWithState parentContainer = elementStack.Last(); VariableSave variableSave = instance.GetVariableFromThisOrBase(parentContainer, variable, forceDefault, true); if (variableSave != null) { return(variableSave.Value); } else { VariableListSave variableListSave = parentContainer.Element.DefaultState.GetVariableListSave(instance.Name + "." + variable); if (variableListSave == null) { ElementSave instanceBase = ObjectFinder.Self.GetElementSave(instance.BaseType); if (instanceBase != null) { variableListSave = instanceBase.DefaultState.GetVariableListSave(variable); } } if (variableListSave != null) { return(variableListSave.ValueAsIList); } } // If we get ehre that means there isn't any VariableSave or VariableListSave return(null); }
public List <VariableSave> GetExposedVariablesForThisInstance(DataTypes.InstanceSave instance, string parentInstanceName, List <ElementWithState> elementStack, string requiredName) { List <VariableSave> exposedVariables = new List <VariableSave>(); if (elementStack.Count > 1) { ElementWithState containerOfVariables = elementStack[elementStack.Count - 2]; ElementWithState definerOfVariables = elementStack[elementStack.Count - 1]; foreach (VariableSave variable in definerOfVariables.Element.DefaultState.Variables.Where( item => !string.IsNullOrEmpty(item.ExposedAsName) && item.GetRootName() == requiredName)) { if (variable.SourceObject == instance.Name) { // This variable is exposed, let's see if the container does anything with it VariableSave foundVariable = containerOfVariables.StateSave.GetVariableRecursive( parentInstanceName + "." + variable.ExposedAsName); if (foundVariable != null) { if (!string.IsNullOrEmpty(foundVariable.ExposedAsName)) { // This variable is itself exposed, so we should go up one level to see // what's going on. var instanceInParent = containerOfVariables.Element.GetInstance(parentInstanceName); var parentparentInstanceName = containerOfVariables.InstanceName; List <ElementWithState> stackWithLastRemoved = new List <ElementWithState>(); stackWithLastRemoved.AddRange(elementStack); stackWithLastRemoved.RemoveAt(stackWithLastRemoved.Count - 1); var exposedExposed = GetExposedVariablesForThisInstance(instanceInParent, parentparentInstanceName, stackWithLastRemoved, // This used to be this: //foundVariable.ExposedAsName // But it should be this: variable.ExposedAsName ); if (exposedExposed.Count != 0) { foundVariable = exposedExposed.First(); } } VariableSave variableToAdd = new VariableSave(); variableToAdd.Type = variable.Type; variableToAdd.Value = foundVariable.Value; variableToAdd.SetsValue = foundVariable.SetsValue; variableToAdd.Name = variable.Name.Substring(variable.Name.IndexOf('.') + 1); exposedVariables.Add(variableToAdd); } } } } return(exposedVariables); }
private static bool GetIfShouldIncludeAccordingToDefaultState(VariableSave defaultVariable, ElementSave container, InstanceSave currentInstance) { bool canOnlyBeSetInDefaultState = defaultVariable.CanOnlyBeSetInDefaultState; if (currentInstance != null) { var root = ObjectFinder.Self.GetRootStandardElementSave(currentInstance); if (root != null && root.GetVariableFromThisOrBase(defaultVariable.Name, true) != null) { var foundVariable = root.GetVariableFromThisOrBase(defaultVariable.Name, true); canOnlyBeSetInDefaultState = foundVariable.CanOnlyBeSetInDefaultState; } } else if (container != null) { var root = ObjectFinder.Self.GetRootStandardElementSave(container); if (root != null && root.GetVariableFromThisOrBase(defaultVariable.Name, true) != null) { canOnlyBeSetInDefaultState = root.GetVariableFromThisOrBase(defaultVariable.Name, true).CanOnlyBeSetInDefaultState; } } bool shouldInclude = true; bool isDefault = SelectedState.Self.SelectedStateSave == SelectedState.Self.SelectedElement.DefaultState; if (!isDefault && canOnlyBeSetInDefaultState) { shouldInclude = false; } return(shouldInclude); }
private static string GetCodeLine(InstanceSave instance, VariableSave variable, ElementSave container, VisualApi visualApi, StateSave state) { string instancePrefix = instance != null ? $"{instance.Name}." : "this."; if (visualApi == VisualApi.Gum) { var fullLineReplacement = TryGetFullGumLineReplacement(instance, variable); if (fullLineReplacement != null) { return(fullLineReplacement); } else { return($"{instancePrefix}{GetGumVariableName(variable, container)} = {VariableValueToGumCodeValue(variable, container)};"); } } else // xamarin forms { var fullLineReplacement = TryGetFullXamarinFormsLineReplacement(instance, container, variable, state); if (fullLineReplacement != null) { return(fullLineReplacement); } else { return($"{instancePrefix}{GetXamarinFormsVariableName(variable)} = {VariableValueToXamarinFormsCodeValue(variable, container)};"); } } }
public object GetValue(string variableName) { switch (ContainerType) { case VariableContainerType.InstanceSave: #if DEBUG if (ElementStack.Count != 0) { if (ElementStack.Last().Element == null) { throw new InvalidOperationException("The ElementStack contains an ElementWithState with no Element"); } } #endif VariableSave variable = GetVariable(variableName); if (variable != null) { return(variable.Value); } else { return(null); } //return mInstanceSave.GetValueFromThisOrBase(mElementStack, variableName); //break; case VariableContainerType.StateSave: return(mStateSave.GetValueRecursive(variableName)); //break; } throw new NotImplementedException(); }
public static bool IsState(this VariableSave variableSave, ElementSave container) { ElementSave throwaway1; StateSaveCategory throwaway2; return(variableSave.IsState(container, out throwaway1, out throwaway2)); }
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)) { // See if this is relative to the project var isRelativeToProject = FileManager.IsRelativeTo( value, 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>()); } } }
public void CallFillVariableAttributes(VariableSave variableSave, List <Attribute> listToFill) { if (FillVariableAttributes != null) { FillVariableAttributes(variableSave, listToFill); } }
public static bool IsEnumeration(this VariableSave variableSave) { string type = variableSave.Type; switch (type) { case "string": return(false); //break; case "int": return(false); //break; case "float": return(false); //break; case "bool": return(false); //break; } return(true); }
private static VariableSave TryGetVariableFromStatesOnInstance(InstanceSave instance, string variable, ElementSave instanceBase, IEnumerable <StateSave> statesToPullFrom) { string stateVariableName; StateSave fallbackState; List <StateSave> statesToLoopThrough; VariableSave foundVariableSave = null; foreach (var stateCategory in instanceBase.Categories) { stateVariableName = stateCategory.Name + "State"; fallbackState = null; statesToLoopThrough = stateCategory.States; foundVariableSave = TryGetVariableFromStateOnInstance(instance, variable, statesToPullFrom, stateVariableName, fallbackState, statesToLoopThrough); } if (foundVariableSave == null) { stateVariableName = "State"; fallbackState = instanceBase.DefaultState; statesToLoopThrough = instanceBase.States; foundVariableSave = TryGetVariableFromStateOnInstance(instance, variable, statesToPullFrom, stateVariableName, fallbackState, statesToLoopThrough); } return(foundVariableSave); }
private static bool GetIfShouldInclude(VariableSave defaultVariable, ElementSave container, InstanceSave currentInstance, StandardElementSave rootElementSave) { bool shouldInclude = GetIfShouldIncludeAccordingToDefaultState(defaultVariable, container, currentInstance); if (shouldInclude) { shouldInclude = GetShouldIncludeBasedOnAttachments(defaultVariable, container, currentInstance); } if (shouldInclude) { shouldInclude = GetShouldIncludeBasedOnBaseType(defaultVariable, container, currentInstance, rootElementSave); } if (shouldInclude) { RecursiveVariableFinder rvf; if (currentInstance != null) { rvf = new RecursiveVariableFinder(currentInstance, container); } else { rvf = new RecursiveVariableFinder(container.DefaultState); } shouldInclude = !PluginManager.Self.ShouldExclude(defaultVariable, rvf); } return(shouldInclude); }
private void HandleAddVariable(object sender, EventArgs e) { var window = new AddVariableWindow(); var result = window.ShowDialog(); if (result == true) { var type = window.SelectedType; var name = window.EnteredName; string whyNotValid; bool isValid = NameVerifier.Self.IsVariableNameValid( name, out whyNotValid); if (!isValid) { MessageBox.Show(whyNotValid); } else { var behavior = SelectedState.Self.SelectedBehavior; var newVariable = new VariableSave(); newVariable.Name = name; newVariable.Type = type; behavior.RequiredVariables.Variables.Add(newVariable); GumCommands.Self.GuiCommands.RefreshPropertyGrid(); GumCommands.Self.FileCommands.TryAutoSaveBehavior(behavior); } } }
string AdjustStandardElementVariableGetIfNecessary(VariableSave variableSave, string value) { if (variableSave.Type == "Blend") { value = "Gum.RenderingLibrary.BlendExtensions.ToBlend(" + value + ")"; } return(value); }
private static void ApplyTextureTop(List <VariableSave> gumVariables, float value, FlatRedBall.Glue.SaveClasses.NamedObjectSave namedObject) { VariableSave variableSave = new VariableSave(); variableSave.Name = $"{namedObject.InstanceName}.Texture Top"; variableSave.Type = "int"; variableSave.Value = RenderingLibrary.Math.MathFunctions.RoundToInt(value); gumVariables.Add(variableSave); }
private static void ApplyTextureAddress(List <VariableSave> gumVariables, FlatRedBall.Glue.SaveClasses.NamedObjectSave namedObject, TextureAddress textureAddress) { VariableSave variableSave = new VariableSave(); variableSave.Name = $"{namedObject.InstanceName}.Texture Address"; variableSave.Type = nameof(Gum.Managers.TextureAddress); variableSave.Value = textureAddress; gumVariables.Add(variableSave); }
public static void AddPositioningVariables(StateSave stateSave, bool addOriginVariables = true, bool includeBaseline = false) { List <object> xUnitsExclusions = new List <object>(); xUnitsExclusions.Add(PositionUnitType.PixelsFromTop); xUnitsExclusions.Add(PositionUnitType.PercentageHeight); xUnitsExclusions.Add(PositionUnitType.PixelsFromBottom); xUnitsExclusions.Add(PositionUnitType.PixelsFromCenterY); xUnitsExclusions.Add(PositionUnitType.PixelsFromCenterYInverted); xUnitsExclusions.Add(PositionUnitType.PixelsFromBaseline); List <object> yUnitsExclusions = new List <object>(); yUnitsExclusions.Add(PositionUnitType.PixelsFromLeft); yUnitsExclusions.Add(PositionUnitType.PixelsFromCenterX); yUnitsExclusions.Add(PositionUnitType.PercentageWidth); yUnitsExclusions.Add(PositionUnitType.PixelsFromRight); stateSave.Variables.Add(new VariableSave { SetsValue = true, Type = "float", Value = 0.0f, Name = "X", Category = "Position" }); stateSave.Variables.Add(new VariableSave { SetsValue = true, Type = typeof(PositionUnitType).Name, Value = PositionUnitType.PixelsFromLeft, Name = "X Units", Category = "Position", ExcludedValuesForEnum = xUnitsExclusions }); stateSave.Variables.Add(new VariableSave { SetsValue = true, Type = "float", Value = 0.0f, Name = "Y", Category = "Position" }); stateSave.Variables.Add(new VariableSave { SetsValue = true, Type = typeof(PositionUnitType).Name, Value = PositionUnitType.PixelsFromTop, Name = "Y Units", Category = "Position", ExcludedValuesForEnum = yUnitsExclusions }); if (addOriginVariables) { stateSave.Variables.Add(new VariableSave { SetsValue = true, Type = nameof(HorizontalAlignment), Value = HorizontalAlignment.Left, Name = "X Origin", Category = "Position" }); var verticalAlignmentVariable = new VariableSave { SetsValue = true, Type = nameof(VerticalAlignment), Value = VerticalAlignment.Top, Name = "Y Origin", Category = "Position" }; if (includeBaseline == false) { verticalAlignmentVariable.ExcludedValuesForEnum.Add(VerticalAlignment.TextBaseline); } stateSave.Variables.Add(verticalAlignmentVariable); } stateSave.Variables.Add(new VariableSave { SetsValue = true, Type = "string", Value = null, Name = "Guide", Category = "Position" }); #if GUM AddParentVariables(stateSave); #endif }
private static void TryDisplayVariableSave(List <InstanceSavePropertyDescriptor> pdc, ElementSave elementSave, InstanceSave instanceSave, AmountToDisplay amountToDisplay, VariableSave defaultVariable) { ElementSave container = elementSave; if (instanceSave != null) { container = instanceSave.ParentContainer; } // Not sure why we were passing elementSave to this function: // I added a container object //bool shouldInclude = GetIfShouldInclude(defaultVariable, elementSave, instanceSave, ses); bool shouldInclude = Gum.Logic.VariableSaveLogic.GetIfVariableIsActive(defaultVariable, container, instanceSave); shouldInclude &= ( string.IsNullOrEmpty(defaultVariable.SourceObject) || amountToDisplay == AmountToDisplay.AllVariables || !string.IsNullOrEmpty(defaultVariable.ExposedAsName)); if (shouldInclude) { TypeConverter typeConverter = defaultVariable.GetTypeConverter(elementSave); Attribute[] customAttributes = GetAttributesForVariable(defaultVariable); string category = null; if (!string.IsNullOrEmpty(defaultVariable.Category)) { category = defaultVariable.Category; } else if (!string.IsNullOrEmpty(defaultVariable.ExposedAsName)) { category = "Exposed"; } //Type type = typeof(string); Type type = Gum.Reflection.TypeManager.Self.GetTypeFromString(defaultVariable.Type); string name = defaultVariable.Name; if (!string.IsNullOrEmpty(defaultVariable.ExposedAsName)) { name = defaultVariable.ExposedAsName; } var property = mHelper.AddProperty(pdc, name, type, typeConverter, //, customAttributes ); property.Category = category; } }
public static VariableSave Clone(this VariableSave whatToClone) { var toReturn = FileManager.CloneSaveObject <VariableSave>(whatToClone); toReturn.ExcludedValuesForEnum.AddRange(whatToClone.ExcludedValuesForEnum); #if GUM toReturn.FixEnumerations(); #endif return(toReturn); }
internal bool GetIfVariableIsExcluded(VariableSave defaultVariable, RecursiveVariableFinder rvf) { if (VariableExcluded == null) { return(false); } else { return(VariableExcluded(defaultVariable, rvf)); } }
public string GetEventName(VariableSave variable, ElementSave container) { if (!string.IsNullOrEmpty(variable.ExposedAsName)) { return($"{variable.ExposedAsName.Replace(" ", "")}Changed"); } else { return($"{variable.MemberNameInCode(container)}Changed"); } }
private bool IsVariableNumeric(VariableSave variable) { string type = variable.Type; return(type == "float" || type == "int" || type == "double" || type == "byte" || type == "decimal" || type == "long"); }
public StateReferencingInstanceMember(InstanceSavePropertyDescriptor ispd, StateSave stateSave, string variableName, InstanceSave instanceSave, ElementSave elementSave) : base(variableName, stateSave) { mInstanceSave = instanceSave; mStateSave = stateSave; mVariableName = variableName; mPropertyDescriptor = ispd; mElementSave = elementSave; this.CustomGetEvent += GetEvent; this.CustomSetEvent += SetEvent; this.CustomGetTypeEvent += GetTypeEvent; this.SortValue = int.MaxValue; if (instanceSave != null) { this.Instance = instanceSave; } else { this.Instance = elementSave; } DisplayName = RootVariableName; TryAddExposeVariableMenuOptions(instanceSave); // This could be slow since we have to check it for every variable in an object. // Maybe we'll want to pass this in to the function? StandardElementSave standardElement = null; if (instanceSave != null) { standardElement = ObjectFinder.Self.GetRootStandardElementSave(instanceSave); } else { standardElement = ObjectFinder.Self.GetRootStandardElementSave(elementSave); } VariableSave standardVariable = null; if (standardElement != null) { standardVariable = standardElement.DefaultState.Variables.FirstOrDefault(item => item.Name == RootVariableName); } if (standardVariable != null) { this.SortValue = standardVariable.DesiredOrder; } }
private void HandleUnexposeVariableClick(object sender, System.Windows.RoutedEventArgs e) { // Find this variable in the source instance and make it not exposed VariableSave variableSave = this.VariableSave; if (variableSave != null) { variableSave.ExposedAsName = null; GumCommands.Self.FileCommands.TryAutoSaveCurrentElement(); PropertyGridManager.Self.RefreshUI(); } }
private void ModifyVariableTypeForProperty(ref string variableType, VariableSave variableSave, ElementSave elementSave) { if (mTypeToQualifiedTypes.ContainsKey(variableType)) { variableType = mTypeToQualifiedTypes[variableType]; } if (string.IsNullOrEmpty(variableSave.SourceObject)) { if (variableType == "State") { // Not sure why this was returning CurrentVariableState, as that is the property name, // not the property type, and here we want the property type. //variableType = elementSave.Name + "Runtime.CurrentVariableState"; variableType = FlatRedBall.IO.FileManager.RemovePath(elementSave.Name) + "Runtime.VariableState"; } else if (variableSave.Type.EndsWith("State")) { var typeWithoutState = variableSave.Type.Substring(0, variableSave.Type.Length - "State".Length); var foundCategory = elementSave.Categories.FirstOrDefault(item => item.Name == typeWithoutState); if (foundCategory != null) { variableType = FlatRedBall.IO.FileManager.RemovePath(elementSave.Name) + "Runtime." + foundCategory.Name; } } } else if (variableSave.IsFile) { variableType = "Microsoft.Xna.Framework.Graphics.Texture2D"; } else { var instance = elementSave.Instances.FirstOrDefault(item => item.Name == variableSave.SourceObject); if (instance != null) { var element = ObjectFinder.Self.GetElementSave(instance); if (element != null) { var rootName = variableSave.GetRootName(); var variableInInstanceElement = element.DefaultState.Variables.FirstOrDefault(item => item.Name == rootName || item.ExposedAsName == rootName); if (variableInInstanceElement != null) { ModifyVariableTypeForProperty(ref variableType, variableInInstanceElement, element); } } } } }
public static bool GetIsFileFromRoot(this VariableSave variable, ElementSave element) { var variableInRoot = element.DefaultState.Variables.FirstOrDefault(item => item.Name == variable.GetRootName()); if (variableInRoot != null) { return(variableInRoot.IsFile); } else { // unknown so assume no return(false); } }