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

Last change on this file since 13161 was 13161, checked in by gkronber, 4 years ago

#1966: removed SharpDX binaries since these are available through a transport plugin now (see #2504) and also made some changes to make the code work with the latest stable release of SharpDX

File size: 5.2 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    WindowRenderTarget wndRender              = null;
36    Factory2D1 factory                        = new Factory2D1(FactoryType2D1.SingleThreaded);
37    SharpDX.DirectWrite.Factory dwFactory     = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared);
38    RenderTargetProperties rndTargProperties  = new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied));
39    HwndRenderTargetProperties hwndProperties = new HwndRenderTargetProperties();
40
41
42    #region Packing members BEGIN     
43    SolidColorBrush containerBrush;
44    SolidColorBrush containerFillingBrush;
45    SolidColorBrush itemBrush;
46    SolidColorBrush itemFillingBrush;
47    SolidColorBrush labelBrush;
48    TextFormat textFormat;
49
50    CenteredContainer2D container;
51    #endregion Packing members END
52
53
54    //Initialization-Methods
55
56    public PackingPlan2D() {
57      InitializeComponent();
58      this.SetStyle(
59        ControlStyles.ResizeRedraw, true);
60    }
61
62    private void InitializeRenderTarget() {
63      hwndProperties.Hwnd = this.Handle;
64      hwndProperties.PixelSize = new Size2(this.Width, this.Height);
65      hwndProperties.PresentOptions = PresentOptions.None;
66      textFormat = new TextFormat(dwFactory, "arial", 14);
67
68      wndRender = new WindowRenderTarget(factory, rndTargProperties, hwndProperties);
69      wndRender.AntialiasMode = AntialiasMode.Aliased;
70      containerBrush = new SolidColorBrush(wndRender, Colors.Black);
71      containerFillingBrush = new SolidColorBrush(wndRender, Colors.DarkGray);
72      itemBrush = new SolidColorBrush(wndRender, Colors.Black);
73      itemFillingBrush = new SolidColorBrush(wndRender, new Color4 (Color3.White, 0.5f));
74      labelBrush = new SolidColorBrush(wndRender, Colors.Black);
75    }
76
77
78
79
80
81    //Packing-Methods
82    public void InitializeContainer (float width, float height) {
83      container = new CenteredContainer2D(new Vector2 (this.Width, this.Height), new Vector2 (width, height));   
84    }
85
86    public void AddItemToContainer(float width, float height, float x, float y, string label) {
87      container.AddItem(new Vector2(width, height), new Vector2 (x, y), label);
88    }
89
90
91
92   
93
94
95    //Event-Listeners 
96
97    private void PackingPlan2D_Load(object sender, EventArgs e) {
98      InitializeRenderTarget();
99    }
100
101    private void PackingPlan2D_Paint(object sender, PaintEventArgs e) {
102      wndRender.BeginDraw();
103      var controlColor = Colors.White;//System.Drawing.SystemColors.Control;
104      wndRender.Clear(new Colors (controlColor.R, controlColor.G, controlColor.B));
105
106      if (container != null) {
107        container.UpdateContainer(new Vector2(this.Width, this.Height));
108        wndRender.FillRectangle(container.GetContainerData(), containerFillingBrush);
109
110        //foreach (PackingPlanVisualizations.CenteredContainer2D.PackingItem item in container.GetPackingItems()) {
111        //  SharpDX.RectangleF rect = item.GetRectangle(container.ContainerTopLeft);
112        //  wndRender.DrawRectangle(rect, itemBrush, 2f);
113        //  wndRender.FillRectangle(rect, itemFillingBrush);
114        //  wndRender.DrawText(item.GetLabel(), textFormat, rect, labelBrush);
115        //}
116
117        foreach (PackingPlanVisualizations.CenteredContainer2D.LabeledRectangle item in container.GetItemRectangles()) {
118          wndRender.DrawRectangle(item.rectangle, itemBrush, 2f);
119          wndRender.FillRectangle(item.rectangle, itemFillingBrush);
120          wndRender.DrawText(item.label, textFormat, item.rectangle, labelBrush);
121        }
122
123        wndRender.DrawRectangle(container.GetContainerData(), containerBrush, 2.0f);
124      }
125
126      wndRender.Flush();
127      wndRender.EndDraw();
128    }
129
130    private void PackingPlan2D_Resize(object sender, EventArgs e) {
131      if (wndRender != null)
132        wndRender.Resize(new Size2(this.Width, this.Height));
133      //this.Refresh();
134    }
135
136    private void PackingPlan2D_MouseClick(object sender, MouseEventArgs e) {
137      Console.WriteLine(e.X + " - " + e.Y);
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.