-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlighter-manager.js
More file actions
184 lines (162 loc) · 4.72 KB
/
highlighter-manager.js
File metadata and controls
184 lines (162 loc) · 4.72 KB
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
import * as THREE from 'three';
import * as OBCFront from '@thatopen/components-front';
export class HighlighterManager {
constructor(components, world) {
this.components = components;
this.world = world;
this.highlighter = null;
this.highlighterReady = false;
this.selectedItems = new Map(); // Track selected items
}
init() {
console.log('Initializing Highlighter...');
try {
// Get the Highlighter component from the front module
const Highlighter = OBCFront.Highlighter;
if (!Highlighter) {
console.warn('Highlighter not found in OBCFront');
return false;
}
// Create the highlighter instance
this.highlighter = new Highlighter(this.components);
// Setup the highlighter with world and material definition
this.highlighter.setup({
world: this.world,
selectMaterialDefinition: {
color: new THREE.Color('#bcf124'),
opacity: 1,
transparent: false,
renderedFaces: 0,
},
});
// Listen to selection events
this.highlighter.events.select.onHighlight.add((modelIdMap) => {
console.log('Items highlighted:', modelIdMap);
this.selectedItems = new Map(Object.entries(modelIdMap));
});
this.highlighter.events.select.onClear.add(() => {
console.log('Selection cleared');
this.selectedItems.clear();
});
this.highlighterReady = true;
console.log('Highlighter initialized successfully');
return true;
} catch (error) {
console.warn('Could not initialize Highlighter:', error.message);
this.highlighterReady = false;
return false;
}
}
// Add custom highlight style
addStyle(name, color) {
if (!this.highlighter) {
console.warn('Highlighter not initialized');
return false;
}
try {
this.highlighter.styles.set(name, {
color: new THREE.Color(color),
opacity: 1,
transparent: false,
renderedFaces: 0,
});
console.log(`Highlighter style '${name}' added with color:`, color);
return true;
} catch (error) {
console.error('Error adding highlighter style:', error);
return false;
}
}
// Apply highlighting to items
highlightByID(styleName, modelIdMap) {
if (!this.highlighter) {
console.warn('Highlighter not initialized');
return false;
}
try {
this.highlighter.highlightByID(styleName, modelIdMap);
console.log(`Applied '${styleName}' style to items:`, modelIdMap);
return true;
} catch (error) {
console.error('Error highlighting items:', error);
return false;
}
}
// Clear highlighting from items
clear(styleName, modelIdMap) {
if (!this.highlighter) {
console.warn('Highlighter not initialized');
return false;
}
try {
this.highlighter.clear(styleName, modelIdMap);
console.log(`Cleared '${styleName}' style from items:`, modelIdMap);
return true;
} catch (error) {
console.error('Error clearing highlight:', error);
return false;
}
}
// Clear all highlights
clearAll() {
if (!this.highlighter) {
console.warn('Highlighter not initialized');
return false;
}
try {
// Get all style names and clear them
for (const [styleName] of this.highlighter.styles) {
this.highlighter.clear(styleName);
}
console.log('All highlights cleared');
return true;
} catch (error) {
console.error('Error clearing all highlights:', error);
return false;
}
}
// Get currently selected items
getSelectedItems() {
return Array.from(this.selectedItems.entries());
}
// Get selection count
getSelectionCount() {
let count = 0;
for (const items of this.selectedItems.values()) {
count += items.length || 0;
}
return count;
}
// Set selection color
setSelectionColor(hexColor) {
if (!this.highlighter) {
console.warn('Highlighter not initialized');
return false;
}
try {
// Update the select material definition
this.highlighter.setup({
world: this.world,
selectMaterialDefinition: {
color: new THREE.Color(hexColor),
opacity: 1,
transparent: false,
renderedFaces: 0,
},
});
console.log('Selection color set to:', hexColor);
return true;
} catch (error) {
console.error('Error setting selection color:', error);
return false;
}
}
// Get info about highlighter
getInfo() {
return {
ready: this.highlighterReady,
selectionCount: this.getSelectionCount(),
styles: this.highlighter ? Array.from(this.highlighter.styles.keys()) : [],
};
}
}