+ Added Color overload to automatically bind variables to passed-in values

This commit is contained in:
Michael L Smith 2016-03-02 19:24:24 -08:00
parent 13dbc41149
commit 892fccdf7e
4 changed files with 50 additions and 4 deletions

View File

@ -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);

View File

@ -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"));
}
}
}

View File

@ -44,7 +44,18 @@ namespace OSCADSharp
public OSCADObject Color(string colorName, double opacity = 1.0)
{
return new ColoredObject(this, colorName, opacity);
}
}
/// <summary>
/// Applies Color and/or Opacity to this object with variable bindings
/// </summary>
/// <param name="colorName">Color name variable to apply</param>
/// <param name="opacity">(optional)Opacity variable</param>
/// <returns></returns>
public OSCADObject Color(Variable colorName, Variable opacity = null)
{
return new ColoredObject(this, colorName, opacity);
}
/// <summary>
/// Mirrors the object about a plane, as specified by the normal

View File

@ -30,14 +30,29 @@ namespace OSCADSharp.Transforms
this.Opacity = opacity;
}
/// <summary>
/// Creates a colorized object with predefined bindings
/// </summary>
/// <param name="obj">The object(s) to which color will be applied</param>
/// <param name="colorName"></param>
/// <param name="opacity"></param>
internal ColoredObject(OSCADObject obj, Variable colorName, Variable opacity) : base(obj)
{
this.bindings.Add<ColoredObject>(this, "color", colorName);
if(opacity != null)
{
this.bindings.Add<ColoredObject>(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<ColoredObject>(this, property, variable);