Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.Views/3.3/Container2DView.xaml.cs @ 14045

Last change on this file since 14045 was 14045, checked in by gkronber, 8 years ago

#1966: removed types for *PackingBin because PackingBins and PackingShapes have the same capabilities

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