From 892fccdf7e10cb49e97f2210b21455235df3dc95 Mon Sep 17 00:00:00 2001 From: Michael L Smith Date: Wed, 2 Mar 2016 19:24:24 -0800 Subject: [PATCH] + Added Color overload to automatically bind variables to passed-in values --- OSCADSharp/OSCADSharp.ConsoleTests/Program.cs | 5 ++++- .../Transforms/ColorTests.cs | 14 ++++++++++++ OSCADSharp/OSCADSharp/OSCADObject.cs | 13 ++++++++++- .../OSCADSharp/Transforms/ColoredObject.cs | 22 +++++++++++++++++-- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs b/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs index f159b76..5e22705 100644 --- a/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs +++ b/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs @@ -14,6 +14,7 @@ namespace OSCADSharp.ConsoleTests { static void Main(string[] args) { + Variables.Global.Add("myColor", "\"Red\""); Variables.Global.Add("sphereRadius", 15); Variables.Global.Add("cubeWidth", 10); @@ -24,8 +25,10 @@ namespace OSCADSharp.ConsoleTests cube.Bind("Width", Variables.Global["cubeWidth"]); cube.Bind("Height", Variables.Global["sphereRadius"]); cube.Size.X = 30; + var cb = cube.Color(Variables.Global["myColor"]); - obj = obj + cube; + + obj = obj + cb; var pos = obj.Position(); var cyl1 = new Cylinder(1, 100, true).Translate(pos); diff --git a/OSCADSharp/OSCADSharp.UnitTests/Transforms/ColorTests.cs b/OSCADSharp/OSCADSharp.UnitTests/Transforms/ColorTests.cs index 3151a7a..5d67864 100644 --- a/OSCADSharp/OSCADSharp.UnitTests/Transforms/ColorTests.cs +++ b/OSCADSharp/OSCADSharp.UnitTests/Transforms/ColorTests.cs @@ -27,5 +27,19 @@ namespace OSCADSharp.UnitTests.Transforms Assert.IsTrue(script.Contains("myFavoriteColor")); Assert.IsTrue(script.Contains("cubeOpacity")); } + + [TestMethod] + public void Color_CanBindWithColorOverload() + { + Variable colorVar = new Variable("myFavoriteColor", "blue"); + Variable cubeOpacity = new Variable("cubeOpacity", .6); + + var obj = new Cube().Color(colorVar, cubeOpacity); + + string script = obj.ToString(); + + Assert.IsTrue(script.Contains("myFavoriteColor")); + Assert.IsTrue(script.Contains("cubeOpacity")); + } } } diff --git a/OSCADSharp/OSCADSharp/OSCADObject.cs b/OSCADSharp/OSCADSharp/OSCADObject.cs index 0b3dc64..c262b06 100644 --- a/OSCADSharp/OSCADSharp/OSCADObject.cs +++ b/OSCADSharp/OSCADSharp/OSCADObject.cs @@ -44,7 +44,18 @@ namespace OSCADSharp public OSCADObject Color(string colorName, double opacity = 1.0) { return new ColoredObject(this, colorName, opacity); - } + } + + /// + /// Applies Color and/or Opacity to this object with variable bindings + /// + /// Color name variable to apply + /// (optional)Opacity variable + /// + public OSCADObject Color(Variable colorName, Variable opacity = null) + { + return new ColoredObject(this, colorName, opacity); + } /// /// Mirrors the object about a plane, as specified by the normal diff --git a/OSCADSharp/OSCADSharp/Transforms/ColoredObject.cs b/OSCADSharp/OSCADSharp/Transforms/ColoredObject.cs index 017905c..7e892cb 100644 --- a/OSCADSharp/OSCADSharp/Transforms/ColoredObject.cs +++ b/OSCADSharp/OSCADSharp/Transforms/ColoredObject.cs @@ -30,14 +30,29 @@ namespace OSCADSharp.Transforms this.Opacity = opacity; } + /// + /// Creates a colorized object with predefined bindings + /// + /// The object(s) to which color will be applied + /// + /// + internal ColoredObject(OSCADObject obj, Variable colorName, Variable opacity) : base(obj) + { + this.bindings.Add(this, "color", colorName); + if(opacity != null) + { + this.bindings.Add(this, "opacity", opacity); + } + } + public override string ToString() { string colorName = this.bindings.Contains("color") ? this.bindings.Get("color").BoundVariable.Name : - this.ColorName; + "\""+this.ColorName+"\""; string opacity = this.bindings.Contains("opacity") ? this.bindings.Get("opacity").BoundVariable.Name : this.Opacity.ToString(); - string colorCommand = String.Format("color(\"{0}\", {1})", colorName, opacity); + string colorCommand = String.Format("color({0}, {1})", colorName, opacity); var formatter = new SingleBlockFormatter(colorCommand, this.obj.ToString()); return formatter.ToString(); } @@ -64,6 +79,9 @@ namespace OSCADSharp.Transforms {"color", "color" }, {"opacity", "opacity" } }); + private Variable colorName; + private Variable opacity; + public override void Bind(string property, Variable variable) { this.bindings.Add(this, property, variable);