UNPKG

diginext-pixi

Version:
1 lines 4.33 kB
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,a){void 0===a&&(a=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,a,o)}:function(e,t,r,a){void 0===a&&(a=r),e[a]=t[r]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&__createBinding(t,e,r);return __setModuleDefault(t,e),t},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(e,t,r,a){if("a"===r&&!a)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!a:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?a:"a"===r?a.call(e):a?a.value:t.get(e)},__classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(e,t,r,a,o){if("m"===a)throw new TypeError("Private method is not writable");if("a"===a&&!o)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!o:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===a?o.call(e,r):o?o.value=r:t.set(e,r),r},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});const Event_1=__importDefault(require("../../data/Event")),getChildByName_1=__importDefault(require("../../helper/getChildByName")),moveToBottom_1=__importDefault(require("../../helper/moveToBottom")),PIXI=__importStar(require("pixi.js")),PrintBase=e=>{var t,r,a,o;return o=class extends e{constructor(...e){super(e),t.add(this),this.backgroundHolder=new PIXI.Container,this._role="",this.PIXEL_PER_CM=118.13,r.set(this,!1),this.addChild(this.backgroundHolder),this.on(Event_1.default.CHANGE_MATRIX,__classPrivateFieldGet(this,t,"m",a))}get role(){return this._role}setRole(e){return this._role=e,this._role}get WIDTH_VIA_CM(){return Math.abs(Math.ceil(this.width)/this.PIXEL_PER_CM)}get HEIGHT_VIA_CM(){return Math.abs(Math.ceil(this.height)/this.PIXEL_PER_CM)}get AREA_VIA_CM(){return Math.abs(this.WIDTH_VIA_CM*this.HEIGHT_VIA_CM)}get hasBackground(){return __classPrivateFieldGet(this,r,"f")}addBackground(e){var t,a;this.removeBackground();const o=(null==e?void 0:e.color)||this.data.backgroundColor||287624;__classPrivateFieldSet(this,r,!0,"f"),this.data.backgroundColor=o;try{const{width:e,height:r}=this,i=e/this.scale.x,s=r/this.scale.y,n=12,l=8,d=-(i/2+n),c=-(s/2+l),h=i+2*n,u=s+2*l,_=new PIXI.Graphics;_.name="background",_.beginFill(o),_.drawRoundedRect(d,c,h,u,12),_.endFill(),null===(a=null===(t=this.backgroundHolder)||void 0===t?void 0:t.addChild)||void 0===a||a.call(t,_),(0,moveToBottom_1.default)(_)}catch(e){console.error("addBackground error",e)}}removeBackground(){__classPrivateFieldSet(this,r,!1,"f");try{(0,getChildByName_1.default)(this.backgroundHolder,"background")&&this.backgroundHolder.removeChild((0,getChildByName_1.default)(this.backgroundHolder,"background"))}catch(e){console.error("removeBackground error",e)}}toJSON(e){const t=this;this.hasBackground&&(this.backgroundHolder.visible=!1);const r=Object.assign({id:t.id,type:t.type,rotation:t.rotation,x:t.x,y:t.y,scaleX:t.scale.x,scaleY:t.scale.y,width:t.width,height:t.height,hasBackground:this.hasBackground,role:this._role,data:this.data,lock:this.lock},e);return this.hasBackground&&(this.backgroundHolder.visible=!0),r}fromJSON(e){if("string"==typeof e)try{e=JSON.parse(e)}catch(e){console.error("fromJSON error",e)}const t=this,{url:r,rotation:a,x:o,y:i,scaleX:s,scaleY:n,hasBackground:l,role:d,data:c,lock:h,anchor:u}=e;t.rotation=a,t.x=o,t.y=i,t.data=c,t.scale.set(s,n),d&&this.setRole(d),l&&this.addBackground(),h&&this.doLock(),t.emit(Event_1.default.CHANGE_MATRIX)}},r=new WeakMap,t=new WeakSet,a=function(){try{this.hasBackground&&this.addBackground()}catch(e){throw new Error(`#onchange failed: ${e instanceof Error?e.message:"Unknown error"}`)}},o};exports.default=PrintBase;