Skip to content

Commit

Permalink
Fixed generation of tile shape collections with default values
Browse files Browse the repository at this point in the history
Removed more localization
  • Loading branch information
vchelaru committed Nov 6, 2024
1 parent 385d1be commit f9a8968
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 252 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using FlatRedBall.Glue.CodeGeneration;
using FlatRedBall.Glue.CodeGeneration.CodeBuilder;
using FlatRedBall.Glue.GuiDisplay;
using FlatRedBall.Glue.Plugins.ICollidablePlugins;
using FlatRedBall.Glue.SaveClasses;
using FlatRedBall.Math;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TileGraphicsPlugin.ViewModels;
Expand Down Expand Up @@ -79,12 +82,7 @@ public override ICodeBlock GenerateAddToManagers(ICodeBlock codeBlock, IElement

foreach (var shapeCollection in tileShapeCollections)
{
T Get<T>(string name)
{
return shapeCollection.Properties.GetValue<T>(name);
}

var creationOptions = Get<CollisionCreationOptions>(
var creationOptions = Get<CollisionCreationOptions>(shapeCollection,
nameof(TileShapeCollectionPropertiesViewModel.CollisionCreationOptions));

var variable = shapeCollection.GetCustomVariable("Visible");
Expand All @@ -107,22 +105,17 @@ T Get<T>(string name)

public static string GenerateConstructorFor(NamedObjectSave namedObjectSave)
{
T Get<T>(string name)
{
return namedObjectSave.Properties.GetValue<T>(name);
}

var creationOptions = Get<CollisionCreationOptions>(
var creationOptions = Get<CollisionCreationOptions>(namedObjectSave,
nameof(TileShapeCollectionPropertiesViewModel.CollisionCreationOptions));

bool comesFromLayer = creationOptions == CollisionCreationOptions.FromLayer;

if(comesFromLayer)
{
var instanceName = namedObjectSave.FieldName;
var mapName = Get<string>(nameof(TileShapeCollectionPropertiesViewModel.SourceTmxName));
var layerName = Get<string>(nameof(TileShapeCollectionPropertiesViewModel.CollisionLayerName));
var typeNameInLayer = Get<string>(nameof(TileShapeCollectionPropertiesViewModel.CollisionLayerTileType));
var mapName = Get<string>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.SourceTmxName));
var layerName = Get<string>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.CollisionLayerName));
var typeNameInLayer = Get<string>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.CollisionLayerTileType));

var effectiveName = layerName;
if (!string.IsNullOrEmpty(typeNameInLayer))
Expand Down Expand Up @@ -159,11 +152,7 @@ T Get<T>(string name)
private void GenerateInitializeCodeFor(NamedObjectSave namedObjectSave, ICodeBlock codeBlock)
{

T Get<T>(string name)
{
return namedObjectSave.Properties.GetValue<T>(name);
}
var creationOptions = Get<CollisionCreationOptions>(
var creationOptions = Get<CollisionCreationOptions>(namedObjectSave,
nameof(TileShapeCollectionPropertiesViewModel.CollisionCreationOptions));

// Victor Chelaru Sept 6 2021
Expand Down Expand Up @@ -248,8 +237,8 @@ T Get<T>(string name)

void GenerateFromMapCollision(NamedObjectSave namedObjectSave, ICodeBlock codeBlock)
{
var mapName = Get<string>(nameof(TileShapeCollectionPropertiesViewModel.SourceTmxName));
var tmxCollisionName = Get<string>(nameof(TileShapeCollectionPropertiesViewModel.TmxCollisionName));
var mapName = Get<string>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.SourceTmxName));
var tmxCollisionName = Get<string>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.TmxCollisionName));

var instanceName = namedObjectSave.FieldName;
// If we do FirstOrDefault this doesn't fail, but codegen will fail later on. This gives a more informative error
Expand All @@ -276,25 +265,44 @@ void GenerateFromMapCollision(NamedObjectSave namedObjectSave, ICodeBlock codeBl
}


private void GenerateBorderOutline(NamedObjectSave namedObjectSave, ICodeBlock codeBlock)
static T Get<T>(NamedObjectSave namedObjectSave, string name)
{

T Get<T>(string name)
var property = namedObjectSave.Properties.Find(item => item.Name == name);
if(property == null)
{
// go to the view model, get the default value:
var viewModelType = typeof(TileShapeCollectionPropertiesViewModel);
var propertyInfo = viewModelType.GetProperty(name);
var attribute = propertyInfo.GetCustomAttribute<DefaultValueAttribute>();
if(attribute != null)
{
return (T)attribute.Value;
}
else
{
return default;
}
}
else
{
return namedObjectSave.Properties.GetValue<T>(name);
}
}
private void GenerateBorderOutline(NamedObjectSave namedObjectSave, ICodeBlock codeBlock)
{

string FloatString(float value) => value.ToString(CultureInfo.InvariantCulture) + "f";

var tileSize = Get<float>(nameof(TileShapeCollectionPropertiesViewModel.CollisionTileSize));
var tileSize = Get<float>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.CollisionTileSize));
var tileSizeString = FloatString(tileSize);

var leftFill = Get<float>(nameof(TileShapeCollectionPropertiesViewModel.CollisionFillLeft));
var leftFill = Get<float>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.CollisionFillLeft));
var leftFillString = FloatString(leftFill);

var topFill = Get<float>(nameof(TileShapeCollectionPropertiesViewModel.CollisionFillTop));
var topFill = Get<float>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.CollisionFillTop));
var topFillString = FloatString(topFill);

var borderOutlineType = (BorderOutlineType)Get<int>(
var borderOutlineType = (BorderOutlineType)Get<int>(namedObjectSave,
nameof(TileShapeCollectionPropertiesViewModel.BorderOutlineType));

var remainderX = leftFill % tileSize;
Expand All @@ -313,15 +321,15 @@ T Get<T>(string name)
codeBlock.Line($"{instanceName}.SortAxis = FlatRedBall.Math.Axis.X;");
//TileShapeCollectionInstance.SortAxis = FlatRedBall.Math.Axis.X;

var widthFill = Get<int>(nameof(TileShapeCollectionPropertiesViewModel.CollisionFillWidth));
var heightFill = Get<int>(nameof(TileShapeCollectionPropertiesViewModel.CollisionFillHeight));
var widthFill = Get<int>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.CollisionFillWidth));
var heightFill = Get<int>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.CollisionFillHeight));

if(borderOutlineType == BorderOutlineType.InnerSize)
{
var innerWidth = Get<float>(
var innerWidth = Get<float>(namedObjectSave,
nameof(TileShapeCollectionPropertiesViewModel.InnerSizeWidth));

var innerHeight = Get<float>(
var innerHeight = Get<float>(namedObjectSave,
nameof(TileShapeCollectionPropertiesViewModel.InnerSizeHeight));

var additionalWidth = 2 * tileSize;
Expand Down Expand Up @@ -365,22 +373,18 @@ T Get<T>(string name)

private void GenerateFillCompletely(NamedObjectSave namedObjectSave, ICodeBlock codeBlock)
{
T Get<T>(string name)
{
return namedObjectSave.Properties.GetValue<T>(name);
}

var tileSize = Get<float>(nameof(TileShapeCollectionPropertiesViewModel.CollisionTileSize));
var tileSize = Get<float>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.CollisionTileSize));
var tileSizeString = tileSize.ToString(CultureInfo.InvariantCulture) + "f";

var leftFill = Get<float>(nameof(TileShapeCollectionPropertiesViewModel.CollisionFillLeft));
var leftFill = Get<float>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.CollisionFillLeft));
var leftFillString = leftFill.ToString(CultureInfo.InvariantCulture) + "f";

var topFill = Get<float>(nameof(TileShapeCollectionPropertiesViewModel.CollisionFillTop));
var topFill = Get<float>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.CollisionFillTop));
var topFillString = topFill.ToString(CultureInfo.InvariantCulture) + "f";

var widthFill = Get<int>(nameof(TileShapeCollectionPropertiesViewModel.CollisionFillWidth));
var heightFill = Get<int>(nameof(TileShapeCollectionPropertiesViewModel.CollisionFillHeight));
var widthFill = Get<int>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.CollisionFillWidth));
var heightFill = Get<int>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.CollisionFillHeight));

var remainderX = leftFill % tileSize;
var remainderY = topFill % tileSize;
Expand Down Expand Up @@ -417,13 +421,9 @@ T Get<T>(string name)

private void GenerateFromProperties(NamedObjectSave namedObjectSave, ICodeBlock codeBlock)
{
T Get<T>(string name)
{
return namedObjectSave.Properties.GetValue<T>(name);
}

var mapName = Get<string>(nameof(TileShapeCollectionPropertiesViewModel.SourceTmxName));
var propertyName = Get<string>(nameof(TileShapeCollectionPropertiesViewModel.CollisionPropertyName));
var mapName = Get<string>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.SourceTmxName));
var propertyName = Get<string>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.CollisionPropertyName));

if(!string.IsNullOrEmpty(mapName) && !string.IsNullOrEmpty(propertyName))
{
Expand All @@ -441,15 +441,11 @@ T Get<T>(string name)

private void GenerateFromTileType(NamedObjectSave namedObjectSave, ICodeBlock codeBlock)
{
T Get<T>(string name)
{
return namedObjectSave.Properties.GetValue<T>(name);
}

var mapName = Get<string>(nameof(TileShapeCollectionPropertiesViewModel.SourceTmxName));
var typeName = Get<string>(nameof(TileShapeCollectionPropertiesViewModel.CollisionTileTypeName));
var removeTiles = Get<bool>(nameof(TileShapeCollectionPropertiesViewModel.RemoveTilesAfterCreatingCollision));
var isMerged = Get<bool>(nameof(TileShapeCollectionPropertiesViewModel.IsCollisionMerged));
var mapName = Get<string>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.SourceTmxName));
var typeName = Get<string>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.CollisionTileTypeName));
var removeTiles = Get<bool>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.RemoveTilesAfterCreatingCollision));
var isMerged = Get<bool>(namedObjectSave, nameof(TileShapeCollectionPropertiesViewModel.IsCollisionMerged));
if (!string.IsNullOrEmpty(mapName) && !string.IsNullOrEmpty(typeName))
{
string method = "AddCollisionFromTilesWithType";
Expand Down Expand Up @@ -478,11 +474,7 @@ public override ICodeBlock GenerateAdditionalMethods(ICodeBlock codeBlock, IElem
else
{
// override it if it's an outline:
T Get<T>(string name)
{
return tileShapeCollection.Properties.GetValue<T>(name);
}
var creationOptions = Get<CollisionCreationOptions>(
var creationOptions = Get<CollisionCreationOptions>(tileShapeCollection,
nameof(TileShapeCollectionPropertiesViewModel.CollisionCreationOptions));

if(creationOptions == CollisionCreationOptions.BorderOutline)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,61 @@
<ScrollViewer VerticalScrollBarVisibility="Auto">

<StackPanel IsEnabled="{Binding IsEntireViewEnabled}">
<GroupBox Header="{x:Static localization:Texts.LinksPerNode}">
<GroupBox Header="Links Per Node">
<StackPanel Orientation="Horizontal" >
<RadioButton IsChecked="{Binding IsFourDirectionalTypeChecked}" Content="{x:Static localization:Texts.Four}" />
<RadioButton IsChecked="{Binding IsEightDirectionalTypeChecked}" Margin="10,0,0,0" Content="{x:Static localization:Texts.Eight}" />
<RadioButton IsChecked="{Binding IsFourDirectionalTypeChecked}" Content="Four" />
<RadioButton IsChecked="{Binding IsEightDirectionalTypeChecked}" Margin="10,0,0,0" Content="Eight" />
</StackPanel>
</GroupBox>
<CheckBox IsChecked="{Binding EliminateCutCorners}" VerticalContentAlignment="Center" Content="{x:Static localization:Texts.EliminateCutCorners}" />
<GroupBox Header="{x:Static localization:Texts.CreationOptions}" Grid.Row="1">
<CheckBox IsChecked="{Binding EliminateCutCorners}" VerticalContentAlignment="Center" Content="Eliminate Cut Corners" />
<GroupBox Header="Creation Options" Grid.Row="1">
<StackPanel>
<RadioButton
Margin="0,10,0,0"
VerticalContentAlignment="Center"
IsChecked="{Binding IsEmptyChecked}" Content="{x:Static localization:Texts.Empty}" />
IsChecked="{Binding IsEmptyChecked}" Content="Empty" />
<RadioButton VerticalContentAlignment="Center" Margin="0,5,0,0"
IsChecked="{Binding IsFillCompletelyChecked}" Content="{x:Static localization:Texts.FillCompletely}" />
IsChecked="{Binding IsFillCompletelyChecked}" Content="Fill Completely" />
<StackPanel Margin="10, 0, 0, 0" Visibility="{Binding FillDimensionsVisibility}">

<StackPanel Orientation="Horizontal">
<TextBlock Width="60" Text="{x:Static localization:Texts.TileSize}" />
<TextBlock Width="60" Text="Tile Size:" />
<TextBox Width="100" Text="{Binding NodeNetworkTileSize}"
KeyUp="TextBox_KeyEnterUpdate"></TextBox>
</StackPanel>

<StackPanel Orientation="Horizontal" Margin="0,3,0,0">
<TextBlock Width="40" Text="{x:Static localization:Texts.Left}" />
<TextBlock Width="40" Text="Left:" />
<TextBox Width="100" Text="{Binding NodeNetworkFillLeft}"
KeyUp="TextBox_KeyEnterUpdate"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Width="40" Text="{x:Static localization:Texts.Top}" />
<TextBlock Width="40" Text="Top:" />
<TextBox Width="100" Text="{Binding NodeNetworkFillTop}"
KeyUp="TextBox_KeyEnterUpdate"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,3,0,0">
<TextBlock Width="60" Text="{x:Static localization:Texts.TilesWide}" />
<TextBlock Width="60" Text="Tiles wide:" />
<TextBox Width="100" Text="{Binding NodeNetworkFillWidth}"
KeyUp="TextBox_KeyEnterUpdate"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Width="60" Text="{x:Static localization:Texts.TilesHigh}" />
<TextBlock Width="60" Text="Tiles high:" />
<TextBox Width="100" Text="{Binding NodeNetworkFillHeight}"
KeyUp="TextBox_KeyEnterUpdate"></TextBox>
</StackPanel>
</StackPanel>


<RadioButton VerticalContentAlignment="Center" Margin="0,5,0,0"
IsChecked="{Binding IsFromTypeChecked}" Content="{x:Static localization:Texts.TypeFrom}" />
IsChecked="{Binding IsFromTypeChecked}" Content="From Type:" />
<StackPanel x:Name="Types" Margin="10, 0, 0, 0"
Visibility="{Binding FromTypeVisibility}">
<TextBlock Text="{x:Static localization:Texts.SourceTmxFileObject}" />
<TextBlock Text="Source TMX File/Object" />
<ComboBox Width="200" HorizontalAlignment="Left"
ItemsSource="{Binding TmxObjectNames}"
SelectedItem="{Binding SourceTmxName}"></ComboBox>
<TextBlock Margin="0,5,0,0" Text="{x:Static localization:Texts.Type}" />
<TextBlock Margin="0,5,0,0" Text="Type" />
<ComboBox x:Name="CollisionTileTypeComboBox"
IsEditable="True"
HorizontalAlignment="Left" Width="200"
Expand All @@ -90,7 +90,7 @@


<RadioButton VerticalContentAlignment="Center" Margin="0,5,0,0"
IsChecked="{Binding IsFromLayerChecked}" Content="{x:Static localization:Texts.LayerFrom}" />
IsChecked="{Binding IsFromLayerChecked}" Content="From Layer:" />
<StackPanel Margin="10,0,0,0" Visibility="{Binding FromLayerVisibility}">
<TextBlock Text="{x:Static localization:Texts.SourceTmxFileObject}" />
<ComboBox Width="200" HorizontalAlignment="Left"
Expand Down
Loading

0 comments on commit f9a8968

Please sign in to comment.