|
| 1 | +using System.Globalization; |
| 2 | +using DiagramForge.Models; |
| 3 | + |
| 4 | +namespace DiagramForge.Layout; |
| 5 | + |
| 6 | +public sealed partial class DefaultLayoutEngine |
| 7 | +{ |
| 8 | + private static void LayoutSnakeDiagram( |
| 9 | + Diagram diagram, |
| 10 | + Theme theme, |
| 11 | + double minW, |
| 12 | + double nodeH, |
| 13 | + double pad) |
| 14 | + { |
| 15 | + var orderedNodes = diagram.Nodes.Values |
| 16 | + .OrderBy(n => GetMetadataInt(n.Metadata, "snake:stepIndex")) |
| 17 | + .ToList(); |
| 18 | + |
| 19 | + if (orderedNodes.Count == 0) |
| 20 | + return; |
| 21 | + |
| 22 | + double fontSize = theme.FontSize; |
| 23 | + double titleOffset = !string.IsNullOrWhiteSpace(diagram.Title) ? theme.TitleFontSize * 1.4 + 12 : 0; |
| 24 | + |
| 25 | + // ── Wrap all multi-word labels to two lines for larger text ─────────── |
| 26 | + foreach (var node in orderedNodes) |
| 27 | + { |
| 28 | + string text = node.Label.Text; |
| 29 | + if (text.Contains(' ', StringComparison.Ordinal) && !text.Contains('\n', StringComparison.Ordinal)) |
| 30 | + { |
| 31 | + // Split at the space nearest the middle |
| 32 | + int mid = text.Length / 2; |
| 33 | + int bestSplit = -1; |
| 34 | + int bestDist = int.MaxValue; |
| 35 | + for (int j = 0; j < text.Length; j++) |
| 36 | + { |
| 37 | + if (text[j] == ' ' && Math.Abs(j - mid) < bestDist) |
| 38 | + { |
| 39 | + bestDist = Math.Abs(j - mid); |
| 40 | + bestSplit = j; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if (bestSplit > 0) |
| 45 | + { |
| 46 | + node.Label.Text = text[..bestSplit] + "\n" + text[(bestSplit + 1)..]; |
| 47 | + node.Label.Lines = [text[..bestSplit], text[(bestSplit + 1)..]]; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // ── Sizing ──────────────────────────────────────────────────────────── |
| 53 | + // Set a generous minimum circle diameter first, then derive font size from it. |
| 54 | + double minCircleDiameter = fontSize * 10; |
| 55 | + double circleDiameter = Math.Max(minCircleDiameter, Math.Max(minW, nodeH)); |
| 56 | + circleDiameter = Math.Max(circleDiameter, EnsureIconHeight(orderedNodes[0], circleDiameter)); |
| 57 | + |
| 58 | + // Derive label font size from circle diameter so text fills the circle. |
| 59 | + // For two-line labels the inscribed width at ~60% height is ~0.8 × diameter. |
| 60 | + double snakeFontSize = circleDiameter * 0.14; |
| 61 | + |
| 62 | + // Verify labels fit; if the widest label overflows, grow the circle |
| 63 | + double availableTextWidth = circleDiameter * 0.75; // inscribed text area |
| 64 | + double widestLine = orderedNodes.Max(node => |
| 65 | + { |
| 66 | + string t = node.Label.Text; |
| 67 | + string longest = t.Contains('\n') ? t.Split('\n').MaxBy(l => l.Length)! : t; |
| 68 | + return EstimateTextWidth(longest, snakeFontSize); |
| 69 | + }); |
| 70 | + if (widestLine > availableTextWidth) |
| 71 | + { |
| 72 | + circleDiameter *= widestLine / availableTextWidth; |
| 73 | + snakeFontSize = circleDiameter * 0.14; |
| 74 | + } |
| 75 | + |
| 76 | + // Store the snake-specific font size and icon size per node |
| 77 | + double iconSize = circleDiameter * 0.3; |
| 78 | + foreach (var node in orderedNodes) |
| 79 | + { |
| 80 | + node.Label.FontSize = snakeFontSize; |
| 81 | + node.Metadata["icon:size"] = iconSize; |
| 82 | + // Position icon snugly above the label text inside the circle. |
| 83 | + // label:centerY is at circleDiameter/2; the label then shifts down |
| 84 | + // by iconAreaHeight/2, so effective text top ≈ center + gap/2. |
| 85 | + // Place icon just above that, with a small gap. |
| 86 | + double iconGap = snakeFontSize * 0.15; |
| 87 | + double iconY = circleDiameter / 2 - iconSize - iconGap; |
| 88 | + node.Metadata["icon:y"] = iconY; |
| 89 | + } |
| 90 | + |
| 91 | + // Description text blocks — proportional to circle size for readability |
| 92 | + double descFontSize = snakeFontSize * 0.85; |
| 93 | + double descMaxWidth = circleDiameter * 2.0; |
| 94 | + double descLineHeight = descFontSize * 1.35; |
| 95 | + |
| 96 | + // Measure max description block height |
| 97 | + double maxDescHeight = 0; |
| 98 | + foreach (var node in orderedNodes) |
| 99 | + { |
| 100 | + if (node.Metadata.TryGetValue("snake:description", out var descObj) && descObj is string desc) |
| 101 | + { |
| 102 | + int lineCount = EstimateWrappedLineCount(desc, descFontSize, descMaxWidth); |
| 103 | + double blockHeight = lineCount * descLineHeight; |
| 104 | + maxDescHeight = Math.Max(maxDescHeight, blockHeight); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Gap between circle edge and description text |
| 109 | + double descGap = 12; |
| 110 | + |
| 111 | + // ── Snake curve geometry ────────────────────────────────────────────── |
| 112 | + // The snake connector wraps around each circle as a semicircular arc, |
| 113 | + // alternating above and below. The arcs tile seamlessly — each arc's |
| 114 | + // endpoint is the next arc's start point, so no horizontal segments. |
| 115 | + double arcGap = circleDiameter * 0.25; // visual gap between arc and circle edge |
| 116 | + double arcRadius = circleDiameter / 2 + arcGap; |
| 117 | + double snakeStrokeWidth = circleDiameter * 0.22; |
| 118 | + |
| 119 | + // Arcs tile directly: each circle center is 2 × arcRadius apart |
| 120 | + double hSpacing = 2 * arcRadius; |
| 121 | + |
| 122 | + // Vertical space needed for descriptions that sit above/below the circles |
| 123 | + double descSpace = maxDescHeight > 0 ? maxDescHeight + descGap : 0; |
| 124 | + |
| 125 | + // Center line Y: leave room for title, top descriptions, and arc extent |
| 126 | + double centerY = pad + titleOffset + descSpace + arcRadius + circleDiameter / 2; |
| 127 | + |
| 128 | + // ── Palette ─────────────────────────────────────────────────────────── |
| 129 | + // For the snake diagram we want vibrant, saturated colors for the circles. |
| 130 | + // Use the node palette colors but boost their saturation for this diagram type. |
| 131 | + string[] palette = theme.NodePalette is { Count: > 0 } |
| 132 | + ? [.. theme.NodePalette] |
| 133 | + : [theme.NodeFillColor]; |
| 134 | + |
| 135 | + string[] strokePalette = theme.NodeStrokePalette is { Count: > 0 } |
| 136 | + ? [.. theme.NodeStrokePalette] |
| 137 | + : palette.Select(c => ColorUtils.Darken(c, 0.18)).ToArray(); |
| 138 | + |
| 139 | + // ── Position circles ────────────────────────────────────────────────── |
| 140 | + for (int i = 0; i < orderedNodes.Count; i++) |
| 141 | + { |
| 142 | + var node = orderedNodes[i]; |
| 143 | + double cx = pad + circleDiameter / 2 + i * hSpacing; |
| 144 | + double cy = centerY; |
| 145 | + |
| 146 | + node.Shape = Shape.Circle; |
| 147 | + node.Width = circleDiameter; |
| 148 | + node.Height = circleDiameter; |
| 149 | + node.X = cx - circleDiameter / 2; |
| 150 | + node.Y = cy - circleDiameter / 2; |
| 151 | + |
| 152 | + // Assign vibrant colors from palette |
| 153 | + node.FillColor = palette[i % palette.Length]; |
| 154 | + node.StrokeColor = strokePalette[i % strokePalette.Length]; |
| 155 | + |
| 156 | + SetLabelCenter(node, circleDiameter / 2, circleDiameter / 2); |
| 157 | + } |
| 158 | + |
| 159 | + // ── Store snake path data in diagram metadata for the renderer ──────── |
| 160 | + // The connector is a series of tiled semicircular arcs wrapping around |
| 161 | + // each circle, alternating below and above. Because hSpacing = 2·arcR, |
| 162 | + // each arc's right endpoint is exactly the next arc's left endpoint. |
| 163 | + // |
| 164 | + // Even-indexed circles: arc wraps BELOW (sweep-flag=1 → clockwise) |
| 165 | + // Odd-indexed circles: arc wraps ABOVE (sweep-flag=0 → counter-clockwise) |
| 166 | + var pathSegments = new List<string>(); |
| 167 | + int n = orderedNodes.Count; |
| 168 | + |
| 169 | + // Start at the left edge of the first circle's arc |
| 170 | + double firstCx = orderedNodes[0].X + circleDiameter / 2; |
| 171 | + pathSegments.Add($"M {Fmt(firstCx - arcRadius)},{Fmt(centerY)}"); |
| 172 | + |
| 173 | + for (int i = 0; i < n; i++) |
| 174 | + { |
| 175 | + double cx = orderedNodes[i].X + circleDiameter / 2; |
| 176 | + |
| 177 | + // Semicircular arc around circle i — arcs tile with no gap |
| 178 | + int sweep = (i % 2 == 0) ? 1 : 0; |
| 179 | + pathSegments.Add( |
| 180 | + $"A {Fmt(arcRadius)} {Fmt(arcRadius)} 0 0 {sweep} {Fmt(cx + arcRadius)},{Fmt(centerY)}"); |
| 181 | + } |
| 182 | + |
| 183 | + string snakePathData = string.Join(" ", pathSegments); |
| 184 | + |
| 185 | + // Build per-segment color list and paired stop positions for the gradient. |
| 186 | + // Each circle gets a solid-color band equal to the circle's width, with |
| 187 | + // smooth gradient transitions only in the gaps between circles. |
| 188 | + // |
| 189 | + // Path spans 2·n·arcRadius. Circle i centre = (2i+1)·arcRadius. |
| 190 | + // Solid band edges: centre ± circleRadius. |
| 191 | + // ratio r = circleRadius / arcRadius (≈ 0.667 for default arcGap). |
| 192 | + // solidStart% = ((2i+1) − r) / (2n) × 100 |
| 193 | + // solidEnd% = ((2i+1) + r) / (2n) × 100 |
| 194 | + double circleRadius = circleDiameter / 2; |
| 195 | + double r = circleRadius / arcRadius; |
| 196 | + var segmentColors = new List<string>(); |
| 197 | + var segmentStops = new List<(double Start, double End)>(); |
| 198 | + for (int i = 0; i < n; i++) |
| 199 | + { |
| 200 | + segmentColors.Add(palette[i % palette.Length]); |
| 201 | + double solidStart = ((2.0 * i + 1) - r) / (2.0 * n) * 100; |
| 202 | + double solidEnd = ((2.0 * i + 1) + r) / (2.0 * n) * 100; |
| 203 | + segmentStops.Add((solidStart, solidEnd)); |
| 204 | + } |
| 205 | + |
| 206 | + diagram.Metadata["snake:pathData"] = snakePathData; |
| 207 | + diagram.Metadata["snake:strokeWidth"] = snakeStrokeWidth; |
| 208 | + diagram.Metadata["snake:segmentColors"] = segmentColors; |
| 209 | + diagram.Metadata["snake:segmentStops"] = segmentStops; |
| 210 | + diagram.Metadata["snake:nodeCount"] = n; |
| 211 | + diagram.Metadata["snake:descFontSize"] = descFontSize; |
| 212 | + |
| 213 | + // ── Store description text positions ────────────────────────────────── |
| 214 | + for (int i = 0; i < orderedNodes.Count; i++) |
| 215 | + { |
| 216 | + var node = orderedNodes[i]; |
| 217 | + if (!node.Metadata.TryGetValue("snake:description", out var descObj) || descObj is not string desc) |
| 218 | + continue; |
| 219 | + |
| 220 | + double cx = node.X + circleDiameter / 2; |
| 221 | + double nodeCy = node.Y + circleDiameter / 2; |
| 222 | + |
| 223 | + // Alternate: even indices arc below → description below; odd arc above → desc above |
| 224 | + bool descBelow = i % 2 == 0; |
| 225 | + double descY = descBelow |
| 226 | + ? nodeCy + circleDiameter / 2 + arcGap + descGap |
| 227 | + : nodeCy - circleDiameter / 2 - arcGap - descGap; |
| 228 | + |
| 229 | + node.Metadata["snake:descX"] = cx; |
| 230 | + node.Metadata["snake:descY"] = descY; |
| 231 | + node.Metadata["snake:descBelow"] = descBelow; |
| 232 | + node.Metadata["snake:descMaxWidth"] = descMaxWidth; |
| 233 | + } |
| 234 | + |
| 235 | + // ── Set canvas height to account for all elements ───────────────────── |
| 236 | + double canvasBottom = centerY + circleDiameter / 2 + arcRadius + descSpace + pad; |
| 237 | + double canvasTop = pad; |
| 238 | + double totalHeight = canvasBottom + pad; |
| 239 | + |
| 240 | + // Store so ComputeHeight can use the full extent |
| 241 | + diagram.Metadata["snake:canvasHeight"] = totalHeight; |
| 242 | + } |
| 243 | + |
| 244 | + private static int EstimateWrappedLineCount(string text, double fontSize, double maxWidth) |
| 245 | + { |
| 246 | + double charWidth = fontSize * AvgGlyphAdvanceEm; |
| 247 | + int charsPerLine = Math.Max(1, (int)(maxWidth / charWidth)); |
| 248 | + int lineCount = (int)Math.Ceiling((double)text.Length / charsPerLine); |
| 249 | + return Math.Max(1, lineCount); |
| 250 | + } |
| 251 | + |
| 252 | + private static string Fmt(double v) => v.ToString("F2", CultureInfo.InvariantCulture); |
| 253 | +} |
0 commit comments