private static void Draw(ReticleDefinition reticle, string baseName) { var canvas = SvgCanvasFactory.Create("reticle", "2in", "2in"); var controller = new ReticleDrawController(reticle, canvas); controller.DrawReticle(); var svg = SvgCanvasFactory.ToSvg(canvas); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(svg); var svgDocument = SvgDocument.Open(xmlDocument); var bm = svgDocument.Draw(1024, 1024); var bm1 = new Bitmap(1024, 1024); Graphics g = Graphics.FromImage(bm1); g.FillRectangle(Brushes.White, 0, 0, 1024, 1024); g.DrawImage(bm, 0, 0); bm1.Save($"{baseName}.png", ImageFormat.Png); xmlDocument.Save($"{baseName}.svg"); xmlDocument = new XmlDocument(); BallisticXmlSerializer serializer = new BallisticXmlSerializer(xmlDocument); xmlDocument.AppendChild(serializer.Serialize(reticle)); xmlDocument.Save($"{baseName}.reticle"); xmlDocument = new XmlDocument(); xmlDocument.Load($"{baseName}.reticle"); BallisticXmlDeserializer deserializer = new BallisticXmlDeserializer(); _ = deserializer.Deserialize <ReticleDefinition>(xmlDocument.DocumentElement); }
private static void Draw(ReticleDefinition reticle, string baseName, TrajectoryPoint[] trajectory) { const double svgWidth = 10; double svgHeight = Math.Round(reticle.Size.Y / reticle.Size.X * svgWidth, 2); var canvas = SvgCanvasFactory.Create("reticle", $"{svgWidth}in", $"{svgHeight}in"); var controller = new ReticleDrawController(reticle, canvas); controller.DrawReticle(); controller.DrawBulletDropCompensator(trajectory, DistanceUnit.Yard.New(100), false, DistanceUnit.Yard, "black"); var svg = SvgCanvasFactory.ToSvg(canvas); const int pngWidth = 1024; int pngHeight = (int)((reticle.Size.Y / reticle.Size.X) * pngWidth); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(svg); var svgDocument = SvgDocument.Open(xmlDocument); var bm = svgDocument.Draw(pngWidth, pngHeight); var bm1 = new Bitmap(pngWidth, pngHeight); Graphics g = Graphics.FromImage(bm1); g.FillRectangle(Brushes.White, 0, 0, pngWidth, pngHeight); g.DrawImage(bm, 0, 0); bm1.Save($"{baseName}.png", ImageFormat.Png); xmlDocument.Save($"{baseName}.svg"); xmlDocument = new XmlDocument(); BallisticXmlSerializer serializer = new BallisticXmlSerializer(xmlDocument); xmlDocument.AppendChild(serializer.Serialize(reticle)); xmlDocument.Save($"{baseName}.reticle"); }
public void ReticleElement_Rectangle( double sx0, double sy0, double sx1, double sy1, double sw, string color, bool fill, float dx0, float dy0, float dx1, float dy1, float dw) { var canvas = CreateMockCanvas(); var reticle = CreateReticle(); reticle.Elements.Add(new ReticleRectangle() { TopLeft = new ReticlePosition(sx0, sy0, AngularUnit.Mil), Size = new ReticlePosition(sx1, sy1, AngularUnit.Mil), LineWidth = AngularUnit.Mil.New(sw), Color = color, Fill = fill, }); canvas.Setup(canvas => canvas.Rectangle( It.Is <float>(f => Approximately(f, dx0)), It.Is <float>(f => Approximately(f, dy0)), It.Is <float>(f => Approximately(f, dx1)), It.Is <float>(f => Approximately(f, dy1)), It.Is <float>(f => Approximately(f, dw)), It.Is <bool>(b => b == fill), It.Is <string>(s => s == color))).Verifiable(); ReticleDrawController controller = new ReticleDrawController(reticle, canvas.Object); controller.DrawReticle(); canvas.Verify(); }
public void ReticleElement_Line_ZeroOffCenter( double sx0, double sy0, double sx1, double sy1, double sw, string color, float dx0, float dy0, float dx1, float dy1, float dw) { var canvas = CreateMockCanvas(); var reticle = CreateReticle(10, 4); reticle.Elements.Add(new ReticleLine() { Start = new ReticlePosition(sx0, sy0, AngularUnit.Mil), End = new ReticlePosition(sx1, sy1, AngularUnit.Mil), LineWidth = AngularUnit.Mil.New(sw), Color = color, }); canvas.Setup(canvas => canvas.Line( It.Is <float>(f => Approximately(f, dx0)), It.Is <float>(f => Approximately(f, dy0)), It.Is <float>(f => Approximately(f, dx1)), It.Is <float>(f => Approximately(f, dy1)), It.Is <float>(f => Approximately(f, dw)), It.Is <string>(s => s == color))).Verifiable(); ReticleDrawController controller = new ReticleDrawController(reticle, canvas.Object); controller.DrawReticle(); canvas.Verify(); }
public void ReticleElement_Text( double sx0, double sy0, double sh, string text, string color, float dx0, float dy0, float dh) { var canvas = CreateMockCanvas(); var reticle = CreateReticle(); reticle.Elements.Add(new ReticleText() { Position = new ReticlePosition(sx0, sy0, AngularUnit.Mil), TextHeight = AngularUnit.Mil.New(sh), Text = text, Color = color, }); canvas.Setup(canvas => canvas.Text( It.Is <float>(f => Approximately(f, dx0)), It.Is <float>(f => Approximately(f, dy0)), It.Is <float>(f => Approximately(f, dh)), It.Is <string>(s => s == text), It.Is <string>(s => s == color))).Verifiable(); ReticleDrawController controller = new ReticleDrawController(reticle, canvas.Object); controller.DrawReticle(); canvas.Verify(); }
private static void DrawGraphics(ReticleDefinition reticle, string baseName) { var bm1 = new Bitmap(1024, 1024); var canvas = GraphicsCanvas.FromImage(bm1, Color.White); canvas.Clear(); var controller = new ReticleDrawController(reticle, canvas); controller.DrawReticle(); bm1.Save($"{baseName}-a.png", ImageFormat.Png); }
public void ReticleElement_Path() { var canvas = CreateMockCanvas(); var reticle = CreateReticle(); var reticlePath = new ReticlePath() { Color = "red", Fill = true, }; reticlePath.Elements.Add(new ReticlePathElementMoveTo() { Position = new ReticlePosition(-2.5, 0, AngularUnit.Mil), }); reticlePath.Elements.Add(new ReticlePathElementLineTo() { Position = new ReticlePosition(0, 1, AngularUnit.Mil), }); reticlePath.Elements.Add(new ReticlePathElementLineTo() { Position = new ReticlePosition(2.5, 0, AngularUnit.Mil), }); reticlePath.Elements.Add(new ReticlePathElementArc() { Position = new ReticlePosition(-2.5, 0, AngularUnit.Mil), ClockwiseDirection = false, MajorArc = true, Radius = AngularUnit.Mil.New(2.5) }); reticle.Elements.Add(reticlePath); int order = 0; var canvasPath = new Mock <IReticleCanvasPath>(); canvasPath.Setup( path => path.MoveTo( It.Is <float>(f => Approximately(f, 2500)), It.Is <float>(f => Approximately(f, 5000)) )) .Callback(() => (order++).Should().Be(1, "Order of adding path items into path failed")) .Verifiable(); canvasPath.Setup( path => path.LineTo( It.Is <float>(f => Approximately(f, 5000)), It.Is <float>(f => Approximately(f, 4000)) )) .Callback(() => (order++).Should().Be(2, "Order of adding path items into path failed")) .Verifiable(); canvasPath.Setup( path => path.LineTo( It.Is <float>(f => Approximately(f, 7500)), It.Is <float>(f => Approximately(f, 5000)) )) .Callback(() => (order++).Should().Be(3, "Order of adding path items into path failed")) .Verifiable(); canvasPath.Setup( path => path.Arc( It.Is <float>(f => Approximately(f, 2500)), It.Is <float>(f => Approximately(f, 2500)), It.Is <float>(f => Approximately(f, 5000)), It.Is <bool>(f => f), It.Is <bool>(f => !f) )) .Callback(() => (order++).Should().Be(4, "Order of adding path items into path failed")) .Verifiable(); canvas.Setup( canvas => canvas.CreatePath()) .Returns(canvasPath.Object) .Callback(() => (order++).Should().Be(0, "Creating a patch must be first action")) .Verifiable(); canvas.Setup( canvas => canvas.Path( It.Is <IReticleCanvasPath>(path => path == canvasPath.Object), It.Is <float>(f => Approximately(f, 1)), It.Is <bool>(f => f), It.Is <string>(s => s == "red"))) .Callback(() => (order++).Should().Be(5, "Drawing path must be the last action")) .Verifiable(); ReticleDrawController controller = new ReticleDrawController(reticle, canvas.Object); controller.DrawReticle(); canvas.Verify(); canvasPath.Verify(); }