@labelbee/lb-annotation 1.12.0 → 1.12.1

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,733 @@
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
+ clearResult() {
163
+ this.setCuboidList([], true);
164
+ this.setSelectedID(void 0);
165
+ }
166
+ exportData() {
167
+ const {cuboidList} = this;
168
+ return [cuboidList, this.basicImgInfo];
169
+ }
170
+ setSelectedID(newID) {
171
+ var _a, _b;
172
+ const oldID = this.selectedID;
173
+ if (newID !== oldID && oldID) {
174
+ (_a = this._textAttributeInstance) == null ? void 0 : _a.changeSelected();
175
+ }
176
+ if (!newID) {
177
+ (_b = this._textAttributeInstance) == null ? void 0 : _b.clearTextAttribute();
178
+ }
179
+ this.selectedID = newID;
180
+ this.emit("selectedChange");
181
+ }
182
+ setSelectedIDAndRender(newID) {
183
+ this.setSelectedID(newID);
184
+ this.render();
185
+ }
186
+ setCuboidValidAndRender(id) {
187
+ if (!id) {
188
+ return;
189
+ }
190
+ const newPolygonList = this.cuboidList.map((cuboid) => {
191
+ if (cuboid.id === id) {
192
+ return __spreadProps(__spreadValues({}, cuboid), {
193
+ valid: !cuboid.valid
194
+ });
195
+ }
196
+ return cuboid;
197
+ });
198
+ this.setCuboidList(newPolygonList, true);
199
+ this.history.pushHistory(this.cuboidList);
200
+ this.render();
201
+ }
202
+ onRightDblClick(e) {
203
+ super.onRightDblClick(e);
204
+ const {hoverID} = this.getHoverData(e);
205
+ if (this.selectedID && this.selectedID === hoverID) {
206
+ this.deleteCuboid(hoverID);
207
+ }
208
+ }
209
+ setCuboidList(cuboidList, isUpload = false) {
210
+ const oldLen = this.cuboidList.length;
211
+ this.cuboidList = cuboidList;
212
+ if (oldLen !== cuboidList.length) {
213
+ this.emit("updatePageNumber");
214
+ }
215
+ if (isUpload) {
216
+ this.emit("updateResult");
217
+ }
218
+ }
219
+ deleteCuboid(id) {
220
+ var _a;
221
+ if (!id) {
222
+ return;
223
+ }
224
+ this.setCuboidList(this.cuboidList.filter((v) => v.id !== id));
225
+ this.history.pushHistory(this.cuboidList);
226
+ this.setSelectedID("");
227
+ (_a = this._textAttributeInstance) == null ? void 0 : _a.clearTextAttribute();
228
+ this.render();
229
+ }
230
+ onKeyDown(e) {
231
+ if (!CommonToolUtils.hotkeyFilter(e)) {
232
+ return;
233
+ }
234
+ if (super.onKeyDown(e) === false) {
235
+ return;
236
+ }
237
+ const {keyCode} = e;
238
+ switch (keyCode) {
239
+ case EKeyCode.Ctrl:
240
+ if (this.drawingCuboid) {
241
+ this.drawingCuboid = __spreadProps(__spreadValues({}, this.drawingCuboid), {
242
+ valid: false
243
+ });
244
+ this.render();
245
+ }
246
+ break;
247
+ case EKeyCode.F:
248
+ if (this.selectedID) {
249
+ this.setCuboidValidAndRender(this.selectedID);
250
+ }
251
+ break;
252
+ default: {
253
+ if (this.config.attributeConfigurable) {
254
+ const keyCode2Attribute = AttributeUtils.getAttributeByKeycode(keyCode, this.config.attributeList);
255
+ if (keyCode2Attribute !== void 0) {
256
+ this.setDefaultAttribute(keyCode2Attribute);
257
+ }
258
+ }
259
+ }
260
+ }
261
+ }
262
+ onKeyUp(e) {
263
+ super.onKeyUp(e);
264
+ switch (e.keyCode) {
265
+ case EKeyCode.Ctrl:
266
+ if (this.drawingCuboid) {
267
+ this.drawingCuboid = __spreadProps(__spreadValues({}, this.drawingCuboid), {
268
+ valid: true
269
+ });
270
+ this.render();
271
+ }
272
+ break;
273
+ }
274
+ }
275
+ onMouseDown(e) {
276
+ if (super.onMouseDown(e) || this.forbidMouseOperation || e.ctrlKey === true) {
277
+ return;
278
+ }
279
+ const {selectedCuboid} = this;
280
+ if (!selectedCuboid || e.button === 2 || e.button === 0 && this.isSpaceKey === true) {
281
+ return;
282
+ }
283
+ if (!this.getIsHoverSelectedCuboid(e)) {
284
+ return;
285
+ }
286
+ this.dragStatus = EDragStatus.Start;
287
+ const dragStartCoord = this.getCoordinateUnderZoom(e);
288
+ const DEFAULT_DRAG_INFO = {
289
+ initCuboid: selectedCuboid,
290
+ dragStartCoord
291
+ };
292
+ const highlightInfo = AxisUtils.returnClosePointOrLineInCuboid(dragStartCoord, AxisUtils.changeCuboidByZoom(selectedCuboid, this.zoom));
293
+ const firstHighlightInfo = highlightInfo == null ? void 0 : highlightInfo[0];
294
+ switch (firstHighlightInfo == null ? void 0 : firstHighlightInfo.type) {
295
+ case "point":
296
+ this.dragInfo = __spreadProps(__spreadValues({}, DEFAULT_DRAG_INFO), {
297
+ dragTarget: EDragTarget.Point,
298
+ positions: firstHighlightInfo.positions
299
+ });
300
+ break;
301
+ case "line":
302
+ this.dragInfo = __spreadProps(__spreadValues({}, DEFAULT_DRAG_INFO), {
303
+ dragTarget: EDragTarget.Line,
304
+ positions: firstHighlightInfo.positions
305
+ });
306
+ break;
307
+ default: {
308
+ this.dragInfo = __spreadProps(__spreadValues({}, DEFAULT_DRAG_INFO), {
309
+ dragTarget: EDragTarget.Cuboid
310
+ });
311
+ }
312
+ }
313
+ }
314
+ onMouseUp(e) {
315
+ if (super.onMouseUp(e) || this.forbidMouseOperation || !this.imgInfo) {
316
+ return void 0;
317
+ }
318
+ if (this.dragInfo && this.dragStatus === EDragStatus.Move) {
319
+ this.dragInfo = void 0;
320
+ this.dragStatus = EDragStatus.Wait;
321
+ this.history.pushHistory(this.cuboidList);
322
+ this.emit("updateResult");
323
+ return;
324
+ }
325
+ const basicSourceID = CommonToolUtils.getSourceID(this.basicResult);
326
+ if (e.button === 0) {
327
+ if (this.hoverID && e.ctrlKey && !this.drawingCuboid) {
328
+ this.setCuboidValidAndRender(this.hoverID);
329
+ return;
330
+ }
331
+ if (!this.drawingCuboid) {
332
+ this.createNewDrawingCuboid(e, basicSourceID);
333
+ return;
334
+ }
335
+ if (this.drawingCuboid) {
336
+ switch (this.drawingStatus) {
337
+ case 2:
338
+ this.closeNewDrawingFrontPlane();
339
+ break;
340
+ case 3:
341
+ this.closeAndCreateNewCuboid();
342
+ break;
343
+ }
344
+ }
345
+ }
346
+ if (e.button === 2) {
347
+ this.rightMouseUp(e);
348
+ }
349
+ }
350
+ onMouseMove(e) {
351
+ var _a;
352
+ if (super.onMouseMove(e) || this.forbidMouseOperation || !this.imgInfo) {
353
+ return;
354
+ }
355
+ if (this.selectedID && this.dragInfo) {
356
+ this.onDragMove(e);
357
+ return;
358
+ }
359
+ if (this.drawingCuboid) {
360
+ if (this.drawingFrontPlanesMove(e)) {
361
+ return;
362
+ }
363
+ this.drawingBackPlaneMove(e);
364
+ return;
365
+ }
366
+ this.hoverID = (_a = this.getHoverData(e).hoverID) != null ? _a : "";
367
+ this.onHoverMove(e);
368
+ }
369
+ drawingFrontPlanesMove(e) {
370
+ if (this.drawingCuboid && this.firstClickCoord && this.drawingStatus === 2) {
371
+ const coord = this.getCoordinateInOrigin(e);
372
+ this.drawingCuboid = __spreadProps(__spreadValues({}, this.drawingCuboid), {
373
+ frontPoints: getPlainPointsByDiagonalPoints(this.firstClickCoord, coord)
374
+ });
375
+ this.render();
376
+ return true;
377
+ }
378
+ }
379
+ drawingBackPlaneMove(e) {
380
+ if (this.drawingCuboid && this.firstClickCoord && this.drawingStatus === 3) {
381
+ const coord = this.getCoordinateInOrigin(e);
382
+ if (this.isForbiddenMove(e, this.drawingCuboid)) {
383
+ return;
384
+ }
385
+ this.drawingCuboid = __spreadProps(__spreadValues({}, this.drawingCuboid), {
386
+ backPoints: getPointsByBottomRightPoint({coord, points: this.drawingCuboid.frontPoints})
387
+ });
388
+ this.render();
389
+ }
390
+ }
391
+ onDragMove(e) {
392
+ if (!this.dragInfo || !this.selectedID) {
393
+ return;
394
+ }
395
+ const {dragTarget, initCuboid, dragStartCoord, positions} = this.dragInfo;
396
+ const coordinate = this.getCoordinateUnderZoom(e);
397
+ const offset = {
398
+ x: (coordinate.x - dragStartCoord.x) / this.zoom,
399
+ y: (coordinate.y - dragStartCoord.y) / this.zoom
400
+ };
401
+ this.dragStatus = EDragStatus.Move;
402
+ const newCuboid = getCuboidDragMove({offset, cuboid: initCuboid, dragTarget, positions});
403
+ if ((newCuboid == null ? void 0 : newCuboid.backPoints) && (newCuboid == null ? void 0 : newCuboid.backPoints.br.y) > (newCuboid == null ? void 0 : newCuboid.frontPoints.br.y)) {
404
+ return;
405
+ }
406
+ if (newCuboid) {
407
+ this.updateSelectedCuboid(newCuboid);
408
+ }
409
+ this.render();
410
+ }
411
+ onHoverMove(e) {
412
+ const {selectedCuboid} = this;
413
+ if (selectedCuboid) {
414
+ const currentCoord = this.getCoordinateUnderZoom(e);
415
+ const highlightInfo = AxisUtils.returnClosePointOrLineInCuboid(currentCoord, AxisUtils.changeCuboidByZoom(selectedCuboid, this.zoom), {
416
+ zoom: 1 / this.zoom,
417
+ scope: 5
418
+ });
419
+ this.highlightInfo = highlightInfo;
420
+ this.render();
421
+ }
422
+ }
423
+ createNewDrawingCuboid(e, basicSourceID) {
424
+ if (!this.imgInfo) {
425
+ return;
426
+ }
427
+ const coordinate = this.getCoordinateInOrigin(e);
428
+ this.drawingCuboid = {
429
+ attribute: this.defaultAttribute,
430
+ direction: ECuboidDirection.Front,
431
+ valid: !e.ctrlKey,
432
+ id: uuid(8, 62),
433
+ sourceID: basicSourceID,
434
+ textAttribute: "",
435
+ order: CommonToolUtils.getMaxOrder(this.cuboidList.filter((v) => CommonToolUtils.isSameSourceID(v.sourceID, basicSourceID))) + 1,
436
+ frontPoints: {
437
+ tl: coordinate,
438
+ bl: coordinate,
439
+ tr: coordinate,
440
+ br: coordinate
441
+ }
442
+ };
443
+ this.firstClickCoord = __spreadValues({}, coordinate);
444
+ this.drawingStatus = 2;
445
+ }
446
+ closeNewDrawingFrontPlane() {
447
+ this.drawingStatus = 3;
448
+ }
449
+ closeAndCreateNewCuboid() {
450
+ var _a, _b;
451
+ if (!((_a = this.drawingCuboid) == null ? void 0 : _a.frontPoints) || !this.drawingCuboid.backPoints) {
452
+ return;
453
+ }
454
+ if (this.isNeedCheckCuboidSize && isCuboidWithInLimits({cuboid: this.drawingCuboid, config: this.config}) === false) {
455
+ this.emit("messageInfo", Locale.getMessagesByLocale(EMessage.RectErrorSizeNotice, this.lang));
456
+ } else {
457
+ this.setCuboidList([...this.cuboidList, this.drawingCuboid]);
458
+ this.setSelectedID((_b = this.drawingCuboid) == null ? void 0 : _b.id);
459
+ this.history.pushHistory(this.cuboidList);
460
+ }
461
+ this.clearDrawingStatus();
462
+ this.render();
463
+ }
464
+ clearDrawingStatus() {
465
+ if (this.drawingCuboid) {
466
+ this.drawingCuboid = void 0;
467
+ this.drawingStatus = 1;
468
+ }
469
+ }
470
+ rightMouseUp(e) {
471
+ const {hoverID, hoverCuboid} = this.getHoverData(e);
472
+ this.setSelectedID(hoverID);
473
+ if (hoverCuboid) {
474
+ this.setDefaultAttribute(hoverCuboid.attribute);
475
+ }
476
+ if (this.drawingCuboid) {
477
+ this.clearDrawingStatus();
478
+ }
479
+ this.render();
480
+ }
481
+ renderSingleCuboid(cuboid) {
482
+ var _a, _b, _c, _d;
483
+ const transformCuboid = AxisUtils.changeCuboidByZoom(cuboid, this.zoom, this.currentPos);
484
+ const isHover = transformCuboid.id === this.hoverID;
485
+ const isSelected = transformCuboid.id === this.selectedID;
486
+ const {strokeColor, fillColor} = this.getColorToRender(transformCuboid.attribute, transformCuboid.valid);
487
+ const textColor = strokeColor;
488
+ const lineWidth = (_b = (_a = this.style) == null ? void 0 : _a.width) != null ? _b : 2;
489
+ const {hiddenText = false} = this.style;
490
+ const defaultStyle = {
491
+ color: strokeColor,
492
+ thickness: lineWidth
493
+ };
494
+ const {backPoints, frontPoints, textAttribute} = transformCuboid;
495
+ const frontPointsSizeWidth = frontPoints.br.x - frontPoints.bl.x;
496
+ DrawUtils.drawCuboid(this.canvas, transformCuboid, {strokeColor, fillColor, thickness: lineWidth});
497
+ if (isHover || isSelected) {
498
+ const hoverPointList = getHighlightPoints(transformCuboid);
499
+ hoverPointList.forEach((data) => {
500
+ DrawUtils.drawCircleWithFill(this.canvas, data.point, 5, __spreadValues({}, defaultStyle));
501
+ });
502
+ if (isSelected) {
503
+ hoverPointList.forEach((data) => {
504
+ DrawUtils.drawCircleWithFill(this.canvas, data.point, 3, {color: "white"});
505
+ });
506
+ }
507
+ }
508
+ let showText = "";
509
+ if (((_c = this.config) == null ? void 0 : _c.isShowOrder) && transformCuboid.order && (transformCuboid == null ? void 0 : transformCuboid.order) > 0) {
510
+ showText = `${transformCuboid.order}`;
511
+ }
512
+ if (transformCuboid.attribute) {
513
+ showText = `${showText} ${AttributeUtils.getAttributeShowText(transformCuboid.attribute, (_d = this.config) == null ? void 0 : _d.attributeList)}`;
514
+ }
515
+ if (!hiddenText && backPoints) {
516
+ DrawUtils.drawText(this.canvas, {x: backPoints.tl.x, y: backPoints.tl.y - 5}, showText, {
517
+ color: strokeColor,
518
+ textMaxWidth: 300
519
+ });
520
+ }
521
+ const textPosition = getCuboidTextAttributeOffset({
522
+ cuboid,
523
+ currentPos: this.currentPos,
524
+ zoom: this.zoom,
525
+ topOffset: 16,
526
+ leftOffset: 0
527
+ });
528
+ if (!hiddenText && textAttribute && cuboid.id !== this.selectedID) {
529
+ const textWidth = Math.max(20, frontPointsSizeWidth * 0.8);
530
+ DrawUtils.drawText(this.canvas, {x: textPosition.left, y: textPosition.top}, textAttribute, {
531
+ color: textColor,
532
+ textMaxWidth: textWidth
533
+ });
534
+ }
535
+ this.renderTextAttribute();
536
+ }
537
+ setDefaultAttribute(defaultAttribute) {
538
+ const oldDefault = this.defaultAttribute;
539
+ this.defaultAttribute = defaultAttribute != null ? defaultAttribute : "";
540
+ if (oldDefault !== defaultAttribute) {
541
+ this.changeStyle(defaultAttribute);
542
+ this.emit("changeAttributeSidebar");
543
+ const {selectedCuboid} = this;
544
+ if (selectedCuboid) {
545
+ this.setCuboidList(this.cuboidList.map((v) => {
546
+ if (v.id === this.selectedID) {
547
+ return __spreadProps(__spreadValues({}, v), {
548
+ attribute: this.defaultAttribute
549
+ });
550
+ }
551
+ return v;
552
+ }));
553
+ this.history.pushHistory(this.cuboidList);
554
+ this.render();
555
+ }
556
+ if (this.drawingCuboid) {
557
+ this.drawingCuboid = __spreadProps(__spreadValues({}, this.drawingCuboid), {
558
+ attribute: this.defaultAttribute
559
+ });
560
+ this.render();
561
+ }
562
+ if (this._textAttributeInstance) {
563
+ if (this.attributeLockList.length > 0 && !this.attributeLockList.includes(this.defaultAttribute)) {
564
+ this._textAttributeInstance.clearTextAttribute();
565
+ return;
566
+ }
567
+ this._textAttributeInstance.updateIcon(this.getTextIconSvg(defaultAttribute));
568
+ }
569
+ }
570
+ }
571
+ renderToggleButton() {
572
+ const {selectedCuboid} = this;
573
+ if (!this.ctx || this.config.textConfigurable !== true || !selectedCuboid) {
574
+ return;
575
+ }
576
+ const {attribute, valid} = selectedCuboid;
577
+ const {strokeColor: color} = this.getColorToRender(attribute, valid);
578
+ if (!this.toggleButtonInstance) {
579
+ this.toggleButtonInstance = new CuboidToggleButtonClass({
580
+ container: this.container,
581
+ cuboidButtonMove: (type) => this.updateMouseOperation(type),
582
+ toggleDirection: (direction) => this.toggleDirection(direction)
583
+ });
584
+ }
585
+ const toggleOffset = getToggleDirectionButtonOffset({
586
+ cuboid: selectedCuboid,
587
+ zoom: this.zoom,
588
+ currentPos: this.currentPos
589
+ });
590
+ this.toggleButtonInstance.update({
591
+ left: toggleOffset.left,
592
+ top: toggleOffset.top,
593
+ color
594
+ });
595
+ }
596
+ renderTextAttribute() {
597
+ var _a;
598
+ const {selectedCuboid} = this;
599
+ if (!this.ctx || this.config.textConfigurable === false || !selectedCuboid) {
600
+ return;
601
+ }
602
+ const {strokeColor: color} = this.getColorToRender(selectedCuboid.attribute, selectedCuboid.valid);
603
+ const {attribute, textAttribute, frontPoints} = selectedCuboid;
604
+ const offset = getCuboidTextAttributeOffset({
605
+ cuboid: selectedCuboid,
606
+ currentPos: this.currentPos,
607
+ zoom: this.zoom
608
+ });
609
+ const newWidth = (frontPoints.br.x - frontPoints.bl.x) * this.zoom * 0.8;
610
+ if (!this._textAttributeInstance) {
611
+ this._textAttributeInstance = new TextAttributeClass({
612
+ width: DEFAULT_TEXT_MAX_WIDTH,
613
+ container: this.container,
614
+ icon: this.getTextIconSvg(attribute),
615
+ color,
616
+ getCurrentSelectedData: this.getCurrentSelectedData,
617
+ updateSelectedTextAttribute: this.updateSelectedTextAttribute
618
+ });
619
+ }
620
+ if (this._textAttributeInstance && !((_a = this._textAttributeInstance) == null ? void 0 : _a.isExit)) {
621
+ this._textAttributeInstance.appendToContainer();
622
+ }
623
+ this._textAttributeInstance.update(`${textAttribute}`, {
624
+ left: offset.left,
625
+ top: offset.top,
626
+ color,
627
+ width: newWidth
628
+ });
629
+ }
630
+ renderDrawing() {
631
+ if (this.drawingCuboid) {
632
+ this.renderSingleCuboid(this.drawingCuboid);
633
+ }
634
+ }
635
+ renderStatic() {
636
+ this.cuboidList.forEach((cuboid) => this.renderSingleCuboid(cuboid));
637
+ }
638
+ renderSelected() {
639
+ var _a;
640
+ const {selectedCuboid} = this;
641
+ if (selectedCuboid) {
642
+ this.renderSingleCuboid(selectedCuboid);
643
+ this.renderToggleButton();
644
+ } else {
645
+ (_a = this.toggleButtonInstance) == null ? void 0 : _a.clearCuboidButtonDOM();
646
+ this.toggleButtonInstance = void 0;
647
+ }
648
+ }
649
+ updateMouseOperation(type) {
650
+ if (type === "in") {
651
+ this.setForbidCursorLine(true);
652
+ this.setForbidOperation(true);
653
+ this.setShowDefaultCursor(true);
654
+ } else {
655
+ this.setForbidCursorLine(false);
656
+ this.setShowDefaultCursor(false);
657
+ this.setForbidOperation(false);
658
+ }
659
+ }
660
+ toggleDirection(direction) {
661
+ if (this.cuboidList && this.selectedCuboid) {
662
+ this.setCuboidList(this.cuboidList.map((i) => {
663
+ var _a;
664
+ return i.id === ((_a = this.selectedCuboid) == null ? void 0 : _a.id) ? __spreadProps(__spreadValues({}, i), {direction}) : i;
665
+ }));
666
+ this.history.pushHistory(this.cuboidList);
667
+ this.render();
668
+ }
669
+ }
670
+ renderHover() {
671
+ var _a;
672
+ if (this.dragInfo) {
673
+ return;
674
+ }
675
+ (_a = this.highlightInfo) == null ? void 0 : _a.forEach((data) => {
676
+ var _a2, _b;
677
+ const {strokeColor} = this.getColorToRender(data.originCuboid.attribute, data.originCuboid.valid);
678
+ const thickness = 8;
679
+ switch (data.type) {
680
+ case "point":
681
+ (_a2 = data.points) == null ? void 0 : _a2.forEach((point) => {
682
+ DrawUtils.drawCircleWithFill(this.canvas, AxisUtils.changePointByZoom(point, this.zoom, this.currentPos), thickness, {
683
+ color: strokeColor
684
+ });
685
+ });
686
+ break;
687
+ case "line": {
688
+ const pointList = (_b = data.points) == null ? void 0 : _b.map((point) => AxisUtils.changePointByZoom(point, this.zoom, this.currentPos));
689
+ if (pointList) {
690
+ DrawUtils.drawLineWithPointList(this.canvas, pointList, {color: strokeColor, thickness});
691
+ }
692
+ break;
693
+ }
694
+ }
695
+ });
696
+ }
697
+ renderCuboid() {
698
+ this.renderStatic();
699
+ this.renderDrawing();
700
+ this.renderSelected();
701
+ this.renderHover();
702
+ }
703
+ render() {
704
+ if (!this.ctx) {
705
+ return;
706
+ }
707
+ super.render();
708
+ this.renderCuboid();
709
+ this.renderCursorLine(this.getLineColor(this.defaultAttribute));
710
+ }
711
+ undo() {
712
+ const cuboidList = this.history.undo();
713
+ if (cuboidList) {
714
+ if (cuboidList.length !== this.cuboidList.length) {
715
+ this.setSelectedID("");
716
+ }
717
+ this.setCuboidList(cuboidList, true);
718
+ this.render();
719
+ }
720
+ }
721
+ redo() {
722
+ const cuboidList = this.history.redo();
723
+ if (cuboidList) {
724
+ if (cuboidList.length !== this.cuboidList.length) {
725
+ this.setSelectedID("");
726
+ }
727
+ this.setCuboidList(cuboidList, true);
728
+ this.render();
729
+ }
730
+ }
731
+ }
732
+
733
+ export { CuboidOperation as default };