Skip to content

Commit

Permalink
Finish up API separation between SVGImport and body/fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Genbox committed May 25, 2017
1 parent 3189550 commit f68d1a6
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 85 deletions.
31 changes: 15 additions & 16 deletions ContentPipeline/SVGImport/PathContainerProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,25 @@ public override VerticesContainer Process(List<PathDefinition> input, ContentPro

Matrix matScale = Matrix.CreateScale(_scaleFactor, _scaleFactor, 1f);
SVGPathParser parser = new SVGPathParser(BezierIterations);
VerticesContainer vc = new VerticesContainer();
VerticesContainer container = new VerticesContainer();

foreach (PathDefinition definition in input)
foreach (PathDefinition d in input)
{
List<VerticesExt> vertices = parser.ParseSVGPath(definition.Path, definition.Transformation * matScale);

//if (vertices.Count == 1)
// vc.Add(vertices[0]);
//else
//{
// for (int i = 0; i < vertices.Count; i++)
// {
// list.Add(vertices[i]);
// }
//}

//vc.Add(c.Name, list);
List<VerticesExt> vertices = parser.ParseSVGPath(d.Path, d.Transformation * matScale);
List<VerticesExt> c = container.ContainsKey(d.Id) ? container[d.Id] : (container[d.Id] = new List<VerticesExt>());

if (vertices.Count == 1)
c.Add(vertices[0]);
else
{
for (int i = 0; i < vertices.Count; i++)
{
c.Add(vertices[i]);
}
}
}

return vc;
return container;
}
}
}
16 changes: 0 additions & 16 deletions ContentPipeline/SVGImport/Program.cs

This file was deleted.

9 changes: 2 additions & 7 deletions ContentPipeline/SVGImport/SVGImport.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputType>exe</OutputType>
<OutputType>Library</OutputType>
</PropertyGroup>

<PropertyGroup>
<TargetFramework>net45</TargetFramework>
<AssemblyName>VelcroPhysics.ContentPipelines.SVGImport</AssemblyName>
<RootNamespace>VelcroPhysics.ContentPipelines.SVGImport</RootNamespace>
<ApplicationIcon />
<OutputTypeEx>exe</OutputTypeEx>
<OutputTypeEx>library</OutputTypeEx>
<StartupObject />
</PropertyGroup>

<ItemGroup>
<Compile Remove="VerticesContainerReader.cs" />
<Compile Remove="VerticesContainerWriter.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MonoGame.Framework.Content.Pipeline.Portable" Version="3.6.0.1625" />
</ItemGroup>
Expand Down
16 changes: 7 additions & 9 deletions ContentPipeline/SVGImport/SVGImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

namespace VelcroPhysics.ContentPipelines.SVGImport
{

[ContentImporter(".svg", DisplayName = "SVG Importer", DefaultProcessor = "PathContainerProcessor")]
public class SVGImporter : ContentImporter<List<PathDefinition>>
{
private const string _isNumber = @"\A[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?";
private const string _isCommaWhitespace = @"\A[\s,]*";
private string _currentId;
private List<PathDefinition> _parsedSVG;

private Stack<Matrix> _transformations;
Expand All @@ -29,7 +27,6 @@ public override List<PathDefinition> Import(string filename, ContentImporterCont
XmlDocument input = new XmlDocument();
input.Load(filename);

_currentId = null;
ParseSVGNode(input["svg"]);

return _parsedSVG;
Expand All @@ -48,17 +45,18 @@ private void ParseSVGNode(XmlNode currentNode)
popTransform = true;
}

if (currentElement.Name == "g" && currentElement.HasAttribute("id"))
_currentId = currentElement.Attributes["id"].Value;

if (currentElement.Name == "path")
{
PathDefinition path = new PathDefinition();

if (_currentId != null)
path.Id = _currentId;
string currentId = currentElement.HasAttribute("velcro_id") ? currentElement.Attributes["velcro_id"].Value : null;
XmlElement parent = currentElement.ParentNode as XmlElement;

//Take the attribute from the parent if it is a group, otherwise just take it from the current element
if (currentId == null && parent != null && parent.HasAttribute("velcro_id"))
path.Id = parent.Attributes["velcro_id"].Value;
else
path.Id = currentElement.HasAttribute("id") ? currentElement.Attributes["id"].Value : "empty_id";
path.Id = currentElement.HasAttribute("velcro_id") ? currentElement.Attributes["velcro_id"].Value : "empty_id";

path.Path = currentElement.Attributes["d"].Value;
path.Transformation = Matrix.Identity;
Expand Down
31 changes: 20 additions & 11 deletions ContentPipeline/SVGImport/VerticesContainerReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,38 @@

namespace VelcroPhysics.ContentPipelines.SVGImport
{
public class VerticesContainerReader : ContentTypeReader<Dictionary<string, VerticesExt>>
public class VerticesContainerReader : ContentTypeReader<VerticesContainer>
{
protected override Dictionary<string, VerticesExt> Read(ContentReader input, Dictionary<string, VerticesExt> existingInstance)
protected override VerticesContainer Read(ContentReader input, VerticesContainer existingInstance)
{
Dictionary<string, VerticesExt> paths = existingInstance ?? new Dictionary<string, VerticesExt>();
VerticesContainer container = existingInstance ?? new VerticesContainer();

int count = input.ReadInt32();
int count = input.ReadInt32(); //container.Count
for (int i = 0; i < count; i++)
{
string name = input.ReadString();
bool closed = input.ReadBoolean();
int vertsCount = input.ReadInt32();
int listCount = input.ReadInt32();

Vertices verts = new Vertices(vertsCount);
for (int j = 0; j < vertsCount; j++)
List<VerticesExt> exts = new List<VerticesExt>();

for (int j = 0; j < listCount; j++)
{
verts.Add(input.ReadVector2());
bool closed = input.ReadBoolean();
int vertCount = input.ReadInt32();

Vertices verts = new Vertices(vertCount);
for (int x = 0; x < vertCount; x++)
{
verts.Add(input.ReadVector2());
}

exts.Add(new VerticesExt(verts, closed));
}

paths[name] = new VerticesExt(verts, closed);
container.Add(name, exts);
}

return paths;
return container;
}
}
}
14 changes: 10 additions & 4 deletions ContentPipeline/SVGImport/VerticesContainerWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ public class VerticesContainerWriter : ContentTypeWriter<VerticesContainer>
protected override void Write(ContentWriter output, VerticesContainer container)
{
output.Write(container.Count);
foreach (KeyValuePair<string, VerticesExt> p in container)
foreach (KeyValuePair<string, List<VerticesExt>> p in container)
{
output.Write(p.Key);
output.Write(p.Value.Closed);
output.Write(p.Value.Count);
foreach (Vector2 vec in p.Value)

foreach (VerticesExt ext in p.Value)
{
output.Write(vec);
output.Write(ext.Closed);
output.Write(ext.Count);

foreach (Vector2 vec in ext)
{
output.Write(vec);
}
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions Samples/Demo/Content/Pipeline/Body.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Samples/Demo/Content/Pipeline/BreakableBody.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 3 additions & 5 deletions Samples/Demo/Demos/D15_SVGtoBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ public override void LoadContent()
_clubBody = Create(_loadedVertices["Club"]);
_club = new Sprite(ContentWrapper.GetTexture("Club"), ContentWrapper.CalculateOrigin(_clubBody));

//_spadeBody = Create(_loadedVertices["Spade"]);
//_spade = new Sprite(ContentWrapper.GetTexture("Spade"), ContentWrapper.CalculateOrigin(_spadeBody));
_spadeBody = Create(_loadedVertices["Spade"]);
_spade = new Sprite(ContentWrapper.GetTexture("Spade"), ContentWrapper.CalculateOrigin(_spadeBody));

_diamondBody = Create(_loadedVertices["Diamond"]);
_diamond = new Sprite(ContentWrapper.GetTexture("Diamond"), ContentWrapper.CalculateOrigin(_diamondBody));
}


private Body Create(List<VerticesExt> ext)
{
Body b = BodyFactory.CreateBody(World, bodyType: BodyType.Dynamic);
Expand All @@ -63,7 +62,6 @@ private Body Create(List<VerticesExt> ext)
{
FixtureFactory.AttachPolygon(v, 1, b);
}

}

return b;
Expand All @@ -74,7 +72,7 @@ public override void Draw(GameTime gameTime)
Sprites.Begin(0, null, null, null, null, null, Camera.View);
Sprites.Draw(_heart.Image, ConvertUnits.ToDisplayUnits(_heartBody.Position), null, Color.White, _heartBody.Rotation, _heart.Origin, 1f, SpriteEffects.None, 0f);
Sprites.Draw(_club.Image, ConvertUnits.ToDisplayUnits(_clubBody.Position), null, Color.White, _clubBody.Rotation, _club.Origin, 1f, SpriteEffects.None, 0f);
//Sprites.Draw(_spade.Image, ConvertUnits.ToDisplayUnits(_spadeBody.Position), null, Color.White, _spadeBody.Rotation, _spade.Origin, 1f, SpriteEffects.None, 0f);
Sprites.Draw(_spade.Image, ConvertUnits.ToDisplayUnits(_spadeBody.Position), null, Color.White, _spadeBody.Rotation, _spade.Origin, 1f, SpriteEffects.None, 0f);
Sprites.Draw(_diamond.Image, ConvertUnits.ToDisplayUnits(_diamondBody.Position), null, Color.White, _diamondBody.Rotation, _diamond.Origin, 1f, SpriteEffects.None, 0f);
Sprites.End();

Expand Down
21 changes: 11 additions & 10 deletions Samples/Demo/Demos/D16_BreakableBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
using VelcroPhysics.Collision.Shapes;
using VelcroPhysics.ContentPipelines.SVGImport.Objects;
using VelcroPhysics.Dynamics;
using VelcroPhysics.Factories;
using VelcroPhysics.Samples.Demo.Demos.Prefabs;
using VelcroPhysics.Samples.Demo.MediaSystem;
using VelcroPhysics.Samples.Demo.ScreenSystem;
using VelcroPhysics.Shared;
using VelcroPhysics.Tools.PolygonManipulation;
using VelcroPhysics.Tools.Triangulation.TriangulationBase;
using VelcroPhysics.Utilities;

Expand All @@ -33,7 +33,7 @@ public override void LoadContent()
for (int i = 0; i < 3; i++)
{
VerticesContainer verticesContainer = Framework.Content.Load<VerticesContainer>("Pipeline/BreakableBody");
var def = verticesContainer["Cookie"];
List<VerticesExt> def = verticesContainer["Cookie"];
_breakableCookie[i] = CreateBreakable(def);
_breakableCookie[i].Strength = 120f;
_breakableCookie[i].MainBody.Position = new Vector2(-20.33f + 15f * i, -5.33f);
Expand All @@ -55,24 +55,25 @@ public override void LoadContent()

private BreakableBody CreateBreakable(List<VerticesExt> ext)
{
List<PolygonShape> polygons = new List<PolygonShape>();

foreach (VerticesExt ve in ext)
{
List<Vertices> list = Triangulate.ConvexPartition(ve, TriangulationAlgorithm.Bayazit);
Vertices simple = SimplifyTools.DouglasPeuckerSimplify(ve, 0.1f);

List<Vertices> list = Triangulate.ConvexPartition(simple, TriangulationAlgorithm.Bayazit);

List<PolygonShape> polygons = new List<PolygonShape>(list.Count);
foreach (Vertices v in list)
{
PolygonShape s = new PolygonShape(v, 1f);
polygons.Add(s);
}

BreakableBody body = new BreakableBody(World, polygons);
World.AddBreakableBody(body);

return body;
}

return null;
BreakableBody body = new BreakableBody(World, polygons);
World.AddBreakableBody(body);

return body;
}

public override void HandleInput(InputHelper input, GameTime gameTime)
Expand Down

0 comments on commit f68d1a6

Please sign in to comment.