@labelbee/lb-annotation 1.13.0 → 1.13.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,736 @@
1
+ import CommonToolUtils from '../../utils/tool/CommonToolUtils.js';
2
+ import AxisUtils from '../../utils/tool/AxisUtils.js';
3
+ import uuid from '../../utils/uuid.js';
4
+ import { getCuboidHoverRange, getPlainPointsByDiagonalPoints, getPointsByBottomRightPoint, getCuboidDragMove, isCuboidWithInLimits, getHighlightPoints, getCuboidTextAttributeOffset, getToggleDirectionButtonOffset } from '../../utils/tool/CuboidUtils.js';
5
+ import PolygonUtils from '../../utils/tool/PolygonUtils.js';
6
+ import { EDragStatus, EDragTarget, ECuboidDirection } from '../../constant/annotation.js';
7
+ import AttributeUtils from '../../utils/tool/AttributeUtils.js';
8
+ import { DEFAULT_TEXT_MAX_WIDTH } from '../../constant/tool.js';
9
+ import EKeyCode from '../../constant/keyCode.js';
10
+ import { BasicToolOperation } from './basicToolOperation.js';
11
+ import DrawUtils from '../../utils/tool/DrawUtils.js';
12
+ import CuboidToggleButtonClass from './cuboidToggleButtonClass.js';
13
+ import TextAttributeClass from './textAttributeClass.js';
14
+ import Locale from '../../locales/index.js';
15
+ import { EMessage } from '../../locales/constants.js';
16
+
17
+ var __defProp = Object.defineProperty;
18
+ var __defProps = Object.defineProperties;
19
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
20
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
21
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
22
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
23
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, {enumerable: true, configurable: true, writable: true, value}) : obj[key] = value;
24
+ var __spreadValues = (a, b) => {
25
+ for (var prop in b || (b = {}))
26
+ if (__hasOwnProp.call(b, prop))
27
+ __defNormalProp(a, prop, b[prop]);
28
+ if (__getOwnPropSymbols)
29
+ for (var prop of __getOwnPropSymbols(b)) {
30
+ if (__propIsEnum.call(b, prop))
31
+ __defNormalProp(a, prop, b[prop]);
32
+ }
33
+ return a;
34
+ };
35
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
36
+ var EDrawingStatus;
37
+ (function(EDrawingStatus2) {
38
+ EDrawingStatus2[EDrawingStatus2["Ready"] = 1] = "Ready";
39
+ EDrawingStatus2[EDrawingStatus2["FirstPoint"] = 2] = "FirstPoint";
40
+ EDrawingStatus2[EDrawingStatus2["Cuboid"] = 3] = "Cuboid";
41
+ })(EDrawingStatus || (EDrawingStatus = {}));
42
+ const TEXT_MAX_WIDTH = 164;
43
+ class CuboidOperation extends BasicToolOperation {
44
+ constructor(props) {
45
+ super(props);
46
+ this.drawingStatus = 1;
47
+ this.cuboidList = [];
48
+ this.hoverID = "";
49
+ this.getHoverData = (e) => {
50
+ const coordinate = this.getCoordinateUnderZoom(e);
51
+ const {currentShowList} = this;
52
+ if ((currentShowList == null ? void 0 : currentShowList.length) > 0) {
53
+ const polygonList = currentShowList.map((cuboid) => {
54
+ return {id: cuboid.id, pointList: AxisUtils.changePointListByZoom(getCuboidHoverRange(cuboid), this.zoom)};
55
+ });
56
+ const hoverID = PolygonUtils.getHoverPolygonID(coordinate, polygonList);
57
+ if (hoverID) {
58
+ return {
59
+ hoverID,
60
+ hoverCuboid: currentShowList.find((cuboid) => cuboid.id === hoverID)
61
+ };
62
+ }
63
+ }
64
+ return {};
65
+ };
66
+ this.textChange = (v) => {
67
+ if (this.config.textConfigurable === false || !this.selectedID) {
68
+ return;
69
+ }
70
+ this.setCuboidList(AttributeUtils.textChange(v, this.selectedID, this.cuboidList));
71
+ this.emit("selectedChange");
72
+ this.render();
73
+ };
74
+ this.config = CommonToolUtils.jsonParser(props.config);
75
+ this.getCurrentSelectedData = this.getCurrentSelectedData.bind(this);
76
+ this.updateSelectedTextAttribute = this.updateSelectedTextAttribute.bind(this);
77
+ }
78
+ destroy() {
79
+ super.destroy();
80
+ if (this._textAttributeInstance) {
81
+ this._textAttributeInstance.clearTextAttribute();
82
+ }
83
+ }
84
+ get currentShowList() {
85
+ let cuboidList = [];
86
+ const [showingCuboid, selectedCuboid] = CommonToolUtils.getRenderResultList(this.cuboidList, CommonToolUtils.getSourceID(this.basicResult), this.attributeLockList, this.selectedID);
87
+ cuboidList = showingCuboid;
88
+ if (this.isHidden) {
89
+ cuboidList = [];
90
+ }
91
+ if (selectedCuboid) {
92
+ cuboidList.push(selectedCuboid);
93
+ }
94
+ return cuboidList;
95
+ }
96
+ get selectedCuboid() {
97
+ return this.cuboidList.find((v) => v.id === this.selectedID);
98
+ }
99
+ get dataList() {
100
+ return this.cuboidList;
101
+ }
102
+ get isNeedCheckCuboidSize() {
103
+ var _a, _b;
104
+ return ((_a = this.config) == null ? void 0 : _a.minWidth) >= 0 && ((_b = this.config) == null ? void 0 : _b.minHeight) >= 0;
105
+ }
106
+ getIsHoverSelectedCuboid(e) {
107
+ const currentCoord = this.getCoordinateUnderZoom(e);
108
+ const {selectedCuboid} = this;
109
+ return selectedCuboid && AxisUtils.isCloseCuboid(currentCoord, AxisUtils.changeCuboidByZoom(selectedCuboid, this.zoom));
110
+ }
111
+ isForbiddenMove(e, cuboid) {
112
+ const coord = this.getCoordinateInOrigin(e);
113
+ if (coord.y > cuboid.frontPoints.br.y) {
114
+ return true;
115
+ }
116
+ return false;
117
+ }
118
+ updateSelectedCuboid(newCuboid) {
119
+ this.cuboidList = this.cuboidList.map((cuboid) => {
120
+ if (cuboid.id === this.selectedID) {
121
+ return newCuboid;
122
+ }
123
+ return cuboid;
124
+ });
125
+ }
126
+ getColorToRender(attribute, valid) {
127
+ const toolColor = this.getColor(attribute);
128
+ const strokeColor = valid ? toolColor == null ? void 0 : toolColor.valid.stroke : toolColor == null ? void 0 : toolColor.invalid.stroke;
129
+ const fillColor = valid ? toolColor == null ? void 0 : toolColor.valid.fill : toolColor == null ? void 0 : toolColor.invalid.fill;
130
+ return {strokeColor, toolColor, fillColor};
131
+ }
132
+ getCurrentSelectedData() {
133
+ const {selectedCuboid} = this;
134
+ if (!selectedCuboid) {
135
+ return;
136
+ }
137
+ const {strokeColor: color} = this.getColorToRender(selectedCuboid.attribute, selectedCuboid.valid);
138
+ return {
139
+ width: TEXT_MAX_WIDTH,
140
+ textAttribute: selectedCuboid.textAttribute,
141
+ color
142
+ };
143
+ }
144
+ updateSelectedTextAttribute(newTextAttribute) {
145
+ if (this._textAttributeInstance && newTextAttribute && this.selectedID) {
146
+ let textAttribute = newTextAttribute;
147
+ if (AttributeUtils.textAttributeValidate(this.config.textCheckType, "", textAttribute) === false) {
148
+ this.emit("messageError", AttributeUtils.getErrorNotice(this.config.textCheckType, this.lang));
149
+ textAttribute = "";
150
+ }
151
+ this.setCuboidList(AttributeUtils.textChange(textAttribute, this.selectedID, this.cuboidList));
152
+ this.history.pushHistory(this.cuboidList);
153
+ this.emit("updateTextAttribute");
154
+ this.render();
155
+ }
156
+ }
157
+ setResult(cuboidList) {
158
+ this.clearDrawingStatus();
159
+ this.setCuboidList(cuboidList);
160
+ this.render();
161
+ }
162
+ get currentPageResult() {
163
+ return this.cuboidList;
164
+ }
165
+ clearResult() {
166
+ this.setCuboidList([], true);
167
+ this.setSelectedID(void 0);
168
+ }
169
+ exportData() {
170
+ const {cuboidList} = this;
171
+ return [cuboidList, this.basicImgInfo];
172
+ }
173
+ setSelectedID(newID) {
174
+ var _a, _b;
175
+ const oldID = this.selectedID;
176
+ if (newID !== oldID && oldID) {
177
+ (_a = this._textAttributeInstance) == null ? void 0 : _a.changeSelected();
178
+ }
179
+ if (!newID) {
180
+ (_b = this._textAttributeInstance) == null ? void 0 : _b.clearTextAttribute();
181
+ }
182
+ this.selectedID = newID;
183
+ this.emit("selectedChange");
184
+ }
185
+ setSelectedIDAndRender(newID) {
186
+ this.setSelectedID(newID);
187
+ this.render();
188
+ }
189
+ setCuboidValidAndRender(id) {
190
+ if (!id) {
191
+ return;
192
+ }
193
+ const newPolygonList = this.cuboidList.map((cuboid) => {
194
+ if (cuboid.id === id) {
195
+ return __spreadProps(__spreadValues({}, cuboid), {
196
+ valid: !cuboid.valid
197
+ });
198
+ }
199
+ return cuboid;
200
+ });
201
+ this.setCuboidList(newPolygonList, true);
202
+ this.history.pushHistory(this.cuboidList);
203
+ this.render();
204
+ }
205
+ onRightDblClick(e) {
206
+ super.onRightDblClick(e);
207
+ const {hoverID} = this.getHoverData(e);
208
+ if (this.selectedID && this.selectedID === hoverID) {
209
+ this.deleteCuboid(hoverID);
210
+ }
211
+ }
212
+ setCuboidList(cuboidList, isUpload = false) {
213
+ const oldLen = this.cuboidList.length;
214
+ this.cuboidList = cuboidList;
215
+ if (oldLen !== cuboidList.length) {
216
+ this.emit("updatePageNumber");
217
+ }
218
+ if (isUpload) {
219
+ this.emit("updateResult");
220
+ }
221
+ }
222
+ deleteCuboid(id) {
223
+ var _a;
224
+ if (!id) {
225
+ return;
226
+ }
227
+ this.setCuboidList(this.cuboidList.filter((v) => v.id !== id));
228
+ this.history.pushHistory(this.cuboidList);
229
+ this.setSelectedID("");
230
+ (_a = this._textAttributeInstance) == null ? void 0 : _a.clearTextAttribute();
231
+ this.render();
232
+ }
233
+ onKeyDown(e) {
234
+ if (!CommonToolUtils.hotkeyFilter(e)) {
235
+ return;
236
+ }
237
+ if (super.onKeyDown(e) === false) {
238
+ return;
239
+ }
240
+ const {keyCode} = e;
241
+ switch (keyCode) {
242
+ case EKeyCode.Ctrl:
243
+ if (this.drawingCuboid) {
244
+ this.drawingCuboid = __spreadProps(__spreadValues({}, this.drawingCuboid), {
245
+ valid: false
246
+ });
247
+ this.render();
248
+ }
249
+ break;
250
+ case EKeyCode.F:
251
+ if (this.selectedID) {
252
+ this.setCuboidValidAndRender(this.selectedID);
253
+ }
254
+ break;
255
+ default: {
256
+ if (this.config.attributeConfigurable) {
257
+ const keyCode2Attribute = AttributeUtils.getAttributeByKeycode(keyCode, this.config.attributeList);
258
+ if (keyCode2Attribute !== void 0) {
259
+ this.setDefaultAttribute(keyCode2Attribute);
260
+ }
261
+ }
262
+ }
263
+ }
264
+ }
265
+ onKeyUp(e) {
266
+ super.onKeyUp(e);
267
+ switch (e.keyCode) {
268
+ case EKeyCode.Ctrl:
269
+ if (this.drawingCuboid) {
270
+ this.drawingCuboid = __spreadProps(__spreadValues({}, this.drawingCuboid), {
271
+ valid: true
272
+ });
273
+ this.render();
274
+ }
275
+ break;
276
+ }
277
+ }
278
+ onMouseDown(e) {
279
+ if (super.onMouseDown(e) || this.forbidMouseOperation || e.ctrlKey === true) {
280
+ return;
281
+ }
282
+ const {selectedCuboid} = this;
283
+ if (!selectedCuboid || e.button === 2 || e.button === 0 && this.isSpaceKey === true) {
284
+ return;
285
+ }
286
+ if (!this.getIsHoverSelectedCuboid(e)) {
287
+ return;
288
+ }
289
+ this.dragStatus = EDragStatus.Start;
290
+ const dragStartCoord = this.getCoordinateUnderZoom(e);
291
+ const DEFAULT_DRAG_INFO = {
292
+ initCuboid: selectedCuboid,
293
+ dragStartCoord
294
+ };
295
+ const highlightInfo = AxisUtils.returnClosePointOrLineInCuboid(dragStartCoord, AxisUtils.changeCuboidByZoom(selectedCuboid, this.zoom));
296
+ const firstHighlightInfo = highlightInfo == null ? void 0 : highlightInfo[0];
297
+ switch (firstHighlightInfo == null ? void 0 : firstHighlightInfo.type) {
298
+ case "point":
299
+ this.dragInfo = __spreadProps(__spreadValues({}, DEFAULT_DRAG_INFO), {
300
+ dragTarget: EDragTarget.Point,
301
+ positions: firstHighlightInfo.positions
302
+ });
303
+ break;
304
+ case "line":
305
+ this.dragInfo = __spreadProps(__spreadValues({}, DEFAULT_DRAG_INFO), {
306
+ dragTarget: EDragTarget.Line,
307
+ positions: firstHighlightInfo.positions
308
+ });
309
+ break;
310
+ default: {
311
+ this.dragInfo = __spreadProps(__spreadValues({}, DEFAULT_DRAG_INFO), {
312
+ dragTarget: EDragTarget.Cuboid
313
+ });
314
+ }
315
+ }
316
+ }
317
+ onMouseUp(e) {
318
+ if (super.onMouseUp(e) || this.forbidMouseOperation || !this.imgInfo) {
319
+ return void 0;
320
+ }
321
+ if (this.dragInfo && this.dragStatus === EDragStatus.Move) {
322
+ this.dragInfo = void 0;
323
+ this.dragStatus = EDragStatus.Wait;
324
+ this.history.pushHistory(this.cuboidList);
325
+ this.emit("updateResult");
326
+ return;
327
+ }
328
+ const basicSourceID = CommonToolUtils.getSourceID(this.basicResult);
329
+ if (e.button === 0) {
330
+ if (this.hoverID && e.ctrlKey && !this.drawingCuboid) {
331
+ this.setCuboidValidAndRender(this.hoverID);
332
+ return;
333
+ }
334
+ if (!this.drawingCuboid) {
335
+ this.createNewDrawingCuboid(e, basicSourceID);
336
+ return;
337
+ }
338
+ if (this.drawingCuboid) {
339
+ switch (this.drawingStatus) {
340
+ case 2:
341
+ this.closeNewDrawingFrontPlane();
342
+ break;
343
+ case 3:
344
+ this.closeAndCreateNewCuboid();
345
+ break;
346
+ }
347
+ }
348
+ }
349
+ if (e.button === 2) {
350
+ this.rightMouseUp(e);
351
+ }
352
+ }
353
+ onMouseMove(e) {
354
+ var _a;
355
+ if (super.onMouseMove(e) || this.forbidMouseOperation || !this.imgInfo) {
356
+ return;
357
+ }
358
+ if (this.selectedID && this.dragInfo) {
359
+ this.onDragMove(e);
360
+ return;
361
+ }
362
+ if (this.drawingCuboid) {
363
+ if (this.drawingFrontPlanesMove(e)) {
364
+ return;
365
+ }
366
+ this.drawingBackPlaneMove(e);
367
+ return;
368
+ }
369
+ this.hoverID = (_a = this.getHoverData(e).hoverID) != null ? _a : "";
370
+ this.onHoverMove(e);
371
+ }
372
+ drawingFrontPlanesMove(e) {
373
+ if (this.drawingCuboid && this.firstClickCoord && this.drawingStatus === 2) {
374
+ const coord = this.getCoordinateInOrigin(e);
375
+ this.drawingCuboid = __spreadProps(__spreadValues({}, this.drawingCuboid), {
376
+ frontPoints: getPlainPointsByDiagonalPoints(this.firstClickCoord, coord)
377
+ });
378
+ this.render();
379
+ return true;
380
+ }
381
+ }
382
+ drawingBackPlaneMove(e) {
383
+ if (this.drawingCuboid && this.firstClickCoord && this.drawingStatus === 3) {
384
+ const coord = this.getCoordinateInOrigin(e);
385
+ if (this.isForbiddenMove(e, this.drawingCuboid)) {
386
+ return;
387
+ }
388
+ this.drawingCuboid = __spreadProps(__spreadValues({}, this.drawingCuboid), {
389
+ backPoints: getPointsByBottomRightPoint({coord, points: this.drawingCuboid.frontPoints})
390
+ });
391
+ this.render();
392
+ }
393
+ }
394
+ onDragMove(e) {
395
+ if (!this.dragInfo || !this.selectedID) {
396
+ return;
397
+ }
398
+ const {dragTarget, initCuboid, dragStartCoord, positions} = this.dragInfo;
399
+ const coordinate = this.getCoordinateUnderZoom(e);
400
+ const offset = {
401
+ x: (coordinate.x - dragStartCoord.x) / this.zoom,
402
+ y: (coordinate.y - dragStartCoord.y) / this.zoom
403
+ };
404
+ this.dragStatus = EDragStatus.Move;
405
+ const newCuboid = getCuboidDragMove({offset, cuboid: initCuboid, dragTarget, positions});
406
+ if ((newCuboid == null ? void 0 : newCuboid.backPoints) && (newCuboid == null ? void 0 : newCuboid.backPoints.br.y) > (newCuboid == null ? void 0 : newCuboid.frontPoints.br.y)) {
407
+ return;
408
+ }
409
+ if (newCuboid) {
410
+ this.updateSelectedCuboid(newCuboid);
411
+ }
412
+ this.render();
413
+ }
414
+ onHoverMove(e) {
415
+ const {selectedCuboid} = this;
416
+ if (selectedCuboid) {
417
+ const currentCoord = this.getCoordinateUnderZoom(e);
418
+ const highlightInfo = AxisUtils.returnClosePointOrLineInCuboid(currentCoord, AxisUtils.changeCuboidByZoom(selectedCuboid, this.zoom), {
419
+ zoom: 1 / this.zoom,
420
+ scope: 5
421
+ });
422
+ this.highlightInfo = highlightInfo;
423
+ this.render();
424
+ }
425
+ }
426
+ createNewDrawingCuboid(e, basicSourceID) {
427
+ if (!this.imgInfo) {
428
+ return;
429
+ }
430
+ const coordinate = this.getCoordinateInOrigin(e);
431
+ this.drawingCuboid = {
432
+ attribute: this.defaultAttribute,
433
+ direction: ECuboidDirection.Front,
434
+ valid: !e.ctrlKey,
435
+ id: uuid(8, 62),
436
+ sourceID: basicSourceID,
437
+ textAttribute: "",
438
+ order: CommonToolUtils.getMaxOrder(this.cuboidList.filter((v) => CommonToolUtils.isSameSourceID(v.sourceID, basicSourceID))) + 1,
439
+ frontPoints: {
440
+ tl: coordinate,
441
+ bl: coordinate,
442
+ tr: coordinate,
443
+ br: coordinate
444
+ }
445
+ };
446
+ this.firstClickCoord = __spreadValues({}, coordinate);
447
+ this.drawingStatus = 2;
448
+ }
449
+ closeNewDrawingFrontPlane() {
450
+ this.drawingStatus = 3;
451
+ }
452
+ closeAndCreateNewCuboid() {
453
+ var _a, _b;
454
+ if (!((_a = this.drawingCuboid) == null ? void 0 : _a.frontPoints) || !this.drawingCuboid.backPoints) {
455
+ return;
456
+ }
457
+ if (this.isNeedCheckCuboidSize && isCuboidWithInLimits({cuboid: this.drawingCuboid, config: this.config}) === false) {
458
+ this.emit("messageInfo", Locale.getMessagesByLocale(EMessage.RectErrorSizeNotice, this.lang));
459
+ } else {
460
+ this.setCuboidList([...this.cuboidList, this.drawingCuboid]);
461
+ this.setSelectedID((_b = this.drawingCuboid) == null ? void 0 : _b.id);
462
+ this.history.pushHistory(this.cuboidList);
463
+ }
464
+ this.clearDrawingStatus();
465
+ this.render();
466
+ }
467
+ clearDrawingStatus() {
468
+ if (this.drawingCuboid) {
469
+ this.drawingCuboid = void 0;
470
+ this.drawingStatus = 1;
471
+ }
472
+ }
473
+ rightMouseUp(e) {
474
+ const {hoverID, hoverCuboid} = this.getHoverData(e);
475
+ this.setSelectedID(hoverID);
476
+ if (hoverCuboid) {
477
+ this.setDefaultAttribute(hoverCuboid.attribute);
478
+ }
479
+ if (this.drawingCuboid) {
480
+ this.clearDrawingStatus();
481
+ }
482
+ this.render();
483
+ }
484
+ renderSingleCuboid(cuboid) {
485
+ var _a, _b, _c, _d;
486
+ const transformCuboid = AxisUtils.changeCuboidByZoom(cuboid, this.zoom, this.currentPos);
487
+ const isHover = transformCuboid.id === this.hoverID;
488
+ const isSelected = transformCuboid.id === this.selectedID;
489
+ const {strokeColor, fillColor} = this.getColorToRender(transformCuboid.attribute, transformCuboid.valid);
490
+ const textColor = strokeColor;
491
+ const lineWidth = (_b = (_a = this.style) == null ? void 0 : _a.width) != null ? _b : 2;
492
+ const {hiddenText = false} = this.style;
493
+ const defaultStyle = {
494
+ color: strokeColor,
495
+ thickness: lineWidth
496
+ };
497
+ const {backPoints, frontPoints, textAttribute} = transformCuboid;
498
+ const frontPointsSizeWidth = frontPoints.br.x - frontPoints.bl.x;
499
+ DrawUtils.drawCuboid(this.canvas, transformCuboid, {strokeColor, fillColor, thickness: lineWidth});
500
+ if (isHover || isSelected) {
501
+ const hoverPointList = getHighlightPoints(transformCuboid);
502
+ hoverPointList.forEach((data) => {
503
+ DrawUtils.drawCircleWithFill(this.canvas, data.point, 5, __spreadValues({}, defaultStyle));
504
+ });
505
+ if (isSelected) {
506
+ hoverPointList.forEach((data) => {
507
+ DrawUtils.drawCircleWithFill(this.canvas, data.point, 3, {color: "white"});
508
+ });
509
+ }
510
+ }
511
+ let showText = "";
512
+ if (((_c = this.config) == null ? void 0 : _c.isShowOrder) && transformCuboid.order && (transformCuboid == null ? void 0 : transformCuboid.order) > 0) {
513
+ showText = `${transformCuboid.order}`;
514
+ }
515
+ if (transformCuboid.attribute) {
516
+ showText = `${showText} ${AttributeUtils.getAttributeShowText(transformCuboid.attribute, (_d = this.config) == null ? void 0 : _d.attributeList)}`;
517
+ }
518
+ if (!hiddenText && backPoints) {
519
+ DrawUtils.drawText(this.canvas, {x: backPoints.tl.x, y: backPoints.tl.y - 5}, showText, {
520
+ color: strokeColor,
521
+ textMaxWidth: 300
522
+ });
523
+ }
524
+ const textPosition = getCuboidTextAttributeOffset({
525
+ cuboid,
526
+ currentPos: this.currentPos,
527
+ zoom: this.zoom,
528
+ topOffset: 16,
529
+ leftOffset: 0
530
+ });
531
+ if (!hiddenText && textAttribute && cuboid.id !== this.selectedID) {
532
+ const textWidth = Math.max(20, frontPointsSizeWidth * 0.8);
533
+ DrawUtils.drawText(this.canvas, {x: textPosition.left, y: textPosition.top}, textAttribute, {
534
+ color: textColor,
535
+ textMaxWidth: textWidth
536
+ });
537
+ }
538
+ this.renderTextAttribute();
539
+ }
540
+ setDefaultAttribute(defaultAttribute) {
541
+ const oldDefault = this.defaultAttribute;
542
+ this.defaultAttribute = defaultAttribute != null ? defaultAttribute : "";
543
+ if (oldDefault !== defaultAttribute) {
544
+ this.changeStyle(defaultAttribute);
545
+ this.emit("changeAttributeSidebar");
546
+ const {selectedCuboid} = this;
547
+ if (selectedCuboid) {
548
+ this.setCuboidList(this.cuboidList.map((v) => {
549
+ if (v.id === this.selectedID) {
550
+ return __spreadProps(__spreadValues({}, v), {
551
+ attribute: this.defaultAttribute
552
+ });
553
+ }
554
+ return v;
555
+ }));
556
+ this.history.pushHistory(this.cuboidList);
557
+ this.render();
558
+ }
559
+ if (this.drawingCuboid) {
560
+ this.drawingCuboid = __spreadProps(__spreadValues({}, this.drawingCuboid), {
561
+ attribute: this.defaultAttribute
562
+ });
563
+ this.render();
564
+ }
565
+ if (this._textAttributeInstance) {
566
+ if (this.attributeLockList.length > 0 && !this.attributeLockList.includes(this.defaultAttribute)) {
567
+ this._textAttributeInstance.clearTextAttribute();
568
+ return;
569
+ }
570
+ this._textAttributeInstance.updateIcon(this.getTextIconSvg(defaultAttribute));
571
+ }
572
+ }
573
+ }
574
+ renderToggleButton() {
575
+ const {selectedCuboid} = this;
576
+ if (!this.ctx || this.config.textConfigurable !== true || !selectedCuboid) {
577
+ return;
578
+ }
579
+ const {attribute, valid} = selectedCuboid;
580
+ const {strokeColor: color} = this.getColorToRender(attribute, valid);
581
+ if (!this.toggleButtonInstance) {
582
+ this.toggleButtonInstance = new CuboidToggleButtonClass({
583
+ container: this.container,
584
+ cuboidButtonMove: (type) => this.updateMouseOperation(type),
585
+ toggleDirection: (direction) => this.toggleDirection(direction)
586
+ });
587
+ }
588
+ const toggleOffset = getToggleDirectionButtonOffset({
589
+ cuboid: selectedCuboid,
590
+ zoom: this.zoom,
591
+ currentPos: this.currentPos
592
+ });
593
+ this.toggleButtonInstance.update({
594
+ left: toggleOffset.left,
595
+ top: toggleOffset.top,
596
+ color
597
+ });
598
+ }
599
+ renderTextAttribute() {
600
+ var _a;
601
+ const {selectedCuboid} = this;
602
+ if (!this.ctx || this.config.textConfigurable === false || !selectedCuboid) {
603
+ return;
604
+ }
605
+ const {strokeColor: color} = this.getColorToRender(selectedCuboid.attribute, selectedCuboid.valid);
606
+ const {attribute, textAttribute, frontPoints} = selectedCuboid;
607
+ const offset = getCuboidTextAttributeOffset({
608
+ cuboid: selectedCuboid,
609
+ currentPos: this.currentPos,
610
+ zoom: this.zoom
611
+ });
612
+ const newWidth = (frontPoints.br.x - frontPoints.bl.x) * this.zoom * 0.8;
613
+ if (!this._textAttributeInstance) {
614
+ this._textAttributeInstance = new TextAttributeClass({
615
+ width: DEFAULT_TEXT_MAX_WIDTH,
616
+ container: this.container,
617
+ icon: this.getTextIconSvg(attribute),
618
+ color,
619
+ getCurrentSelectedData: this.getCurrentSelectedData,
620
+ updateSelectedTextAttribute: this.updateSelectedTextAttribute
621
+ });
622
+ }
623
+ if (this._textAttributeInstance && !((_a = this._textAttributeInstance) == null ? void 0 : _a.isExit)) {
624
+ this._textAttributeInstance.appendToContainer();
625
+ }
626
+ this._textAttributeInstance.update(`${textAttribute}`, {
627
+ left: offset.left,
628
+ top: offset.top,
629
+ color,
630
+ width: newWidth
631
+ });
632
+ }
633
+ renderDrawing() {
634
+ if (this.drawingCuboid) {
635
+ this.renderSingleCuboid(this.drawingCuboid);
636
+ }
637
+ }
638
+ renderStatic() {
639
+ this.cuboidList.forEach((cuboid) => this.renderSingleCuboid(cuboid));
640
+ }
641
+ renderSelected() {
642
+ var _a;
643
+ const {selectedCuboid} = this;
644
+ if (selectedCuboid) {
645
+ this.renderSingleCuboid(selectedCuboid);
646
+ this.renderToggleButton();
647
+ } else {
648
+ (_a = this.toggleButtonInstance) == null ? void 0 : _a.clearCuboidButtonDOM();
649
+ this.toggleButtonInstance = void 0;
650
+ }
651
+ }
652
+ updateMouseOperation(type) {
653
+ if (type === "in") {
654
+ this.setForbidCursorLine(true);
655
+ this.setForbidOperation(true);
656
+ this.setShowDefaultCursor(true);
657
+ } else {
658
+ this.setForbidCursorLine(false);
659
+ this.setShowDefaultCursor(false);
660
+ this.setForbidOperation(false);
661
+ }
662
+ }
663
+ toggleDirection(direction) {
664
+ if (this.cuboidList && this.selectedCuboid) {
665
+ this.setCuboidList(this.cuboidList.map((i) => {
666
+ var _a;
667
+ return i.id === ((_a = this.selectedCuboid) == null ? void 0 : _a.id) ? __spreadProps(__spreadValues({}, i), {direction}) : i;
668
+ }));
669
+ this.history.pushHistory(this.cuboidList);
670
+ this.render();
671
+ }
672
+ }
673
+ renderHover() {
674
+ var _a;
675
+ if (this.dragInfo) {
676
+ return;
677
+ }
678
+ (_a = this.highlightInfo) == null ? void 0 : _a.forEach((data) => {
679
+ var _a2, _b;
680
+ const {strokeColor} = this.getColorToRender(data.originCuboid.attribute, data.originCuboid.valid);
681
+ const thickness = 8;
682
+ switch (data.type) {
683
+ case "point":
684
+ (_a2 = data.points) == null ? void 0 : _a2.forEach((point) => {
685
+ DrawUtils.drawCircleWithFill(this.canvas, AxisUtils.changePointByZoom(point, this.zoom, this.currentPos), thickness, {
686
+ color: strokeColor
687
+ });
688
+ });
689
+ break;
690
+ case "line": {
691
+ const pointList = (_b = data.points) == null ? void 0 : _b.map((point) => AxisUtils.changePointByZoom(point, this.zoom, this.currentPos));
692
+ if (pointList) {
693
+ DrawUtils.drawLineWithPointList(this.canvas, pointList, {color: strokeColor, thickness});
694
+ }
695
+ break;
696
+ }
697
+ }
698
+ });
699
+ }
700
+ renderCuboid() {
701
+ this.renderStatic();
702
+ this.renderDrawing();
703
+ this.renderSelected();
704
+ this.renderHover();
705
+ }
706
+ render() {
707
+ if (!this.ctx) {
708
+ return;
709
+ }
710
+ super.render();
711
+ this.renderCuboid();
712
+ this.renderCursorLine(this.getLineColor(this.defaultAttribute));
713
+ }
714
+ undo() {
715
+ const cuboidList = this.history.undo();
716
+ if (cuboidList) {
717
+ if (cuboidList.length !== this.cuboidList.length) {
718
+ this.setSelectedID("");
719
+ }
720
+ this.setCuboidList(cuboidList, true);
721
+ this.render();
722
+ }
723
+ }
724
+ redo() {
725
+ const cuboidList = this.history.redo();
726
+ if (cuboidList) {
727
+ if (cuboidList.length !== this.cuboidList.length) {
728
+ this.setSelectedID("");
729
+ }
730
+ this.setCuboidList(cuboidList, true);
731
+ this.render();
732
+ }
733
+ }
734
+ }
735
+
736
+ export { CuboidOperation as default };