private static ICodeBlock SetInterpolateBetweenValuesForStates(IElement element, string enumType, List <StateSave> states, ICodeBlock curBlock, Dictionary <InstructionSave, InterpolationCharacteristic> mInterpolationCharacteristics, string firstOrSecondValue) { foreach (StateSave state in states) { curBlock = curBlock.Case(enumType + "." + state.Name); foreach (InstructionSave instructionSave in state.InstructionSaves) { var customVariable = element.GetCustomVariable(instructionSave.Member); NamedObjectSave sourceNamedObjectSave = null; if (customVariable != null) { sourceNamedObjectSave = element.GetNamedObjectRecursively(customVariable.SourceObject); } if (sourceNamedObjectSave != null) { NamedObjectSaveCodeGenerator.AddIfConditionalSymbolIfNecesssary(curBlock, sourceNamedObjectSave); } if (GetValue(mInterpolationCharacteristics, instructionSave.Member) != InterpolationCharacteristic.CantInterpolate) { if (instructionSave.Value == null) { curBlock.Line("set" + instructionSave.Member + " = false;"); } else { string valueToWrite = GetRightSideAssignmentValueAsString(element, instructionSave); curBlock.Line(instructionSave.Member + firstOrSecondValue + " = " + valueToWrite + ";"); } } else // This value can't be interpolated, but if the user has set a value of 0 or 1, then it should be set { ICodeBlock ifBlock; // value will come from the first state unless the interpolationValue is 1. // This makes the code behave the same as InterpolateTo which uses instructions. if (firstOrSecondValue == FirstValue) { ifBlock = curBlock.If("interpolationValue < 1"); } else { ifBlock = curBlock.If("interpolationValue >= 1"); } string valueToWrite = GetRightSideAssignmentValueAsString(element, instructionSave); ifBlock.Line("this." + instructionSave.Member + " = " + valueToWrite + ";"); } if (sourceNamedObjectSave != null) { NamedObjectSaveCodeGenerator.AddEndIfIfNecessary(curBlock, sourceNamedObjectSave); } } curBlock = curBlock.End(); } return(curBlock); }
private static ICodeBlock AssignValuesUsingStartingValues(IElement element, ICodeBlock curBlock, Dictionary <InstructionSave, InterpolationCharacteristic> mInterpolationCharacteristics) { foreach (KeyValuePair <InstructionSave, InterpolationCharacteristic> kvp in mInterpolationCharacteristics) { if (kvp.Value != InterpolationCharacteristic.CantInterpolate) { curBlock = curBlock.If("set" + kvp.Key.Member); CustomVariable variable = element.GetCustomVariable(kvp.Key.Member); var nos = element.GetNamedObjectRecursively(variable.SourceObject); if (nos != null) { NamedObjectSaveCodeGenerator.AddIfConditionalSymbolIfNecesssary(curBlock, nos); } string relativeValue = InstructionManager.GetRelativeForAbsolute(kvp.Key.Member); string variableToAssign = kvp.Key.Member; string leftSideOfEqualsWithRelative = GetLeftSideOfEquals(element, variable, kvp.Key, true); if (!string.IsNullOrEmpty(leftSideOfEqualsWithRelative) && leftSideOfEqualsWithRelative != kvp.Key.Member) { string beforeDotParent = variable.SourceObject; if (string.IsNullOrEmpty(variable.SourceObject)) { beforeDotParent = "this"; } curBlock = curBlock.If(beforeDotParent + ".Parent != null"); AddAssignmentForInterpolationForVariable(curBlock, variable, variableToAssign, leftSideOfEqualsWithRelative); curBlock = curBlock.End().Else(); } AddAssignmentForInterpolationForVariable(curBlock, variable, variableToAssign, variableToAssign); if (!string.IsNullOrEmpty(relativeValue)) { curBlock = curBlock.End(); // end the else } curBlock = curBlock.End(); if (nos != null) { NamedObjectSaveCodeGenerator.AddEndIfIfNecessary(curBlock, nos); } } } return(curBlock); }
private static CodeBuilder.ICodeBlock GenerateSetToIgnorePausing(CodeBuilder.ICodeBlock codeBlock, SaveClasses.IElement element, bool hasBase) { string virtualOrOverride = "virtual"; if (hasBase) { virtualOrOverride = "override"; } codeBlock = codeBlock.Function("public " + virtualOrOverride + " void", "SetToIgnorePausing", ""); if (hasBase) { codeBlock.Line("base.SetToIgnorePausing();"); } else { codeBlock.Line("FlatRedBall.Instructions.InstructionManager.IgnorePausingFor(this);"); } foreach (NamedObjectSave nos in element.AllNamedObjects) { if (nos.IsFullyDefined && !nos.IsDisabled && !nos.IsContainer) { NamedObjectSaveCodeGenerator.AddIfConditionalSymbolIfNecesssary(codeBlock, nos); bool shouldWrapInNullCheck = nos.SetByDerived || nos.SetByContainer || nos.Instantiate == false; if (shouldWrapInNullCheck) { codeBlock = codeBlock.If(nos.InstanceName + " != null"); } if (nos.GetAssetTypeInfo() != null && nos.GetAssetTypeInfo().CanIgnorePausing) { codeBlock.Line("FlatRedBall.Instructions.InstructionManager.IgnorePausingFor(" + nos.InstanceName + ");"); } else if (nos.SourceType == SourceType.Entity) { codeBlock.Line(nos.InstanceName + ".SetToIgnorePausing();"); } if (shouldWrapInNullCheck) { codeBlock = codeBlock.End(); } NamedObjectSaveCodeGenerator.AddEndIfIfNecessary(codeBlock, nos); } } codeBlock = codeBlock.End(); return(codeBlock); }
private static ICodeBlock GenerateInterpolateForIndividualState(IElement element, ICodeBlock codeBlock, ICodeBlock otherBlock, StateSave stateSave, string enumType) { codeBlock = codeBlock.Case(enumType + "." + stateSave.Name); otherBlock = otherBlock.Case(enumType + "." + stateSave.Name); foreach (InstructionSave instruction in stateSave.InstructionSaves) { CustomVariable customVariable = null; customVariable = element.GetCustomVariable(instruction.Member); string valueAsString = CodeParser.ParseObjectValue(instruction.Value); if (customVariable != null && !string.IsNullOrEmpty(valueAsString)) { NamedObjectSave sourceNamedObjectSave = element.GetNamedObjectRecursively(customVariable.SourceObject); if (sourceNamedObjectSave == null || sourceNamedObjectSave.IsDisabled == false) { if (sourceNamedObjectSave != null) { NamedObjectSaveCodeGenerator.AddIfConditionalSymbolIfNecesssary(codeBlock, sourceNamedObjectSave); NamedObjectSaveCodeGenerator.AddIfConditionalSymbolIfNecesssary(otherBlock, sourceNamedObjectSave); } string timeCastString = ""; if (instruction.Value is float) { timeCastString = "(float)"; } if (string.IsNullOrEmpty(customVariable.SourceObject)) { GenerateInterpolateForIndividualStateNoSource(ref codeBlock, element, ref otherBlock, instruction, customVariable, valueAsString, timeCastString); } else { GenerateInterpolateForIndividualStateWithSource(ref codeBlock, element, ref otherBlock, customVariable, valueAsString, sourceNamedObjectSave, timeCastString); } if (sourceNamedObjectSave != null) { NamedObjectSaveCodeGenerator.AddEndIfIfNecessary(codeBlock, sourceNamedObjectSave); NamedObjectSaveCodeGenerator.AddEndIfIfNecessary(otherBlock, sourceNamedObjectSave); } } } } return(codeBlock); }
public override ICodeBlock GenerateAdditionalMethods(ICodeBlock codeBlock, FlatRedBall.Glue.SaveClasses.IElement element) { //////////////////////////EARLY OUT////////////////////////////////////// if (element is ScreenSave) { return(codeBlock); } ///////////////////////END EARLY OUT///////////////////////////////////// codeBlock = codeBlock.Function("public void", "MoveToLayer", "Layer layerToMoveTo"); foreach (NamedObjectSave nos in element.NamedObjects) { if (!nos.IsDisabled) { if (nos.GetAssetTypeInfo() != null && !string.IsNullOrEmpty(nos.GetAssetTypeInfo().RemoveFromLayerMethod)) { NamedObjectSaveCodeGenerator.AddIfConditionalSymbolIfNecesssary(codeBlock, nos); bool shouldSkip = GetShouldSkip(nos); if (!shouldSkip) { codeBlock.If("LayerProvidedByContainer != null") .Line(nos.GetAssetTypeInfo().RemoveFromLayerMethod.Replace("this", nos.InstanceName).Replace("mLayer", "LayerProvidedByContainer") + ";") .End(); codeBlock.Line(nos.GetAssetTypeInfo().LayeredAddToManagersMethod[0].Replace("this", nos.InstanceName).Replace("mLayer", "layerToMoveTo") + ";"); } NamedObjectSaveCodeGenerator.AddEndIfIfNecessary(codeBlock, nos); } else if (nos.SourceType == SourceType.Entity && !string.IsNullOrEmpty(nos.SourceClassType)) { NamedObjectSaveCodeGenerator.AddIfConditionalSymbolIfNecesssary(codeBlock, nos); codeBlock.Line(nos.InstanceName + ".MoveToLayer(layerToMoveTo);"); NamedObjectSaveCodeGenerator.AddEndIfIfNecessary(codeBlock, nos); } } } codeBlock.Line("LayerProvidedByContainer = layerToMoveTo;"); codeBlock = codeBlock.End(); return(codeBlock); }
private static void GenerateInitializeForEvent(ICodeBlock codeBlock, IElement element, EventResponseSave ers) { bool wasEventAdded = false; //We always want this to happen, even if it's // emtpy //if (!string.IsNullOrEmpty(ers.Contents)) //{ NamedObjectSave sourceNos = null; bool shouldCloseIfStatementForNos = false; if (!string.IsNullOrEmpty(ers.SourceVariable)) { // This is tied to a variable, so the name comes from the variable event rather than the // event name itself string eventName = ers.BeforeOrAfter.ToString() + ers.SourceVariable + "Set"; codeBlock.Line("this." + eventName + " += On" + ers.EventName + ";"); wasEventAdded = true; } else if (string.IsNullOrEmpty(ers.SourceObject) || ers.SourceObject == "<NONE>") { string leftSide = null; EventSave eventSave = ers.GetEventSave(); if (eventSave == null || string.IsNullOrEmpty(eventSave.ExternalEvent)) { leftSide = "this." + ers.EventName; } else { leftSide = eventSave.ExternalEvent; } codeBlock.Line(leftSide + " += On" + ers.EventName + ";"); wasEventAdded = true; } else if (!string.IsNullOrEmpty(ers.SourceObjectEvent)) { // Only append this if the source NOS is fully-defined. If not, we don't want to generate compile errors. sourceNos = element.GetNamedObject(ers.SourceObject); if (sourceNos != null && sourceNos.IsFullyDefined) { NamedObjectSaveCodeGenerator.AddIfConditionalSymbolIfNecesssary(codeBlock, sourceNos); string leftSide = null; leftSide = ers.SourceObject + "." + ers.SourceObjectEvent; codeBlock.Line(leftSide + " += On" + ers.EventName + ";"); wasEventAdded = true; shouldCloseIfStatementForNos = true; } } if (!string.IsNullOrEmpty(ers.SourceObject) && !string.IsNullOrEmpty(ers.SourceObjectEvent) && wasEventAdded) { codeBlock.Line(ers.SourceObject + "." + ers.SourceObjectEvent + " += On" + ers.EventName + "Tunnel;"); if (shouldCloseIfStatementForNos) { NamedObjectSaveCodeGenerator.AddEndIfIfNecessary(codeBlock, sourceNos); } } }
public override ICodeBlock GenerateAdditionalMethods(ICodeBlock codeBlock, FlatRedBall.Glue.SaveClasses.IElement element) { //////////////////////////EARLY OUT////////////////////////////////////// if (element is ScreenSave) { return(codeBlock); } ///////////////////////END EARLY OUT///////////////////////////////////// bool isInDerived = element.InheritsFromElement(); if (isInDerived) { codeBlock = codeBlock.Function("public override void", "MoveToLayer", "FlatRedBall.Graphics.Layer layerToMoveTo"); codeBlock.Line("var layerToRemoveFrom = LayerProvidedByContainer; // assign before calling base so removal is not impacted by base call"); codeBlock.Line("base.MoveToLayer(layerToMoveTo);"); } else { codeBlock = codeBlock.Function("public virtual void", "MoveToLayer", "FlatRedBall.Graphics.Layer layerToMoveTo"); codeBlock.Line("var layerToRemoveFrom = LayerProvidedByContainer;"); } if (element.InheritsFromFrbType()) { AssetTypeInfo ati = AvailableAssetTypes.Self.GetAssetTypeFromRuntimeType(element.BaseElement, element); if (ati != null) { if (ati.RemoveFromLayerMethod != null) { codeBlock.If("layerToRemoveFrom != null") .Line(ati.RemoveFromLayerMethod.Replace("mLayer", "layerToRemoveFrom") + ";") .End(); } if (ati.LayeredAddToManagersMethod.Count != 0) { codeBlock.Line(ati.LayeredAddToManagersMethod[0].Replace("mLayer", "layerToMoveTo") + ";"); } } } foreach (NamedObjectSave nos in element.NamedObjects) { if (!nos.IsDisabled && !nos.IsContainer && !nos.DefinedByBase) { bool shouldCheckForNull = nos.Instantiate == false; if (nos.GetAssetTypeInfo() != null && !string.IsNullOrEmpty(nos.GetAssetTypeInfo().RemoveFromLayerMethod)) { NamedObjectSaveCodeGenerator.AddIfConditionalSymbolIfNecesssary(codeBlock, nos); bool shouldSkip = GetShouldSkip(nos); if (!shouldSkip) { if (shouldCheckForNull) { codeBlock = codeBlock.If(nos.InstanceName + " != null"); } codeBlock.If("layerToRemoveFrom != null") .Line(nos.GetAssetTypeInfo().RemoveFromLayerMethod.Replace("this", nos.InstanceName).Replace("mLayer", "layerToRemoveFrom") + ";") .End(); codeBlock.Line(nos.GetAssetTypeInfo().LayeredAddToManagersMethod[0].Replace("this", nos.InstanceName).Replace("mLayer", "layerToMoveTo") + ";"); if (shouldCheckForNull) { codeBlock = codeBlock.End(); } } NamedObjectSaveCodeGenerator.AddEndIfIfNecessary(codeBlock, nos); } else if (nos.SourceType == SourceType.Entity && !string.IsNullOrEmpty(nos.SourceClassType)) { if (shouldCheckForNull) { codeBlock = codeBlock.If(nos.InstanceName + " != null"); } NamedObjectSaveCodeGenerator.AddIfConditionalSymbolIfNecesssary(codeBlock, nos); codeBlock.Line(nos.InstanceName + ".MoveToLayer(layerToMoveTo);"); NamedObjectSaveCodeGenerator.AddEndIfIfNecessary(codeBlock, nos); if (shouldCheckForNull) { codeBlock = codeBlock.End(); } } } } if (isInDerived == false) { // Doesn't hurt if derived assigns this but...I guess we can keep it clean and only do it in one place codeBlock.Line("LayerProvidedByContainer = layerToMoveTo;"); } codeBlock = codeBlock.End(); return(codeBlock); }
private static ICodeBlock GenerateVariableAssignmentForState(IElement element, ICodeBlock codeBlock, StateSave stateSave, string enumType, bool isElse) { string variableNameModifier = enumType; if (enumType == "VariableState") { variableNameModifier = ""; } ICodeBlock curBlock; if (isElse) { curBlock = codeBlock.ElseIf($"Current{variableNameModifier}State == {enumType}.{stateSave.Name}"); } else { curBlock = codeBlock.If($"Current{variableNameModifier}State == {enumType}.{stateSave.Name}"); } bool doesStateAssignAbsoluteValues = GetDoesStateAssignAbsoluteValues(stateSave, element); foreach (InstructionSave instruction in stateSave.InstructionSaves) { if (instruction.Value != null) { // Get the valueAsString, which is the right-side of the equals sign string rightSideOfEquals = GetRightSideAssignmentValueAsString(element, instruction); if (!string.IsNullOrEmpty(rightSideOfEquals)) { CustomVariable customVariable = element.GetCustomVariableRecursively(instruction.Member); NamedObjectSave referencedNos = element.GetNamedObjectRecursively(customVariable.SourceObject); if (referencedNos != null) { NamedObjectSaveCodeGenerator.AddIfConditionalSymbolIfNecesssary(curBlock, referencedNos); } string leftSideOfEquals = GetLeftSideOfEquals(element, customVariable, instruction, false); string leftSideOfEqualsWithRelative = GetLeftSideOfEquals(element, customVariable, instruction, true); if (leftSideOfEquals != leftSideOfEqualsWithRelative) { string objectWithParent = null; if (string.IsNullOrEmpty(customVariable.SourceObject)) { objectWithParent = "this"; } else { objectWithParent = customVariable.SourceObject; } curBlock .If(objectWithParent + ".Parent == null") .Line(leftSideOfEquals + " = " + rightSideOfEquals + ";") .End() .Else() .Line(leftSideOfEqualsWithRelative + " = " + rightSideOfEquals + ";"); } else { curBlock.Line(leftSideOfEquals + " = " + rightSideOfEquals + ";"); } if (referencedNos != null) { NamedObjectSaveCodeGenerator.AddEndIfIfNecessary(curBlock, referencedNos); } } } } return(codeBlock); }