@orbat-mapper/tactical-draw 0.3.2 → 0.4.0

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.
package/dist/index.d.mts CHANGED
@@ -719,6 +719,11 @@ interface EditSession {
719
719
  * from `EditOptions.sizeAnchor` and mutated by [[setSizeAnchor]].
720
720
  */
721
721
  readonly sizeAnchor: SizeAnchor;
722
+ /**
723
+ * Whether this ground-anchored measure has one or more sizes that can be
724
+ * restored at the adapter's current resolution.
725
+ */
726
+ readonly canResetGroundSizes: boolean;
722
727
  /**
723
728
  * Commit: returns the `GraphicSnapshot<ControlMeasure>` of the current working state
724
729
  * and resolves the outer `edit` promise with the same value (the same
@@ -757,6 +762,13 @@ interface EditSession {
757
762
  * inside.
758
763
  */
759
764
  setSizeAnchor(anchor: SizeAnchor, options?: ControlMeasure["options"]): void;
765
+ /**
766
+ * Reset every geometry and label size to its default on-screen appearance at
767
+ * the current map resolution, persisted in meters. The measure stays
768
+ * ground-anchored and the reset is one undoable programmatic mutation.
769
+ * No-op when [[canResetGroundSizes]] is false or after settlement.
770
+ */
771
+ resetGroundSizes(): void;
760
772
  /** Shallow-merge per-measure style into the live working copy. */
761
773
  setStyle(partial: ControlMeasureStyle): void;
762
774
  /**
package/dist/index.mjs CHANGED
@@ -1130,6 +1130,40 @@ function sizeOptionSpecs(kind) {
1130
1130
  return specs;
1131
1131
  }
1132
1132
  /**
1133
+ * Whether `kind` has at least one size that can be restored to its normal
1134
+ * pixel-authored appearance at the adapter's current resolution. Kept inside
1135
+ * tactical-draw with the private size-pair catalog so hosts never need to
1136
+ * infer option names or special-case captured labels.
1137
+ */
1138
+ function canResetGroundSizes(measure, adapter) {
1139
+ const mpp = adapter.getResolution();
1140
+ return mpp !== void 0 && mpp > 0 && sizeOptionSpecs(measure.kind).length > 0;
1141
+ }
1142
+ /**
1143
+ * Return the complete option record produced by resetting every known size
1144
+ * pair to its default on-screen appearance at the adapter's current
1145
+ * resolution. Pixel keys are removed and meter keys are written, so the
1146
+ * result remains ground-anchored. Unrelated options are preserved.
1147
+ *
1148
+ * Returns `undefined` when the kind has no resettable sizes or the adapter has
1149
+ * no usable resolution.
1150
+ */
1151
+ function resetGroundSizeOptions(measure, adapter) {
1152
+ const mpp = adapter.getResolution();
1153
+ const specs = sizeOptionSpecs(measure.kind);
1154
+ if (mpp === void 0 || mpp <= 0 || specs.length === 0) return void 0;
1155
+ const defaults = getDefaultOptions(measure.kind);
1156
+ const next = { ...measure.options ?? {} };
1157
+ for (const spec of specs) {
1158
+ const defaultPixels = spec === LABEL_SIZE_SPEC ? 14 : defaults[spec.pixels];
1159
+ if (typeof defaultPixels !== "number") continue;
1160
+ delete next[spec.pixels];
1161
+ next[spec.meters] = defaultPixels * mpp;
1162
+ }
1163
+ delete next.metersPerPixel;
1164
+ return next;
1165
+ }
1166
+ /**
1133
1167
  * Whether `kind` authors `spec` in pixels — its defaults declare the pixel key
1134
1168
  * with no meter counterpart. A static property of the definition, but
1135
1169
  * `getDefaultOptions` deep-clones on every call, so it is memoised: the gate
@@ -1513,6 +1547,26 @@ function snappedDrawPosition(snap, event) {
1513
1547
  originalEvent: event.originalEvent
1514
1548
  })?.coordinate ?? event.coordinate;
1515
1549
  }
1550
+ /** Read Shift through engine event wrappers without assuming a DOM event type. */
1551
+ function shiftKeyPressed(originalEvent) {
1552
+ let event = originalEvent;
1553
+ for (let depth = 0; depth < 4; depth += 1) {
1554
+ if (typeof event !== "object" || event === null) return false;
1555
+ if ("shiftKey" in event && event.shiftKey === true) return true;
1556
+ if (!("originalEvent" in event)) return false;
1557
+ event = event.originalEvent;
1558
+ }
1559
+ return false;
1560
+ }
1561
+ /**
1562
+ * Apply a rule's angular constraint to `points` when the gesture asks for it
1563
+ * (Shift held) and the rule defines one. Returns `points` unchanged otherwise,
1564
+ * so callers can use the result unconditionally.
1565
+ */
1566
+ function constrainedDrawPoints(rule, points, activePointIndex, originalEvent) {
1567
+ if (!rule?.constrainAngles || !shiftKeyPressed(originalEvent)) return points;
1568
+ return rule.constrainAngles(points, activePointIndex);
1569
+ }
1516
1570
  const DEFAULT_TOLERANCE_PIXELS = 12;
1517
1571
  function graphicCandidates(graphics) {
1518
1572
  const candidates = [];
@@ -1958,7 +2012,9 @@ function runFixedLengthDraw(options) {
1958
2012
  ctx.commit(buildSnapshot);
1959
2013
  return;
1960
2014
  }
2015
+ const activePointIndex = committed.length;
1961
2016
  committed.push(position);
2017
+ committed.splice(0, committed.length, ...constrainedDrawPoints(rule, committed, activePointIndex, event.originalEvent));
1962
2018
  rubberBand = void 0;
1963
2019
  if (committed.length >= required) {
1964
2020
  ctx.commit(buildSnapshot);
@@ -1975,8 +2031,9 @@ function runFixedLengthDraw(options) {
1975
2031
  if (ctx.settled) return;
1976
2032
  if (committed.length === 0) return;
1977
2033
  rubberBand = snappedDrawPosition(snap, event);
2034
+ const points = constrainedDrawPoints(rule, [...committed, rubberBand], committed.length, event.originalEvent);
2035
+ rubberBand = points[committed.length] ?? rubberBand;
1978
2036
  renderPreview();
1979
- const points = [...committed, rubberBand];
1980
2037
  const phase = transientStarted ? "move" : "start";
1981
2038
  transientStarted = true;
1982
2039
  transientEmitter.emit({
@@ -2258,7 +2315,7 @@ function runVariableLengthDraw(options) {
2258
2315
  const idx = insertionIndex(committed.length);
2259
2316
  committed.splice(idx, 0, [...pos]);
2260
2317
  if (rule) {
2261
- const derived = rule.derive(committed);
2318
+ const derived = rule.derive(constrainedDrawPoints(rule, committed, idx, event.originalEvent));
2262
2319
  committed.splice(0, committed.length, ...derived);
2263
2320
  }
2264
2321
  committed.splice(0, committed.length, ...roundControlPoints(committed));
@@ -2278,10 +2335,12 @@ function runVariableLengthDraw(options) {
2278
2335
  if (ctx.settled) return;
2279
2336
  if (committed.length === 0) return;
2280
2337
  rubberBand = snappedDrawPosition(snap, event);
2281
- renderPreview();
2282
2338
  const idx = insertionIndex(committed.length);
2283
- const points = [...committed];
2284
- points.splice(idx, 0, rubberBand);
2339
+ const spine = [...committed];
2340
+ spine.splice(idx, 0, rubberBand);
2341
+ const points = constrainedDrawPoints(rule, spine, idx, event.originalEvent);
2342
+ rubberBand = points[idx] ?? rubberBand;
2343
+ renderPreview();
2285
2344
  const phase = transientStarted ? "move" : "start";
2286
2345
  transientStarted = true;
2287
2346
  transientEmitter.emit({
@@ -2890,7 +2949,7 @@ function begun(kind, initialBase, move) {
2890
2949
  drag: {
2891
2950
  kind,
2892
2951
  move,
2893
- end: (pixel) => move(pixel).points
2952
+ end: (pixel, modifiers) => move(pixel, modifiers).points
2894
2953
  },
2895
2954
  initialPoints: clonePoints(initialBase)
2896
2955
  };
@@ -2907,14 +2966,17 @@ function beginHandleDrag(args) {
2907
2966
  const { hit, points, projection, rule } = args;
2908
2967
  const { fromPixel } = projection;
2909
2968
  const preDragPoints = clonePoints(points);
2910
- const snap = (next, activePointIndex) => rule ? rule.transform({
2911
- previous: preDragPoints,
2912
- next,
2913
- activePointIndex
2914
- }) : next;
2969
+ const snap = (next, activePointIndex, modifiers) => {
2970
+ const transformed = rule ? rule.transform({
2971
+ previous: preDragPoints,
2972
+ next,
2973
+ activePointIndex
2974
+ }) : next;
2975
+ return modifiers?.constrainAngles && rule?.constrainAngles ? rule.constrainAngles(transformed, activePointIndex) : transformed;
2976
+ };
2915
2977
  const dragOnePoint = (base, activeIndex) => {
2916
- const move = (pixel) => {
2917
- return { points: snap(base.map((p, i) => i === activeIndex ? fromPixel(pixel) : [...p]), activeIndex) };
2978
+ const move = (pixel, modifiers) => {
2979
+ return { points: snap(base.map((p, i) => i === activeIndex ? fromPixel(pixel) : [...p]), activeIndex, modifiers) };
2918
2980
  };
2919
2981
  return move;
2920
2982
  };
@@ -4453,6 +4515,36 @@ function runEdit(opts) {
4453
4515
  dirty = true;
4454
4516
  emitChange(previousRendered, previousPoints, previousOptions, previousStyle, previousNonGeometryDirty, previousSizeAnchor, currentRendered, previousTextAmplifiers, previousAmplifierPlacements, previousProperties, "programmatic", nextOptions ? "Change size mode" : "Change size anchor");
4455
4517
  }
4518
+ /** Restore all ground-sized dimensions in one atomic, undoable mutation. */
4519
+ function resetGroundSizes() {
4520
+ if (ctx.settled || sizeAnchor !== "ground") return;
4521
+ const nextOptions = resetGroundSizeOptions({
4522
+ kind: measure.kind,
4523
+ options: workingOptions
4524
+ }, adapter);
4525
+ if (!nextOptions) return;
4526
+ const previousPoints = workingPoints.map((p) => [...p]);
4527
+ const previousOptions = { ...workingOptions ?? {} };
4528
+ if (jsonEqual(nextOptions, previousOptions)) return;
4529
+ const previousStyle = currentStyle;
4530
+ const previousNonGeometryDirty = nonGeometryDirty;
4531
+ const previousTextAmplifiers = workingTextAmplifiers;
4532
+ const previousAmplifierPlacements = snapshotAmplifierPlacements();
4533
+ const previousProperties = snapshotProperties();
4534
+ const previousRendered = rebuildRendered();
4535
+ replaceWorkingOptions(nextOptions);
4536
+ let currentRendered;
4537
+ try {
4538
+ currentRendered = rebuildRendered();
4539
+ } catch (err) {
4540
+ replaceWorkingOptions(previousOptions);
4541
+ throw err;
4542
+ }
4543
+ renderWorkingGraphicAndHandles();
4544
+ nonGeometryDirty = true;
4545
+ dirty = true;
4546
+ emitChange(previousRendered, previousPoints, previousOptions, previousStyle, previousNonGeometryDirty, sizeAnchor, currentRendered, previousTextAmplifiers, previousAmplifierPlacements, previousProperties, "programmatic", "Reset sizes for current zoom");
4547
+ }
4456
4548
  function modesEqual(a, b) {
4457
4549
  if (a.length !== b.length) return false;
4458
4550
  for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
@@ -4535,6 +4627,9 @@ function runEdit(opts) {
4535
4627
  get sizeAnchor() {
4536
4628
  return sizeAnchor;
4537
4629
  },
4630
+ get canResetGroundSizes() {
4631
+ return !ctx.settled && sizeAnchor === "ground" && canResetGroundSizes(measure, adapter);
4632
+ },
4538
4633
  close() {
4539
4634
  return ctx.commit(rebuildRendered);
4540
4635
  },
@@ -4589,6 +4684,9 @@ function runEdit(opts) {
4589
4684
  setSizeAnchor(next, options) {
4590
4685
  setSizeAnchor(next, options);
4591
4686
  },
4687
+ resetGroundSizes() {
4688
+ resetGroundSizes();
4689
+ },
4592
4690
  onChange(listener) {
4593
4691
  if (ctx.settled) return () => {};
4594
4692
  return changeEmitter.register(listener);
@@ -4770,7 +4868,7 @@ function runEdit(opts) {
4770
4868
  function paintDragSnapshot(pixel, info) {
4771
4869
  if (!activeDrag) return;
4772
4870
  const resolvedPixel = snappedPixel(pixel, info, "reshape", activeDrag.controlPointIndex);
4773
- const snapshot = activeDrag.fsm.move(resolvedPixel);
4871
+ const snapshot = activeDrag.fsm.move(resolvedPixel, { constrainAngles: info?.shiftKey });
4774
4872
  workingPoints.splice(0, workingPoints.length, ...snapshot.points);
4775
4873
  renderWorkingGraphic();
4776
4874
  renderHandlesLayer();
@@ -4871,7 +4969,7 @@ function runEdit(opts) {
4871
4969
  const { fsm, preDragPoints, controlPointIndex } = activeDrag;
4872
4970
  activeDrag = null;
4873
4971
  driver.setDragPanEnabled(true);
4874
- const next = fsm.end(snappedPixel(pixel, info, "reshape", controlPointIndex));
4972
+ const next = fsm.end(snappedPixel(pixel, info, "reshape", controlPointIndex), { constrainAngles: info?.shiftKey });
4875
4973
  emitTransient("end", next, controlPointIndex);
4876
4974
  replaceWorkingPoints(preDragPoints);
4877
4975
  commitGesture(next);
@@ -8106,7 +8204,8 @@ const HATCH_PATTERNS = [
8106
8204
  "mine-antipersonnel-directional",
8107
8205
  "mine-antitank",
8108
8206
  "mine-antitank-antihandling",
8109
- "mine-wide-area-antitank"
8207
+ "mine-wide-area-antitank",
8208
+ "mine-cluster"
8110
8209
  ];
8111
8210
  /** Tile edge length, CSS px, before `pixelRatio` scaling. Kept a power of two. */
8112
8211
  const BASE_TILE_SIZE = 32;
@@ -8237,100 +8336,170 @@ function segmentCoverage(x, y, ax, ay, bx, by, width) {
8237
8336
  const t = length2 > 0 ? Math.min(1, Math.max(0, ((x - ax) * dx + (y - ay) * dy) / length2)) : 0;
8238
8337
  return Math.min(1, Math.max(0, width / 2 + .5 - Math.hypot(x - (ax + t * dx), y - (ay + t * dy))));
8239
8338
  }
8339
+ function cubicMinePoint(t, p0, p1, p2, p3) {
8340
+ const u = 1 - t;
8341
+ const u2 = u * u;
8342
+ const t2 = t * t;
8343
+ return [u2 * u * p0[0] + 3 * u2 * t * p1[0] + 3 * u * t2 * p2[0] + t2 * t * p3[0], u2 * u * p0[1] + 3 * u2 * t * p1[1] + 3 * u * t2 * p2[1] + t2 * t * p3[1]];
8344
+ }
8345
+ /**
8346
+ * Raster segments for milsymbol's dashed 36×20-unit mine-cluster outline. The
8347
+ * same curve is emitted as vector geometry by `minefield.ts`'s
8348
+ * `mineClusterGlyph` in `@orbat-mapper/control-measures` (in meters, y up
8349
+ * rather than tile units, y down) — keep the control points and dash cadence in
8350
+ * step so the area fill and the point glyph read alike.
8351
+ */
8352
+ function mineClusterSegments() {
8353
+ const scale = 1 / 3;
8354
+ const point = (x, y) => [16 + x * scale, 16 + y * scale];
8355
+ const right = point(18, 10);
8356
+ const left = point(-18, 10);
8357
+ const top = point(0, -10);
8358
+ const outline = [right, left];
8359
+ for (let index = 1; index <= 16; index++) outline.push(cubicMinePoint(index / 16, left, point(-18, 0), point(-15, -10), top));
8360
+ for (let index = 1; index <= 16; index++) outline.push(cubicMinePoint(index / 16, top, point(15, -10), point(18, 0), right));
8361
+ const segments = [];
8362
+ let drawing = true;
8363
+ let remaining = 7.5 * scale;
8364
+ for (let index = 0; index < outline.length - 1; index++) {
8365
+ const a = outline[index];
8366
+ const b = outline[index + 1];
8367
+ const dx = b[0] - a[0];
8368
+ const dy = b[1] - a[1];
8369
+ const length = Math.hypot(dx, dy);
8370
+ let walked = 0;
8371
+ while (walked < length - 1e-9) {
8372
+ const step = Math.min(remaining, length - walked);
8373
+ if (drawing) {
8374
+ const t0 = walked / length;
8375
+ const t1 = (walked + step) / length;
8376
+ segments.push([
8377
+ a[0] + dx * t0,
8378
+ a[1] + dy * t0,
8379
+ a[0] + dx * t1,
8380
+ a[1] + dy * t1
8381
+ ]);
8382
+ }
8383
+ walked += step;
8384
+ remaining -= step;
8385
+ if (remaining <= 1e-9) {
8386
+ drawing = !drawing;
8387
+ remaining = (drawing ? 7.5 : 5) * scale;
8388
+ }
8389
+ }
8390
+ }
8391
+ return segments;
8392
+ }
8240
8393
  const MINE_GLYPHS = {
8241
8394
  "mine-unspecified": {
8242
- filled: false,
8395
+ body: "ring",
8243
8396
  segments: []
8244
8397
  },
8245
8398
  "mine-antipersonnel": {
8246
- filled: true,
8399
+ body: "disc",
8247
8400
  segments: [[
8248
- 13,
8249
- 12,
8250
- 9,
8251
- 7
8401
+ 12.867,
8402
+ 12.1,
8403
+ 9.333,
8404
+ 7.667
8252
8405
  ], [
8253
- 19,
8254
- 12,
8255
- 23,
8256
- 7
8406
+ 19.133,
8407
+ 12.1,
8408
+ 22.667,
8409
+ 7.667
8257
8410
  ]]
8258
8411
  },
8259
8412
  "mine-antipersonnel-directional": {
8260
- filled: true,
8413
+ body: "disc",
8261
8414
  segments: [
8262
8415
  [
8263
- 13,
8264
- 12,
8265
- 9,
8266
- 7
8416
+ 12.867,
8417
+ 12.1,
8418
+ 9.333,
8419
+ 7.667
8267
8420
  ],
8268
8421
  [
8269
- 19,
8270
- 12,
8271
- 23,
8272
- 7
8422
+ 19.133,
8423
+ 12.1,
8424
+ 22.667,
8425
+ 7.667
8273
8426
  ],
8274
8427
  [
8275
- 20,
8428
+ 21,
8276
8429
  16,
8277
- 27,
8430
+ 22.1,
8278
8431
  16
8432
+ ],
8433
+ [
8434
+ 23.667,
8435
+ 15.333,
8436
+ 24.333,
8437
+ 15.9
8438
+ ],
8439
+ [
8440
+ 24.333,
8441
+ 15.9,
8442
+ 23.667,
8443
+ 16.667
8444
+ ],
8445
+ [
8446
+ 23.667,
8447
+ 16.667,
8448
+ 23.667,
8449
+ 15.333
8279
8450
  ]
8280
- ],
8281
- arrow: [
8282
- 29,
8283
- 16,
8284
- 26,
8285
- 2.4
8286
8451
  ]
8287
8452
  },
8288
8453
  "mine-antitank": {
8289
- filled: true,
8454
+ body: "disc",
8290
8455
  segments: []
8291
8456
  },
8292
8457
  "mine-antitank-antihandling": {
8293
- filled: true,
8458
+ body: "disc",
8294
8459
  segments: [[
8295
8460
  16,
8296
- 20,
8461
+ 21,
8297
8462
  16,
8298
- 27
8463
+ 25.333
8299
8464
  ], [
8300
8465
  16,
8301
- 27,
8302
- 19,
8303
- 24
8466
+ 25.333,
8467
+ 18,
8468
+ 23.333
8304
8469
  ]]
8305
8470
  },
8306
8471
  "mine-wide-area-antitank": {
8307
- filled: true,
8472
+ body: "disc",
8308
8473
  segments: [
8309
8474
  [
8310
- 10,
8311
- 22,
8475
+ 9.333,
8476
+ 21,
8312
8477
  12,
8313
- 25
8478
+ 24.333
8314
8479
  ],
8315
8480
  [
8316
8481
  12,
8317
- 25,
8318
- 14,
8319
- 21
8482
+ 24.333,
8483
+ 13.833,
8484
+ 20.667
8320
8485
  ],
8321
8486
  [
8322
- 18,
8323
- 21,
8487
+ 18.167,
8488
+ 20.667,
8324
8489
  20,
8325
- 25
8490
+ 24.333
8326
8491
  ],
8327
8492
  [
8328
8493
  20,
8329
- 25,
8330
- 22,
8331
- 22
8494
+ 24.333,
8495
+ 22.667,
8496
+ 21
8332
8497
  ]
8333
8498
  ]
8499
+ },
8500
+ "mine-cluster": {
8501
+ body: "none",
8502
+ segments: mineClusterSegments()
8334
8503
  }
8335
8504
  };
8336
8505
  function minePatternCoverage(x, y, pattern, scale) {
@@ -8340,15 +8509,8 @@ function minePatternCoverage(x, y, pattern, scale) {
8340
8509
  const radius = 5 * scale;
8341
8510
  const lineWidth = Math.max(1, scale);
8342
8511
  const distance = Math.hypot(x - cx, y - cy);
8343
- let coverage = glyph.filled ? Math.min(1, Math.max(0, radius + .5 - distance)) : Math.min(1, Math.max(0, lineWidth / 2 + .5 - Math.abs(distance - radius)));
8512
+ let coverage = glyph.body === "none" ? 0 : glyph.body === "disc" ? Math.min(1, Math.max(0, radius + .5 - distance)) : Math.min(1, Math.max(0, lineWidth / 2 + .5 - Math.abs(distance - radius)));
8344
8513
  for (const [ax, ay, bx, by] of glyph.segments) coverage = Math.max(coverage, segmentCoverage(x, y, ax * scale, ay * scale, bx * scale, by * scale, lineWidth));
8345
- if (glyph.arrow) {
8346
- const [tipX, tipY, baseX, halfHeight] = glyph.arrow;
8347
- const px = x / scale;
8348
- const py = y / scale;
8349
- const slope = halfHeight / (tipX - baseX);
8350
- if (px >= baseX && px <= tipX && Math.abs(py - tipY) <= (tipX - px) * slope) coverage = 1;
8351
- }
8352
8514
  return coverage;
8353
8515
  }
8354
8516
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orbat-mapper/tactical-draw",
3
- "version": "0.3.2",
3
+ "version": "0.4.0",
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.5.0"
42
+ "@orbat-mapper/control-measures": "^0.6.0"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "vitest": ">=4.0.0"