diff --git a/CutList/Controls/BinLayoutView.cs b/CutList/Controls/BinLayoutView.cs index d3ab0a5..be5b1d7 100644 --- a/CutList/Controls/BinLayoutView.cs +++ b/CutList/Controls/BinLayoutView.cs @@ -10,6 +10,7 @@ namespace CutList.Controls public Bin Bin { get; set; } private const int BorderPixels = 15; + private const int BorderPixelsX2 = BorderPixels * 2; private const int BinHeightPixels = 100; private readonly HatchBrush hBrush = new HatchBrush(HatchStyle.DiagonalCross, Color.Pink, Color.Transparent); @@ -19,64 +20,57 @@ namespace CutList.Controls SetStyle(ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true); } + private static readonly StringFormat StringFormatCentered = new StringFormat + { + Alignment = StringAlignment.Center, + LineAlignment = StringAlignment.Center + }; + protected override void OnPaint(PaintEventArgs e) { - //base.OnPaint(e); - if (Bin == null) return; - var displayWidth = Width - BorderPixels * 2.0f; - var maxHeight = Height - BorderPixels * 2.0f; + var rect = GetBinRectangle(); - if (displayWidth <= 0) return; - if (maxHeight <= 0) return; + e.Graphics.FillRectangle(hBrush, rect); - var displayHeight = maxHeight < BinHeightPixels ? maxHeight : BinHeightPixels; + var id = 1; + var scale = rect.Width / (float)Bin.Length; + var x = rect.X; + + for (int i = 0; i < Bin.Items.Count; i++) + { + var item = Bin.Items[i]; + + var w = item.Length / Bin.Length * rect.Width; + var r = new RectangleF(x, rect.Y, (float)w, rect.Height); + + e.Graphics.FillRectangle(Brushes.White, r); + e.Graphics.DrawRectangle(Pens.Blue, r.X, r.Y, r.Width, r.Height); + e.Graphics.DrawString(id++.ToString(), Font, Brushes.Blue, r, StringFormatCentered); + + x += (float)item.Length * scale; + + if (i < Bin.Items.Count - 1) + x += (float)Bin.Spacing * scale; + } + + e.Graphics.DrawRectangle(Pens.Blue, rect.X, rect.Y, rect.Width, rect.Height); + } + + private RectangleF GetBinRectangle() + { + var displayWidth = Width - BorderPixelsX2; + var heightMinusBorder = Height - BorderPixelsX2; + var displayHeight = Height > BinHeightPixels ? BinHeightPixels : Height; var x = (Width - displayWidth) / 2.0f; var y = (Height - displayHeight) / 2.0f; var rect = new RectangleF(x, y, displayWidth, displayHeight); - var id = 1; - var scale = displayWidth / (float)Bin.Length; - - var beginningScrap = rect.Left; - - if (Bin != null) - { - for (int i = 0; i < Bin.Items.Count; i++) - { - var item = Bin.Items[i]; - - var w = item.Length / Bin.Length * displayWidth; - var r = new RectangleF(x, y, (float)w, displayHeight); - - e.Graphics.DrawRectangle(Pens.Blue, r.X, r.Y, r.Width, r.Height); - e.Graphics.DrawString(id++.ToString(), Font, Brushes.Blue, r, new StringFormat - { - Alignment = StringAlignment.Center, - LineAlignment = StringAlignment.Center - }); - - x += (float)item.Length * scale; - - if (i < Bin.Items.Count - 1) - x += (float)Bin.Spacing; - - if (r.Right > beginningScrap) - beginningScrap = r.Right; - } - } - - // add 1 pixel so the last items' border will not be drawn over. - beginningScrap += 1; - - var scrapRect = new RectangleF(beginningScrap, y, rect.Right - beginningScrap, displayHeight); - - e.Graphics.FillRectangle(hBrush, scrapRect); - e.Graphics.DrawRectangle(Pens.Black, rect.X, rect.Y, rect.Width, rect.Height); + return rect; } } } \ No newline at end of file