1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 |
|
---|
22 | using System;
|
---|
23 | using System.Windows;
|
---|
24 | using System.Windows.Media;
|
---|
25 | using HeuristicLab.Problems.BinPacking2D;
|
---|
26 | using Point = System.Windows.Point;
|
---|
27 | using Size = System.Windows.Size;
|
---|
28 | using UserControl = System.Windows.Controls.UserControl;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.BinPacking.Views {
|
---|
31 | public partial class Container2DView : UserControl {
|
---|
32 | private int selectedItemKey;
|
---|
33 | private Size renderSize;
|
---|
34 | private BinPacking<BinPacking2D.PackingPosition, PackingShape, PackingItem> packing;
|
---|
35 | public BinPacking<BinPacking2D.PackingPosition, PackingShape, PackingItem> Packing {
|
---|
36 | get { return packing; }
|
---|
37 | set {
|
---|
38 | if (packing != value) {
|
---|
39 | this.packing = value;
|
---|
40 | ClearSelection(); // also updates visualization
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public Container2DView() {
|
---|
46 | InitializeComponent();
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo) {
|
---|
51 | base.OnRenderSizeChanged(sizeInfo);
|
---|
52 | var s = Math.Min(sizeInfo.NewSize.Height, sizeInfo.NewSize.Width);
|
---|
53 | renderSize = new Size(s, s);
|
---|
54 | InvalidateVisual();
|
---|
55 | }
|
---|
56 |
|
---|
57 | // similar implementation to Container3DView (even though item selection is not supported for 2d packings TODO)
|
---|
58 | public void SelectItem(int itemKey) {
|
---|
59 | // selection of an item should make all other items semi-transparent
|
---|
60 | selectedItemKey = itemKey;
|
---|
61 | InvalidateVisual();
|
---|
62 | }
|
---|
63 | public void ClearSelection() {
|
---|
64 | // remove all transparency
|
---|
65 | selectedItemKey = -1;
|
---|
66 | InvalidateVisual();
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected override void OnRender(DrawingContext drawingContext) {
|
---|
70 | base.OnRender(drawingContext);
|
---|
71 | if (packing == null) return;
|
---|
72 | // the container should fill the whole size
|
---|
73 | var scalingX = renderSize.Width / Packing.BinShape.Width;
|
---|
74 | var scalingY = renderSize.Height / Packing.BinShape.Width;
|
---|
75 | // draw container
|
---|
76 | drawingContext.DrawRectangle(Brushes.LightGray, new Pen(Brushes.Black, 1), new Rect(new Point(0, 0), renderSize));
|
---|
77 |
|
---|
78 | var selectedBrush = Brushes.MediumSeaGreen;
|
---|
79 | var unselectedBrush = selectedItemKey < 0 ? selectedBrush : Brushes.DarkGray;
|
---|
80 |
|
---|
81 | foreach (var t in Packing.Items) {
|
---|
82 | var key = t.Key;
|
---|
83 | var item = t.Value;
|
---|
84 | var pos = Packing.Positions[key];
|
---|
85 |
|
---|
86 | var scaledPos = new Point(pos.X * scalingX, pos.Y * scalingY);
|
---|
87 |
|
---|
88 | var scaledSize = pos.Rotated ?
|
---|
89 | new Size(item.Height * scalingX, item.Width * scalingY) :
|
---|
90 | new Size(item.Width * scalingX, item.Height * scalingY);
|
---|
91 |
|
---|
92 | var brush = key == selectedItemKey ? selectedBrush : unselectedBrush;
|
---|
93 | drawingContext.DrawRectangle(brush, new Pen(Brushes.Black, 1), new Rect(scaledPos, scaledSize));
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|