Skip to content

Commit

Permalink
fix: Review code, modular code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Original-Recipe committed Sep 23, 2024
1 parent 9ff3c39 commit cb8b5c8
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ import { IPointCloud2DRectOperationViewRect } from '@labelbee/lb-utils';
import EKeyCode from '@/constant/keyCode';
import { RectOperation } from './rectOperation';
import reCalcRect from './utils/reCalcRect';
import AxisUtils from '@/utils/tool/AxisUtils';

class PointCloud2DRectOperation extends RectOperation {
// Whether it is in check mode
public checkMode?: Boolean;

public highLightRectList: IRect[] = [];

constructor(props: any) {
super(props);
this.checkMode = props.checkMode;
this.highLightRectList = [];
}

// Set highlighted point cloud 2D rectangular box list
public setHighLightRectList(rectList: IRect[]) {
this.highLightRectList = rectList;
}

// Disable creating new rect in checkMode
Expand Down Expand Up @@ -87,12 +96,36 @@ class PointCloud2DRectOperation extends RectOperation {
rect: IPointCloud2DRectOperationViewRect & IRect,
zoom = this.zoom,
isZoom = false,
extraHightFlag = false,
isPointCloud2DHighlight = false,
) {
// 是否使用强制默认值,如: lineDash: [3]
const shouldNotUseForceValue = rect?.boxID || rect?.lineDash;

super.renderDrawingRect(shouldNotUseForceValue ? rect : { ...rect, lineDash: [3] }, zoom, isZoom, extraHightFlag);
super.renderDrawingRect(
shouldNotUseForceValue ? rect : { ...rect, lineDash: [3] },
zoom,
isZoom,
isPointCloud2DHighlight,
);
}

// The rendering function actually triggers the rendering of the parent class in the end
public renderPointCloud2DHighlight(): void {
const { renderEnhance = {} } = this;

if (this.highLightRectList) {
this.highLightRectList.forEach((rect: IRect) => {
this.renderDrawingRect(rect as IPointCloud2DRectOperationViewRect & IRect, this.zoom, false, true);

if (renderEnhance.selectedRender) {
renderEnhance.selectedRender(
this.canvas,
AxisUtils.changeRectByZoom(rect, this.zoom, this.currentPos),
this.getRenderStyle(rect),
);
}
});
}
}

/*
Expand Down
27 changes: 5 additions & 22 deletions packages/lb-annotation/src/core/toolOperation/rectOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ class RectOperation extends BasicToolOperation {

public _textAttributeInstance?: TextAttributeClass;

public highLightRectList: IRect[] = [];

private _drawOutSideTarget: boolean; // 是否能在边界外进行标注

private selection: Selection;
Expand All @@ -73,6 +71,8 @@ class RectOperation extends BasicToolOperation {
/** Whether or not add rect */
private enableAddRect = true;

public renderPointCloud2DHighlight(): void {}

constructor(props: IRectOperationProps) {
super(props);
this._drawOutSideTarget = props.drawOutSideTarget || false;
Expand All @@ -91,11 +91,6 @@ class RectOperation extends BasicToolOperation {
this.setSelectedID = this.setSelectedID.bind(this);
this.updateSelectedRectSubAttribute = this.updateSelectedRectSubAttribute.bind(this);
this.selection = new Selection(this);
this.highLightRectList = [];
}

public setHighLightRectList(rectList: IRect[]) {
this.highLightRectList = rectList;
}

public setResult(rectList: IRect[]) {
Expand Down Expand Up @@ -1653,7 +1648,7 @@ class RectOperation extends BasicToolOperation {
* @param zoom 缩放比例
* @param isZoom 是否进行缩放
*/
public renderDrawingRect(rect: IRect, zoom = this.zoom, isZoom = false, extraHightFlag = false) {
public renderDrawingRect(rect: IRect, zoom = this.zoom, isZoom = false, isPointCloud2DHighlight = false) {
if (this.ctx && rect) {
const { ctx, style } = this;
// 不看图形信息
Expand Down Expand Up @@ -1723,7 +1718,7 @@ class RectOperation extends BasicToolOperation {
isSameTextAttribute ||
rect.id === this.selectedRectID ||
this.isMultiMoveMode ||
extraHightFlag
isPointCloud2DHighlight
) {
DrawUtils.drawRectWithFill(this.canvas, transformRect, { color: fillColor });
}
Expand Down Expand Up @@ -1827,19 +1822,7 @@ class RectOperation extends BasicToolOperation {
}

// After selecting the top view, it is necessary to highlight the 2D box of the corresponding 2D view
if (this.highLightRectList) {
this.highLightRectList.forEach((rect) => {
this.renderDrawingRect(rect, this.zoom, false, true);

if (renderEnhance.selectedRender) {
renderEnhance.selectedRender(
this.canvas,
AxisUtils.changeRectByZoom(rect, this.zoom, this.currentPos),
this.getRenderStyle(rect),
);
}
});
}
this.renderPointCloud2DHighlight();
}

/**
Expand Down
9 changes: 9 additions & 0 deletions packages/lb-components/src/store/toolConfig/baseToolConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { StateCreator } from 'zustand';
import { BaseStore } from './types';

const baseToolStateCreator: StateCreator<BaseStore> = (set, get) => ({
onlyLoadFirstData: false,
setOnlyLoadFirstData: (onlyLoadFirstData) => set((state) => ({ onlyLoadFirstData })),
});

export default baseToolStateCreator;
19 changes: 6 additions & 13 deletions packages/lb-components/src/store/toolConfig/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { create } from 'zustand';
import { ToolConfigStore } from './types';
import baseToolStateCreator from './baseToolConfig';
import pointCloudToolStateCreator from './pointCloudToolConfig';

interface toolConfigStore {
onlyLoadFirstData: boolean;
selectBoxVisibleSwitch: boolean;
setOnlyLoadFirstData: (onlyLoadFirstData: boolean) => void;
setSelectBoxVisibleSwitch: (selectBoxVisibleSwitch: boolean) => void;
}

const useToolConfigStore = create<toolConfigStore>((set) => ({
onlyLoadFirstData: false,
selectBoxVisibleSwitch: false,
setOnlyLoadFirstData: (onlyLoadFirstData) => set((state) => ({ onlyLoadFirstData })),
setSelectBoxVisibleSwitch: (selectBoxVisibleSwitch) =>
set((state) => ({ selectBoxVisibleSwitch })),
const useToolConfigStore = create<ToolConfigStore>((set, get, api) => ({
...baseToolStateCreator(set, get, api),
...pointCloudToolStateCreator(set, get, api),
}));

export default useToolConfigStore;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { StateCreator } from 'zustand';
import { PointCloudToolConfigStore } from './types';

const pointCloudToolStateCreator: StateCreator<PointCloudToolConfigStore> = (set, get) => ({
selectBoxVisibleSwitch: false,
setSelectBoxVisibleSwitch: (selectBoxVisibleSwitch: boolean) =>
set(() => ({ selectBoxVisibleSwitch })),
});

export default pointCloudToolStateCreator;
14 changes: 14 additions & 0 deletions packages/lb-components/src/store/toolConfig/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Define basic generic types that will be used by all state modules
export interface BaseStore {
onlyLoadFirstData: boolean;
setOnlyLoadFirstData: (onlyLoadFirstData: boolean) => void;
}

// Define point cloud type: PointCloudToolConfig Store
export interface PointCloudToolConfigStore {
selectBoxVisibleSwitch: boolean;
setSelectBoxVisibleSwitch: (selectBoxVisibleSwitch: boolean) => void;
}

// Import all types into toolConfig Store through inheritance
export interface ToolConfigStore extends BaseStore, PointCloudToolConfigStore {}

0 comments on commit cb8b5c8

Please sign in to comment.