UNPKG

@expofp/floorplan

Version:

Interactive floor plan library for expos and events

2 lines (1 loc) 1.66 kB
import{Box as d,Polygon as v,Rect as z}from"@expofp/geometry";import{reaction as I}from"mobx";import{getTrianglesFromFpPaths as X}from"../../data/svg";import{boothStore as b,layersStore as x}from"../../store";import{LayersMode as A}from"../../store/LayerStore";import F from"../../tools/logger";let y=[],u=new Map,g=null,w=[];function S(){const l=b.booths.filter(t=>t.visible&&t.rect&&t.rect.size.width>0&&t.rect.size.height>0);if(y=[],g=null,u=new Map,w=l.filter(t=>t.paths),!l.length)return;const c=[];let s=1/0,n=1/0,f=-1/0,e=-1/0;for(const t of l){const a=t.rotate?new z(t.rect.center,t.rect.size,t.rotate):null,o=a?a.bounds:t.rect;c.push({booth:t,shape:a??t.rect,bounds:o}),o.min.x<s&&(s=o.min.x),o.min.y<n&&(n=o.min.y),o.max.x>f&&(f=o.max.x),o.max.y>e&&(e=o.max.y)}const i=d.fromMinMax({x:s,y:n},{x:f,y:e}),r=2,m=Math.ceil(i.size.width/r),h=Math.ceil(i.size.height/r);for(let t=0;t<r;t++)for(let a=0;a<r;a++){const o=i.min.x+t*m,M=i.min.y+a*h,p=new d(o,M,m,h);y.push(p),u.set(p,c.filter(P=>p.intersects(P.bounds)))}F.log("hover segmentToHitAreas",u)}I(()=>[b.booths,x.loaded,x.visible],()=>S());function B(l,c){const s={x:l,y:c};let n;g&&g.containsPoint(s)?n=g:n=y.find(e=>e.containsPoint(s)),n&&(g=n);const f=n?u.get(n):void 0;if(f){const e=f.filter(i=>i.booth.visible&&i.shape.containsPoint(s));if(e.length)return(e.length>1?e.sort((r,m)=>r.booth.rect.area-m.booth.rect.area)[0]:e[0]).booth}for(const e of w)if(e.visible)for(const i of e.paths)for(const r of X(i.index,x.mode!==A.Default?e.layer.name:"")){const m=r.map(h=>({x:h[0],y:h[1]}));if(v.fromConvexRing(m).containsPoint(s))return e}return null}export default function C(l,c){return B(l,c)}