-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathBookmarksUtils.js
More file actions
230 lines (196 loc) · 6.2 KB
/
BookmarksUtils.js
File metadata and controls
230 lines (196 loc) · 6.2 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
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
// @flow
import { rgbToHex } from '../../Utils/ColorTransformer';
const gd: libGDevelop = global.gd;
export type Bookmark = {|
eventPtr: number,
name: string,
eventType: string,
id: string,
timestamp: number,
borderLeftColor?: ?string,
|};
/**
* Scan all events recursively and collect those that are bookmarked
*/
export const scanEventsForBookmarks = (
events: gdEventsList
): Array<Bookmark> => {
if (!events) return [];
const bookmarks: Array<Bookmark> = [];
const scanEventsList = (eventsList: gdEventsList) => {
try {
// Safety check: ensure eventsList is valid and has the getEventsCount method
if (!eventsList || typeof eventsList.getEventsCount !== 'function') {
return;
}
for (let i = 0; i < eventsList.getEventsCount(); i++) {
const event = eventsList.getEventAt(i);
if (!event) continue;
try {
// Check if event has a bookmark ID
const bookmarkId =
event.getEventBookmarkId && event.getEventBookmarkId();
if (bookmarkId && bookmarkId.length > 0) {
const bookmark: Bookmark = {
eventPtr: event.ptr,
name: generateBookmarkName(event),
eventType: event.getType(),
id: bookmarkId,
timestamp: Date.now(),
borderLeftColor: getEventTypeColor(event),
};
bookmarks.push(bookmark);
}
// Recursively scan sub-events
if (event.canHaveSubEvents && event.canHaveSubEvents()) {
const subEvents = event.getSubEvents();
if (subEvents) {
scanEventsList(subEvents);
}
}
} catch (err) {
console.error('Error processing event for bookmarks:', err);
}
}
} catch (err) {
console.error('Error scanning event list for bookmarks:', err);
}
};
scanEventsList(events);
return bookmarks;
};
/**
* Get the border color for a bookmark based on the event type
*/
const getEventTypeColor = (event: gdBaseEvent): ?string => {
const eventType = event.getType();
if (eventType === 'BuiltinCommonInstructions::Comment') {
const commentEvent = gd.asCommentEvent(event);
return `#${rgbToHex(
commentEvent.getBackgroundColorRed(),
commentEvent.getBackgroundColorGreen(),
commentEvent.getBackgroundColorBlue()
)}`;
}
if (eventType === 'BuiltinCommonInstructions::Group') {
const groupEvent = gd.asGroupEvent(event);
return `#${rgbToHex(
groupEvent.getBackgroundColorR(),
groupEvent.getBackgroundColorG(),
groupEvent.getBackgroundColorB()
)}`;
}
return null;
};
/**
* Generate a default name for a bookmark based on the event
*/
export const generateBookmarkName = (event: gdBaseEvent): string => {
const eventType = event.getType();
// For comment events, use the comment text
if (eventType === 'BuiltinCommonInstructions::Comment') {
const commentEvent = gd.asCommentEvent(event);
const comment = commentEvent.getComment();
if (comment.length > 0) {
return comment.length > 50 ? comment.substring(0, 50) + '...' : comment;
}
return 'Comment';
}
// For group events, use the group name
if (eventType === 'BuiltinCommonInstructions::Group') {
const groupEvent = gd.asGroupEvent(event);
const name = groupEvent.getName();
if (name.length > 0) {
return name.length > 50 ? name.substring(0, 50) + '...' : name;
}
return 'Group';
}
// For standard events, try to extract first condition or action text
if (eventType === 'BuiltinCommonInstructions::Standard') {
const standardEvent = gd.asStandardEvent(event);
// Try to get first condition
const conditions = standardEvent.getConditions();
if (conditions.size() > 0) {
const firstCondition = conditions.get(0);
const type = firstCondition.getType();
if (type.length > 0) {
const conditionText = type.replace(/:/g, ' ');
return conditionText.length > 50
? conditionText.substring(0, 50) + '...'
: conditionText;
}
}
// Try to get first action
const actions = standardEvent.getActions();
if (actions.size() > 0) {
const firstAction = actions.get(0);
const type = firstAction.getType();
if (type.length > 0) {
const actionText = type.replace(/:/g, ' ');
return actionText.length > 50
? actionText.substring(0, 50) + '...'
: actionText;
}
}
return 'Standard Event';
}
// For other event types, use a generic name based on the type
const readableType = eventType
.replace('BuiltinCommonInstructions::', '')
.replace(/([A-Z])/g, ' $1')
.trim();
return readableType || 'Event';
};
/**
* Recursively search for an event by its pointer in an event list
*/
export const findEventByPtr = (
events: gdEventsList,
ptr: number
): ?gdBaseEvent => {
if (!events || !ptr) return null;
for (let i = 0; i < events.getEventsCount(); i++) {
const event = events.getEventAt(i);
if (!event) continue;
if (event.ptr === ptr) return event;
// Recursively search sub-events
if (event.canHaveSubEvents && event.canHaveSubEvents()) {
const subEvents = event.getSubEvents();
if (subEvents) {
const found = findEventByPtr(subEvents, ptr);
if (found) return found;
}
}
}
return null;
};
/**
* Recursively search for an event by its pointer and return it with its parent list and index
*/
export type EventLocation = {|
event: gdBaseEvent,
eventsList: gdEventsList,
indexInList: number,
|};
export const findEventLocationByPtr = (
events: gdEventsList,
ptr: number
): ?EventLocation => {
if (!events || !ptr) return null;
for (let i = 0; i < events.getEventsCount(); i++) {
const event = events.getEventAt(i);
if (!event) continue;
if (event.ptr === ptr) {
return { event, eventsList: events, indexInList: i };
}
// Recursively search sub-events
if (event.canHaveSubEvents && event.canHaveSubEvents()) {
const subEvents = event.getSubEvents();
if (subEvents) {
const found = findEventLocationByPtr(subEvents, ptr);
if (found) return found;
}
}
}
return null;
};