ecspresso
Version:
A minimal Entity-Component-System library for typescript and javascript.
5 lines (3 loc) • 12.9 kB
JavaScript
var E=((J)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(J,{get:(K,Q)=>(typeof require<"u"?require:K)[Q]}):J)(function(J){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+J+'" is not supported')});import{definePlugin as x}from"ecspresso";var M={x:0,y:0,rotation:0,scaleX:1,scaleY:1},l={x:0,y:0,rotation:0,scaleX:1,scaleY:1};function i(J,K){return{localTransform:{x:J,y:K,rotation:0,scaleX:1,scaleY:1}}}function r(J,K){return{worldTransform:{x:J,y:K,rotation:0,scaleX:1,scaleY:1}}}function n(J,K,Q){let Z=Q?.scale??Q?.scaleX??1,k=Q?.scale??Q?.scaleY??1,N=Q?.rotation??0,G={x:J,y:K,rotation:N,scaleX:Z,scaleY:k};return{localTransform:{...G},worldTransform:{...G}}}function t(J){let{systemGroup:K="transform",priority:Q=500,phase:Z="postUpdate"}=J??{};return x("transform").withComponentTypes().withLabels().withGroups().install((k)=>{k.registerRequired("localTransform","worldTransform",(q)=>({x:q.x,y:q.y,rotation:q.rotation,scaleX:q.scaleX,scaleY:q.scaleY}));let N=[],G=new Set;k.addSystem("transform-propagation").setPriority(Q).inPhase(Z).inGroup(K).setProcess(({ecs:q})=>{f(q,N,G)})})}function f(J,K,Q){let Z=J.entityManager;if(!Z.hasHierarchy){Z.getEntitiesWithQueryInto(K,["localTransform","worldTransform"]);for(let k of K){let{localTransform:N,worldTransform:G}=k.components;if(R(N,G))J.markChanged(k.id,"worldTransform")}return}Q.clear(),J.forEachInHierarchy((k,N)=>{Q.add(k);let G=Z.getComponent(k,"localTransform"),q=Z.getComponent(k,"worldTransform");if(!G||!q)return;let B=N!==null?Z.getComponent(N,"worldTransform"):null;if(B?h(B,G,q):R(G,q))J.markChanged(k,"worldTransform")}),Z.getEntitiesWithQueryInto(K,["localTransform","worldTransform"]);for(let k of K){if(Q.has(k.id))continue;let{localTransform:N,worldTransform:G}=k.components;if(R(N,G))J.markChanged(k.id,"worldTransform")}}function R(J,K){if(K.x===J.x&&K.y===J.y&&K.rotation===J.rotation&&K.scaleX===J.scaleX&&K.scaleY===J.scaleY)return!1;return K.x=J.x,K.y=J.y,K.rotation=J.rotation,K.scaleX=J.scaleX,K.scaleY=J.scaleY,!0}function h(J,K,Q){let Z=K.x*J.scaleX,k=K.y*J.scaleY,N=Math.cos(J.rotation),G=Math.sin(J.rotation),q=Z*N-k*G,B=Z*G+k*N,U=J.x+q,X=J.y+B,V=J.rotation+K.rotation,Y=J.scaleX*K.scaleX,H=J.scaleY*K.scaleY;if(Q.x===U&&Q.y===X&&Q.rotation===V&&Q.scaleX===Y&&Q.scaleY===H)return!1;return Q.x=U,Q.y=X,Q.rotation=V,Q.scaleX=Y,Q.scaleY=H,!0}import{definePlugin as w}from"ecspresso";var b=Object.freeze({"top-left":Object.freeze({x:0,y:0}),"top-center":Object.freeze({x:0.5,y:0}),"top-right":Object.freeze({x:1,y:0}),"center-left":Object.freeze({x:0,y:0.5}),center:Object.freeze({x:0.5,y:0.5}),"center-right":Object.freeze({x:1,y:0.5}),"bottom-left":Object.freeze({x:0,y:1}),"bottom-center":Object.freeze({x:0.5,y:1}),"bottom-right":Object.freeze({x:1,y:1})});function P(J){if(typeof J==="string"){let K=b[J];return{x:K.x,y:K.y}}return{x:J.x,y:J.y}}function g(J,K,Q,Z,k,N){N.x=J.x*Z.width+Q.x-K.x*k.width,N.y=J.y*Z.height+Q.y-K.y*k.height}function y(J,K){if(K<=0)return 0;if(J<=0)return 0;if(J>=K)return K;return J}var d={ltr:(J,K,Q,Z)=>{Z.x=0,Z.y=0,Z.width=J*Q,Z.height=K},rtl:(J,K,Q,Z)=>{Z.x=J*(1-Q),Z.y=0,Z.width=J*Q,Z.height=K},ttb:(J,K,Q,Z)=>{Z.x=0,Z.y=0,Z.width=J,Z.height=K*Q},btt:(J,K,Q,Z)=>{Z.x=0,Z.y=K*(1-Q),Z.width=J,Z.height=K*Q}};function p(J,K,Q,Z,k){d[Z](J,K,Q,k)}var T=Object.freeze({fontFamily:"sans-serif",fontSize:16,fill:16777215,align:"left"});function QJ(J){let K=P(J.anchor),Q=J.pivot===void 0?{x:K.x,y:K.y}:P(J.pivot),Z=J.offset===void 0?{x:0,y:0}:{x:J.offset.x,y:J.offset.y};return{uiElement:{anchor:K,pivot:Q,offset:Z,width:J.width,height:J.height}}}function ZJ(J,K){return{uiLabel:{text:J,style:{...T,...K}}}}function $J(J){return{uiPanel:{fillColor:J.fillColor,borderColor:J.borderColor,borderWidth:J.borderWidth??0}}}function kJ(J){return{uiProgressBar:{value:J.value,max:J.max,fillColor:J.fillColor,bgColor:J.bgColor,direction:J.direction??"ltr"}}}function HJ(J){return{uiMessageLog:{lines:J.initialLines===void 0?[]:[...J.initialLines],maxLines:J.maxLines,visibleLines:J.visibleLines,lineHeight:J.lineHeight,style:{...T,...J.style}}}}function WJ(){return{uiInteractive:{}}}function qJ(){return{uiButton:{}}}function zJ(){return{uiDisabled:{}}}function NJ(J,K,Q){J.commands.mutateComponent(K,"uiMessageLog",(Z)=>{let k=Math.max(0,Z.maxLines),N=[...Z.lines,Q];Z.lines=N.length>k?N.slice(N.length-k):N}),J.eventBus.publish("uiLogAppended",{entityId:K,line:Q})}function DJ(J){let{systemGroup:K="ui",anchorPriority:Q=0,interactionPriority:Z=200,renderSyncPriority:k=480}=J??{},N=new Map,G=new Map,q=new Map,B=new Map,U={x:0,y:0},X={x:0,y:0,width:0,height:0},V=null;return w("ui").withComponentTypes().withEventTypes().withLabels().withGroups().withReactiveQueryNames().requires().install((Y)=>{Y.registerRequired("uiElement","localTransform",()=>({x:M.x,y:M.y,rotation:M.rotation,scaleX:M.scaleX,scaleY:M.scaleY})),Y.registerRequired("uiButton","uiInteractive",()=>({})),Y.registerRequired("uiInteractive","uiInteraction",()=>({state:"none"})),Y.addSystem("ui-anchor-resolve").setPriority(Q).inPhase("preUpdate").inGroup(K).addQuery("uiElements",{with:["uiElement","localTransform"]}).setProcess(({queries:H,ecs:z})=>{let D=z.getResource("bounds");for(let W of H.uiElements){let{uiElement:$,localTransform:O}=W.components;if(g($.anchor,$.pivot,$.offset,D,$,U),O.x!==U.x||O.y!==U.y)O.x=U.x,O.y=U.y,z.markChanged(W.id,"localTransform")}}),Y.addSystem("ui-interaction").setPriority(Z).inPhase("preUpdate").inGroup(K).addQuery("interactables",{with:["uiInteractive","uiInteraction","uiElement","worldTransform"],without:["uiDisabled"]}).setProcess(({queries:H,ecs:z})=>{let D=z.getResource("inputState").pointer,W=D.position.x,$=D.position.y,O=D.isDown(0),I=D.justReleased(0);for(let j of H.interactables){let{uiElement:v,worldTransform:C,uiInteraction:F}=j.components,L=W>=C.x&&W<C.x+v.width&&$>=C.y&&$<C.y+v.height,_=F.state,S=!L?"none":O?_==="none"?"hover":"pressed":"hover";if(_==="pressed"&&S==="hover"&&I&&L)z.eventBus.publish("uiButtonPressed",{entityId:j.id});if(_==="none"&&S!=="none")z.eventBus.publish("uiButtonHovered",{entityId:j.id,entered:!0});if(_!=="none"&&S==="none")z.eventBus.publish("uiButtonHovered",{entityId:j.id,entered:!1});if(_!==S)F.state=S,z.markChanged(j.id,"uiInteraction")}}),Y.addSystem("ui-panel-sync").setPriority(k).inPhase("render").inGroup(K).setOnInitialize(async(H)=>{let z=await import("pixi.js"),D=H.tryGetResource("rootContainer");H.addReactiveQuery("ui-panels",{with:["uiPanel","uiElement"],onEnter:(W)=>{let $=new z.Graphics;if(G.set(W.id,{pixiGraphics:$,lastFillColor:Number.NaN,lastBorderColor:void 0,lastBorderWidth:Number.NaN,lastWidth:Number.NaN,lastHeight:Number.NaN}),D)D.addChild($)},onExit:(W)=>{let $=G.get(W);if($)$.pixiGraphics.removeFromParent(),$.pixiGraphics.destroy(),G.delete(W)}})}).setProcess(({ecs:H})=>{for(let[z,D]of G){let W=H.getComponent(z,"uiPanel"),$=H.getComponent(z,"uiElement");if(!W||!$)continue;m(D,W,$),A(D.pixiGraphics,H.getComponent(z,"worldTransform"))}}),Y.addSystem("ui-progress-sync").setPriority(k).inPhase("render").inGroup(K).setOnInitialize(async(H)=>{let z=await import("pixi.js"),D=H.tryGetResource("rootContainer");H.addReactiveQuery("ui-progress-bars",{with:["uiProgressBar","uiElement"],onEnter:(W)=>{let $=new z.Graphics;if(q.set(W.id,{pixiGraphics:$,lastValue:Number.NaN,lastMax:Number.NaN,lastFillColor:Number.NaN,lastBgColor:Number.NaN,lastDirection:"ltr",lastWidth:Number.NaN,lastHeight:Number.NaN}),D)D.addChild($)},onExit:(W)=>{let $=q.get(W);if($)$.pixiGraphics.removeFromParent(),$.pixiGraphics.destroy(),q.delete(W)}})}).setProcess(({ecs:H})=>{for(let[z,D]of q){let W=H.getComponent(z,"uiProgressBar"),$=H.getComponent(z,"uiElement");if(!W||!$)continue;u(D,W,$,X),A(D.pixiGraphics,H.getComponent(z,"worldTransform"))}}),Y.addSystem("ui-message-log-sync").setPriority(k).inPhase("render").inGroup(K).setOnInitialize(async(H)=>{let z=await import("pixi.js");V=z;let D=H.tryGetResource("rootContainer");H.addReactiveQuery("ui-message-logs",{with:["uiMessageLog","uiElement"],onEnter:(W)=>{let $=new z.Container;if(B.set(W.id,{rootContainer:$,lines:[],lastLinesRef:null,lastVisibleLines:-1,lastLineHeight:Number.NaN,lastFontFamily:"",lastFontSize:Number.NaN,lastAlign:""}),D)D.addChild($)},onExit:(W)=>{let $=B.get(W);if($)$.rootContainer.removeFromParent(),$.rootContainer.destroy({children:!0}),B.delete(W)}})}).setProcess(({ecs:H})=>{if(!V)return;for(let[z,D]of B){let W=H.getComponent(z,"uiMessageLog");if(!W)continue;s(D,W,V),A(D.rootContainer,H.getComponent(z,"worldTransform"))}}),Y.addSystem("ui-label-sync").setPriority(k).inPhase("render").inGroup(K).setOnInitialize(async(H)=>{let z=await import("pixi.js"),D=H.tryGetResource("rootContainer");H.addReactiveQuery("ui-labels",{with:["uiLabel"],onEnter:(W)=>{let $=W.components.uiLabel,O=new z.Text({text:$.text,style:{fontFamily:$.style.fontFamily,fontSize:$.style.fontSize,fill:$.style.fill,align:$.style.align}});if(N.set(W.id,{pixiText:O,lastText:$.text,lastFontSize:$.style.fontSize,lastFill:$.style.fill,lastAlign:$.style.align,lastFontFamily:$.style.fontFamily}),D)D.addChild(O)},onExit:(W)=>{let $=N.get(W);if($)$.pixiText.removeFromParent(),$.pixiText.destroy(),N.delete(W)}})}).setProcess(({ecs:H})=>{for(let[z,D]of N){let W=H.getComponent(z,"uiLabel");if(!W)continue;c(D,W),A(D.pixiText,H.getComponent(z,"worldTransform"))}})})}function A(J,K){if(!K)return;if(J.position.x!==K.x)J.position.x=K.x;if(J.position.y!==K.y)J.position.y=K.y}function c(J,K){if(J.lastText!==K.text)J.pixiText.text=K.text,J.lastText=K.text;let Q=J.pixiText.style;if(J.lastFontSize!==K.style.fontSize)Q.fontSize=K.style.fontSize,J.lastFontSize=K.style.fontSize;if(J.lastFill!==K.style.fill)Q.fill=K.style.fill,J.lastFill=K.style.fill;if(J.lastAlign!==K.style.align)Q.align=K.style.align,J.lastAlign=K.style.align;if(J.lastFontFamily!==K.style.fontFamily)Q.fontFamily=K.style.fontFamily,J.lastFontFamily=K.style.fontFamily}function m(J,K,Q){if(!(J.lastFillColor!==K.fillColor||J.lastBorderColor!==K.borderColor||J.lastBorderWidth!==K.borderWidth||J.lastWidth!==Q.width||J.lastHeight!==Q.height))return;let k=J.pixiGraphics;if(k.clear(),k.rect(0,0,Q.width,Q.height),k.fill({color:K.fillColor}),K.borderColor!==void 0&&K.borderWidth>0)k.stroke({color:K.borderColor,width:K.borderWidth});J.lastFillColor=K.fillColor,J.lastBorderColor=K.borderColor,J.lastBorderWidth=K.borderWidth,J.lastWidth=Q.width,J.lastHeight=Q.height}function u(J,K,Q,Z){if(!(J.lastValue!==K.value||J.lastMax!==K.max||J.lastFillColor!==K.fillColor||J.lastBgColor!==K.bgColor||J.lastDirection!==K.direction||J.lastWidth!==Q.width||J.lastHeight!==Q.height))return;let N=y(K.value,K.max),G=K.max>0?N/K.max:0;p(Q.width,Q.height,G,K.direction,Z);let q=J.pixiGraphics;if(q.clear(),q.rect(0,0,Q.width,Q.height),q.fill({color:K.bgColor}),Z.width>0&&Z.height>0)q.rect(Z.x,Z.y,Z.width,Z.height),q.fill({color:K.fillColor});J.lastValue=K.value,J.lastMax=K.max,J.lastFillColor=K.fillColor,J.lastBgColor=K.bgColor,J.lastDirection=K.direction,J.lastWidth=Q.width,J.lastHeight=Q.height}function s(J,K,Q){let Z=J.lastFontFamily!==K.style.fontFamily||J.lastFontSize!==K.style.fontSize||J.lastAlign!==K.style.align,k=J.lastLinesRef!==K.lines,N=J.lastVisibleLines!==K.visibleLines||J.lastLineHeight!==K.lineHeight;if(!k&&!N&&!Z)return;let G=K.lines.slice(-K.visibleLines);while(J.lines.length<G.length){let q=new Q.Container;J.rootContainer.addChild(q),J.lines.push({container:q,texts:[]})}if(J.lines.forEach((q,B)=>{if(B>=G.length)q.container.visible=!1}),G.forEach((q,B)=>{let U=J.lines[B];if(!U)return;U.container.visible=!0,U.container.position.y=B*K.lineHeight;while(U.texts.length<q.length){let V=new Q.Text({text:"",style:{fontFamily:K.style.fontFamily,fontSize:K.style.fontSize,fill:16777215,align:K.style.align}});U.container.addChild(V),U.texts.push(V)}let X=0;U.texts.forEach((V,Y)=>{let H=q[Y];if(!H){V.visible=!1;return}if(V.visible=!0,V.text!==H.text)V.text=H.text;if(V.style.fill!==H.color)V.style.fill=H.color;V.position.x=X,X+=V.width})}),Z)J.lines.forEach((q)=>{q.texts.forEach((B)=>{let U=B.style;if(U.fontFamily!==K.style.fontFamily)U.fontFamily=K.style.fontFamily;if(U.fontSize!==K.style.fontSize)U.fontSize=K.style.fontSize;if(U.align!==K.style.align)U.align=K.style.align})});J.lastLinesRef=K.lines,J.lastVisibleLines=K.visibleLines,J.lastLineHeight=K.lineHeight,J.lastFontFamily=K.style.fontFamily,J.lastFontSize=K.style.fontSize,J.lastAlign=K.style.align}export{P as resolveAnchorPreset,g as resolveAnchorPosition,kJ as createUIProgressBar,DJ as createUIPlugin,$J as createUIPanel,HJ as createUIMessageLog,ZJ as createUILabel,WJ as createUIInteractive,QJ as createUIElement,zJ as createUIDisabled,qJ as createUIButton,p as computeProgressFillRect,y as clampProgressValue,NJ as appendLogLine,b as ANCHOR_PRESETS};
//# debugId=B6E1260EDCFC611D64756E2164756E21
//# sourceMappingURL=ui.js.map