@predy-js/render-interface 0.3.4-beta.10 → 0.3.4-beta.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +706 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +700 -4
- package/dist/index.mjs.map +1 -1
- package/dist/src/module.d.ts +3 -0
- package/dist/src/render/EventSystem.d.ts +21 -0
- package/dist/src/render/event/H5Handler.d.ts +15 -0
- package/dist/src/render/event/RNHandler.d.ts +9 -0
- package/dist/src/render/event/UniversalRecognizer.d.ts +54 -0
- package/dist/src/render/event/WebHandler.d.ts +16 -0
- package/dist/statistic.js +1 -1
- package/dist/types/EventSystem.d.ts +123 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +1 -1
- package/types/EventSystem.ts +213 -0
- package/types/index.ts +1 -0
package/dist/index.mjs
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
* Name: @predy-js/render-interface
|
3
3
|
* Description: undefined
|
4
4
|
* Author: undefined
|
5
|
-
* Version: v0.3.4-beta.
|
5
|
+
* Version: v0.3.4-beta.12
|
6
6
|
*/
|
7
7
|
|
8
8
|
/******************************************************************************
|
@@ -19,7 +19,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
19
19
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
20
20
|
PERFORMANCE OF THIS SOFTWARE.
|
21
21
|
***************************************************************************** */
|
22
|
-
/* global Reflect, Promise, SuppressedError, Symbol
|
22
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
23
23
|
|
24
24
|
var extendStatics = function(d, b) {
|
25
25
|
extendStatics = Object.setPrototypeOf ||
|
@@ -170,6 +170,83 @@ var TextureSourceType;
|
|
170
170
|
|
171
171
|
var GPUBufferOptionsMemoryShared = 1 << 1;
|
172
172
|
|
173
|
+
var EventSystemEnv;
|
174
|
+
(function (EventSystemEnv) {
|
175
|
+
EventSystemEnv[EventSystemEnv["web"] = 0] = "web";
|
176
|
+
EventSystemEnv[EventSystemEnv["h5"] = 1] = "h5";
|
177
|
+
EventSystemEnv[EventSystemEnv["rn"] = 2] = "rn";
|
178
|
+
EventSystemEnv[EventSystemEnv["ios"] = 3] = "ios";
|
179
|
+
EventSystemEnv[EventSystemEnv["android"] = 4] = "android";
|
180
|
+
EventSystemEnv[EventSystemEnv["harmony"] = 5] = "harmony";
|
181
|
+
})(EventSystemEnv || (EventSystemEnv = {}));
|
182
|
+
var RecognizerState;
|
183
|
+
(function (RecognizerState) {
|
184
|
+
RecognizerState[RecognizerState["UNDETERMINED"] = 0] = "UNDETERMINED";
|
185
|
+
RecognizerState[RecognizerState["FAILED"] = 1] = "FAILED";
|
186
|
+
RecognizerState[RecognizerState["BEGAN"] = 2] = "BEGAN";
|
187
|
+
RecognizerState[RecognizerState["CANCELLED"] = 3] = "CANCELLED";
|
188
|
+
RecognizerState[RecognizerState["ACTIVE"] = 4] = "ACTIVE";
|
189
|
+
RecognizerState[RecognizerState["END"] = 5] = "END";
|
190
|
+
})(RecognizerState || (RecognizerState = {}));
|
191
|
+
var EVENT_TYPE_CLICK = 'click';
|
192
|
+
var EVENT_TYPE_TOUCH_START = 'touchstart';
|
193
|
+
var EVENT_TYPE_TOUCH_MOVE = 'touchmove';
|
194
|
+
var EVENT_TYPE_TOUCH_END = 'touchend';
|
195
|
+
var EVENT_TYPE_TOUCH_CANCEL = 'touchcancel';
|
196
|
+
// // 扩展事件类型
|
197
|
+
// export enum EVENT_TYPE {
|
198
|
+
// // 基础事件
|
199
|
+
// START = 'start',
|
200
|
+
// MOVE = 'move',
|
201
|
+
// END = 'end',
|
202
|
+
// CANCEL = 'cancel',
|
203
|
+
// // 手势事件
|
204
|
+
// PINCH_START = 'pinchstart',
|
205
|
+
// PINCH_MOVE = 'pinchmove',
|
206
|
+
// PINCH_END = 'pinchend',
|
207
|
+
// PINCH = 'pinch',
|
208
|
+
// ROTATE_START = 'rotatestart',
|
209
|
+
// ROTATE_MOVE = 'rotatemove',
|
210
|
+
// ROTATE_END = 'rotateend',
|
211
|
+
// ROTATE = 'rotate',
|
212
|
+
// PAN_START = 'panstart',
|
213
|
+
// PAN_MOVE = 'panmove',
|
214
|
+
// PAN_END = 'panend',
|
215
|
+
// PAN = 'pan',
|
216
|
+
// SWIPE = 'swipe',
|
217
|
+
// TAP = 'tap',
|
218
|
+
// }
|
219
|
+
// export type PredyGestureEventArg = {
|
220
|
+
// type: EVENT_TYPE,
|
221
|
+
// touches: PredyTouch[], // 当前所有触点
|
222
|
+
// center: vec2, // 手势中心点
|
223
|
+
// scale?: number, // 当前缩放比例(相对于初始)
|
224
|
+
// rotation?: number, // 旋转角度(弧度,相对于初始)
|
225
|
+
// velocityX?: number, // 移动速度
|
226
|
+
// velocityY?: number, // 移动速度
|
227
|
+
// deltaX?: number, // X轴位移
|
228
|
+
// deltaY?: number, // Y轴位移
|
229
|
+
// timestamp: number, // 时间戳
|
230
|
+
// originEvent: Event, // 原始事件
|
231
|
+
// };
|
232
|
+
// 方向枚举
|
233
|
+
var GESTURE_DIRECTION;
|
234
|
+
(function (GESTURE_DIRECTION) {
|
235
|
+
GESTURE_DIRECTION[GESTURE_DIRECTION["NONE"] = 0] = "NONE";
|
236
|
+
GESTURE_DIRECTION[GESTURE_DIRECTION["ANY"] = 1] = "ANY";
|
237
|
+
// 滑动方向
|
238
|
+
GESTURE_DIRECTION[GESTURE_DIRECTION["UP"] = 2] = "UP";
|
239
|
+
GESTURE_DIRECTION[GESTURE_DIRECTION["DOWN"] = 3] = "DOWN";
|
240
|
+
GESTURE_DIRECTION[GESTURE_DIRECTION["LEFT"] = 4] = "LEFT";
|
241
|
+
GESTURE_DIRECTION[GESTURE_DIRECTION["RIGHT"] = 5] = "RIGHT";
|
242
|
+
// 缩放方向
|
243
|
+
GESTURE_DIRECTION[GESTURE_DIRECTION["IN"] = 6] = "IN";
|
244
|
+
GESTURE_DIRECTION[GESTURE_DIRECTION["OUT"] = 7] = "OUT";
|
245
|
+
// 旋转方向
|
246
|
+
GESTURE_DIRECTION[GESTURE_DIRECTION["CLOCKWISE"] = 8] = "CLOCKWISE";
|
247
|
+
GESTURE_DIRECTION[GESTURE_DIRECTION["COUNTERCLOCKWISE"] = 9] = "COUNTERCLOCKWISE";
|
248
|
+
})(GESTURE_DIRECTION || (GESTURE_DIRECTION = {}));
|
249
|
+
|
173
250
|
var _a$6, _b$3;
|
174
251
|
// @ts-expect-error safe to assign
|
175
252
|
var constants = {};
|
@@ -5909,7 +5986,616 @@ var MarsSharedGeometry = /** @class */ (function (_super) {
|
|
5909
5986
|
return MarsSharedGeometry;
|
5910
5987
|
}(MarsGeometry));
|
5911
5988
|
|
5912
|
-
|
5989
|
+
var WebHandler = /** @class */ (function () {
|
5990
|
+
function WebHandler(system) {
|
5991
|
+
var _this = this;
|
5992
|
+
this.system = system;
|
5993
|
+
this.isPointerDown = false;
|
5994
|
+
this.handleStart = function (e) {
|
5995
|
+
_this.isPointerDown = true;
|
5996
|
+
_this.handleEvent(EVENT_TYPE_TOUCH_START, e);
|
5997
|
+
};
|
5998
|
+
this.handleMove = function (e) {
|
5999
|
+
if (_this.isPointerDown) {
|
6000
|
+
_this.handleEvent(EVENT_TYPE_TOUCH_MOVE, e);
|
6001
|
+
}
|
6002
|
+
};
|
6003
|
+
this.handleEnd = function (e) {
|
6004
|
+
_this.handleEvent(EVENT_TYPE_TOUCH_END, e);
|
6005
|
+
_this.isPointerDown = false;
|
6006
|
+
};
|
6007
|
+
this.pointers = new Map();
|
6008
|
+
this.timeOrigin = Date.now();
|
6009
|
+
}
|
6010
|
+
Object.defineProperty(WebHandler.prototype, "timeStamp", {
|
6011
|
+
get: function () {
|
6012
|
+
return Date.now() - this.timeOrigin;
|
6013
|
+
},
|
6014
|
+
enumerable: false,
|
6015
|
+
configurable: true
|
6016
|
+
});
|
6017
|
+
WebHandler.prototype.setup = function () {
|
6018
|
+
var target = this.system.target;
|
6019
|
+
target.addEventListener('pointerdown', this.handleStart);
|
6020
|
+
target.addEventListener('pointermove', this.handleMove);
|
6021
|
+
target.addEventListener('pointerup', this.handleEnd);
|
6022
|
+
};
|
6023
|
+
WebHandler.prototype.teardown = function () {
|
6024
|
+
var target = this.system.target;
|
6025
|
+
target.removeEventListener('pointerdown', this.handleStart);
|
6026
|
+
target.removeEventListener('pointermove', this.handleMove);
|
6027
|
+
target.removeEventListener('pointerup', this.handleEnd);
|
6028
|
+
};
|
6029
|
+
WebHandler.prototype.getNormalizedPos = function (e) {
|
6030
|
+
var rect = e.target.getBoundingClientRect();
|
6031
|
+
return [
|
6032
|
+
((e.clientX - rect.left) / rect.width) * 2 - 1,
|
6033
|
+
((e.clientY - rect.top) / rect.height) * -2 + 1,
|
6034
|
+
];
|
6035
|
+
};
|
6036
|
+
WebHandler.prototype.handleEvent = function (type, e) {
|
6037
|
+
e.preventDefault();
|
6038
|
+
var np = this.getNormalizedPos(e);
|
6039
|
+
var pointer = {
|
6040
|
+
id: e.pointerId,
|
6041
|
+
x: np[0],
|
6042
|
+
y: np[1],
|
6043
|
+
clientX: e.clientX,
|
6044
|
+
clientY: e.clientY,
|
6045
|
+
force: 1,
|
6046
|
+
ts: this.timeStamp,
|
6047
|
+
};
|
6048
|
+
// 更新触点集合
|
6049
|
+
if (type === EVENT_TYPE_TOUCH_END) {
|
6050
|
+
this.pointers.delete(e.pointerId);
|
6051
|
+
}
|
6052
|
+
else {
|
6053
|
+
this.pointers.set(e.pointerId, pointer);
|
6054
|
+
}
|
6055
|
+
// 封装基础事件
|
6056
|
+
var event = {
|
6057
|
+
type: type,
|
6058
|
+
timestamp: this.timeStamp,
|
6059
|
+
target: e.target,
|
6060
|
+
touches: Array.from(this.pointers.values()),
|
6061
|
+
changedTouches: [pointer],
|
6062
|
+
preventDefault: function () { return e.preventDefault(); },
|
6063
|
+
stopPropagation: function () { return e.stopPropagation(); },
|
6064
|
+
nativeEvent: e,
|
6065
|
+
};
|
6066
|
+
this.system.handleBaseEvent(event);
|
6067
|
+
};
|
6068
|
+
return WebHandler;
|
6069
|
+
}());
|
6070
|
+
|
6071
|
+
var RNHandler = /** @class */ (function () {
|
6072
|
+
function RNHandler(system) {
|
6073
|
+
this.system = system;
|
6074
|
+
this.timeOrigin = Date.now();
|
6075
|
+
}
|
6076
|
+
Object.defineProperty(RNHandler.prototype, "timeStamp", {
|
6077
|
+
get: function () {
|
6078
|
+
return Date.now() - this.timeOrigin;
|
6079
|
+
},
|
6080
|
+
enumerable: false,
|
6081
|
+
configurable: true
|
6082
|
+
});
|
6083
|
+
RNHandler.prototype.setup = function () {
|
6084
|
+
throw new Error('Method not implemented.');
|
6085
|
+
};
|
6086
|
+
RNHandler.prototype.teardown = function () {
|
6087
|
+
throw new Error('Method not implemented.');
|
6088
|
+
};
|
6089
|
+
return RNHandler;
|
6090
|
+
}());
|
6091
|
+
|
6092
|
+
var H5Handler = /** @class */ (function () {
|
6093
|
+
function H5Handler(system) {
|
6094
|
+
var _this = this;
|
6095
|
+
this.system = system;
|
6096
|
+
this.handleTouch = function (type, e) {
|
6097
|
+
e.preventDefault();
|
6098
|
+
// 封装基础事件
|
6099
|
+
var event = {
|
6100
|
+
type: type,
|
6101
|
+
timestamp: _this.timeStamp,
|
6102
|
+
target: e.target,
|
6103
|
+
touches: _this.encapsulate(e.targetTouches),
|
6104
|
+
changedTouches: _this.encapsulate(e.changedTouches),
|
6105
|
+
preventDefault: function () { return e.preventDefault(); },
|
6106
|
+
stopPropagation: function () { return e.stopPropagation(); },
|
6107
|
+
nativeEvent: e,
|
6108
|
+
};
|
6109
|
+
_this.system.handleBaseEvent(event);
|
6110
|
+
};
|
6111
|
+
this.handleStart = function (e) { return _this.handleTouch(EVENT_TYPE_TOUCH_START, e); };
|
6112
|
+
this.handleMove = function (e) { return _this.handleTouch(EVENT_TYPE_TOUCH_MOVE, e); };
|
6113
|
+
this.handleEnd = function (e) { return _this.handleTouch(EVENT_TYPE_TOUCH_END, e); };
|
6114
|
+
this.timeOrigin = Date.now();
|
6115
|
+
}
|
6116
|
+
Object.defineProperty(H5Handler.prototype, "timeStamp", {
|
6117
|
+
get: function () {
|
6118
|
+
return Date.now() - this.timeOrigin;
|
6119
|
+
},
|
6120
|
+
enumerable: false,
|
6121
|
+
configurable: true
|
6122
|
+
});
|
6123
|
+
H5Handler.prototype.setup = function () {
|
6124
|
+
var target = this.system.target;
|
6125
|
+
target.addEventListener('touchstart', this.handleStart);
|
6126
|
+
target.addEventListener('touchmove', this.handleMove);
|
6127
|
+
target.addEventListener('touchend', this.handleEnd);
|
6128
|
+
target.addEventListener('touchcancel', this.handleEnd);
|
6129
|
+
};
|
6130
|
+
H5Handler.prototype.teardown = function () {
|
6131
|
+
var target = this.system.target;
|
6132
|
+
target.removeEventListener('touchstart', this.handleStart);
|
6133
|
+
target.removeEventListener('touchmove', this.handleMove);
|
6134
|
+
target.removeEventListener('touchend', this.handleEnd);
|
6135
|
+
target.removeEventListener('touchcancel', this.handleEnd);
|
6136
|
+
};
|
6137
|
+
H5Handler.prototype.getNormalizedPos = function (e) {
|
6138
|
+
var rect = e.target.getBoundingClientRect();
|
6139
|
+
return [
|
6140
|
+
((e.clientX - rect.left) / rect.width) * 2 - 1,
|
6141
|
+
((e.clientY - rect.top) / rect.height) * -2 + 1,
|
6142
|
+
];
|
6143
|
+
};
|
6144
|
+
H5Handler.prototype.encapsulate = function (list) {
|
6145
|
+
var touches = [];
|
6146
|
+
for (var i = 0; i < list.length; i++) {
|
6147
|
+
var touch = list[i];
|
6148
|
+
var _a = this.getNormalizedPos(touch), x = _a[0], y = _a[1];
|
6149
|
+
touches.push({
|
6150
|
+
id: touch.identifier,
|
6151
|
+
x: x,
|
6152
|
+
y: y,
|
6153
|
+
clientX: touch.clientX,
|
6154
|
+
clientY: touch.clientY,
|
6155
|
+
force: 1,
|
6156
|
+
ts: this.timeStamp,
|
6157
|
+
});
|
6158
|
+
}
|
6159
|
+
return touches;
|
6160
|
+
};
|
6161
|
+
return H5Handler;
|
6162
|
+
}());
|
6163
|
+
|
6164
|
+
var PredyEventSystem = /** @class */ (function () {
|
6165
|
+
function PredyEventSystem(target, env) {
|
6166
|
+
this._handlerMap = new Map();
|
6167
|
+
this._enabled = true;
|
6168
|
+
this.target = target;
|
6169
|
+
this._platformHandler = this.createPlatformHandler(env);
|
6170
|
+
this._platformHandler.setup();
|
6171
|
+
this.setRecognizer(new BaseRecognizer());
|
6172
|
+
}
|
6173
|
+
Object.defineProperty(PredyEventSystem.prototype, "enabled", {
|
6174
|
+
get: function () {
|
6175
|
+
return this._enabled;
|
6176
|
+
},
|
6177
|
+
set: function (value) {
|
6178
|
+
this._enabled = value;
|
6179
|
+
},
|
6180
|
+
enumerable: false,
|
6181
|
+
configurable: true
|
6182
|
+
});
|
6183
|
+
PredyEventSystem.prototype.createPlatformHandler = function (env) {
|
6184
|
+
switch (env) {
|
6185
|
+
case EventSystemEnv.web:
|
6186
|
+
return new WebHandler(this);
|
6187
|
+
case EventSystemEnv.rn:
|
6188
|
+
return new RNHandler(this);
|
6189
|
+
case EventSystemEnv.h5:
|
6190
|
+
return new H5Handler(this);
|
6191
|
+
default:
|
6192
|
+
throw new Error("Unsupported platform ".concat(env));
|
6193
|
+
}
|
6194
|
+
};
|
6195
|
+
PredyEventSystem.prototype.setRecognizer = function (recognizer) {
|
6196
|
+
recognizer._callback = this.dispatch.bind(this);
|
6197
|
+
this.recognizer = recognizer;
|
6198
|
+
};
|
6199
|
+
// 事件分发入口
|
6200
|
+
PredyEventSystem.prototype.handleBaseEvent = function (event) {
|
6201
|
+
if (!this.enabled) {
|
6202
|
+
return;
|
6203
|
+
}
|
6204
|
+
// 识别手势
|
6205
|
+
var gestureType = this.recognizer.detect(event);
|
6206
|
+
if (gestureType) {
|
6207
|
+
this.dispatch(gestureType, event);
|
6208
|
+
}
|
6209
|
+
if (event.touches.length === 0) {
|
6210
|
+
this.recognizer.reset();
|
6211
|
+
}
|
6212
|
+
};
|
6213
|
+
PredyEventSystem.prototype.dispatch = function (type, event) {
|
6214
|
+
var _a;
|
6215
|
+
(_a = this._handlerMap.get(type)) === null || _a === void 0 ? void 0 : _a.forEach(function (fn) { return fn(event); });
|
6216
|
+
};
|
6217
|
+
PredyEventSystem.prototype.addEventListener = function (type, handler) {
|
6218
|
+
var _this = this;
|
6219
|
+
var set = this._handlerMap.get(type) || new Set();
|
6220
|
+
set.add(handler);
|
6221
|
+
this._handlerMap.set(type, set);
|
6222
|
+
return function () { return _this.removeEventListener(type, handler); };
|
6223
|
+
};
|
6224
|
+
PredyEventSystem.prototype.removeEventListener = function (type, handler) {
|
6225
|
+
var _a;
|
6226
|
+
(_a = this._handlerMap.get(type)) === null || _a === void 0 ? void 0 : _a.delete(handler);
|
6227
|
+
};
|
6228
|
+
PredyEventSystem.prototype.destroy = function () {
|
6229
|
+
this._platformHandler.teardown();
|
6230
|
+
this._handlerMap.clear();
|
6231
|
+
};
|
6232
|
+
return PredyEventSystem;
|
6233
|
+
}());
|
6234
|
+
var BaseRecognizer = /** @class */ (function () {
|
6235
|
+
function BaseRecognizer() {
|
6236
|
+
}
|
6237
|
+
BaseRecognizer.prototype.detect = function (event) {
|
6238
|
+
return event.type;
|
6239
|
+
};
|
6240
|
+
BaseRecognizer.prototype.reset = function () { };
|
6241
|
+
return BaseRecognizer;
|
6242
|
+
}());
|
6243
|
+
|
6244
|
+
var UniversalRecognizer = /** @class */ (function () {
|
6245
|
+
function UniversalRecognizer(options) {
|
6246
|
+
this._state = RecognizerState.UNDETERMINED;
|
6247
|
+
this.trackedTouches = new Map(); // 跟踪所有触点
|
6248
|
+
this.gestureStartTime = 0;
|
6249
|
+
this.startPositions = [];
|
6250
|
+
this.lastEventTs = 0;
|
6251
|
+
// 缩放/旋转专用缓存
|
6252
|
+
this.initialCenter = [0, 0];
|
6253
|
+
this.validateOptions(options);
|
6254
|
+
this.options = options;
|
6255
|
+
}
|
6256
|
+
Object.defineProperty(UniversalRecognizer.prototype, "state", {
|
6257
|
+
get: function () {
|
6258
|
+
return this._state;
|
6259
|
+
},
|
6260
|
+
enumerable: false,
|
6261
|
+
configurable: true
|
6262
|
+
});
|
6263
|
+
UniversalRecognizer.prototype.detect = function (event) {
|
6264
|
+
this.lastEventTs = event.timestamp;
|
6265
|
+
switch (event.type) {
|
6266
|
+
case 'touchstart':
|
6267
|
+
return this.handleStart(event);
|
6268
|
+
case 'touchmove':
|
6269
|
+
return this.handleMove(event);
|
6270
|
+
case 'touchend':
|
6271
|
+
case 'touchcancel':
|
6272
|
+
return this.handleEnd(event);
|
6273
|
+
}
|
6274
|
+
};
|
6275
|
+
UniversalRecognizer.prototype.trigger = function (event) {
|
6276
|
+
var _a;
|
6277
|
+
var type = this.getGestureName();
|
6278
|
+
(_a = this._callback) === null || _a === void 0 ? void 0 : _a.call(this, type, event);
|
6279
|
+
};
|
6280
|
+
UniversalRecognizer.prototype.handleStart = function (event) {
|
6281
|
+
var _this = this;
|
6282
|
+
if (this.state !== RecognizerState.UNDETERMINED) {
|
6283
|
+
return;
|
6284
|
+
}
|
6285
|
+
// 验证触点数量
|
6286
|
+
if (event.touches.length !== this.options.touchCount) {
|
6287
|
+
this.reset();
|
6288
|
+
return;
|
6289
|
+
}
|
6290
|
+
// 初始化跟踪数据
|
6291
|
+
this.gestureStartTime = event.timestamp;
|
6292
|
+
this.trackedTouches.clear();
|
6293
|
+
event.touches.forEach(function (t) { return _this.trackedTouches.set(t.id, t); });
|
6294
|
+
this.startPositions = event.touches.map(function (t) { return [t.clientX, t.clientY]; });
|
6295
|
+
// 初始化多指手势数据
|
6296
|
+
if (this.options.touchCount > 1) {
|
6297
|
+
this.initialCenter = this.calculateCenter(event.touches);
|
6298
|
+
}
|
6299
|
+
this.setState(RecognizerState.BEGAN);
|
6300
|
+
return this.getGestureName('start');
|
6301
|
+
};
|
6302
|
+
UniversalRecognizer.prototype.handleMove = function (event) {
|
6303
|
+
if (![RecognizerState.BEGAN, RecognizerState.ACTIVE].includes(this.state)) {
|
6304
|
+
return;
|
6305
|
+
}
|
6306
|
+
// 验证触点数量一致性
|
6307
|
+
if (event.touches.length !== this.options.touchCount) {
|
6308
|
+
this.reset();
|
6309
|
+
return;
|
6310
|
+
}
|
6311
|
+
// 核心验证逻辑
|
6312
|
+
if (!this.validateGesture(event)) {
|
6313
|
+
this.handleFailure();
|
6314
|
+
return;
|
6315
|
+
}
|
6316
|
+
// 更新状态为持续活动
|
6317
|
+
if (this.state === RecognizerState.BEGAN) {
|
6318
|
+
this.setState(RecognizerState.ACTIVE);
|
6319
|
+
// return this.getGestureName('start');
|
6320
|
+
}
|
6321
|
+
return this.getGestureName('move');
|
6322
|
+
};
|
6323
|
+
UniversalRecognizer.prototype.handleEnd = function (event) {
|
6324
|
+
if (![RecognizerState.BEGAN, RecognizerState.ACTIVE].includes(this.state)) {
|
6325
|
+
return;
|
6326
|
+
}
|
6327
|
+
// 最终验证(例如点击的持续时间)
|
6328
|
+
var isValid = this.validateFinalConditions(event);
|
6329
|
+
if (isValid) {
|
6330
|
+
this.setState(RecognizerState.END);
|
6331
|
+
if (this.validateThreshold(event)) {
|
6332
|
+
this.trigger(event);
|
6333
|
+
}
|
6334
|
+
return this.getGestureName('end');
|
6335
|
+
}
|
6336
|
+
else {
|
6337
|
+
this.handleFailure();
|
6338
|
+
}
|
6339
|
+
};
|
6340
|
+
/**
|
6341
|
+
* 手势识别核心算法
|
6342
|
+
*/
|
6343
|
+
UniversalRecognizer.prototype.validateGesture = function (event) {
|
6344
|
+
// 基础验证
|
6345
|
+
if (!this.validateTouchCount(event)) {
|
6346
|
+
return false;
|
6347
|
+
}
|
6348
|
+
if (!this.validateDuration(event)) {
|
6349
|
+
return false;
|
6350
|
+
}
|
6351
|
+
if (!this.validateDistance(event)) {
|
6352
|
+
return false;
|
6353
|
+
}
|
6354
|
+
// 方向/路径验证
|
6355
|
+
if (this.options.direction !== undefined) {
|
6356
|
+
if (!this.validateDirection(event)) {
|
6357
|
+
return false;
|
6358
|
+
}
|
6359
|
+
}
|
6360
|
+
// 高级手势验证
|
6361
|
+
return true;
|
6362
|
+
};
|
6363
|
+
UniversalRecognizer.prototype.validateTouchCount = function (event) {
|
6364
|
+
return event.touches.length === this.options.touchCount;
|
6365
|
+
};
|
6366
|
+
UniversalRecognizer.prototype.validateDuration = function (event) {
|
6367
|
+
var duration = event.timestamp - this.gestureStartTime;
|
6368
|
+
if (this.options.minDuration && duration < this.options.minDuration) {
|
6369
|
+
return false;
|
6370
|
+
}
|
6371
|
+
if (this.options.maxDuration && duration > this.options.maxDuration) {
|
6372
|
+
return false;
|
6373
|
+
}
|
6374
|
+
return true;
|
6375
|
+
};
|
6376
|
+
UniversalRecognizer.prototype.validateDistance = function (event) {
|
6377
|
+
var currentCenter = this.calculateCenter(event.touches);
|
6378
|
+
var startCenter = [0, 0];
|
6379
|
+
this.startPositions.forEach(function (pos) {
|
6380
|
+
startCenter[0] += pos[0];
|
6381
|
+
startCenter[1] += pos[1];
|
6382
|
+
});
|
6383
|
+
startCenter[0] /= this.startPositions.length;
|
6384
|
+
startCenter[1] /= this.startPositions.length;
|
6385
|
+
var distance = Math.hypot(currentCenter[0] - startCenter[0], currentCenter[1] - startCenter[1]);
|
6386
|
+
if (this.options.minDistance && distance < this.options.minDistance) {
|
6387
|
+
return false;
|
6388
|
+
}
|
6389
|
+
if (this.options.maxDistance && distance > this.options.maxDistance) {
|
6390
|
+
return false;
|
6391
|
+
}
|
6392
|
+
return true;
|
6393
|
+
};
|
6394
|
+
UniversalRecognizer.prototype.validateDirection = function (event) {
|
6395
|
+
return true;
|
6396
|
+
};
|
6397
|
+
UniversalRecognizer.prototype.validateThreshold = function (event) {
|
6398
|
+
var delta = this.calculateOverallDelta(event);
|
6399
|
+
switch (this.options.direction) {
|
6400
|
+
case GESTURE_DIRECTION.LEFT:
|
6401
|
+
return delta[0] < -this.options.minDistance;
|
6402
|
+
case GESTURE_DIRECTION.RIGHT:
|
6403
|
+
return delta[0] > this.options.minDistance;
|
6404
|
+
case GESTURE_DIRECTION.UP:
|
6405
|
+
return delta[1] < -this.options.minDistance;
|
6406
|
+
case GESTURE_DIRECTION.DOWN:
|
6407
|
+
return delta[1] > this.options.minDistance;
|
6408
|
+
case GESTURE_DIRECTION.IN:
|
6409
|
+
return this.calculateScale(event.touches) < (1 - this.options.scaleThreshold);
|
6410
|
+
case GESTURE_DIRECTION.OUT:
|
6411
|
+
return this.calculateScale(event.touches) > (1 + this.options.scaleThreshold);
|
6412
|
+
case GESTURE_DIRECTION.CLOCKWISE:
|
6413
|
+
return this.calculateRotation(event.touches) > this.options.rotationThreshold;
|
6414
|
+
case GESTURE_DIRECTION.COUNTERCLOCKWISE:
|
6415
|
+
return this.calculateRotation(event.touches) < -this.options.rotationThreshold;
|
6416
|
+
default:
|
6417
|
+
return true;
|
6418
|
+
}
|
6419
|
+
};
|
6420
|
+
UniversalRecognizer.prototype.validateFinalConditions = function (event) {
|
6421
|
+
// 验证时间区间
|
6422
|
+
var duration = event.timestamp - this.gestureStartTime;
|
6423
|
+
if (this.options.minDuration && duration < this.options.minDuration) {
|
6424
|
+
return false;
|
6425
|
+
}
|
6426
|
+
if (this.options.maxDuration && duration > this.options.maxDuration) {
|
6427
|
+
return false;
|
6428
|
+
}
|
6429
|
+
// 验证点击次数(需要外部状态管理)
|
6430
|
+
// 此处需要结合外部计数器实现双击等逻辑
|
6431
|
+
return true;
|
6432
|
+
};
|
6433
|
+
UniversalRecognizer.prototype.calculateOverallDelta = function (event) {
|
6434
|
+
var _this = this;
|
6435
|
+
// 计算多点触控的平均移动量
|
6436
|
+
var totalDelta = [0, 0];
|
6437
|
+
event.touches.forEach(function (t, idx) {
|
6438
|
+
totalDelta[0] += t.clientX - _this.startPositions[idx][0];
|
6439
|
+
totalDelta[1] += t.clientY - _this.startPositions[idx][1];
|
6440
|
+
});
|
6441
|
+
return [totalDelta[0] / event.touches.length, totalDelta[1] / event.touches.length];
|
6442
|
+
};
|
6443
|
+
/**
|
6444
|
+
* 计算聚合缩放比例(基于各触点到中心点的距离变化)
|
6445
|
+
*/
|
6446
|
+
UniversalRecognizer.prototype.calculateScale = function (touches) {
|
6447
|
+
var _this = this;
|
6448
|
+
var currentCentroid = this.calculateCenter(touches);
|
6449
|
+
// 计算所有触点到中心点的平均距离
|
6450
|
+
var currentDistances = touches.map(function (t) {
|
6451
|
+
return Math.hypot(t.clientX - currentCentroid[0], t.clientY - currentCentroid[1]);
|
6452
|
+
});
|
6453
|
+
var currentAvgDistance = currentDistances.reduce(function (a, b) { return a + b; }) / touches.length;
|
6454
|
+
// 初始平均距离(从startPositions计算)
|
6455
|
+
var initialDistances = this.startPositions.map(function (pos) {
|
6456
|
+
return Math.hypot(pos[0] - _this.initialCenter[0], pos[1] - _this.initialCenter[1]);
|
6457
|
+
});
|
6458
|
+
var initialAvgDistance = initialDistances.reduce(function (a, b) { return a + b; }) / touches.length;
|
6459
|
+
return initialAvgDistance > 0 ? currentAvgDistance / initialAvgDistance : 1;
|
6460
|
+
};
|
6461
|
+
/**
|
6462
|
+
* 计算聚合旋转角度(基于各触点相对于中心点的角度变化)
|
6463
|
+
*/
|
6464
|
+
UniversalRecognizer.prototype.calculateRotation = function (touches) {
|
6465
|
+
var _this = this;
|
6466
|
+
var currentCentroid = this.calculateCenter(touches);
|
6467
|
+
var totalAngleChange = 0;
|
6468
|
+
touches.forEach(function (t, index) {
|
6469
|
+
// 当前触点角度
|
6470
|
+
var currentAngle = Math.atan2(t.clientY - currentCentroid[1], t.clientX - currentCentroid[0]);
|
6471
|
+
// 初始触点角度
|
6472
|
+
var initialPos = _this.startPositions[index];
|
6473
|
+
var initialAngle = Math.atan2(initialPos[1] - _this.initialCenter[1], initialPos[0] - _this.initialCenter[0]);
|
6474
|
+
// 累加角度变化(处理360°边界)
|
6475
|
+
var angleChange = currentAngle - initialAngle;
|
6476
|
+
if (angleChange > Math.PI) {
|
6477
|
+
angleChange -= 2 * Math.PI;
|
6478
|
+
}
|
6479
|
+
if (angleChange < -Math.PI) {
|
6480
|
+
angleChange += 2 * Math.PI;
|
6481
|
+
}
|
6482
|
+
totalAngleChange += angleChange;
|
6483
|
+
});
|
6484
|
+
// 转换为角度制并返回平均值
|
6485
|
+
return (totalAngleChange * 180 / Math.PI) / touches.length;
|
6486
|
+
};
|
6487
|
+
// 工具方法
|
6488
|
+
UniversalRecognizer.prototype.calculateCenter = function (touchList) {
|
6489
|
+
var pointersLength = touchList.length;
|
6490
|
+
if (pointersLength === 1) {
|
6491
|
+
return [touchList[0].clientX, touchList[0].clientY];
|
6492
|
+
}
|
6493
|
+
var x = 0;
|
6494
|
+
var y = 0;
|
6495
|
+
var i = 0;
|
6496
|
+
while (i < pointersLength) {
|
6497
|
+
x += touchList[i].clientX;
|
6498
|
+
y += touchList[i].clientY;
|
6499
|
+
i++;
|
6500
|
+
}
|
6501
|
+
return [x / pointersLength, y / pointersLength];
|
6502
|
+
};
|
6503
|
+
UniversalRecognizer.prototype.getGestureName = function (phase) {
|
6504
|
+
return phase ? "".concat(this.options.name).concat(phase) : this.options.name;
|
6505
|
+
};
|
6506
|
+
UniversalRecognizer.prototype.handleFailure = function () {
|
6507
|
+
this.setState(RecognizerState.FAILED);
|
6508
|
+
this.reset();
|
6509
|
+
};
|
6510
|
+
/**
|
6511
|
+
* 重置所有状态
|
6512
|
+
*/
|
6513
|
+
UniversalRecognizer.prototype.reset = function () {
|
6514
|
+
this.trackedTouches.clear();
|
6515
|
+
this.startPositions = [];
|
6516
|
+
this.setState(RecognizerState.UNDETERMINED);
|
6517
|
+
};
|
6518
|
+
UniversalRecognizer.prototype.setState = function (s) {
|
6519
|
+
this._state = s;
|
6520
|
+
};
|
6521
|
+
/**
|
6522
|
+
* 验证选项合规性,抛出详细错误信息
|
6523
|
+
*/
|
6524
|
+
UniversalRecognizer.prototype.validateOptions = function (options) {
|
6525
|
+
var errors = [];
|
6526
|
+
// 1. 基础必填字段检查
|
6527
|
+
if (!options.name || typeof options.name !== 'string') {
|
6528
|
+
errors.push('Missing or invalid `name`: must be a non-empty string');
|
6529
|
+
}
|
6530
|
+
if (typeof options.touchCount !== 'number' ||
|
6531
|
+
options.touchCount < 1 ||
|
6532
|
+
options.touchCount > 5 ||
|
6533
|
+
!Number.isInteger(options.touchCount)) {
|
6534
|
+
errors.push('Invalid `touchCount`: must be an integer of [1, 5]');
|
6535
|
+
}
|
6536
|
+
// 2. 方向相关检查
|
6537
|
+
if (options.direction !== undefined) {
|
6538
|
+
if (!Object.values(GESTURE_DIRECTION).includes(options.direction)) {
|
6539
|
+
errors.push("Invalid `moveDirection`: ".concat(options.direction, " is not a valid GESTURE_DIRECTION enum"));
|
6540
|
+
}
|
6541
|
+
// 当方向为NONE时,必须检查点击次数
|
6542
|
+
if (options.direction === GESTURE_DIRECTION.NONE) {
|
6543
|
+
if (options.clickCount === undefined) {
|
6544
|
+
errors.push('Missing `clickCount` when direction is NONE');
|
6545
|
+
}
|
6546
|
+
else if (typeof options.clickCount !== 'number' || options.clickCount < 1) {
|
6547
|
+
errors.push('Invalid `clickCount`: must be a positive integer');
|
6548
|
+
}
|
6549
|
+
}
|
6550
|
+
}
|
6551
|
+
// 3. 时间区间检查
|
6552
|
+
this.validateRange('duration', options.minDuration, options.maxDuration, 'Time duration range invalid', errors);
|
6553
|
+
// 4. 距离区间检查
|
6554
|
+
this.validateRange('distance', options.minDistance, options.maxDistance, 'Distance range invalid', errors);
|
6555
|
+
// 5. 高级参数检查
|
6556
|
+
if (options.scaleThreshold !== undefined) {
|
6557
|
+
if (options.touchCount < 2) {
|
6558
|
+
errors.push('`scaleThreshold` requires touchCount>=2');
|
6559
|
+
}
|
6560
|
+
else if (options.scaleThreshold <= 0) {
|
6561
|
+
errors.push('`scaleThreshold` must be positive');
|
6562
|
+
}
|
6563
|
+
}
|
6564
|
+
if (options.rotationThreshold !== undefined) {
|
6565
|
+
if (options.touchCount < 2) {
|
6566
|
+
errors.push('`rotationThreshold` requires touchCount>=2');
|
6567
|
+
}
|
6568
|
+
else if (options.rotationThreshold <= 0) {
|
6569
|
+
errors.push('`rotationThreshold` must be positive');
|
6570
|
+
}
|
6571
|
+
}
|
6572
|
+
// 6. 互斥参数检查
|
6573
|
+
if (options.direction !== undefined && options.customPath !== undefined) {
|
6574
|
+
errors.push('`moveDirection` and `customPath` cannot coexist');
|
6575
|
+
}
|
6576
|
+
// 抛出所有错误
|
6577
|
+
if (errors.length > 0) {
|
6578
|
+
throw new Error("Invalid options for \"".concat(options.name, "\":\n- ").concat(errors.join('\n- ')));
|
6579
|
+
}
|
6580
|
+
};
|
6581
|
+
/**
|
6582
|
+
* 通用区间验证工具
|
6583
|
+
*/
|
6584
|
+
UniversalRecognizer.prototype.validateRange = function (field, min, max, errorMsg, errors) {
|
6585
|
+
if (min !== undefined && min < 0) {
|
6586
|
+
errors.push("Invalid `min".concat(field, "`: must be non-negative"));
|
6587
|
+
}
|
6588
|
+
if (max !== undefined && max < 0) {
|
6589
|
+
errors.push("Invalid `max".concat(field, "`: must be non-negative"));
|
6590
|
+
}
|
6591
|
+
if (min !== undefined && max !== undefined && min > max) {
|
6592
|
+
errors.push("".concat(errorMsg, ": min (").concat(min, ") > max (").concat(max, ")"));
|
6593
|
+
}
|
6594
|
+
};
|
6595
|
+
return UniversalRecognizer;
|
6596
|
+
}());
|
6597
|
+
|
6598
|
+
consoleLog('version: ' + "0.3.4-beta.12");
|
5913
6599
|
var ModuleMsg = 'RI Package: @predy-js/render-interface';
|
5914
6600
|
|
5915
6601
|
var RI = /*#__PURE__*/Object.freeze({
|
@@ -5930,6 +6616,8 @@ var RI = /*#__PURE__*/Object.freeze({
|
|
5930
6616
|
getDefaultTextureFactory: getDefaultTextureFactory,
|
5931
6617
|
setDefaultTextureFactory: setDefaultTextureFactory,
|
5932
6618
|
MarsTextureFactory: MarsTextureFactory,
|
6619
|
+
EventSystem: PredyEventSystem,
|
6620
|
+
UniversalRecognizer: UniversalRecognizer,
|
5933
6621
|
get DestroyOptions () { return DestroyOptions; },
|
5934
6622
|
get PredyTextEncoding () { return PredyTextEncoding; },
|
5935
6623
|
get PredyVideoCodec () { return PredyVideoCodec; },
|
@@ -5944,6 +6632,14 @@ var RI = /*#__PURE__*/Object.freeze({
|
|
5944
6632
|
get RenderPassMeshOrder () { return RenderPassMeshOrder; },
|
5945
6633
|
get TextureSourceType () { return TextureSourceType; },
|
5946
6634
|
GPUBufferOptionsMemoryShared: GPUBufferOptionsMemoryShared,
|
6635
|
+
get EventSystemEnv () { return EventSystemEnv; },
|
6636
|
+
get RecognizerState () { return RecognizerState; },
|
6637
|
+
EVENT_TYPE_CLICK: EVENT_TYPE_CLICK,
|
6638
|
+
EVENT_TYPE_TOUCH_START: EVENT_TYPE_TOUCH_START,
|
6639
|
+
EVENT_TYPE_TOUCH_MOVE: EVENT_TYPE_TOUCH_MOVE,
|
6640
|
+
EVENT_TYPE_TOUCH_END: EVENT_TYPE_TOUCH_END,
|
6641
|
+
EVENT_TYPE_TOUCH_CANCEL: EVENT_TYPE_TOUCH_CANCEL,
|
6642
|
+
get GESTURE_DIRECTION () { return GESTURE_DIRECTION; },
|
5947
6643
|
get ShaderCompileResultStatus () { return ShaderCompileResultStatus; },
|
5948
6644
|
ShaderLibraryEmpty: ShaderLibraryEmpty
|
5949
6645
|
});
|
@@ -5955,5 +6651,5 @@ else if (typeof global === 'object') {
|
|
5955
6651
|
global.PredyRI = RI;
|
5956
6652
|
}
|
5957
6653
|
|
5958
|
-
export { DestroyOptions, GPUBufferOptionsMemoryShared, MarsGeometry as Geometry, MarsInstancedMesh as InstancedMesh, MarsTextureFactory, MarsMaterial as Material, MarsMaterialDataBlock as MaterialDataBlock, MarsMesh as Mesh, ModuleMsg, PredyResourceCacheStatus, PredyTextEncoding, PredyVideoCodec, MarsRenderFrame as RenderFrame, MarsRenderPass as RenderPass, RenderPassAttachmentStorageType, RenderPassDestroyAttachmentType, RenderPassMeshOrder, RenderPassPriorityNormal, RenderPassPriorityPostprocess, RenderPassPriorityPrepare, MarsRenderer as Renderer, ShaderCompileResultStatus, ShaderLibraryEmpty, MarsSharedGeometry as SharedGeometry, MarsTexture as Texture, TextureLoadAction, TextureSourceType, TextureStoreAction, constants, getDefaultGPUCapability, getDefaultTextureFactory, setDefaultTextureFactory };
|
6654
|
+
export { DestroyOptions, EVENT_TYPE_CLICK, EVENT_TYPE_TOUCH_CANCEL, EVENT_TYPE_TOUCH_END, EVENT_TYPE_TOUCH_MOVE, EVENT_TYPE_TOUCH_START, PredyEventSystem as EventSystem, EventSystemEnv, GESTURE_DIRECTION, GPUBufferOptionsMemoryShared, MarsGeometry as Geometry, MarsInstancedMesh as InstancedMesh, MarsTextureFactory, MarsMaterial as Material, MarsMaterialDataBlock as MaterialDataBlock, MarsMesh as Mesh, ModuleMsg, PredyResourceCacheStatus, PredyTextEncoding, PredyVideoCodec, RecognizerState, MarsRenderFrame as RenderFrame, MarsRenderPass as RenderPass, RenderPassAttachmentStorageType, RenderPassDestroyAttachmentType, RenderPassMeshOrder, RenderPassPriorityNormal, RenderPassPriorityPostprocess, RenderPassPriorityPrepare, MarsRenderer as Renderer, ShaderCompileResultStatus, ShaderLibraryEmpty, MarsSharedGeometry as SharedGeometry, MarsTexture as Texture, TextureLoadAction, TextureSourceType, TextureStoreAction, UniversalRecognizer, constants, getDefaultGPUCapability, getDefaultTextureFactory, setDefaultTextureFactory };
|
5959
6655
|
//# sourceMappingURL=index.mjs.map
|