public override ICodeBlock GenerateLoadStaticContent(ICodeBlock codeBlock, SaveClasses.IElement element) { #region For debugging record if we're using a global content manager codeBlock.Line("#if DEBUG"); codeBlock.If("contentManagerName == FlatRedBall.FlatRedBallServices.GlobalContentManager") .Line("HasBeenLoadedWithGlobalContentManager = true;"); codeBlock.ElseIf("HasBeenLoadedWithGlobalContentManager") .Line("throw new System.Exception(\"This type has been loaded with a Global content manager, then loaded with a non-global. This can lead to a lot of bugs\");"); codeBlock.Line("#endif"); #endregion return codeBlock; }
public override ICodeBlock GenerateLoadStaticContent(ICodeBlock codeBlock, SaveClasses.IElement element) { #region For debugging record if we're using a global content manager codeBlock.Line("#if DEBUG"); codeBlock.If("contentManagerName == FlatRedBall.FlatRedBallServices.GlobalContentManager") .Line("HasBeenLoadedWithGlobalContentManager = true;"); codeBlock.ElseIf("HasBeenLoadedWithGlobalContentManager") .Line("throw new System.Exception(\"This type has been loaded with a Global content manager, then loaded with a non-global. This can lead to a lot of bugs\");"); codeBlock.Line("#endif"); #endregion return(codeBlock); }
private static ICodeBlock GeneratePreloadStateContentForStateType(ICodeBlock codeBlock, IElement element, List <StateSave> list, string variableType) { if (list.Count != 0) { codeBlock = codeBlock.Function("public static void", "PreloadStateContent", variableType + " state, string contentManagerName"); codeBlock.Line("ContentManagerName = contentManagerName;"); //codeBlock = codeBlock.Switch("state"); bool isElse = false; // Loop through states here and access properties that need the values foreach (StateSave state in list) { if (isElse) { codeBlock = codeBlock.ElseIf($"state == {variableType}.{state.Name}"); } else { codeBlock = codeBlock.If($"state == {variableType}.{state.Name}"); } isElse = true; foreach (InstructionSave instruction in state.InstructionSaves) { if (instruction.Value != null && instruction.Value is string) { // We insert a block so that object throwaway is not redefined in the switch scope. // We do this instead of making an object throwaway above the switch so that we don't // get warnings if is nothing to load codeBlock.Block().Line("object throwaway = " + GetRightSideAssignmentValueAsString(element, instruction) + ";"); } } codeBlock = codeBlock.End(); } codeBlock = codeBlock.End(); } return(codeBlock); }
public void CodeGenerationStart(IElement element) { var stateChainCollection = GlueCommands.TreeNodeCommands.GetProperty <StateChainCollection>(element, PropertyName); if (stateChainCollection == null) { return; } var elementNameWithoutPath = FileManager.RemovePath(element.Name); var document = new CodeDocument(); ICodeBlock codeBlock = document; if (stateChainCollection.StateChains.Count <= 0) { return; } codeBlock = codeBlock .Line("using FlatRedBall.Instructions;") .Namespace(GlueCommands.GenerateCodeCommands.GetNamespaceForElement(element)) .Class("public partial ", element.ClassName, ""); //Create Enum codeBlock = codeBlock .Enum("public", "StateChains") .Line("None = 0,"); for (int i = 0; i < stateChainCollection.StateChains.Count; i++) { if (i == stateChainCollection.StateChains.Count - 1) { codeBlock.Line(stateChainCollection.StateChains[i].Name); } else { codeBlock.Line(stateChainCollection.StateChains[i].Name + ","); } } codeBlock = codeBlock.End(); //Private members codeBlock ._() .Line("private StateChains _currentStateChain = StateChains.None;") .Line("private int _index;") .Line("private Instruction _instruction;") ._(); //CurrentStateChain Property codeBlock = codeBlock .Property("public StateChains", "CurrentStateChain") .Get() .Line("return _currentStateChain;") .End() .Set() .Line("StopStateChain();") ._() .Line("_currentStateChain = value;") .Line("_index = 0;") ._() .Switch("_currentStateChain"); foreach (var stateChain in stateChainCollection.StateChains) { codeBlock .Case("StateChains." + stateChain.Name) .Line("StartNextState" + stateChain.Name + "();"); } codeBlock = codeBlock .End() .End() .End(); codeBlock._(); //ManageStateChains codeBlock = codeBlock .Function("public void", "ManageStateChains", "") .If("CurrentStateChain == StateChains.None") .Line("return;") .End() ._() .Switch("CurrentStateChain"); foreach (var stateChain in stateChainCollection.StateChains) { var index = 0; codeBlock = codeBlock .Case("StateChains." + stateChain.Name); foreach (var stateChainState in stateChain.StateChainStates.Where(stateChainState => !string.IsNullOrEmpty(stateChainState.State))) { if (index == 0) { codeBlock .If("_index == 0 && CurrentState == VariableState." + stateChainState.State) .Line("_index++;") .Line("StartNextState" + stateChain.Name + "();"); } else { codeBlock .ElseIf("_index == " + index + " && CurrentState == VariableState." + stateChainState.State) .Line("_index++;") .Line("StartNextState" + stateChain.Name + "();"); } index++; } codeBlock = codeBlock .End(); } codeBlock = codeBlock .End() .End(); codeBlock._(); //StopStateChain codeBlock = codeBlock .Function("public void", "StopStateChain", "") .If("CurrentStateChain == StateChains.None") .Line("return;") .End() ._() .Switch("CurrentStateChain"); foreach (var stateChain in stateChainCollection.StateChains) { var index = 0; codeBlock = codeBlock .Case("StateChains." + stateChain.Name); foreach (var stateChainState in stateChain.StateChainStates) { if (index == 0) { codeBlock .If("_index == 0") .Line("Instructions.Remove(_instruction);") .Line("StopStateInterpolation(VariableState." + stateChainState.State + ");") .End(); } else { codeBlock .ElseIf("_index == " + index) .Line("Instructions.Remove(_instruction);") .Line("StopStateInterpolation(VariableState." + stateChainState.State + ");") .End(); } index++; } codeBlock = codeBlock .End(); } codeBlock = codeBlock .End() .Line("_instruction = null;") .End(); codeBlock._(); //StartNextState***** foreach (var stateChain in stateChainCollection.StateChains) { codeBlock = codeBlock .Function("private void", "StartNextState" + stateChain.Name, "") .If("_index < 0") .Line("_index = 0;") .End() ._() .If("_index >= " + stateChain.StateChainStates.Count) .Line("_index = 0;") .End() ._() .Switch("_index"); var index = 0; foreach (var stateChainState in stateChain.StateChainStates) { codeBlock .Case(index.ToString()) .Line("_instruction = InterpolateToState(VariableState." + stateChainState.State + ", " + stateChainState.Time / 1000 + ");"); index++; } codeBlock = codeBlock .End() .End() ._(); } GlueCommands.ProjectCommands.CreateAndAddPartialFile(element, "StateChains", document.ToString()); }
private static ICodeBlock SetInterpolateBetweenValuesForStates(IElement element, string enumType, List <StateSave> states, ICodeBlock curBlock, Dictionary <InstructionSave, InterpolationCharacteristic> mInterpolationCharacteristics, string firstOrSecondValue, string localStateName) { bool isElse = false; foreach (StateSave state in states) { if (isElse) { curBlock = curBlock.ElseIf($"{localStateName} == {enumType}.{state.Name}"); } else { curBlock = curBlock.If($"{localStateName} == {enumType}.{state.Name}"); } isElse = true; 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 GenerateInterpolateForIndividualState(IElement element, ICodeBlock codeBlock, ICodeBlock otherBlock, StateSave stateSave, string enumType, bool isElse) { //.Switch("stateToInterpolateTo"); //otherBlock = otherBlock // .Function("public void", "StopStateInterpolation", enumType + " stateToStop"); //.Switch("stateToStop"); if (isElse) { codeBlock = codeBlock.ElseIf($"stateToInterpolateTo == {enumType}.{stateSave.Name}"); otherBlock = otherBlock.ElseIf($"stateToStop == {enumType}.{stateSave.Name}"); } else { codeBlock = codeBlock.If($"stateToInterpolateTo == {enumType}.{stateSave.Name}"); otherBlock = otherBlock.If($"stateToStop == {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); }
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); }