private void GenerateEntitySaveUpdateDependencies(ICodeBlock codeBlock, IElement element) { EntityManagementValues managementValues = Values?.EntityManagementValueList?.FirstOrDefault(item => item.Name == element.Name); if (managementValues != null) { var innerCodeBlock = new CodeBlockBase(null); GenerateEntityUpdateDependencies(innerCodeBlock, element, managementValues); GenerateNamedObjectUpdateDependencies(innerCodeBlock, element, managementValues); var doAnyNamedObjectsHaveUpdateDependencies = innerCodeBlock.BodyCodeLines.Any(); if (doAnyNamedObjectsHaveUpdateDependencies) { var ifBlock = codeBlock.If("this.LastDependencyUpdate != currentTime"); // todo - need to call base here if we actually want to update dependencies var shouldCallBase = managementValues.PropertyManagementMode == Enums.PropertyManagementMode.FullyManaged || managementValues.SelectedProperties.Contains("Attachment"); if (shouldCallBase) { ifBlock.Line("base.UpdateDependencies(currentTime);"); } ifBlock.Line("this.mLastDependencyUpdate = currentTime;"); ifBlock.InsertBlock(innerCodeBlock); } } }
public string GenerateCodeFor(ElementSave elementSave) { if (elementSave == null) { throw new ArgumentNullException(nameof(elementSave)); } CodeBlockBase topBlock = new CodeBlockBase(null); // It's much easier to add the LINQ usings here instead of using the qualified method names in code gen below topBlock.Line("using System.Linq;"); var fullNamespace = GetFullRuntimeNamespaceFor(elementSave); ICodeBlock currentBlock = topBlock.Namespace(fullNamespace); bool generated = false; if (elementSave is StandardElementSave) { generated = StandardsCodeGenerator.Self.GenerateStandardElementSaveCodeFor(elementSave as StandardElementSave, currentBlock); } else { bool doesElementSaveHaveDefaultState = elementSave.DefaultState != null; // If the element has no default state that may mean that the element is referenced // by the Gum project, but that the file (such as the .gusx or gucx) is missing from // the file system. Therefore we should not try to generate code for it. if (doesElementSaveHaveDefaultState) { GenerateScreenAndComponentCodeFor(elementSave, currentBlock); // for now always true generated = true; } else { generated = false; } } if (generated) { return(topBlock.ToString()); } else { return(null); } }
private static void ImplementIEntityFactory(string factoryClassName, CodeBlockBase codeBlock) { codeBlock.Function("object", "IEntityFactory.CreateNew", "") .Line($"return {factoryClassName}.CreateNew();"); codeBlock.Function("object", "IEntityFactory.CreateNew", "Layer layer") .Line($"return {factoryClassName}.CreateNew(layer);"); codeBlock.Function("void", "IEntityFactory.Initialize", "string contentManagerName") .Line($"{factoryClassName}.Initialize(contentManagerName);"); codeBlock.Function("void", "IEntityFactory.ClearListsToAddTo", "") .Line($"{factoryClassName}.ClearListsToAddTo();"); }
internal string GenerateInterfaceCodeFor(BehaviorSave behavior) { CodeBlockBase fileLevel = new CodeBlockBase(null); ICodeBlock namespaceLevel = fileLevel.Namespace(GueDerivingClassCodeGenerator.GueRuntimeNamespace); StateCodeGenerator.Self.GenerateStateEnums(behavior, namespaceLevel, enumNamePrefix: behavior.Name); ICodeBlock interfaceLevel = namespaceLevel.Interface("public", $"I{behavior.Name}", ""); GenerateInterface(interfaceLevel, behavior); return(fileLevel.ToString()); }
private void GenerateEntityUpdateDependencies(CodeBlockBase codeBlock, IElement element, EntityManagementValues managementValues) { var ati = GetAssetTypeInfoFor(element); string name = "this"; if (ati?.RuntimeTypeName == "Text") { GenerateTextVariableUpdateDependenciesFor(codeBlock, name, managementValues.SelectedProperties); } else if (ati?.RuntimeTypeName == "Sprite") { GenerateSpriteVariableUpdateDependenciesFor(codeBlock, name, managementValues.SelectedProperties); } }
public string GetRuntimeRegistrationPartialClassContents() { CodeBlockBase codeBlock = new CodeBlockBase(null); ICodeBlock currentBlock = codeBlock.Namespace("FlatRedBall.Gum"); currentBlock = currentBlock.Class("public partial", "GumIdb", ""); currentBlock = currentBlock.Function("public static void", "RegisterTypes", ""); { AddAssignmentFunctionContents(currentBlock); } return(codeBlock.ToString()); }
public string GetCustomCodeTemplateCode(ElementSave element) { // Example: /* * using System; * using System.Collections.Generic; * using System.Linq; * * namespace DesktopGlForms.GumRuntimes.DefaultForms * { * public partial class ButtonRuntime * { * partial void CustomInitialize() * { * * } * } * } * */ ICodeBlock codeBlock = new CodeBlockBase(null); var toReturn = codeBlock; codeBlock.Line("using System;"); codeBlock.Line("using System.Collections.Generic;"); codeBlock.Line("using System.Linq;"); codeBlock.Line(); codeBlock = codeBlock.Namespace(GetFullRuntimeNamespaceFor(element)); { string runtimeClassName = GetUnqualifiedRuntimeTypeFor(element); codeBlock = codeBlock.Class("public partial", runtimeClassName); { codeBlock = codeBlock.Function("partial void", "CustomInitialize"); } } return(toReturn.ToString()); }
private static string GetCameraSetupCsContents() { bool shouldGenerateNew = GlueState.Self.CurrentGlueProject.DisplaySettings != null; string fileContents = null; ICodeBlock classContents = new CodeBlockBase(null); classContents.Line("// This is a generated file created by Glue. To change this file, edit the camera settings in Glue."); classContents.Line("// To access the camera settings, push the camera icon."); if (shouldGenerateNew == false) { classContents.TabCount = 2; fileContents = GetDisplaySetupOld(classContents); } else { fileContents = GetDisplaySetupNew(classContents); } return(fileContents); }
public string GetRuntimeRegistrationPartialClassContents(bool registerFormsAssociations) { CodeBlockBase codeBlock = new CodeBlockBase(null); ICodeBlock currentBlock = codeBlock.Namespace("FlatRedBall.Gum"); currentBlock = currentBlock.Class("public ", "GumIdbExtensions", ""); currentBlock = currentBlock.Function("public static void", "RegisterTypes", ""); { AddAssignmentFunctionContents(currentBlock); if (registerFormsAssociations) { currentBlock._(); AddFormsAssociations(currentBlock); } } return(codeBlock.ToString()); }
public string GenerateCodeFor(ElementSave elementSave) { if (elementSave == null) { throw new ArgumentNullException(nameof(elementSave)); } CodeBlockBase topBlock = new CodeBlockBase(null); // It's much easier to add the LINQ usings here instead of using the qualified method names in code gen below topBlock.Line("using System.Linq;"); ICodeBlock currentBlock = topBlock.Namespace(GueRuntimeNamespace); bool generated = false; if (elementSave is StandardElementSave) { generated = StandardsCodeGenerator.Self.GenerateStandardElementSaveCodeFor(elementSave as StandardElementSave, currentBlock); } else { GenerateScreenAndComponentCodeFor(elementSave, currentBlock); // for now always true generated = true; } if (generated) { return(topBlock.ToString()); } else { return(null); } }
private AssetTypeInfo CreateAtiForCollisionRelationship() { AssetTypeInfo toReturn = new AssetTypeInfo(); toReturn.FriendlyName = "Collision Relationship"; // prevents the NamedObjectSaveCodeGenerator from doing anything: toReturn.ConstructorFunc = (a, b, c) => ""; toReturn.QualifiedRuntimeTypeName = new PlatformSpecificType(); // as a fallback to systems that haven't yet converted over to the func toReturn.QualifiedRuntimeTypeName.QualifiedType = "FlatRedBall.Math.Collision.CollisionRelationship"; toReturn.ConstructorFunc = (element, namedObjectSave, referencedFileSave) => { if (namedObjectSave != null) { string objectName = namedObjectSave.FieldName; var codeBlock = new CodeBlockBase(); CollisionCodeGenerator.GenerateInitializeCodeFor(namedObjectSave, codeBlock); return(codeBlock.ToString()); } else { return(null); } }; toReturn.QualifiedRuntimeTypeName.PlatformFunc = (nosAsObject) => { var nos = nosAsObject as NamedObjectSave; if (nos == null) { return($"FlatRedBall.Math.Collision.CollisionRelationship"); } else { bool isFirstList; bool isSecondList; var firstType = GetFirstGenericType(nos, out isFirstList); var secondType = GetSecondGenericType(nos, out isSecondList); var isFirstTileShapeCollection = firstType == "FlatRedBall.TileCollisions.TileShapeCollection"; var isSecondTileShapeCollection = secondType == "FlatRedBall.TileCollisions.TileShapeCollection"; var isFirstShapeCollection = firstType == "FlatRedBall.Math.Geometry.ShapeCollection"; var isSecondShapeCollection = secondType == "FlatRedBall.Math.Geometry.ShapeCollection"; // todo - single vs. shape collection // todo - list vs. shape collection string relationshipType; if (isFirstList == false && isSecondList == false) { if (isSecondTileShapeCollection) { relationshipType = "FlatRedBall.Math.Collision.CollidableVsTileShapeCollectionRelationship"; } else if (isSecondShapeCollection) { relationshipType = "FlatRedBall.Math.Collision.PositionedObjectVsShapeCollection"; } else { relationshipType = "FlatRedBall.Math.Collision.CollisionRelationship"; } } else if (isFirstList && isSecondList) { relationshipType = "FlatRedBall.Math.Collision.ListVsListRelationship"; } else if (isFirstList) { if (isSecondTileShapeCollection) { relationshipType = "FlatRedBall.Math.Collision.CollidableListVsTileShapeCollectionRelationship"; } else if (isSecondShapeCollection) { relationshipType = "FlatRedBall.Math.Collision.ListVsShapeCollectionRelationship"; } else { relationshipType = "FlatRedBall.Math.Collision.ListVsPositionedObjectRelationship"; } } else if (isSecondList) { relationshipType = "FlatRedBall.Math.Collision.PositionedObjectVsListRelationship"; } else { // not handled: relationshipType = "FlatRedBall.Math.Collision.CollisionRelationship"; } if (isSecondTileShapeCollection || isSecondShapeCollection) { // doesn't require 2nd type param: return ($"{relationshipType}<{firstType}>"); } else { return ($"{relationshipType}<{firstType}, {secondType}>"); } } }; toReturn.QualifiedSaveTypeName = null; toReturn.Extension = null; toReturn.AddToManagersMethod = null; toReturn.CustomLoadMethod = null; toReturn.DestroyMethod = null; toReturn.ShouldBeDisposed = false; toReturn.ShouldAttach = false; toReturn.CanBeCloned = false; toReturn.MustBeAddedToContentPipeline = false; toReturn.HasCursorIsOn = false; toReturn.HasVisibleProperty = false; toReturn.CanIgnorePausing = false; toReturn.CanBeObject = true; toReturn.HideFromNewFileWindow = true; return(toReturn); }
public override string GetCode() { string entityClassName = FileManager.RemovePath(FileManager.RemoveExtension(EntitySave.Name)); string baseEntityName = null; if (!string.IsNullOrEmpty(EntitySave.BaseEntity)) { EntitySave rootEntitySave = EntitySave.GetRootBaseEntitySave(); // There could be an invalid inheritance chain. We don't want Glue to bomb if so, so // we'll check for this. if (rootEntitySave != null && rootEntitySave != EntitySave) { baseEntityName = rootEntitySave.Name; } } string factoryClassName = ClassName; ClassProperties classProperties = new ClassProperties(); classProperties.NamespaceName = ProjectManager.ProjectNamespace + ".Factories"; classProperties.ClassName = factoryClassName + " : IEntityFactory"; classProperties.Members = new List <FlatRedBall.Instructions.Reflection.TypedMemberBase>(); classProperties.UntypedMembers = new Dictionary <string, string>(); string positionedObjectListType = string.Format("FlatRedBall.Math.PositionedObjectList<{0}>", entityClassName); // Factories used to be always static but we're going to make them singletons instead //classProperties.IsStatic = true; classProperties.UsingStatements = new List <string>(); classProperties.UsingStatements.Add(GlueCommands.Self.GenerateCodeCommands.GetNamespaceForElement(EntitySave)); classProperties.UsingStatements.Add("System"); if (!string.IsNullOrEmpty(baseEntityName)) { EntitySave baseEntity = ObjectFinder.Self.GetEntitySave(baseEntityName); classProperties.UsingStatements.Add(GlueCommands.Self.GenerateCodeCommands.GetNamespaceForElement(baseEntity)); } classProperties.UsingStatements.Add("FlatRedBall.Math"); classProperties.UsingStatements.Add("FlatRedBall.Graphics"); classProperties.UsingStatements.Add(ProjectManager.ProjectNamespace + ".Performance"); ICodeBlock codeContent = CodeWriter.CreateClass(classProperties); const int numberOfInstancesToPool = 20; var methodTag = codeContent.GetTag("Methods")[0]; var methodBlock = GetAllFactoryMethods(factoryClassName, baseEntityName, numberOfInstancesToPool, ShouldPoolObjects); methodTag.InsertBlock(methodBlock); var codeBlock = new CodeBlockBase(null); codeBlock.Line("static string mContentManagerName;"); codeBlock.Line("static System.Collections.Generic.List<System.Collections.IList> ListsToAddTo = new System.Collections.Generic.List<System.Collections.IList>();"); codeBlock.Line(string.Format("static PoolList<{0}> mPool = new PoolList<{0}>();", entityClassName)); codeBlock.Line(string.Format("public static Action<{0}> EntitySpawned;", entityClassName)); ImplementIEntityFactory(factoryClassName, codeBlock); #region Self and mSelf codeBlock.Line("static " + factoryClassName + " mSelf;"); var selfProperty = codeBlock.Property("public static " + factoryClassName, "Self"); var selfGet = selfProperty.Get(); selfGet.If("mSelf == null") .Line("mSelf = new " + entityClassName + "Factory" + "();"); selfGet.Line("return mSelf;"); #endregion ((codeContent.BodyCodeLines.Last() as CodeBlockBase).BodyCodeLines.Last() as CodeBlockBase).InsertBlock(codeBlock); return(codeContent.ToString()); }