Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966: simplified class names

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