Skip to content

Commit

Permalink
Improvements in Variant codegen.
Browse files Browse the repository at this point in the history
Removed XML Docs region
Fixed IWindow entity codegen to include new events.
Fixed Gum codegen bug which was not casting objects properly.
Fixed bug when referencing Texture2D variable.
Fixed possible crash loading project with files in folders.
Upgraded Test project - almost compiles!
  • Loading branch information
vchelaru committed Feb 21, 2024
1 parent c6f5d0d commit bfb90b2
Show file tree
Hide file tree
Showing 72 changed files with 2,228 additions and 1,309 deletions.
2 changes: 0 additions & 2 deletions Engines/FlatRedBallXNA/FlatRedBall/Math/Geometry/Segment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,6 @@ public bool Intersects(Segment s2, out Point intersectionPoint)
return !double.IsNaN(intersectionPoint.X);
}

#region XML Docs
/// <summary>
/// Returns the point where this segment intersects the argument segment.
/// </summary>
Expand All @@ -688,7 +687,6 @@ public bool Intersects(Segment s2, out Point intersectionPoint)
/// argument segment. If the two segments do not touch, the point
/// will have both values be double.NAN.
/// </returns>
#endregion
public void IntersectionPoint(ref Segment s2, out Point intersectionPoint)
{
// code borrowed from
Expand Down
61 changes: 13 additions & 48 deletions FRBDK/Glue/Glue/CodeGeneration/CustomVariableCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Security.Cryptography.X509Certificates;
using Microsoft.Xna.Framework;
using Newtonsoft.Json.Linq;
using FlatRedBall.Glue.Plugins.ExportedImplementations;

namespace FlatRedBall.Glue.CodeGeneration
{
Expand Down Expand Up @@ -169,54 +170,8 @@ private static void CreateNewVariableMember(ICodeBlock codeBlock, CustomVariable

if (customVariable.DefaultValue != null)
{
// July 24, 2023
// Why not use GetRightSide?
// That is
if (!IsTypeFromCsv(customVariable, element))
{
//variableAssignment =
// CodeParser.ConvertValueToCodeString(customVariable.DefaultValue);

//// If this is a file, we don't want to assign it here
//if (customVariable.GetIsFile())
//{
// variableAssignment = null;
//}

//if (customVariable.Type == "Color")
//{
// variableAssignment = "Color." + variableAssignment.Replace("\"", "");

//}
//else if (customVariable.Type != "string" && variableAssignment == "\"\"")
//{
// variableAssignment = null;
//}
//else
//{
// if (customVariable.DefaultValue != null)
// {
// (bool isState, StateSaveCategory category) =
// customVariable.GetIsVariableStateAndCategory(element as GlueElement);
// if (isState)
// {
// var type = customVariable.Type;
// if (category != null)
// {
// var categoryElement = ObjectFinder.Self.GetElementContaining(category);

// if (categoryElement != null)
// {
// type = $"{categoryElement.Name.Replace("\\", ".")}.{category.Name}";

// }

// }
// variableAssignment = type + "." + customVariable.DefaultValue;
// }

// }
//}
var variableAssignmentValue = GetRightSideOfEquals(customVariable, element);

if (variableAssignmentValue != null)
Expand Down Expand Up @@ -722,7 +677,7 @@ public static string GetRightSideOfEquals(CustomVariable customVariable, GlueEle
{
rightSide = rightSide.Replace("\"", "").Replace("-", "_");

if (rightSide == "<NONE>")
if (rightSide == "<NONE>" || string.IsNullOrEmpty(rightSide))
{
rightSide = "null";
}
Expand Down Expand Up @@ -1644,8 +1599,10 @@ public static void WriteVelocityForCustomVariables(List<CustomVariable> customVa

internal static bool IsTypeFromCsv(CustomVariable customVariable, GlueElement glueElement = null)
{
if(customVariable != null && customVariable.Type != null &&
if(customVariable != null &&
customVariable.Type != null &&
customVariable.GetIsVariableState(glueElement) == false &&
!IsQualifiedGumState(customVariable.Type) &&
customVariable.GetIsBaseElementType() == false &&
customVariable.Type.Contains(".") &&
customVariable.GetRuntimeType() == null)
Expand All @@ -1670,5 +1627,13 @@ internal static bool IsTypeFromCsv(CustomVariable customVariable, GlueElement gl
}
return false;
}

private static bool IsQualifiedGumState(string type)
{
// For the sake of speed, we're going to just look at the prefix and guess based on that. A full
// implementation would do a browsing of Gum files and look for states but....that's not easy because
// all of that code is in a plugin, and we can approximate it here:
return type.StartsWith($"{GlueState.Self.ProjectNamespace}.GumRuntimes");
}
}
}
10 changes: 9 additions & 1 deletion FRBDK/Glue/Glue/Elements/ObjectFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,15 @@ public StateSaveCategory GetStateSaveCategory(StateSave stateSave)
}
else
{
category = containingElement.GetStateCategoryRecursively(customVariable.Type);
var typeToSearchFor = customVariable.Type;
// this could be nullable since that's the type used in Variant codegen

if(typeToSearchFor?.EndsWith("?") == true)
{
typeToSearchFor = typeToSearchFor.Substring(0, typeToSearchFor.Length - 1);
}

category = containingElement.GetStateCategoryRecursively(typeToSearchFor);
isState = category != null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using FlatRedBall.Glue.CodeGeneration;
using FlatRedBall.Glue.CodeGeneration.CodeBuilder;
using FlatRedBall.Glue.Plugins.ExportedImplementations;
using FlatRedBall.Glue.SaveClasses;
using FlatRedBall.Gui;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -23,6 +25,20 @@ public override ICodeBlock GenerateFields(ICodeBlock codeBlock, FlatRedBall.Glue
// codeBlock.Line("FlatRedBall.Gum.GueIWindowWrapper " + GetWrapperNameFor(namedObject) + ";");
//}

// Feb 21, 2024
// Some codegen is handled by IWindowTemplate.txt
// That's a yucky system because it doesn't (easily)
// allow for conditional generation, and it breaks from
// how normal codegen is done. So I'll add these props here.
// IWindow is out of favor anyway due to Gum so...this may not
// be touched much in the future:
var isNewEnough = GlueState.Self.IsReferencingFrbSource;
if(element is EntitySave entitySave && entitySave.ImplementsIWindow && isNewEnough)
{
codeBlock.Line("public void CallRemovedAsPushedWindow() => RemovedAsPushedWindow?.Invoke(this);");
codeBlock.Line("public event WindowEvent RemovedAsPushedWindow;");
}

return codeBlock;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ string GetLoadStaticContentCodeFor(ReferencedFileSave rfs, NamedObjectSave nos,
"\n{" +
"\n var wasSuspended = Gum.Wireframe.GraphicalUiElement.IsAllLayoutSuspended;" +
"\n Gum.Wireframe.GraphicalUiElement.IsAllLayoutSuspended = true;";

if(string.IsNullOrEmpty(qualifiedName))
{
qualifiedName = rfs?.RuntimeType;
}

if(string.IsNullOrEmpty(qualifiedName))
{
toReturn +=
Expand Down Expand Up @@ -684,7 +690,7 @@ public void UpdateAtiForElement(AssetTypeInfo newAti, ElementSave element)
{
var uncastedLine = ComponentAti.CustomLoadFunc(innerElement, nos, rfs, contentManagerName);
// remove the semicolon
return uncastedLine.Substring(0, uncastedLine.Length - 1) + " as " + GueDerivingClassCodeGenerator.Self.GetQualifiedRuntimeTypeFor(element) + ";";
return uncastedLine;

};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,14 @@ private void CreateVariableFields(IElement element, ICodeBlock classBlock)
// as a simple field
tempVariable.Name = variable.Name;

tempVariable.Type = ConvertFileToType(variable.Type);
if(IsTypeFileType(variable.Type))
{
tempVariable.Type = "string";
}
else
{
tempVariable.Type = CustomVariableCodeGenerator.GetMemberTypeFor(variable, element);
}
tempVariable.DefaultValue = variable.DefaultValue;
tempVariable.OverridingPropertyType = variable.OverridingPropertyType;

Expand All @@ -212,11 +219,6 @@ private static bool IsTypeFileType(string type)
"AnimationChainList";
}

private string ConvertFileToType(string type)
{
return IsTypeFileType(type) ? "string" : type;
}

static bool ShouldSkipVariantField(CustomVariable customVariable)
{
if(customVariable.SetByDerived == false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ private void FillWithBadTypeReferences(GlueElement element, CustomVariable varia
// this check was added to catch missing CSV references
if (string.IsNullOrEmpty(baseDefiningVariable?.SourceObject))
{
// it better be a CSV or state
var found = variable.GetIsCsv() || variable.GetIsVariableState() || variable.GetIsBaseElementType();
// it better be a CSV or state or Texture
var found =
variable.Type == "Microsoft.Xna.Framework.Graphics.Texture2D" ||
variable.GetIsCsv() ||
variable.GetIsVariableState() ||
variable.GetIsBaseElementType();

if (!found)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ public override void RefreshTreeNodes(TreeNodeRefreshType treeNodeRefreshType)
if (i != index)
{
nodeList.RemoveAt(index);
nodeList.Insert(i, nodeForFile);
if(nodeList.Count == 0)
{
nodeList.Add(nodeForFile);
}
else
{
nodeList.Insert(i, nodeForFile);
}
}

string newText = FileManager.RemovePath(file.Name);
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
</State>
<Category>
<Name>Position</Name>
<IsSetRecursively>false</IsSetRecursively>
<State>
<Name>State1</Name>
<Variable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
</State>
<Category>
<Name>ColorCategory</Name>
<IsSetRecursively>false</IsSetRecursively>
<State>
<Name>RedState</Name>
<Variable>
Expand All @@ -187,6 +188,7 @@
</Category>
<Category>
<Name>SizeCategory</Name>
<IsSetRecursively>false</IsSetRecursively>
<State>
<Name>BigState</Name>
</State>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
</State>
<Category>
<Name>TestCategory</Name>
<IsSetRecursively>false</IsSetRecursively>
<State>
<Name>CategorizedState</Name>
</State>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
</State>
<Category>
<Name>Category1</Name>
<IsSetRecursively>false</IsSetRecursively>
<State>
<Name>Categorized1</Name>
<Variable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
</State>
<Category>
<Name>Category1</Name>
<IsSetRecursively>false</IsSetRecursively>
<State>
<Name>Categorized1</Name>
</State>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
</State>
<Category>
<Name>ButtonCategory</Name>
<IsSetRecursively>false</IsSetRecursively>
<State>
<Name>Enabled</Name>
<Variable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@
</State>
<Category>
<Name>CheckBoxCategory</Name>
<IsSetRecursively>false</IsSetRecursively>
<State>
<Name>EnabledOn</Name>
<Variable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@
</State>
<Category>
<Name>ColorCategory</Name>
<IsSetRecursively>false</IsSetRecursively>
<State>
<Name>Gray</Name>
<Variable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@
</State>
<Category>
<Name>ComboBoxCategory</Name>
<IsSetRecursively>false</IsSetRecursively>
<State>
<Name>Enabled</Name>
<Variable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
</State>
<Category>
<Name>ListBoxItemCategory</Name>
<IsSetRecursively>false</IsSetRecursively>
<State>
<Name>Enabled</Name>
<Variable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
</State>
<Category>
<Name>RadioButtonCategory</Name>
<IsSetRecursively>false</IsSetRecursively>
<State>
<Name>EnabledOn</Name>
<Variable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@
</State>
<Category>
<Name>ScrollBarCategory</Name>
<IsSetRecursively>false</IsSetRecursively>
</Category>
<Instance>
<Name>BackgroundInstance</Name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
</State>
<Category>
<Name>ButtonCategory</Name>
<IsSetRecursively>false</IsSetRecursively>
<State>
<Name>Enabled</Name>
</State>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
</State>
<Category>
<Name>SliderCategory</Name>
<IsSetRecursively>false</IsSetRecursively>
</Category>
<Instance>
<Name>LineInstance</Name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
</State>
<Category>
<Name>TextBoxCategory</Name>
<IsSetRecursively>false</IsSetRecursively>
<State>
<Name>Enabled</Name>
</State>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@
</State>
<Category>
<Name>ToggleCategory</Name>
<IsSetRecursively>false</IsSetRecursively>
<State>
<Name>EnabledOn</Name>
<Variable>
Expand Down
Loading

0 comments on commit bfb90b2

Please sign in to comment.