@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 26.2 kB
JavaScript
import{Mat4 as t}from"../../../core/math/mat4.js";import{Vec2 as e}from"../../../core/math/vec2.js";import{Vec3 as s}from"../../../core/math/vec3.js";import{Vec4 as i}from"../../../core/math/vec4.js";import{FUNC_EQUAL as n,STENCILOP_INCREMENT as r,FUNC_ALWAYS as h,STENCILOP_REPLACE as a}from"../../../platform/graphics/constants.js";import{LAYERID_UI as o}from"../../../scene/constants.js";import{BatchGroup as l}from"../../../scene/batching/batch-group.js";import{StencilParameters as c}from"../../../platform/graphics/stencil-parameters.js";import{Entity as _}from"../../entity.js";import{Component as d}from"../component.js";import{ELEMENTTYPE_GROUP as m,FITMODE_STRETCH as u,ELEMENTTYPE_IMAGE as y,ELEMENTTYPE_TEXT as g}from"./constants.js";import{ImageElement as p}from"./image-element.js";import{TextElement as f}from"./text-element.js";const x=new s,w=new t,b=new s,v=new s,L=new t,C=new t,M=new t,T=new t;class S extends d{constructor(n,r){super(n,r),this._evtLayersChanged=null,this._evtLayerAdded=null,this._evtLayerRemoved=null,this._beingInitialized=!1,this._anchor=new i,this._localAnchor=new i,this._pivot=new e,this._width=this._calculatedWidth=32,this._height=this._calculatedHeight=32,this._margin=new i(0,0,-32,-32),this._modelTransform=new t,this._screenToWorld=new t,this._anchorTransform=new t,this._anchorDirty=!0,this._parentWorldTransform=new t,this._screenTransform=new t,this._screenCorners=[new s,new s,new s,new s],this._canvasCorners=[new e,new e,new e,new e],this._worldCorners=[new s,new s,new s,new s],this._cornersDirty=!0,this._canvasCornersDirty=!0,this._worldCornersDirty=!0,this.entity.on("insert",this._onInsert,this),this._patch(),this.screen=null,this._type=m,this._image=null,this._text=null,this._group=null,this._drawOrder=0,this._fitMode=u,this._useInput=!1,this._layers=[o],this._addedModels=[],this._batchGroupId=-1,this._offsetReadAt=0,this._maskOffset=.5,this._maskedBy=null}get data(){const t=this.system.store[this.entity.getGuid()];return t?t.data:null}set enabled(t){const e=this.data,s=e.enabled;e.enabled=t,this.fire("set","enabled",s,t)}get enabled(){return this.data.enabled}get _absLeft(){return this._localAnchor.x+this._margin.x}get _absRight(){return this._localAnchor.z-this._margin.z}get _absTop(){return this._localAnchor.w-this._margin.w}get _absBottom(){return this._localAnchor.y+this._margin.y}get _hasSplitAnchorsX(){return Math.abs(this._anchor.x-this._anchor.z)>.001}get _hasSplitAnchorsY(){return Math.abs(this._anchor.y-this._anchor.w)>.001}get aabb(){return this._image?this._image.aabb:this._text?this._text.aabb:null}set anchor(t){t instanceof i?this._anchor.copy(t):this._anchor.set(...t),this.entity._parent||this.screen?this._calculateSize(this._hasSplitAnchorsX,this._hasSplitAnchorsY):this._calculateLocalAnchors(),this._anchorDirty=!0,this.entity._dirtyLocal||this.entity._dirtifyLocal(),this.fire("set:anchor",this._anchor)}get anchor(){return this._anchor}set batchGroupId(t){if(this._batchGroupId!==t){var e,s;if(this.entity.enabled&&this._batchGroupId>=0)null==(e=this.system.app.batcher)||e.remove(l.ELEMENT,this.batchGroupId,this.entity);if(this.entity.enabled&&t>=0)null==(s=this.system.app.batcher)||s.insert(l.ELEMENT,t,this.entity);t<0&&this._batchGroupId>=0&&this.enabled&&this.entity.enabled&&(this._image&&this._image._renderable.model?this.addModelToLayers(this._image._renderable.model):this._text&&this._text._model&&this.addModelToLayers(this._text._model)),this._batchGroupId=t}}get batchGroupId(){return this._batchGroupId}set bottom(t){this._margin.y=t;const e=this.entity.getLocalPosition(),s=this._absTop,i=this._localAnchor.y+t;this._setHeight(s-i),e.y=t+this._calculatedHeight*this._pivot.y,this.entity.setLocalPosition(e)}get bottom(){return this._margin.y}set calculatedWidth(t){this._setCalculatedWidth(t,!0)}get calculatedWidth(){return this._calculatedWidth}set calculatedHeight(t){this._setCalculatedHeight(t,!0)}get calculatedHeight(){return this._calculatedHeight}get canvasCorners(){if(!this._canvasCornersDirty||!this.screen||!this.screen.screen.screenSpace)return this._canvasCorners;const t=this.system.app.graphicsDevice,e=this.screenCorners,s=t.canvas.clientWidth/t.width,i=t.canvas.clientHeight/t.height;for(let n=0;n<4;n++)this._canvasCorners[n].set(e[n].x*s,(t.height-e[n].y)*i);return this._canvasCornersDirty=!1,this._canvasCorners}set drawOrder(t){let e=0;this.screen&&(e=this.screen.screen.priority),t>16777215&&(t=16777215),this._drawOrder=(e<<24)+t,this.fire("set:draworder",this._drawOrder)}get drawOrder(){return this._drawOrder}set height(t){this._height=t,this._hasSplitAnchorsY||this._setCalculatedHeight(t,!0),this.fire("set:height",this._height)}get height(){return this._height}set layers(t){if(this._addedModels.length)for(let t=0;t<this._layers.length;t++){const e=this.system.app.scene.layers.getLayerById(this._layers[t]);if(e)for(let t=0;t<this._addedModels.length;t++)e.removeMeshInstances(this._addedModels[t].meshInstances)}if(this._layers=t,this.enabled&&this.entity.enabled&&this._addedModels.length)for(let t=0;t<this._layers.length;t++){const e=this.system.app.scene.layers.getLayerById(this._layers[t]);if(e)for(let t=0;t<this._addedModels.length;t++)e.addMeshInstances(this._addedModels[t].meshInstances)}}get layers(){return this._layers}set left(t){this._margin.x=t;const e=this.entity.getLocalPosition(),s=this._absRight,i=this._localAnchor.x+t;this._setWidth(s-i),e.x=t+this._calculatedWidth*this._pivot.x,this.entity.setLocalPosition(e)}get left(){return this._margin.x}set margin(t){this._margin.copy(t),this._calculateSize(!0,!0),this.fire("set:margin",this._margin)}get margin(){return this._margin}get maskedBy(){return this._maskedBy}set pivot(t){const{pivot:s,margin:i}=this,n=s.x,r=s.y;t instanceof e?s.copy(t):s.set(...t);const h=i.x+i.z,a=s.x-n;i.x+=h*a,i.z-=h*a;const o=i.y+i.w,l=s.y-r;i.y+=o*l,i.w-=o*l,this._anchorDirty=!0,this._cornersDirty=!0,this._worldCornersDirty=!0,this._calculateSize(!1,!1),this._flagChildrenAsDirty(),this.fire("set:pivot",s)}get pivot(){return this._pivot}set right(t){this._margin.z=t;const e=this.entity.getLocalPosition(),s=this._absLeft,i=this._localAnchor.z-t;this._setWidth(i-s),e.x=this._localAnchor.z-this._localAnchor.x-t-this._calculatedWidth*(1-this._pivot.x),this.entity.setLocalPosition(e)}get right(){return this._margin.z}get screenCorners(){if(!this._cornersDirty||!this.screen)return this._screenCorners;const t=this.entity.parent&&this.entity.parent.element&&this.entity.parent.element.screenCorners[0];this._screenCorners[0].set(this._absLeft,this._absBottom,0),this._screenCorners[1].set(this._absRight,this._absBottom,0),this._screenCorners[2].set(this._absRight,this._absTop,0),this._screenCorners[3].set(this._absLeft,this._absTop,0);const e=this.screen.screen.screenSpace;for(let s=0;s<4;s++)this._screenTransform.transformPoint(this._screenCorners[s],this._screenCorners[s]),e&&this._screenCorners[s].mulScalar(this.screen.screen.scale),t&&this._screenCorners[s].add(t);return this._cornersDirty=!1,this._canvasCornersDirty=!0,this._worldCornersDirty=!0,this._screenCorners}get textWidth(){return this._text?this._text.width:0}get textHeight(){return this._text?this._text.height:0}set top(t){this._margin.w=t;const e=this.entity.getLocalPosition(),s=this._absBottom,i=this._localAnchor.w-t;this._setHeight(i-s),e.y=this._localAnchor.w-this._localAnchor.y-t-this._calculatedHeight*(1-this._pivot.y),this.entity.setLocalPosition(e)}get top(){return this._margin.w}set type(t){t!==this._type&&(this._type=t,this._image&&(this._image.destroy(),this._image=null),this._text&&(this._text.destroy(),this._text=null),t===y?this._image=new p(this):t===g&&(this._text=new f(this)))}get type(){return this._type}set useInput(t){this._useInput!==t&&(this._useInput=t,this.system.app.elementInput?t?this.enabled&&this.entity.enabled&&this.system.app.elementInput.addElement(this):this.system.app.elementInput.removeElement(this):this._useInput,this.fire("set:useInput",t))}get useInput(){return this._useInput}set fitMode(t){this._fitMode=t,this._calculateSize(!0,!0),this._image&&this._image.refreshMesh()}get fitMode(){return this._fitMode}set width(t){this._width=t,this._hasSplitAnchorsX||this._setCalculatedWidth(t,!0),this.fire("set:width",this._width)}get width(){return this._width}get worldCorners(){if(!this._worldCornersDirty)return this._worldCorners;if(this.screen){const t=this.screenCorners;if(!this.screen.screen.screenSpace){L.copy(this.screen.screen._screenMatrix),L.data[13]=-L.data[13],L.mul2(this.screen.getWorldTransform(),L);for(let e=0;e<4;e++)L.transformPoint(t[e],this._worldCorners[e])}}else{const t=this.entity.getLocalPosition();L.setTranslate(-t.x,-t.y,-t.z),C.setTRS(s.ZERO,this.entity.getLocalRotation(),this.entity.getLocalScale()),M.setTranslate(t.x,t.y,t.z);const e=this.entity.parent?this.entity.parent:this.entity;T.copy(e.getWorldTransform()),T.mul(M).mul(C).mul(L),b.set(t.x-this.pivot.x*this.calculatedWidth,t.y-this.pivot.y*this.calculatedHeight,t.z),T.transformPoint(b,this._worldCorners[0]),b.set(t.x+(1-this.pivot.x)*this.calculatedWidth,t.y-this.pivot.y*this.calculatedHeight,t.z),T.transformPoint(b,this._worldCorners[1]),b.set(t.x+(1-this.pivot.x)*this.calculatedWidth,t.y+(1-this.pivot.y)*this.calculatedHeight,t.z),T.transformPoint(b,this._worldCorners[2]),b.set(t.x-this.pivot.x*this.calculatedWidth,t.y+(1-this.pivot.y)*this.calculatedHeight,t.z),T.transformPoint(b,this._worldCorners[3])}return this._worldCornersDirty=!1,this._worldCorners}set fontSize(t){this._setValue("fontSize",t)}get fontSize(){return this._text?this._text.fontSize:null}set minFontSize(t){this._setValue("minFontSize",t)}get minFontSize(){return this._text?this._text.minFontSize:null}set maxFontSize(t){this._setValue("maxFontSize",t)}get maxFontSize(){return this._text?this._text.maxFontSize:null}set maxLines(t){this._setValue("maxLines",t)}get maxLines(){return this._text?this._text.maxLines:null}set autoFitWidth(t){this._setValue("autoFitWidth",t)}get autoFitWidth(){return this._text?this._text.autoFitWidth:null}set autoFitHeight(t){this._setValue("autoFitHeight",t)}get autoFitHeight(){return this._text?this._text.autoFitHeight:null}set color(t){this._setValue("color",t)}get color(){return this._text?this._text.color:this._image?this._image.color:null}set font(t){this._setValue("font",t)}get font(){return this._text?this._text.font:null}set fontAsset(t){this._setValue("fontAsset",t)}get fontAsset(){return this._text&&"number"==typeof this._text.fontAsset?this._text.fontAsset:null}set spacing(t){this._setValue("spacing",t)}get spacing(){return this._text?this._text.spacing:null}set lineHeight(t){this._setValue("lineHeight",t)}get lineHeight(){return this._text?this._text.lineHeight:null}set wrapLines(t){this._setValue("wrapLines",t)}get wrapLines(){return this._text?this._text.wrapLines:null}set lines(t){this._setValue("lines",t)}get lines(){return this._text?this._text.lines:null}set alignment(t){this._setValue("alignment",t)}get alignment(){return this._text?this._text.alignment:null}set autoWidth(t){this._setValue("autoWidth",t)}get autoWidth(){return this._text?this._text.autoWidth:null}set autoHeight(t){this._setValue("autoHeight",t)}get autoHeight(){return this._text?this._text.autoHeight:null}set rtlReorder(t){this._setValue("rtlReorder",t)}get rtlReorder(){return this._text?this._text.rtlReorder:null}set unicodeConverter(t){this._setValue("unicodeConverter",t)}get unicodeConverter(){return this._text?this._text.unicodeConverter:null}set text(t){this._setValue("text",t)}get text(){return this._text?this._text.text:null}set key(t){this._setValue("key",t)}get key(){return this._text?this._text.key:null}set texture(t){this._setValue("texture",t)}get texture(){return this._image?this._image.texture:null}set textureAsset(t){this._setValue("textureAsset",t)}get textureAsset(){return this._image?this._image.textureAsset:null}set material(t){this._setValue("material",t)}get material(){return this._image?this._image.material:null}set materialAsset(t){this._setValue("materialAsset",t)}get materialAsset(){return this._image?this._image.materialAsset:null}set sprite(t){this._setValue("sprite",t)}get sprite(){return this._image?this._image.sprite:null}set spriteAsset(t){this._setValue("spriteAsset",t)}get spriteAsset(){return this._image?this._image.spriteAsset:null}set spriteFrame(t){this._setValue("spriteFrame",t)}get spriteFrame(){return this._image?this._image.spriteFrame:null}set pixelsPerUnit(t){this._setValue("pixelsPerUnit",t)}get pixelsPerUnit(){return this._image?this._image.pixelsPerUnit:null}set opacity(t){this._setValue("opacity",t)}get opacity(){return this._text?this._text.opacity:this._image?this._image.opacity:null}set rect(t){this._setValue("rect",t)}get rect(){return this._image?this._image.rect:null}set mask(t){this._setValue("mask",t)}get mask(){return this._image?this._image.mask:null}set outlineColor(t){this._setValue("outlineColor",t)}get outlineColor(){return this._text?this._text.outlineColor:null}set outlineThickness(t){this._setValue("outlineThickness",t)}get outlineThickness(){return this._text?this._text.outlineThickness:null}set shadowColor(t){this._setValue("shadowColor",t)}get shadowColor(){return this._text?this._text.shadowColor:null}set shadowOffset(t){this._setValue("shadowOffset",t)}get shadowOffset(){return this._text?this._text.shadowOffset:null}set enableMarkup(t){this._setValue("enableMarkup",t)}get enableMarkup(){return this._text?this._text.enableMarkup:null}set rangeStart(t){this._setValue("rangeStart",t)}get rangeStart(){return this._text?this._text.rangeStart:null}set rangeEnd(t){this._setValue("rangeEnd",t)}get rangeEnd(){return this._text?this._text.rangeEnd:null}_setValue(t,e){this._text?(this._text[t]!==e&&this._dirtyBatch(),this._text[t]=e):this._image&&(this._image[t]!==e&&this._dirtyBatch(),this._image[t]=e)}_patch(){this.entity._sync=this._sync,this.entity.setPosition=this._setPosition,this.entity.setLocalPosition=this._setLocalPosition}_unpatch(){this.entity._sync=_.prototype._sync,this.entity.setPosition=_.prototype.setPosition,this.entity.setLocalPosition=_.prototype.setLocalPosition}_setPosition(t,e,i){this.element.screen?(t instanceof s?x.copy(t):x.set(t,e,i),this.getWorldTransform(),w.copy(this.element._screenToWorld).invert(),w.transformPoint(x,this.localPosition),this._dirtyLocal||this._dirtifyLocal()):_.prototype.setPosition.call(this,t,e,i)}_setLocalPosition(t,e,i){t instanceof s?this.localPosition.copy(t):this.localPosition.set(t,e,i);const n=this.element,r=this.localPosition,h=n._pivot;n._margin.x=r.x-n._calculatedWidth*h.x,n._margin.z=n._localAnchor.z-n._localAnchor.x-n._calculatedWidth-n._margin.x,n._margin.y=r.y-n._calculatedHeight*h.y,n._margin.w=n._localAnchor.w-n._localAnchor.y-n._calculatedHeight-n._margin.y,this._dirtyLocal||this._dirtifyLocal()}_sync(){const t=this.element,e=t.screen;if(e){if(t._anchorDirty){let s=0,i=0,n=0,r=1;if(this._parent&&this._parent.element)s=this._parent.element.calculatedWidth,i=this._parent.element.calculatedHeight,n=this._parent.element.pivot.x,r=this._parent.element.pivot.y;else{const t=e.screen.resolution;s=t.x/e.screen.scale,i=t.y/e.screen.scale}t._anchorTransform.setTranslate(s*(t.anchor.x-n),-i*(r-t.anchor.y),0),t._anchorDirty=!1,t._calculateLocalAnchors()}t._sizeDirty&&t._calculateSize(!1,!1)}if(this._dirtyLocal){this.localTransform.setTRS(this.localPosition,this.localRotation,this.localScale);const e=this.localPosition,s=t._pivot;t._margin.x=e.x-t._calculatedWidth*s.x,t._margin.z=t._localAnchor.z-t._localAnchor.x-t._calculatedWidth-t._margin.x,t._margin.y=e.y-t._calculatedHeight*s.y,t._margin.w=t._localAnchor.w-t._localAnchor.y-t._calculatedHeight-t._margin.y,this._dirtyLocal=!1}if(!e)return this._dirtyWorld&&(t._cornersDirty=!0,t._canvasCornersDirty=!0,t._worldCornersDirty=!0),void _.prototype._sync.call(this);if(this._dirtyWorld){if(null===this._parent)this.worldTransform.copy(this.localTransform);else if(this._parent.element?t._screenToWorld.mul2(this._parent.element._modelTransform,t._anchorTransform):t._screenToWorld.copy(t._anchorTransform),t._modelTransform.mul2(t._screenToWorld,this.localTransform),e){t._screenToWorld.mul2(e.screen._screenMatrix,t._screenToWorld),e.screen.screenSpace||t._screenToWorld.mul2(e.worldTransform,t._screenToWorld),this.worldTransform.mul2(t._screenToWorld,this.localTransform);const i=t._parentWorldTransform;i.setIdentity();const n=this._parent;n&&n.element&&n!==e&&(L.setTRS(s.ZERO,n.getLocalRotation(),n.getLocalScale()),i.mul2(n.element._parentWorldTransform,L));const r=b;r.set(0,0,this.localPosition.z);const h=v;h.set(t._absLeft+t._pivot.x*t.calculatedWidth,t._absBottom+t._pivot.y*t.calculatedHeight,0),L.setTranslate(-h.x,-h.y,-h.z),C.setTRS(r,this.getLocalRotation(),this.getLocalScale()),M.setTranslate(h.x,h.y,h.z),t._screenTransform.mul2(t._parentWorldTransform,M).mul(C).mul(L),t._cornersDirty=!0,t._canvasCornersDirty=!0,t._worldCornersDirty=!0}else this.worldTransform.copy(t._modelTransform);this._dirtyWorld=!1}}_onInsert(t){const e=this._parseUpToScreen();this.entity._dirtifyWorld(),this._updateScreen(e.screen),this._dirtifyMask()}_dirtifyMask(){let t=this.entity;for(;t;){const e=t.parent;if((null===e||e.screen)&&t.element){this.system._prerender&&this.system._prerender.length||(this.system._prerender=[],this.system.app.once("prerender",this._onPrerender,this));const e=this.system._prerender.indexOf(this.entity);e>=0&&this.system._prerender.splice(e,1);this.system._prerender.indexOf(t)<0&&this.system._prerender.push(t)}t=e}}_onPrerender(){for(let t=0;t<this.system._prerender.length;t++){const e=this.system._prerender[t];if(e.element){const t=1;e.element.syncMask(t)}}this.system._prerender.length=0}_bindScreen(t){t._bindElement(this)}_unbindScreen(t){t._unbindElement(this)}_updateScreen(t){this.screen&&this.screen!==t&&this._unbindScreen(this.screen.screen);const e=this.screen;this.screen=t,this.screen&&this._bindScreen(this.screen.screen),this._calculateSize(this._hasSplitAnchorsX,this._hasSplitAnchorsY),this.fire("set:screen",this.screen,e),this._anchorDirty=!0;const s=this.entity.children;for(let e=0,i=s.length;e<i;e++)s[e].element&&s[e].element._updateScreen(t);this.screen&&this.screen.screen.syncDrawOrder()}syncMask(t){const e=this._parseUpToScreen();this._updateMask(e.mask,t)}_setMaskedBy(t){const e=this._image||this._text;if(t){const s=t.element._image._maskRef;null==e||e._setStencil(new c({ref:s,func:n})),this._maskedBy=t}else null==e||e._setStencil(null),this._maskedBy=null}_updateMask(t,e){if(t){if(this._setMaskedBy(t),this.mask){const s=t.element._image._maskRef,i=new c({ref:s,func:n,zpass:r});this._image._setStencil(i),this._image._maskRef=e,e++,t=this.entity}const i=this.entity.children;for(let n=0,r=i.length;n<r;n++){var s;null==(s=i[n].element)||s._updateMask(t,e)}this.mask&&e--}else{if(this._setMaskedBy(null),this.mask){const s=new c({ref:e,func:h,zpass:a});this._image._setStencil(s),this._image._maskRef=e,e++,t=this.entity}const s=this.entity.children;for(let n=0,r=s.length;n<r;n++){var i;null==(i=s[n].element)||i._updateMask(t,e)}this.mask&&e--}}_parseUpToScreen(){const t={screen:null,mask:null};let e=this.entity._parent;for(;e&&!e.screen;)e.element&&e.element.mask&&(t.mask||(t.mask=e)),e=e.parent;return e&&e.screen&&(t.screen=e),t}_onScreenResize(t){this._anchorDirty=!0,this._cornersDirty=!0,this._worldCornersDirty=!0,this._calculateSize(this._hasSplitAnchorsX,this._hasSplitAnchorsY),this.fire("screen:set:resolution",t)}_onScreenSpaceChange(){this.fire("screen:set:screenspace",this.screen.screen.screenSpace)}_onScreenRemove(){this.screen&&(this.screen._destroying?this.screen=null:this._updateScreen(null))}_calculateLocalAnchors(){let t=1e3,e=1e3;const s=this.entity._parent;if(s&&s.element)t=s.element.calculatedWidth,e=s.element.calculatedHeight;else if(this.screen){const s=this.screen.screen.resolution,i=this.screen.screen.scale;t=s.x/i,e=s.y/i}this._localAnchor.set(this._anchor.x*t,this._anchor.y*e,this._anchor.z*t,this._anchor.w*e)}getOffsetPosition(t,e){const s=this.entity.getLocalPosition().clone();return s.x+=t,s.y+=e,this._screenToWorld.transformPoint(s,s),s}onLayersChanged(t,e){this.addModelToLayers(this._image?this._image._renderable.model:this._text._model),t.off("add",this.onLayerAdded,this),t.off("remove",this.onLayerRemoved,this),e.on("add",this.onLayerAdded,this),e.on("remove",this.onLayerRemoved,this)}onLayerAdded(t){this.layers.indexOf(t.id)<0||(this._image?t.addMeshInstances(this._image._renderable.model.meshInstances):this._text&&t.addMeshInstances(this._text._model.meshInstances))}onLayerRemoved(t){this.layers.indexOf(t.id)<0||(this._image?t.removeMeshInstances(this._image._renderable.model.meshInstances):this._text&&t.removeMeshInstances(this._text._model.meshInstances))}onEnable(){const t=this.system.app.scene,e=t.layers;var s;(this._image&&this._image.onEnable(),this._text&&this._text.onEnable(),this._group&&this._group.onEnable(),this.useInput&&this.system.app.elementInput&&this.system.app.elementInput.addElement(this),this._evtLayersChanged=t.on("set:layers",this.onLayersChanged,this),e&&(this._evtLayerAdded=e.on("add",this.onLayerAdded,this),this._evtLayerRemoved=e.on("remove",this.onLayerRemoved,this)),this._batchGroupId>=0)&&(null==(s=this.system.app.batcher)||s.insert(l.ELEMENT,this.batchGroupId,this.entity));this.fire("enableelement")}onDisable(){var t;const e=this.system.app.scene.layers;var s,i,n;(null==(t=this._evtLayersChanged)||t.off(),this._evtLayersChanged=null,e)&&(null==(s=this._evtLayerAdded)||s.off(),this._evtLayerAdded=null,null==(i=this._evtLayerRemoved)||i.off(),this._evtLayerRemoved=null);(this._image&&this._image.onDisable(),this._text&&this._text.onDisable(),this._group&&this._group.onDisable(),this.system.app.elementInput&&this.useInput&&this.system.app.elementInput.removeElement(this),this._batchGroupId>=0)&&(null==(n=this.system.app.batcher)||n.remove(l.ELEMENT,this.batchGroupId,this.entity));this.fire("disableelement")}onRemove(){this.entity.off("insert",this._onInsert,this),this._unpatch(),this._image&&this._image.destroy(),this._text&&this._text.destroy(),this.system.app.elementInput&&this.useInput&&this.system.app.elementInput.removeElement(this),this.screen&&this.screen.screen&&(this._unbindScreen(this.screen.screen),this.screen.screen.syncDrawOrder()),this.off()}_calculateSize(t,e){if(!this.entity._parent&&!this.screen)return;this._calculateLocalAnchors();const s=this._absRight-this._absLeft,i=this._absTop-this._absBottom;t?this._setWidth(s):this._setCalculatedWidth(s,!1),e?this._setHeight(i):this._setCalculatedHeight(i,!1);const n=this.entity.getLocalPosition();n.x=this._margin.x+this._calculatedWidth*this._pivot.x,n.y=this._margin.y+this._calculatedHeight*this._pivot.y,this.entity.setLocalPosition(n),this._sizeDirty=!1}_setWidth(t){this._width=t,this._setCalculatedWidth(t,!1),this.fire("set:width",this._width)}_setHeight(t){this._height=t,this._setCalculatedHeight(t,!1),this.fire("set:height",this._height)}_setCalculatedWidth(t,e){if(!(Math.abs(t-this._calculatedWidth)<=1e-4)){if(this._calculatedWidth=t,this.entity._dirtifyLocal(),e){const t=this.entity.getLocalPosition(),e=this._pivot;this._margin.x=t.x-this._calculatedWidth*e.x,this._margin.z=this._localAnchor.z-this._localAnchor.x-this._calculatedWidth-this._margin.x}this._flagChildrenAsDirty(),this.fire("set:calculatedWidth",this._calculatedWidth),this.fire("resize",this._calculatedWidth,this._calculatedHeight)}}_setCalculatedHeight(t,e){if(!(Math.abs(t-this._calculatedHeight)<=1e-4)){if(this._calculatedHeight=t,this.entity._dirtifyLocal(),e){const t=this.entity.getLocalPosition(),e=this._pivot;this._margin.y=t.y-this._calculatedHeight*e.y,this._margin.w=this._localAnchor.w-this._localAnchor.y-this._calculatedHeight-this._margin.y}this._flagChildrenAsDirty(),this.fire("set:calculatedHeight",this._calculatedHeight),this.fire("resize",this._calculatedWidth,this._calculatedHeight)}}_flagChildrenAsDirty(){const t=this.entity._children;for(let e=0,s=t.length;e<s;e++)t[e].element&&(t[e].element._anchorDirty=!0,t[e].element._sizeDirty=!0)}addModelToLayers(t){this._addedModels.push(t);for(let e=0;e<this.layers.length;e++){const s=this.system.app.scene.layers.getLayerById(this.layers[e]);s&&s.addMeshInstances(t.meshInstances)}}removeModelFromLayers(t){const e=this._addedModels.indexOf(t);e>=0&&this._addedModels.splice(e,1);for(let e=0;e<this.layers.length;e++){const s=this.system.app.scene.layers.getLayerById(this.layers[e]);s&&s.removeMeshInstances(t.meshInstances)}}getMaskOffset(){const t=this.system.app.frame;this._offsetReadAt!==t&&(this._maskOffset=.5,this._offsetReadAt=t);const e=this._maskOffset;return this._maskOffset-=.001,e}isVisibleForCamera(t){let e,s,i,n;if(this.maskedBy){const t=this.maskedBy.element.screenCorners;e=Math.min(Math.min(t[0].x,t[1].x),Math.min(t[2].x,t[3].x)),s=Math.max(Math.max(t[0].x,t[1].x),Math.max(t[2].x,t[3].x)),n=Math.min(Math.min(t[0].y,t[1].y),Math.min(t[2].y,t[3].y)),i=Math.max(Math.max(t[0].y,t[1].y),Math.max(t[2].y,t[3].y))}else{const r=this.system.app.graphicsDevice.width,h=this.system.app.graphicsDevice.height,a=t._rect.z*r,o=t._rect.w*h;e=t._rect.x*r,s=e+a,i=(1-t._rect.y)*h,n=i-o}const r=this.screenCorners,h=Math.min(Math.min(r[0].x,r[1].x),Math.min(r[2].x,r[3].x)),a=Math.max(Math.max(r[0].x,r[1].x),Math.max(r[2].x,r[3].x)),o=Math.min(Math.min(r[0].y,r[1].y),Math.min(r[2].y,r[3].y)),l=Math.max(Math.max(r[0].y,r[1].y),Math.max(r[2].y,r[3].y));return!(a<e||h>s||o>i||l<n)}_isScreenSpace(){return!(!this.screen||!this.screen.screen)&&this.screen.screen.screenSpace}_isScreenCulled(){return!(!this.screen||!this.screen.screen)&&this.screen.screen.cull}_dirtyBatch(){var t;-1!==this.batchGroupId&&(null==(t=this.system.app.batcher)||t.markGroupDirty(this.batchGroupId))}}S.EVENT_MOUSEDOWN="mousedown",S.EVENT_MOUSEUP="mouseup",S.EVENT_MOUSEENTER="mouseenter",S.EVENT_MOUSELEAVE="mouseleave",S.EVENT_MOUSEMOVE="mousemove",S.EVENT_MOUSEWHEEL="mousewheel",S.EVENT_CLICK="click",S.EVENT_TOUCHSTART="touchstart",S.EVENT_TOUCHEND="touchend",S.EVENT_TOUCHMOVE="touchmove",S.EVENT_TOUCHCANCEL="touchcancel";export{S as ElementComponent};