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
import type { BezierPoint } from "./types";
 
export interface GlobalSelectionState {
  selectedInstances: Map<string, Set<number>>; // instanceId -> selected point indices
  activeInstanceId: string | null;
  isTransforming: boolean;
  transformerState: {
    rotation: number;
    scaleX: number;
    scaleY: number;
    centerX: number;
    centerY: number;
  } | null;
}
 
export interface VectorInstance {
  id: string;
  getPoints: () => BezierPoint[];
  updatePoints: (points: BezierPoint[]) => void;
  setSelectedPoints: (selectedPoints: Set<number>) => void;
  setSelectedPointIndex: (index: number | null) => void;
  onPointSelected?: (index: number | null) => void;
  onTransformationComplete?: (data: any) => void;
  getTransform: () => { zoom: number; offsetX: number; offsetY: number };
  getFitScale: () => number;
  getBounds: () => { width: number; height: number } | undefined;
}
 
export class VectorSelectionTracker {
  private static instance: VectorSelectionTracker | null = null;
  private state: GlobalSelectionState = {
    selectedInstances: new Map(),
    activeInstanceId: null,
    isTransforming: false,
    transformerState: null,
  };
  private instances: Map<string, VectorInstance> = new Map();
  private listeners: Set<(state: GlobalSelectionState) => void> = new Set();
 
  private constructor() {}
 
  static getInstance(): VectorSelectionTracker {
    if (!VectorSelectionTracker.instance) {
      VectorSelectionTracker.instance = new VectorSelectionTracker();
    }
    return VectorSelectionTracker.instance;
  }
 
  // Instance Management
  registerInstance(instance: VectorInstance): void {
    this.instances.set(instance.id, instance);
  }
 
  unregisterInstance(instanceId: string): void {
    this.instances.delete(instanceId);
    this.state.selectedInstances.delete(instanceId);
 
    // If this was the active instance, clear it
    if (this.state.activeInstanceId === instanceId) {
      this.state.activeInstanceId = null;
    }
 
    this.notifyListeners();
  }
 
  // Selection Management
  selectPoints(instanceId: string, pointIndices: Set<number>): void {
    // Allow multiple instances to have selections simultaneously
    // Removed the blocking logic that prevented multiple vector regions from being selected
 
    if (pointIndices.size === 0) {
      this.state.selectedInstances.delete(instanceId);
      // If this was the active instance and we're clearing selection, clear active instance
      if (this.state.activeInstanceId === instanceId) {
        this.state.activeInstanceId = null;
      }
    } else {
      this.state.selectedInstances.set(instanceId, new Set(pointIndices));
      // Set this as the active instance for transformation purposes
      // Multiple instances can now have selections, but only one can be actively transforming
      this.state.activeInstanceId = instanceId;
    }
 
    this.notifyListeners();
 
    // Update the instance's local selection state
    const instance = this.instances.get(instanceId);
    if (instance) {
      instance.setSelectedPoints(pointIndices);
      instance.setSelectedPointIndex(pointIndices.size === 1 ? Array.from(pointIndices)[0] : null);
      instance.onPointSelected?.(pointIndices.size === 1 ? Array.from(pointIndices)[0] : null);
    }
  }
 
  // Check if an instance can have selection
  canInstanceHaveSelection(instanceId: string): boolean {
    // Allow all instances to have selections simultaneously
    return true;
  }
 
  // Get the currently active instance ID
  getActiveInstanceId(): string | null {
    return this.state.activeInstanceId;
  }
 
  clearSelection(): void {
    // Clear all instance selections
    for (const [instanceId, instance] of this.instances) {
      instance.setSelectedPoints(new Set());
      instance.setSelectedPointIndex(null);
      instance.onPointSelected?.(null);
    }
 
    this.state.selectedInstances.clear();
    this.state.activeInstanceId = null;
    this.notifyListeners();
  }
 
  getGlobalSelection(): GlobalSelectionState {
    return { ...this.state };
  }
 
  getSelectedPoints(): Array<{ instanceId: string; pointIndex: number; point: BezierPoint }> {
    const selectedPoints: Array<{ instanceId: string; pointIndex: number; point: BezierPoint }> = [];
 
    for (const [instanceId, pointIndices] of this.state.selectedInstances) {
      const instance = this.instances.get(instanceId);
      if (instance) {
        const points = instance.getPoints();
        for (const pointIndex of pointIndices) {
          if (pointIndex < points.length) {
            selectedPoints.push({
              instanceId,
              pointIndex,
              point: points[pointIndex],
            });
          }
        }
      }
    }
 
    return selectedPoints;
  }
 
  // Event Listeners
  subscribe(listener: (state: GlobalSelectionState) => void): () => void {
    this.listeners.add(listener);
    return () => {
      this.listeners.delete(listener);
    };
  }
 
  private notifyListeners(): void {
    const globalState = this.getGlobalSelection();
    for (const listener of this.listeners) {
      listener(globalState);
    }
  }
 
  // Utility Methods
  hasSelection(): boolean {
    return this.state.selectedInstances.size > 0;
  }
 
  getSelectionCount(): number {
    let count = 0;
    for (const pointIndices of this.state.selectedInstances.values()) {
      count += pointIndices.size;
    }
    return count;
  }
 
  isInstanceSelected(instanceId: string): boolean {
    return this.state.selectedInstances.has(instanceId);
  }
 
  getInstanceSelection(instanceId: string): Set<number> | undefined {
    return this.state.selectedInstances.get(instanceId);
  }
}