From 637e10162b4fcf7e5dd6d2a47484ac0ad4b324b2 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Thu, 17 Mar 2016 23:12:38 -0700 Subject: [PATCH] Reverted addition of CubeBindings/CubeScriptBuilder --- OSCADSharp/OSCADSharp/BindableBoolean.cs | 8 +++ OSCADSharp/OSCADSharp/Cube.cs | 80 +++++++++++++++++----- OSCADSharp/OSCADSharp/CubeBindings.cs | 74 -------------------- OSCADSharp/OSCADSharp/CubeScriptBuilder.cs | 33 --------- OSCADSharp/OSCADSharp/OSCADSharp.csproj | 2 - 5 files changed, 70 insertions(+), 127 deletions(-) delete mode 100644 OSCADSharp/OSCADSharp/CubeBindings.cs delete mode 100644 OSCADSharp/OSCADSharp/CubeScriptBuilder.cs diff --git a/OSCADSharp/OSCADSharp/BindableBoolean.cs b/OSCADSharp/OSCADSharp/BindableBoolean.cs index f7468d9..9202c59 100644 --- a/OSCADSharp/OSCADSharp/BindableBoolean.cs +++ b/OSCADSharp/OSCADSharp/BindableBoolean.cs @@ -45,5 +45,13 @@ namespace OSCADSharp { return this.bindings.Get(this.boundProperty).BoundVariable.Text; } + + public BindableBoolean Clone() + { + var clone = new BindableBoolean(this.boundProperty); + clone.bindings = this.bindings; + + return clone; + } } } diff --git a/OSCADSharp/OSCADSharp/Cube.cs b/OSCADSharp/OSCADSharp/Cube.cs index 83e5776..d724857 100644 --- a/OSCADSharp/OSCADSharp/Cube.cs +++ b/OSCADSharp/OSCADSharp/Cube.cs @@ -12,17 +12,33 @@ namespace OSCADSharp public class Cube : OSCADObject { #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 /// - public Vector3 Size { get; set; } = new Vector3(1, 1, 1); + public Vector3 Size + { + get { return this.size; } + set { this.size = new BindableVector(value, sizeSynonyms); } + } /// /// If True, the center of the cube will be at 0, 0, 0 /// /// 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 @@ -40,14 +56,8 @@ namespace OSCADSharp /// Indicates whether the cube should be centered on the origin public Cube(Vector3 size = null, bool center = false) { - this.Size = size; + this.Size = new BindableVector(size, sizeSynonyms) ?? new BindableVector(1, 1, 1, sizeSynonyms); this.Center = center; - - this.bindings.SizeBinding.X = size.X; - this.bindings.SizeBinding.Y = size.Y; - this.bindings.SizeBinding.Z = size.Z; - - this.bindings.CenterBinding.InnerValue = this.Center.ToString().ToLower(); } /// @@ -89,8 +99,10 @@ namespace OSCADSharp /// Script for this object public override string ToString() { - var scriptBuilder = new CubeScriptBuilder(this.bindings, this); - return scriptBuilder.GetScript(); + return String.Format("cube(size = {0}, center = {1}); {2}", + this.Size.ToString(), + this.centerBinding.IsBound ? this.centerBinding.ToString() : this.center.ToString().ToLower(), + Environment.NewLine); ; } /// @@ -99,11 +111,15 @@ namespace OSCADSharp /// public override OSCADObject Clone() { + var size = this.size as BindableVector; + var center = this.centerBinding.Clone(); + var clone = new Cube() { Name = this.Name, - Size = this.Size.Clone(), - Center = this.Center, + size = size.Clone(), + center = this.Center, + centerBinding = center, bindings = this.bindings.Clone() }; @@ -118,7 +134,7 @@ namespace OSCADSharp public override Vector3 Position() { Vector3 position; - if(this.Center == false) + if (this.Center == false) { position = new Vector3(this.Size.X / 2, this.Size.Y / 2, this.Size.Z / 2); } @@ -136,18 +152,28 @@ namespace OSCADSharp /// public override Bounds Bounds() { - if(Center == false) + if (Center == false) { return new Bounds(new Vector3(), new Vector3(this.Size.X, this.Size.Y, this.Size.Z)); } else { - return new Bounds(new Vector3(-this.Size.X / 2, -this.Size.Y / 2, -this.Size.Z / 2), + return new Bounds(new Vector3(-this.Size.X / 2, -this.Size.Y / 2, -this.Size.Z / 2), new Vector3(this.Size.X / 2, this.Size.Y / 2, this.Size.Z / 2)); } } - private CubeBindings bindings = new CubeBindings(); + private Bindings bindings = new Bindings(); + private static readonly Dictionary sizeSynonyms = new Dictionary() + { + {"size.x", "x" }, + {"size.y", "y" }, + {"size.z", "z" }, + {"length", "x" }, + {"width", "y" }, + {"height", "z" } + }; + /// /// Binds a a variable to a property on this object /// @@ -156,7 +182,25 @@ namespace OSCADSharp /// literal value of the property public override void Bind(string property, Variable variable) { - bindings.Bind(this, property, variable); + if (sizeSynonyms.ContainsKey(property.ToLower())) + { + BindableVector vec; + if (this.size is BindableVector) + vec = this.Size as BindableVector; + else + vec = new BindableVector(this.size); + + vec.Bind(property, variable); + } + else if (property.ToLower() == "center") + { + this.centerBinding.Bind(property, variable); + this.center = Convert.ToBoolean(variable.Value); + } + else + { + throw new KeyNotFoundException(String.Format("No bindable property matching the name {0} was found", property)); + } } #endregion } diff --git a/OSCADSharp/OSCADSharp/CubeBindings.cs b/OSCADSharp/OSCADSharp/CubeBindings.cs deleted file mode 100644 index eb597c5..0000000 --- a/OSCADSharp/OSCADSharp/CubeBindings.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OSCADSharp -{ - internal class CubeBindings : ICloneable, IBindings - { - private Bindings bindings = new Bindings(); - private static readonly Dictionary sizeSynonyms = new Dictionary() - { - {"size.x", "x" }, - {"size.y", "y" }, - {"size.z", "z" }, - {"length", "x" }, - {"width", "y" }, - {"height", "z" } - }; - - public BindableVector SizeBinding { get; set; } = new BindableVector(new Vector3(), sizeSynonyms); - public BindableBoolean CenterBinding { get; set; } = new BindableBoolean("center"); - - public void Bind(T obj, string property, Variable variable) - { - string prop = property.ToLower(); - Cube cube = obj as Cube; - if (sizeSynonyms.ContainsKey(prop)) - { - this.SizeBinding.X = cube.Size.X; - this.SizeBinding.Y = cube.Size.Y; - this.SizeBinding.Z = cube.Size.Z; - - this.SizeBinding.Bind(prop, variable); - cube.Size = new Vector3(SizeBinding.X, SizeBinding.Y, SizeBinding.Z); - } - else if (prop == "center") - { - this.CenterBinding.Bind(prop, variable); - cube.Center = Convert.ToBoolean(variable.Value); - } - else - { - throw new KeyNotFoundException(String.Format("No bindable property matching the name {0} was found", prop)); - } - } - - 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 CubeBindings Clone() - { - return new CubeBindings() - { - bindings = bindings.Clone(), - CenterBinding = this.CenterBinding, - SizeBinding = this.SizeBinding - }; - } - } -} diff --git a/OSCADSharp/OSCADSharp/CubeScriptBuilder.cs b/OSCADSharp/OSCADSharp/CubeScriptBuilder.cs deleted file mode 100644 index f027835..0000000 --- a/OSCADSharp/OSCADSharp/CubeScriptBuilder.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OSCADSharp -{ - internal class CubeScriptBuilder - { - private CubeBindings bindings; - private Cube cube; - - internal CubeScriptBuilder(CubeBindings bindings, Cube cube) - { - this.bindings = bindings; - this.cube = cube; - } - - internal string GetScript() - { - this.bindings.SizeBinding.X = this.cube.Size.X; - this.bindings.SizeBinding.Y = this.cube.Size.Y; - this.bindings.SizeBinding.Z = this.cube.Size.Z; - - return String.Format("cube(size = {0}, center = {1}); {2}", - this.bindings.SizeBinding.ToString(), - this.bindings.CenterBinding.IsBound ? this.bindings.CenterBinding.ToString() : - this.cube.Center.ToString().ToLower(), - Environment.NewLine); - } - } -} diff --git a/OSCADSharp/OSCADSharp/OSCADSharp.csproj b/OSCADSharp/OSCADSharp/OSCADSharp.csproj index 09675a2..572469b 100644 --- a/OSCADSharp/OSCADSharp/OSCADSharp.csproj +++ b/OSCADSharp/OSCADSharp/OSCADSharp.csproj @@ -44,10 +44,8 @@ - -