UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 4.55 kB
import{Vec2 as e}from"../../../core/math/vec2.js";import{Vec4 as t}from"../../../core/math/vec4.js";import{ORIENTATION_HORIZONTAL as i}from"../../../scene/constants.js";import{FITTING_NONE as s}from"./constants.js";import{Component as n}from"../component.js";import{LayoutCalculator as h}from"./layout-calculator.js";function o(e){return e.element}function l(e){return e.enabled&&e.element&&e.element.enabled}class r extends n{constructor(n,o){super(n,o),this._orientation=i,this._reverseX=!1,this._reverseY=!0,this._alignment=new e(0,1),this._padding=new t,this._spacing=new e,this._widthFitting=s,this._heightFitting=s,this._wrap=!1,this._layoutCalculator=new h,this._listenForReflowEvents(this.entity,"on"),this.entity.children.forEach((e=>{this._listenForReflowEvents(e,"on")})),this.entity.on("childinsert",this._onChildInsert,this),this.entity.on("childremove",this._onChildRemove,this),n.app.systems.element.on("add",this._onElementOrLayoutComponentAdd,this),n.app.systems.element.on("beforeremove",this._onElementOrLayoutComponentRemove,this),n.app.systems.layoutchild.on("add",this._onElementOrLayoutComponentAdd,this),n.app.systems.layoutchild.on("beforeremove",this._onElementOrLayoutComponentRemove,this)}set orientation(e){e!==this._orientation&&(this._orientation=e,this._scheduleReflow())}get orientation(){return this._orientation}set reverseX(e){e!==this._reverseX&&(this._reverseX=e,this._scheduleReflow())}get reverseX(){return this._reverseX}set reverseY(e){e!==this._reverseY&&(this._reverseY=e,this._scheduleReflow())}get reverseY(){return this._reverseY}set alignment(e){e.equals(this._alignment)||(this._alignment.copy(e),this._scheduleReflow())}get alignment(){return this._alignment}set padding(e){e.equals(this._padding)||(this._padding.copy(e),this._scheduleReflow())}get padding(){return this._padding}set spacing(e){e.equals(this._spacing)||(this._spacing.copy(e),this._scheduleReflow())}get spacing(){return this._spacing}set widthFitting(e){e!==this._widthFitting&&(this._widthFitting=e,this._scheduleReflow())}get widthFitting(){return this._widthFitting}set heightFitting(e){e!==this._heightFitting&&(this._heightFitting=e,this._scheduleReflow())}get heightFitting(){return this._heightFitting}set wrap(e){e!==this._wrap&&(this._wrap=e,this._scheduleReflow())}get wrap(){return this._wrap}_isSelfOrChild(e){return e===this.entity||-1!==this.entity.children.indexOf(e)}_listenForReflowEvents(e,t){e.element&&(e.element[t]("enableelement",this._scheduleReflow,this),e.element[t]("disableelement",this._scheduleReflow,this),e.element[t]("resize",this._scheduleReflow,this),e.element[t]("set:pivot",this._scheduleReflow,this)),e.layoutchild&&(e.layoutchild[t]("set_enabled",this._scheduleReflow,this),e.layoutchild[t]("resize",this._scheduleReflow,this))}_onElementOrLayoutComponentAdd(e){this._isSelfOrChild(e)&&(this._listenForReflowEvents(e,"on"),this._scheduleReflow())}_onElementOrLayoutComponentRemove(e){this._isSelfOrChild(e)&&(this._listenForReflowEvents(e,"off"),this._scheduleReflow())}_onChildInsert(e){this._listenForReflowEvents(e,"on"),this._scheduleReflow()}_onChildRemove(e){this._listenForReflowEvents(e,"off"),this._scheduleReflow()}_scheduleReflow(){this.enabled&&this.entity&&this.entity.enabled&&!this._isPerformingReflow&&this.system.scheduleReflow(this)}reflow(){const t=o(this.entity),i=this.entity.children.filter(l).map(o);if(!t||0===i.length)return;const s=Math.max(t.calculatedWidth,0),n=Math.max(t.calculatedHeight,0),h={orientation:this._orientation,reverseX:this._reverseX,reverseY:this._reverseY,alignment:this._alignment,padding:this._padding,spacing:this._spacing,widthFitting:this._widthFitting,heightFitting:this._heightFitting,wrap:this._wrap,containerSize:new e(s,n)};this._isPerformingReflow=!0;const r=this._layoutCalculator.calculateLayout(i,h);this._isPerformingReflow=!1,this.fire("reflow",r)}onEnable(){this._scheduleReflow()}onRemove(){this.entity.off("childinsert",this._onChildInsert,this),this.entity.off("childremove",this._onChildRemove,this),this._listenForReflowEvents(this.entity,"off"),this.entity.children.forEach((e=>{this._listenForReflowEvents(e,"off")})),this.system.app.systems.element.off("add",this._onElementOrLayoutComponentAdd,this),this.system.app.systems.element.off("beforeremove",this._onElementOrLayoutComponentRemove,this),this.system.app.systems.layoutchild.off("add",this._onElementOrLayoutComponentAdd,this),this.system.app.systems.layoutchild.off("beforeremove",this._onElementOrLayoutComponentRemove,this)}}export{r as LayoutGroupComponent};