@labelbee/lb-annotation 1.28.0-alpha.30 → 1.28.0-alpha.31
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.
|
@@ -1 +1,2 @@
|
|
|
1
|
-
"use strict";var _=require("lodash"),lbUtils=require("@labelbee/lb-utils"),MathUtils=require("../../utils/MathUtils.js"),RectUtils=require("../../utils/tool/RectUtils.js"),annotation=require("../../constant/annotation.js"),keyCode=require("../../constant/keyCode.js"),tool=require("../../constant/tool.js"),index=require("../../locales/index.js"),constants=require("../../locales/constants.js"),ActionsHistory=require("../../utils/ActionsHistory.js"),AttributeUtils=require("../../utils/tool/AttributeUtils.js"),AxisUtils=require("../../utils/tool/AxisUtils.js"),CanvasUtils=require("../../utils/tool/CanvasUtils.js"),CommonToolUtils=require("../../utils/tool/CommonToolUtils.js"),DrawUtils=require("../../utils/tool/DrawUtils.js"),PolygonUtils=require("../../utils/tool/PolygonUtils.js"),StyleUtils=require("../../utils/tool/StyleUtils.js"),uuid=require("../../utils/uuid.js"),basicToolOperation=require("./basicToolOperation.js"),textAttributeClass=require("./textAttributeClass.js"),Selection=require("./Selection.js");function _interopDefaultLegacy(c){return c&&typeof c=="object"&&"default"in c?c:{default:c}}var ___default=_interopDefaultLegacy(_),__defProp=Object.defineProperty,__defProps=Object.defineProperties,__getOwnPropDescs=Object.getOwnPropertyDescriptors,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(c,t,e)=>t in c?__defProp(c,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):c[t]=e,__spreadValues=(c,t)=>{for(var e in t||(t={}))__hasOwnProp.call(t,e)&&__defNormalProp(c,e,t[e]);if(__getOwnPropSymbols)for(var e of __getOwnPropSymbols(t))__propIsEnum.call(t,e)&&__defNormalProp(c,e,t[e]);return c},__spreadProps=(c,t)=>__defProps(c,__getOwnPropDescs(t));const TEXT_MAX_WIDTH=164;class PolygonOperation extends basicToolOperation.BasicToolOperation{constructor(t){super(__spreadProps(__spreadValues({},t),{isOffscreenCanvas:!0}));this.isAllowDouble=e=>{const{selectedID:i}=this,s=this.getHoverID(e);return!!(i&&i===s)},this.textChange=e=>{this.config.textConfigurable===!1||!this.selectedID||(this.setPolygonList(AttributeUtils.default.textChange(e,this.selectedID,this.polygonList)),this.emit("selectedChange"),this.render())},this.lastMouseMoveTime=0,this.mouseMoveThrottle=16,this.emitUpdatePolygonByDrag=()=>{if(this.dragInfo){const{originPolygon:e}=this.dragInfo;this.emit("updatePolygonByDrag",[{newPolygon:this.selectedPolygon,originPolygon:e}])}},this.config=CommonToolUtils.jsonParser(t.config),this.drawingPointList=[],this.polygonList=[],this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.attrChangeTrigger=0,this.drawingHistory=new ActionsHistory,this.isCtrl=!1,this.isAlt=!1,this.isCombined=!1,this.pattern=tool.EPolygonPattern.Normal,this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.selection=new Selection(this)}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}get minArea(){return this.config.minArea||0}eventBinding(){super.eventBinding(),this.container.removeEventListener("mouseup",this.onMouseUp),this.container.addEventListener("mouseup",this.dragMouseUp),this.dblClickListener.addEvent(this.onMouseUp,this.onLeftDblClick,this.onRightDblClick,this.isAllowDouble)}eventUnbinding(){super.eventUnbinding(),this.container.removeEventListener("mouseup",this.dragMouseUp)}destroy(){super.destroy(),this._textAttributeInstance&&this._textAttributeInstance.clearTextAttribute()}get selectedPolygon(){return PolygonUtils.getPolygonByID(this.visiblePolygonList,this.selectedID)}get selectedPolygons(){return PolygonUtils.getPolygonByIDs(this.polygonList,this.selectedIDs)}get hoverPolygon(){return this.visiblePolygonList.find(t=>t.id===this.hoverID&&t.id!==this.selectedID)}get enableDrag(){return Boolean(this.selectedID&&this.dragInfo)}get visiblePolygonList(){return this.polygonList}get polygonListUnderZoom(){return this.visiblePolygonList.map(t=>__spreadProps(__spreadValues({},t),{pointList:AxisUtils.default.changePointListByZoom(t.pointList,this.zoom)}))}get selectedText(){var t;return(t=this.selectedPolygon)==null?void 0:t.textAttribute}get dataList(){return this.polygonList}setPattern(t,e=!1){var i;((i=this.drawingPointList)==null?void 0:i.length)>0&&e===!0||(this.pattern=t)}get currentShowList(){let t=[];const[e,i]=CommonToolUtils.getRenderResultList(this.polygonList,CommonToolUtils.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);return t=e,this.isHidden&&(t=[]),i&&t.push(...i),t}get currentPageResult(){const[t]=CommonToolUtils.getRenderResultList(this.polygonList,CommonToolUtils.getSourceID(this.basicResult),[]);return t}setResultAndSelectedID(t,e){this.setPolygonList(t),e&&this.setSelectedID(e),this.render()}setResult(t,e=!0){this.clearActiveStatus(e),this.setPolygonList(t),this.render()}setPolygonDataByID(t,e){return this.polygonList.map(i=>i.id===e?__spreadValues(__spreadValues({},i),t):i)}rotatePolygon(t=1,e=annotation.ERotateDirection.Clockwise,i=this.selectedID){if(!i)return;const s=PolygonUtils.getPolygonByID(this.polygonList,i);if(!s)return;const o=PolygonUtils.updatePolygonByRotate(e,t,s==null?void 0:s.pointList);this.setPolygonList(this.setPolygonDataByID({pointList:o},i)),this.render()}rotatePolygonEdge(t,e=this.selectedID){!e||!PolygonUtils.getPolygonByID(this.polygonList,e)||(this.setPolygonList(this.setPolygonDataByID({pointList:t},e)),this.render())}addPointInDrawing(t){if(!this.imgInfo||this.forbidAddNewPolygonFuc&&this.forbidAddNewPolygonFuc(t))return;const{upperLimitPointNum:e,edgeAdsorption:i}=this.config;if(e&&this.drawingPointList.length>=e){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.UpperLimitErrorNotice,this.lang)}${e}`);return}const s=this.getCoordinateUnderZoom(t),o=AxisUtils.default.changeDrawOutsideTarget(s,{x:0,y:0},this.imgInfo,this.config.drawOutsideTarget,this.basicResult,this.zoom);if(AxisUtils.default.returnClosePointIndex(o,AxisUtils.default.changePointListByZoom(this.drawingPointList,this.zoom))===0){this.addDrawingPointToPolygonList();return}const{dropFoot:l}=PolygonUtils.getClosestPoint(o,this.polygonListUnderZoom,this.config.lineType,tool.edgeAdsorptionScope),n=AxisUtils.default.changePointByZoom(l&&t.altKey===!1&&i?l:o,1/this.zoom);!this.addPointInDrawingHook(n).continue||(this.drawingPointList.push(n),this.drawingPointList.length===1?this.drawingHistory.initRecord(this.drawingPointList):this.drawingHistory.pushHistory(this.drawingPointList))}clearResult(){this.setPolygonList([]),this.deleteSelectedID(),this.render()}clearPolygonDrag(){this.drawingPointList=[],this.stopDrag(),this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,this.hoverID=""}clearActiveStatus(t=!0){this.clearPolygonDrag(),t&&this.deleteSelectedID()}clearDrawingStatus(){this.drawingPointList=[]}setPolygonList(t){const e=this.polygonList.length;this.polygonList=t,e!==t.length&&this.emit("updatePageNumber")}setSelectedID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}setAttrChangeTrigger(t=0){this.attrChangeTrigger=t}setDefaultAttribute(t=""){const e=this.defaultAttribute;if(this.defaultAttribute=t,(this.attrChangeTrigger||e!==t)&&(this.changeStyle(t),this.emit("changeAttributeSidebar"),this.selectedPolygons.length>0&&(this.selectedPolygons.forEach(i=>{i.attribute=t}),this.history.pushHistory(this.polygonList),this.render()),this._textAttributeInstance)){if(this.attributeLockList.length>0&&!this.attributeLockList.includes(t)){this._textAttributeInstance.clearTextAttribute();return}this._textAttributeInstance.updateIcon(this.getTextIconSvg(t))}}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}setPolygonValidAndRender(t,e){if(!t)return;const i=this.polygonList.map(s=>{var o;if(s.id===t){const r=(o=s==null?void 0:s.valid)!=null?o:!0;return __spreadProps(__spreadValues({},s),{valid:e!=null?e:!r})}return s});this.setPolygonList(i),this.history.pushHistory(this.polygonList),this.render(),this.emit("updateResult")}addDrawingPointToPolygonList(t){let{lowerLimitPointNum:e=3}=this.config;e<3&&(e=3);let i;if(this.drawingPointList.length<e){this.drawingPointList=[],this.editPolygonID="";return}if(PolygonUtils.calcPolygonSize(this.drawingPointList||[])<this.minArea){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.MinAreaLimitErrorNotice,this.lang)}${this.minArea}`);return}const s=CommonToolUtils.getSourceID(this.basicResult),o=[...this.polygonList];if(this.editPolygonID){const r=o.find(l=>l.id===this.editPolygonID);if(!r)return;r.pointList=this.drawingPointList,this.editPolygonID=""}else{const r=uuid(8,62);let l={id:r,sourceID:s,valid:!this.isCtrl,textAttribute:"",pointList:this.drawingPointList,attribute:this.defaultAttribute,order:CommonToolUtils.getMaxOrder(o.filter(n=>CommonToolUtils.isSameSourceID(n.sourceID,s)))+1};if(this.config.textConfigurable){let n="";n=AttributeUtils.default.getTextAttribute(this.polygonList.filter(h=>CommonToolUtils.isSameSourceID(h.sourceID,s)),this.config.textCheckType),l=__spreadProps(__spreadValues({},l),{textAttribute:n})}this.pattern===tool.EPolygonPattern.Rect&&t===!0&&(l=__spreadProps(__spreadValues({},l),{isRect:!0})),o.push(l),i=l,this.setSelectedIdAfterAddingDrawing(r)}this.setPolygonList(o),this.isCtrl=!1,this.drawingPointList=[],this.history.pushHistory(o),i&&this.emit("polygonCreated",i,this.zoom,this.currentPos)}setSelectedIdAfterAddingDrawing(t){this.drawingPointList.length!==0&&(this.config.textConfigurable?this.setSelectedID(t):this.deleteSelectedID())}getHoverID(t){var e;const i=this.getCoordinateUnderZoom(t),s=this.currentShowList.map(o=>__spreadProps(__spreadValues({},o),{pointList:AxisUtils.default.changePointListByZoom(o.pointList,this.zoom)}));return PolygonUtils.getHoverPolygonID(i,s,10,(e=this.config)==null?void 0:e.lineType)}getHoverEdgeIndex(t){var e;if(!this.selectedID)return-1;const i=this.selectedPolygon;if(!i)return-1;const s=this.getCoordinateUnderZoom(t),o=AxisUtils.default.changePointListByZoom(i.pointList,this.zoom);return PolygonUtils.getHoverEdgeIndex(s,o,(e=this.config)==null?void 0:e.lineType)}getHoverPointIndex(t){if(!this.selectedID)return-1;const e=this.selectedPolygon;if(!e)return-1;const i=this.getCoordinateUnderZoom(t),s=AxisUtils.default.changePointListByZoom(e.pointList,this.zoom);return AxisUtils.default.returnClosePointIndex(i,s)}deletePolygons(t){var e;if(!t||t.length===0)return;const i=this.polygonList.filter(s=>this.selection.isIdSelected(s.id));this.emit("deletedObject",{deletedObject:i[0],id:i[0].id}),this.setPolygonList(this.polygonList.filter(s=>!this.selection.isIdSelected(s.id))),this.history.pushHistory(this.polygonList),(e=this._textAttributeInstance)==null||e.clearTextAttribute(),this.emit("selectedChange"),this.render()}deletePolygonPoint(t){if(!this.selectedID)return;const{selectedPolygon:e}=this;if(!e)return;let{lowerLimitPointNum:i}=this.config;if(i<3&&(i=3),e.pointList.length<=i){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.LowerLimitErrorNotice,this.lang)}${i}`);return}e==null||e.pointList.splice(t,1),this.history.pushHistory(this.polygonList),this.render()}spaceKeydown(){var t,e,i;if(this.selectedID){if(((t=this.selectedPolygon)==null?void 0:t.isRect)===!0){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.UnableToReannotation,this.lang)}`);return}this.editPolygonID=this.selectedID,this.drawingPointList=(i=(e=this.selectedPolygon)==null?void 0:e.pointList)!=null?i:[],this.drawingHistory.empty(),this.drawingHistory.initRecord(this.drawingPointList),this.hoverID="",this.deleteSelectedID(),this.render()}}onTabKeyDown(t){t.preventDefault();let e=annotation.ESortDirection.ascend;t.shiftKey&&(e=annotation.ESortDirection.descend),this.switchToNextPolygon(e)}switchToNextPolygon(t){if(this.drawingPointList.length>0)return;const[e,i]=CommonToolUtils.getRenderResultList(this.polygonList,CommonToolUtils.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let s=[...e];i&&(s=[...s,...i]);const o=CanvasUtils.getViewPort(this.canvas,this.currentPos,this.zoom),r=s.map(n=>{var h,d,g,a;return __spreadProps(__spreadValues({},n),{x:(d=(h=n.pointList[0])==null?void 0:h.x)!=null?d:0,y:(a=(g=n.pointList[0])==null?void 0:g.y)!=null?a:0})}).filter(n=>CanvasUtils.inViewPort({x:n.x,y:n.y},o)),l=CommonToolUtils.getNextSelectedRectID(r,t,this.selectedID);if(l){this.setSelectedID(l.id);const{selectedPolygon:n}=this;n&&this.setDefaultAttribute(n.attribute)}this.render()}onKeyDown(t){if(!CommonToolUtils.hotkeyFilter(t)||super.onKeyDown(t)===!1||this.selection.triggerKeyboardEvent(t,this.setPolygonList.bind(this)))return;const{keyCode:e}=t;switch(e){case keyCode.Space:this.spaceKeydown();break;case keyCode.Esc:this.drawingPointList=[],this.editPolygonID="";break;case keyCode.F:this.selectedID&&this.setPolygonValidAndRender(this.selectedID);break;case keyCode.Z:if(t.altKey){this.onCombinedExecute();return}this.setIsHidden(!this.isHidden),this.render();break;case keyCode.Delete:this.deletePolygons(this.selectedIDs),this.render();break;case keyCode.Ctrl:this.isCtrl=!0;break;case keyCode.Alt:this.isAlt===!1&&(t.preventDefault(),this.isAlt=!0,this.render());break;case keyCode.Tab:{this.onTabKeyDown(t);break}case keyCode.X:t.altKey&&this.segment();break;default:{if(this.config.attributeConfigurable){const i=AttributeUtils.default.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}}onKeyUp(t){switch(super.onKeyUp(t),t.keyCode){case keyCode.Ctrl:this.isCtrl=!1;break;case keyCode.Alt:{const e=this.isAlt;this.isAlt=!1,e===!0&&this.render();break}}}rightMouseUp(t){if(this.drawingPointList.length>0){this.addDrawingPointToPolygonList();return}this.setSelectedID(this.hoverID,t.ctrlKey);const{selectedPolygon:e}=this;e&&this.setDefaultAttribute(e.attribute)}onLeftDblClick(t){if(this.hoverEdgeIndex>-1){const e=this.getCoordinateUnderZoom(t),{selectedPolygon:i}=this;if(!i)return;if(i.isRect===!0){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.ForbidAddNewPoint,this.lang)}`),this.clearPolygonDrag();return}const{dropFoot:s}=PolygonUtils.getClosestPoint(e,this.polygonListUnderZoom,this.config.lineType,tool.edgeAdsorptionScope);if(!s)return;const{upperLimitPointNum:o}=this.config;if(o&&i.pointList.length>=o){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.UpperLimitErrorNotice,this.lang)}${o}`),this.clearPolygonDrag();return}i==null||i.pointList.splice(this.hoverEdgeIndex+1,0,AxisUtils.default.changePointByZoom(s,1/this.zoom)),this.setPolygonDataByID(i,this.selectedID),this.history.pushHistory(this.polygonList),this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.render()}this.dragInfo=void 0}onRightDblClick(t){this.dragInfo=void 0,this.clearImgDrag();const e=this.getHoverID(t),i=this.getHoverPointIndex(t);if(this.hoverPointIndex>-1&&this.hoverPointIndex===i){this.deletePolygonPoint(i),this.dragInfo=void 0,this.hoverPointIndex=-1,this.render();return}this.hoverID===this.selectedID&&this.deletePolygons([e]),this.render()}onMouseDown(t){if(!(super.onMouseDown(t)||this.forbidMouseOperation||t.ctrlKey===!0))return this.dragMouseDown(t),!0}onMouseLeave(){this.dragInfo&&(this.emitUpdatePolygonByDrag(),this.clearPolygonDrag(),this.history.pushHistory(this.polygonList))}dragMouseDown(t){if(t.button!==0)return;const e=this.getHoverID(t);if(!this.selection.isIdSelected(e))return;let i=[0],s=annotation.EDragTarget.Plane;const o=this.selectedPolygons[0].pointList,r=this.getCoordinateUnderZoom(t);if(this.selectedPolygons.length===1){this.dragStatus=annotation.EDragStatus.Start;const l=this.getHoverPointIndex(t),n=this.getHoverEdgeIndex(t);l>-1?(s=annotation.EDragTarget.Point,i=[l]):n>-1&&this.hoverEdgeIndex>-1&&(s=annotation.EDragTarget.Line,i=[n,(n+1)%o.length])}this.dragInfo={dragStartCoord:r,dragTarget:s,initPointList:o,changePointIndex:i,originPolygon:this.selectedPolygon,selectedPolygons:this.selectedPolygons,dragPrevCoord:r,originPolygonList:this.polygonList}}segment(){var t;if(!this.selectedID||((t=this.config)==null?void 0:t.lineType)!==tool.ELineTypes.Line)return;const e=PolygonUtils.getPolygonPointList(this.selectedID,this.currentShowList),i=this.currentShowList.filter(r=>r.id!==this.selectedID);if(i.length===0||e.length===0)return;const s=PolygonUtils.getWrapPolygonIndex(e,i);let o=[...this.polygonList];if(s===-1){const r=PolygonUtils.segmentPolygonByPolygon(e,i);if(!r)return;const l=r.shift();if(!l)return;let n="",h=!0;const d=CommonToolUtils.getSourceID(this.basicResult);let g="";o=this.polygonList.map(a=>{var u,f;return a.id===this.selectedID?(n=a.attribute,h=(u=a==null?void 0:a.valid)!=null?u:!0,g=(f=a==null?void 0:a.textAttribute)!=null?f:"",__spreadProps(__spreadValues({},a),{pointList:l})):a}),r.length>0&&r.forEach((a,u)=>{o.push({sourceID:d,id:uuid(8,62),pointList:a,valid:h,order:CommonToolUtils.getMaxOrder(this.currentShowList)+1+u,attribute:n,textAttribute:g})})}else o[s].pointList=PolygonUtils.clipPolygonFromWrapPolygon(e,o[s].pointList),o=o.filter(r=>r.id!==this.selectedID);this.setPolygonList(o),this.history.pushHistory(o),this.render()}onCombinedExecute(){if(!this.selectedID){this.emit("messageInfo",lbUtils.i18n.t("PolygonsToBeCombinedNeedToBeSelected"));return}this.isCombined=!this.isCombined}combine(t){var e;const i=this.getHoverID(t);if(!i||!this.selectedID||this.selectedID===i)return;if(((e=this.config)==null?void 0:e.lineType)!==tool.ELineTypes.Line){this.emit("messageInfo",lbUtils.i18n.t("CurveModeDoesNotSupportCutting"));return}const s=this.polygonList.find(h=>h.id===this.selectedID),o=this.currentShowList.find(h=>h.id===i);if(!o||!s)return;const r=PolygonUtils.combinePolygonWithPolygon(s,o);if(!r)return;const{newPolygon:l,unionList:n}=r;if(n.length===1&&l){const h=this.polygonList.filter(d=>!n.includes(d.id)).map(d=>d.id===this.selectedID?l:d);this.setPolygonList(h),this.history.pushHistory(h),this.render(),this.emit("messageInfo",lbUtils.i18n.t("CombineSuccess"))}else this.emit("messageInfo",lbUtils.i18n.t("CombiningFailedNotify"));this.isCombined=!1}isPolygonOutSide(t){if(this.dependToolName&&this.basicCanvas&&this.basicResult){let l=!1;switch(this.dependToolName){case tool.EToolName.Rect:{l=t.filter(n=>!RectUtils.isInRect(n,this.basicResult)).length>0;break}case tool.EToolName.Polygon:{l=PolygonUtils.isPointListOutSidePolygon(t,this.basicResult.pointList,this.config.lineType);break}}return l}if(!this.imgInfo)return!1;const{left:e,top:i,right:s,bottom:o}=MathUtils.default.calcViewportBoundaries(AxisUtils.default.changePointListByZoom(t,this.zoom)),r=1e-5;return e<0||i<0||s>this.imgInfo.width+r||o>this.imgInfo.height+r}onDragMove(t){const e=this.polygonList.map(i=>{if(this.selectedIDs.includes(i.id)){const s=this.dragPolygon(t,i);if(!s)return i;const o=__spreadProps(__spreadValues({},i),{pointList:s});return i.isRect===!0&&this.pattern===tool.EPolygonPattern.Normal&&Object.assign(o,{isRect:!1}),o}return i});this.dragInfo.dragPrevCoord=this.getCoordinateUnderZoom(t),this.setPolygonList(e)}getDragOffset(t,e){var i;const s=this.getCoordinateUnderZoom(t),{dragTarget:o,dragPrevCoord:r,changePointIndex:l,initPointList:n,dragStartCoord:h}=this.dragInfo;if(this.pattern===tool.EPolygonPattern.Rect&&(e==null?void 0:e.isRect)===!0&&l&&[annotation.EDragTarget.Line].includes(o)){const d=MathUtils.default.getArrayIndex(l[0]-2,4),g=MathUtils.default.getArrayIndex(l[0]-1,4),a=[n[d],n[g]],u=MathUtils.default.getRectPerpendicularOffset(h,s,a);return{x:u.x/this.zoom,y:u.y/this.zoom}}return((i=this.dragInfo)==null?void 0:i.dragTarget)===annotation.EDragTarget.Plane?{x:(s.x-r.x)/this.zoom,y:(s.y-r.y)/this.zoom}:{x:(s.x-h.x)/this.zoom,y:(s.y-h.y)/this.zoom}}dragPolygon(t,e){let i=___default.default.cloneDeep(e==null?void 0:e.pointList);if(!i||!this.dragInfo)return;const{initPointList:s,dragTarget:o,changePointIndex:r}=this.dragInfo,l=this.getDragOffset(t,e);switch(this.dragStatus=annotation.EDragStatus.Move,o){case annotation.EDragTarget.Plane:i=i.map(n=>__spreadProps(__spreadValues({},n),{x:n.x+l.x,y:n.y+l.y}));break;case annotation.EDragTarget.Point:case annotation.EDragTarget.Line:i=i.map((n,h)=>r&&r.includes(h)?__spreadProps(__spreadValues({},n),{x:s[h].x+l.x,y:s[h].y+l.y}):n);break}if(this.pattern===tool.EPolygonPattern.Rect&&(e==null?void 0:e.isRect)===!0&&o===annotation.EDragTarget.Point&&r&&(i=MathUtils.default.getPointListFromPointOffset(s,r[0],l)),!(this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(i)))return i}determineTrigger(t){if(this.isDrag)return"drag";if(this.dragInfo)return"dragSingle";let e="";const i=this.getHoverID(t);this.hoverID!==i?(this.hoverID=i,e=""):e="move";let s=-1,o=-1;const{selectedID:r}=this;return r&&(s=this.getHoverPointIndex(t),s>-1?this.hoverPointIndex!==s?e="":e="noRender":(o=this.getHoverEdgeIndex(t),this.hoverEdgeIndex!==o?e="":e="noRender"),this.hoverEdgeIndex=o,this.hoverPointIndex=s),e}onMouseMove(t){requestAnimationFrame(()=>{const e=Date.now();if(e-this.lastMouseMoveTime<this.mouseMoveThrottle||(this.lastMouseMoveTime=e,super.onMouseMove(t,!1)||this.forbidMouseOperation||!this.imgInfo))return;if(this.drawingPointList.length>0){this.render();return}const i=this.determineTrigger(t);this.selectedIDs.length>0&&this.dragInfo&&this.onDragMove(t),this.render(i)})}leftMouseUpdateValid(t){const e=this.getHoverID(t);return this.drawingPointList.length===0&&t.ctrlKey===!0&&e?(this.setPolygonValidAndRender(e),!0):!1}stopDrag(){this.dragInfo=void 0,this.dragStatus=annotation.EDragStatus.Wait}leftMouseUp(t){this.leftMouseUpdateValid(t)||this.addPointInDrawing(t)}onMouseUp(t){if(this.isCombined){switch(t.button){case 0:this.combine(t);break;case 2:this.isCombined=!1;break;default:return}return}if(!(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)){if(this.dragInfo&&this.dragStatus===annotation.EDragStatus.Move){this.emit("updateResult"),this.emitUpdatePolygonByDrag(),this.stopDrag(),this.history.pushHistory(this.polygonList);return}if(this.dragInfo&&this.dragStatus===annotation.EDragStatus.Start){this.stopDrag();return}switch(t.button){case 0:{this.leftMouseUp(t);break}case 2:{this.rightMouseUp(t);break}}this.render()}}dragMouseUp(){this.dragStatus===annotation.EDragStatus.Start&&this.stopDrag()}exportData(){const{polygonList:t}=this;return[t,this.basicImgInfo]}getTextIconSvg(t=""){return AttributeUtils.default.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}getCurrentSelectedData(){const{selectedPolygon:t}=this;if(!t)return;const e=this.getColor(t.attribute),i=t.valid?e==null?void 0:e.valid.stroke:e==null?void 0:e.invalid.stroke;return{width:TEXT_MAX_WIDTH,textAttribute:t.textAttribute,color:i}}updateSelectedTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedID){let e=t;AttributeUtils.default.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",AttributeUtils.default.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setPolygonList(AttributeUtils.default.textChange(e,this.selectedID,this.polygonList)),this.emit("updateTextAttribute"),this.render()}}renderTextAttribute(){var t;const{selectedPolygon:e}=this;if(!this.ctx||this.config.textConfigurable===!1||!e)return;const{pointList:i,attribute:s,valid:o,textAttribute:r}=e,{x:l,y:n}=i[i.length-1],h=TEXT_MAX_WIDTH,d=AxisUtils.default.getOffsetCoordinate({x:l,y:n},this.currentPos,this.zoom),g=this.getColor(s),a=o?g==null?void 0:g.valid.stroke:g==null?void 0:g.invalid.stroke;this._textAttributeInstance||(this._textAttributeInstance=new textAttributeClass({width:h,container:this.container,icon:this.getTextIconSvg(s),color:a,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((t=this._textAttributeInstance)==null?void 0:t.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${r}`,{left:d.x,top:d.y,color:a,width:h})}renderStaticPolygon(){var t;this.isHidden===!1&&((t=this.visiblePolygonList)==null||t.forEach(e=>{var i,s,o,r,l;if([this.selectedID,this.editPolygonID].includes(e.id))return;const{textAttribute:n,attribute:h}=e,d=this.getColor(h),g=StyleUtils.getStrokeAndFill(d,e.valid),a=AxisUtils.default.changePointListByZoom(e.pointList||[],this.zoom,this.currentPos);DrawUtils.drawPolygonWithFillAndLine(this.canvas,a,{fillColor:g.fill,strokeColor:g.stroke,pointColor:"white",thickness:(s=(i=this.style)==null?void 0:i.width)!=null?s:2,lineCap:"round",isClose:!0,lineType:(o=this.config)==null?void 0:o.lineType});let u=`${(r=AttributeUtils.default.getAttributeShowText(h,this.config.attributeList))!=null?r:""}`;((l=this.config)==null?void 0:l.isShowOrder)&&(e==null?void 0:e.order)>0&&(u=`${e.order} ${u}`),DrawUtils.drawText(this.canvas,a[0],u,__spreadValues({color:g.stroke},annotation.DEFAULT_TEXT_OFFSET));const f=a[a.length-1];DrawUtils.drawText(this.canvas,{x:f.x+annotation.TEXT_ATTRIBUTE_OFFSET.x,y:f.y+annotation.TEXT_ATTRIBUTE_OFFSET.y},n,__spreadValues({color:g.stroke},annotation.DEFAULT_TEXT_OFFSET))}))}renderSelectedPolygons(){this.selectedPolygons.forEach(t=>{this.renderSelectedPolygon(t)})}renderSelectedPolygon(t){var e,i,s;if(t){const o=this.getColor(t.attribute),r=StyleUtils.getStrokeAndFill(o,t.valid,{isSelected:!0});DrawUtils.drawSelectedPolygonWithFillAndLine(this.canvas,AxisUtils.default.changePointListByZoom(t.pointList,this.zoom,this.currentPos),{fillColor:r.fill,strokeColor:r.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!0,lineType:(e=this.config)==null?void 0:e.lineType});let l=`${(i=AttributeUtils.default.getAttributeShowText(t.attribute,this.config.attributeList))!=null?i:""}`;((s=this.config)==null?void 0:s.isShowOrder)&&(t==null?void 0:t.order)>0&&(l=`${t.order} ${l}`),DrawUtils.drawText(this.canvas,AxisUtils.default.changePointByZoom(t.pointList[0],this.zoom,this.currentPos),l,__spreadValues({color:r.stroke},annotation.DEFAULT_TEXT_OFFSET)),this.renderTextAttribute()}}renderHoverPolygon(){var t;if(this.hoverID&&this.hoverID!==this.editPolygonID){const{hoverPolygon:e}=this;if(e){let i="";const s=this.getColor(e.attribute);e.valid?i=s.validHover.fill:i=StyleUtils.getStrokeAndFill(s,!1,{isHover:!0}).fill,DrawUtils.drawPolygonWithFill(this.canvas,AxisUtils.default.changePointListByZoom(e.pointList,this.zoom,this.currentPos),{color:i,lineType:(t=this.config)==null?void 0:t.lineType})}}}renderPolygon(t){var e,i,s,o;t!=="move"&&this.renderStaticPolygon(),this.renderHoverPolygon(),this.renderSelectedPolygons();const r=this.getColor(this.defaultAttribute),l=StyleUtils.getStrokeAndFill(r,!this.isCtrl);if(((e=this.drawingPointList)==null?void 0:e.length)>0){let n=[...this.drawingPointList],h=AxisUtils.default.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);const d=this.getPointListByRectDrawing(h,n).value;if(d)n=d;else{if(((i=this.config)==null?void 0:i.edgeAdsorption)&&this.isAlt===!1){const{dropFoot:a}=PolygonUtils.getClosestPoint(h,this.polygonList,(s=this.config)==null?void 0:s.lineType,tool.edgeAdsorptionScope/this.zoom);a&&(h=a)}n.push(h)}const g=AxisUtils.default.changePointListByZoom(n,this.zoom,this.currentPos);DrawUtils.drawSelectedPolygonWithFillAndLine(this.canvas,g,{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!1,lineType:this.config.lineType}),this.renderPolygonHook(g)}if(this.hoverPointIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const h=StyleUtils.getStrokeAndFill(r,n.valid,{isSelected:!0}),d=n==null?void 0:n.pointList[this.hoverPointIndex];if(d){const{x:g,y:a}=AxisUtils.default.changePointByZoom(d,this.zoom,this.currentPos);DrawUtils.drawCircleWithFill(this.canvas,{x:g,y:a},5,{color:h.fill})}}if(this.hoverEdgeIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const h=StyleUtils.getStrokeAndFill(r,n.valid,{isSelected:!0});DrawUtils.drawLineWithPointList(this.canvas,AxisUtils.default.changePointListByZoom(n.pointList,this.zoom,this.currentPos),{color:h.stroke,thickness:10,hoverEdgeIndex:this.hoverEdgeIndex,lineType:(o=this.config)==null?void 0:o.lineType})}}render(t){!this.ctx||(t!=="move"&&super.render(),t!=="drag"&&t!=="dragSingle"&&this.renderPolygon(t),t==="dragSingle"&&this.renderSelectedPolygons(),this.renderCursorLine(this.getLineColor(this.defaultAttribute)))}renderCursorLine(t){requestAnimationFrame(()=>{this.clearOffscreenCanvas();const{x:e,y:i}=this.coord;if(DrawUtils.drawLine(this.offscreenCanvas,{x:0,y:i},{x:1e4,y:i},{color:t}),DrawUtils.drawLine(this.offscreenCanvas,{x:e,y:0},{x:e,y:1e4},{color:t}),DrawUtils.drawCircleWithFill(this.offscreenCanvas,{x:e,y:i},1,{color:"white"}),this.isCombined){const s=10,o=186,r=32;DrawUtils.drawRectWithFill(this.canvas,{x:e+s,y:i-s*4-1,width:o,height:r},{color:"black"}),DrawUtils.drawText(this.canvas,{x:e,y:i},lbUtils.i18n.t("ClickAnotherPolygon"),{textAlign:"center",color:"white",offsetX:o/2+s,offsetY:-(r/2+s/2)}),DrawUtils.drawRect(this.canvas,{x:e-s,y:i-s,width:s*2,height:s*2},{lineDash:[6],color:"white"})}})}undo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.undo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.undo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}redo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.redo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.redo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}deleteSelectedID(){this.setSelectedID("")}addPointInDrawingHook(...t){return{continue:!0}}renderPolygonHook(...t){return{continue:!0}}getPointListByRectDrawing(...t){return{continue:!0}}}module.exports=PolygonOperation;
|
|
1
|
+
"use strict";var _=require("lodash"),lbUtils=require("@labelbee/lb-utils"),MathUtils=require("../../utils/MathUtils.js"),RectUtils=require("../../utils/tool/RectUtils.js"),annotation=require("../../constant/annotation.js"),keyCode=require("../../constant/keyCode.js"),tool=require("../../constant/tool.js"),index=require("../../locales/index.js"),constants=require("../../locales/constants.js"),ActionsHistory=require("../../utils/ActionsHistory.js"),AttributeUtils=require("../../utils/tool/AttributeUtils.js"),AxisUtils=require("../../utils/tool/AxisUtils.js"),CanvasUtils=require("../../utils/tool/CanvasUtils.js"),CommonToolUtils=require("../../utils/tool/CommonToolUtils.js"),DrawUtils=require("../../utils/tool/DrawUtils.js"),PolygonUtils=require("../../utils/tool/PolygonUtils.js"),StyleUtils=require("../../utils/tool/StyleUtils.js"),TagUtils=require("../../utils/tool/TagUtils.js"),uuid=require("../../utils/uuid.js"),basicToolOperation=require("./basicToolOperation.js"),textAttributeClass=require("./textAttributeClass.js"),Selection=require("./Selection.js");function _interopDefaultLegacy(c){return c&&typeof c=="object"&&"default"in c?c:{default:c}}var ___default=_interopDefaultLegacy(_),__defProp=Object.defineProperty,__defProps=Object.defineProperties,__getOwnPropDescs=Object.getOwnPropertyDescriptors,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(c,t,e)=>t in c?__defProp(c,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):c[t]=e,__spreadValues=(c,t)=>{for(var e in t||(t={}))__hasOwnProp.call(t,e)&&__defNormalProp(c,e,t[e]);if(__getOwnPropSymbols)for(var e of __getOwnPropSymbols(t))__propIsEnum.call(t,e)&&__defNormalProp(c,e,t[e]);return c},__spreadProps=(c,t)=>__defProps(c,__getOwnPropDescs(t));const TEXT_MAX_WIDTH=164;class PolygonOperation extends basicToolOperation.BasicToolOperation{constructor(t){super(__spreadProps(__spreadValues({},t),{isOffscreenCanvas:!0}));this.isAllowDouble=e=>{const{selectedID:i}=this,s=this.getHoverID(e);return!!(i&&i===s)},this.textChange=e=>{this.config.textConfigurable===!1||!this.selectedID||(this.setPolygonList(AttributeUtils.default.textChange(e,this.selectedID,this.polygonList)),this.emit("selectedChange"),this.render())},this.lastMouseMoveTime=0,this.mouseMoveThrottle=16,this.emitUpdatePolygonByDrag=()=>{if(this.dragInfo){const{originPolygon:e}=this.dragInfo;this.emit("updatePolygonByDrag",[{newPolygon:this.selectedPolygon,originPolygon:e}])}},this.config=CommonToolUtils.jsonParser(t.config),this.drawingPointList=[],this.polygonList=[],this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.attrChangeTrigger=0,this.drawingHistory=new ActionsHistory,this.isCtrl=!1,this.isAlt=!1,this.isCombined=!1,this.pattern=tool.EPolygonPattern.Normal,this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.selection=new Selection(this)}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}get minArea(){return this.config.minArea||0}eventBinding(){super.eventBinding(),this.container.removeEventListener("mouseup",this.onMouseUp),this.container.addEventListener("mouseup",this.dragMouseUp),this.dblClickListener.addEvent(this.onMouseUp,this.onLeftDblClick,this.onRightDblClick,this.isAllowDouble)}eventUnbinding(){super.eventUnbinding(),this.container.removeEventListener("mouseup",this.dragMouseUp)}destroy(){super.destroy(),this._textAttributeInstance&&this._textAttributeInstance.clearTextAttribute()}get selectedPolygon(){return PolygonUtils.getPolygonByID(this.visiblePolygonList,this.selectedID)}get selectedPolygons(){return PolygonUtils.getPolygonByIDs(this.polygonList,this.selectedIDs)}get hoverPolygon(){return this.visiblePolygonList.find(t=>t.id===this.hoverID&&t.id!==this.selectedID)}get enableDrag(){return Boolean(this.selectedID&&this.dragInfo)}get visiblePolygonList(){return this.polygonList}get polygonListUnderZoom(){return this.visiblePolygonList.map(t=>__spreadProps(__spreadValues({},t),{pointList:AxisUtils.default.changePointListByZoom(t.pointList,this.zoom)}))}get selectedText(){var t;return(t=this.selectedPolygon)==null?void 0:t.textAttribute}get dataList(){return this.polygonList}setPattern(t,e=!1){var i;((i=this.drawingPointList)==null?void 0:i.length)>0&&e===!0||(this.pattern=t)}get currentShowList(){let t=[];const[e,i]=CommonToolUtils.getRenderResultList(this.polygonList,CommonToolUtils.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);return t=e,this.isHidden&&(t=[]),i&&t.push(...i),t}get currentPageResult(){const[t]=CommonToolUtils.getRenderResultList(this.polygonList,CommonToolUtils.getSourceID(this.basicResult),[]);return t}setResultAndSelectedID(t,e){this.setPolygonList(t),e&&this.setSelectedID(e),this.render()}setResult(t,e=!0){this.clearActiveStatus(e),this.setPolygonList(t),this.render()}setPolygonDataByID(t,e){return this.polygonList.map(i=>i.id===e?__spreadValues(__spreadValues({},i),t):i)}rotatePolygon(t=1,e=annotation.ERotateDirection.Clockwise,i=this.selectedID){if(!i)return;const s=PolygonUtils.getPolygonByID(this.polygonList,i);if(!s)return;const o=PolygonUtils.updatePolygonByRotate(e,t,s==null?void 0:s.pointList);this.setPolygonList(this.setPolygonDataByID({pointList:o},i)),this.render()}rotatePolygonEdge(t,e=this.selectedID){!e||!PolygonUtils.getPolygonByID(this.polygonList,e)||(this.setPolygonList(this.setPolygonDataByID({pointList:t},e)),this.render())}addPointInDrawing(t){if(!this.imgInfo||this.forbidAddNewPolygonFuc&&this.forbidAddNewPolygonFuc(t))return;const{upperLimitPointNum:e,edgeAdsorption:i}=this.config;if(e&&this.drawingPointList.length>=e){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.UpperLimitErrorNotice,this.lang)}${e}`);return}const s=this.getCoordinateUnderZoom(t),o=AxisUtils.default.changeDrawOutsideTarget(s,{x:0,y:0},this.imgInfo,this.config.drawOutsideTarget,this.basicResult,this.zoom);if(AxisUtils.default.returnClosePointIndex(o,AxisUtils.default.changePointListByZoom(this.drawingPointList,this.zoom))===0){this.addDrawingPointToPolygonList();return}const{dropFoot:l}=PolygonUtils.getClosestPoint(o,this.polygonListUnderZoom,this.config.lineType,tool.edgeAdsorptionScope),n=AxisUtils.default.changePointByZoom(l&&t.altKey===!1&&i?l:o,1/this.zoom);!this.addPointInDrawingHook(n).continue||(this.drawingPointList.push(n),this.drawingPointList.length===1?this.drawingHistory.initRecord(this.drawingPointList):this.drawingHistory.pushHistory(this.drawingPointList))}clearResult(){this.setPolygonList([]),this.deleteSelectedID(),this.render()}clearPolygonDrag(){this.drawingPointList=[],this.stopDrag(),this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,this.hoverID=""}clearActiveStatus(t=!0){this.clearPolygonDrag(),t&&this.deleteSelectedID()}clearDrawingStatus(){this.drawingPointList=[]}setPolygonList(t){const e=this.polygonList.length;this.polygonList=t,e!==t.length&&this.emit("updatePageNumber")}setSelectedID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}setAttrChangeTrigger(t=0){this.attrChangeTrigger=t}setDefaultAttribute(t=""){const e=this.defaultAttribute;if(this.defaultAttribute=t,(this.attrChangeTrigger||e!==t)&&(this.changeStyle(t),this.emit("changeAttributeSidebar"),this.selectedPolygons.length>0&&(this.selectedPolygons.forEach(i=>{i.attribute=t}),this.history.pushHistory(this.polygonList),this.render()),this._textAttributeInstance)){if(this.attributeLockList.length>0&&!this.attributeLockList.includes(t)){this._textAttributeInstance.clearTextAttribute();return}this._textAttributeInstance.updateIcon(this.getTextIconSvg(t))}}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}setPolygonValidAndRender(t,e){if(!t)return;const i=this.polygonList.map(s=>{var o;if(s.id===t){const r=(o=s==null?void 0:s.valid)!=null?o:!0;return __spreadProps(__spreadValues({},s),{valid:e!=null?e:!r})}return s});this.setPolygonList(i),this.history.pushHistory(this.polygonList),this.render(),this.emit("updateResult")}addDrawingPointToPolygonList(t){let{lowerLimitPointNum:e=3}=this.config;e<3&&(e=3);let i;if(this.drawingPointList.length<e){this.drawingPointList=[],this.editPolygonID="";return}if(PolygonUtils.calcPolygonSize(this.drawingPointList||[])<this.minArea){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.MinAreaLimitErrorNotice,this.lang)}${this.minArea}`);return}const s=CommonToolUtils.getSourceID(this.basicResult),o=[...this.polygonList];if(this.editPolygonID){const r=o.find(l=>l.id===this.editPolygonID);if(!r)return;r.pointList=this.drawingPointList,this.editPolygonID=""}else{const r=uuid(8,62);let l={id:r,sourceID:s,valid:!this.isCtrl,textAttribute:"",pointList:this.drawingPointList,attribute:this.defaultAttribute,order:CommonToolUtils.getMaxOrder(o.filter(n=>CommonToolUtils.isSameSourceID(n.sourceID,s)))+1};if(this.config.textConfigurable){let n="";n=AttributeUtils.default.getTextAttribute(this.polygonList.filter(a=>CommonToolUtils.isSameSourceID(a.sourceID,s)),this.config.textCheckType),l=__spreadProps(__spreadValues({},l),{textAttribute:n})}this.pattern===tool.EPolygonPattern.Rect&&t===!0&&(l=__spreadProps(__spreadValues({},l),{isRect:!0})),o.push(l),i=l,this.setSelectedIdAfterAddingDrawing(r)}this.setPolygonList(o),this.isCtrl=!1,this.drawingPointList=[],this.history.pushHistory(o),i&&this.emit("polygonCreated",i,this.zoom,this.currentPos)}setSelectedIdAfterAddingDrawing(t){this.drawingPointList.length!==0&&(this.config.textConfigurable?this.setSelectedID(t):this.deleteSelectedID())}getHoverID(t){var e;const i=this.getCoordinateUnderZoom(t),s=this.currentShowList.map(o=>__spreadProps(__spreadValues({},o),{pointList:AxisUtils.default.changePointListByZoom(o.pointList,this.zoom)}));return PolygonUtils.getHoverPolygonID(i,s,10,(e=this.config)==null?void 0:e.lineType)}getHoverEdgeIndex(t){var e;if(!this.selectedID)return-1;const i=this.selectedPolygon;if(!i)return-1;const s=this.getCoordinateUnderZoom(t),o=AxisUtils.default.changePointListByZoom(i.pointList,this.zoom);return PolygonUtils.getHoverEdgeIndex(s,o,(e=this.config)==null?void 0:e.lineType)}getHoverPointIndex(t){if(!this.selectedID)return-1;const e=this.selectedPolygon;if(!e)return-1;const i=this.getCoordinateUnderZoom(t),s=AxisUtils.default.changePointListByZoom(e.pointList,this.zoom);return AxisUtils.default.returnClosePointIndex(i,s)}deletePolygons(t){var e;if(!t||t.length===0)return;const i=this.polygonList.filter(s=>this.selection.isIdSelected(s.id));this.emit("deletedObject",{deletedObject:i[0],id:i[0].id}),this.setPolygonList(this.polygonList.filter(s=>!this.selection.isIdSelected(s.id))),this.history.pushHistory(this.polygonList),(e=this._textAttributeInstance)==null||e.clearTextAttribute(),this.emit("selectedChange"),this.render()}deletePolygonPoint(t){if(!this.selectedID)return;const{selectedPolygon:e}=this;if(!e)return;let{lowerLimitPointNum:i}=this.config;if(i<3&&(i=3),e.pointList.length<=i){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.LowerLimitErrorNotice,this.lang)}${i}`);return}e==null||e.pointList.splice(t,1),this.history.pushHistory(this.polygonList),this.render()}spaceKeydown(){var t,e,i;if(this.selectedID){if(((t=this.selectedPolygon)==null?void 0:t.isRect)===!0){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.UnableToReannotation,this.lang)}`);return}this.editPolygonID=this.selectedID,this.drawingPointList=(i=(e=this.selectedPolygon)==null?void 0:e.pointList)!=null?i:[],this.drawingHistory.empty(),this.drawingHistory.initRecord(this.drawingPointList),this.hoverID="",this.deleteSelectedID(),this.render()}}onTabKeyDown(t){t.preventDefault();let e=annotation.ESortDirection.ascend;t.shiftKey&&(e=annotation.ESortDirection.descend),this.switchToNextPolygon(e)}switchToNextPolygon(t){if(this.drawingPointList.length>0)return;const[e,i]=CommonToolUtils.getRenderResultList(this.polygonList,CommonToolUtils.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let s=[...e];i&&(s=[...s,...i]);const o=CanvasUtils.getViewPort(this.canvas,this.currentPos,this.zoom),r=s.map(n=>{var a,d,g,h;return __spreadProps(__spreadValues({},n),{x:(d=(a=n.pointList[0])==null?void 0:a.x)!=null?d:0,y:(h=(g=n.pointList[0])==null?void 0:g.y)!=null?h:0})}).filter(n=>CanvasUtils.inViewPort({x:n.x,y:n.y},o)),l=CommonToolUtils.getNextSelectedRectID(r,t,this.selectedID);if(l){this.setSelectedID(l.id);const{selectedPolygon:n}=this;n&&this.setDefaultAttribute(n.attribute)}this.render()}onKeyDown(t){if(!CommonToolUtils.hotkeyFilter(t)||super.onKeyDown(t)===!1||this.selection.triggerKeyboardEvent(t,this.setPolygonList.bind(this)))return;const{keyCode:e}=t;switch(e){case keyCode.Space:this.spaceKeydown();break;case keyCode.Esc:this.drawingPointList=[],this.editPolygonID="";break;case keyCode.F:this.selectedID&&this.setPolygonValidAndRender(this.selectedID);break;case keyCode.Z:if(t.altKey){this.onCombinedExecute();return}this.setIsHidden(!this.isHidden),this.render();break;case keyCode.Delete:this.deletePolygons(this.selectedIDs),this.render();break;case keyCode.Ctrl:this.isCtrl=!0;break;case keyCode.Alt:this.isAlt===!1&&(t.preventDefault(),this.isAlt=!0,this.render());break;case keyCode.Tab:{this.onTabKeyDown(t);break}case keyCode.X:t.altKey&&this.segment();break;default:{if(this.config.attributeConfigurable){const i=AttributeUtils.default.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}}onKeyUp(t){switch(super.onKeyUp(t),t.keyCode){case keyCode.Ctrl:this.isCtrl=!1;break;case keyCode.Alt:{const e=this.isAlt;this.isAlt=!1,e===!0&&this.render();break}}}rightMouseUp(t){if(this.drawingPointList.length>0){this.addDrawingPointToPolygonList();return}this.setSelectedID(this.hoverID,t.ctrlKey);const{selectedPolygon:e}=this;e&&this.setDefaultAttribute(e.attribute)}onLeftDblClick(t){if(this.hoverEdgeIndex>-1){const e=this.getCoordinateUnderZoom(t),{selectedPolygon:i}=this;if(!i)return;if(i.isRect===!0){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.ForbidAddNewPoint,this.lang)}`),this.clearPolygonDrag();return}const{dropFoot:s}=PolygonUtils.getClosestPoint(e,this.polygonListUnderZoom,this.config.lineType,tool.edgeAdsorptionScope);if(!s)return;const{upperLimitPointNum:o}=this.config;if(o&&i.pointList.length>=o){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.UpperLimitErrorNotice,this.lang)}${o}`),this.clearPolygonDrag();return}i==null||i.pointList.splice(this.hoverEdgeIndex+1,0,AxisUtils.default.changePointByZoom(s,1/this.zoom)),this.setPolygonDataByID(i,this.selectedID),this.history.pushHistory(this.polygonList),this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.render()}this.dragInfo=void 0}onRightDblClick(t){this.dragInfo=void 0,this.clearImgDrag();const e=this.getHoverID(t),i=this.getHoverPointIndex(t);if(this.hoverPointIndex>-1&&this.hoverPointIndex===i){this.deletePolygonPoint(i),this.dragInfo=void 0,this.hoverPointIndex=-1,this.render();return}this.hoverID===this.selectedID&&this.deletePolygons([e]),this.render()}onMouseDown(t){if(!(super.onMouseDown(t)||this.forbidMouseOperation||t.ctrlKey===!0))return this.dragMouseDown(t),!0}onMouseLeave(){this.dragInfo&&(this.emitUpdatePolygonByDrag(),this.clearPolygonDrag(),this.history.pushHistory(this.polygonList))}dragMouseDown(t){if(t.button!==0)return;const e=this.getHoverID(t);if(!this.selection.isIdSelected(e))return;let i=[0],s=annotation.EDragTarget.Plane;const o=this.selectedPolygons[0].pointList,r=this.getCoordinateUnderZoom(t);if(this.selectedPolygons.length===1){this.dragStatus=annotation.EDragStatus.Start;const l=this.getHoverPointIndex(t),n=this.getHoverEdgeIndex(t);l>-1?(s=annotation.EDragTarget.Point,i=[l]):n>-1&&this.hoverEdgeIndex>-1&&(s=annotation.EDragTarget.Line,i=[n,(n+1)%o.length])}this.dragInfo={dragStartCoord:r,dragTarget:s,initPointList:o,changePointIndex:i,originPolygon:this.selectedPolygon,selectedPolygons:this.selectedPolygons,dragPrevCoord:r,originPolygonList:this.polygonList}}segment(){var t;if(!this.selectedID||((t=this.config)==null?void 0:t.lineType)!==tool.ELineTypes.Line)return;const e=PolygonUtils.getPolygonPointList(this.selectedID,this.currentShowList),i=this.currentShowList.filter(r=>r.id!==this.selectedID);if(i.length===0||e.length===0)return;const s=PolygonUtils.getWrapPolygonIndex(e,i);let o=[...this.polygonList];if(s===-1){const r=PolygonUtils.segmentPolygonByPolygon(e,i);if(!r)return;const l=r.shift();if(!l)return;let n="",a=!0;const d=CommonToolUtils.getSourceID(this.basicResult);let g="";o=this.polygonList.map(h=>{var u,f;return h.id===this.selectedID?(n=h.attribute,a=(u=h==null?void 0:h.valid)!=null?u:!0,g=(f=h==null?void 0:h.textAttribute)!=null?f:"",__spreadProps(__spreadValues({},h),{pointList:l})):h}),r.length>0&&r.forEach((h,u)=>{o.push({sourceID:d,id:uuid(8,62),pointList:h,valid:a,order:CommonToolUtils.getMaxOrder(this.currentShowList)+1+u,attribute:n,textAttribute:g})})}else o[s].pointList=PolygonUtils.clipPolygonFromWrapPolygon(e,o[s].pointList),o=o.filter(r=>r.id!==this.selectedID);this.setPolygonList(o),this.history.pushHistory(o),this.render()}onCombinedExecute(){if(!this.selectedID){this.emit("messageInfo",lbUtils.i18n.t("PolygonsToBeCombinedNeedToBeSelected"));return}this.isCombined=!this.isCombined}combine(t){var e;const i=this.getHoverID(t);if(!i||!this.selectedID||this.selectedID===i)return;if(((e=this.config)==null?void 0:e.lineType)!==tool.ELineTypes.Line){this.emit("messageInfo",lbUtils.i18n.t("CurveModeDoesNotSupportCutting"));return}const s=this.polygonList.find(a=>a.id===this.selectedID),o=this.currentShowList.find(a=>a.id===i);if(!o||!s)return;const r=PolygonUtils.combinePolygonWithPolygon(s,o);if(!r)return;const{newPolygon:l,unionList:n}=r;if(n.length===1&&l){const a=this.polygonList.filter(d=>!n.includes(d.id)).map(d=>d.id===this.selectedID?l:d);this.setPolygonList(a),this.history.pushHistory(a),this.render(),this.emit("messageInfo",lbUtils.i18n.t("CombineSuccess"))}else this.emit("messageInfo",lbUtils.i18n.t("CombiningFailedNotify"));this.isCombined=!1}isPolygonOutSide(t){if(this.dependToolName&&this.basicCanvas&&this.basicResult){let l=!1;switch(this.dependToolName){case tool.EToolName.Rect:{l=t.filter(n=>!RectUtils.isInRect(n,this.basicResult)).length>0;break}case tool.EToolName.Polygon:{l=PolygonUtils.isPointListOutSidePolygon(t,this.basicResult.pointList,this.config.lineType);break}}return l}if(!this.imgInfo)return!1;const{left:e,top:i,right:s,bottom:o}=MathUtils.default.calcViewportBoundaries(AxisUtils.default.changePointListByZoom(t,this.zoom)),r=1e-5;return e<0||i<0||s>this.imgInfo.width+r||o>this.imgInfo.height+r}onDragMove(t){const e=this.polygonList.map(i=>{if(this.selectedIDs.includes(i.id)){const s=this.dragPolygon(t,i);if(!s)return i;const o=__spreadProps(__spreadValues({},i),{pointList:s});return i.isRect===!0&&this.pattern===tool.EPolygonPattern.Normal&&Object.assign(o,{isRect:!1}),o}return i});this.dragInfo.dragPrevCoord=this.getCoordinateUnderZoom(t),this.setPolygonList(e)}getDragOffset(t,e){var i;const s=this.getCoordinateUnderZoom(t),{dragTarget:o,dragPrevCoord:r,changePointIndex:l,initPointList:n,dragStartCoord:a}=this.dragInfo;if(this.pattern===tool.EPolygonPattern.Rect&&(e==null?void 0:e.isRect)===!0&&l&&[annotation.EDragTarget.Line].includes(o)){const d=MathUtils.default.getArrayIndex(l[0]-2,4),g=MathUtils.default.getArrayIndex(l[0]-1,4),h=[n[d],n[g]],u=MathUtils.default.getRectPerpendicularOffset(a,s,h);return{x:u.x/this.zoom,y:u.y/this.zoom}}return((i=this.dragInfo)==null?void 0:i.dragTarget)===annotation.EDragTarget.Plane?{x:(s.x-r.x)/this.zoom,y:(s.y-r.y)/this.zoom}:{x:(s.x-a.x)/this.zoom,y:(s.y-a.y)/this.zoom}}dragPolygon(t,e){let i=___default.default.cloneDeep(e==null?void 0:e.pointList);if(!i||!this.dragInfo)return;const{initPointList:s,dragTarget:o,changePointIndex:r}=this.dragInfo,l=this.getDragOffset(t,e);switch(this.dragStatus=annotation.EDragStatus.Move,o){case annotation.EDragTarget.Plane:i=i.map(n=>__spreadProps(__spreadValues({},n),{x:n.x+l.x,y:n.y+l.y}));break;case annotation.EDragTarget.Point:case annotation.EDragTarget.Line:i=i.map((n,a)=>r&&r.includes(a)?__spreadProps(__spreadValues({},n),{x:s[a].x+l.x,y:s[a].y+l.y}):n);break}if(this.pattern===tool.EPolygonPattern.Rect&&(e==null?void 0:e.isRect)===!0&&o===annotation.EDragTarget.Point&&r&&(i=MathUtils.default.getPointListFromPointOffset(s,r[0],l)),!(this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(i)))return i}determineTrigger(t){if(this.isDrag)return"drag";if(this.dragInfo)return"dragSingle";let e="";const i=this.getHoverID(t);this.hoverID!==i?(this.hoverID=i,e=""):e="move";let s=-1,o=-1;const{selectedID:r}=this;return r&&(s=this.getHoverPointIndex(t),s>-1?this.hoverPointIndex!==s?e="":e="noRender":(o=this.getHoverEdgeIndex(t),this.hoverEdgeIndex!==o?e="":e="noRender"),this.hoverEdgeIndex=o,this.hoverPointIndex=s),e}onMouseMove(t){requestAnimationFrame(()=>{const e=Date.now();if(e-this.lastMouseMoveTime<this.mouseMoveThrottle||(this.lastMouseMoveTime=e,super.onMouseMove(t,!1)||this.forbidMouseOperation||!this.imgInfo))return;if(this.drawingPointList.length>0){this.render();return}const i=this.determineTrigger(t);this.selectedIDs.length>0&&this.dragInfo&&this.onDragMove(t),this.render(i)})}leftMouseUpdateValid(t){const e=this.getHoverID(t);return this.drawingPointList.length===0&&t.ctrlKey===!0&&e?(this.setPolygonValidAndRender(e),!0):!1}stopDrag(){this.dragInfo=void 0,this.dragStatus=annotation.EDragStatus.Wait}leftMouseUp(t){this.leftMouseUpdateValid(t)||this.addPointInDrawing(t)}onMouseUp(t){if(this.isCombined){switch(t.button){case 0:this.combine(t);break;case 2:this.isCombined=!1;break;default:return}return}if(!(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)){if(this.dragInfo&&this.dragStatus===annotation.EDragStatus.Move){this.emit("updateResult"),this.emitUpdatePolygonByDrag(),this.stopDrag(),this.history.pushHistory(this.polygonList);return}if(this.dragInfo&&this.dragStatus===annotation.EDragStatus.Start){this.stopDrag();return}switch(t.button){case 0:{this.leftMouseUp(t);break}case 2:{this.rightMouseUp(t);break}}this.render()}}dragMouseUp(){this.dragStatus===annotation.EDragStatus.Start&&this.stopDrag()}exportData(){const{polygonList:t}=this;return[t,this.basicImgInfo]}getTextIconSvg(t=""){return AttributeUtils.default.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}getCurrentSelectedData(){const{selectedPolygon:t}=this;if(!t)return;const e=this.getColor(t.attribute),i=t.valid?e==null?void 0:e.valid.stroke:e==null?void 0:e.invalid.stroke;return{width:TEXT_MAX_WIDTH,textAttribute:t.textAttribute,color:i}}updateSelectedTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedID){let e=t;AttributeUtils.default.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",AttributeUtils.default.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setPolygonList(AttributeUtils.default.textChange(e,this.selectedID,this.polygonList)),this.emit("updateTextAttribute"),this.render()}}renderTextAttribute(){var t;const{selectedPolygon:e}=this;if(!this.ctx||this.config.textConfigurable===!1||!e)return;const{pointList:i,attribute:s,valid:o,textAttribute:r}=e,{x:l,y:n}=i[i.length-1],a=TEXT_MAX_WIDTH,d=AxisUtils.default.getOffsetCoordinate({x:l,y:n},this.currentPos,this.zoom),g=this.getColor(s),h=o?g==null?void 0:g.valid.stroke:g==null?void 0:g.invalid.stroke;this._textAttributeInstance||(this._textAttributeInstance=new textAttributeClass({width:a,container:this.container,icon:this.getTextIconSvg(s),color:h,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((t=this._textAttributeInstance)==null?void 0:t.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${r}`,{left:d.x,top:d.y,color:h,width:a})}renderStaticPolygon(){var t;this.isHidden===!1&&((t=this.visiblePolygonList)==null||t.forEach(e=>{var i,s,o,r,l,n,a;if([this.selectedID,this.editPolygonID].includes(e.id))return;const{textAttribute:d,attribute:g}=e,h=this.getColor(g),u=StyleUtils.getStrokeAndFill(h,e.valid),f=AxisUtils.default.changePointListByZoom(e.pointList||[],this.zoom,this.currentPos);DrawUtils.drawPolygonWithFillAndLine(this.canvas,f,{fillColor:u.fill,strokeColor:u.stroke,pointColor:"white",thickness:(s=(i=this.style)==null?void 0:i.width)!=null?s:2,lineCap:"round",isClose:!0,lineType:(o=this.config)==null?void 0:o.lineType});let y=`${(r=AttributeUtils.default.getAttributeShowText(g,this.config.attributeList))!=null?r:""}`;((l=this.config)==null?void 0:l.isShowOrder)&&(e==null?void 0:e.order)>0&&(y=`${e.order} ${y}`),(e==null?void 0:e.subAttribute)&&((n=this.config)==null?void 0:n.secondaryAttributeConfigurable)&&((a=this.config)==null?void 0:a.subAttributeList)&&TagUtils.getTagNameList(e.subAttribute,this.config.subAttributeList).forEach(p=>{y+=`
|
|
2
|
+
${p.keyName}: ${p.value.join("\u3001")}`}),DrawUtils.drawText(this.canvas,f[0],y,__spreadValues({color:u.stroke},annotation.DEFAULT_TEXT_OFFSET));const P=f[f.length-1];DrawUtils.drawText(this.canvas,{x:P.x+annotation.TEXT_ATTRIBUTE_OFFSET.x,y:P.y+annotation.TEXT_ATTRIBUTE_OFFSET.y},d,__spreadValues({color:u.stroke},annotation.DEFAULT_TEXT_OFFSET))}))}renderSelectedPolygons(){this.selectedPolygons.forEach(t=>{this.renderSelectedPolygon(t)})}renderSelectedPolygon(t){var e,i,s;if(t){const o=this.getColor(t.attribute),r=StyleUtils.getStrokeAndFill(o,t.valid,{isSelected:!0});DrawUtils.drawSelectedPolygonWithFillAndLine(this.canvas,AxisUtils.default.changePointListByZoom(t.pointList,this.zoom,this.currentPos),{fillColor:r.fill,strokeColor:r.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!0,lineType:(e=this.config)==null?void 0:e.lineType});let l=`${(i=AttributeUtils.default.getAttributeShowText(t.attribute,this.config.attributeList))!=null?i:""}`;((s=this.config)==null?void 0:s.isShowOrder)&&(t==null?void 0:t.order)>0&&(l=`${t.order} ${l}`),DrawUtils.drawText(this.canvas,AxisUtils.default.changePointByZoom(t.pointList[0],this.zoom,this.currentPos),l,__spreadValues({color:r.stroke},annotation.DEFAULT_TEXT_OFFSET)),this.renderTextAttribute()}}renderHoverPolygon(){var t;if(this.hoverID&&this.hoverID!==this.editPolygonID){const{hoverPolygon:e}=this;if(e){let i="";const s=this.getColor(e.attribute);e.valid?i=s.validHover.fill:i=StyleUtils.getStrokeAndFill(s,!1,{isHover:!0}).fill,DrawUtils.drawPolygonWithFill(this.canvas,AxisUtils.default.changePointListByZoom(e.pointList,this.zoom,this.currentPos),{color:i,lineType:(t=this.config)==null?void 0:t.lineType})}}}renderPolygon(t){var e,i,s,o;t!=="move"&&this.renderStaticPolygon(),this.renderHoverPolygon(),this.renderSelectedPolygons();const r=this.getColor(this.defaultAttribute),l=StyleUtils.getStrokeAndFill(r,!this.isCtrl);if(((e=this.drawingPointList)==null?void 0:e.length)>0){let n=[...this.drawingPointList],a=AxisUtils.default.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);const d=this.getPointListByRectDrawing(a,n).value;if(d)n=d;else{if(((i=this.config)==null?void 0:i.edgeAdsorption)&&this.isAlt===!1){const{dropFoot:h}=PolygonUtils.getClosestPoint(a,this.polygonList,(s=this.config)==null?void 0:s.lineType,tool.edgeAdsorptionScope/this.zoom);h&&(a=h)}n.push(a)}const g=AxisUtils.default.changePointListByZoom(n,this.zoom,this.currentPos);DrawUtils.drawSelectedPolygonWithFillAndLine(this.canvas,g,{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!1,lineType:this.config.lineType}),this.renderPolygonHook(g)}if(this.hoverPointIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const a=StyleUtils.getStrokeAndFill(r,n.valid,{isSelected:!0}),d=n==null?void 0:n.pointList[this.hoverPointIndex];if(d){const{x:g,y:h}=AxisUtils.default.changePointByZoom(d,this.zoom,this.currentPos);DrawUtils.drawCircleWithFill(this.canvas,{x:g,y:h},5,{color:a.fill})}}if(this.hoverEdgeIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const a=StyleUtils.getStrokeAndFill(r,n.valid,{isSelected:!0});DrawUtils.drawLineWithPointList(this.canvas,AxisUtils.default.changePointListByZoom(n.pointList,this.zoom,this.currentPos),{color:a.stroke,thickness:10,hoverEdgeIndex:this.hoverEdgeIndex,lineType:(o=this.config)==null?void 0:o.lineType})}}render(t){!this.ctx||(t!=="move"&&super.render(),t!=="drag"&&t!=="dragSingle"&&this.renderPolygon(t),t==="dragSingle"&&this.renderSelectedPolygons(),this.renderCursorLine(this.getLineColor(this.defaultAttribute)))}renderCursorLine(t){requestAnimationFrame(()=>{this.clearOffscreenCanvas();const{x:e,y:i}=this.coord;if(DrawUtils.drawLine(this.offscreenCanvas,{x:0,y:i},{x:1e4,y:i},{color:t}),DrawUtils.drawLine(this.offscreenCanvas,{x:e,y:0},{x:e,y:1e4},{color:t}),DrawUtils.drawCircleWithFill(this.offscreenCanvas,{x:e,y:i},1,{color:"white"}),this.isCombined){const s=10,o=186,r=32;DrawUtils.drawRectWithFill(this.canvas,{x:e+s,y:i-s*4-1,width:o,height:r},{color:"black"}),DrawUtils.drawText(this.canvas,{x:e,y:i},lbUtils.i18n.t("ClickAnotherPolygon"),{textAlign:"center",color:"white",offsetX:o/2+s,offsetY:-(r/2+s/2)}),DrawUtils.drawRect(this.canvas,{x:e-s,y:i-s,width:s*2,height:s*2},{lineDash:[6],color:"white"})}})}undo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.undo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.undo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}redo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.redo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.redo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}deleteSelectedID(){this.setSelectedID("")}addPointInDrawingHook(...t){return{continue:!0}}renderPolygonHook(...t){return{continue:!0}}getPointListByRectDrawing(...t){return{continue:!0}}}module.exports=PolygonOperation;
|
|
@@ -59,6 +59,9 @@ declare class PolygonOperation extends BasicToolOperation {
|
|
|
59
59
|
attribute: string;
|
|
60
60
|
isRect?: boolean | undefined;
|
|
61
61
|
trackID?: number | undefined;
|
|
62
|
+
subAttribute?: {
|
|
63
|
+
[key: string]: string;
|
|
64
|
+
} | undefined;
|
|
62
65
|
}[];
|
|
63
66
|
get selectedText(): string | undefined;
|
|
64
67
|
isAllowDouble: (e: MouseEvent) => boolean;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
import j from"lodash";import{i18n as A}from"@labelbee/lb-utils";import T from"../../utils/MathUtils.js";import N from"../../utils/tool/RectUtils.js";import{ERotateDirection as K,ESortDirection as U,EDragTarget as v,EDragStatus as b,TEXT_ATTRIBUTE_OFFSET as E,DEFAULT_TEXT_OFFSET as k}from"../../constant/annotation.js";import m from"../../constant/keyCode.js";import{EPolygonPattern as _,edgeAdsorptionScope as R,ELineTypes as H,EToolName as M}from"../../constant/tool.js";import x from"../../locales/index.js";import{EMessage as w}from"../../locales/constants.js";import W from"../../utils/ActionsHistory.js";import D from"../../utils/tool/AttributeUtils.js";import c from"../../utils/tool/AxisUtils.js";import O from"../../utils/tool/CanvasUtils.js";import y from"../../utils/tool/CommonToolUtils.js";import P from"../../utils/tool/DrawUtils.js";import u from"../../utils/tool/PolygonUtils.js";import C from"../../utils/tool/StyleUtils.js";import B from"../../utils/uuid.js";import{BasicToolOperation as $}from"./basicToolOperation.js";import V from"./textAttributeClass.js";import X from"./Selection.js";var q=Object.defineProperty,Y=Object.defineProperties,G=Object.getOwnPropertyDescriptors,F=Object.getOwnPropertySymbols,J=Object.prototype.hasOwnProperty,Q=Object.prototype.propertyIsEnumerable,Z=(I,t,e)=>t in I?q(I,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):I[t]=e,f=(I,t)=>{for(var e in t||(t={}))J.call(t,e)&&Z(I,e,t[e]);if(F)for(var e of F(t))Q.call(t,e)&&Z(I,e,t[e]);return I},L=(I,t)=>Y(I,G(t));const z=164;class tt extends ${constructor(t){super(L(f({},t),{isOffscreenCanvas:!0}));this.isAllowDouble=e=>{const{selectedID:i}=this,s=this.getHoverID(e);return!!(i&&i===s)},this.textChange=e=>{this.config.textConfigurable===!1||!this.selectedID||(this.setPolygonList(D.textChange(e,this.selectedID,this.polygonList)),this.emit("selectedChange"),this.render())},this.lastMouseMoveTime=0,this.mouseMoveThrottle=16,this.emitUpdatePolygonByDrag=()=>{if(this.dragInfo){const{originPolygon:e}=this.dragInfo;this.emit("updatePolygonByDrag",[{newPolygon:this.selectedPolygon,originPolygon:e}])}},this.config=y.jsonParser(t.config),this.drawingPointList=[],this.polygonList=[],this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.attrChangeTrigger=0,this.drawingHistory=new W,this.isCtrl=!1,this.isAlt=!1,this.isCombined=!1,this.pattern=_.Normal,this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.selection=new X(this)}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}get minArea(){return this.config.minArea||0}eventBinding(){super.eventBinding(),this.container.removeEventListener("mouseup",this.onMouseUp),this.container.addEventListener("mouseup",this.dragMouseUp),this.dblClickListener.addEvent(this.onMouseUp,this.onLeftDblClick,this.onRightDblClick,this.isAllowDouble)}eventUnbinding(){super.eventUnbinding(),this.container.removeEventListener("mouseup",this.dragMouseUp)}destroy(){super.destroy(),this._textAttributeInstance&&this._textAttributeInstance.clearTextAttribute()}get selectedPolygon(){return u.getPolygonByID(this.visiblePolygonList,this.selectedID)}get selectedPolygons(){return u.getPolygonByIDs(this.polygonList,this.selectedIDs)}get hoverPolygon(){return this.visiblePolygonList.find(t=>t.id===this.hoverID&&t.id!==this.selectedID)}get enableDrag(){return Boolean(this.selectedID&&this.dragInfo)}get visiblePolygonList(){return this.polygonList}get polygonListUnderZoom(){return this.visiblePolygonList.map(t=>L(f({},t),{pointList:c.changePointListByZoom(t.pointList,this.zoom)}))}get selectedText(){var t;return(t=this.selectedPolygon)==null?void 0:t.textAttribute}get dataList(){return this.polygonList}setPattern(t,e=!1){var i;((i=this.drawingPointList)==null?void 0:i.length)>0&&e===!0||(this.pattern=t)}get currentShowList(){let t=[];const[e,i]=y.getRenderResultList(this.polygonList,y.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);return t=e,this.isHidden&&(t=[]),i&&t.push(...i),t}get currentPageResult(){const[t]=y.getRenderResultList(this.polygonList,y.getSourceID(this.basicResult),[]);return t}setResultAndSelectedID(t,e){this.setPolygonList(t),e&&this.setSelectedID(e),this.render()}setResult(t,e=!0){this.clearActiveStatus(e),this.setPolygonList(t),this.render()}setPolygonDataByID(t,e){return this.polygonList.map(i=>i.id===e?f(f({},i),t):i)}rotatePolygon(t=1,e=K.Clockwise,i=this.selectedID){if(!i)return;const s=u.getPolygonByID(this.polygonList,i);if(!s)return;const o=u.updatePolygonByRotate(e,t,s==null?void 0:s.pointList);this.setPolygonList(this.setPolygonDataByID({pointList:o},i)),this.render()}rotatePolygonEdge(t,e=this.selectedID){!e||!u.getPolygonByID(this.polygonList,e)||(this.setPolygonList(this.setPolygonDataByID({pointList:t},e)),this.render())}addPointInDrawing(t){if(!this.imgInfo||this.forbidAddNewPolygonFuc&&this.forbidAddNewPolygonFuc(t))return;const{upperLimitPointNum:e,edgeAdsorption:i}=this.config;if(e&&this.drawingPointList.length>=e){this.emit("messageInfo",`${x.getMessagesByLocale(w.UpperLimitErrorNotice,this.lang)}${e}`);return}const s=this.getCoordinateUnderZoom(t),o=c.changeDrawOutsideTarget(s,{x:0,y:0},this.imgInfo,this.config.drawOutsideTarget,this.basicResult,this.zoom);if(c.returnClosePointIndex(o,c.changePointListByZoom(this.drawingPointList,this.zoom))===0){this.addDrawingPointToPolygonList();return}const{dropFoot:l}=u.getClosestPoint(o,this.polygonListUnderZoom,this.config.lineType,R),n=c.changePointByZoom(l&&t.altKey===!1&&i?l:o,1/this.zoom);!this.addPointInDrawingHook(n).continue||(this.drawingPointList.push(n),this.drawingPointList.length===1?this.drawingHistory.initRecord(this.drawingPointList):this.drawingHistory.pushHistory(this.drawingPointList))}clearResult(){this.setPolygonList([]),this.deleteSelectedID(),this.render()}clearPolygonDrag(){this.drawingPointList=[],this.stopDrag(),this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,this.hoverID=""}clearActiveStatus(t=!0){this.clearPolygonDrag(),t&&this.deleteSelectedID()}clearDrawingStatus(){this.drawingPointList=[]}setPolygonList(t){const e=this.polygonList.length;this.polygonList=t,e!==t.length&&this.emit("updatePageNumber")}setSelectedID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}setAttrChangeTrigger(t=0){this.attrChangeTrigger=t}setDefaultAttribute(t=""){const e=this.defaultAttribute;if(this.defaultAttribute=t,(this.attrChangeTrigger||e!==t)&&(this.changeStyle(t),this.emit("changeAttributeSidebar"),this.selectedPolygons.length>0&&(this.selectedPolygons.forEach(i=>{i.attribute=t}),this.history.pushHistory(this.polygonList),this.render()),this._textAttributeInstance)){if(this.attributeLockList.length>0&&!this.attributeLockList.includes(t)){this._textAttributeInstance.clearTextAttribute();return}this._textAttributeInstance.updateIcon(this.getTextIconSvg(t))}}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}setPolygonValidAndRender(t,e){if(!t)return;const i=this.polygonList.map(s=>{var o;if(s.id===t){const r=(o=s==null?void 0:s.valid)!=null?o:!0;return L(f({},s),{valid:e!=null?e:!r})}return s});this.setPolygonList(i),this.history.pushHistory(this.polygonList),this.render(),this.emit("updateResult")}addDrawingPointToPolygonList(t){let{lowerLimitPointNum:e=3}=this.config;e<3&&(e=3);let i;if(this.drawingPointList.length<e){this.drawingPointList=[],this.editPolygonID="";return}if(u.calcPolygonSize(this.drawingPointList||[])<this.minArea){this.emit("messageInfo",`${x.getMessagesByLocale(w.MinAreaLimitErrorNotice,this.lang)}${this.minArea}`);return}const s=y.getSourceID(this.basicResult),o=[...this.polygonList];if(this.editPolygonID){const r=o.find(l=>l.id===this.editPolygonID);if(!r)return;r.pointList=this.drawingPointList,this.editPolygonID=""}else{const r=B(8,62);let l={id:r,sourceID:s,valid:!this.isCtrl,textAttribute:"",pointList:this.drawingPointList,attribute:this.defaultAttribute,order:y.getMaxOrder(o.filter(n=>y.isSameSourceID(n.sourceID,s)))+1};if(this.config.textConfigurable){let n="";n=D.getTextAttribute(this.polygonList.filter(h=>y.isSameSourceID(h.sourceID,s)),this.config.textCheckType),l=L(f({},l),{textAttribute:n})}this.pattern===_.Rect&&t===!0&&(l=L(f({},l),{isRect:!0})),o.push(l),i=l,this.setSelectedIdAfterAddingDrawing(r)}this.setPolygonList(o),this.isCtrl=!1,this.drawingPointList=[],this.history.pushHistory(o),i&&this.emit("polygonCreated",i,this.zoom,this.currentPos)}setSelectedIdAfterAddingDrawing(t){this.drawingPointList.length!==0&&(this.config.textConfigurable?this.setSelectedID(t):this.deleteSelectedID())}getHoverID(t){var e;const i=this.getCoordinateUnderZoom(t),s=this.currentShowList.map(o=>L(f({},o),{pointList:c.changePointListByZoom(o.pointList,this.zoom)}));return u.getHoverPolygonID(i,s,10,(e=this.config)==null?void 0:e.lineType)}getHoverEdgeIndex(t){var e;if(!this.selectedID)return-1;const i=this.selectedPolygon;if(!i)return-1;const s=this.getCoordinateUnderZoom(t),o=c.changePointListByZoom(i.pointList,this.zoom);return u.getHoverEdgeIndex(s,o,(e=this.config)==null?void 0:e.lineType)}getHoverPointIndex(t){if(!this.selectedID)return-1;const e=this.selectedPolygon;if(!e)return-1;const i=this.getCoordinateUnderZoom(t),s=c.changePointListByZoom(e.pointList,this.zoom);return c.returnClosePointIndex(i,s)}deletePolygons(t){var e;if(!t||t.length===0)return;const i=this.polygonList.filter(s=>this.selection.isIdSelected(s.id));this.emit("deletedObject",{deletedObject:i[0],id:i[0].id}),this.setPolygonList(this.polygonList.filter(s=>!this.selection.isIdSelected(s.id))),this.history.pushHistory(this.polygonList),(e=this._textAttributeInstance)==null||e.clearTextAttribute(),this.emit("selectedChange"),this.render()}deletePolygonPoint(t){if(!this.selectedID)return;const{selectedPolygon:e}=this;if(!e)return;let{lowerLimitPointNum:i}=this.config;if(i<3&&(i=3),e.pointList.length<=i){this.emit("messageInfo",`${x.getMessagesByLocale(w.LowerLimitErrorNotice,this.lang)}${i}`);return}e==null||e.pointList.splice(t,1),this.history.pushHistory(this.polygonList),this.render()}spaceKeydown(){var t,e,i;if(this.selectedID){if(((t=this.selectedPolygon)==null?void 0:t.isRect)===!0){this.emit("messageInfo",`${x.getMessagesByLocale(w.UnableToReannotation,this.lang)}`);return}this.editPolygonID=this.selectedID,this.drawingPointList=(i=(e=this.selectedPolygon)==null?void 0:e.pointList)!=null?i:[],this.drawingHistory.empty(),this.drawingHistory.initRecord(this.drawingPointList),this.hoverID="",this.deleteSelectedID(),this.render()}}onTabKeyDown(t){t.preventDefault();let e=U.ascend;t.shiftKey&&(e=U.descend),this.switchToNextPolygon(e)}switchToNextPolygon(t){if(this.drawingPointList.length>0)return;const[e,i]=y.getRenderResultList(this.polygonList,y.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let s=[...e];i&&(s=[...s,...i]);const o=O.getViewPort(this.canvas,this.currentPos,this.zoom),r=s.map(n=>{var h,d,g,a;return L(f({},n),{x:(d=(h=n.pointList[0])==null?void 0:h.x)!=null?d:0,y:(a=(g=n.pointList[0])==null?void 0:g.y)!=null?a:0})}).filter(n=>O.inViewPort({x:n.x,y:n.y},o)),l=y.getNextSelectedRectID(r,t,this.selectedID);if(l){this.setSelectedID(l.id);const{selectedPolygon:n}=this;n&&this.setDefaultAttribute(n.attribute)}this.render()}onKeyDown(t){if(!y.hotkeyFilter(t)||super.onKeyDown(t)===!1||this.selection.triggerKeyboardEvent(t,this.setPolygonList.bind(this)))return;const{keyCode:e}=t;switch(e){case m.Space:this.spaceKeydown();break;case m.Esc:this.drawingPointList=[],this.editPolygonID="";break;case m.F:this.selectedID&&this.setPolygonValidAndRender(this.selectedID);break;case m.Z:if(t.altKey){this.onCombinedExecute();return}this.setIsHidden(!this.isHidden),this.render();break;case m.Delete:this.deletePolygons(this.selectedIDs),this.render();break;case m.Ctrl:this.isCtrl=!0;break;case m.Alt:this.isAlt===!1&&(t.preventDefault(),this.isAlt=!0,this.render());break;case m.Tab:{this.onTabKeyDown(t);break}case m.X:t.altKey&&this.segment();break;default:{if(this.config.attributeConfigurable){const i=D.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}}onKeyUp(t){switch(super.onKeyUp(t),t.keyCode){case m.Ctrl:this.isCtrl=!1;break;case m.Alt:{const e=this.isAlt;this.isAlt=!1,e===!0&&this.render();break}}}rightMouseUp(t){if(this.drawingPointList.length>0){this.addDrawingPointToPolygonList();return}this.setSelectedID(this.hoverID,t.ctrlKey);const{selectedPolygon:e}=this;e&&this.setDefaultAttribute(e.attribute)}onLeftDblClick(t){if(this.hoverEdgeIndex>-1){const e=this.getCoordinateUnderZoom(t),{selectedPolygon:i}=this;if(!i)return;if(i.isRect===!0){this.emit("messageInfo",`${x.getMessagesByLocale(w.ForbidAddNewPoint,this.lang)}`),this.clearPolygonDrag();return}const{dropFoot:s}=u.getClosestPoint(e,this.polygonListUnderZoom,this.config.lineType,R);if(!s)return;const{upperLimitPointNum:o}=this.config;if(o&&i.pointList.length>=o){this.emit("messageInfo",`${x.getMessagesByLocale(w.UpperLimitErrorNotice,this.lang)}${o}`),this.clearPolygonDrag();return}i==null||i.pointList.splice(this.hoverEdgeIndex+1,0,c.changePointByZoom(s,1/this.zoom)),this.setPolygonDataByID(i,this.selectedID),this.history.pushHistory(this.polygonList),this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.render()}this.dragInfo=void 0}onRightDblClick(t){this.dragInfo=void 0,this.clearImgDrag();const e=this.getHoverID(t),i=this.getHoverPointIndex(t);if(this.hoverPointIndex>-1&&this.hoverPointIndex===i){this.deletePolygonPoint(i),this.dragInfo=void 0,this.hoverPointIndex=-1,this.render();return}this.hoverID===this.selectedID&&this.deletePolygons([e]),this.render()}onMouseDown(t){if(!(super.onMouseDown(t)||this.forbidMouseOperation||t.ctrlKey===!0))return this.dragMouseDown(t),!0}onMouseLeave(){this.dragInfo&&(this.emitUpdatePolygonByDrag(),this.clearPolygonDrag(),this.history.pushHistory(this.polygonList))}dragMouseDown(t){if(t.button!==0)return;const e=this.getHoverID(t);if(!this.selection.isIdSelected(e))return;let i=[0],s=v.Plane;const o=this.selectedPolygons[0].pointList,r=this.getCoordinateUnderZoom(t);if(this.selectedPolygons.length===1){this.dragStatus=b.Start;const l=this.getHoverPointIndex(t),n=this.getHoverEdgeIndex(t);l>-1?(s=v.Point,i=[l]):n>-1&&this.hoverEdgeIndex>-1&&(s=v.Line,i=[n,(n+1)%o.length])}this.dragInfo={dragStartCoord:r,dragTarget:s,initPointList:o,changePointIndex:i,originPolygon:this.selectedPolygon,selectedPolygons:this.selectedPolygons,dragPrevCoord:r,originPolygonList:this.polygonList}}segment(){var t;if(!this.selectedID||((t=this.config)==null?void 0:t.lineType)!==H.Line)return;const e=u.getPolygonPointList(this.selectedID,this.currentShowList),i=this.currentShowList.filter(r=>r.id!==this.selectedID);if(i.length===0||e.length===0)return;const s=u.getWrapPolygonIndex(e,i);let o=[...this.polygonList];if(s===-1){const r=u.segmentPolygonByPolygon(e,i);if(!r)return;const l=r.shift();if(!l)return;let n="",h=!0;const d=y.getSourceID(this.basicResult);let g="";o=this.polygonList.map(a=>{var p,S;return a.id===this.selectedID?(n=a.attribute,h=(p=a==null?void 0:a.valid)!=null?p:!0,g=(S=a==null?void 0:a.textAttribute)!=null?S:"",L(f({},a),{pointList:l})):a}),r.length>0&&r.forEach((a,p)=>{o.push({sourceID:d,id:B(8,62),pointList:a,valid:h,order:y.getMaxOrder(this.currentShowList)+1+p,attribute:n,textAttribute:g})})}else o[s].pointList=u.clipPolygonFromWrapPolygon(e,o[s].pointList),o=o.filter(r=>r.id!==this.selectedID);this.setPolygonList(o),this.history.pushHistory(o),this.render()}onCombinedExecute(){if(!this.selectedID){this.emit("messageInfo",A.t("PolygonsToBeCombinedNeedToBeSelected"));return}this.isCombined=!this.isCombined}combine(t){var e;const i=this.getHoverID(t);if(!i||!this.selectedID||this.selectedID===i)return;if(((e=this.config)==null?void 0:e.lineType)!==H.Line){this.emit("messageInfo",A.t("CurveModeDoesNotSupportCutting"));return}const s=this.polygonList.find(h=>h.id===this.selectedID),o=this.currentShowList.find(h=>h.id===i);if(!o||!s)return;const r=u.combinePolygonWithPolygon(s,o);if(!r)return;const{newPolygon:l,unionList:n}=r;if(n.length===1&&l){const h=this.polygonList.filter(d=>!n.includes(d.id)).map(d=>d.id===this.selectedID?l:d);this.setPolygonList(h),this.history.pushHistory(h),this.render(),this.emit("messageInfo",A.t("CombineSuccess"))}else this.emit("messageInfo",A.t("CombiningFailedNotify"));this.isCombined=!1}isPolygonOutSide(t){if(this.dependToolName&&this.basicCanvas&&this.basicResult){let l=!1;switch(this.dependToolName){case M.Rect:{l=t.filter(n=>!N.isInRect(n,this.basicResult)).length>0;break}case M.Polygon:{l=u.isPointListOutSidePolygon(t,this.basicResult.pointList,this.config.lineType);break}}return l}if(!this.imgInfo)return!1;const{left:e,top:i,right:s,bottom:o}=T.calcViewportBoundaries(c.changePointListByZoom(t,this.zoom)),r=1e-5;return e<0||i<0||s>this.imgInfo.width+r||o>this.imgInfo.height+r}onDragMove(t){const e=this.polygonList.map(i=>{if(this.selectedIDs.includes(i.id)){const s=this.dragPolygon(t,i);if(!s)return i;const o=L(f({},i),{pointList:s});return i.isRect===!0&&this.pattern===_.Normal&&Object.assign(o,{isRect:!1}),o}return i});this.dragInfo.dragPrevCoord=this.getCoordinateUnderZoom(t),this.setPolygonList(e)}getDragOffset(t,e){var i;const s=this.getCoordinateUnderZoom(t),{dragTarget:o,dragPrevCoord:r,changePointIndex:l,initPointList:n,dragStartCoord:h}=this.dragInfo;if(this.pattern===_.Rect&&(e==null?void 0:e.isRect)===!0&&l&&[v.Line].includes(o)){const d=T.getArrayIndex(l[0]-2,4),g=T.getArrayIndex(l[0]-1,4),a=[n[d],n[g]],p=T.getRectPerpendicularOffset(h,s,a);return{x:p.x/this.zoom,y:p.y/this.zoom}}return((i=this.dragInfo)==null?void 0:i.dragTarget)===v.Plane?{x:(s.x-r.x)/this.zoom,y:(s.y-r.y)/this.zoom}:{x:(s.x-h.x)/this.zoom,y:(s.y-h.y)/this.zoom}}dragPolygon(t,e){let i=j.cloneDeep(e==null?void 0:e.pointList);if(!i||!this.dragInfo)return;const{initPointList:s,dragTarget:o,changePointIndex:r}=this.dragInfo,l=this.getDragOffset(t,e);switch(this.dragStatus=b.Move,o){case v.Plane:i=i.map(n=>L(f({},n),{x:n.x+l.x,y:n.y+l.y}));break;case v.Point:case v.Line:i=i.map((n,h)=>r&&r.includes(h)?L(f({},n),{x:s[h].x+l.x,y:s[h].y+l.y}):n);break}if(this.pattern===_.Rect&&(e==null?void 0:e.isRect)===!0&&o===v.Point&&r&&(i=T.getPointListFromPointOffset(s,r[0],l)),!(this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(i)))return i}determineTrigger(t){if(this.isDrag)return"drag";if(this.dragInfo)return"dragSingle";let e="";const i=this.getHoverID(t);this.hoverID!==i?(this.hoverID=i,e=""):e="move";let s=-1,o=-1;const{selectedID:r}=this;return r&&(s=this.getHoverPointIndex(t),s>-1?this.hoverPointIndex!==s?e="":e="noRender":(o=this.getHoverEdgeIndex(t),this.hoverEdgeIndex!==o?e="":e="noRender"),this.hoverEdgeIndex=o,this.hoverPointIndex=s),e}onMouseMove(t){requestAnimationFrame(()=>{const e=Date.now();if(e-this.lastMouseMoveTime<this.mouseMoveThrottle||(this.lastMouseMoveTime=e,super.onMouseMove(t,!1)||this.forbidMouseOperation||!this.imgInfo))return;if(this.drawingPointList.length>0){this.render();return}const i=this.determineTrigger(t);this.selectedIDs.length>0&&this.dragInfo&&this.onDragMove(t),this.render(i)})}leftMouseUpdateValid(t){const e=this.getHoverID(t);return this.drawingPointList.length===0&&t.ctrlKey===!0&&e?(this.setPolygonValidAndRender(e),!0):!1}stopDrag(){this.dragInfo=void 0,this.dragStatus=b.Wait}leftMouseUp(t){this.leftMouseUpdateValid(t)||this.addPointInDrawing(t)}onMouseUp(t){if(this.isCombined){switch(t.button){case 0:this.combine(t);break;case 2:this.isCombined=!1;break;default:return}return}if(!(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)){if(this.dragInfo&&this.dragStatus===b.Move){this.emit("updateResult"),this.emitUpdatePolygonByDrag(),this.stopDrag(),this.history.pushHistory(this.polygonList);return}if(this.dragInfo&&this.dragStatus===b.Start){this.stopDrag();return}switch(t.button){case 0:{this.leftMouseUp(t);break}case 2:{this.rightMouseUp(t);break}}this.render()}}dragMouseUp(){this.dragStatus===b.Start&&this.stopDrag()}exportData(){const{polygonList:t}=this;return[t,this.basicImgInfo]}getTextIconSvg(t=""){return D.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}getCurrentSelectedData(){const{selectedPolygon:t}=this;if(!t)return;const e=this.getColor(t.attribute),i=t.valid?e==null?void 0:e.valid.stroke:e==null?void 0:e.invalid.stroke;return{width:z,textAttribute:t.textAttribute,color:i}}updateSelectedTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedID){let e=t;D.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",D.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setPolygonList(D.textChange(e,this.selectedID,this.polygonList)),this.emit("updateTextAttribute"),this.render()}}renderTextAttribute(){var t;const{selectedPolygon:e}=this;if(!this.ctx||this.config.textConfigurable===!1||!e)return;const{pointList:i,attribute:s,valid:o,textAttribute:r}=e,{x:l,y:n}=i[i.length-1],h=z,d=c.getOffsetCoordinate({x:l,y:n},this.currentPos,this.zoom),g=this.getColor(s),a=o?g==null?void 0:g.valid.stroke:g==null?void 0:g.invalid.stroke;this._textAttributeInstance||(this._textAttributeInstance=new V({width:h,container:this.container,icon:this.getTextIconSvg(s),color:a,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((t=this._textAttributeInstance)==null?void 0:t.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${r}`,{left:d.x,top:d.y,color:a,width:h})}renderStaticPolygon(){var t;this.isHidden===!1&&((t=this.visiblePolygonList)==null||t.forEach(e=>{var i,s,o,r,l;if([this.selectedID,this.editPolygonID].includes(e.id))return;const{textAttribute:n,attribute:h}=e,d=this.getColor(h),g=C.getStrokeAndFill(d,e.valid),a=c.changePointListByZoom(e.pointList||[],this.zoom,this.currentPos);P.drawPolygonWithFillAndLine(this.canvas,a,{fillColor:g.fill,strokeColor:g.stroke,pointColor:"white",thickness:(s=(i=this.style)==null?void 0:i.width)!=null?s:2,lineCap:"round",isClose:!0,lineType:(o=this.config)==null?void 0:o.lineType});let p=`${(r=D.getAttributeShowText(h,this.config.attributeList))!=null?r:""}`;((l=this.config)==null?void 0:l.isShowOrder)&&(e==null?void 0:e.order)>0&&(p=`${e.order} ${p}`),P.drawText(this.canvas,a[0],p,f({color:g.stroke},k));const S=a[a.length-1];P.drawText(this.canvas,{x:S.x+E.x,y:S.y+E.y},n,f({color:g.stroke},k))}))}renderSelectedPolygons(){this.selectedPolygons.forEach(t=>{this.renderSelectedPolygon(t)})}renderSelectedPolygon(t){var e,i,s;if(t){const o=this.getColor(t.attribute),r=C.getStrokeAndFill(o,t.valid,{isSelected:!0});P.drawSelectedPolygonWithFillAndLine(this.canvas,c.changePointListByZoom(t.pointList,this.zoom,this.currentPos),{fillColor:r.fill,strokeColor:r.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!0,lineType:(e=this.config)==null?void 0:e.lineType});let l=`${(i=D.getAttributeShowText(t.attribute,this.config.attributeList))!=null?i:""}`;((s=this.config)==null?void 0:s.isShowOrder)&&(t==null?void 0:t.order)>0&&(l=`${t.order} ${l}`),P.drawText(this.canvas,c.changePointByZoom(t.pointList[0],this.zoom,this.currentPos),l,f({color:r.stroke},k)),this.renderTextAttribute()}}renderHoverPolygon(){var t;if(this.hoverID&&this.hoverID!==this.editPolygonID){const{hoverPolygon:e}=this;if(e){let i="";const s=this.getColor(e.attribute);e.valid?i=s.validHover.fill:i=C.getStrokeAndFill(s,!1,{isHover:!0}).fill,P.drawPolygonWithFill(this.canvas,c.changePointListByZoom(e.pointList,this.zoom,this.currentPos),{color:i,lineType:(t=this.config)==null?void 0:t.lineType})}}}renderPolygon(t){var e,i,s,o;t!=="move"&&this.renderStaticPolygon(),this.renderHoverPolygon(),this.renderSelectedPolygons();const r=this.getColor(this.defaultAttribute),l=C.getStrokeAndFill(r,!this.isCtrl);if(((e=this.drawingPointList)==null?void 0:e.length)>0){let n=[...this.drawingPointList],h=c.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);const d=this.getPointListByRectDrawing(h,n).value;if(d)n=d;else{if(((i=this.config)==null?void 0:i.edgeAdsorption)&&this.isAlt===!1){const{dropFoot:a}=u.getClosestPoint(h,this.polygonList,(s=this.config)==null?void 0:s.lineType,R/this.zoom);a&&(h=a)}n.push(h)}const g=c.changePointListByZoom(n,this.zoom,this.currentPos);P.drawSelectedPolygonWithFillAndLine(this.canvas,g,{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!1,lineType:this.config.lineType}),this.renderPolygonHook(g)}if(this.hoverPointIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const h=C.getStrokeAndFill(r,n.valid,{isSelected:!0}),d=n==null?void 0:n.pointList[this.hoverPointIndex];if(d){const{x:g,y:a}=c.changePointByZoom(d,this.zoom,this.currentPos);P.drawCircleWithFill(this.canvas,{x:g,y:a},5,{color:h.fill})}}if(this.hoverEdgeIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const h=C.getStrokeAndFill(r,n.valid,{isSelected:!0});P.drawLineWithPointList(this.canvas,c.changePointListByZoom(n.pointList,this.zoom,this.currentPos),{color:h.stroke,thickness:10,hoverEdgeIndex:this.hoverEdgeIndex,lineType:(o=this.config)==null?void 0:o.lineType})}}render(t){!this.ctx||(t!=="move"&&super.render(),t!=="drag"&&t!=="dragSingle"&&this.renderPolygon(t),t==="dragSingle"&&this.renderSelectedPolygons(),this.renderCursorLine(this.getLineColor(this.defaultAttribute)))}renderCursorLine(t){requestAnimationFrame(()=>{this.clearOffscreenCanvas();const{x:e,y:i}=this.coord;if(P.drawLine(this.offscreenCanvas,{x:0,y:i},{x:1e4,y:i},{color:t}),P.drawLine(this.offscreenCanvas,{x:e,y:0},{x:e,y:1e4},{color:t}),P.drawCircleWithFill(this.offscreenCanvas,{x:e,y:i},1,{color:"white"}),this.isCombined){const s=10,o=186,r=32;P.drawRectWithFill(this.canvas,{x:e+s,y:i-s*4-1,width:o,height:r},{color:"black"}),P.drawText(this.canvas,{x:e,y:i},A.t("ClickAnotherPolygon"),{textAlign:"center",color:"white",offsetX:o/2+s,offsetY:-(r/2+s/2)}),P.drawRect(this.canvas,{x:e-s,y:i-s,width:s*2,height:s*2},{lineDash:[6],color:"white"})}})}undo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.undo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.undo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}redo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.redo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.redo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}deleteSelectedID(){this.setSelectedID("")}addPointInDrawingHook(...t){return{continue:!0}}renderPolygonHook(...t){return{continue:!0}}getPointListByRectDrawing(...t){return{continue:!0}}}export{tt as default};
|
|
1
|
+
import K from"lodash";import{i18n as A}from"@labelbee/lb-utils";import T from"../../utils/MathUtils.js";import W from"../../utils/tool/RectUtils.js";import{ERotateDirection as V,ESortDirection as M,EDragTarget as v,EDragStatus as x,TEXT_ATTRIBUTE_OFFSET as O,DEFAULT_TEXT_OFFSET as R}from"../../constant/annotation.js";import m from"../../constant/keyCode.js";import{EPolygonPattern as _,edgeAdsorptionScope as U,ELineTypes as B,EToolName as F}from"../../constant/tool.js";import w from"../../locales/index.js";import{EMessage as C}from"../../locales/constants.js";import X from"../../utils/ActionsHistory.js";import b from"../../utils/tool/AttributeUtils.js";import c from"../../utils/tool/AxisUtils.js";import Z from"../../utils/tool/CanvasUtils.js";import y from"../../utils/tool/CommonToolUtils.js";import P from"../../utils/tool/DrawUtils.js";import u from"../../utils/tool/PolygonUtils.js";import S from"../../utils/tool/StyleUtils.js";import q from"../../utils/tool/TagUtils.js";import j from"../../utils/uuid.js";import{BasicToolOperation as Y}from"./basicToolOperation.js";import G from"./textAttributeClass.js";import J from"./Selection.js";var Q=Object.defineProperty,tt=Object.defineProperties,et=Object.getOwnPropertyDescriptors,z=Object.getOwnPropertySymbols,it=Object.prototype.hasOwnProperty,st=Object.prototype.propertyIsEnumerable,N=(I,t,e)=>t in I?Q(I,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):I[t]=e,f=(I,t)=>{for(var e in t||(t={}))it.call(t,e)&&N(I,e,t[e]);if(z)for(var e of z(t))st.call(t,e)&&N(I,e,t[e]);return I},L=(I,t)=>tt(I,et(t));const $=164;class ot extends Y{constructor(t){super(L(f({},t),{isOffscreenCanvas:!0}));this.isAllowDouble=e=>{const{selectedID:i}=this,s=this.getHoverID(e);return!!(i&&i===s)},this.textChange=e=>{this.config.textConfigurable===!1||!this.selectedID||(this.setPolygonList(b.textChange(e,this.selectedID,this.polygonList)),this.emit("selectedChange"),this.render())},this.lastMouseMoveTime=0,this.mouseMoveThrottle=16,this.emitUpdatePolygonByDrag=()=>{if(this.dragInfo){const{originPolygon:e}=this.dragInfo;this.emit("updatePolygonByDrag",[{newPolygon:this.selectedPolygon,originPolygon:e}])}},this.config=y.jsonParser(t.config),this.drawingPointList=[],this.polygonList=[],this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.attrChangeTrigger=0,this.drawingHistory=new X,this.isCtrl=!1,this.isAlt=!1,this.isCombined=!1,this.pattern=_.Normal,this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.selection=new J(this)}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}get minArea(){return this.config.minArea||0}eventBinding(){super.eventBinding(),this.container.removeEventListener("mouseup",this.onMouseUp),this.container.addEventListener("mouseup",this.dragMouseUp),this.dblClickListener.addEvent(this.onMouseUp,this.onLeftDblClick,this.onRightDblClick,this.isAllowDouble)}eventUnbinding(){super.eventUnbinding(),this.container.removeEventListener("mouseup",this.dragMouseUp)}destroy(){super.destroy(),this._textAttributeInstance&&this._textAttributeInstance.clearTextAttribute()}get selectedPolygon(){return u.getPolygonByID(this.visiblePolygonList,this.selectedID)}get selectedPolygons(){return u.getPolygonByIDs(this.polygonList,this.selectedIDs)}get hoverPolygon(){return this.visiblePolygonList.find(t=>t.id===this.hoverID&&t.id!==this.selectedID)}get enableDrag(){return Boolean(this.selectedID&&this.dragInfo)}get visiblePolygonList(){return this.polygonList}get polygonListUnderZoom(){return this.visiblePolygonList.map(t=>L(f({},t),{pointList:c.changePointListByZoom(t.pointList,this.zoom)}))}get selectedText(){var t;return(t=this.selectedPolygon)==null?void 0:t.textAttribute}get dataList(){return this.polygonList}setPattern(t,e=!1){var i;((i=this.drawingPointList)==null?void 0:i.length)>0&&e===!0||(this.pattern=t)}get currentShowList(){let t=[];const[e,i]=y.getRenderResultList(this.polygonList,y.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);return t=e,this.isHidden&&(t=[]),i&&t.push(...i),t}get currentPageResult(){const[t]=y.getRenderResultList(this.polygonList,y.getSourceID(this.basicResult),[]);return t}setResultAndSelectedID(t,e){this.setPolygonList(t),e&&this.setSelectedID(e),this.render()}setResult(t,e=!0){this.clearActiveStatus(e),this.setPolygonList(t),this.render()}setPolygonDataByID(t,e){return this.polygonList.map(i=>i.id===e?f(f({},i),t):i)}rotatePolygon(t=1,e=V.Clockwise,i=this.selectedID){if(!i)return;const s=u.getPolygonByID(this.polygonList,i);if(!s)return;const o=u.updatePolygonByRotate(e,t,s==null?void 0:s.pointList);this.setPolygonList(this.setPolygonDataByID({pointList:o},i)),this.render()}rotatePolygonEdge(t,e=this.selectedID){!e||!u.getPolygonByID(this.polygonList,e)||(this.setPolygonList(this.setPolygonDataByID({pointList:t},e)),this.render())}addPointInDrawing(t){if(!this.imgInfo||this.forbidAddNewPolygonFuc&&this.forbidAddNewPolygonFuc(t))return;const{upperLimitPointNum:e,edgeAdsorption:i}=this.config;if(e&&this.drawingPointList.length>=e){this.emit("messageInfo",`${w.getMessagesByLocale(C.UpperLimitErrorNotice,this.lang)}${e}`);return}const s=this.getCoordinateUnderZoom(t),o=c.changeDrawOutsideTarget(s,{x:0,y:0},this.imgInfo,this.config.drawOutsideTarget,this.basicResult,this.zoom);if(c.returnClosePointIndex(o,c.changePointListByZoom(this.drawingPointList,this.zoom))===0){this.addDrawingPointToPolygonList();return}const{dropFoot:l}=u.getClosestPoint(o,this.polygonListUnderZoom,this.config.lineType,U),n=c.changePointByZoom(l&&t.altKey===!1&&i?l:o,1/this.zoom);!this.addPointInDrawingHook(n).continue||(this.drawingPointList.push(n),this.drawingPointList.length===1?this.drawingHistory.initRecord(this.drawingPointList):this.drawingHistory.pushHistory(this.drawingPointList))}clearResult(){this.setPolygonList([]),this.deleteSelectedID(),this.render()}clearPolygonDrag(){this.drawingPointList=[],this.stopDrag(),this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,this.hoverID=""}clearActiveStatus(t=!0){this.clearPolygonDrag(),t&&this.deleteSelectedID()}clearDrawingStatus(){this.drawingPointList=[]}setPolygonList(t){const e=this.polygonList.length;this.polygonList=t,e!==t.length&&this.emit("updatePageNumber")}setSelectedID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}setAttrChangeTrigger(t=0){this.attrChangeTrigger=t}setDefaultAttribute(t=""){const e=this.defaultAttribute;if(this.defaultAttribute=t,(this.attrChangeTrigger||e!==t)&&(this.changeStyle(t),this.emit("changeAttributeSidebar"),this.selectedPolygons.length>0&&(this.selectedPolygons.forEach(i=>{i.attribute=t}),this.history.pushHistory(this.polygonList),this.render()),this._textAttributeInstance)){if(this.attributeLockList.length>0&&!this.attributeLockList.includes(t)){this._textAttributeInstance.clearTextAttribute();return}this._textAttributeInstance.updateIcon(this.getTextIconSvg(t))}}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}setPolygonValidAndRender(t,e){if(!t)return;const i=this.polygonList.map(s=>{var o;if(s.id===t){const r=(o=s==null?void 0:s.valid)!=null?o:!0;return L(f({},s),{valid:e!=null?e:!r})}return s});this.setPolygonList(i),this.history.pushHistory(this.polygonList),this.render(),this.emit("updateResult")}addDrawingPointToPolygonList(t){let{lowerLimitPointNum:e=3}=this.config;e<3&&(e=3);let i;if(this.drawingPointList.length<e){this.drawingPointList=[],this.editPolygonID="";return}if(u.calcPolygonSize(this.drawingPointList||[])<this.minArea){this.emit("messageInfo",`${w.getMessagesByLocale(C.MinAreaLimitErrorNotice,this.lang)}${this.minArea}`);return}const s=y.getSourceID(this.basicResult),o=[...this.polygonList];if(this.editPolygonID){const r=o.find(l=>l.id===this.editPolygonID);if(!r)return;r.pointList=this.drawingPointList,this.editPolygonID=""}else{const r=j(8,62);let l={id:r,sourceID:s,valid:!this.isCtrl,textAttribute:"",pointList:this.drawingPointList,attribute:this.defaultAttribute,order:y.getMaxOrder(o.filter(n=>y.isSameSourceID(n.sourceID,s)))+1};if(this.config.textConfigurable){let n="";n=b.getTextAttribute(this.polygonList.filter(h=>y.isSameSourceID(h.sourceID,s)),this.config.textCheckType),l=L(f({},l),{textAttribute:n})}this.pattern===_.Rect&&t===!0&&(l=L(f({},l),{isRect:!0})),o.push(l),i=l,this.setSelectedIdAfterAddingDrawing(r)}this.setPolygonList(o),this.isCtrl=!1,this.drawingPointList=[],this.history.pushHistory(o),i&&this.emit("polygonCreated",i,this.zoom,this.currentPos)}setSelectedIdAfterAddingDrawing(t){this.drawingPointList.length!==0&&(this.config.textConfigurable?this.setSelectedID(t):this.deleteSelectedID())}getHoverID(t){var e;const i=this.getCoordinateUnderZoom(t),s=this.currentShowList.map(o=>L(f({},o),{pointList:c.changePointListByZoom(o.pointList,this.zoom)}));return u.getHoverPolygonID(i,s,10,(e=this.config)==null?void 0:e.lineType)}getHoverEdgeIndex(t){var e;if(!this.selectedID)return-1;const i=this.selectedPolygon;if(!i)return-1;const s=this.getCoordinateUnderZoom(t),o=c.changePointListByZoom(i.pointList,this.zoom);return u.getHoverEdgeIndex(s,o,(e=this.config)==null?void 0:e.lineType)}getHoverPointIndex(t){if(!this.selectedID)return-1;const e=this.selectedPolygon;if(!e)return-1;const i=this.getCoordinateUnderZoom(t),s=c.changePointListByZoom(e.pointList,this.zoom);return c.returnClosePointIndex(i,s)}deletePolygons(t){var e;if(!t||t.length===0)return;const i=this.polygonList.filter(s=>this.selection.isIdSelected(s.id));this.emit("deletedObject",{deletedObject:i[0],id:i[0].id}),this.setPolygonList(this.polygonList.filter(s=>!this.selection.isIdSelected(s.id))),this.history.pushHistory(this.polygonList),(e=this._textAttributeInstance)==null||e.clearTextAttribute(),this.emit("selectedChange"),this.render()}deletePolygonPoint(t){if(!this.selectedID)return;const{selectedPolygon:e}=this;if(!e)return;let{lowerLimitPointNum:i}=this.config;if(i<3&&(i=3),e.pointList.length<=i){this.emit("messageInfo",`${w.getMessagesByLocale(C.LowerLimitErrorNotice,this.lang)}${i}`);return}e==null||e.pointList.splice(t,1),this.history.pushHistory(this.polygonList),this.render()}spaceKeydown(){var t,e,i;if(this.selectedID){if(((t=this.selectedPolygon)==null?void 0:t.isRect)===!0){this.emit("messageInfo",`${w.getMessagesByLocale(C.UnableToReannotation,this.lang)}`);return}this.editPolygonID=this.selectedID,this.drawingPointList=(i=(e=this.selectedPolygon)==null?void 0:e.pointList)!=null?i:[],this.drawingHistory.empty(),this.drawingHistory.initRecord(this.drawingPointList),this.hoverID="",this.deleteSelectedID(),this.render()}}onTabKeyDown(t){t.preventDefault();let e=M.ascend;t.shiftKey&&(e=M.descend),this.switchToNextPolygon(e)}switchToNextPolygon(t){if(this.drawingPointList.length>0)return;const[e,i]=y.getRenderResultList(this.polygonList,y.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let s=[...e];i&&(s=[...s,...i]);const o=Z.getViewPort(this.canvas,this.currentPos,this.zoom),r=s.map(n=>{var h,d,g,a;return L(f({},n),{x:(d=(h=n.pointList[0])==null?void 0:h.x)!=null?d:0,y:(a=(g=n.pointList[0])==null?void 0:g.y)!=null?a:0})}).filter(n=>Z.inViewPort({x:n.x,y:n.y},o)),l=y.getNextSelectedRectID(r,t,this.selectedID);if(l){this.setSelectedID(l.id);const{selectedPolygon:n}=this;n&&this.setDefaultAttribute(n.attribute)}this.render()}onKeyDown(t){if(!y.hotkeyFilter(t)||super.onKeyDown(t)===!1||this.selection.triggerKeyboardEvent(t,this.setPolygonList.bind(this)))return;const{keyCode:e}=t;switch(e){case m.Space:this.spaceKeydown();break;case m.Esc:this.drawingPointList=[],this.editPolygonID="";break;case m.F:this.selectedID&&this.setPolygonValidAndRender(this.selectedID);break;case m.Z:if(t.altKey){this.onCombinedExecute();return}this.setIsHidden(!this.isHidden),this.render();break;case m.Delete:this.deletePolygons(this.selectedIDs),this.render();break;case m.Ctrl:this.isCtrl=!0;break;case m.Alt:this.isAlt===!1&&(t.preventDefault(),this.isAlt=!0,this.render());break;case m.Tab:{this.onTabKeyDown(t);break}case m.X:t.altKey&&this.segment();break;default:{if(this.config.attributeConfigurable){const i=b.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}}onKeyUp(t){switch(super.onKeyUp(t),t.keyCode){case m.Ctrl:this.isCtrl=!1;break;case m.Alt:{const e=this.isAlt;this.isAlt=!1,e===!0&&this.render();break}}}rightMouseUp(t){if(this.drawingPointList.length>0){this.addDrawingPointToPolygonList();return}this.setSelectedID(this.hoverID,t.ctrlKey);const{selectedPolygon:e}=this;e&&this.setDefaultAttribute(e.attribute)}onLeftDblClick(t){if(this.hoverEdgeIndex>-1){const e=this.getCoordinateUnderZoom(t),{selectedPolygon:i}=this;if(!i)return;if(i.isRect===!0){this.emit("messageInfo",`${w.getMessagesByLocale(C.ForbidAddNewPoint,this.lang)}`),this.clearPolygonDrag();return}const{dropFoot:s}=u.getClosestPoint(e,this.polygonListUnderZoom,this.config.lineType,U);if(!s)return;const{upperLimitPointNum:o}=this.config;if(o&&i.pointList.length>=o){this.emit("messageInfo",`${w.getMessagesByLocale(C.UpperLimitErrorNotice,this.lang)}${o}`),this.clearPolygonDrag();return}i==null||i.pointList.splice(this.hoverEdgeIndex+1,0,c.changePointByZoom(s,1/this.zoom)),this.setPolygonDataByID(i,this.selectedID),this.history.pushHistory(this.polygonList),this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.render()}this.dragInfo=void 0}onRightDblClick(t){this.dragInfo=void 0,this.clearImgDrag();const e=this.getHoverID(t),i=this.getHoverPointIndex(t);if(this.hoverPointIndex>-1&&this.hoverPointIndex===i){this.deletePolygonPoint(i),this.dragInfo=void 0,this.hoverPointIndex=-1,this.render();return}this.hoverID===this.selectedID&&this.deletePolygons([e]),this.render()}onMouseDown(t){if(!(super.onMouseDown(t)||this.forbidMouseOperation||t.ctrlKey===!0))return this.dragMouseDown(t),!0}onMouseLeave(){this.dragInfo&&(this.emitUpdatePolygonByDrag(),this.clearPolygonDrag(),this.history.pushHistory(this.polygonList))}dragMouseDown(t){if(t.button!==0)return;const e=this.getHoverID(t);if(!this.selection.isIdSelected(e))return;let i=[0],s=v.Plane;const o=this.selectedPolygons[0].pointList,r=this.getCoordinateUnderZoom(t);if(this.selectedPolygons.length===1){this.dragStatus=x.Start;const l=this.getHoverPointIndex(t),n=this.getHoverEdgeIndex(t);l>-1?(s=v.Point,i=[l]):n>-1&&this.hoverEdgeIndex>-1&&(s=v.Line,i=[n,(n+1)%o.length])}this.dragInfo={dragStartCoord:r,dragTarget:s,initPointList:o,changePointIndex:i,originPolygon:this.selectedPolygon,selectedPolygons:this.selectedPolygons,dragPrevCoord:r,originPolygonList:this.polygonList}}segment(){var t;if(!this.selectedID||((t=this.config)==null?void 0:t.lineType)!==B.Line)return;const e=u.getPolygonPointList(this.selectedID,this.currentShowList),i=this.currentShowList.filter(r=>r.id!==this.selectedID);if(i.length===0||e.length===0)return;const s=u.getWrapPolygonIndex(e,i);let o=[...this.polygonList];if(s===-1){const r=u.segmentPolygonByPolygon(e,i);if(!r)return;const l=r.shift();if(!l)return;let n="",h=!0;const d=y.getSourceID(this.basicResult);let g="";o=this.polygonList.map(a=>{var p,D;return a.id===this.selectedID?(n=a.attribute,h=(p=a==null?void 0:a.valid)!=null?p:!0,g=(D=a==null?void 0:a.textAttribute)!=null?D:"",L(f({},a),{pointList:l})):a}),r.length>0&&r.forEach((a,p)=>{o.push({sourceID:d,id:j(8,62),pointList:a,valid:h,order:y.getMaxOrder(this.currentShowList)+1+p,attribute:n,textAttribute:g})})}else o[s].pointList=u.clipPolygonFromWrapPolygon(e,o[s].pointList),o=o.filter(r=>r.id!==this.selectedID);this.setPolygonList(o),this.history.pushHistory(o),this.render()}onCombinedExecute(){if(!this.selectedID){this.emit("messageInfo",A.t("PolygonsToBeCombinedNeedToBeSelected"));return}this.isCombined=!this.isCombined}combine(t){var e;const i=this.getHoverID(t);if(!i||!this.selectedID||this.selectedID===i)return;if(((e=this.config)==null?void 0:e.lineType)!==B.Line){this.emit("messageInfo",A.t("CurveModeDoesNotSupportCutting"));return}const s=this.polygonList.find(h=>h.id===this.selectedID),o=this.currentShowList.find(h=>h.id===i);if(!o||!s)return;const r=u.combinePolygonWithPolygon(s,o);if(!r)return;const{newPolygon:l,unionList:n}=r;if(n.length===1&&l){const h=this.polygonList.filter(d=>!n.includes(d.id)).map(d=>d.id===this.selectedID?l:d);this.setPolygonList(h),this.history.pushHistory(h),this.render(),this.emit("messageInfo",A.t("CombineSuccess"))}else this.emit("messageInfo",A.t("CombiningFailedNotify"));this.isCombined=!1}isPolygonOutSide(t){if(this.dependToolName&&this.basicCanvas&&this.basicResult){let l=!1;switch(this.dependToolName){case F.Rect:{l=t.filter(n=>!W.isInRect(n,this.basicResult)).length>0;break}case F.Polygon:{l=u.isPointListOutSidePolygon(t,this.basicResult.pointList,this.config.lineType);break}}return l}if(!this.imgInfo)return!1;const{left:e,top:i,right:s,bottom:o}=T.calcViewportBoundaries(c.changePointListByZoom(t,this.zoom)),r=1e-5;return e<0||i<0||s>this.imgInfo.width+r||o>this.imgInfo.height+r}onDragMove(t){const e=this.polygonList.map(i=>{if(this.selectedIDs.includes(i.id)){const s=this.dragPolygon(t,i);if(!s)return i;const o=L(f({},i),{pointList:s});return i.isRect===!0&&this.pattern===_.Normal&&Object.assign(o,{isRect:!1}),o}return i});this.dragInfo.dragPrevCoord=this.getCoordinateUnderZoom(t),this.setPolygonList(e)}getDragOffset(t,e){var i;const s=this.getCoordinateUnderZoom(t),{dragTarget:o,dragPrevCoord:r,changePointIndex:l,initPointList:n,dragStartCoord:h}=this.dragInfo;if(this.pattern===_.Rect&&(e==null?void 0:e.isRect)===!0&&l&&[v.Line].includes(o)){const d=T.getArrayIndex(l[0]-2,4),g=T.getArrayIndex(l[0]-1,4),a=[n[d],n[g]],p=T.getRectPerpendicularOffset(h,s,a);return{x:p.x/this.zoom,y:p.y/this.zoom}}return((i=this.dragInfo)==null?void 0:i.dragTarget)===v.Plane?{x:(s.x-r.x)/this.zoom,y:(s.y-r.y)/this.zoom}:{x:(s.x-h.x)/this.zoom,y:(s.y-h.y)/this.zoom}}dragPolygon(t,e){let i=K.cloneDeep(e==null?void 0:e.pointList);if(!i||!this.dragInfo)return;const{initPointList:s,dragTarget:o,changePointIndex:r}=this.dragInfo,l=this.getDragOffset(t,e);switch(this.dragStatus=x.Move,o){case v.Plane:i=i.map(n=>L(f({},n),{x:n.x+l.x,y:n.y+l.y}));break;case v.Point:case v.Line:i=i.map((n,h)=>r&&r.includes(h)?L(f({},n),{x:s[h].x+l.x,y:s[h].y+l.y}):n);break}if(this.pattern===_.Rect&&(e==null?void 0:e.isRect)===!0&&o===v.Point&&r&&(i=T.getPointListFromPointOffset(s,r[0],l)),!(this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(i)))return i}determineTrigger(t){if(this.isDrag)return"drag";if(this.dragInfo)return"dragSingle";let e="";const i=this.getHoverID(t);this.hoverID!==i?(this.hoverID=i,e=""):e="move";let s=-1,o=-1;const{selectedID:r}=this;return r&&(s=this.getHoverPointIndex(t),s>-1?this.hoverPointIndex!==s?e="":e="noRender":(o=this.getHoverEdgeIndex(t),this.hoverEdgeIndex!==o?e="":e="noRender"),this.hoverEdgeIndex=o,this.hoverPointIndex=s),e}onMouseMove(t){requestAnimationFrame(()=>{const e=Date.now();if(e-this.lastMouseMoveTime<this.mouseMoveThrottle||(this.lastMouseMoveTime=e,super.onMouseMove(t,!1)||this.forbidMouseOperation||!this.imgInfo))return;if(this.drawingPointList.length>0){this.render();return}const i=this.determineTrigger(t);this.selectedIDs.length>0&&this.dragInfo&&this.onDragMove(t),this.render(i)})}leftMouseUpdateValid(t){const e=this.getHoverID(t);return this.drawingPointList.length===0&&t.ctrlKey===!0&&e?(this.setPolygonValidAndRender(e),!0):!1}stopDrag(){this.dragInfo=void 0,this.dragStatus=x.Wait}leftMouseUp(t){this.leftMouseUpdateValid(t)||this.addPointInDrawing(t)}onMouseUp(t){if(this.isCombined){switch(t.button){case 0:this.combine(t);break;case 2:this.isCombined=!1;break;default:return}return}if(!(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)){if(this.dragInfo&&this.dragStatus===x.Move){this.emit("updateResult"),this.emitUpdatePolygonByDrag(),this.stopDrag(),this.history.pushHistory(this.polygonList);return}if(this.dragInfo&&this.dragStatus===x.Start){this.stopDrag();return}switch(t.button){case 0:{this.leftMouseUp(t);break}case 2:{this.rightMouseUp(t);break}}this.render()}}dragMouseUp(){this.dragStatus===x.Start&&this.stopDrag()}exportData(){const{polygonList:t}=this;return[t,this.basicImgInfo]}getTextIconSvg(t=""){return b.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}getCurrentSelectedData(){const{selectedPolygon:t}=this;if(!t)return;const e=this.getColor(t.attribute),i=t.valid?e==null?void 0:e.valid.stroke:e==null?void 0:e.invalid.stroke;return{width:$,textAttribute:t.textAttribute,color:i}}updateSelectedTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedID){let e=t;b.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",b.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setPolygonList(b.textChange(e,this.selectedID,this.polygonList)),this.emit("updateTextAttribute"),this.render()}}renderTextAttribute(){var t;const{selectedPolygon:e}=this;if(!this.ctx||this.config.textConfigurable===!1||!e)return;const{pointList:i,attribute:s,valid:o,textAttribute:r}=e,{x:l,y:n}=i[i.length-1],h=$,d=c.getOffsetCoordinate({x:l,y:n},this.currentPos,this.zoom),g=this.getColor(s),a=o?g==null?void 0:g.valid.stroke:g==null?void 0:g.invalid.stroke;this._textAttributeInstance||(this._textAttributeInstance=new G({width:h,container:this.container,icon:this.getTextIconSvg(s),color:a,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((t=this._textAttributeInstance)==null?void 0:t.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${r}`,{left:d.x,top:d.y,color:a,width:h})}renderStaticPolygon(){var t;this.isHidden===!1&&((t=this.visiblePolygonList)==null||t.forEach(e=>{var i,s,o,r,l,n,h;if([this.selectedID,this.editPolygonID].includes(e.id))return;const{textAttribute:d,attribute:g}=e,a=this.getColor(g),p=S.getStrokeAndFill(a,e.valid),D=c.changePointListByZoom(e.pointList||[],this.zoom,this.currentPos);P.drawPolygonWithFillAndLine(this.canvas,D,{fillColor:p.fill,strokeColor:p.stroke,pointColor:"white",thickness:(s=(i=this.style)==null?void 0:i.width)!=null?s:2,lineCap:"round",isClose:!0,lineType:(o=this.config)==null?void 0:o.lineType});let k=`${(r=b.getAttributeShowText(g,this.config.attributeList))!=null?r:""}`;((l=this.config)==null?void 0:l.isShowOrder)&&(e==null?void 0:e.order)>0&&(k=`${e.order} ${k}`),(e==null?void 0:e.subAttribute)&&((n=this.config)==null?void 0:n.secondaryAttributeConfigurable)&&((h=this.config)==null?void 0:h.subAttributeList)&&q.getTagNameList(e.subAttribute,this.config.subAttributeList).forEach(H=>{k+=`
|
|
2
|
+
${H.keyName}: ${H.value.join("\u3001")}`}),P.drawText(this.canvas,D[0],k,f({color:p.stroke},R));const E=D[D.length-1];P.drawText(this.canvas,{x:E.x+O.x,y:E.y+O.y},d,f({color:p.stroke},R))}))}renderSelectedPolygons(){this.selectedPolygons.forEach(t=>{this.renderSelectedPolygon(t)})}renderSelectedPolygon(t){var e,i,s;if(t){const o=this.getColor(t.attribute),r=S.getStrokeAndFill(o,t.valid,{isSelected:!0});P.drawSelectedPolygonWithFillAndLine(this.canvas,c.changePointListByZoom(t.pointList,this.zoom,this.currentPos),{fillColor:r.fill,strokeColor:r.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!0,lineType:(e=this.config)==null?void 0:e.lineType});let l=`${(i=b.getAttributeShowText(t.attribute,this.config.attributeList))!=null?i:""}`;((s=this.config)==null?void 0:s.isShowOrder)&&(t==null?void 0:t.order)>0&&(l=`${t.order} ${l}`),P.drawText(this.canvas,c.changePointByZoom(t.pointList[0],this.zoom,this.currentPos),l,f({color:r.stroke},R)),this.renderTextAttribute()}}renderHoverPolygon(){var t;if(this.hoverID&&this.hoverID!==this.editPolygonID){const{hoverPolygon:e}=this;if(e){let i="";const s=this.getColor(e.attribute);e.valid?i=s.validHover.fill:i=S.getStrokeAndFill(s,!1,{isHover:!0}).fill,P.drawPolygonWithFill(this.canvas,c.changePointListByZoom(e.pointList,this.zoom,this.currentPos),{color:i,lineType:(t=this.config)==null?void 0:t.lineType})}}}renderPolygon(t){var e,i,s,o;t!=="move"&&this.renderStaticPolygon(),this.renderHoverPolygon(),this.renderSelectedPolygons();const r=this.getColor(this.defaultAttribute),l=S.getStrokeAndFill(r,!this.isCtrl);if(((e=this.drawingPointList)==null?void 0:e.length)>0){let n=[...this.drawingPointList],h=c.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);const d=this.getPointListByRectDrawing(h,n).value;if(d)n=d;else{if(((i=this.config)==null?void 0:i.edgeAdsorption)&&this.isAlt===!1){const{dropFoot:a}=u.getClosestPoint(h,this.polygonList,(s=this.config)==null?void 0:s.lineType,U/this.zoom);a&&(h=a)}n.push(h)}const g=c.changePointListByZoom(n,this.zoom,this.currentPos);P.drawSelectedPolygonWithFillAndLine(this.canvas,g,{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!1,lineType:this.config.lineType}),this.renderPolygonHook(g)}if(this.hoverPointIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const h=S.getStrokeAndFill(r,n.valid,{isSelected:!0}),d=n==null?void 0:n.pointList[this.hoverPointIndex];if(d){const{x:g,y:a}=c.changePointByZoom(d,this.zoom,this.currentPos);P.drawCircleWithFill(this.canvas,{x:g,y:a},5,{color:h.fill})}}if(this.hoverEdgeIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const h=S.getStrokeAndFill(r,n.valid,{isSelected:!0});P.drawLineWithPointList(this.canvas,c.changePointListByZoom(n.pointList,this.zoom,this.currentPos),{color:h.stroke,thickness:10,hoverEdgeIndex:this.hoverEdgeIndex,lineType:(o=this.config)==null?void 0:o.lineType})}}render(t){!this.ctx||(t!=="move"&&super.render(),t!=="drag"&&t!=="dragSingle"&&this.renderPolygon(t),t==="dragSingle"&&this.renderSelectedPolygons(),this.renderCursorLine(this.getLineColor(this.defaultAttribute)))}renderCursorLine(t){requestAnimationFrame(()=>{this.clearOffscreenCanvas();const{x:e,y:i}=this.coord;if(P.drawLine(this.offscreenCanvas,{x:0,y:i},{x:1e4,y:i},{color:t}),P.drawLine(this.offscreenCanvas,{x:e,y:0},{x:e,y:1e4},{color:t}),P.drawCircleWithFill(this.offscreenCanvas,{x:e,y:i},1,{color:"white"}),this.isCombined){const s=10,o=186,r=32;P.drawRectWithFill(this.canvas,{x:e+s,y:i-s*4-1,width:o,height:r},{color:"black"}),P.drawText(this.canvas,{x:e,y:i},A.t("ClickAnotherPolygon"),{textAlign:"center",color:"white",offsetX:o/2+s,offsetY:-(r/2+s/2)}),P.drawRect(this.canvas,{x:e-s,y:i-s,width:s*2,height:s*2},{lineDash:[6],color:"white"})}})}undo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.undo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.undo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}redo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.redo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.redo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}deleteSelectedID(){this.setSelectedID("")}addPointInDrawingHook(...t){return{continue:!0}}renderPolygonHook(...t){return{continue:!0}}getPointListByRectDrawing(...t){return{continue:!0}}}export{ot as default};
|