UNPKG

@expofp/floorplan

Version:

Interactive floor plan library for expos and events

2 lines (1 loc) 4.7 kB
import{Box as v,pointRotate as S,Polygon as tt,Rect as nt}from"@expofp/geometry";import et from"color";import{reaction as W}from"mobx";import{SEPARATOR as $}from"../../../../constants";import N from"../../../../data";import{getTrianglesFromFpPaths as ot}from"../../../../data/svg";import{createShapeDef as st,createTextDef as it,createTextDefLines as rt,getRollValue as ct,getRotation as at,rectToRenderer as X}from"../../../../renderer";import{boothStore as lt,uiState as Z}from"../../../../store";import{RegularBooth as dt}from"../../../../store/BoothStore";import V from"../../../../tools/settings";import{getContrastTextColor as ft}from"../../../../utils/getContrastTextColor";import{getFont as ut}from"./canvases";import{isBoothBadgeEligible as gt}from"./config-booth-badge-eligibility";import{getBadgeFontWeight as ht,getFontUrlByWeight as mt}from"./config-booth-labels";const pt=.5,xt=.3,j=15,Bt=24,q=30,yt=12,At=12,J=.8,K=document.createElement("canvas").getContext("2d");function Et(t){return t.length<=j?t:t.slice(0,j-1)+"\u2026"}function Q(t){if(!t.paths?.length)return[];const n=t.layer?.name||"",s=[];for(const e of t.paths)for(const i of ot(e.index,n))s.push(i);return s}function Tt(t){const n=Q(t);return n.length?v.fromPoints(n.flat().map(([s,e])=>({x:s,y:e}))):null}function It(t){const n=[],s=[];for(const e of t){const i=n.length;n.push({x:e[0][0],y:e[0][1]}),n.push({x:e[1][0],y:e[1][1]}),n.push({x:e[2][0],y:e[2][1]}),s.push([i,i+1,i+2])}return new tt(n,s)}function H(t,n,s){return s.containsPoint({x:t,y:n})}function Rt(t,n,s,e,i,u,l,r){if(!t.length||!H(s,e,n))return l;const c={x:s,y:e};let d=0,B=l;const R=10;for(let p=0;p<R;p++){const g=(d+B)/2,I=i*g,x=u*g;[S({x:s+I,y:e},r,c),S({x:s,y:e+x},r,c),S({x:s+I,y:e+x},r,c)].every(y=>H(y.x,y.y,n))?d=g:B=g}return d}function _t(t){return Tt(t)??t.rect}function Dt(t,n,s,e,i,u){if(n.length){const d=_t(t),B={x:i>0?d.min.x:d.max.x,y:u>0?d.min.y:d.max.y},R=new Set;let p=null,g=1/0;for(const I of n)for(const x of I){const _=`${x[0]},${x[1]}`;if(R.has(_))continue;R.add(_);const A=x[0]+e*i,y=x[1]+e*u;if(H(A,y,s)){const E=Math.abs(A-B.x)+Math.abs(y-B.y);E<g&&(g=E,p={x:A,y})}}if(p)return p}const l=i>0?t.rect.min.x+e:t.rect.max.x-e,r=u>0?t.rect.min.y+e:t.rect.max.y-e,c=t.rotate??0;return c?S({x:l,y:r},c,t.rect.center):{x:l,y:r}}function F(t){return t&&t!=="none"?t:void 0}function St(t){const n=F(t.borderColor)||F(V.boothBorderColor)||V.colors.booths.default;return t.paths?.length>1&&F(t.paths[t.paths.length-1].color)||n}export function createBoothBadge(t,n){const s=Et(n.title||n.name),e=St(n),i=ft(et(e).hex()),u=window.devicePixelRatio||1,l=ht(),r=ut(q,l);K.font=r;const c=K.measureText(s).width,d=Math.ceil(c+yt*2),B=q+At*2,R=(n.borderWidth||lt.borderWidth)/2,p=d*J,g=B*J,I=n.rect.size.width,x=n.rect.size.height,_=o=>{const a=o?x:I,m=o?I:x;return Math.min(a>0?a*pt/p:1/0,m>0?m*xt/g:1/0)},A=Q(n),y=It(A);let E=n.rotate??0,T=0,P,C,M;const L=()=>{const m=Dt(n,A,y,R,T===0||T===3?1:-1,T===0||T===1?1:-1);P=m.x,C=m.y;const G=_(T===1||T===3);M=Rt(A,y,P,C,p,g,G,E)};L();const b=(o,a)=>{const m=o/2,G=a/2,Y=S({x:P+m,y:C+G},E,{x:P,y:C});return new nt(Y,{x:o,y:a},E)},O=o=>{const a=Math.min(o,M);return b(p*a,g*a)},w=mt(l,t.fontUrls),h=st(b(1,1),e);h.hidden=!0,h.dim=n.skipDim?!1:void 0;const f=it(rt([[s],[]],i,0,[w,w]),b(1,1),[0,0],{horizontal:"center",vertical:"center"});f.hidden=!0,f.dim=n.skipDim?!1:void 0;const U=()=>!!(n.layer&&!n.layer.visible),D=[];D.push(W(()=>n.skipDim,o=>{h.dim=o?!1:void 0,f.dim=o?!1:void 0,t.update(h,f)})),D.push(W(()=>n.layer?.visible??!0,o=>{h.hidden=!o,f.hidden=!o,t.update(h,f)}));const k=o=>{h.shape=X(O(o)),h.hidden=U()},z=o=>{const a=Math.min(o,M);f.lines[0].fontSize=Bt/u*a,f.bounds=X(O(o)),f.hidden=U()};return t.onPtScale(`badge-bg-${n.id}`,o=>(k(o),h)),t.onPtScale(`badge-text-${n.id}`,o=>(z(o),f)),D.push(W(()=>Z.mapSettings,({roll:o})=>{ct(o??0)&&t.onRoll(`badge-roll-${n.id}`,a=>{const m=at(a,E);m!==void 0&&(E+=m,T=(T+(m>0?1:3))%4,L(),k(t.scale),z(t.scale),t.update(h,f))})},{fireImmediately:!0})),{bg:h,text:f,dispose:()=>{D.forEach(o=>o()),D.length=0}}}export function configBoothsBadges(t,n,s){const e=[],i=[],u=[];for(const r of s)if(r instanceof dt&&gt({hideExhibitorBoothNumber:N.hideExhibitorBoothNumber,hideBoothBadges:N.hideBoothBadges,hideLogoInBooth:Z.hideLogoInBooth,noLabels:r.noLabels,hasExhibitors:r.exhibitors.length>0,hasLogoInBooth:r.exhibitors.some(c=>c.logoInBooth&&c.logo)})){const{bg:c,text:d,dispose:B}=createBoothBadge(t,r);e.push(c),i.push(d),u.push(B)}const l=()=>{u.forEach(r=>r()),u.length=0};return e.length?{layer:{name:`${n}${$}badges`,children:[{name:`${n}${$}badgeBgs`,children:e},{name:`${n}${$}badgeTexts`,children:i}]},cleanup:l}:{layer:null,cleanup:l}}