@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 7.54 kB
JavaScript
import{find as t}from"../../core/iteratorUtils.js";import{someMap as e}from"../../core/MapUtils.js";import{clamp as o}from"../../core/mathUtils.js";import{isSome as r,isNone as i,applySome as a,unwrap as n}from"../../core/maybe.js";import{createScreenPoint as s}from"../../core/screenUtils.js";import{areToolManipulatorsEditable as p}from"./interactiveToolUtils.js";import{createScreenPointFromEvent as l}from"../support/screenUtils.js";class c{constructor(){this._pointerLocations=new Map,this._hoveredManipulators=new Map,this._grabbedManipulators=new Map,this._draggedManipulators=new Map,this._stopDrag=!1,this._revertToNullActiveTool=!1,this._cursor=null}get cursor(){return this._cursor}handleInputEvent(t,e){const s=()=>t.stopPropagation();switch(t.type){case"pointer-move":u(t.pointerType)&&this._pointerLocations.set(t.pointerId,{x:t.x,y:t.y,pointerType:t.pointerType});break;case"drag":this._grabbedManipulators.size>0&&(this._stopDrag=!0),this._stopDrag&&(s(),"end"===t.action&&(this._stopDrag=!1));break;case"pointer-down":{if(!h(t))break;const o=l(t),a=this._intersect(o,t.pointerType,e.forEachTool);if(i(a))break;const n=a.manipulator,p=a.tool;!(r(n)&&r(p)&&n.interactive)||n.grabbable&&n.grabbableForEvent(t)||!n.grabbing||n.dragging||this._ungrabManipulatorBeforeDragging(n,t,e),r(n)&&r(p)&&n.interactive&&n.grabbable&&n.grabbableForEvent(t)&&!n.grabbing&&(this._grabbedManipulators.set(t.pointerId,{manipulator:n,tool:p,start:o,pointerType:t.pointerType}),1===this._grabbedManipulators.size&&i(e.activeTool)&&(this._revertToNullActiveTool=!0,e.setActiveTool(a.tool)),n.grabbing=!0,n.events.emit("grab-changed",{action:"start",pointerType:t.pointerType,screenPoint:o}),s());break}case"pointer-up":this._draggedManipulators.has(t.pointerId)||this._handlePointerEnd(t,e);break;case"pointer-drag":{if(!h(t))break;const r=this._grabbedManipulators.get(t.pointerId),p=a(r,(({manipulator:t})=>t)),c=a(r,(({tool:t})=>t));if(i(p)||i(c))break;const u=l(t);u.x=o(u.x,0,e.view.width),u.y=o(u.y,0,e.view.height);const d=n(r).start,g=this._draggedManipulators.get(t.pointerId);switch(t.action){case"start":case"update":"update"!==t.action&&1!==this._grabbedManipulators.size||(p.dragging=!0,g?p.events.emit("drag",{action:"update",start:d,screenPoint:u}):p.events.emit("drag",{action:"start",start:d,screenPoint:u,pointerType:t.pointerType}),this._draggedManipulators.set(t.pointerId,{tool:c,manipulator:p,start:d}));break;case"end":p.dragging=!1,g&&p.events.emit("drag",{action:"end",start:d,screenPoint:u}),this._draggedManipulators.delete(t.pointerId),this._handlePointerEnd(t,e)}s();break}case"immediate-click":{const o=l(t),a=this._intersect(o,t.pointerType,e.forEachTool);if(d(t)||e.forEachTool((t=>{if((!r(a)||a.tool!==t||t.automaticManipulatorSelection)&&t.manipulators){let e=!1;t.manipulators.forEach((({manipulator:t})=>{t.selected&&(t.selected=!1,e=!0)})),e&&t.onManipulatorSelectionChanged&&t.onManipulatorSelectionChanged()}})),i(a))break;const{manipulator:n,tool:p}=a;if(!n.interactive)break;n.selectable&&p.automaticManipulatorSelection&&(n.selected=!n.selected,p.onManipulatorSelectionChanged&&p.onManipulatorSelectionChanged());const c=t.native.shiftKey;n.events.emit("immediate-click",{screenPoint:o,button:t.button,pointerType:t.pointerType,shiftKey:c,stopPropagation:s});break}case"click":{const o=l(t),a=this._intersect(o,t.pointerType,e.forEachTool),n=r(a)?a.manipulator:null;if(i(n)||!n.interactive)break;const p=t.native.shiftKey;n.events.emit(t.type,{screenPoint:o,button:t.button,pointerType:t.pointerType,shiftKey:p}),s();break}case"double-click":{const o=l(t),a=this._intersect(o,t.pointerType,e.forEachTool),n=r(a)?a.manipulator:null;if(i(n)||!n.interactive)break;const p=t.native.shiftKey;n.events.emit("double-click",{screenPoint:o,button:t.button,pointerType:t.pointerType,shiftKey:p,stopPropagation:s});break}case"immediate-double-click":{const o=l(t),a=this._intersect(o,t.pointerType,e.forEachTool),n=r(a)?a.manipulator:null;if(i(n)||!n.interactive)break;const p=t.native.shiftKey;n.events.emit("immediate-double-click",{screenPoint:o,button:t.button,pointerType:t.pointerType,shiftKey:p,stopPropagation:s});break}}this._onFocusChange(e.forEachTool)}_ungrabManipulatorBeforeDragging(t,e,o){t.grabbing=!1,t.events.emit("grab-changed",{action:"end",pointerType:e.pointerType,screenPoint:l(e)}),this._grabbedManipulators.forEach((({manipulator:e},o)=>{e===t&&this._grabbedManipulators.delete(o)})),this._afterManipulatorUngrab(o.setActiveTool)}_handlePointerEnd(t,e){const o=a(this._grabbedManipulators.get(t.pointerId),(({manipulator:t})=>t));i(o)||o.grabbing&&(o.grabbing=!1,o.events.emit("grab-changed",{action:"end",pointerType:t.pointerType,screenPoint:l(t)}),this._grabbedManipulators.delete(t.pointerId),this._afterManipulatorUngrab(e.setActiveTool))}_cursorFromMap(t){let o=null;return e(t,(({manipulator:t})=>!(i(t)||!t.interactive)&&(t.grabbing&&t.grabCursor?(o=t.grabCursor,!0):!!t.cursor&&(o=t.cursor,!0)))),o}_onFocusChange(t){this._updateCursor(),this._updateFocusedManipulatorTools(t)}_updateCursor(){this._grabbedManipulators.size>0?this._cursor=this._cursorFromMap(this._grabbedManipulators)||"grabbing":this._hoveredManipulators.size>0?this._cursor=this._cursorFromMap(this._hoveredManipulators)||"pointer":this._cursor=null}_updateFocusedManipulatorTools(e){const o=new Set,i=new Set;this._grabbedManipulators.forEach((({tool:t})=>{o.add(t)})),this._hoveredManipulators.forEach((({tool:t})=>{i.add(t)})),e((e=>{e.hasGrabbedManipulators=o.has(e),e.hasHoveredManipulators=i.has(e);const a=this._grabbedManipulators.values(),n=t(a,(({tool:t})=>t===e));e.firstGrabbedManipulator=r(n)?n.manipulator:null}))}clearPointers(t,{forEachTool:e,setActiveTool:o},r=!0,a){const n=(e,o)=>e===t&&(i(a)||a===o);this._grabbedManipulators.forEach((({tool:t,manipulator:e,pointerType:o},r)=>{n(t,e)&&(this._grabbedManipulators.delete(r),e.grabbing=!1,e.events.emit("grab-changed",{action:"end",screenPoint:null,pointerType:o}))})),this._draggedManipulators.forEach((({tool:t,manipulator:e},o)=>{n(t,e)&&(this._draggedManipulators.delete(o),e.dragging=!1,e.events.emit("drag",{action:"cancel"}))})),r&&this._hoveredManipulators.forEach((({tool:t,manipulator:e},o)=>{n(t,e)&&(this._hoveredManipulators.delete(o),e.hovering=!1)})),this._afterManipulatorUngrab(o),this._onFocusChange(e)}_intersect(t,e,o){let r=null;return o((o=>{if(null==o.manipulators||!p(o))return!1;const a=o.manipulators.intersect(t,e);return!i(a)&&(r={tool:o,manipulator:a},!0)})),r}updateHoveredStateFromKnownPointers(t){this._pointerLocations.forEach(((e,o)=>{this._updateHoveredStateForPointerAtScreenPosition(s(e.x,e.y),o,e.pointerType,t)}))}handleHoverEvent(t,e){"pointer-up"!==t.type&&"immediate-click"!==t.type&&"pointer-move"!==t.type||!u(t.pointerType)||this._updateHoveredStateForPointerAtScreenPosition(l(t),t.pointerId,t.pointerType,e)}_updateHoveredStateForPointerAtScreenPosition(t,e,o,i){let n=this._intersect(t,o,i);const s=a(this._hoveredManipulators.get(e),(({manipulator:t})=>t));r(n)&&!n.manipulator.interactive&&(n=null),r(n)&&s===n.manipulator||(r(s)&&(s.hovering=!1),r(n)?(n.manipulator.hovering=!0,this._hoveredManipulators.set(e,n)):this._hoveredManipulators.delete(e),this._onFocusChange(i))}_afterManipulatorUngrab(t){0===this._grabbedManipulators.size&&this._revertToNullActiveTool&&(t(null),this._revertToNullActiveTool=!1)}}function u(t){return"mouse"===t}function h(t){return"mouse"!==t.pointerType||0===t.button}function d(t){return!!t.native.shiftKey}export{c as ToolViewManagerManipulatorState};