HumanBreak/src/plugin/use.ts

172 lines
4.8 KiB
TypeScript
Raw Normal View History

2024-04-22 23:08:46 +08:00
import { tip } from './utils';
2022-11-14 17:11:23 +08:00
export default function init() {
2022-11-19 11:30:14 +08:00
return { useDrag, useWheel, useUp, isMobile };
2022-11-14 17:11:23 +08:00
}
2022-11-19 11:30:14 +08:00
type DragFn = (x: number, y: number, e: MouseEvent | TouchEvent) => void;
type DragMap = [
(e: MouseEvent) => void,
(e: TouchEvent) => void,
((e: MouseEvent) => void)?,
((e: TouchEvent) => void)?
];
const dragFnMap = new Map<DragFn, DragMap>();
/**
*
*/
2023-06-16 19:17:59 +08:00
export let isMobile = matchMedia('(max-width: 600px)').matches;
2023-08-06 17:46:29 +08:00
let alerted = false;
2023-06-16 19:17:59 +08:00
window.addEventListener('resize', () => {
requestAnimationFrame(() => {
isMobile = matchMedia('(max-width: 600px)').matches;
2023-08-05 12:12:02 +08:00
checkMobile();
2023-06-16 19:17:59 +08:00
});
});
2023-08-05 12:12:02 +08:00
checkMobile();
function checkMobile() {
2023-08-06 17:46:29 +08:00
if (isMobile && !alerted) {
2024-04-22 23:08:46 +08:00
tip(
'info',
2024-04-21 18:30:19 +08:00
'手机端建议使用新版APP或者自带的浏览器进行游玩并在进入游戏后开启游戏内的全屏设置游玩'
2023-08-08 13:29:22 +08:00
);
2023-08-06 17:46:29 +08:00
alerted = true;
2023-08-05 12:12:02 +08:00
}
}
2022-11-19 11:30:14 +08:00
2022-11-14 17:11:23 +08:00
/**
*
2022-11-30 16:42:44 +08:00
* @param ele
2024-04-21 18:30:19 +08:00
* @param fn x y和鼠标事件或点击事件
2022-11-14 17:11:23 +08:00
* @param ondown
* @param global
*/
export function useDrag(
2022-11-30 16:42:44 +08:00
ele: HTMLElement | HTMLElement[],
2022-11-19 11:30:14 +08:00
fn: DragFn,
ondown?: DragFn,
2022-11-30 16:42:44 +08:00
onup?: (e: MouseEvent | TouchEvent) => void,
2022-11-14 17:11:23 +08:00
global: boolean = false
) {
2023-04-30 22:15:54 +08:00
const touchFn = (e: TouchEvent) => {
fn(e.touches[0].clientX, e.touches[0].clientY, e);
};
2023-08-06 17:46:29 +08:00
const mouseFn = (e: MouseEvent) => {
fn(e.clientX, e.clientY, e);
};
2023-04-30 22:15:54 +08:00
const mouseUp = (e: MouseEvent) => {
const ele = global ? document : e.target;
if (ele) {
(ele as HTMLElement).removeEventListener('mousemove', mouseFn);
}
onup && onup(e);
};
2022-11-30 16:42:44 +08:00
2024-04-21 18:30:19 +08:00
const touchUp = (e: TouchEvent) => {
const ele = global ? document : e.target;
if (ele) {
(ele as HTMLElement).removeEventListener('touchmove', touchFn);
}
onup && onup(e);
};
2022-11-30 16:42:44 +08:00
const md = (e: MouseEvent) => {
2023-04-30 22:15:54 +08:00
const ele = global ? document : e.target;
if (ele) {
(ele as HTMLElement).addEventListener('mousemove', mouseFn);
}
2022-11-14 17:11:23 +08:00
if (ondown) ondown(e.clientX, e.clientY, e);
2022-11-30 16:42:44 +08:00
};
const td = (e: TouchEvent) => {
2023-04-30 22:15:54 +08:00
const ele = global ? document : e.target;
if (ele) {
(ele as HTMLElement).addEventListener('touchmove', touchFn);
}
2022-11-14 17:11:23 +08:00
if (ondown) ondown(e.touches[0].clientX, e.touches[0].clientY, e);
2022-11-30 16:42:44 +08:00
};
if (ele instanceof Array) {
ele.forEach(v => {
v.addEventListener('mousedown', md);
v.addEventListener('touchstart', td);
});
} else {
ele.addEventListener('mousedown', md);
ele.addEventListener('touchstart', td);
}
2022-11-14 17:11:23 +08:00
const target = global ? document : ele;
2022-11-30 16:42:44 +08:00
if (target instanceof Array) {
target.forEach(v => {
v.addEventListener('mouseup', mouseUp);
v.addEventListener('touchend', touchUp);
});
} else {
target.addEventListener('mouseup', mouseUp as EventListener);
target.addEventListener('touchend', touchUp as EventListener);
}
2023-05-02 15:37:38 +08:00
dragFnMap.set(fn, [mouseUp, touchUp]);
2022-11-19 11:30:14 +08:00
}
/**
*
* @param fn
*/
export function cancelGlobalDrag(fn: DragFn): void {
const fns = dragFnMap.get(fn);
dragFnMap.delete(fn);
2022-12-29 00:26:12 +08:00
if (!fns) return;
2022-11-19 11:30:14 +08:00
document.removeEventListener('mouseup', fns[0]);
document.removeEventListener('touchend', fns[1]);
2022-11-14 17:11:23 +08:00
}
/**
*
* @param ele
* @param fn
*/
export function useWheel(
ele: HTMLElement,
fn: (x: number, y: number, z: number, e: WheelEvent) => void
): void {
ele.addEventListener('wheel', e => {
fn(e.deltaX, e.deltaY, e.deltaZ, e);
});
}
2022-11-16 23:01:23 +08:00
/**
2022-11-19 11:30:14 +08:00
*
* @param ele
* @param fn
2022-11-16 23:01:23 +08:00
*/
2022-11-19 11:30:14 +08:00
export function useUp(ele: HTMLElement, fn: DragFn): void {
ele.addEventListener('mouseup', e => {
fn(e.clientX, e.clientY, e);
});
ele.addEventListener('touchend', e => {
fn(e.touches[0].clientX, e.touches[0].clientY, e);
});
}
2022-11-30 16:42:44 +08:00
/**
*
* @param ele
* @param fn
*/
export function useDown(ele: HTMLElement, fn: DragFn): void {
ele.addEventListener('mousedown', e => {
fn(e.clientX, e.clientY, e);
});
ele.addEventListener('touchstart', e => {
fn(e.touches[0].clientX, e.touches[0].clientY, e);
});
}