@orbat-mapper/tactical-draw 0.36.1 → 0.36.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.mjs +84 -31
  2. package/package.json +2 -2
package/dist/index.mjs CHANGED
@@ -361,13 +361,13 @@ function evaluateGeometry(feature, presentation, context) {
361
361
  function evaluatePolygon(pixel, rings, presentation, context, pickInterior) {
362
362
  const { tolerance, toPixel, viewport } = context;
363
363
  const projectedRings = rings.map((ring) => ring.map(toPixel));
364
- const distance = hasVisibleBoundary(presentation) ? minimum(projectedRings.map((points) => minVisibleSegmentDistance(pixel, points, viewport))) : null;
364
+ const visiblyFilled = (presentation.fill?.opacity ?? 0) > 0;
365
+ const distance = hasVisibleBoundary(presentation) || visiblyFilled ? minimum(projectedRings.map((points) => minVisibleSegmentDistance(pixel, points, viewport))) : null;
365
366
  if (distance !== null && distance <= tolerance) return {
366
367
  kind: "polygon-boundary",
367
368
  distance
368
369
  };
369
370
  if (!(projectedRings.length > 0 && pointInProjectedRing(pixel, projectedRings[0]) && !projectedRings.slice(1).some((points) => pointInProjectedRing(pixel, points)))) return null;
370
- const visiblyFilled = (presentation.fill?.opacity ?? 0) > 0;
371
371
  const inwardReach = context.pointerType === "touch" ? 36 : 24;
372
372
  if (pickInterior || visiblyFilled || distance !== null && distance <= inwardReach) return {
373
373
  kind: "polygon-interior",
@@ -6184,26 +6184,17 @@ function normalizeAngle(angle) {
6184
6184
  * that no adapter is drawing.
6185
6185
  */
6186
6186
  function fitLabelTransformBox(input) {
6187
- const { text, rotation, textAnchor, textOffsetEms, textSizePixels, textSizeMeters, anchor, resolution, bearing, unclamped, textMaxSizePixels, labelSizeBand } = input;
6187
+ const { text, textAnchor, anchor } = input;
6188
6188
  if (!text) return null;
6189
- const angle = normalizeAngle(Math.PI - (rotation ?? 0) - (bearing ?? 0));
6190
- let h = 16;
6191
- if (textSizePixels !== void 0 || textSizeMeters !== void 0 && resolution !== void 0 && resolution > 0) {
6192
- const appearance = resolveTextAppearance({
6193
- textSizePixels,
6194
- textSizeMeters,
6195
- textMaxSizePixels: unclamped ? textMaxSizePixels ?? Infinity : textMaxSizePixels,
6196
- ...labelSizeBand
6197
- }, resolution ?? 0);
6198
- if (!appearance) return null;
6199
- h = Math.max(1, appearance.sizePixels);
6200
- }
6189
+ const angle = labelScreenAngle(input);
6190
+ const h = labelTextSizePixels(input);
6191
+ if (h === null) return null;
6201
6192
  const lines = text.split("\n");
6202
6193
  const width = lines.reduce((max, line) => Math.max(max, line.length), 0) * CHAR_WIDTH_FACTOR * h;
6203
6194
  const height = lines.length * LINE_HEIGHT_FACTOR * h;
6204
6195
  const halfWidth = width / 2 + 4;
6205
6196
  const halfHeight = height / 2 + 4;
6206
- const [offsetRight, offsetDown] = textOffsetEms ?? [0, 0];
6197
+ const [offsetRight, offsetDown] = input.textOffsetEms ?? [0, 0];
6207
6198
  const localX = (textAnchor === "start" ? width / 2 : textAnchor === "end" ? -width / 2 : 0) + offsetRight * h;
6208
6199
  const localY = offsetDown * h;
6209
6200
  let center = anchor;
@@ -6218,6 +6209,38 @@ function fitLabelTransformBox(input) {
6218
6209
  angle
6219
6210
  };
6220
6211
  }
6212
+ /** The on-screen angle a label's text is drawn at, radians, y-down. */
6213
+ function labelScreenAngle({ rotation, bearing }) {
6214
+ return normalizeAngle(Math.PI - (rotation ?? 0) - (bearing ?? 0));
6215
+ }
6216
+ /**
6217
+ * The text's on-screen offset from its point, pixels: `textOffsetEms` times
6218
+ * the rendered size, turned with the text. `[0, 0]` without an offset,
6219
+ * `null` while the label is hidden.
6220
+ */
6221
+ function labelTextOffsetPixels(input) {
6222
+ const [offsetRight, offsetDown] = input.textOffsetEms ?? [0, 0];
6223
+ if (offsetRight === 0 && offsetDown === 0) return [0, 0];
6224
+ const h = labelTextSizePixels(input);
6225
+ if (h === null) return null;
6226
+ return rotate(offsetRight * h, offsetDown * h, labelScreenAngle(input));
6227
+ }
6228
+ /** The label's rendered text size in pixels, or `null` while it is hidden. */
6229
+ function labelTextSizePixels(input) {
6230
+ const { textSizePixels, textSizeMeters, resolution, unclamped, textMaxSizePixels, labelSizeBand } = input;
6231
+ let h = 16;
6232
+ if (textSizePixels !== void 0 || textSizeMeters !== void 0 && resolution !== void 0 && resolution > 0) {
6233
+ const appearance = resolveTextAppearance({
6234
+ textSizePixels,
6235
+ textSizeMeters,
6236
+ textMaxSizePixels: unclamped ? textMaxSizePixels ?? Infinity : textMaxSizePixels,
6237
+ ...labelSizeBand
6238
+ }, resolution ?? 0);
6239
+ if (!appearance) return null;
6240
+ h = Math.max(1, appearance.sizePixels);
6241
+ }
6242
+ return h;
6243
+ }
6221
6244
  /**
6222
6245
  * Read a label `Point` feature's rendered props (`text`, `rotation`,
6223
6246
  * `textAnchor`, `textSizePixels`, `textSizeMeters`) into a
@@ -6228,8 +6251,12 @@ function fitLabelTransformBox(input) {
6228
6251
  * click-away check matches what the edit controller would pick.
6229
6252
  */
6230
6253
  function fitLabelBoxFromFeature(feature, anchorPixel, view) {
6254
+ return fitLabelTransformBox(labelBoxInputFromFeature(feature, anchorPixel, view));
6255
+ }
6256
+ /** A label feature's rendered props as a {@link fitLabelTransformBox} input. */
6257
+ function labelBoxInputFromFeature(feature, anchorPixel, view) {
6231
6258
  const props = feature.properties ?? {};
6232
- return fitLabelTransformBox({
6259
+ return {
6233
6260
  text: typeof props.text === "string" ? props.text : void 0,
6234
6261
  rotation: typeof props.rotation === "number" ? props.rotation : void 0,
6235
6262
  textAnchor: props.textAnchor === "start" || props.textAnchor === "end" ? props.textAnchor : void 0,
@@ -6242,7 +6269,7 @@ function fitLabelBoxFromFeature(feature, anchorPixel, view) {
6242
6269
  bearing: view.bearing,
6243
6270
  unclamped: view.unclamped,
6244
6271
  labelSizeBand: view.labelSizeBand
6245
- });
6272
+ };
6246
6273
  }
6247
6274
  /**
6248
6275
  * Fit a `TransformBox` around a rendered **Text generic graphic** feature — the
@@ -6413,17 +6440,18 @@ function createLabelPlacement(deps) {
6413
6440
  if (typeof key !== "string" || feature.geometry?.type !== "Point") continue;
6414
6441
  const projected = projection.toPixel(feature.geometry.coordinates);
6415
6442
  if (!projected) continue;
6443
+ const origin = placedTextPixel(feature, projected) ?? projected;
6416
6444
  for (const handle of [false, true]) {
6417
- const targetY = projected[1] - (handle ? LABEL_HANDLE_OFFSET_PX : 0);
6418
- if (Math.hypot(projected[0] - pixel[0], targetY - pixel[1]) <= tolerance) return {
6445
+ const targetY = origin[1] - (handle ? LABEL_HANDLE_OFFSET_PX : 0);
6446
+ if (Math.hypot(origin[0] - pixel[0], targetY - pixel[1]) <= tolerance) return {
6419
6447
  key,
6420
- anchorPixel: projected
6448
+ anchorPixel: origin
6421
6449
  };
6422
6450
  }
6423
6451
  const box = fitLabelBoxFromFeature(feature, projected, view());
6424
6452
  if (box && pointInBox(box, pixel)) return {
6425
6453
  key,
6426
- anchorPixel: projected
6454
+ anchorPixel: origin
6427
6455
  };
6428
6456
  }
6429
6457
  return null;
@@ -6454,6 +6482,20 @@ function createLabelPlacement(deps) {
6454
6482
  pixel
6455
6483
  };
6456
6484
  }
6485
+ /**
6486
+ * Where the text of a label whose offset lapses once placed
6487
+ * (`textOffsetUntilPlaced`, ADR-0068) is drawn, for a label whose point is
6488
+ * at `pixel`; `undefined` for any other label, or one already placed. Once
6489
+ * placed it sits on its stored point without the offset, so a gesture
6490
+ * moves and stores this pixel instead of the point, and the text stays
6491
+ * under the pointer when the offset lapses.
6492
+ */
6493
+ function placedTextPixel(feature, pixel) {
6494
+ if (feature.properties?.textOffsetUntilPlaced !== true) return void 0;
6495
+ const offset = labelTextOffsetPixels(labelBoxInputFromFeature(feature, pixel, view()));
6496
+ if (!offset || offset[0] === 0 && offset[1] === 0) return void 0;
6497
+ return [pixel[0] + offset[0], pixel[1] + offset[1]];
6498
+ }
6457
6499
  /** The screen-space angle (radians, y-down) from `anchor` to `pixel`, as used by the label rotate drag's pointer-angle math. */
6458
6500
  function pointerAngleAround(pixel, anchor) {
6459
6501
  return Math.atan2(pixel[1] - anchor[1], pixel[0] - anchor[0]);
@@ -6473,8 +6515,9 @@ function createLabelPlacement(deps) {
6473
6515
  const key = feature.properties?.labelPlacementKey;
6474
6516
  if (typeof key !== "string" || feature.geometry?.type !== "Point") continue;
6475
6517
  if (key === selectedLabelKey) continue;
6476
- const pixel = projection.toPixel(feature.geometry.coordinates);
6477
- if (!pixel) continue;
6518
+ const projected = projection.toPixel(feature.geometry.coordinates);
6519
+ if (!projected) continue;
6520
+ const pixel = placedTextPixel(feature, projected) ?? projected;
6478
6521
  features_.push({
6479
6522
  type: "Feature",
6480
6523
  id: `${handleIdPrefix}:label-handle:${index}`,
@@ -6506,19 +6549,22 @@ function createLabelPlacement(deps) {
6506
6549
  if (labelBox) {
6507
6550
  const anchor = selectedLabelAnchor();
6508
6551
  if (anchor) {
6552
+ const placedText = placedTextPixel(anchor.feature, anchor.pixel);
6553
+ const origin = placedText ?? anchor.pixel;
6509
6554
  const gripPixel = rotateGripPixel(labelBox, 14);
6510
6555
  if (Math.hypot(gripPixel[0] - pixel[0], gripPixel[1] - pixel[1]) <= tolerance) {
6511
6556
  const props = anchor.feature.properties ?? {};
6512
6557
  activeLabelRotateDrag = {
6513
6558
  key: selectedLabelKey,
6514
- anchorPixel: anchor.pixel,
6515
- startPointerAngle: pointerAngleAround(pixel, anchor.pixel),
6559
+ anchorPixel: origin,
6560
+ position: placedText && projection.fromPixel(placedText),
6561
+ startPointerAngle: pointerAngleAround(pixel, origin),
6516
6562
  startRotation: typeof props.rotation === "number" ? props.rotation : 0,
6517
6563
  previous: snapshotAmplifierPlacements(),
6518
6564
  downPixel: pixel,
6519
6565
  text: typeof props.text === "string" ? props.text : void 0,
6520
6566
  textAnchor: props.textAnchor === "start" || props.textAnchor === "end" ? props.textAnchor : void 0,
6521
- textOffsetEms: labelTextOffsetEms(props),
6567
+ textOffsetEms: placedText ? void 0 : labelTextOffsetEms(props),
6522
6568
  textSizePixels: typeof props.textSizePixels === "number" ? props.textSizePixels : void 0,
6523
6569
  textSizeMeters: typeof props.textSizeMeters === "number" ? props.textSizeMeters : void 0,
6524
6570
  lastBox: labelBox
@@ -6528,7 +6574,7 @@ function createLabelPlacement(deps) {
6528
6574
  if (pointInBox(labelBox, pixel)) {
6529
6575
  activeAmplifierDrag = {
6530
6576
  field: selectedLabelKey,
6531
- grabOffset: [anchor.pixel[0] - pixel[0], anchor.pixel[1] - pixel[1]],
6577
+ grabOffset: [origin[0] - pixel[0], origin[1] - pixel[1]],
6532
6578
  previous: snapshotAmplifierPlacements(),
6533
6579
  downPixel: pixel
6534
6580
  };
@@ -6548,6 +6594,13 @@ function createLabelPlacement(deps) {
6548
6594
  }
6549
6595
  return false;
6550
6596
  }
6597
+ /** A rotate drag's placement: the rotation, plus its `position` when it has one. */
6598
+ function rotatedPlacement(drag, rotation) {
6599
+ return drag.position ? {
6600
+ position: drag.position,
6601
+ rotation
6602
+ } : { rotation };
6603
+ }
6551
6604
  function onMove(pixel) {
6552
6605
  if (activeLabelRotateDrag) {
6553
6606
  const drag = activeLabelRotateDrag;
@@ -6569,7 +6622,7 @@ function createLabelPlacement(deps) {
6569
6622
  if (liveBox) drag.lastBox = liveBox;
6570
6623
  return {
6571
6624
  field: drag.key,
6572
- value: { rotation }
6625
+ value: rotatedPlacement(drag, rotation)
6573
6626
  };
6574
6627
  }
6575
6628
  if (activeAmplifierDrag) {
@@ -6592,7 +6645,7 @@ function createLabelPlacement(deps) {
6592
6645
  return {
6593
6646
  kind: "rotate",
6594
6647
  key: drag.key,
6595
- rotation,
6648
+ value: rotatedPlacement(drag, rotation),
6596
6649
  previous: drag.previous
6597
6650
  };
6598
6651
  }
@@ -7922,7 +7975,7 @@ function runEdit(opts) {
7922
7975
  if (labelOutcome) {
7923
7976
  workingAmplifierPlacements = labelOutcome.previous;
7924
7977
  endDragBookkeeping();
7925
- if (labelOutcome.kind === "rotate") setAmplifierPlacements({ [labelOutcome.key]: { rotation: labelOutcome.rotation } }, "gesture", "Rotate label");
7978
+ if (labelOutcome.kind === "rotate") setAmplifierPlacements({ [labelOutcome.key]: labelOutcome.value }, "gesture", "Rotate label");
7926
7979
  else if (labelOutcome.kind === "move") setAmplifierPlacements({ [labelOutcome.field]: labelOutcome.position }, "gesture", "Move label");
7927
7980
  else {
7928
7981
  renderWorkingGraphic();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orbat-mapper/tactical-draw",
3
- "version": "0.36.1",
3
+ "version": "0.36.3",
4
4
  "description": "Engine-agnostic draw/edit core for tactical control measures (MIL-STD-2525 / APP-6). The MapAdapter ABI and bring-your-own-engine entry point for the tactical-draw package family.",
5
5
  "license": "MIT",
6
6
  "author": "Orbat Mapper",
@@ -39,7 +39,7 @@
39
39
  "access": "public"
40
40
  },
41
41
  "dependencies": {
42
- "@orbat-mapper/control-measures": "^0.47.0"
42
+ "@orbat-mapper/control-measures": "^0.47.1"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "vitest": ">=4.0.0"