projectNodes method

List<ProjectedNode> projectNodes({
  1. required List<InspectorNode> nodes,
  2. required Size viewport,
  3. required double layerSpacing,
  4. required double yaw,
  5. required double pitch,
  6. required int layerIndex(
    1. InspectorNode node
    ),
})

Implementation

List<ProjectedNode> projectNodes({
  required List<InspectorNode> nodes,
  required Size viewport,
  required double layerSpacing,
  required double yaw,
  required double pitch,
  required int Function(InspectorNode node) layerIndex,
}) {
  if (nodes.isEmpty || viewport.isEmpty) return const [];

  final raw = <({InspectorNode node, List<Offset> points})>[];
  for (final node in nodes) {
    final z = layerIndex(node) * layerSpacing;
    final rect = node.bounds;
    raw.add((
      node: node,
      points: [
        _project(rect.topLeft, z, yaw, pitch),
        _project(rect.topRight, z, yaw, pitch),
        _project(rect.bottomRight, z, yaw, pitch),
        _project(rect.bottomLeft, z, yaw, pitch),
      ],
    ));
  }

  final allPoints = raw.expand((entry) => entry.points);
  final minX = allPoints.map((p) => p.dx).reduce(math.min);
  final maxX = allPoints.map((p) => p.dx).reduce(math.max);
  final minY = allPoints.map((p) => p.dy).reduce(math.min);
  final maxY = allPoints.map((p) => p.dy).reduce(math.max);
  final rawWidth = math.max(1.0, maxX - minX);
  final rawHeight = math.max(1.0, maxY - minY);
  const padding = 52.0;
  final scale = math
      .min(
        (viewport.width - padding * 2) / rawWidth,
        (viewport.height - padding * 2) / rawHeight,
      )
      .clamp(0.08, 3.0);
  final offset = Offset(
    (viewport.width - rawWidth * scale) / 2 - minX * scale,
    (viewport.height - rawHeight * scale) / 2 - minY * scale,
  );

  return [
    for (final entry in raw)
      ProjectedNode(
        node: entry.node,
        points: [for (final point in entry.points) point * scale + offset],
      ),
  ];
}