UNPKG

@trap_stevo/legendarybuilderproreact-ui

Version:

The legendary UI & utility API that makes your application a legendary application. ~ Created by Steven Compton

457 lines 18.4 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function getTouchDistance(t1, t2) { var dx = t2.clientX - t1.clientX; var dy = t2.clientY - t1.clientY; return Math.sqrt(dx * dx + dy * dy); } ; function getTouchAngle(t1, t2) { var dx = t2.clientX - t1.clientX; var dy = t2.clientY - t1.clientY; return Math.atan2(dy, dx) * 180 / Math.PI; } ; function getMidpoint(t1, t2) { return { x: (t1.clientX + t2.clientX) / 2, y: (t1.clientY + t2.clientY) / 2 }; } ; function matchesKeyCombo(set, comboArray) { return comboArray.every(function (k) { return set.has(k); }); } ; export function GetHUDInteractionHandlers() { var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var _config$gestureSettin = config.gestureSettings, gestureSettings = _config$gestureSettin === void 0 ? {} : _config$gestureSettin, onTrackpadGesture = config.onTrackpadGesture, onGestureSequence = config.onGestureSequence, onResumeFromIdle = config.onResumeFromIdle, onPinchRotate = config.onPinchRotate, onDoubleClick = config.onDoubleClick, onTripleClick = config.onTripleClick, onRapidClicks = config.onRapidClicks, onIdleWithin = config.onIdleWithin, onRightClick = config.onRightClick, onSwipeRight = config.onSwipeRight, onSwipeLeft = config.onSwipeLeft, onSwipeDown = config.onSwipeDown, onLongHover = config.onLongHover, onDragStart = config.onDragStart, onHoverOut = config.onHoverOut, onDragMove = config.onDragMove, onKeyCombo = config.onKeyCombo, onTapHold = config.onTapHold, onDragEnd = config.onDragEnd, onHoverIn = config.onHoverIn, onSwipeUp = config.onSwipeUp, onRotate = config.onRotate, onEscape = config.onEscape, onPinch = config.onPinch, _onWheel = config.onWheel, onSwipe = config.onSwipe, _onClick = config.onClick, onFlick = config.onFlick, onThrow = config.onThrow, onHold = config.onHold, onPan = config.onPan, onTap = config.onTap; var _gestureSettings$long = gestureSettings.longPressDelay, longPressDelay = _gestureSettings$long === void 0 ? 500 : _gestureSettings$long, _gestureSettings$doub = gestureSettings.doubleClickDelay, doubleClickDelay = _gestureSettings$doub === void 0 ? 250 : _gestureSettings$doub, _gestureSettings$long2 = gestureSettings.longHoverDelay, longHoverDelay = _gestureSettings$long2 === void 0 ? 1000 : _gestureSettings$long2, _gestureSettings$tapH = gestureSettings.tapHoldDelay, tapHoldDelay = _gestureSettings$tapH === void 0 ? 500 : _gestureSettings$tapH, _gestureSettings$pinc = gestureSettings.pinchThreshold, pinchThreshold = _gestureSettings$pinc === void 0 ? 0.05 : _gestureSettings$pinc, _gestureSettings$rota = gestureSettings.rotateThreshold, rotateThreshold = _gestureSettings$rota === void 0 ? 5 : _gestureSettings$rota, _gestureSettings$panM = gestureSettings.panMinDistance, panMinDistance = _gestureSettings$panM === void 0 ? 2 : _gestureSettings$panM, _gestureSettings$flic = gestureSettings.flickVelocityThreshold, flickVelocityThreshold = _gestureSettings$flic === void 0 ? 0.75 : _gestureSettings$flic, _gestureSettings$thro = gestureSettings.throwVelocityThreshold, throwVelocityThreshold = _gestureSettings$thro === void 0 ? 1.25 : _gestureSettings$thro, _gestureSettings$velo = gestureSettings.velocityThreshold, velocityThreshold = _gestureSettings$velo === void 0 ? 0.25 : _gestureSettings$velo, _gestureSettings$swip = gestureSettings.swipe, _gestureSettings$swip2 = _gestureSettings$swip === void 0 ? {} : _gestureSettings$swip, _gestureSettings$swip3 = _gestureSettings$swip2.minSwipeDistance, minSwipeDistance = _gestureSettings$swip3 === void 0 ? 25 : _gestureSettings$swip3, _gestureSettings$swip4 = _gestureSettings$swip2.angleTolerance, angleTolerance = _gestureSettings$swip4 === void 0 ? 45 : _gestureSettings$swip4, _gestureSettings$swip5 = _gestureSettings$swip2.threshold, threshold = _gestureSettings$swip5 === void 0 ? 30 : _gestureSettings$swip5, _gestureSettings$drag = gestureSettings.drag, _gestureSettings$drag2 = _gestureSettings$drag === void 0 ? {} : _gestureSettings$drag, _gestureSettings$drag3 = _gestureSettings$drag2.minDistance, minDistance = _gestureSettings$drag3 === void 0 ? 3 : _gestureSettings$drag3, _gestureSettings$rapi = gestureSettings.rapidClickWindow, rapidClickWindow = _gestureSettings$rapi === void 0 ? 1000 : _gestureSettings$rapi, _gestureSettings$rapi2 = gestureSettings.rapidClickCount, rapidClickCount = _gestureSettings$rapi2 === void 0 ? 5 : _gestureSettings$rapi2, _gestureSettings$idle = gestureSettings.idleTimeout, idleTimeout = _gestureSettings$idle === void 0 ? 30000 : _gestureSettings$idle, _gestureSettings$gest = gestureSettings.gestureSequence, gestureSequence = _gestureSettings$gest === void 0 ? [] : _gestureSettings$gest, _gestureSettings$gest2 = gestureSettings.gestureSequenceTime, gestureSequenceTime = _gestureSettings$gest2 === void 0 ? 2000 : _gestureSettings$gest2, _gestureSettings$keyC = gestureSettings.keyCombos, keyCombos = _gestureSettings$keyC === void 0 ? [] : _gestureSettings$keyC, _gestureSettings$keyC2 = gestureSettings.keyComboMap, keyComboMap = _gestureSettings$keyC2 === void 0 ? {} : _gestureSettings$keyC2; var state = { gestureStart: null, lastPos: null, isDragging: false, hasMoved: false, keysPressed: new Set(), sequenceTimer: null, sequenceBuffer: [], rapidClicks: [], clickTimer: null, hoverTimer: null, holdTimer: null, idleTimer: null, tapTimer: null, clickCount: 0, idle: false, touches: [], clicks: [] }; var now = function now() { return performance.now(); }; var fireGesture = function fireGesture(name) { state.sequenceBuffer.push(name); if (state.sequenceTimer) { clearTimeout(state.sequenceTimer); } state.sequenceTimer = setTimeout(function () { return state.sequenceBuffer = []; }, gestureSequenceTime); if (state.sequenceBuffer.join(",") === gestureSequence.join(",")) { onGestureSequence === null || onGestureSequence === void 0 || onGestureSequence(gestureSequence); state.sequenceBuffer = []; } }; var resetIdle = function resetIdle() { if (state.idle) { onResumeFromIdle === null || onResumeFromIdle === void 0 || onResumeFromIdle(); state.idle = false; } if (state.idleTimer) { clearTimeout(state.idleTimer); } state.idleTimer = setTimeout(function () { state.idle = true; onIdleWithin === null || onIdleWithin === void 0 || onIdleWithin(); }, idleTimeout); }; var getDirection = function getDirection(dx, dy) { var angle = Math.atan2(dy, dx) * (180 / Math.PI); var normAngle = (angle + 360) % 360; if (Math.abs(dx) < minSwipeDistance && Math.abs(dy) < minSwipeDistance) { return null; } if (normAngle >= 315 || normAngle < 45) { return "right"; } if (normAngle >= 45 && normAngle < 135) { return "down"; } if (normAngle >= 135 && normAngle < 225) { return "left"; } if (normAngle >= 225 && normAngle < 315) { return "up"; } return null; }; var fireSwipe = function fireSwipe(dir, e) { if (!dir) { return; } onSwipe === null || onSwipe === void 0 || onSwipe(e); if (dir === "right") { onSwipeRight === null || onSwipeRight === void 0 || onSwipeRight(e); } if (dir === "left") { onSwipeLeft === null || onSwipeLeft === void 0 || onSwipeLeft(e); } if (dir === "down") { onSwipeDown === null || onSwipeDown === void 0 || onSwipeDown(e); } if (dir === "up") { onSwipeUp === null || onSwipeUp === void 0 || onSwipeUp(e); } fireGesture("swipe" + dir.charAt(0).toUpperCase() + dir.slice(1)); }; var handlers = { onClick: function onClick(e) { if (state.hasMoved) { return; } resetIdle(); var time = Date.now(); state.rapidClicks.push(time); state.rapidClicks = state.rapidClicks.filter(function (t) { return time - t <= rapidClickWindow; }); if (state.rapidClicks.length > 1) { onRapidClicks === null || onRapidClicks === void 0 || onRapidClicks(state.rapidClicks.length); } state.clickCount++; if (state.clickTimer) { clearTimeout(state.clickTimer); } state.clickTimer = setTimeout(function () { if (state.clickCount === 1) { _onClick === null || _onClick === void 0 || _onClick(e); } else if (state.clickCount === 2) { onDoubleClick === null || onDoubleClick === void 0 || onDoubleClick(e); } else { onTripleClick === null || onTripleClick === void 0 || onTripleClick(e); } state.clickCount = 0; }, doubleClickDelay); }, onMouseDown: function onMouseDown(e) { state.isDragging = true; state.hasMoved = false; var t = Date.now(); state.gestureStart = { x: e.clientX, y: e.clientY, time: t }; state.lastPos = { x: e.clientX, y: e.clientY }; state.holdTimer = setTimeout(function () { if (!state.hasMoved) { onHold === null || onHold === void 0 || onHold(e); } state.holdTimer = null; }, longPressDelay); onDragStart === null || onDragStart === void 0 || onDragStart(e); }, onMouseMove: function onMouseMove(e) { if (!state.isDragging || !state.gestureStart) { return; } var dx = e.clientX - state.gestureStart.x; var dy = e.clientY - state.gestureStart.y; if (Math.abs(dx) > 3 || Math.abs(dy) > 3) { state.hasMoved = true; } if (state.holdTimer && state.hasMoved) { clearTimeout(state.holdTimer); state.holdTimer = null; } onDragMove === null || onDragMove === void 0 || onDragMove(e); state.lastPos = { x: e.clientX, y: e.clientY }; }, onMouseUp: function onMouseUp(e) { e.preventDefault(); e.stopPropagation(); var t = Date.now(); if (state.holdTimer) { clearTimeout(state.holdTimer); } if (state.isDragging && state.gestureStart && state.lastPos) { var dx = state.lastPos.x - state.gestureStart.x; var dy = state.lastPos.y - state.gestureStart.y; var dt = t - state.gestureStart.time; var velocity = Math.sqrt(dx * dx + dy * dy) / dt; var angle = Math.atan2(dy, dx) * (180 / Math.PI); var normAngle = (angle + 360) % 360; var dir = getDirection(dx, dy); var primaryDelta = Math.max(Math.abs(dx), Math.abs(dy)); if (velocity > velocityThreshold && primaryDelta > threshold) { fireSwipe(dir, e); } } state.isDragging = false; onDragEnd === null || onDragEnd === void 0 || onDragEnd(e); }, onMouseEnter: function onMouseEnter() { onHoverIn === null || onHoverIn === void 0 || onHoverIn(); if (onLongHover) { state.hoverTimer = setTimeout(function () { onLongHover === null || onLongHover === void 0 || onLongHover(); state.hoverTimer = null; }, longHoverDelay); } }, onMouseLeave: function onMouseLeave() { onHoverOut === null || onHoverOut === void 0 || onHoverOut(); if (state.hoverTimer) { clearTimeout(state.hoverTimer); } }, onContextMenu: function onContextMenu(e) { e.preventDefault(); onRightClick === null || onRightClick === void 0 || onRightClick(e); }, onTouchStart: function onTouchStart(e) { if (e.touches.length === 1) { var t = Date.now(); var touch = e.touches[0]; state.isDragging = true; state.hasMoved = false; state.gestureStart = { x: touch.clientX, y: touch.clientY, time: t }; state.lastPos = { x: touch.clientX, y: touch.clientY }; state.holdTimer = setTimeout(function () { if (!state.hasMoved) { onHold === null || onHold === void 0 || onHold(e); } state.holdTimer = null; }, longPressDelay); } if (e.touches.length === 2) { state.touches = [e.touches[0], e.touches[1]]; state.initialDist = getTouchDistance.apply(void 0, _toConsumableArray(state.touches)); state.initialAngle = getTouchAngle.apply(void 0, _toConsumableArray(state.touches)); } }, onTouchMove: function onTouchMove(e) { if (e.touches.length === 1) { var touch = e.touches[0]; var dx = touch.clientX - state.gestureStart.x; var dy = touch.clientY - state.gestureStart.y; if (Math.abs(dx) > 3 || Math.abs(dy) > 3) { state.hasMoved = true; } if (state.holdTimer && state.hasMoved) { clearTimeout(state.holdTimer); state.holdTimer = null; } var deltaX = touch.clientX - state.lastPos.x; var deltaY = touch.clientY - state.lastPos.y; state.lastPos = { x: touch.clientX, y: touch.clientY }; onPan === null || onPan === void 0 || onPan({ dx: deltaX, dy: deltaY, x: touch.clientX, y: touch.clientY }); } if (e.touches.length === 2 && state.touches.length === 2) { var _ref = [e.touches[0], e.touches[1]], t1 = _ref[0], t2 = _ref[1]; var newDist = getTouchDistance(t1, t2); var newAngle = getTouchAngle(t1, t2); var scale = newDist / state.initialDist; var angleDelta = newAngle - state.initialAngle; var center = getMidpoint(t1, t2); if (Math.abs(scale - 1) >= pinchThreshold) onPinch === null || onPinch === void 0 || onPinch({ scale: scale, center: center }); if (Math.abs(angleDelta) >= rotateThreshold) onRotate === null || onRotate === void 0 || onRotate({ angle: angleDelta, center: center }); if (Math.abs(scale - 1) >= pinchThreshold && Math.abs(angleDelta) >= rotateThreshold) { onPinchRotate === null || onPinchRotate === void 0 || onPinchRotate({ scale: scale, angle: angleDelta, center: center }); } } }, onTouchEnd: function onTouchEnd(e) { if (state.holdTimer) { clearTimeout(state.holdTimer); state.holdTimer = null; } var t = Date.now(); if (state.isDragging && state.gestureStart && state.lastPos) { var dx = state.lastPos.x - state.gestureStart.x; var dy = state.lastPos.y - state.gestureStart.y; var dt = t - state.gestureStart.time; var velocity = Math.sqrt(dx * dx + dy * dy) / dt; var dir = getDirection(dx, dy); var primaryDelta = Math.max(Math.abs(dx), Math.abs(dy)); if (velocity > velocityThreshold && primaryDelta > threshold) { fireSwipe(dir, e); } } state.isDragging = false; state.gestureStart = null; state.lastPos = null; state.touches = []; if (e.touches.length === 0 && !state.hasMoved) { onTap === null || onTap === void 0 || onTap(e); fireGesture("tap"); } }, onWheel: function onWheel(e) { _onWheel === null || _onWheel === void 0 || _onWheel(e); onTrackpadGesture === null || onTrackpadGesture === void 0 || onTrackpadGesture({ dx: e.deltaX, dy: e.deltaY, source: "wheel" }); } }; var handleKeyDown = function handleKeyDown(e) { state.keysPressed.add(e.key); var keysArr = Array.from(state.keysPressed); var _iterator = _createForOfIteratorHelper(keyCombos), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var combo = _step.value; var match = combo.every(function (k) { return keysArr.includes(k); }); if (match) { onKeyCombo === null || onKeyCombo === void 0 || onKeyCombo(combo); state.keysPressed.clear(); break; } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } }; var handleKeyUp = function handleKeyUp(e) { state.keysPressed["delete"](e.key); }; if (typeof window !== "undefined") { resetIdle(); window.addEventListener("keydown", handleKeyDown); window.addEventListener("keyup", handleKeyUp); } return handlers; } ;