Files
OpenNest/OpenNest/Controls/EntityView.PartPreview.cs
T
ajandClaude Opus 5.5 3b19fda767 feat(cad): preview which part each entity separates into
Separate gave no way to see what each part would take before
splitting the file. Preview Parts, joined to the front of the
Separate button, draws every entity in its part's color and labels
each part with the name it would get.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 18:41:55 -05:00

100 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using OpenNest.Geometry;
namespace OpenNest.Controls
{
/// <summary>
/// A part's name and color in the Separate preview, shown just above <paramref name="Anchor"/>, the top
/// center of its outline's bounds, so a part sitting in another's window does not hide its label.
/// </summary>
public sealed record PartPreviewLabel(string Name, Vector Anchor, Color Color);
/// <summary>
/// What the Separate preview shows: each entity's part color by entity ID, and each part's name.
/// </summary>
public sealed record PartPreview(IReadOnlyDictionary<Guid, Color> Colors, IReadOnlyList<PartPreviewLabel> Labels);
public partial class EntityView
{
private const float PartPenWidth = 2f;
private readonly Dictionary<int, Pen> partPenCache = new Dictionary<int, Pen>();
private readonly Dictionary<int, Pen> partDisplayPenCache = new Dictionary<int, Pen>();
private readonly Font partLabelFont = new Font("Segoe UI", 8.5f, FontStyle.Bold);
private PartPreview partPreview;
/// <summary>
/// While the Separate preview is on, the parts separating would make: every entity is drawn in
/// its part's color and each part is labeled with the name it would get. Null draws normally.
/// </summary>
public PartPreview PartPreview
{
get => partPreview;
set
{
partPreview = value;
Invalidate();
}
}
private bool TryGetPartColor(Entity entity, out Color color)
{
color = default;
return partPreview != null && partPreview.Colors.TryGetValue(entity.Id, out color);
}
private Pen PartPen(Entity entity) =>
TryGetPartColor(entity, out var color) ? CachedPen(partPenCache, color, dashed: false) : null;
private Pen PartDisplayPen(Entity entity) =>
TryGetPartColor(entity, out var color) ? CachedPen(partDisplayPenCache, color, dashed: true) : null;
private static Pen CachedPen(Dictionary<int, Pen> cache, Color color, bool dashed)
{
var argb = color.ToArgb();
if (!cache.TryGetValue(argb, out var pen))
{
pen = new Pen(color, PartPenWidth);
if (dashed)
pen.DashPattern = new float[] { 6, 3 };
cache[argb] = pen;
}
return pen;
}
private void DrawPartPreviewLabels(Graphics g)
{
if (partPreview == null)
return;
var smoothing = g.SmoothingMode;
g.SmoothingMode = SmoothingMode.AntiAlias;
using var textBrush = new SolidBrush(Color.FromArgb(25, 30, 36));
foreach (var label in partPreview.Labels)
{
var size = g.MeasureString(label.Name, partLabelFont);
var at = PointWorldToGraph(label.Anchor);
var box = new RectangleF(at.X - size.Width / 2f - 3f, at.Y - size.Height - 5f,
size.Width + 6f, size.Height + 2f);
using var back = new SolidBrush(Color.FromArgb(220, label.Color));
g.FillRectangle(back, box);
g.DrawString(label.Name, partLabelFont, textBrush, box.X + 3f, box.Y + 1f);
}
g.SmoothingMode = smoothing;
}
private void DisposePartPreview()
{
foreach (var pen in partPenCache.Values)
pen.Dispose();
partPenCache.Clear();
foreach (var pen in partDisplayPenCache.Values)
pen.Dispose();
partDisplayPenCache.Clear();
partLabelFont.Dispose();
}
}
}