#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using SharpDX.Direct2D1; using Factory2D1 = SharpDX.Direct2D1.Factory; using FactoryType2D1 = SharpDX.Direct2D1.FactoryType; using System.Windows.Forms; using SharpDX.DXGI; using SharpDX; using Colors = SharpDX.Color; using SharpDX.DirectWrite; namespace PackingPlanVisualizations { public partial class PackingPlan2D : UserControl { private WindowRenderTarget wndRender; private readonly Factory2D1 factory = new Factory2D1(FactoryType2D1.SingleThreaded); private readonly SharpDX.DirectWrite.Factory dwFactory = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared); private readonly RenderTargetProperties rndTargProperties = new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)); private HwndRenderTargetProperties hwndProperties; private SolidColorBrush containerBrush; private SolidColorBrush containerFillingBrush; private SolidColorBrush itemBrush; private SolidColorBrush itemFillingBrush; private SolidColorBrush labelBrush; private TextFormat textFormat; private CenteredContainer2D container; public PackingPlan2D() { InitializeComponent(); this.SetStyle(ControlStyles.ResizeRedraw, true); } private void InitializeRenderTarget() { hwndProperties.Hwnd = this.Handle; hwndProperties.PixelSize = new Size2(this.Width, this.Height); hwndProperties.PresentOptions = PresentOptions.None; textFormat = new TextFormat(dwFactory, "arial", 14); wndRender = new WindowRenderTarget(factory, rndTargProperties, hwndProperties); wndRender.AntialiasMode = AntialiasMode.Aliased; containerBrush = new SolidColorBrush(wndRender, Colors.Black); containerFillingBrush = new SolidColorBrush(wndRender, Colors.DarkGray); itemBrush = new SolidColorBrush(wndRender, Colors.Black); itemFillingBrush = new SolidColorBrush(wndRender, new Color4(Color3.White, 0.5f)); labelBrush = new SolidColorBrush(wndRender, Colors.Black); } public void InitializeContainer(float width, float height) { container = new CenteredContainer2D(new Vector2(Math.Max(this.Width - 12, 1), Math.Max(this.Height - 12, 1)), new Vector2(width, height)); } public void AddItemToContainer(float width, float height, float x, float y, string label) { container.AddItem(new Vector2(width, height), new Vector2(x, y), label); } private void PackingPlan2D_Load(object sender, EventArgs e) { InitializeRenderTarget(); } private void PackingPlan2D_Paint(object sender, PaintEventArgs e) { wndRender.BeginDraw(); var controlColor = Colors.White; wndRender.Clear(new Colors(controlColor.R, controlColor.G, controlColor.B)); if (container != null) { container.UpdateContainer(new Vector2(this.Width, this.Height)); wndRender.FillRectangle(container.GetContainerData(), containerFillingBrush); foreach (var item in container.GetItemRectangles()) { wndRender.DrawRectangle(item.rectangle, itemBrush, 2f); wndRender.FillRectangle(item.rectangle, itemFillingBrush); wndRender.DrawText(item.label, textFormat, item.rectangle, labelBrush); } wndRender.DrawRectangle(container.GetContainerData(), containerBrush, 2.0f); } wndRender.Flush(); wndRender.EndDraw(); } private void PackingPlan2D_Resize(object sender, EventArgs e) { if (wndRender != null) wndRender.Resize(new Size2(this.Width, this.Height)); } } }