Refactored SphereBindings out of Sphere.cs

This commit is contained in:
Michael Smith 2016-03-08 23:53:44 -08:00
parent f4642a205c
commit 4ecf664db5
6 changed files with 96 additions and 13 deletions

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.Bindings
{
internal interface IBindings
{
void Bind<T>(T obj, string property, Variable variable);
bool Contains(string openScadFieldName);
Binding Get(string propertyName);
void Synonym(string propertyName, string alternateName);
}
}

View File

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.Bindings.Solids
{
internal class SphereBindings : ICloneable<SphereBindings>, IBindings
{
private Bindings bindings = new Bindings(new Dictionary<string, string>()
{
{ "radius", "r" },
{ "minimumangle", "$fa" },
{ "minimumfragmentsize", "$fs" },
{ "resolution", "$fn" },
{ "diameter", "d" }
});
public SphereBindings Clone()
{
return new SphereBindings() {
bindings = bindings.Clone()
};
}
public bool Contains(string openScadFieldName)
{
return this.bindings.Contains(openScadFieldName);
}
public Binding Get(string propertyName)
{
return this.bindings.Get(propertyName);
}
public void Synonym(string propertyName, string alternateName)
{
this.bindings.Synonym(propertyName, alternateName);
}
public void Bind<T>(T obj, string property, Variable variable)
{
this.bindings.Add<T>(obj, property, variable);
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp
{
internal interface ICloneable<T>
{
T Clone();
}
}

View File

@ -1,4 +1,5 @@
using System;
using OSCADSharp.Bindings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -14,6 +15,7 @@ namespace OSCADSharp.Scripting
{
private StringBuilder SB { get; set; } = new StringBuilder();
private Bindings.Bindings bindings = null;
private IBindings ibindings = null;
internal StatementBuilder()
{
@ -24,6 +26,10 @@ namespace OSCADSharp.Scripting
this.bindings = bindings;
}
internal StatementBuilder(IBindings ibindings)
{
this.ibindings = ibindings;
}
/// <summary>
/// Special append method for conditionally adding value-pairs
@ -47,7 +53,8 @@ namespace OSCADSharp.Scripting
if(useBinding)
{
SB.Append(this.bindings.Get(name).BoundVariable.Text);
SB.Append(this.bindings?.Get(name).BoundVariable.Text);
SB.Append(this.ibindings?.Get(name).BoundVariable.Text);
}
else
{
@ -58,7 +65,8 @@ namespace OSCADSharp.Scripting
private bool shouldUseBinding(string name)
{
return this.bindings != null && this.bindings.Contains(name);
return (this.bindings != null && this.bindings.Contains(name))
|| (this.ibindings != null && this.ibindings.Contains(name));
}
/// <summary>

View File

@ -43,6 +43,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Internal\Bindings\BindableBoolean.cs" />
<Compile Include="Internal\Bindings\IBindings.cs" />
<Compile Include="Internal\Bindings\Solids\SphereBindings.cs" />
<Compile Include="Internal\ICloneable.cs" />
<Compile Include="Internal\Scripting\VariableCalculator.cs" />
<Compile Include="Internal\Scripting\CompoundVariable.cs" />
<Compile Include="Public\Settings\Dependencies.cs" />

View File

@ -8,6 +8,7 @@ using OSCADSharp.Scripting;
using System.Collections.Concurrent;
using System.Reflection;
using OSCADSharp.Bindings;
using OSCADSharp.Bindings.Solids;
namespace OSCADSharp
{
@ -147,15 +148,7 @@ namespace OSCADSharp
new Vector3(this.Radius, this.Radius, this.Radius));
}
private Bindings.Bindings bindings = new Bindings.Bindings(new Dictionary<string, string>()
{
{ "radius", "r" },
{ "minimumangle", "$fa" },
{ "minimumfragmentsize", "$fs" },
{ "resolution", "$fn" },
{ "diameter", "d" }
});
private SphereBindings bindings = new SphereBindings();
/// <summary>
/// Binds a a variable to a property on this object
/// </summary>
@ -164,7 +157,7 @@ namespace OSCADSharp
/// literal value of the property</param>
public override void Bind(string property, Variable variable)
{
this.bindings.Add<Sphere>(this, property, variable);
this.bindings.Bind<Sphere>(this, property, variable);
}
#endregion
}