UNPKG

coherent-gameface-interaction-manager

Version:
7 lines (6 loc) 11.9 kB
(function(g,f){typeof exports=="object"&&typeof module<"u"?module.exports=f():typeof define=="function"&&define.amd?define(f):(g=typeof globalThis<"u"?globalThis:g||self,g.draggable=f())})(this,(function(){"use strict";class g{constructor(e){this.options=e,this.draggableElements=[],this.draggedElement=null,this.enabled=!1,this.offset={x:0,y:0}}get draggedItemIndex(){return[...this.draggableElements].indexOf(this.draggedElement)}get bodyScrollOffset(){return{x:document.body.scrollLeft,y:document.body.scrollTop}}setPointerOffset(e,t,n){const{x:i,y:r}=n.getBoundingClientRect();this.offset={x:e-i,y:t-r}}}function f(c){return c*180/Math.PI}function b(c,e,t){return Math.min(Math.max(c,e),t)}function _(){return(Math.random()+1).toString(36).substring(7)}function I(c,e,t,n){const i=c-t,r=e-n;return Math.hypot(i,r)}function L(c,e,t,n){return{x:(c+t)/2,y:(e+n)/2}}class M{constructor(){this.actions=[],this.keyboardFunctions=[],this.gamepadFunctions=[]}init(){window._IM||(window._IM=new M)}getKeys(e){return _IM.keyboardFunctions.filter(t=>t.keys.every(n=>e.includes(n)))}getKeysIndex(e){return _IM.keyboardFunctions.findIndex(t=>t.keys.every(n=>e.includes(n)))}getGamepadAction({actions:e,type:t}){return _IM.gamepadFunctions.find(n=>n.actions.every(i=>e.includes(i))&&n.type===t&&n.actions.length===e.length)}getGamepadActions(e,t=!0){return _IM.gamepadFunctions.filter(n=>n.actions.every(i=>e.includes(i))&&(t?n.actions.length===e.length:!0))}getGamepadActionIndex(e){return _IM.gamepadFunctions.findIndex(t=>t.actions.every(n=>e.includes(n)))}getAction(e){return _IM.actions.find(t=>t.name===e)}getActionIndex(e){return _IM.actions.findIndex(t=>t.name===e)}hasDuplicateCallback(e,t){return e.callbacks.some(n=>n===t)}addCallbackToEntry(e,t,n){if(this.hasDuplicateCallback(e,t)){const i=typeof t=="string"?"action":"function",r=typeof t=="string"?t:"(anonymous function)";return console.error(`Duplicate callback detected! ${n.identifier} Type: '${n.type}' Callback: ${r} This ${i} is already registered for this combination and type. To update it, first remove with the .off() method.`)}return e.callbacks.push(t)}removeKeyboardFunction(e){const t=_IM.keyboardFunctions.indexOf(e);t!==-1&&_IM.keyboardFunctions.splice(t,1)}removeGamepadFunction(e){const t=_IM.gamepadFunctions.indexOf(e);t!==-1&&_IM.gamepadFunctions.splice(t,1)}}const v=new M;class x{register(e,t){if(v.getAction(e))return console.error(`The following action "${e}" is already registered!`);_IM.actions.push({name:e,callback:t})}remove(e){const t=v.getActionIndex(e);if(t===-1)return console.error(`${e} is not a registered action!`);_IM.actions.splice(t,1)}execute(e,t){const n=v.getAction(e);if(!n)return console.error(`${e} is not a registered action!`);n.callback(t)}}const E=new x;function F(c,e){if(e<0&&c>-200&&c<200)return"top";if(e>0&&c>-200&&c<200)return"bottom";if(c<0&&e>-200&&e<200)return"left";if(c>0&&e>-200&&e<200)return"right";if(c<=-200&&e<=-200)return"top-left";if(c>=200&&e<=-200)return"top-right";if(c<=-200&&e>=200)return"bottom-left";if(c>=200&&e>=200)return"bottom-right"}function h(c){return c instanceof HTMLElement?c:document.querySelector(c)}const T=2;class D{constructor(){this.activeTouches=new Map}hold(e){if(!e)return console.error("Options not provided for hold!");let t;const n=h(e.element);if(!n)return console.error("Element not found!");const i=({touches:o})=>{this.activeTouches.set(o[0].identifier,o[0]),t=setTimeout(()=>{e.callback&&e.callback()},e.time||1e3)},r=({touches:o})=>{this.activeTouches.delete(o[0].identifier),clearTimeout(t)};return n.addEventListener("touchstart",i),n.addEventListener("touchend",r),{remove(){n.removeEventListener("touchstart",i),n.removeEventListener("touchend",r)}}}tap(e){if(!e)return console.error("Options not provided for tap!");let t,n,i=!0,r=e.tapsNumber||1;const o=h(e.element);if(!o)return console.error("Element not found!");const l=({touches:s})=>{this.activeTouches.set(s[0].identifier,s[0]),clearTimeout(n),i=!0,t=setTimeout(()=>{i=!1},e.tapTime||200)},a=({touches:s})=>{this.activeTouches.delete(s[0].identifier),clearTimeout(t),i&&(r--,n=setTimeout(()=>{r=e.tapsNumber||1,clearTimeout(n)},e.betweenTapsTime||500),!(r!==0||!e.callback)&&(e.callback(),i=!0,clearTimeout(t),r=e.tapsNumber||1))};return o.addEventListener("touchstart",l),o.addEventListener("touchend",a),{remove(){o.removeEventListener("touchstart",l),o.removeEventListener("touchend",a)}}}drag(e){if(!e)return console.error("Options not provided for drag!");const t=h(e.element);if(!t)return console.error("Element not found!");const n=({touches:o,target:l,currentTarget:a})=>{this.activeTouches.set(o[0].identifier,o[0]),document.addEventListener("touchmove",i),document.addEventListener("touchend",r),e.onDragStart&&e.onDragStart({x:o[0].clientX,y:o[0].clientY,target:l,currentTarget:a})},i=({touches:o})=>{this.activeTouches.has(o[0].identifier)&&e.onDrag&&e.onDrag({x:o[0].clientX,y:o[0].clientY})},r=({touches:o})=>{this.activeTouches.delete(o[0].identifier),document.removeEventListener("touchmove",i),document.removeEventListener("touchend",r),e.onDragEnd&&e.onDragEnd({x:o[0].clientX,y:o[0].clientY})};return t.addEventListener("touchstart",n),{remove(){t.removeEventListener("touchstart",n)}}}swipe(e){if(!e)return console.error("Options not provided for swipe!");let t,n,i,r=!0;const o=100;e.touchNumber||=1;const l=h(e.element);if(!l)return console.error("Element not found!");const a=({touches:u})=>{this.activeTouches.set(u[0].identifier,u[0]),this.activeTouches.size>e.touchNumber&&(document.removeEventListener("touchmove",s),document.removeEventListener("touchend",d)),this.activeTouches.size===e.touchNumber&&(t=setTimeout(()=>{r=!1,clearTimeout(t),t=null},1e3),document.addEventListener("touchmove",s),document.addEventListener("touchend",d))},s=({touches:u})=>{if(!this.activeTouches.has(u[0].identifier))return;const p=this.activeTouches.get(u[0].identifier);if(!p)return;const{clientX:y,clientY:S}=p,P=u[0].clientX-y,k=u[0].clientY-S;n=F(P,k),i=I(y,S,u[0].clientX,u[0].clientY)},d=({touches:u})=>{this.activeTouches.delete(u[0].identifier),this.activeTouches.size===0&&(document.removeEventListener("touchmove",s),document.removeEventListener("touchend",d),m()&&e.callback(n),clearTimeout(t),r=!0,t=null)},m=()=>r&&n&&i&&i>o;return l.addEventListener("touchstart",a),{remove(){l.removeEventListener("touchstart",a)}}}pinch(e){if(!e)return console.error("Options not provided for pinch!");let t;const n=40,i=h(e.element);if(!i)return console.error("Element not found!");const r=({touches:a})=>{if(this.activeTouches.set(a[0].identifier,a[0]),this.activeTouches.size<T)return;const s=this.activeTouches.get(0),d=this.activeTouches.get(1);!s||!d||(document.addEventListener("touchmove",o),document.addEventListener("touchend",l),t=I(s.clientX,s.clientY,d.clientX,d.clientY))},o=({touches:a})=>{if(this.activeTouches.size!==T)return;this.activeTouches.set(a[0].identifier,a[0]);const s=this.activeTouches.get(0),d=this.activeTouches.get(1);if(!s||!d)return;const m=I(s.clientX,s.clientY,d.clientX,d.clientY),u=Math.sign(m-t)*n;t=m;const p=L(s.clientX,s.clientY,d.clientX,d.clientY);e.callback&&e.callback({pinchDelta:u,midpoint:p})},l=({touches:a})=>{this.activeTouches.delete(a[0].identifier),this.activeTouches.size===0&&(document.removeEventListener("touchmove",o),document.removeEventListener("touchend",l))};return i.addEventListener("touchstart",r),{remove(){i.removeEventListener("touchstart",r)}}}rotate(e){if(!e)return console.error("Options not provided for rotate!");let t=0,n=0;const i=h(e.element);if(!i)return console.error("Element not found!");const r=({touches:s})=>{this.activeTouches.size<T||(this.activeTouches.set(s[0].identifier,s[0]),t=a()-n,e.callback&&e.callback(t))},o=({touches:s})=>{this.activeTouches.set(s[0].identifier,s[0]),this.activeTouches.size===T&&(n=a()-t,document.addEventListener("touchmove",r),document.addEventListener("touchend",l))},l=({touches:s})=>{this.activeTouches.delete(s[0].identifier),document.removeEventListener("touchmove",r),document.removeEventListener("touchend",l)};i.addEventListener("touchstart",o);const a=()=>{const m=this.activeTouches.get(0).clientY-this.activeTouches.get(1).clientY,u=this.activeTouches.get(0).clientX-this.activeTouches.get(1).clientX;return(f(Math.atan2(m,u))+360+90)%360};return{remove(){i.removeEventListener("touchstart",o)}}}}const O=new D,N=["x","y"];class w extends g{constructor(e){super(e),this.actionName=`drag-around-${_()}`,this.restrict={top:0,left:0,right:1/0,bottom:1/0},this.touchEvents=[],this._touchEnabled=!1,this.onMouseDown=this.onMouseDown.bind(this),this.onMouseMove=this.onMouseMove.bind(this),this.onMouseUp=this.onMouseUp.bind(this),this.init()}set touchEnabled(e){this._touchEnabled!==e&&(this._touchEnabled=e,this._touchEnabled?this.addTouchEvents():this.removeTouchEvents())}init(){if(!this.enabled){if(this.draggableElements=document.querySelectorAll(this.options.element),this.draggableElements.length===0)return console.error(`${this.options.element} is not a valid element selector.`);this.draggableElements.forEach(e=>e.addEventListener("mousedown",this.onMouseDown)),this.registerDragActions(),this.enabled=!0}}deinit(){this.enabled&&(this.enabled=!1,this.draggableElements.forEach(e=>e.removeEventListener("mousedown",this.onMouseDown)),this.removeDragActions())}onMouseDown(e){this.draggedElement=e.currentTarget,this.draggedElement.style.position="absolute",this.elementRect=this.draggedElement.getBoundingClientRect(),this.setRestriction(),this.setPointerOffset(e.clientX,e.clientY,this.draggedElement),E.execute(this.actionName,{x:e.clientX+this.bodyScrollOffset.x-this.offset.x,y:e.clientY+this.bodyScrollOffset.y-this.offset.y,index:this.draggedItemIndex}),this.options.dragClass&&this.draggedElement.classList.add(this.options.dragClass),this.options.onDragStart&&this.options.onDragStart(this.draggedElement),document.addEventListener("mousemove",this.onMouseMove),document.addEventListener("mouseup",this.onMouseUp)}onMouseMove(e){E.execute(this.actionName,{x:e.clientX+this.bodyScrollOffset.x-this.offset.x,y:e.clientY+this.bodyScrollOffset.y-this.offset.y,index:this.draggedItemIndex})}onMouseUp(){document.removeEventListener("mousemove",this.onMouseMove),document.removeEventListener("mouseup",this.onMouseUp),this.options.onDragEnd&&this.options.onDragEnd(this.draggedElement),this.options.dragClass&&this.draggedElement.classList.remove(this.options.dragClass),this.draggedElement=null}registerDragActions(){E.register(this.actionName,({x:e,y:t,index:n})=>{if(!this.draggableElements[n])return console.error(`There is no draggable element at index ${n}`);this.options.lockAxis&&N.includes(this.options.lockAxis)&&(e=this.options.lockAxis==="y"?this.elementRect.x:e,t=this.options.lockAxis==="x"?this.elementRect.y:t),this.draggableElements[n].style.left=`${b(e,this.restrict.left,this.restrict.right)}px`,this.draggableElements[n].style.top=`${b(t,this.restrict.top,this.restrict.bottom)}px`,this.options.onDragMove&&this.options.onDragMove({x:e,y:t})})}removeDragActions(){E.remove(this.actionName)}addTouchEvents(){this.draggableElements.forEach(e=>{this.touchEvents.push(O.drag({element:e,onDragStart:t=>{this.onMouseDown({currentTarget:t.currentTarget,clientX:t.x,clientY:t.y})},onDrag:({x:t,y:n})=>{this.onMouseMove({clientX:t,clientY:n})},onDragEnd:()=>{this.onMouseUp()}}))})}removeTouchEvents(){this.touchEvents.forEach(e=>e?.remove()),this.touchEvents=[]}setRestriction(){if(!this.options.restrictTo)return;const e=document.querySelector(this.options.restrictTo);if(!e)return console.error(`The element ${this.options.restrictTo} you trying to restrict dragging to is not a valid element`);const{x:t,y:n,height:i,width:r}=e.getBoundingClientRect();this.restrict={top:n,left:t,right:r+t-this.elementRect.width,bottom:i+n-this.elementRect.height}}}return v.init(),w})); //# sourceMappingURL=draggable.min.js.map