Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13028 was 13028, checked in by gkronber, 9 years ago

#1966

  • added PackingPlanVisualizations plugin received from jhelm.
  • this project also contains necessary binaries of SharpDX
  • visualization in HL works now
File size: 4.6 KB
RevLine 
[13028]1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using SharpDX.Direct3D;
7using SharpDX.Direct2D1;
8using Factory2D1 = SharpDX.Direct2D1.Factory;
9using FactoryType2D1 = SharpDX.Direct2D1.FactoryType;
10using System.Text;
11using System.Windows.Forms;
12using SharpDX.DXGI;
13using SharpDX;
14using Colors = SharpDX.Color;
15using SharpDX.Windows;
16using System.Globalization;
17using SharpDX.DirectWrite;
18
19
20namespace PackingPlanVisualizations {
21  public partial class PackingPlan2D : UserControl {
22    WindowRenderTarget wndRender              = null;
23    Factory2D1 factory                        = new Factory2D1(FactoryType2D1.SingleThreaded);
24    SharpDX.DirectWrite.Factory dwFactory     = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared);
25    RenderTargetProperties rndTargProperties  = new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied));
26    HwndRenderTargetProperties hwndProperties = new HwndRenderTargetProperties();
27
28
29    #region Packing members BEGIN     
30    SolidColorBrush containerBrush;
31    SolidColorBrush containerFillingBrush;
32    SolidColorBrush itemBrush;
33    SolidColorBrush itemFillingBrush;
34    SolidColorBrush labelBrush;
35    TextFormat textFormat;
36
37    CenteredContainer2D container;
38    #endregion Packing members END
39
40
41    //Initialization-Methods
42
43    public PackingPlan2D() {
44      InitializeComponent();
45      this.SetStyle(
46        ControlStyles.ResizeRedraw, true);
47    }
48
49    private void InitializeRenderTarget() {
50      hwndProperties.Hwnd = this.Handle;
51      hwndProperties.PixelSize = new DrawingSize(this.Width, this.Height);
52      hwndProperties.PresentOptions = PresentOptions.None;
53      textFormat = new TextFormat(dwFactory, "arial", 14);
54
55      wndRender = new WindowRenderTarget(factory, rndTargProperties, hwndProperties);
56      wndRender.AntialiasMode = AntialiasMode.Aliased;
57      containerBrush = new SolidColorBrush(wndRender, Colors.Black);
58      containerFillingBrush = new SolidColorBrush(wndRender, Colors.DarkGray);
59      itemBrush = new SolidColorBrush(wndRender, Colors.Black);
60      itemFillingBrush = new SolidColorBrush(wndRender, new Color4 (Color3.White, 0.5f));
61      labelBrush = new SolidColorBrush(wndRender, Colors.Black);
62    }
63
64
65
66
67
68    //Packing-Methods
69    public void InitializeContainer (float width, float height) {
70      container = new CenteredContainer2D(new Vector2 (this.Width, this.Height), new Vector2 (width, height));   
71    }
72
73    public void AddItemToContainer(float width, float height, float x, float y, string label) {
74      container.AddItem(new Vector2(width, height), new Vector2 (x, y), label);
75    }
76
77
78
79   
80
81
82    //Event-Listeners 
83
84    private void PackingPlan2D_Load(object sender, EventArgs e) {
85      InitializeRenderTarget();
86    }
87
88    private void PackingPlan2D_Paint(object sender, PaintEventArgs e) {
89      wndRender.BeginDraw();
90      var controlColor = Colors.White;//System.Drawing.SystemColors.Control;
91      wndRender.Clear(new Colors (controlColor.R, controlColor.G, controlColor.B));
92
93      if (container != null) {
94        container.UpdateContainer(new Vector2(this.Width, this.Height));
95        wndRender.FillRectangle(container.GetContainerData(), containerFillingBrush);
96
97        //foreach (PackingPlanVisualizations.CenteredContainer2D.PackingItem item in container.GetPackingItems()) {
98        //  SharpDX.RectangleF rect = item.GetRectangle(container.ContainerTopLeft);
99        //  wndRender.DrawRectangle(rect, itemBrush, 2f);
100        //  wndRender.FillRectangle(rect, itemFillingBrush);
101        //  wndRender.DrawText(item.GetLabel(), textFormat, rect, labelBrush);
102        //}
103
104        foreach (PackingPlanVisualizations.CenteredContainer2D.LabeledRectangle item in container.GetItemRectangles()) {
105          wndRender.DrawRectangle(item.rectangle, itemBrush, 2f);
106          wndRender.FillRectangle(item.rectangle, itemFillingBrush);
107          wndRender.DrawText(item.label, textFormat, item.rectangle, labelBrush);
108        }
109
110        wndRender.DrawRectangle(container.GetContainerData(), containerBrush, 2.0f);
111      }
112
113      wndRender.Flush();
114      wndRender.EndDraw();
115    }
116
117    private void PackingPlan2D_Resize(object sender, EventArgs e) {
118      if (wndRender != null)
119        wndRender.Resize(new DrawingSize(this.Width, this.Height));
120      //this.Refresh();
121    }
122
123    private void PackingPlan2D_MouseClick(object sender, MouseEventArgs e) {
124      Console.WriteLine(e.X + " - " + e.Y);
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.