1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Windows;
|
---|
4 | using System.Windows.Forms;
|
---|
5 | using System.Windows.Input;
|
---|
6 | using System.Windows.Media;
|
---|
7 | using System.Windows.Media.Media3D;
|
---|
8 | using HeuristicLab.Encodings.PackingEncoding.PackingPlan;
|
---|
9 | using HeuristicLab.Problems.BinPacking.Dimensions;
|
---|
10 | using HeuristicLab.Problems.BinPacking.PackingBin;
|
---|
11 | using HeuristicLab.Problems.BinPacking.PackingItem;
|
---|
12 | using Point = System.Windows.Point;
|
---|
13 | using Size = System.Windows.Size;
|
---|
14 | using UserControl = System.Windows.Controls.UserControl;
|
---|
15 |
|
---|
16 | namespace HeuristicLab.Problems.BinPacking.Views {
|
---|
17 | public partial class Container2DView : UserControl {
|
---|
18 | private int selectedItemKey;
|
---|
19 | private Size renderSize;
|
---|
20 | private BinPacking<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem> packing;
|
---|
21 | public BinPacking<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem> Packing {
|
---|
22 | get { return packing; }
|
---|
23 | set {
|
---|
24 | if (packing != value) {
|
---|
25 | this.packing = value;
|
---|
26 | ClearSelection(); // also updates visualization
|
---|
27 | }
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | public Container2DView() {
|
---|
32 | InitializeComponent();
|
---|
33 | }
|
---|
34 |
|
---|
35 |
|
---|
36 | protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo) {
|
---|
37 | base.OnRenderSizeChanged(sizeInfo);
|
---|
38 | var s = Math.Min(sizeInfo.NewSize.Height, sizeInfo.NewSize.Width);
|
---|
39 | renderSize = new Size(s, s);
|
---|
40 | InvalidateVisual();
|
---|
41 | }
|
---|
42 |
|
---|
43 | // similar implementation to Container3DView (even though item selection is not supported for 2d packings TODO)
|
---|
44 | public void SelectItem(int itemKey) {
|
---|
45 | // selection of an item should make all other items semi-transparent
|
---|
46 | selectedItemKey = itemKey;
|
---|
47 | InvalidateVisual();
|
---|
48 | }
|
---|
49 | public void ClearSelection() {
|
---|
50 | // remove all transparency
|
---|
51 | selectedItemKey = -1;
|
---|
52 | InvalidateVisual();
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected override void OnRender(DrawingContext drawingContext) {
|
---|
56 | base.OnRender(drawingContext);
|
---|
57 | if (packing == null) return;
|
---|
58 | // the container should fill the whole size
|
---|
59 | var scalingX = renderSize.Width / Packing.BinMeasures.Width;
|
---|
60 | var scalingY = renderSize.Height / Packing.BinMeasures.Width;
|
---|
61 | // draw container
|
---|
62 | drawingContext.DrawRectangle(Brushes.LightGray, new Pen(Brushes.Black, 1), new Rect(new Point(0, 0), renderSize));
|
---|
63 |
|
---|
64 | var selectedBrush = Brushes.MediumSeaGreen;
|
---|
65 | var unselectedBrush = selectedItemKey < 0 ? selectedBrush : Brushes.DarkGray;
|
---|
66 |
|
---|
67 | foreach (var t in Packing.ItemMeasures) {
|
---|
68 | var key = t.Key;
|
---|
69 | var item = t.Value;
|
---|
70 | var pos = Packing.ItemPositions[key];
|
---|
71 |
|
---|
72 | var scaledPos = new Point(pos.X * scalingX, pos.Y * scalingY);
|
---|
73 |
|
---|
74 | var scaledSize = pos.Rotated ?
|
---|
75 | new Size(item.Height * scalingX, item.Width * scalingY) :
|
---|
76 | new Size(item.Width * scalingX, item.Height * scalingY);
|
---|
77 |
|
---|
78 | var brush = key == selectedItemKey ? selectedBrush : unselectedBrush;
|
---|
79 | drawingContext.DrawRectangle(brush, new Pen(Brushes.Black, 1), new Rect(scaledPos, scaledSize));
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|