/// <summary> /// /// </summary> /// <param name="container"></param> /// <param name="renderer"></param> /// <returns></returns> private static Viewbox ToViewbox(Container container, IRenderer renderer) { var visual = new DrawingVisual(); using (var dc = visual.RenderOpen()) { renderer.Draw(dc, container, container.Properties, null); } visual.Drawing.Freeze(); var host = new VisualHost() { Width = container.Width, Height = container.Height }; host.Visuals.Add(visual); var vb = new Viewbox() { Stretch = Stretch.Uniform }; vb.Child = host; return vb; }
/// <summary> /// /// </summary> /// <param name="path"></param> /// <param name="container"></param> /// <param name="ic"></param> public void Save(string path, Container container, IImageCache ic) { if (container == null || container.Template == null) return; using (var bitmap = new Bitmap((int)container.Template.Width, (int)container.Template.Height)) { using (var ms = MakeMetafileStream(bitmap, container, ic)) { using (var fs = File.Create(path)) { ms.WriteTo(fs); } } } }
/// <summary> /// /// </summary> /// <param name="bitmap"></param> /// <param name="container"></param> /// <param name="ic"></param> /// <returns></returns> public MemoryStream MakeMetafileStream( Bitmap bitmap, Container container, IImageCache ic) { var g = default(Graphics); var mf = default(Metafile); var ms = new MemoryStream(); try { using (g = Graphics.FromImage(bitmap)) { var hdc = g.GetHdc(); mf = new Metafile(ms, hdc); g.ReleaseHdc(hdc); } using (g = Graphics.FromImage(mf)) { var r = new WinFormsRenderer(72.0 / 96.0); r.State.ImageCache = ic; g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PageUnit = GraphicsUnit.Display; if (container.Template != null) { r.Draw(g, container.Template, container.Data.Properties, null); } r.Draw(g, container, container.Data.Properties, null); r.ClearCache(isZooming: false); } } finally { if (g != null) { g.Dispose(); } if (mf != null) { mf.Dispose(); } } return ms; }
/// <summary> /// /// </summary> /// <param name="container"></param> public void Add(Container container) { if (container == null) return; container.PropertyChanged += ContainerObserver; if (container.Background != null) { container.Background.PropertyChanged += ContainerBackgroudObserver; } Verbose("Add Container: " + container.Name); if (container.Layers != null) { Add(container.Layers); } if (container.Data.Properties != null) { Add(container.Data.Properties); } container.WorkingLayer.InvalidateLayer += InvalidateLayerObserver; container.HelperLayer.InvalidateLayer += InvalidateLayerObserver; //Add(container.WorkingLayer); //Add(container.HelperLayer); }
/// <summary> /// /// </summary> /// <param name="gfx"></param> /// <param name="container"></param> private void DrawBackgroundInternal(Graphics gfx, Container container) { Brush brush = ToSolidBrush(container.Background); var rect = Rect2.Create(0, 0, container.Width, container.Height); gfx.FillRectangle( brush, _scaleToPage(rect.X), _scaleToPage(rect.Y), _scaleToPage(rect.Width), _scaleToPage(rect.Height)); brush.Dispose(); }
/// <summary> /// /// </summary> /// <param name="gfx"></param> /// <param name="container"></param> /// <param name="db"></param> /// <param name="r"></param> public void Draw(object gfx, Container container, ImmutableArray<ShapeProperty> db, Record r) { foreach (var layer in container.Layers) { if (layer.IsVisible) { Draw(gfx, layer, db, r); } } }
/// <summary> /// /// </summary> /// <param name="container"></param> /// <param name="rect"></param> /// <param name="treshold"></param> /// <returns></returns> public static ImmutableHashSet<BaseShape> HitTest(Container container, Rect2 rect, double treshold) { var builder = ImmutableHashSet.CreateBuilder<BaseShape>(); var selection = new Vector2[] { new Vector2(rect.X, rect.Y), new Vector2(rect.X + rect.Width, rect.Y), new Vector2(rect.X + rect.Width, rect.Y + rect.Height), new Vector2(rect.X, rect.Y + rect.Height) }; HitTest(container.CurrentLayer.Shapes.Reverse(), rect, selection, builder, treshold, 0, 0); return builder.ToImmutableHashSet(); }
/// <summary> /// /// </summary> /// <param name="item"></param> public void OnCopy(object item) { if (item is Container) { var container = item as Container; _containerToCopy = container; _documentToCopy = default(Document); } else if (item is Document) { var document = item as Document; _containerToCopy = default(Container); _documentToCopy = document; } else if (item is EditorContext || item == null) { OnCopy(); } }
/// <summary> /// /// </summary> /// <param name="dc"></param> /// <param name="template"></param> private static void DrawTemplateBackground(DrawingContext dc, Container template) { var brush = CreateBrush(template.Background); var rect = new Rect( 0, 0, template.Width, template.Height); DrawRectangleInternal( dc, 0.5, brush, null, false, true, ref rect); }
/// <summary> /// /// </summary> /// <param name="dc"></param> /// <param name="container"></param> /// <param name="db"></param> /// <param name="r"></param> public void Draw(object dc, Container container, ImmutableArray<Property> db, Record r) { var _dc = dc as DrawingContext; var template = container.Template; if (template != null) { DrawTemplateBackground(_dc, template); } foreach (var layer in container.Layers) { if (layer.IsVisible) { Draw(dc, layer, db, r); } } }
/// <summary> /// /// </summary> /// <param name="container"></param> public void Remove(Container container) { if (container == null) return; container.PropertyChanged -= ContainerObserver; if (container.Background != null) { container.Background.PropertyChanged -= ContainerBackgroudObserver; } Verbose("Remove Container: " + container.Name); if (container.Layers != null) { Add(container.Layers); } if (container.Properties != null) { Remove(container.Properties); } //Remove(container.WorkingLayer); }
/// <summary> /// /// </summary> /// <param name="project"></param> /// <param name="container"></param> private void CreateGrid(Project project, Container container) { var style = project .StyleLibraries.FirstOrDefault(g => g.Name == "Template") .Styles.FirstOrDefault(s => s.Name == "Grid"); var layer = container.Layers.FirstOrDefault(); var builder = layer.Shapes.ToBuilder(); var grid = XRectangle.Create( 0, 0, container.Width, container.Height, style, project.Options.PointShape); grid.IsStroked = false; grid.IsFilled = false; grid.IsGrid = true; grid.OffsetX = 30.0; grid.OffsetY = 30.0; grid.CellWidth = 30.0; grid.CellHeight = 30.0; grid.State.Flags &= ~ShapeStateFlags.Printable; builder.Add(grid); layer.Shapes = builder.ToImmutable(); }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static Container Create(string name = "Container", double width = 840, double height = 600) { var c = new Container() { Name = name, Width = width, Height = height, Background = ArgbColor.Create(0x00, 0xFF, 0xFF, 0xFF), Properties = ImmutableArray.Create<ShapeProperty>(), Layers = ImmutableArray.Create<Layer>() }; var builder = c.Layers.ToBuilder(); builder.Add(Layer.Create("Layer1", c)); builder.Add(Layer.Create("Layer2", c)); builder.Add(Layer.Create("Layer3", c)); builder.Add(Layer.Create("Layer4", c)); c.Layers = builder.ToImmutable(); c.CurrentLayer = c.Layers.FirstOrDefault(); c.WorkingLayer = Layer.Create("Working", c); c.HelperLayer = Layer.Create("Helper", c); return c; }
/// <summary> /// /// </summary> /// <param name="gfx"></param> /// <param name="container"></param> /// <param name="db"></param> /// <param name="r"></param> public void Draw(object gfx, Container container, ImmutableArray<Property> db, Record r) { // NOTE: Template background is drawn in drawable control. //var template = container.Template; //if (template != null) //{ // var _gfx = gfx as Graphics; // DrawTemplateBackgroundInternal(_gfx, template); //} foreach (var layer in container.Layers) { if (layer.IsVisible) { Draw(gfx, layer, db, r); } } }
/// <summary> /// /// </summary> /// <param name="dc"></param> /// <param name="container"></param> private void DrawBackgroundInternal(DrawingContext dc, Container container) { Brush brush = ToSolidBrush(container.Background); var rect = new Rect(0, 0, container.Width, container.Height); dc.FillRectangle(brush, rect); // TODO: brush.Dispose(); }
/// <summary> /// /// </summary> /// <param name="container"></param> /// <param name="ic"></param> public void SetClipboard(Container container, IImageCache ic) { try { if (container == null || container.Template == null) return; using (var bitmap = new Bitmap((int)container.Template.Width, (int)container.Template.Height)) { using (var ms = MakeMetafileStream(bitmap, container, ic)) { var data = new WPF.DataObject(); data.SetData(WPF.DataFormats.EnhancedMetafile, ms); WPF.Clipboard.SetDataObject(data, true); } } } catch (Exception ex) { Debug.Print(ex.Message); Debug.Print(ex.StackTrace); } }
/// <summary> /// /// </summary> /// <param name="container"></param> /// <returns></returns> public Container Clone(Container container) { try { var template = container.Template; var json = _serializer.Serialize(container); if (!string.IsNullOrEmpty(json)) { var clone = _serializer.Deserialize<Container>(json); if (clone != null) { var shapes = clone.Layers.SelectMany(l => l.Shapes); TryToRestoreStyles(shapes); TryToRestoreRecords(shapes); clone.Template = template; return clone; } } } catch (Exception ex) { if (_editor.Log != null) { _editor.Log.LogError("{0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace); } } return null; }
/// <summary> /// Creates a new <see cref="Container"/> instance. /// </summary> /// <param name="name"></param> /// <param name="isTemplate"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static Container Create( string name = "Container", bool isTemplate = false, double width = 840, double height = 600) { var container = new Container() { Name = name, Data = new Data() { Bindings = ImmutableArray.Create<Binding>(), Properties = ImmutableArray.Create<Property>() }, Layers = ImmutableArray.Create<Layer>(), IsTemplate = isTemplate }; if (isTemplate) { container.Background = ArgbColor.Create(0x00, 0xFF, 0xFF, 0xFF); container.Width = width; container.Height = height; } var builder = container.Layers.ToBuilder(); builder.Add(Layer.Create("Layer1", container)); builder.Add(Layer.Create("Layer2", container)); builder.Add(Layer.Create("Layer3", container)); builder.Add(Layer.Create("Layer4", container)); container.Layers = builder.ToImmutable(); container.CurrentLayer = container.Layers.FirstOrDefault(); container.WorkingLayer = Layer.Create("Working", container); container.HelperLayer = Layer.Create("Helper", container); return container; }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="owner"></param> /// <param name="isVisible"></param> /// <returns></returns> public static Layer Create(string name, Container owner, bool isVisible = true) { return new Layer() { Name = name, Owner = owner, IsVisible = isVisible, Shapes = ImmutableArray.Create<BaseShape>() }; }
/// <summary> /// /// </summary> /// <param name="container"></param> /// <param name="p"></param> /// <param name="treshold"></param> /// <returns></returns> public static BaseShape HitTest(Container container, Vector2 p, double treshold) { var result = HitTest(container.CurrentLayer.Shapes.Reverse(), p, treshold, 0, 0); if (result != null) { return result; } return null; }