@orbat-mapper/tactical-draw 0.3.2 → 0.3.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.
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
@@ -4453,6 +4487,36 @@ function runEdit(opts) {
4453
4487
  dirty = true;
4454
4488
  emitChange(previousRendered, previousPoints, previousOptions, previousStyle, previousNonGeometryDirty, previousSizeAnchor, currentRendered, previousTextAmplifiers, previousAmplifierPlacements, previousProperties, "programmatic", nextOptions ? "Change size mode" : "Change size anchor");
4455
4489
  }
4490
+ /** Restore all ground-sized dimensions in one atomic, undoable mutation. */
4491
+ function resetGroundSizes() {
4492
+ if (ctx.settled || sizeAnchor !== "ground") return;
4493
+ const nextOptions = resetGroundSizeOptions({
4494
+ kind: measure.kind,
4495
+ options: workingOptions
4496
+ }, adapter);
4497
+ if (!nextOptions) return;
4498
+ const previousPoints = workingPoints.map((p) => [...p]);
4499
+ const previousOptions = { ...workingOptions ?? {} };
4500
+ if (jsonEqual(nextOptions, previousOptions)) return;
4501
+ const previousStyle = currentStyle;
4502
+ const previousNonGeometryDirty = nonGeometryDirty;
4503
+ const previousTextAmplifiers = workingTextAmplifiers;
4504
+ const previousAmplifierPlacements = snapshotAmplifierPlacements();
4505
+ const previousProperties = snapshotProperties();
4506
+ const previousRendered = rebuildRendered();
4507
+ replaceWorkingOptions(nextOptions);
4508
+ let currentRendered;
4509
+ try {
4510
+ currentRendered = rebuildRendered();
4511
+ } catch (err) {
4512
+ replaceWorkingOptions(previousOptions);
4513
+ throw err;
4514
+ }
4515
+ renderWorkingGraphicAndHandles();
4516
+ nonGeometryDirty = true;
4517
+ dirty = true;
4518
+ emitChange(previousRendered, previousPoints, previousOptions, previousStyle, previousNonGeometryDirty, sizeAnchor, currentRendered, previousTextAmplifiers, previousAmplifierPlacements, previousProperties, "programmatic", "Reset sizes for current zoom");
4519
+ }
4456
4520
  function modesEqual(a, b) {
4457
4521
  if (a.length !== b.length) return false;
4458
4522
  for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
@@ -4535,6 +4599,9 @@ function runEdit(opts) {
4535
4599
  get sizeAnchor() {
4536
4600
  return sizeAnchor;
4537
4601
  },
4602
+ get canResetGroundSizes() {
4603
+ return !ctx.settled && sizeAnchor === "ground" && canResetGroundSizes(measure, adapter);
4604
+ },
4538
4605
  close() {
4539
4606
  return ctx.commit(rebuildRendered);
4540
4607
  },
@@ -4589,6 +4656,9 @@ function runEdit(opts) {
4589
4656
  setSizeAnchor(next, options) {
4590
4657
  setSizeAnchor(next, options);
4591
4658
  },
4659
+ resetGroundSizes() {
4660
+ resetGroundSizes();
4661
+ },
4592
4662
  onChange(listener) {
4593
4663
  if (ctx.settled) return () => {};
4594
4664
  return changeEmitter.register(listener);
@@ -8106,7 +8176,8 @@ const HATCH_PATTERNS = [
8106
8176
  "mine-antipersonnel-directional",
8107
8177
  "mine-antitank",
8108
8178
  "mine-antitank-antihandling",
8109
- "mine-wide-area-antitank"
8179
+ "mine-wide-area-antitank",
8180
+ "mine-cluster"
8110
8181
  ];
8111
8182
  /** Tile edge length, CSS px, before `pixelRatio` scaling. Kept a power of two. */
8112
8183
  const BASE_TILE_SIZE = 32;
@@ -8237,100 +8308,170 @@ function segmentCoverage(x, y, ax, ay, bx, by, width) {
8237
8308
  const t = length2 > 0 ? Math.min(1, Math.max(0, ((x - ax) * dx + (y - ay) * dy) / length2)) : 0;
8238
8309
  return Math.min(1, Math.max(0, width / 2 + .5 - Math.hypot(x - (ax + t * dx), y - (ay + t * dy))));
8239
8310
  }
8311
+ function cubicMinePoint(t, p0, p1, p2, p3) {
8312
+ const u = 1 - t;
8313
+ const u2 = u * u;
8314
+ const t2 = t * t;
8315
+ 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]];
8316
+ }
8317
+ /**
8318
+ * Raster segments for milsymbol's dashed 36×20-unit mine-cluster outline. The
8319
+ * same curve is emitted as vector geometry by `minefield.ts`'s
8320
+ * `mineClusterGlyph` in `@orbat-mapper/control-measures` (in meters, y up
8321
+ * rather than tile units, y down) — keep the control points and dash cadence in
8322
+ * step so the area fill and the point glyph read alike.
8323
+ */
8324
+ function mineClusterSegments() {
8325
+ const scale = 1 / 3;
8326
+ const point = (x, y) => [16 + x * scale, 16 + y * scale];
8327
+ const right = point(18, 10);
8328
+ const left = point(-18, 10);
8329
+ const top = point(0, -10);
8330
+ const outline = [right, left];
8331
+ for (let index = 1; index <= 16; index++) outline.push(cubicMinePoint(index / 16, left, point(-18, 0), point(-15, -10), top));
8332
+ for (let index = 1; index <= 16; index++) outline.push(cubicMinePoint(index / 16, top, point(15, -10), point(18, 0), right));
8333
+ const segments = [];
8334
+ let drawing = true;
8335
+ let remaining = 7.5 * scale;
8336
+ for (let index = 0; index < outline.length - 1; index++) {
8337
+ const a = outline[index];
8338
+ const b = outline[index + 1];
8339
+ const dx = b[0] - a[0];
8340
+ const dy = b[1] - a[1];
8341
+ const length = Math.hypot(dx, dy);
8342
+ let walked = 0;
8343
+ while (walked < length - 1e-9) {
8344
+ const step = Math.min(remaining, length - walked);
8345
+ if (drawing) {
8346
+ const t0 = walked / length;
8347
+ const t1 = (walked + step) / length;
8348
+ segments.push([
8349
+ a[0] + dx * t0,
8350
+ a[1] + dy * t0,
8351
+ a[0] + dx * t1,
8352
+ a[1] + dy * t1
8353
+ ]);
8354
+ }
8355
+ walked += step;
8356
+ remaining -= step;
8357
+ if (remaining <= 1e-9) {
8358
+ drawing = !drawing;
8359
+ remaining = (drawing ? 7.5 : 5) * scale;
8360
+ }
8361
+ }
8362
+ }
8363
+ return segments;
8364
+ }
8240
8365
  const MINE_GLYPHS = {
8241
8366
  "mine-unspecified": {
8242
- filled: false,
8367
+ body: "ring",
8243
8368
  segments: []
8244
8369
  },
8245
8370
  "mine-antipersonnel": {
8246
- filled: true,
8371
+ body: "disc",
8247
8372
  segments: [[
8248
- 13,
8249
- 12,
8250
- 9,
8251
- 7
8373
+ 12.867,
8374
+ 12.1,
8375
+ 9.333,
8376
+ 7.667
8252
8377
  ], [
8253
- 19,
8254
- 12,
8255
- 23,
8256
- 7
8378
+ 19.133,
8379
+ 12.1,
8380
+ 22.667,
8381
+ 7.667
8257
8382
  ]]
8258
8383
  },
8259
8384
  "mine-antipersonnel-directional": {
8260
- filled: true,
8385
+ body: "disc",
8261
8386
  segments: [
8262
8387
  [
8263
- 13,
8264
- 12,
8265
- 9,
8266
- 7
8388
+ 12.867,
8389
+ 12.1,
8390
+ 9.333,
8391
+ 7.667
8267
8392
  ],
8268
8393
  [
8269
- 19,
8270
- 12,
8271
- 23,
8272
- 7
8394
+ 19.133,
8395
+ 12.1,
8396
+ 22.667,
8397
+ 7.667
8273
8398
  ],
8274
8399
  [
8275
- 20,
8400
+ 21,
8276
8401
  16,
8277
- 27,
8402
+ 22.1,
8278
8403
  16
8404
+ ],
8405
+ [
8406
+ 23.667,
8407
+ 15.333,
8408
+ 24.333,
8409
+ 15.9
8410
+ ],
8411
+ [
8412
+ 24.333,
8413
+ 15.9,
8414
+ 23.667,
8415
+ 16.667
8416
+ ],
8417
+ [
8418
+ 23.667,
8419
+ 16.667,
8420
+ 23.667,
8421
+ 15.333
8279
8422
  ]
8280
- ],
8281
- arrow: [
8282
- 29,
8283
- 16,
8284
- 26,
8285
- 2.4
8286
8423
  ]
8287
8424
  },
8288
8425
  "mine-antitank": {
8289
- filled: true,
8426
+ body: "disc",
8290
8427
  segments: []
8291
8428
  },
8292
8429
  "mine-antitank-antihandling": {
8293
- filled: true,
8430
+ body: "disc",
8294
8431
  segments: [[
8295
8432
  16,
8296
- 20,
8433
+ 21,
8297
8434
  16,
8298
- 27
8435
+ 25.333
8299
8436
  ], [
8300
8437
  16,
8301
- 27,
8302
- 19,
8303
- 24
8438
+ 25.333,
8439
+ 18,
8440
+ 23.333
8304
8441
  ]]
8305
8442
  },
8306
8443
  "mine-wide-area-antitank": {
8307
- filled: true,
8444
+ body: "disc",
8308
8445
  segments: [
8309
8446
  [
8310
- 10,
8311
- 22,
8447
+ 9.333,
8448
+ 21,
8312
8449
  12,
8313
- 25
8450
+ 24.333
8314
8451
  ],
8315
8452
  [
8316
8453
  12,
8317
- 25,
8318
- 14,
8319
- 21
8454
+ 24.333,
8455
+ 13.833,
8456
+ 20.667
8320
8457
  ],
8321
8458
  [
8322
- 18,
8323
- 21,
8459
+ 18.167,
8460
+ 20.667,
8324
8461
  20,
8325
- 25
8462
+ 24.333
8326
8463
  ],
8327
8464
  [
8328
8465
  20,
8329
- 25,
8330
- 22,
8331
- 22
8466
+ 24.333,
8467
+ 22.667,
8468
+ 21
8332
8469
  ]
8333
8470
  ]
8471
+ },
8472
+ "mine-cluster": {
8473
+ body: "none",
8474
+ segments: mineClusterSegments()
8334
8475
  }
8335
8476
  };
8336
8477
  function minePatternCoverage(x, y, pattern, scale) {
@@ -8340,15 +8481,8 @@ function minePatternCoverage(x, y, pattern, scale) {
8340
8481
  const radius = 5 * scale;
8341
8482
  const lineWidth = Math.max(1, scale);
8342
8483
  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)));
8484
+ 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
8485
  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
8486
  return coverage;
8353
8487
  }
8354
8488
  /**
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.3.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.5.0"
42
+ "@orbat-mapper/control-measures": "^0.5.1"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "vitest": ">=4.0.0"