Skip to content

Commit

Permalink
refactor: Replace toggleLabelOnId
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrimpCryptid committed Jan 6, 2025
1 parent be2d956 commit 91d6b68
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
43 changes: 22 additions & 21 deletions src/colorizer/AnnotationData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export interface IAnnotationDataGetters {
*/
getLabelsAppliedToId(id: number): number[];

/**
* Returns whether the label has been applied to the object ID.
* @param labelIdx The index of the label to look up.
* @param id The object ID.
* @returns `true` if the label has been applied to the object ID, `false`
* otherwise.
*/
getLabelOnId(labelIdx: number, id: number): boolean;

/**
* Returns all object IDs that the label has been applied to.
* @param labelIdx The index of the label to look up object IDs for.
Expand Down Expand Up @@ -83,9 +92,7 @@ export interface IAnnotationDataSetters {
setLabelColor(labelIdx: number, color: Color): void;
deleteLabel(labelIdx: number): void;

applyLabelToId(labelIdx: number, id: number): void;
removeLabelFromId(labelIdx: number, id: number): void;
toggleLabelOnId(labelIdx: number, id: number): void;
setLabelOnId(labelIdx: number, id: number, value: boolean): void;
}

export type IAnnotationData = IAnnotationDataGetters & IAnnotationDataSetters;
Expand Down Expand Up @@ -113,9 +120,8 @@ export class AnnotationData implements IAnnotationDataGetters, IAnnotationDataSe
this.setLabelName = this.setLabelName.bind(this);
this.setLabelColor = this.setLabelColor.bind(this);
this.deleteLabel = this.deleteLabel.bind(this);
this.toggleLabelOnId = this.toggleLabelOnId.bind(this);
this.applyLabelToId = this.applyLabelToId.bind(this);
this.removeLabelFromId = this.removeLabelFromId.bind(this);
this.getLabelOnId = this.getLabelOnId.bind(this);
this.setLabelOnId = this.setLabelOnId.bind(this);
}

// Getters
Expand All @@ -124,6 +130,11 @@ export class AnnotationData implements IAnnotationDataGetters, IAnnotationDataSe
return [...this.labelData];
}

getLabelOnId(labelIdx: number, id: number): boolean {
this.validateIndex(labelIdx);
return this.labelData[labelIdx].ids.has(id);
}

getLabelsAppliedToId(id: number): number[] {
const labelIdxs: number[] = [];
for (let i = 0; i < this.labelData.length; i++) {
Expand Down Expand Up @@ -210,23 +221,13 @@ export class AnnotationData implements IAnnotationDataGetters, IAnnotationDataSe
this.timeToLabelIdMap = null;
}

applyLabelToId(labelIdx: number, id: number): void {
this.validateIndex(labelIdx);
this.labelData[labelIdx].ids.add(id);
this.timeToLabelIdMap = null;
}

removeLabelFromId(labelIdx: number, id: number): void {
setLabelOnId(labelIdx: number, id: number, value: boolean): void {
this.validateIndex(labelIdx);
this.labelData[labelIdx].ids.delete(id);
this.timeToLabelIdMap = null;
}

toggleLabelOnId(labelIdx: number, id: number): void {
if (!this.labelData[labelIdx].ids.has(id)) {
this.applyLabelToId(labelIdx, id);
if (value) {
this.labelData[labelIdx].ids.add(id);
} else {
this.removeLabelFromId(labelIdx, id);
this.labelData[labelIdx].ids.delete(id);
}
this.timeToLabelIdMap = null;
}
}
8 changes: 3 additions & 5 deletions src/colorizer/utils/react_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ export const useAnnotations = (): AnnotationState => {
} else if (currentLabelIdx === labelIdx) {
setCurrentLabelIdx(null);
setIsAnnotationEnabled(false);
}else if (currentLabelIdx > labelIdx) {
} else if (currentLabelIdx > labelIdx) {
// Decrement because all indices will shift over
setCurrentLabelIdx(currentLabelIdx - 1);
}
Expand All @@ -385,6 +385,7 @@ export const useAnnotations = (): AnnotationState => {
getLabelsAppliedToId: annotationData.getLabelsAppliedToId,
getLabeledIds: annotationData.getLabeledIds,
getTimeToLabelIdMap: annotationData.getTimeToLabelIdMap,
getLabelOnId: annotationData.getLabelOnId,
})
, [dataUpdateCounter]);

Expand All @@ -403,9 +404,6 @@ export const useAnnotations = (): AnnotationState => {
setLabelName: wrapFunctionInUpdate(annotationData.setLabelName),
setLabelColor: wrapFunctionInUpdate(annotationData.setLabelColor),
deleteLabel: wrapFunctionInUpdate(onDeleteLabel),

applyLabelToId: wrapFunctionInUpdate(annotationData.applyLabelToId),
removeLabelFromId: wrapFunctionInUpdate(annotationData.removeLabelFromId),
toggleLabelOnId: wrapFunctionInUpdate(annotationData.toggleLabelOnId),
setLabelOnId: wrapFunctionInUpdate(annotationData.setLabelOnId),
};
};

0 comments on commit 91d6b68

Please sign in to comment.