@blorkfield/overlay-core 0.8.0 → 0.8.2

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.cts CHANGED
@@ -669,12 +669,6 @@ declare class OverlayScene {
669
669
  * Get all unique tags currently in use by objects in the scene.
670
670
  */
671
671
  getAllTags(): string[];
672
- /**
673
- * Set the mouse position for follow behavior.
674
- * This overrides the browser mouse position for the 'follow' and 'follow-mouse' tags.
675
- * @deprecated Use setFollowTarget('mouse', x, y) instead
676
- */
677
- setMousePosition(x: number, y: number): void;
678
672
  /**
679
673
  * Set a follow target position. Objects with 'follow-{key}' tag will
680
674
  * automatically move toward this target each frame.
@@ -693,6 +687,23 @@ declare class OverlayScene {
693
687
  * @returns Array of follow target keys
694
688
  */
695
689
  getFollowTargetKeys(): string[];
690
+ /**
691
+ * Programmatically grab an object at the current mouse position.
692
+ * Uses the externally set mouse position (via setFollowTarget('mouse', x, y))
693
+ * or the native canvas mouse position if no external position is set.
694
+ * Only objects with the 'grabable' tag can be grabbed.
695
+ * @returns The ID of the grabbed object, or null if no grabable object at position
696
+ */
697
+ startGrab(): string | null;
698
+ /**
699
+ * Release any currently grabbed object.
700
+ */
701
+ endGrab(): void;
702
+ /**
703
+ * Get the ID of the currently grabbed object.
704
+ * @returns The ID of the grabbed object, or null if nothing is grabbed
705
+ */
706
+ getGrabbedObject(): string | null;
696
707
  /**
697
708
  * Apply a force to an object.
698
709
  * @param objectId - The ID of the object
package/dist/index.d.ts CHANGED
@@ -669,12 +669,6 @@ declare class OverlayScene {
669
669
  * Get all unique tags currently in use by objects in the scene.
670
670
  */
671
671
  getAllTags(): string[];
672
- /**
673
- * Set the mouse position for follow behavior.
674
- * This overrides the browser mouse position for the 'follow' and 'follow-mouse' tags.
675
- * @deprecated Use setFollowTarget('mouse', x, y) instead
676
- */
677
- setMousePosition(x: number, y: number): void;
678
672
  /**
679
673
  * Set a follow target position. Objects with 'follow-{key}' tag will
680
674
  * automatically move toward this target each frame.
@@ -693,6 +687,23 @@ declare class OverlayScene {
693
687
  * @returns Array of follow target keys
694
688
  */
695
689
  getFollowTargetKeys(): string[];
690
+ /**
691
+ * Programmatically grab an object at the current mouse position.
692
+ * Uses the externally set mouse position (via setFollowTarget('mouse', x, y))
693
+ * or the native canvas mouse position if no external position is set.
694
+ * Only objects with the 'grabable' tag can be grabbed.
695
+ * @returns The ID of the grabbed object, or null if no grabable object at position
696
+ */
697
+ startGrab(): string | null;
698
+ /**
699
+ * Release any currently grabbed object.
700
+ */
701
+ endGrab(): void;
702
+ /**
703
+ * Get the ID of the currently grabbed object.
704
+ * @returns The ID of the grabbed object, or null if nothing is grabbed
705
+ */
706
+ getGrabbedObject(): string | null;
696
707
  /**
697
708
  * Apply a force to an object.
698
709
  * @param objectId - The ID of the object
package/dist/index.js CHANGED
@@ -2282,14 +2282,6 @@ var OverlayScene = class {
2282
2282
  }
2283
2283
  return Array.from(tagsSet).sort();
2284
2284
  }
2285
- /**
2286
- * Set the mouse position for follow behavior.
2287
- * This overrides the browser mouse position for the 'follow' and 'follow-mouse' tags.
2288
- * @deprecated Use setFollowTarget('mouse', x, y) instead
2289
- */
2290
- setMousePosition(x, y) {
2291
- this.setFollowTarget("mouse", x, y);
2292
- }
2293
2285
  /**
2294
2286
  * Set a follow target position. Objects with 'follow-{key}' tag will
2295
2287
  * automatically move toward this target each frame.
@@ -2299,6 +2291,12 @@ var OverlayScene = class {
2299
2291
  */
2300
2292
  setFollowTarget(key, x, y) {
2301
2293
  this.followTargets.set(key, { x, y });
2294
+ if (key === "mouse" && this.mouse) {
2295
+ this.mouse.position.x = x;
2296
+ this.mouse.position.y = y;
2297
+ this.mouse.absolute.x = x;
2298
+ this.mouse.absolute.y = y;
2299
+ }
2302
2300
  }
2303
2301
  /**
2304
2302
  * Remove a follow target. Objects with the corresponding tag will stop following.
@@ -2314,6 +2312,52 @@ var OverlayScene = class {
2314
2312
  getFollowTargetKeys() {
2315
2313
  return Array.from(this.followTargets.keys());
2316
2314
  }
2315
+ // ==================== GRAB/DRAG METHODS ====================
2316
+ /**
2317
+ * Programmatically grab an object at the current mouse position.
2318
+ * Uses the externally set mouse position (via setFollowTarget('mouse', x, y))
2319
+ * or the native canvas mouse position if no external position is set.
2320
+ * Only objects with the 'grabable' tag can be grabbed.
2321
+ * @returns The ID of the grabbed object, or null if no grabable object at position
2322
+ */
2323
+ startGrab() {
2324
+ if (!this.mouseConstraint || !this.mouse) return null;
2325
+ const mouseTarget = this.followTargets.get("mouse");
2326
+ const position = mouseTarget ?? { x: this.mouse.position.x, y: this.mouse.position.y };
2327
+ const bodies = Matter5.Query.point(
2328
+ Matter5.Composite.allBodies(this.engine.world),
2329
+ position
2330
+ );
2331
+ for (const body of bodies) {
2332
+ const entry = this.findObjectByBody(body);
2333
+ if (entry && entry.tags.includes("grabable")) {
2334
+ this.mouseConstraint.constraint.bodyB = entry.body;
2335
+ this.mouseConstraint.constraint.pointB = {
2336
+ x: position.x - entry.body.position.x,
2337
+ y: position.y - entry.body.position.y
2338
+ };
2339
+ return entry.id;
2340
+ }
2341
+ }
2342
+ return null;
2343
+ }
2344
+ /**
2345
+ * Release any currently grabbed object.
2346
+ */
2347
+ endGrab() {
2348
+ if (this.mouseConstraint) {
2349
+ this.mouseConstraint.constraint.bodyB = null;
2350
+ }
2351
+ }
2352
+ /**
2353
+ * Get the ID of the currently grabbed object.
2354
+ * @returns The ID of the grabbed object, or null if nothing is grabbed
2355
+ */
2356
+ getGrabbedObject() {
2357
+ if (!this.mouseConstraint?.constraint.bodyB) return null;
2358
+ const entry = this.findObjectByBody(this.mouseConstraint.constraint.bodyB);
2359
+ return entry?.id ?? null;
2360
+ }
2317
2361
  // ==================== PHYSICS MANIPULATION METHODS ====================
2318
2362
  /**
2319
2363
  * Apply a force to an object.