UNPKG

@expofp/floorplan

Version:

Interactive floor plan library for expos and events

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