diff --git a/OSCADSharp/OSCADSharp.UnitTests/Solids/CubeTests.cs b/OSCADSharp/OSCADSharp.UnitTests/Solids/CubeTests.cs index 25d31e0..fb2a027 100644 --- a/OSCADSharp/OSCADSharp.UnitTests/Solids/CubeTests.cs +++ b/OSCADSharp/OSCADSharp.UnitTests/Solids/CubeTests.cs @@ -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")); + } } } diff --git a/OSCADSharp/OSCADSharp/Bindings/BindableBoolean.cs b/OSCADSharp/OSCADSharp/Bindings/BindableBoolean.cs new file mode 100644 index 0000000..0951efc --- /dev/null +++ b/OSCADSharp/OSCADSharp/Bindings/BindableBoolean.cs @@ -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() + { + { "innervalue", "innervalue" } + }); + + /// + /// Creates a bindable boolean, which is more or less a + /// proxy for bindings on boolean fields + /// + /// Name of the property in the containing class for binding. + /// This will be used as a synonym + 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(this, property, stringifiedVar); + } + + public override string ToString() + { + return this.bindings.Get(this.boundProperty).BoundVariable.Name; + } + } +} diff --git a/OSCADSharp/OSCADSharp/Bindings/Bindings.cs b/OSCADSharp/OSCADSharp/Bindings/Bindings.cs index 5c5efd3..62ac29f 100644 --- a/OSCADSharp/OSCADSharp/Bindings/Bindings.cs +++ b/OSCADSharp/OSCADSharp/Bindings/Bindings.cs @@ -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]; } diff --git a/OSCADSharp/OSCADSharp/OSCADSharp.csproj b/OSCADSharp/OSCADSharp/OSCADSharp.csproj index a6643e1..4619df6 100644 --- a/OSCADSharp/OSCADSharp/OSCADSharp.csproj +++ b/OSCADSharp/OSCADSharp/OSCADSharp.csproj @@ -42,6 +42,7 @@ + diff --git a/OSCADSharp/OSCADSharp/Solids/Cube.cs b/OSCADSharp/OSCADSharp/Solids/Cube.cs index 958e009..57cf5a7 100644 --- a/OSCADSharp/OSCADSharp/Solids/Cube.cs +++ b/OSCADSharp/OSCADSharp/Solids/Cube.cs @@ -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"); /// /// 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) /// - 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 /// Script for this object 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); ; } /// @@ -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 }