@expofp/floorplan
Version:
Interactive floor plan library for expos and events
2 lines (1 loc) • 4.18 kB
JavaScript
import{pointRotate as M,Rect as W}from"@expofp/geometry";import{update as v}from"@expofp/renderer";import z from"color";import{reaction as T}from"mobx";import{SEPARATOR as _}from"../../../../constants";import w from"../../../../data";import{createShapeDef as Y,createTextDef as Z,createTextDefLines as Q}from"../../../../engine/defs";import{loadCustomFontsUrls as V}from"../../../../engine/fonts";import{getQuarterTurns as j,getRollValue as q}from"../../../../engine/update-def";import{boothStore as J,uiState as L}from"../../../../store";import{RegularBooth as K}from"../../../../store/BoothStore";import{getContrastTextColor as tt}from"../../../../utils/getContrastTextColor";import et from"../../../../utils/is-phone";import{BoothBadgeAnchors as ot,getBoothBadgeFootprint as nt}from"./booth-badge-anchor";import{getBoothFillColor as rt}from"./booth-fill-color.js";import{getFont as it}from"./canvases";import{isBoothBadgeEligible as st}from"./config-booth-badge-eligibility";import{getBadgeScale as at}from"./config-booth-badge-scale";import{getBadgeFontWeight as ct,getFontUrlByWeight as lt}from"./config-booth-labels";const P=15,ht=et?16:18,I=30,dt=6,mt=4,F=.8,H=I*F,U=document.createElement("canvas").getContext("2d");function gt(t){return t.length<=P?t:t.slice(0,P-1)+"\u2026"}export function createBoothBadge(t){const n=gt(t.title||t.name),e=rt(t),r=tt(z(e).hex()),a=ct(),h=it(I,a);U.font=h;const i=U.measureText(n).width,c=Math.ceil(i+dt*2),m=I+mt*2,{geometry:E,outlineInset:$}=nt(t),X=Math.max((t.borderWidth||J.borderWidth)/2,$),N=c*F,R=m*F,A=new ot(t,E,X,{x:N,y:R});let d=t.rotate??0,b=0,g=0,u=0,D=0;const O=()=>{const o=A.fit(b,d);g=o.x,u=o.y,D=o.maxScale};O();const C=(o,S)=>{const y=o/2,B=S/2,k=M({x:g+y,y:u+B},d,{x:g,y:u});return new W(k,{x:o,y:S},d,t.elevation)},G=lt(a,V()),s=Y(C(1,1),e);s.hidden=!0,s.dim=t.skipDim?0:void 0;const l=Z(Q([[n],[]],r,0,[G,G]),C(1,1),[0,0],{horizontal:"center",vertical:"center"});l.hidden=!0,l.dim=t.skipDim?0:void 0;const f={booth:t,anchor:{x:g,y:u},source:{x:N,y:R},rotation:d,maxScale:D,bg:s,text:l},p=new BoothBadgeDrawer(f);s.onScale=({pixelSize:o})=>p.layOutBg(o),l.onScale=({pixelSize:o})=>p.layOutText(o);const x=[];return x.push(T(()=>t.skipDim,o=>{s.dim=o?0:void 0,l.dim=o?0:void 0,v(s,l)})),x.push(T(()=>t.layer?.visible??!0,o=>{s.hidden=!o,l.hidden=!o,v(s,l)})),x.push(T(()=>L.mapSettings,({roll:o})=>{q(o??0)&&(A.prefetch(t.rotate??0),s.onRoll=({roll:S,pixelSize:y})=>{const B=j(S,d);if(B!==0)return d+=B*Math.PI/2,b=((b+B)%4+4)%4,O(),f.anchor.x=g,f.anchor.y=u,f.rotation=d,f.maxScale=D,p.invalidate(),[...p.layOutBg(y),...p.layOutText(y)]})},{fireImmediately:!0}),()=>A.cancelPrefetch()),{bg:s,text:l,dispose:()=>{x.forEach(o=>o()),x.length=0}}}export class BoothBadgeDrawer{constructor(n){this.placement=n,this.bgScale=NaN,this.textScale=NaN}invalidate(){this.bgScale=NaN,this.textScale=NaN}layOutBg(n){const{bg:e}=this.placement,r=this.scaleFor(n);return r===this.bgScale?[]:(this.bgScale=r,e.shape=this.rectAt(r),e.hidden=this.isHidden(),e.hidden?[]:[e])}layOutText(n){const{text:e}=this.placement,r=this.scaleFor(n);return r===this.textScale?[]:(this.textScale=r,e.lines[0].fontSize=H*r,e.bounds=this.rectAt(r),e.hidden=this.isHidden(),e.hidden?[]:[e])}scaleFor(n){return at({pixelSize:n,fontSize:ht,plateFontSize:H,maxScale:this.placement.maxScale})}rectAt(n){const{anchor:e,booth:r,source:a,rotation:h}=this.placement,i=a.x*n,c=a.y*n,m=M({x:e.x+i/2,y:e.y+c/2},h,e);return new W(m,{x:i,y:c},h,r.elevation)}isHidden(){const{booth:n}=this.placement;return!!(n.layer&&!n.layer.visible)}}export function configBoothsBadges(t,n){const e=[],r=[],a=[];for(const i of n)if(i instanceof K&&st({hideExhibitorBoothNumber:w.hideExhibitorBoothNumber,hideBoothBadges:w.hideBoothBadges,hideLogoInBooth:L.hideLogoInBooth,noLabels:i.noLabels,hasExhibitors:i.exhibitors.length>0,hasLogoInBooth:i.exhibitors.some(c=>c.logoInBooth&&c.logo)})){const{bg:c,text:m,dispose:E}=createBoothBadge(i);e.push(c),r.push(m),a.push(E)}const h=()=>{a.forEach(i=>i()),a.length=0};return e.length?{layer:{name:`${t}${_}badges`,children:[{name:`${t}${_}badgeBgs`,children:e},{name:`${t}${_}badgeTexts`,children:r}]},cleanup:h}:{layer:null,cleanup:h}}