source: branches/HeuristicLab.BinPacking/PackingPlanVisualizations/2D/PackingPlan2D.cs @ 13497

Last change on this file since 13497 was 13497, checked in by gkronber, 5 years ago

#1966: fixed various problems: bugs in cloning, bugs in persistence, method names, various minor improvements of source code for readability.

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using SharpDX.Direct2D1;
24using Factory2D1 = SharpDX.Direct2D1.Factory;
25using FactoryType2D1 = SharpDX.Direct2D1.FactoryType;
26using System.Windows.Forms;
27using SharpDX.DXGI;
28using SharpDX;
29using Colors = SharpDX.Color;
30using SharpDX.DirectWrite;
31
32
33namespace PackingPlanVisualizations {
34  public partial class PackingPlan2D : UserControl {
35    private WindowRenderTarget wndRender;
36    private readonly Factory2D1 factory = new Factory2D1(FactoryType2D1.SingleThreaded);
37    private readonly SharpDX.DirectWrite.Factory dwFactory = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared);
38    private readonly RenderTargetProperties rndTargProperties = new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied));
39    private HwndRenderTargetProperties hwndProperties;
40
41
42    private SolidColorBrush containerBrush;
43    private SolidColorBrush containerFillingBrush;
44    private SolidColorBrush itemBrush;
45    private SolidColorBrush itemFillingBrush;
46    private SolidColorBrush labelBrush;
47    private TextFormat textFormat;
48
49    private CenteredContainer2D container;
50
51    public PackingPlan2D() {
52      InitializeComponent();
53      this.SetStyle(ControlStyles.ResizeRedraw, true);
54    }
55
56    private void InitializeRenderTarget() {
57      hwndProperties.Hwnd = this.Handle;
58      hwndProperties.PixelSize = new Size2(this.Width, this.Height);
59      hwndProperties.PresentOptions = PresentOptions.None;
60      textFormat = new TextFormat(dwFactory, "arial", 14);
61
62      wndRender = new WindowRenderTarget(factory, rndTargProperties, hwndProperties);
63      wndRender.AntialiasMode = AntialiasMode.Aliased;
64      containerBrush = new SolidColorBrush(wndRender, Colors.Black);
65      containerFillingBrush = new SolidColorBrush(wndRender, Colors.DarkGray);
66      itemBrush = new SolidColorBrush(wndRender, Colors.Black);
67      itemFillingBrush = new SolidColorBrush(wndRender, new Color4(Color3.White, 0.5f));
68      labelBrush = new SolidColorBrush(wndRender, Colors.Black);
69    }
70
71    public void InitializeContainer(float width, float height) {
72      container = new CenteredContainer2D(new Vector2(Math.Max(this.Width - 12, 1), Math.Max(this.Height - 12, 1)), new Vector2(width, height));
73    }
74
75    public void AddItemToContainer(float width, float height, float x, float y, string label) {
76      container.AddItem(new Vector2(width, height), new Vector2(x, y), label);
77    }
78
79    private void PackingPlan2D_Load(object sender, EventArgs e) {
80      InitializeRenderTarget();
81    }
82
83    private void PackingPlan2D_Paint(object sender, PaintEventArgs e) {
84      wndRender.BeginDraw();
85      var controlColor = Colors.White;
86      wndRender.Clear(new Colors(controlColor.R, controlColor.G, controlColor.B));
87
88      if (container != null) {
89        container.UpdateContainer(new Vector2(this.Width, this.Height));
90        wndRender.FillRectangle(container.GetContainerData(), containerFillingBrush);
91
92        foreach (var item in container.GetItemRectangles()) {
93          wndRender.DrawRectangle(item.rectangle, itemBrush, 2f);
94          wndRender.FillRectangle(item.rectangle, itemFillingBrush);
95          wndRender.DrawText(item.label, textFormat, item.rectangle, labelBrush);
96        }
97
98        wndRender.DrawRectangle(container.GetContainerData(), containerBrush, 2.0f);
99      }
100
101      wndRender.Flush();
102      wndRender.EndDraw();
103    }
104
105    private void PackingPlan2D_Resize(object sender, EventArgs e) {
106      if (wndRender != null)
107        wndRender.Resize(new Size2(this.Width, this.Height));
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.