Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import type { Interactive } from "./Interactive";
 
export interface InteractionManagerOptions {
  container: HTMLElement;
  pixelRatio?: number;
  getLayerInfo?: (interactive: Interactive) => LayerInfo | null;
}
 
export interface LayerInfo {
  offsetX: number;
  offsetY: number;
  width: number;
  height: number;
}
 
/**
 * Manages interactions between mouse events and Interactive objects
 */
export class InteractionManager {
  private container: HTMLElement;
  private pixelRatio: number;
  private getLayerInfo?: (interactive: Interactive) => LayerInfo | null;
  private interactiveObjects: Interactive[] = [];
  private hoveredObject: Interactive | null = null;
  private draggedObject: Interactive | null = null;
  private isDestroyed = false;
 
  constructor({ container, pixelRatio = 1, getLayerInfo }: InteractionManagerOptions) {
    this.container = container;
    this.pixelRatio = pixelRatio;
    this.getLayerInfo = getLayerInfo;
    this.attachEvents();
  }
 
  /**
   * Register an interactive object
   */
  register(interactive: Interactive): void {
    if (!this.interactiveObjects.includes(interactive)) {
      this.interactiveObjects.push(interactive);
      // Sort by z-index (highest first for proper hit testing)
      this.interactiveObjects.sort((a, b) => {
        const aZ = a.getZIndex?.() ?? 0;
        const bZ = b.getZIndex?.() ?? 0;
        return bZ - aZ;
      });
    }
  }
 
  /**
   * Unregister an interactive object
   */
  unregister(interactive: Interactive): void {
    const index = this.interactiveObjects.indexOf(interactive);
    if (index !== -1) {
      this.interactiveObjects.splice(index, 1);
    }
 
    // Clear references if this object was active
    if (this.hoveredObject === interactive) {
      this.hoveredObject = null;
    }
    if (this.draggedObject === interactive) {
      this.draggedObject = null;
    }
  }
 
  /**
   * Find the interactive object under the given coordinates
   */
  private findInteractiveUnderPoint(x: number, y: number): Interactive | null {
    for (const interactive of this.interactiveObjects) {
      if (interactive.isEnabled?.() !== false) {
        // Get layer-specific coordinates
        const layerCoords = this.getLayerCoordinates(interactive, x, y);
        if (layerCoords && interactive.hitTest(layerCoords.x, layerCoords.y)) {
          return interactive;
        }
      }
    }
    return null;
  }
 
  /**
   * Transform global coordinates to layer-specific coordinates
   */
  private getLayerCoordinates(
    interactive: Interactive,
    globalX: number,
    globalY: number,
  ): { x: number; y: number } | null {
    if (!this.getLayerInfo) {
      // Fallback to global coordinates if no layer info provider
      return { x: globalX, y: globalY };
    }
 
    const layerInfo = this.getLayerInfo(interactive);
    if (!layerInfo) {
      return null;
    }
 
    // Transform global coordinates to layer-relative coordinates
    const layerX = globalX - layerInfo.offsetX;
    const layerY = globalY - layerInfo.offsetY;
 
    // Check if coordinates are within layer bounds
    if (layerX < 0 || layerX > layerInfo.width || layerY < 0 || layerY > layerInfo.height) {
      return null;
    }
 
    return { x: layerX, y: layerY };
  }
 
  /**
   * Get coordinates relative to the container
   */
  private getRelativeCoordinates(event: MouseEvent): { x: number; y: number } {
    const rect = this.container.getBoundingClientRect();
    return {
      x: event.clientX - rect.left,
      y: event.clientY - rect.top,
    };
  }
 
  /**
   * Update cursor based on hovered object
   */
  private updateCursor(interactive: Interactive | null): void {
    if (interactive?.getCursor) {
      const cursor = interactive.getCursor();
      this.container.style.cursor = cursor;
    } else {
      // Clear inline cursor to allow parent container (e.g., Visualizer) to control it
      this.container.style.cursor = "";
    }
  }
 
  private handleMouseMove = (event: MouseEvent): void => {
    if (this.isDestroyed) return;
 
    const { x, y } = this.getRelativeCoordinates(event);
 
    // If we're dragging, send move events to the dragged object
    if (this.draggedObject) {
      this.draggedObject.onMouseMove?.(event);
      return;
    }
 
    const interactive = this.findInteractiveUnderPoint(x, y);
 
    // Handle hover state changes
    if (interactive !== this.hoveredObject) {
      // Mouse leave previous object
      if (this.hoveredObject) {
        this.hoveredObject.onMouseLeave?.(event);
      }
 
      // Mouse enter new object
      if (interactive) {
        interactive.onMouseEnter?.(event);
      }
 
      this.hoveredObject = interactive;
      this.updateCursor(interactive);
    }
 
    // Send mouse move to currently hovered object
    if (this.hoveredObject) {
      this.hoveredObject.onMouseMove?.(event);
    }
  };
 
  private handleMouseDown = (event: MouseEvent): void => {
    if (this.isDestroyed) return;
 
    const { x, y } = this.getRelativeCoordinates(event);
    const interactive = this.findInteractiveUnderPoint(x, y);
 
    if (interactive) {
      this.draggedObject = interactive;
      interactive.onMouseDown?.(event);
      // Update cursor to reflect dragging state
      this.updateCursor(interactive);
    }
  };
 
  private handleMouseUp = (event: MouseEvent): void => {
    if (this.isDestroyed) return;
 
    const { x, y } = this.getRelativeCoordinates(event);
    const interactive = this.findInteractiveUnderPoint(x, y);
 
    // Send mouse up to dragged object if it exists
    if (this.draggedObject) {
      this.draggedObject.onMouseUp?.(event);
      this.draggedObject = null;
    }
 
    // Send mouse up to object under cursor
    if (interactive) {
      interactive.onMouseUp?.(event);
    }
 
    // Update cursor based on current hover state
    this.updateCursor(interactive);
  };
 
  private handleClick = (event: MouseEvent): void => {
    if (this.isDestroyed) return;
 
    const { x, y } = this.getRelativeCoordinates(event);
    const interactive = this.findInteractiveUnderPoint(x, y);
 
    if (interactive) {
      interactive.onClick?.(event);
    }
  };
 
  private handleDoubleClick = (event: MouseEvent): void => {
    if (this.isDestroyed) return;
 
    const { x, y } = this.getRelativeCoordinates(event);
    const interactive = this.findInteractiveUnderPoint(x, y);
 
    if (interactive) {
      interactive.onDoubleClick?.(event);
    }
  };
 
  private handleMouseLeave = (event: MouseEvent): void => {
    if (this.isDestroyed) return;
 
    // Clear hover state when mouse leaves container
    if (this.hoveredObject) {
      this.hoveredObject.onMouseLeave?.(event);
      this.hoveredObject = null;
      // Clear inline cursor to allow parent container to control it
      this.container.style.cursor = "";
    }
  };
 
  private attachEvents(): void {
    this.container.addEventListener("mousemove", this.handleMouseMove);
    this.container.addEventListener("mousedown", this.handleMouseDown);
    this.container.addEventListener("mouseup", this.handleMouseUp);
    this.container.addEventListener("click", this.handleClick);
    this.container.addEventListener("dblclick", this.handleDoubleClick);
    this.container.addEventListener("mouseleave", this.handleMouseLeave);
  }
 
  private removeEvents(): void {
    this.container.removeEventListener("mousemove", this.handleMouseMove);
    this.container.removeEventListener("mousedown", this.handleMouseDown);
    this.container.removeEventListener("mouseup", this.handleMouseUp);
    this.container.removeEventListener("click", this.handleClick);
    this.container.removeEventListener("dblclick", this.handleDoubleClick);
    this.container.removeEventListener("mouseleave", this.handleMouseLeave);
  }
 
  /**
   * Update pixel ratio (e.g., on zoom or device change)
   */
  setPixelRatio(pixelRatio: number): void {
    this.pixelRatio = pixelRatio;
  }
 
  /**
   * Get all registered interactive objects
   */
  getInteractiveObjects(): readonly Interactive[] {
    return this.interactiveObjects;
  }
 
  /**
   * Clean up the interaction manager
   */
  destroy(): void {
    this.isDestroyed = true;
    this.removeEvents();
    this.interactiveObjects.length = 0;
    this.hoveredObject = null;
    this.draggedObject = null;
  }
}