-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathuseModeManager.ts
More file actions
885 lines (821 loc) · 31.3 KB
/
useModeManager.ts
File metadata and controls
885 lines (821 loc) · 31.3 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
import {
computed, Ref, reactive, ref, onBeforeUnmount, toRef,
} from 'vue';
import { uniq, flatMapDeep, flattenDeep } from 'lodash';
import Track, { TrackId } from 'vue-media-annotator/track';
import {
RectBounds,
updateBounds,
validateRotation,
ROTATION_ATTRIBUTE_NAME,
} from 'vue-media-annotator/utils';
import { EditAnnotationTypes, VisibleAnnotationTypes } from 'vue-media-annotator/layers';
import { AggregateMediaController } from 'vue-media-annotator/components/annotators/mediaControllerType';
import Recipe from 'vue-media-annotator/recipe';
import type { AnnotationId } from 'vue-media-annotator/BaseAnnotation';
import type TrackFilterControls from 'vue-media-annotator/TrackFilterControls';
import { usePrompt } from 'dive-common/vue-utilities/prompt-service';
import { clientSettings } from 'dive-common/store/settings';
import GroupFilterControls from 'vue-media-annotator/GroupFilterControls';
import CameraStore from 'vue-media-annotator/CameraStore';
import { SortedAnnotation } from 'vue-media-annotator/BaseAnnotationStore';
type SupportedFeature = GeoJSON.Feature<GeoJSON.Point | GeoJSON.Polygon | GeoJSON.LineString>;
/* default to index + 1
* call with -1 to select previous, or pass any other delta
*/
function selectNext<T extends SortedAnnotation<T>>(filtered: Readonly<T>[], selected: Readonly<AnnotationId | null>, delta = 1): AnnotationId | null {
if (filtered.length > 0) {
if (selected === null) {
// if no track is selected, return the first trackId
return filtered[0].id;
}
// return the trackId by the delta offset if it exists
const index = filtered.findIndex((t) => t.id === selected);
const newIndex = index + delta;
if (newIndex >= 0 && newIndex < filtered.length) {
// if we are not at the end
return filtered[newIndex].id;
}
}
//Return null if no other conditions are met
return null;
}
interface SetAnnotationStateArgs {
visible?: VisibleAnnotationTypes[];
editing?: EditAnnotationTypes;
key?: string;
recipeName?: string;
}
/**
* The point of this composition function is to define and manage the transition betwee
* different UI states within the program. States and state transitions can be modified
* based on settings, blocked if it tries to go to incompatible state or provide feedback
*
* Mostly allows us to inject additional logic into transitions.
*/
export default function useModeManager({
cameraStore,
trackFilterControls,
groupFilterControls,
aggregateController,
readonlyState,
recipes,
}: {
cameraStore: CameraStore;
trackFilterControls: TrackFilterControls;
groupFilterControls: GroupFilterControls;
aggregateController: Ref<AggregateMediaController>;
readonlyState: Readonly<Ref<boolean>>;
recipes: Recipe[];
}) {
let creating = false;
const { prompt } = usePrompt();
const annotationModes = reactive({
visible: ['rectangle', 'Polygon', 'LineString', 'text'] as VisibleAnnotationTypes[],
editing: 'rectangle' as EditAnnotationTypes,
});
const trackSettings = toRef(clientSettings, 'trackSettings');
const groupSettings = toRef(clientSettings, 'groupSettings');
const selectedCamera = ref('singleCam');
const linkingState = ref(false);
const linkingTrack: Ref<AnnotationId| null> = ref(null);
const linkingCamera = ref('');
// Meaning of this value varies based on the editing mode. When in
// polygon edit mode, this corresponds to a polygon point. Ditto in line mode.
const selectedFeatureHandle = ref(-1);
//The Key of the selected type, for now mostly ''
const selectedKey = ref('');
// the currently selected Track
const selectedTrackId = ref(null as AnnotationId | null);
// the currently editing Group
const editingGroupId = ref(null as AnnotationId | null);
// boolean whether or not selectedTrackId is also being edited.
const editingTrack = ref(false);
// boolean denoting whether or not multiple tracks are selected for delete/edit
const editingMultiTrack = ref(false);
// which type is currently being edited, if any
const editingMode = computed(() => editingTrack.value && annotationModes.editing);
const editingCanary = ref(false);
// Track Multi-select state
const multiSelectList = ref([] as AnnotationId[]);
const multiSelectActive = computed(() => multiSelectList.value.length > 0);
const _filteredTracks = computed(
() => trackFilterControls.filteredAnnotations.value.map((filtered) => filtered.annotation),
);
const _filteredGroups = computed(
() => groupFilterControls.filteredAnnotations.value.map((filtered) => filtered.annotation),
);
const selectNextTrack = (delta = 1) => selectNext(_filteredTracks.value, selectedTrackId.value, delta);
const selectNextGroup = (delta = 1) => selectNext(_filteredGroups.value, editingGroupId.value, delta);
function selectTrack(trackId: AnnotationId | null, edit = false) {
selectedTrackId.value = trackId;
if (edit && readonlyState.value) {
prompt({ title: 'Read Only Mode', text: 'This Dataset is in Read Only mode, no edits can be made.' });
} else {
editingTrack.value = trackId !== null && edit;
}
}
/** end */
function _depend(): boolean {
return editingCanary.value;
}
function _nudgeEditingCanary() {
editingCanary.value = !editingCanary.value;
}
// What is occuring in editing mode
const editingDetails = computed(() => {
_depend();
if (editingMode.value && selectedTrackId.value !== null) {
const { frame } = aggregateController.value;
try {
const track = cameraStore.getPossibleTrack(selectedTrackId.value, selectedCamera.value);
if (track) {
const [feature] = track.getFeature(frame.value);
if (feature) {
if (!feature?.bounds?.length) {
return 'Creating';
} if (annotationModes.editing === 'rectangle') {
return 'Editing';
}
return (feature.geometry?.features.filter((item) => item.geometry.type === annotationModes.editing).length ? 'Editing' : 'Creating');
}
return 'Creating';
}
} catch {
// No Track for this camera
return 'disabled';
}
}
return 'disabled';
});
// which types are currently visible, always including the editingType
const visibleModes = computed(() => (
uniq(annotationModes.visible.concat(editingMode.value || []))
));
/**
* Figure out if a new feature should enable interpolation
* based on current state and the result of canInterolate.
*/
function _shouldInterpolate(canInterpolate: boolean) {
// if this is a track, then whether to interpolate
// is determined by newTrackSettings (if new track)
// or canInterpolate (if existing track)
const interpolateTrack = creating
? trackSettings.value.newTrackSettings.modeSettings.Track.interpolate
: canInterpolate;
// if detection, interpolate is always false
return trackSettings.value.newTrackSettings.mode === 'Detection'
? false
: interpolateTrack;
}
function seekNearest(track: Track) {
// Seek to the nearest point in the track.
const { frame } = aggregateController.value;
if (frame.value < track.begin) {
aggregateController.value.seek(track.begin);
} else if (frame.value > track.end) {
aggregateController.value.seek(track.end);
}
}
function seekFrame(frame: number) {
aggregateController.value.seek(frame);
}
async function _setLinkingTrack(trackId: TrackId) {
//Confirm that there is no track for other cameras.
const trackList = cameraStore.getTrackAll(trackId);
if (trackList.length > 1) {
prompt({
title: 'Linking Error',
text: [`TrackId: ${trackId} has tracks on other cameras besides the selected camera ${linkingCamera.value}`,
`You need to select a track that only exists on camera: ${linkingCamera.value} `,
'You can split of the track you were trying to select by clicking OK and hitting Escape to exit Linking Mode and using the split tool',
],
positiveButton: 'OK',
});
} else {
linkingTrack.value = trackId;
}
}
function _selectKey(key: string | undefined) {
if (typeof key === 'string') {
selectedKey.value = key;
} else {
selectedKey.value = '';
}
}
function handleSelectFeatureHandle(i: number, key = '') {
if (i !== selectedFeatureHandle.value) {
selectedFeatureHandle.value = i;
} else {
selectedFeatureHandle.value = -1;
}
_selectKey(key);
}
function handleSelectTrack(
trackId: AnnotationId | null,
edit = false,
modifiers: { ctrl: boolean } = { ctrl: false },
) {
/**
* If creating mode and editing and selectedTrackId is the same,
* don't kick out of creating mode. This happens when moving between
* rect/poly/line during continuous creation.
*/
if (!(creating && edit && trackId === selectedTrackId.value)) {
creating = false;
}
if (!modifiers?.ctrl && editingMultiTrack.value) {
editingMultiTrack.value = false;
multiSelectList.value = [];
}
if (trackId !== null && !edit && !creating && modifiers?.ctrl) {
const selected = new Set(multiSelectList.value);
if (selected.has(trackId) && editingMultiTrack.value) {
// If ctrl + click on an already-selected track, remove that track from the list.
selected.delete(trackId);
multiSelectList.value = Array.from(selected);
} else if (selectedTrackId.value === null) {
multiSelectList.value = Array.from(selected.add(trackId));
} else {
multiSelectList.value = Array.from((new Set([selectedTrackId.value])).add(trackId));
selectedTrackId.value = null;
}
if (multiSelectList.value.length > 0) {
editingMultiTrack.value = true;
selectedTrackId.value = null;
}
return;
}
/**
* If merge is in progress, add selected tracks to the merge list
*/
if (trackId !== null && multiSelectActive.value) {
multiSelectList.value = Array.from((new Set(multiSelectList.value).add(trackId)));
/**
* If editing group, then the newly selected track should be added to the group
*/
if (editingGroupId.value !== null && !edit) {
const track = cameraStore.getAnyTrack(trackId);
const groupStore = cameraStore.camMap.value.get(selectedCamera.value)?.groupStore;
if (groupStore) {
groupStore.get(editingGroupId.value).addMembers({
[trackId]: { ranges: [[track.begin, track.end]] },
});
}
} else if (edit) {
editingGroupId.value = null;
multiSelectList.value = [];
}
} else if (linkingState.value) {
// Only use the first non-null track with is clicked on to link
if (trackId !== null) {
_setLinkingTrack(trackId);
}
return;
}
/* Do not allow editing when merge is in progress */
selectTrack(trackId, edit && !multiSelectActive.value);
}
/** Put UI into group editing mode. */
function handleGroupEdit(groupId: AnnotationId | null) {
creating = false;
editingTrack.value = false;
editingGroupId.value = groupId;
if (groupId !== null) {
/** When moving into a group edit mode, multi-select all track members */
const groupStore = cameraStore.camMap.value.get(selectedCamera.value)?.groupStore;
if (groupStore) {
const group = groupStore.get(groupId);
multiSelectList.value = group.memberIds;
selectedTrackId.value = null;
seekNearest(cameraStore.getAnyTrack(multiSelectList.value[0]));
}
}
}
//Handles deselection or hitting escape including while editing
function handleEscapeMode() {
if (selectedTrackId.value !== null) {
const track = cameraStore.getPossibleTrack(selectedTrackId.value, selectedCamera.value);
if (track && track.begin === track.end) {
const features = track.getFeature(track.begin);
// If no features exist we remove the empty track
if (!features.filter((item) => item !== null).length) {
const trackStore = cameraStore.camMap.value.get(selectedCamera.value)?.trackStore;
if (trackStore) {
trackStore.remove(selectedTrackId.value);
}
}
}
}
linkingState.value = false;
linkingCamera.value = '';
linkingTrack.value = null;
multiSelectList.value = [];
handleGroupEdit(null);
handleSelectTrack(null, false);
}
function handleAddTrackOrDetection(overrideTrackId?: number): TrackId {
// Handles adding a new track with the NewTrack Settings
handleEscapeMode();
const { frame } = aggregateController.value;
let trackType = trackSettings.value.newTrackSettings.type;
if (overrideTrackId !== undefined) {
const track = cameraStore.getAnyPossibleTrack(overrideTrackId);
if (track !== undefined) {
// eslint-disable-next-line prefer-destructuring
trackType = track.confidencePairs[0][0];
}
} else {
// eslint-disable-next-line no-param-reassign
overrideTrackId = cameraStore.getNewTrackId();
}
const trackStore = cameraStore.camMap.value.get(selectedCamera.value)?.trackStore;
if (trackStore) {
const newTrackId = trackStore.add(
frame.value,
trackType,
selectedTrackId.value || undefined,
overrideTrackId,
).id;
selectTrack(newTrackId, true);
creating = true;
return newTrackId;
}
throw Error(`Could not find trackStore for Camera: ${selectedCamera.value}`);
}
function newTrackSettingsAfterLogic(addedTrack: Track) {
// Default settings which are updated by the TrackSettings component
let newCreatingValue = false; // by default, disable creating at the end of this function
if (creating) {
if (addedTrack && trackSettings.value.newTrackSettings !== null) {
if (trackSettings.value.newTrackSettings.mode === 'Track'
&& trackSettings.value.newTrackSettings.modeSettings.Track.autoAdvanceFrame
) {
aggregateController.value.nextFrame();
newCreatingValue = true;
} else if (trackSettings.value.newTrackSettings.mode === 'Detection') {
if (
trackSettings.value.newTrackSettings.modeSettings.Detection.continuous) {
handleAddTrackOrDetection(cameraStore.getNewTrackId());
newCreatingValue = true; // don't disable creating mode
}
}
}
}
_nudgeEditingCanary();
creating = newCreatingValue;
}
function handleUpdateRectBounds(frameNum: number, flickNum: number, bounds: RectBounds, rotation?: number) {
if (selectedTrackId.value !== null) {
const track = cameraStore.getPossibleTrack(selectedTrackId.value, selectedCamera.value);
if (track) {
// Determines if we are creating a new Detection
const { interpolate, features } = track.canInterpolate(frameNum);
const [real] = features;
// If there's already a keyframe at this frame, we're editing an existing annotation
const isEditingExisting = real !== null && real.keyframe;
track.setFeature({
frame: frameNum,
flick: flickNum,
bounds,
keyframe: true,
interpolate: _shouldInterpolate(interpolate),
});
// Save rotation as detection attribute if provided
const normalizedRotation = validateRotation(rotation);
if (normalizedRotation !== undefined) {
track.setFeatureAttribute(frameNum, ROTATION_ATTRIBUTE_NAME, normalizedRotation);
} else {
// Remove rotation attribute if rotation is 0 or undefined
const feature = track.features[frameNum];
if (feature && feature.attributes && ROTATION_ATTRIBUTE_NAME in feature.attributes) {
track.setFeatureAttribute(frameNum, ROTATION_ATTRIBUTE_NAME, undefined);
}
}
// Mark as user-modified if editing existing annotation (as detection attribute)
// Skip if track is userCreated (user-created tracks don't need userModified on every detection)
if (isEditingExisting && track.attributes?.userCreated !== true) {
track.setFeatureAttribute(frameNum, 'userModified', true);
}
newTrackSettingsAfterLogic(track);
}
}
}
function handleUpdateGeoJSON(
eventType: 'in-progress' | 'editing',
frameNum: number,
flickNum: number,
// Type alias this
data: SupportedFeature,
key?: string,
preventInterrupt?: () => void,
) {
/**
* Declare aggregate update collector. Each recipe
* will have the opportunity to modify this object.
*/
const update = {
// Geometry data to be applied to the feature
geoJsonFeatureRecord: {} as Record<string, SupportedFeature[]>,
// Ploygons to be unioned with existing bounds (update)
union: [] as GeoJSON.Polygon[],
// Polygons to be unioned without existing bounds (overwrite)
unionWithoutBounds: [] as GeoJSON.Polygon[],
// If the editor mode should change types
newType: undefined as EditAnnotationTypes | undefined,
// If the selected key should change
newSelectedKey: undefined as string | undefined,
// If the recipe has completed
done: [] as (boolean|undefined)[],
};
if (selectedTrackId.value !== null) {
const track = cameraStore.getPossibleTrack(selectedTrackId.value, selectedCamera.value);
if (track) {
// newDetectionMode is true if there's no keyframe on frameNum
const { features, interpolate } = track.canInterpolate(frameNum);
const [real] = features;
// Give each recipe the opportunity to make changes
recipes.forEach((recipe) => {
const changes = recipe.update(eventType, frameNum, track, [data], key);
// Prevent key conflicts among recipes
Object.keys(changes.data).forEach((key_) => {
if (key_ in update.geoJsonFeatureRecord) {
throw new Error(`Recipe ${recipe.name} tried to overwrite key ${key_} when it was already set`);
}
});
Object.assign(update.geoJsonFeatureRecord, changes.data);
// Collect unions
update.union.push(...changes.union);
update.unionWithoutBounds.push(...changes.unionWithoutBounds);
update.done.push(changes.done);
// Prevent more than 1 recipe from changing a given mode/key
if (changes.newType) {
if (update.newType) {
throw new Error(`Recipe ${recipe.name} tried to modify type when it was already set`);
}
update.newType = changes.newType;
}
if (changes.newSelectedKey) {
if (update.newSelectedKey) {
throw new Error(`Recipe ${recipe.name} tried to modify selectedKey when it was already set`);
}
update.newSelectedKey = changes.newSelectedKey;
}
});
// somethingChanged indicates whether there will need to be a redraw
// of the geometry currently displayed
const somethingChanged = (
update.union.length !== 0
|| update.unionWithoutBounds.length !== 0
|| Object.keys(update.geoJsonFeatureRecord).length !== 0
);
// If a drawable changed, but we aren't changing modes
// prevent an interrupt within EditAnnotationLayer
if (
somethingChanged
&& !update.newSelectedKey
&& !update.newType
&& preventInterrupt
) {
preventInterrupt();
} else {
// Otherwise, one of these state changes will trigger an interrupt.
if (update.newSelectedKey) {
selectedKey.value = update.newSelectedKey;
}
if (update.newType) {
annotationModes.editing = update.newType;
recipes.forEach((r) => r.deactivate());
}
}
// Update the state of the track in the trackstore.
if (somethingChanged) {
// If there's already a keyframe at this frame, we're editing an existing annotation
const isEditingExisting = real !== null && real.keyframe;
track.setFeature({
frame: frameNum,
flick: flickNum,
keyframe: true,
bounds: updateBounds(real?.bounds, update.union, update.unionWithoutBounds),
interpolate,
}, flatMapDeep(
update.geoJsonFeatureRecord,
(geomlist, key_) => geomlist.map((geom) => ({
type: geom.type,
geometry: geom.geometry,
properties: { key: key_ },
})),
));
// Mark as user-modified if editing existing annotation (as detection attribute)
// Skip if track is userCreated (user-created tracks don't need userModified on every detection)
if (isEditingExisting && track.attributes?.userCreated !== true) {
track.setFeatureAttribute(frameNum, 'userModified', true);
}
// Only perform "initialization" after the first shape.
// Treat this as a completed annotation if eventType is editing
// Or none of the recieps reported that they were unfinished.
if (eventType === 'editing' || update.done.every((v) => v !== false)) {
newTrackSettingsAfterLogic(track);
}
}
} else {
throw new Error(`${selectedTrackId.value} missing from trackMap`);
}
} else {
throw new Error('Cannot call handleUpdateGeojson without a selected Track ID');
}
}
/* If any recipes are active, allow them to remove a point */
function handleRemovePoint() {
if (selectedTrackId.value !== null && selectedFeatureHandle.value !== -1) {
const track = cameraStore.getPossibleTrack(selectedTrackId.value, selectedCamera.value);
if (track) {
recipes.forEach((r) => {
if (r.active.value) {
const { frame } = aggregateController.value;
r.deletePoint(
frame.value,
track,
selectedFeatureHandle.value,
selectedKey.value,
annotationModes.editing,
);
}
});
}
}
handleSelectFeatureHandle(-1);
}
/* If any recipes are active, remove the geometry they added */
function handleRemoveAnnotation() {
if (selectedTrackId.value !== null) {
const track = cameraStore.getPossibleTrack(selectedTrackId.value, selectedCamera.value);
if (track) {
const { frame } = aggregateController.value;
recipes.forEach((r) => {
if (r.active.value) {
r.delete(frame.value, track, selectedKey.value, annotationModes.editing);
}
});
_nudgeEditingCanary();
}
}
}
/**
* Unstage a track from the merge list
*/
function handleUnstageFromMerge(trackIds: TrackId[]) {
multiSelectList.value = multiSelectList.value.filter((trackId) => !trackIds.includes(trackId));
/* Unselect a track when it is unstaged */
if (selectedTrackId.value !== null && trackIds.includes(selectedTrackId.value)) {
handleSelectTrack(null);
}
/** Remove members from group if group editing */
if (editingGroupId.value !== null) {
const groupStore = cameraStore.camMap.value.get(selectedCamera.value)?.groupStore;
if (groupStore) {
const group = groupStore.annotationMap.get(editingGroupId.value);
if (group) group.removeMembers(trackIds);
}
}
if (editingMultiTrack.value && !editingMultiTrack.value) {
handleSelectTrack(null);
}
/** Exit group editing mode if last track is removed */
if (multiSelectList.value.length === 0) {
handleEscapeMode();
}
}
async function handleRemoveTrack(
trackIds: TrackId[],
forcePromptDisable = false,
cameraName = '',
autoSelectNextTrack = true,
) {
/* Figure out next track ID */
const maybeNextTrackId = selectNextTrack(1);
let previousOrNext = maybeNextTrackId !== null
? maybeNextTrackId
: selectNextTrack(-1);
/* Delete track */
if (!forcePromptDisable && trackSettings.value.deletionSettings.promptUser) {
const trackStrings = trackIds.map((track) => track.toString());
const groups = flattenDeep(
trackIds.map((trackId) => cameraStore.lookupGroups(trackId)),
);
const text = (['Would you like to delete the following tracks:']).concat(trackStrings);
if (groups.length > 0) {
text.push('');
text.push(`This track will be removed from ${groups.length} groups.`);
}
text.push('');
text.push('This setting can be changed under the Track Settings');
const result = await prompt({
title: 'Delete Confirmation',
text,
positiveButton: 'OK',
negativeButton: 'Cancel',
confirm: true,
});
if (!result) {
return;
}
}
trackIds.forEach((trackId) => {
cameraStore.remove(trackId, cameraName);
});
if (!multiSelectList.value.length) {
editingMultiTrack.value = false;
previousOrNext = null;
}
handleUnstageFromMerge(trackIds);
if (autoSelectNextTrack) {
selectTrack(previousOrNext, false);
}
}
/** Toggle editing mode for track */
function handleTrackEdit(trackId: TrackId) {
const track = cameraStore.getPossibleTrack(trackId, selectedCamera.value);
if (track) {
seekNearest(track);
const editing = trackId === selectedTrackId.value ? (!editingTrack.value) : true;
handleSelectTrack(trackId, editing);
} else if (cameraStore.getAnyTrack(trackId) !== undefined) {
//track exists in other cameras we create in the current map using override
handleAddTrackOrDetection(trackId);
const camTrack = cameraStore.getPossibleTrack(trackId, selectedCamera.value);
// now that we have a new track we select it for editing
if (camTrack) {
const editing = trackId === selectedTrackId.value;
handleSelectTrack(trackId, editing);
}
}
}
function handleTrackClick(trackId: TrackId, modifiers?: { ctrl: boolean }) {
const track = cameraStore.getTracksMerged(trackId);
seekNearest(track);
handleSelectTrack(trackId, editingTrack.value, modifiers);
}
function handleSelectNext(delta: number) {
const newTrack = selectNextTrack(delta);
/** Only allow selectNext when not in group editing mode. */
if (newTrack !== null && editingGroupId.value === null) {
handleSelectTrack(newTrack, false);
seekNearest(cameraStore.getAnyTrack(newTrack));
}
}
function handleSetAnnotationState({
visible, editing, key, recipeName,
}: SetAnnotationStateArgs) {
if (visible) {
annotationModes.visible = visible;
}
if (editing) {
annotationModes.editing = editing;
_selectKey(key);
handleSelectTrack(selectedTrackId.value, true);
recipes.forEach((r) => {
if (recipeName !== r.name) {
r.deactivate();
}
});
}
}
/**
* Merge: Enabled whenever there are candidates in the merge list
*/
function handleToggleMerge(): TrackId[] {
if (!multiSelectActive.value && selectedTrackId.value !== null) {
/* If no merge in progress and there is a selected track id */
multiSelectList.value = [selectedTrackId.value];
/* no editing in merge mode */
selectTrack(selectedTrackId.value, false);
} else {
multiSelectList.value = [];
handleGroupEdit(null);
}
return multiSelectList.value;
}
/**
* Merge: Commit the multi-select list to merge
*/
function handleCommitMerge() {
if (multiSelectList.value.length >= 2) {
const track = cameraStore.getTrack(multiSelectList.value[0], selectedCamera.value);
const otherTrackIds = multiSelectList.value.slice(1);
track.merge(otherTrackIds.map(
(trackId) => cameraStore.getTrack(trackId, selectedCamera.value),
));
handleRemoveTrack(otherTrackIds, true);
handleToggleMerge();
handleSelectTrack(track.id, false);
}
}
function handleStartLinking(camera: string) {
if (!linkingState.value && selectedTrackId.value !== null) {
linkingState.value = true;
if (cameraStore.camMap.value.has(camera)) {
linkingCamera.value = camera;
} else {
throw Error(`Camera: ${camera} does not exist in the system for linking`);
}
} else if (selectedTrackId.value === null) {
throw Error('Cannot start Linking without a track selected');
}
}
function handleStopLinking() {
linkingState.value = false;
linkingTrack.value = null;
linkingCamera.value = '';
}
/**
* Group: Add the currently selected track to a new group and
* enter group editing mode.
*/
function handleAddGroup() {
if (selectedTrackId.value !== null) {
const members = [cameraStore.getTrack(selectedTrackId.value, selectedCamera.value)];
const groupStore = cameraStore.camMap.value.get(selectedCamera.value)?.groupStore;
if (groupStore) {
const newGrp = groupStore.add(members, groupSettings.value.newGroupSettings.type);
handleGroupEdit(newGrp.id);
}
}
}
/**
* Delete every track that is currently multi-selected.
*/
async function handleDeleteSelectedTracks() {
if (editingMultiTrack.value) {
await handleRemoveTrack(multiSelectList.value, undefined, undefined, false);
selectedTrackId.value = null;
}
}
/**
* Group: Remove group ids and unselect everything.
*/
function handleRemoveGroup(ids: AnnotationId[]) {
ids.forEach((groupId) => {
const groupStore = cameraStore.camMap.value.get(selectedCamera.value)?.groupStore;
if (groupStore) {
groupStore.remove(groupId);
}
});
/* Figure out next track ID */
const maybeNextGroupId = selectNextGroup(1);
const previousOrNext = maybeNextGroupId !== null
? maybeNextGroupId
: selectNextGroup(-1);
handleEscapeMode();
handleGroupEdit(previousOrNext);
}
/* Subscribe to recipe activation events */
recipes.forEach((r) => r.bus.$on('activate', handleSetAnnotationState));
/* Unsubscribe before unmount */
onBeforeUnmount(() => {
recipes.forEach((r) => r.bus.$off('activate', handleSetAnnotationState));
});
return {
selectedTrackId,
editingGroupId,
editingMode,
editingTrack,
editingMultiTrack,
editingDetails,
linkingTrack,
linkingState,
linkingCamera,
multiSelectList,
multiSelectActive,
visibleModes,
selectedFeatureHandle,
selectedKey,
selectedCamera,
selectNextTrack,
handler: {
commitMerge: handleCommitMerge,
groupAdd: handleAddGroup,
deleteSelectedTracks: handleDeleteSelectedTracks,
groupEdit: handleGroupEdit,
toggleMerge: handleToggleMerge,
trackAdd: handleAddTrackOrDetection,
trackAbort: handleEscapeMode,
trackEdit: handleTrackEdit,
trackSeek: handleTrackClick,
trackSelect: handleSelectTrack,
trackSelectNext: handleSelectNext,
updateRectBounds: handleUpdateRectBounds,
updateGeoJSON: handleUpdateGeoJSON,
removeTrack: handleRemoveTrack,
removePoint: handleRemovePoint,
removeAnnotation: handleRemoveAnnotation,
removeGroup: handleRemoveGroup,
selectFeatureHandle: handleSelectFeatureHandle,
setAnnotationState: handleSetAnnotationState,
unstageFromMerge: handleUnstageFromMerge,
startLinking: handleStartLinking,
stopLinking: handleStopLinking,
seekFrame,
},
};
}