@blorkfield/overlay-core 0.8.0 → 0.8.1

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
@@ -693,6 +693,23 @@ declare class OverlayScene {
693
693
  * @returns Array of follow target keys
694
694
  */
695
695
  getFollowTargetKeys(): string[];
696
+ /**
697
+ * Programmatically grab an object at the current mouse position.
698
+ * Uses the externally set mouse position (via setFollowTarget('mouse', x, y))
699
+ * or the native canvas mouse position if no external position is set.
700
+ * Only objects with the 'grabable' tag can be grabbed.
701
+ * @returns The ID of the grabbed object, or null if no grabable object at position
702
+ */
703
+ startGrab(): string | null;
704
+ /**
705
+ * Release any currently grabbed object.
706
+ */
707
+ endGrab(): void;
708
+ /**
709
+ * Get the ID of the currently grabbed object.
710
+ * @returns The ID of the grabbed object, or null if nothing is grabbed
711
+ */
712
+ getGrabbedObject(): string | null;
696
713
  /**
697
714
  * Apply a force to an object.
698
715
  * @param objectId - The ID of the object
package/dist/index.d.ts CHANGED
@@ -693,6 +693,23 @@ declare class OverlayScene {
693
693
  * @returns Array of follow target keys
694
694
  */
695
695
  getFollowTargetKeys(): string[];
696
+ /**
697
+ * Programmatically grab an object at the current mouse position.
698
+ * Uses the externally set mouse position (via setFollowTarget('mouse', x, y))
699
+ * or the native canvas mouse position if no external position is set.
700
+ * Only objects with the 'grabable' tag can be grabbed.
701
+ * @returns The ID of the grabbed object, or null if no grabable object at position
702
+ */
703
+ startGrab(): string | null;
704
+ /**
705
+ * Release any currently grabbed object.
706
+ */
707
+ endGrab(): void;
708
+ /**
709
+ * Get the ID of the currently grabbed object.
710
+ * @returns The ID of the grabbed object, or null if nothing is grabbed
711
+ */
712
+ getGrabbedObject(): string | null;
696
713
  /**
697
714
  * Apply a force to an object.
698
715
  * @param objectId - The ID of the object
package/dist/index.js CHANGED
@@ -2299,6 +2299,12 @@ var OverlayScene = class {
2299
2299
  */
2300
2300
  setFollowTarget(key, x, y) {
2301
2301
  this.followTargets.set(key, { x, y });
2302
+ if (key === "mouse" && this.mouse) {
2303
+ this.mouse.position.x = x;
2304
+ this.mouse.position.y = y;
2305
+ this.mouse.absolute.x = x;
2306
+ this.mouse.absolute.y = y;
2307
+ }
2302
2308
  }
2303
2309
  /**
2304
2310
  * Remove a follow target. Objects with the corresponding tag will stop following.
@@ -2314,6 +2320,52 @@ var OverlayScene = class {
2314
2320
  getFollowTargetKeys() {
2315
2321
  return Array.from(this.followTargets.keys());
2316
2322
  }
2323
+ // ==================== GRAB/DRAG METHODS ====================
2324
+ /**
2325
+ * Programmatically grab an object at the current mouse position.
2326
+ * Uses the externally set mouse position (via setFollowTarget('mouse', x, y))
2327
+ * or the native canvas mouse position if no external position is set.
2328
+ * Only objects with the 'grabable' tag can be grabbed.
2329
+ * @returns The ID of the grabbed object, or null if no grabable object at position
2330
+ */
2331
+ startGrab() {
2332
+ if (!this.mouseConstraint || !this.mouse) return null;
2333
+ const mouseTarget = this.followTargets.get("mouse");
2334
+ const position = mouseTarget ?? { x: this.mouse.position.x, y: this.mouse.position.y };
2335
+ const bodies = Matter5.Query.point(
2336
+ Matter5.Composite.allBodies(this.engine.world),
2337
+ position
2338
+ );
2339
+ for (const body of bodies) {
2340
+ const entry = this.findObjectByBody(body);
2341
+ if (entry && entry.tags.includes("grabable")) {
2342
+ this.mouseConstraint.constraint.bodyB = entry.body;
2343
+ this.mouseConstraint.constraint.pointB = {
2344
+ x: position.x - entry.body.position.x,
2345
+ y: position.y - entry.body.position.y
2346
+ };
2347
+ return entry.id;
2348
+ }
2349
+ }
2350
+ return null;
2351
+ }
2352
+ /**
2353
+ * Release any currently grabbed object.
2354
+ */
2355
+ endGrab() {
2356
+ if (this.mouseConstraint) {
2357
+ this.mouseConstraint.constraint.bodyB = null;
2358
+ }
2359
+ }
2360
+ /**
2361
+ * Get the ID of the currently grabbed object.
2362
+ * @returns The ID of the grabbed object, or null if nothing is grabbed
2363
+ */
2364
+ getGrabbedObject() {
2365
+ if (!this.mouseConstraint?.constraint.bodyB) return null;
2366
+ const entry = this.findObjectByBody(this.mouseConstraint.constraint.bodyB);
2367
+ return entry?.id ?? null;
2368
+ }
2317
2369
  // ==================== PHYSICS MANIPULATION METHODS ====================
2318
2370
  /**
2319
2371
  * Apply a force to an object.