+ In-progress Cube/center binding implementation

This commit is contained in:
Michael L Smith 2016-02-29 21:52:13 -08:00
parent 0989c4660c
commit 564a19a286
5 changed files with 89 additions and 3 deletions

View File

@ -144,5 +144,19 @@ namespace OSCADSharp.UnitTests
Assert.AreEqual(Convert.ToDouble(zValue.Value), obj.Size.Z);
Assert.IsTrue(script.Contains("size = [xVal, yVal, zVal]"));
}
[TestMethod]
public void Cube_CenterBindingAppearsInOutput()
{
Variable centerVal = new Variable("isCentered", true);
var obj = new Cube();
obj.Bind("Center", centerVal);
string script = obj.ToString();
Assert.AreEqual(centerVal.Value, obj.Center);
Assert.IsTrue(script.Contains("center = isCentered"));
}
}
}

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OSCADSharp.Scripting;
namespace OSCADSharp.Bindings
{
internal class BindableBoolean : IBindable
{
public string InnerValue
{
get;
set;
}
private Bindings bindings = new Bindings(new Dictionary<string, string>()
{
{ "innervalue", "innervalue" }
});
/// <summary>
/// Creates a bindable boolean, which is more or less a
/// proxy for bindings on boolean fields
/// </summary>
/// <param name="propertyName">Name of the property in the containing class for binding.
/// This will be used as a synonym</param>
internal BindableBoolean(string propertyName)
{
this.boundProperty = propertyName;
this.bindings.Synonym("innervalue", propertyName);
}
private string boundProperty = null;
public bool IsBound { get; set; } = false;
public void Bind(string property, Variable variable)
{
this.IsBound = true;
var stringifiedVar = new Variable(variable.Name, variable.Value.ToString().ToLower());
this.bindings.Add<BindableBoolean>(this, property, stringifiedVar);
}
public override string ToString()
{
return this.bindings.Get(this.boundProperty).BoundVariable.Name;
}
}
}

View File

@ -120,6 +120,9 @@ namespace OSCADSharp.Bindings
internal Binding Get(string propertyName)
{
if (this.hasMatchingSynonym(propertyName))
return this.bindings[this.synonyms[propertyName]];
return bindings[propertyName];
}

View File

@ -42,6 +42,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Bindings\BindableBoolean.cs" />
<Compile Include="Settings\Dependencies.cs" />
<Compile Include="Files\DefaultFileInvoker.cs" />
<Compile Include="Files\DefaultFileWriter.cs" />

View File

@ -16,6 +16,8 @@ namespace OSCADSharp.Solids
{
#region Attributes
private Vector3 size = new BindableVector(1, 1, 1, sizeSynonyms);
private bool center = false;
private BindableBoolean centerBinding = new BindableBoolean("center");
/// <summary>
/// The Size of the cube in terms of X/Y/Z units
@ -31,7 +33,15 @@ namespace OSCADSharp.Solids
///
/// If False (default) one corner will be centered at 0,0, 0, with the cube extending into the positive octant (positive X/Y/Z)
/// </summary>
public bool Center { get; set; } = false;
public bool Center
{
get { return this.center; }
set
{
this.center = value;
this.centerBinding.InnerValue = this.center.ToString().ToLower();
}
}
#endregion
#region Constructors
@ -77,8 +87,10 @@ namespace OSCADSharp.Solids
/// <returns>Script for this object</returns>
public override string ToString()
{
return String.Format("cube(size = {0}, center = {1}); {2}",
this.Size.ToString(), this.Center.ToString().ToLower(), Environment.NewLine); ;
return String.Format("cube(size = {0}, center = {1}); {2}",
this.Size.ToString(),
this.centerBinding.IsBound ? this.centerBinding.ToString() : this.center.ToString().ToLower(),
Environment.NewLine); ;
}
/// <summary>
@ -161,6 +173,12 @@ namespace OSCADSharp.Solids
vec.Bind(property, variable);
}
if(property.ToLower() == "center")
{
this.centerBinding.Bind(property, variable);
this.center = Convert.ToBoolean(variable.Value);
}
}
#endregion
}