react-simple-game-engine 0.3.27 → 0.3.28
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,19 @@
|
|
1
1
|
import { ReactElement, ReactNode } from "react";
|
2
2
|
import { IJoystickProps } from "./react-joystick-component/Joystick";
|
3
|
-
|
3
|
+
declare type TouchEvent = {
|
4
|
+
onPressed?: (container: HTMLDivElement, stick: HTMLButtonElement) => void;
|
5
|
+
onReleased?: (container: HTMLDivElement, stick: HTMLButtonElement) => void;
|
6
|
+
};
|
7
|
+
export declare type MovementControlProps = TouchEvent & {
|
4
8
|
top?: number;
|
5
9
|
left?: number;
|
6
10
|
right?: number;
|
7
11
|
bottom?: number;
|
8
12
|
render?: (el: ReactElement) => ReactNode;
|
13
|
+
joystickTouchEvent?: TouchEvent;
|
14
|
+
containerTouchEvent?: TouchEvent;
|
9
15
|
props?: Omit<IJoystickProps, "start" | "move" | "stop">;
|
10
16
|
};
|
11
|
-
export declare function MovementControl({ render, top, left, right, bottom, props, }: MovementControlProps): JSX.Element;
|
17
|
+
export declare function MovementControl({ render, top, left, right, bottom, props, containerTouchEvent, joystickTouchEvent, }: MovementControlProps): JSX.Element;
|
18
|
+
export {};
|
12
19
|
//# sourceMappingURL=movement-control.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"movement-control.d.ts","sourceRoot":"","sources":["../../src/ui-components/movement-control.tsx"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,EACZ,SAAS,
|
1
|
+
{"version":3,"file":"movement-control.d.ts","sourceRoot":"","sources":["../../src/ui-components/movement-control.tsx"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,EACZ,SAAS,EAKV,MAAM,OAAO,CAAC;AAMf,OAAO,EACL,cAAc,EAEf,MAAM,qCAAqC,CAAC;AAE7C,aAAK,UAAU,GAAG;IAChB,SAAS,CAAC,EAAE,CAAC,SAAS,EAAE,cAAc,EAAE,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;IAC1E,UAAU,CAAC,EAAE,CAAC,SAAS,EAAE,cAAc,EAAE,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;CAC5E,CAAC;AACF,oBAAY,oBAAoB,GAAG,UAAU,GAAG;IAC9C,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,CAAC,EAAE,EAAE,YAAY,KAAK,SAAS,CAAC;IACzC,kBAAkB,CAAC,EAAE,UAAU,CAAC;IAChC,mBAAmB,CAAC,EAAE,UAAU,CAAC;IACjC,KAAK,CAAC,EAAE,IAAI,CAAC,cAAc,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,CAAC,CAAC;CACzD,CAAC;AAEF,wBAAgB,eAAe,CAAC,EAC9B,MAAM,EACN,GAAG,EACH,IAAS,EACT,KAAK,EACL,MAAW,EACX,KAAK,EACL,mBAAmB,EACnB,kBAAkB,GACnB,EAAE,oBAAoB,eAwGtB"}
|
@@ -10,33 +10,41 @@ var __assign = (this && this.__assign) || function () {
|
|
10
10
|
return __assign.apply(this, arguments);
|
11
11
|
};
|
12
12
|
import { jsx as _jsx } from "react/jsx-runtime";
|
13
|
-
import {
|
13
|
+
import { useContext, useEffect, useMemo, useRef, } from "react";
|
14
14
|
import { JoystickActionType } from "../export-enums";
|
15
15
|
import { UISceneContext } from "../react-context";
|
16
16
|
import { Watcher } from "../utilities";
|
17
17
|
import { Joystick } from "./react-joystick-component";
|
18
18
|
export function MovementControl(_a) {
|
19
|
-
var render = _a.render, top = _a.top, _b = _a.left, left = _b === void 0 ? 50 : _b, right = _a.right, _c = _a.bottom, bottom = _c === void 0 ? 50 : _c, props = _a.props;
|
19
|
+
var render = _a.render, top = _a.top, _b = _a.left, left = _b === void 0 ? 50 : _b, right = _a.right, _c = _a.bottom, bottom = _c === void 0 ? 50 : _c, props = _a.props, containerTouchEvent = _a.containerTouchEvent, joystickTouchEvent = _a.joystickTouchEvent;
|
20
20
|
var refJoyWrap = useRef(null);
|
21
21
|
var refStick = useRef(null);
|
22
|
-
var refStickBasePosition = useRef(null);
|
23
22
|
var scene = useContext(UISceneContext);
|
24
23
|
var defaultShow = useMemo(function () {
|
25
24
|
return scene.getInitialData();
|
26
25
|
}, [scene]).joystick;
|
27
|
-
var debutStickPosition = useCallback(function (x, y) {
|
28
|
-
refJoyWrap.current.style.setProperty("--stick-center-x", x.toString());
|
29
|
-
refJoyWrap.current.style.setProperty("--stick-center-y", y.toString());
|
30
|
-
}, []);
|
31
26
|
useEffect(function () {
|
32
27
|
refStick.current = refJoyWrap.current.querySelectorAll("button")[0];
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
28
|
+
if (!joystickTouchEvent) {
|
29
|
+
return;
|
30
|
+
}
|
31
|
+
var handlePointerdown = function (event) {
|
32
|
+
var _a;
|
33
|
+
event.target.setPointerCapture(event.pointerId);
|
34
|
+
(_a = joystickTouchEvent.onPressed) === null || _a === void 0 ? void 0 : _a.call(joystickTouchEvent, refJoyWrap.current, refStick.current);
|
35
|
+
};
|
36
|
+
var handlePointerup = function (event) {
|
37
|
+
var _a;
|
38
|
+
event.target.releasePointerCapture(event.pointerId);
|
39
|
+
(_a = joystickTouchEvent.onReleased) === null || _a === void 0 ? void 0 : _a.call(joystickTouchEvent, refJoyWrap.current, refStick.current);
|
40
|
+
};
|
41
|
+
refStick.current.addEventListener("pointerdown", handlePointerdown);
|
42
|
+
refStick.current.addEventListener("pointerup", handlePointerup);
|
43
|
+
return function () {
|
44
|
+
refStick.current.removeEventListener("pointerdown", handlePointerdown);
|
45
|
+
refStick.current.removeEventListener("pointerup", handlePointerup);
|
37
46
|
};
|
38
|
-
|
39
|
-
}, [debutStickPosition]);
|
47
|
+
}, [joystickTouchEvent]);
|
40
48
|
var el = useMemo(function () {
|
41
49
|
var onAction = function (e) {
|
42
50
|
if (e.type === JoystickActionType.MOVE) {
|
@@ -53,20 +61,26 @@ export function MovementControl(_a) {
|
|
53
61
|
type: e.type,
|
54
62
|
});
|
55
63
|
}
|
56
|
-
var _a = refStick.current.getBoundingClientRect(), left = _a.left, top = _a.top;
|
57
|
-
var stickCenterX = left - refStickBasePosition.current.x;
|
58
|
-
var stickCenterY = top - refStickBasePosition.current.y;
|
59
|
-
debutStickPosition(stickCenterX, stickCenterY);
|
60
64
|
};
|
61
65
|
var joystick = (_jsx(Joystick, __assign({ size: 60, baseColor: "#2D2D2D", stickColor: "rgb(120,121,122)", throttle: 100 }, props, { start: onAction, move: onAction, stop: onAction })));
|
62
|
-
return (_jsx("div", __assign({
|
66
|
+
return (_jsx("div", __assign({ onPointerDown: function (event) {
|
67
|
+
var _a;
|
68
|
+
event.target.setPointerCapture(event.pointerId);
|
69
|
+
(_a = containerTouchEvent === null || containerTouchEvent === void 0 ? void 0 : containerTouchEvent.onPressed) === null || _a === void 0 ? void 0 : _a.call(containerTouchEvent, refJoyWrap.current, refStick.current);
|
70
|
+
}, onPointerUp: function (event) {
|
71
|
+
var _a;
|
72
|
+
event.target.releasePointerCapture(event.pointerId);
|
73
|
+
(_a = containerTouchEvent === null || containerTouchEvent === void 0 ? void 0 : containerTouchEvent.onReleased) === null || _a === void 0 ? void 0 : _a.call(containerTouchEvent, refJoyWrap.current, refStick.current);
|
74
|
+
}, ref: refJoyWrap, style: {
|
75
|
+
"--stick-center-x": 0,
|
76
|
+
"--stick-center-y": 0,
|
63
77
|
position: "absolute",
|
64
78
|
left: right != null ? undefined : left,
|
65
79
|
right: right,
|
66
80
|
bottom: top != null ? undefined : bottom,
|
67
81
|
top: top,
|
68
82
|
} }, { children: render ? render(joystick) : joystick })));
|
69
|
-
}, [props,
|
83
|
+
}, [props, containerTouchEvent]);
|
70
84
|
return (_jsx(Watcher, __assign({ initialValues: {
|
71
85
|
isShow: defaultShow,
|
72
86
|
}, names: "control-visible" }, { children: function (_a) {
|