internal static ICodeBlock GenerateFields(IElement saveObject) { ICodeBlock codeBlock = new CodeDocument(2); foreach (ElementComponentCodeGenerator codeGenerator in CodeWriter.CodeGenerators) { if (codeGenerator == null) { throw new Exception("The CodeWriter contains a null code generator. A plugin must have added this"); } try { codeGenerator.GenerateFields(codeBlock, saveObject); } catch (Exception e) { throw new Exception("Error generating fields in generator " + codeGenerator.GetType().Name + "\n\n" + e.ToString()); } } PerformancePluginCodeGenerator.GenerateFields(saveObject, codeBlock); EntitySave asEntitySave = saveObject as EntitySave; // No need to create LayerProvidedByContainer if this inherits from another object. if (asEntitySave != null && !saveObject.InheritsFromEntity()) { // Add the layer that is going to get assigned in generated code codeBlock.Line("protected FlatRedBall.Graphics.Layer LayerProvidedByContainer = null;"); } return codeBlock; }
internal static ICodeBlock GenerateDestroy(IElement saveObject) { bool isScreen = saveObject is ScreenSave; ICodeBlock codeBlock = new CodeDocument(3); var currentBlock = codeBlock; #region Call base.Destroy if it has a derived object // The Screen template already includes a call to base.Destroy if ( saveObject.InheritsFromEntity() ) { currentBlock.Line("base.Destroy();"); } #endregion #region If Entity, remove from managers (SpriteManager, GuiManager) if (saveObject is EntitySave) { if (saveObject.InheritsFromFrbType()) { AssetTypeInfo ati = AvailableAssetTypes.Self.GetAssetTypeFromRuntimeType(saveObject.BaseObject); if (ati != null) { currentBlock.Line(ati.DestroyMethod + ";"); } } else if (! saveObject.InheritsFromElement()) { currentBlock.Line("FlatRedBall.SpriteManager.RemovePositionedObject(this);"); } if ((saveObject as EntitySave).ImplementsIWindow && !(saveObject as EntitySave).GetInheritsFromIWindow()) { currentBlock.Line("FlatRedBall.Gui.GuiManager.RemoveWindow(this);"); } } #endregion foreach (ElementComponentCodeGenerator codeGenerator in CodeWriter.CodeGenerators) { codeGenerator.GenerateDestroy(currentBlock, saveObject); } return currentBlock; }
private static void GenerateRemoveFromManagers(ICodeBlock currentBlock, IElement saveObject) { if (saveObject.InheritsFromElement()) { currentBlock = currentBlock.Function("public override void", "RemoveFromManagers", ""); currentBlock.Line("base.RemoveFromManagers();"); } else { currentBlock = currentBlock.Function("public virtual void", "RemoveFromManagers", ""); } #region Call base.Destroy if it has a derived object // The Screen template already includes a call to base.Destroy if (saveObject.InheritsFromEntity()) { currentBlock.Line("base.RemoveFromManagers();"); } #endregion if (saveObject is EntitySave) { if (saveObject.InheritsFromFrbType()) { AssetTypeInfo ati = AvailableAssetTypes.Self.GetAssetTypeFromRuntimeType(saveObject.BaseObject); if (ati != null) { EntitySave asEntitySave = saveObject as EntitySave; if (asEntitySave.CreatedByOtherEntities && !string.IsNullOrEmpty(ati.RecycledDestroyMethod)) { currentBlock.Line(ati.RecycledDestroyMethod + ";"); } else { currentBlock.Line(ati.DestroyMethod + ";"); } } } else if (!saveObject.InheritsFromElement()) { currentBlock.Line("FlatRedBall.SpriteManager.ConvertToManuallyUpdated(this);"); } if ((saveObject as EntitySave).ImplementsIWindow && !(saveObject as EntitySave).GetInheritsFromIWindow()) { currentBlock.Line("FlatRedBall.Gui.GuiManager.RemoveWindow(this);"); } } foreach (ElementComponentCodeGenerator codeGenerator in CodeWriter.CodeGenerators) { codeGenerator.GenerateRemoveFromManagers(currentBlock, saveObject); } }
internal static ICodeBlock GenerateGeneralActivity(ICodeBlock codeBlock, IElement saveObject) { bool isEntity = saveObject is EntitySave; EntitySave entitySave = saveObject as EntitySave; // This code might seem a little weird. The reason we do this // is because when an Entity is paused, it has a method that is // called. However, when it is unpaused, there's just an instruction // that is executed - there is no event. But if a Screen is paused, then // objects within that Screen don't get unpaused....so we're going to bet on // the Activity function only being called in unpaused Screens. If this causes // probelsm we may have to make something a little more standard like an Unpause // method. if (isEntity && (entitySave.ImplementsIClickable || entitySave.ImplementsIWindow) && !entitySave.GetInheritsFromIWindowOrIClickable() ) { codeBlock.Line("mIsPaused = false;"); } #region Call base.Activity if it has a derived object // We only need to do this for EntitySaves. Screens inherit from the // Screen class so they ALWAYS call base.Activity. It's in the generated // Screen template. if ( saveObject.InheritsFromEntity()) { codeBlock.Line("base.Activity();"); } #endregion codeBlock._(); // Eventually do we want to move this in the generate activity for custom variable code gen. CustomVariableCodeGenerator.WriteVelocityForCustomVariables(saveObject.CustomVariables, codeBlock); foreach (ElementComponentCodeGenerator codeGenerator in CodeWriter.CodeGenerators) { codeGenerator.GenerateActivity(codeBlock, saveObject); } return codeBlock; }