@eva/plugin-renderer-event 2.1.0-beta.3 → 2.1.0-beta.4
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/EVA.plugin.renderer.event.js +251 -0
- package/dist/EVA.plugin.renderer.event.min.js +1 -0
- package/dist/plugin-renderer-event.cjs.js +366 -0
- package/dist/plugin-renderer-event.cjs.prod.js +1 -0
- package/dist/plugin-renderer-event.d.ts +164 -0
- package/dist/plugin-renderer-event.esm.js +361 -0
- package/package.json +3 -3
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
|
|
2
|
+
globalThis.EVA = globalThis.EVA || {};
|
|
3
|
+
globalThis.EVA.plugin = globalThis.EVA.plugin || {};
|
|
4
|
+
globalThis.EVA.plugin.renderer = globalThis.EVA.plugin.renderer || {};
|
|
5
|
+
var _EVA_IIFE_event = function (exports, pluginRenderer, eva_js, pixi_js) {
|
|
6
|
+
'use strict';
|
|
7
|
+
function __rest(s, e) {
|
|
8
|
+
var t = {};
|
|
9
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
|
|
10
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
11
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
|
|
12
|
+
}
|
|
13
|
+
return t;
|
|
14
|
+
}
|
|
15
|
+
function __decorate(decorators, target, key, desc) {
|
|
16
|
+
var c = arguments.length,
|
|
17
|
+
r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc,
|
|
18
|
+
d;
|
|
19
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
20
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
21
|
+
}
|
|
22
|
+
function __metadata(metadataKey, metadataValue) {
|
|
23
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
|
|
24
|
+
}
|
|
25
|
+
const hitAreaFunc = {
|
|
26
|
+
Circle: pixi_js.Circle,
|
|
27
|
+
Ellipse: pixi_js.Ellipse,
|
|
28
|
+
Polygon: pixi_js.Polygon,
|
|
29
|
+
Rect: pixi_js.Rectangle,
|
|
30
|
+
RoundedRect: pixi_js.RoundedRectangle
|
|
31
|
+
};
|
|
32
|
+
const propertyForHitArea = {
|
|
33
|
+
Circle: ['x', 'y', 'radius'],
|
|
34
|
+
Ellipse: ['x', 'y', 'width', 'height'],
|
|
35
|
+
Rect: ['x', 'y', 'width', 'height'],
|
|
36
|
+
RoundedRect: ['x', 'y', 'width', 'height', 'radius'],
|
|
37
|
+
Polygon: ['paths']
|
|
38
|
+
};
|
|
39
|
+
let Event$1 = class Event extends pluginRenderer.Renderer {
|
|
40
|
+
constructor() {
|
|
41
|
+
super(...arguments);
|
|
42
|
+
this.name = 'EventSystem';
|
|
43
|
+
}
|
|
44
|
+
init(params) {
|
|
45
|
+
var _a;
|
|
46
|
+
const compatibleParams = params;
|
|
47
|
+
const game = this.game || (typeof (compatibleParams === null || compatibleParams === void 0 ? void 0 : compatibleParams.getSystem) === 'function' ? compatibleParams : undefined);
|
|
48
|
+
const renderSystem = (_a = game === null || game === void 0 ? void 0 : game.getSystem) === null || _a === void 0 ? void 0 : _a.call(game, pluginRenderer.RendererSystem);
|
|
49
|
+
if (!(renderSystem === null || renderSystem === void 0 ? void 0 : renderSystem.rendererManager)) return;
|
|
50
|
+
this.renderSystem = renderSystem;
|
|
51
|
+
this.renderSystem.rendererManager.register(this);
|
|
52
|
+
}
|
|
53
|
+
update(frame) {
|
|
54
|
+
super.update(frame);
|
|
55
|
+
}
|
|
56
|
+
componentChanged(changed) {
|
|
57
|
+
switch (changed.type) {
|
|
58
|
+
case eva_js.OBSERVER_TYPE.ADD:
|
|
59
|
+
this.add(changed);
|
|
60
|
+
break;
|
|
61
|
+
case eva_js.OBSERVER_TYPE.REMOVE:
|
|
62
|
+
this.remove(changed);
|
|
63
|
+
break;
|
|
64
|
+
case eva_js.OBSERVER_TYPE.CHANGE:
|
|
65
|
+
this.change(changed);
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
add(changed) {
|
|
70
|
+
var _a;
|
|
71
|
+
const container = (_a = this.containerManager) === null || _a === void 0 ? void 0 : _a.getContainer(changed.gameObject.id);
|
|
72
|
+
if (!container) return;
|
|
73
|
+
container.interactive = true;
|
|
74
|
+
container.interactiveChildren = true;
|
|
75
|
+
const component = changed.component;
|
|
76
|
+
if (component.hitArea) {
|
|
77
|
+
this.addHitArea(changed, container, component.hitArea);
|
|
78
|
+
}
|
|
79
|
+
container.on('pointertap', e => {
|
|
80
|
+
const eventParam = this.createEventParam(container, component, e);
|
|
81
|
+
component.emit('tap', eventParam);
|
|
82
|
+
component.emit('click', eventParam);
|
|
83
|
+
});
|
|
84
|
+
container.on('pointerdown', e => {
|
|
85
|
+
const eventParam = this.createEventParam(container, component, e);
|
|
86
|
+
component.emit('touchstart', eventParam);
|
|
87
|
+
component.emit('mousedown', eventParam);
|
|
88
|
+
});
|
|
89
|
+
container.on('pointermove', e => {
|
|
90
|
+
const eventParam = this.createEventParam(container, component, e);
|
|
91
|
+
component.emit('touchmove', eventParam);
|
|
92
|
+
component.emit('mousemove', eventParam);
|
|
93
|
+
});
|
|
94
|
+
container.on('pointerup', e => {
|
|
95
|
+
const eventParam = this.createEventParam(container, component, e);
|
|
96
|
+
component.emit('touchend', eventParam);
|
|
97
|
+
component.emit('mouseup', eventParam);
|
|
98
|
+
});
|
|
99
|
+
container.on('pointerupoutside', e => {
|
|
100
|
+
component.emit('touchendoutside', this.createEventParam(container, component, e));
|
|
101
|
+
});
|
|
102
|
+
container.on('pointercancel', e => {
|
|
103
|
+
component.emit('touchcancel', this.createEventParam(container, component, e));
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
createEventParam(container, component, e) {
|
|
107
|
+
return {
|
|
108
|
+
stopPropagation: () => e.stopPropagation(),
|
|
109
|
+
data: {
|
|
110
|
+
pointerId: e.data.pointerId,
|
|
111
|
+
position: {
|
|
112
|
+
x: e.data.global.x,
|
|
113
|
+
y: e.data.global.y
|
|
114
|
+
},
|
|
115
|
+
localPosition: container.worldTransform.applyInverse(e.data.global)
|
|
116
|
+
},
|
|
117
|
+
gameObject: component.gameObject
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
remove(changed) {
|
|
121
|
+
var _a;
|
|
122
|
+
const container = (_a = this.containerManager) === null || _a === void 0 ? void 0 : _a.getContainer(changed.gameObject.id);
|
|
123
|
+
if (!container) return;
|
|
124
|
+
container.interactive = false;
|
|
125
|
+
container.off('pointertap');
|
|
126
|
+
container.off('pointerdown');
|
|
127
|
+
container.off('pointermove');
|
|
128
|
+
container.off('pointerup');
|
|
129
|
+
container.off('pointerupoutside');
|
|
130
|
+
container.off('pointercancel');
|
|
131
|
+
changed.component.removeAllListeners();
|
|
132
|
+
}
|
|
133
|
+
change(changed) {
|
|
134
|
+
var _a;
|
|
135
|
+
const container = (_a = this.containerManager) === null || _a === void 0 ? void 0 : _a.getContainer(changed.gameObject.id);
|
|
136
|
+
if (!container) return;
|
|
137
|
+
container.interactive = true;
|
|
138
|
+
const component = changed.component;
|
|
139
|
+
if (component.hitArea) {
|
|
140
|
+
this.addHitArea(changed, container, component.hitArea);
|
|
141
|
+
} else {
|
|
142
|
+
component.hitArea = null;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
addHitArea(changed, container, hitArea) {
|
|
146
|
+
const {
|
|
147
|
+
type,
|
|
148
|
+
style
|
|
149
|
+
} = hitArea;
|
|
150
|
+
if (!hitAreaFunc[type]) {
|
|
151
|
+
console.error(`${changed.gameObject.name}'s hitArea type is not defined`);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const params = [];
|
|
155
|
+
for (const key of propertyForHitArea[type]) {
|
|
156
|
+
params.push(style[key]);
|
|
157
|
+
}
|
|
158
|
+
const hitAreaShape = new hitAreaFunc[type](...params);
|
|
159
|
+
container.hitArea = hitAreaShape;
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
Event$1.systemName = 'EventSystem';
|
|
163
|
+
Event$1 = __decorate([eva_js.decorators.componentObserver({
|
|
164
|
+
Event: [{
|
|
165
|
+
prop: ['hitArea'],
|
|
166
|
+
deep: true
|
|
167
|
+
}]
|
|
168
|
+
})], Event$1);
|
|
169
|
+
var Event$2 = Event$1;
|
|
170
|
+
exports.HIT_AREA_TYPE = void 0;
|
|
171
|
+
(function (HIT_AREA_TYPE) {
|
|
172
|
+
HIT_AREA_TYPE["Circle"] = "Circle";
|
|
173
|
+
HIT_AREA_TYPE["Ellipse"] = "Ellipse";
|
|
174
|
+
HIT_AREA_TYPE["Polygon"] = "Polygon";
|
|
175
|
+
HIT_AREA_TYPE["Rect"] = "Rect";
|
|
176
|
+
HIT_AREA_TYPE["RoundedRect"] = "RoundedRect";
|
|
177
|
+
})(exports.HIT_AREA_TYPE || (exports.HIT_AREA_TYPE = {}));
|
|
178
|
+
class HitAreaStyleMetadata {}
|
|
179
|
+
__decorate([eva_js.type('number'), eva_js.step(1), __metadata("design:type", Number)], HitAreaStyleMetadata.prototype, "x", void 0);
|
|
180
|
+
__decorate([eva_js.type('number'), eva_js.step(1), __metadata("design:type", Number)], HitAreaStyleMetadata.prototype, "y", void 0);
|
|
181
|
+
__decorate([eva_js.type('number'), eva_js.step(1), __metadata("design:type", Number)], HitAreaStyleMetadata.prototype, "radius", void 0);
|
|
182
|
+
__decorate([eva_js.type('number'), eva_js.step(1), __metadata("design:type", Number)], HitAreaStyleMetadata.prototype, "width", void 0);
|
|
183
|
+
__decorate([eva_js.type('number'), eva_js.step(1), __metadata("design:type", Number)], HitAreaStyleMetadata.prototype, "height", void 0);
|
|
184
|
+
__decorate([eva_js.type('number'), __metadata("design:type", Array)], HitAreaStyleMetadata.prototype, "paths", void 0);
|
|
185
|
+
class HitAreaMetadata {}
|
|
186
|
+
__decorate([eva_js.type('string'), __metadata("design:type", String)], HitAreaMetadata.prototype, "type", void 0);
|
|
187
|
+
__decorate([eva_js.Field(() => HitAreaStyleMetadata), __metadata("design:type", Object)], HitAreaMetadata.prototype, "style", void 0);
|
|
188
|
+
class Event extends eva_js.Component {
|
|
189
|
+
constructor(params) {
|
|
190
|
+
super(params);
|
|
191
|
+
this.hitArea = undefined;
|
|
192
|
+
this.configuredEventHandlers = [];
|
|
193
|
+
this.init(params);
|
|
194
|
+
}
|
|
195
|
+
init(params) {
|
|
196
|
+
this.removeConfiguredEventHandlers();
|
|
197
|
+
if (!params) return;
|
|
198
|
+
const {
|
|
199
|
+
on
|
|
200
|
+
} = params,
|
|
201
|
+
options = __rest(params, ["on"]);
|
|
202
|
+
_extends(this, options);
|
|
203
|
+
if (on) {
|
|
204
|
+
for (const eventName of Object.keys(on)) {
|
|
205
|
+
const handler = on[eventName];
|
|
206
|
+
if (typeof handler !== 'function') continue;
|
|
207
|
+
super.on(eventName, handler);
|
|
208
|
+
this.configuredEventHandlers.push({
|
|
209
|
+
eventName,
|
|
210
|
+
handler
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
emit(en, ...args) {
|
|
216
|
+
return super.emit(en, ...args);
|
|
217
|
+
}
|
|
218
|
+
once(en, fn, context) {
|
|
219
|
+
return super.once(en, fn, context);
|
|
220
|
+
}
|
|
221
|
+
on(en, fn, context) {
|
|
222
|
+
return super.on(en, fn, context);
|
|
223
|
+
}
|
|
224
|
+
destroy() {
|
|
225
|
+
this.removeAllListeners();
|
|
226
|
+
this.configuredEventHandlers = [];
|
|
227
|
+
return this;
|
|
228
|
+
}
|
|
229
|
+
onDestroy() {
|
|
230
|
+
this.destroy();
|
|
231
|
+
}
|
|
232
|
+
removeConfiguredEventHandlers() {
|
|
233
|
+
for (const {
|
|
234
|
+
eventName,
|
|
235
|
+
handler
|
|
236
|
+
} of this.configuredEventHandlers) {
|
|
237
|
+
super.off(eventName, handler);
|
|
238
|
+
}
|
|
239
|
+
this.configuredEventHandlers = [];
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
Event.componentName = 'Event';
|
|
243
|
+
__decorate([eva_js.Field(() => HitAreaMetadata), __metadata("design:type", Object)], Event.prototype, "hitArea", void 0);
|
|
244
|
+
exports.Event = Event;
|
|
245
|
+
exports.EventSystem = Event$2;
|
|
246
|
+
Object.defineProperty(exports, '__esModule', {
|
|
247
|
+
value: true
|
|
248
|
+
});
|
|
249
|
+
return exports;
|
|
250
|
+
}({}, EVA.plugin.renderer, EVA, PIXI);
|
|
251
|
+
globalThis.EVA.plugin.renderer.event = globalThis.EVA.plugin.renderer.event || _EVA_IIFE_event;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function _extends(){return _extends=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)({}).hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},_extends.apply(null,arguments)}globalThis.EVA=globalThis.EVA||{},globalThis.EVA.plugin=globalThis.EVA.plugin||{},globalThis.EVA.plugin.renderer=globalThis.EVA.plugin.renderer||{};var _EVA_IIFE_event=function(e,t,n,r){"use strict";function o(e,t,n,r){var o,i=arguments.length,s=i<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,n,r);else for(var a=e.length-1;a>=0;a--)(o=e[a])&&(s=(i<3?o(s):i>3?o(t,n,s):o(t,n))||s);return i>3&&s&&Object.defineProperty(t,n,s),s}function i(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}const s={Circle:r.Circle,Ellipse:r.Ellipse,Polygon:r.Polygon,Rect:r.Rectangle,RoundedRect:r.RoundedRectangle},a={Circle:["x","y","radius"],Ellipse:["x","y","width","height"],Rect:["x","y","width","height"],RoundedRect:["x","y","width","height","radius"],Polygon:["paths"]};let c=class extends t.Renderer{constructor(){super(...arguments),this.name="EventSystem"}init(e){var n;const r=e,o=this.game||("function"==typeof(null==r?void 0:r.getSystem)?r:void 0),i=null===(n=null==o?void 0:o.getSystem)||void 0===n?void 0:n.call(o,t.RendererSystem);(null==i?void 0:i.rendererManager)&&(this.renderSystem=i,this.renderSystem.rendererManager.register(this))}update(e){super.update(e)}componentChanged(e){switch(e.type){case n.OBSERVER_TYPE.ADD:this.add(e);break;case n.OBSERVER_TYPE.REMOVE:this.remove(e);break;case n.OBSERVER_TYPE.CHANGE:this.change(e)}}add(e){var t;const n=null===(t=this.containerManager)||void 0===t?void 0:t.getContainer(e.gameObject.id);if(!n)return;n.interactive=!0,n.interactiveChildren=!0;const r=e.component;r.hitArea&&this.addHitArea(e,n,r.hitArea),n.on("pointertap",e=>{const t=this.createEventParam(n,r,e);r.emit("tap",t),r.emit("click",t)}),n.on("pointerdown",e=>{const t=this.createEventParam(n,r,e);r.emit("touchstart",t),r.emit("mousedown",t)}),n.on("pointermove",e=>{const t=this.createEventParam(n,r,e);r.emit("touchmove",t),r.emit("mousemove",t)}),n.on("pointerup",e=>{const t=this.createEventParam(n,r,e);r.emit("touchend",t),r.emit("mouseup",t)}),n.on("pointerupoutside",e=>{r.emit("touchendoutside",this.createEventParam(n,r,e))}),n.on("pointercancel",e=>{r.emit("touchcancel",this.createEventParam(n,r,e))})}createEventParam(e,t,n){return{stopPropagation:()=>n.stopPropagation(),data:{pointerId:n.data.pointerId,position:{x:n.data.global.x,y:n.data.global.y},localPosition:e.worldTransform.applyInverse(n.data.global)},gameObject:t.gameObject}}remove(e){var t;const n=null===(t=this.containerManager)||void 0===t?void 0:t.getContainer(e.gameObject.id);n&&(n.interactive=!1,n.off("pointertap"),n.off("pointerdown"),n.off("pointermove"),n.off("pointerup"),n.off("pointerupoutside"),n.off("pointercancel"),e.component.removeAllListeners())}change(e){var t;const n=null===(t=this.containerManager)||void 0===t?void 0:t.getContainer(e.gameObject.id);if(!n)return;n.interactive=!0;const r=e.component;r.hitArea?this.addHitArea(e,n,r.hitArea):r.hitArea=null}addHitArea(e,t,n){const{type:r,style:o}=n;if(!s[r])return void console.error(`${e.gameObject.name}'s hitArea type is not defined`);const i=[];for(const e of a[r])i.push(o[e]);const c=new s[r](...i);t.hitArea=c}};c.systemName="EventSystem",c=o([n.decorators.componentObserver({Event:[{prop:["hitArea"],deep:!0}]})],c);var d,p=c;e.HIT_AREA_TYPE=void 0,(d=e.HIT_AREA_TYPE||(e.HIT_AREA_TYPE={})).Circle="Circle",d.Ellipse="Ellipse",d.Polygon="Polygon",d.Rect="Rect",d.RoundedRect="RoundedRect";class l{}o([n.type("number"),n.step(1),i("design:type",Number)],l.prototype,"x",void 0),o([n.type("number"),n.step(1),i("design:type",Number)],l.prototype,"y",void 0),o([n.type("number"),n.step(1),i("design:type",Number)],l.prototype,"radius",void 0),o([n.type("number"),n.step(1),i("design:type",Number)],l.prototype,"width",void 0),o([n.type("number"),n.step(1),i("design:type",Number)],l.prototype,"height",void 0),o([n.type("number"),i("design:type",Array)],l.prototype,"paths",void 0);class u{}o([n.type("string"),i("design:type",String)],u.prototype,"type",void 0),o([n.Field(()=>l),i("design:type",Object)],u.prototype,"style",void 0);class h extends n.Component{constructor(e){super(e),this.hitArea=void 0,this.configuredEventHandlers=[],this.init(e)}init(e){if(this.removeConfiguredEventHandlers(),!e)return;const{on:t}=e;if(_extends(this,function(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(e);o<r.length;o++)t.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(e,r[o])&&(n[r[o]]=e[r[o]])}return n}(e,["on"])),t)for(const e of Object.keys(t)){const n=t[e];"function"==typeof n&&(super.on(e,n),this.configuredEventHandlers.push({eventName:e,handler:n}))}}emit(e,...t){return super.emit(e,...t)}once(e,t,n){return super.once(e,t,n)}on(e,t,n){return super.on(e,t,n)}destroy(){return this.removeAllListeners(),this.configuredEventHandlers=[],this}onDestroy(){this.destroy()}removeConfiguredEventHandlers(){for(const{eventName:e,handler:t}of this.configuredEventHandlers)super.off(e,t);this.configuredEventHandlers=[]}}return h.componentName="Event",o([n.Field(()=>u),i("design:type",Object)],h.prototype,"hitArea",void 0),e.Event=h,e.EventSystem=p,Object.defineProperty(e,"__esModule",{value:!0}),e}({},EVA.plugin.renderer,EVA,PIXI);globalThis.EVA.plugin.renderer.event=globalThis.EVA.plugin.renderer.event||_EVA_IIFE_event;
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var pluginRenderer = require('@eva/plugin-renderer');
|
|
6
|
+
var eva_js = require('@eva/eva.js');
|
|
7
|
+
var pixi_js = require('pixi.js');
|
|
8
|
+
|
|
9
|
+
/******************************************************************************
|
|
10
|
+
Copyright (c) Microsoft Corporation.
|
|
11
|
+
|
|
12
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
13
|
+
purpose with or without fee is hereby granted.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
16
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
17
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
18
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
19
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
20
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
21
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
22
|
+
***************************************************************************** */
|
|
23
|
+
|
|
24
|
+
function __rest(s, e) {
|
|
25
|
+
var t = {};
|
|
26
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
27
|
+
t[p] = s[p];
|
|
28
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
29
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
30
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
31
|
+
t[p[i]] = s[p[i]];
|
|
32
|
+
}
|
|
33
|
+
return t;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function __decorate(decorators, target, key, desc) {
|
|
37
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
38
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
39
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
40
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function __metadata(metadataKey, metadataValue) {
|
|
44
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
48
|
+
var e = new Error(message);
|
|
49
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const hitAreaFunc = {
|
|
53
|
+
Circle: pixi_js.Circle,
|
|
54
|
+
Ellipse: pixi_js.Ellipse,
|
|
55
|
+
Polygon: pixi_js.Polygon,
|
|
56
|
+
Rect: pixi_js.Rectangle,
|
|
57
|
+
RoundedRect: pixi_js.RoundedRectangle,
|
|
58
|
+
};
|
|
59
|
+
const propertyForHitArea = {
|
|
60
|
+
Circle: ['x', 'y', 'radius'],
|
|
61
|
+
Ellipse: ['x', 'y', 'width', 'height'],
|
|
62
|
+
Rect: ['x', 'y', 'width', 'height'],
|
|
63
|
+
RoundedRect: ['x', 'y', 'width', 'height', 'radius'],
|
|
64
|
+
Polygon: ['paths'],
|
|
65
|
+
};
|
|
66
|
+
let Event$1 = class Event extends pluginRenderer.Renderer {
|
|
67
|
+
constructor() {
|
|
68
|
+
super(...arguments);
|
|
69
|
+
this.name = 'EventSystem';
|
|
70
|
+
}
|
|
71
|
+
init(params) {
|
|
72
|
+
var _a;
|
|
73
|
+
const compatibleParams = params;
|
|
74
|
+
const game = this.game || (typeof (compatibleParams === null || compatibleParams === void 0 ? void 0 : compatibleParams.getSystem) === 'function' ? compatibleParams : undefined);
|
|
75
|
+
const renderSystem = (_a = game === null || game === void 0 ? void 0 : game.getSystem) === null || _a === void 0 ? void 0 : _a.call(game, pluginRenderer.RendererSystem);
|
|
76
|
+
if (!(renderSystem === null || renderSystem === void 0 ? void 0 : renderSystem.rendererManager))
|
|
77
|
+
return;
|
|
78
|
+
this.renderSystem = renderSystem;
|
|
79
|
+
this.renderSystem.rendererManager.register(this);
|
|
80
|
+
}
|
|
81
|
+
update(frame) {
|
|
82
|
+
super.update(frame);
|
|
83
|
+
}
|
|
84
|
+
componentChanged(changed) {
|
|
85
|
+
switch (changed.type) {
|
|
86
|
+
case eva_js.OBSERVER_TYPE.ADD:
|
|
87
|
+
this.add(changed);
|
|
88
|
+
break;
|
|
89
|
+
case eva_js.OBSERVER_TYPE.REMOVE:
|
|
90
|
+
this.remove(changed);
|
|
91
|
+
break;
|
|
92
|
+
case eva_js.OBSERVER_TYPE.CHANGE:
|
|
93
|
+
this.change(changed);
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
add(changed) {
|
|
98
|
+
var _a;
|
|
99
|
+
const container = (_a = this.containerManager) === null || _a === void 0 ? void 0 : _a.getContainer(changed.gameObject.id);
|
|
100
|
+
if (!container)
|
|
101
|
+
return;
|
|
102
|
+
container.interactive = true;
|
|
103
|
+
container.interactiveChildren = true;
|
|
104
|
+
const component = changed.component;
|
|
105
|
+
if (component.hitArea) {
|
|
106
|
+
this.addHitArea(changed, container, component.hitArea);
|
|
107
|
+
}
|
|
108
|
+
container.on('pointertap', e => {
|
|
109
|
+
const eventParam = this.createEventParam(container, component, e);
|
|
110
|
+
component.emit('tap', eventParam);
|
|
111
|
+
component.emit('click', eventParam);
|
|
112
|
+
});
|
|
113
|
+
container.on('pointerdown', e => {
|
|
114
|
+
const eventParam = this.createEventParam(container, component, e);
|
|
115
|
+
component.emit('touchstart', eventParam);
|
|
116
|
+
component.emit('mousedown', eventParam);
|
|
117
|
+
});
|
|
118
|
+
container.on('pointermove', e => {
|
|
119
|
+
const eventParam = this.createEventParam(container, component, e);
|
|
120
|
+
component.emit('touchmove', eventParam);
|
|
121
|
+
component.emit('mousemove', eventParam);
|
|
122
|
+
});
|
|
123
|
+
container.on('pointerup', e => {
|
|
124
|
+
const eventParam = this.createEventParam(container, component, e);
|
|
125
|
+
component.emit('touchend', eventParam);
|
|
126
|
+
component.emit('mouseup', eventParam);
|
|
127
|
+
});
|
|
128
|
+
container.on('pointerupoutside', e => {
|
|
129
|
+
component.emit('touchendoutside', this.createEventParam(container, component, e));
|
|
130
|
+
});
|
|
131
|
+
container.on('pointercancel', e => {
|
|
132
|
+
component.emit('touchcancel', this.createEventParam(container, component, e));
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
createEventParam(container, component, e) {
|
|
136
|
+
return {
|
|
137
|
+
stopPropagation: () => e.stopPropagation(),
|
|
138
|
+
data: {
|
|
139
|
+
// @ts-ignore
|
|
140
|
+
pointerId: e.data.pointerId,
|
|
141
|
+
position: {
|
|
142
|
+
x: e.data.global.x,
|
|
143
|
+
y: e.data.global.y,
|
|
144
|
+
},
|
|
145
|
+
localPosition: container.worldTransform.applyInverse(e.data.global),
|
|
146
|
+
},
|
|
147
|
+
gameObject: component.gameObject,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
remove(changed) {
|
|
151
|
+
var _a;
|
|
152
|
+
const container = (_a = this.containerManager) === null || _a === void 0 ? void 0 : _a.getContainer(changed.gameObject.id);
|
|
153
|
+
if (!container)
|
|
154
|
+
return;
|
|
155
|
+
container.interactive = false;
|
|
156
|
+
container.off('pointertap');
|
|
157
|
+
container.off('pointerdown');
|
|
158
|
+
container.off('pointermove');
|
|
159
|
+
container.off('pointerup');
|
|
160
|
+
container.off('pointerupoutside');
|
|
161
|
+
container.off('pointercancel');
|
|
162
|
+
changed.component.removeAllListeners();
|
|
163
|
+
}
|
|
164
|
+
change(changed) {
|
|
165
|
+
var _a;
|
|
166
|
+
const container = (_a = this.containerManager) === null || _a === void 0 ? void 0 : _a.getContainer(changed.gameObject.id);
|
|
167
|
+
if (!container)
|
|
168
|
+
return;
|
|
169
|
+
container.interactive = true;
|
|
170
|
+
const component = changed.component;
|
|
171
|
+
if (component.hitArea) {
|
|
172
|
+
this.addHitArea(changed, container, component.hitArea);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
component.hitArea = null;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
addHitArea(changed, container, hitArea) {
|
|
179
|
+
const { type, style } = hitArea;
|
|
180
|
+
if (!hitAreaFunc[type]) {
|
|
181
|
+
console.error(`${changed.gameObject.name}'s hitArea type is not defined`);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const params = [];
|
|
185
|
+
for (const key of propertyForHitArea[type]) {
|
|
186
|
+
params.push(style[key]);
|
|
187
|
+
}
|
|
188
|
+
const hitAreaShape = new hitAreaFunc[type](...params);
|
|
189
|
+
container.hitArea = hitAreaShape;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
Event$1.systemName = 'EventSystem';
|
|
193
|
+
Event$1 = __decorate([
|
|
194
|
+
eva_js.decorators.componentObserver({
|
|
195
|
+
Event: [{ prop: ['hitArea'], deep: true }],
|
|
196
|
+
})
|
|
197
|
+
], Event$1);
|
|
198
|
+
var Event$2 = Event$1;
|
|
199
|
+
|
|
200
|
+
exports.HIT_AREA_TYPE = void 0;
|
|
201
|
+
(function (HIT_AREA_TYPE) {
|
|
202
|
+
HIT_AREA_TYPE["Circle"] = "Circle";
|
|
203
|
+
HIT_AREA_TYPE["Ellipse"] = "Ellipse";
|
|
204
|
+
HIT_AREA_TYPE["Polygon"] = "Polygon";
|
|
205
|
+
HIT_AREA_TYPE["Rect"] = "Rect";
|
|
206
|
+
HIT_AREA_TYPE["RoundedRect"] = "RoundedRect";
|
|
207
|
+
})(exports.HIT_AREA_TYPE || (exports.HIT_AREA_TYPE = {}));
|
|
208
|
+
class HitAreaStyleMetadata {
|
|
209
|
+
}
|
|
210
|
+
__decorate([
|
|
211
|
+
eva_js.type('number'),
|
|
212
|
+
eva_js.step(1),
|
|
213
|
+
__metadata("design:type", Number)
|
|
214
|
+
], HitAreaStyleMetadata.prototype, "x", void 0);
|
|
215
|
+
__decorate([
|
|
216
|
+
eva_js.type('number'),
|
|
217
|
+
eva_js.step(1),
|
|
218
|
+
__metadata("design:type", Number)
|
|
219
|
+
], HitAreaStyleMetadata.prototype, "y", void 0);
|
|
220
|
+
__decorate([
|
|
221
|
+
eva_js.type('number'),
|
|
222
|
+
eva_js.step(1),
|
|
223
|
+
__metadata("design:type", Number)
|
|
224
|
+
], HitAreaStyleMetadata.prototype, "radius", void 0);
|
|
225
|
+
__decorate([
|
|
226
|
+
eva_js.type('number'),
|
|
227
|
+
eva_js.step(1),
|
|
228
|
+
__metadata("design:type", Number)
|
|
229
|
+
], HitAreaStyleMetadata.prototype, "width", void 0);
|
|
230
|
+
__decorate([
|
|
231
|
+
eva_js.type('number'),
|
|
232
|
+
eva_js.step(1),
|
|
233
|
+
__metadata("design:type", Number)
|
|
234
|
+
], HitAreaStyleMetadata.prototype, "height", void 0);
|
|
235
|
+
__decorate([
|
|
236
|
+
eva_js.type('number'),
|
|
237
|
+
__metadata("design:type", Array)
|
|
238
|
+
], HitAreaStyleMetadata.prototype, "paths", void 0);
|
|
239
|
+
class HitAreaMetadata {
|
|
240
|
+
}
|
|
241
|
+
__decorate([
|
|
242
|
+
eva_js.type('string'),
|
|
243
|
+
__metadata("design:type", String)
|
|
244
|
+
], HitAreaMetadata.prototype, "type", void 0);
|
|
245
|
+
__decorate([
|
|
246
|
+
eva_js.Field(() => HitAreaStyleMetadata),
|
|
247
|
+
__metadata("design:type", Object)
|
|
248
|
+
], HitAreaMetadata.prototype, "style", void 0);
|
|
249
|
+
/**
|
|
250
|
+
* 事件组件
|
|
251
|
+
*
|
|
252
|
+
* Event 组件为游戏对象添加交互能力,使其能够响应触摸和鼠标事件。
|
|
253
|
+
* 可以自定义交互热区的形状(矩形、圆形、多边形等),
|
|
254
|
+
* 适用于按钮、拖拽对象、可点击元素等交互场景。
|
|
255
|
+
*
|
|
256
|
+
* 支持的事件类型:
|
|
257
|
+
* - touchstart - 触摸/点击开始
|
|
258
|
+
* - touchmove - 触摸/鼠标移动
|
|
259
|
+
* - touchend - 触摸/点击结束
|
|
260
|
+
* - tap - 点击/轻触
|
|
261
|
+
* - touchendoutside - 在对象外部结束触摸
|
|
262
|
+
* - touchcancel - 触摸取消
|
|
263
|
+
*
|
|
264
|
+
* @example
|
|
265
|
+
* ```typescript
|
|
266
|
+
* // 基础用法 - 使用默认热区(Transform 尺寸)
|
|
267
|
+
* const button = new GameObject('button');
|
|
268
|
+
* const event = new Event();
|
|
269
|
+
* button.addComponent(event);
|
|
270
|
+
*
|
|
271
|
+
* event.on('tap', (e) => {
|
|
272
|
+
* console.log('按钮被点击', e.data.position);
|
|
273
|
+
* });
|
|
274
|
+
*
|
|
275
|
+
* // 自定义矩形热区
|
|
276
|
+
* button.addComponent(new Event({
|
|
277
|
+
* hitArea: {
|
|
278
|
+
* type: HIT_AREA_TYPE.Rect,
|
|
279
|
+
* style: { x: 0, y: 0, width: 100, height: 50 }
|
|
280
|
+
* }
|
|
281
|
+
* }));
|
|
282
|
+
*
|
|
283
|
+
* // 圆形热区
|
|
284
|
+
* button.addComponent(new Event({
|
|
285
|
+
* hitArea: {
|
|
286
|
+
* type: HIT_AREA_TYPE.Circle,
|
|
287
|
+
* style: { x: 50, y: 50, radius: 40 }
|
|
288
|
+
* }
|
|
289
|
+
* }));
|
|
290
|
+
*
|
|
291
|
+
* // 多边形热区
|
|
292
|
+
* button.addComponent(new Event({
|
|
293
|
+
* hitArea: {
|
|
294
|
+
* type: HIT_AREA_TYPE.Polygon,
|
|
295
|
+
* style: { paths: [0,0, 100,0, 50,100] }
|
|
296
|
+
* }
|
|
297
|
+
* }));
|
|
298
|
+
*
|
|
299
|
+
* // 事件处理
|
|
300
|
+
* event.on('touchstart', (e) => {
|
|
301
|
+
* console.log('触摸开始', e.data.localPosition);
|
|
302
|
+
* e.stopPropagation(); // 阻止事件冒泡
|
|
303
|
+
* });
|
|
304
|
+
* ```
|
|
305
|
+
*/
|
|
306
|
+
class Event extends eva_js.Component {
|
|
307
|
+
constructor(params) {
|
|
308
|
+
super(params);
|
|
309
|
+
/** 交互热区配置 */
|
|
310
|
+
this.hitArea = undefined;
|
|
311
|
+
this.configuredEventHandlers = [];
|
|
312
|
+
this.init(params);
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* 初始化组件
|
|
316
|
+
* @param params - 初始化参数
|
|
317
|
+
*/
|
|
318
|
+
init(params) {
|
|
319
|
+
this.removeConfiguredEventHandlers();
|
|
320
|
+
if (!params)
|
|
321
|
+
return;
|
|
322
|
+
const { on } = params, options = __rest(params, ["on"]);
|
|
323
|
+
Object.assign(this, options);
|
|
324
|
+
if (on) {
|
|
325
|
+
for (const eventName of Object.keys(on)) {
|
|
326
|
+
const handler = on[eventName];
|
|
327
|
+
if (typeof handler !== 'function')
|
|
328
|
+
continue;
|
|
329
|
+
super.on(eventName, handler);
|
|
330
|
+
this.configuredEventHandlers.push({ eventName, handler });
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
emit(en, ...args) {
|
|
335
|
+
return super.emit(en, ...args);
|
|
336
|
+
}
|
|
337
|
+
once(en, fn, context) {
|
|
338
|
+
return super.once(en, fn, context);
|
|
339
|
+
}
|
|
340
|
+
on(en, fn, context) {
|
|
341
|
+
return super.on(en, fn, context);
|
|
342
|
+
}
|
|
343
|
+
destroy() {
|
|
344
|
+
this.removeAllListeners();
|
|
345
|
+
this.configuredEventHandlers = [];
|
|
346
|
+
return this;
|
|
347
|
+
}
|
|
348
|
+
onDestroy() {
|
|
349
|
+
this.destroy();
|
|
350
|
+
}
|
|
351
|
+
removeConfiguredEventHandlers() {
|
|
352
|
+
for (const { eventName, handler } of this.configuredEventHandlers) {
|
|
353
|
+
super.off(eventName, handler);
|
|
354
|
+
}
|
|
355
|
+
this.configuredEventHandlers = [];
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
/** 组件名称 */
|
|
359
|
+
Event.componentName = 'Event';
|
|
360
|
+
__decorate([
|
|
361
|
+
eva_js.Field(() => HitAreaMetadata),
|
|
362
|
+
__metadata("design:type", Object)
|
|
363
|
+
], Event.prototype, "hitArea", void 0);
|
|
364
|
+
|
|
365
|
+
exports.Event = Event;
|
|
366
|
+
exports.EventSystem = Event$2;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@eva/plugin-renderer"),t=require("@eva/eva.js"),r=require("pixi.js");function o(e,t,r,o){var n,i=arguments.length,s=i<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,r):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,r,o);else for(var a=e.length-1;a>=0;a--)(n=e[a])&&(s=(i<3?n(s):i>3?n(t,r,s):n(t,r))||s);return i>3&&s&&Object.defineProperty(t,r,s),s}function n(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}"function"==typeof SuppressedError&&SuppressedError;const i={Circle:r.Circle,Ellipse:r.Ellipse,Polygon:r.Polygon,Rect:r.Rectangle,RoundedRect:r.RoundedRectangle},s={Circle:["x","y","radius"],Ellipse:["x","y","width","height"],Rect:["x","y","width","height"],RoundedRect:["x","y","width","height","radius"],Polygon:["paths"]};let a=class extends e.Renderer{constructor(){super(...arguments),this.name="EventSystem"}init(t){var r;const o=t,n=this.game||("function"==typeof(null==o?void 0:o.getSystem)?o:void 0),i=null===(r=null==n?void 0:n.getSystem)||void 0===r?void 0:r.call(n,e.RendererSystem);(null==i?void 0:i.rendererManager)&&(this.renderSystem=i,this.renderSystem.rendererManager.register(this))}update(e){super.update(e)}componentChanged(e){switch(e.type){case t.OBSERVER_TYPE.ADD:this.add(e);break;case t.OBSERVER_TYPE.REMOVE:this.remove(e);break;case t.OBSERVER_TYPE.CHANGE:this.change(e)}}add(e){var t;const r=null===(t=this.containerManager)||void 0===t?void 0:t.getContainer(e.gameObject.id);if(!r)return;r.interactive=!0,r.interactiveChildren=!0;const o=e.component;o.hitArea&&this.addHitArea(e,r,o.hitArea),r.on("pointertap",e=>{const t=this.createEventParam(r,o,e);o.emit("tap",t),o.emit("click",t)}),r.on("pointerdown",e=>{const t=this.createEventParam(r,o,e);o.emit("touchstart",t),o.emit("mousedown",t)}),r.on("pointermove",e=>{const t=this.createEventParam(r,o,e);o.emit("touchmove",t),o.emit("mousemove",t)}),r.on("pointerup",e=>{const t=this.createEventParam(r,o,e);o.emit("touchend",t),o.emit("mouseup",t)}),r.on("pointerupoutside",e=>{o.emit("touchendoutside",this.createEventParam(r,o,e))}),r.on("pointercancel",e=>{o.emit("touchcancel",this.createEventParam(r,o,e))})}createEventParam(e,t,r){return{stopPropagation:()=>r.stopPropagation(),data:{pointerId:r.data.pointerId,position:{x:r.data.global.x,y:r.data.global.y},localPosition:e.worldTransform.applyInverse(r.data.global)},gameObject:t.gameObject}}remove(e){var t;const r=null===(t=this.containerManager)||void 0===t?void 0:t.getContainer(e.gameObject.id);r&&(r.interactive=!1,r.off("pointertap"),r.off("pointerdown"),r.off("pointermove"),r.off("pointerup"),r.off("pointerupoutside"),r.off("pointercancel"),e.component.removeAllListeners())}change(e){var t;const r=null===(t=this.containerManager)||void 0===t?void 0:t.getContainer(e.gameObject.id);if(!r)return;r.interactive=!0;const o=e.component;o.hitArea?this.addHitArea(e,r,o.hitArea):o.hitArea=null}addHitArea(e,t,r){const{type:o,style:n}=r;if(!i[o])return void console.error(`${e.gameObject.name}'s hitArea type is not defined`);const a=[];for(const e of s[o])a.push(n[e]);const p=new i[o](...a);t.hitArea=p}};a.systemName="EventSystem",a=o([t.decorators.componentObserver({Event:[{prop:["hitArea"],deep:!0}]})],a);var p,c=a;exports.HIT_AREA_TYPE=void 0,(p=exports.HIT_AREA_TYPE||(exports.HIT_AREA_TYPE={})).Circle="Circle",p.Ellipse="Ellipse",p.Polygon="Polygon",p.Rect="Rect",p.RoundedRect="RoundedRect";class d{}o([t.type("number"),t.step(1),n("design:type",Number)],d.prototype,"x",void 0),o([t.type("number"),t.step(1),n("design:type",Number)],d.prototype,"y",void 0),o([t.type("number"),t.step(1),n("design:type",Number)],d.prototype,"radius",void 0),o([t.type("number"),t.step(1),n("design:type",Number)],d.prototype,"width",void 0),o([t.type("number"),t.step(1),n("design:type",Number)],d.prototype,"height",void 0),o([t.type("number"),n("design:type",Array)],d.prototype,"paths",void 0);class l{}o([t.type("string"),n("design:type",String)],l.prototype,"type",void 0),o([t.Field(()=>d),n("design:type",Object)],l.prototype,"style",void 0);class u extends t.Component{constructor(e){super(e),this.hitArea=void 0,this.configuredEventHandlers=[],this.init(e)}init(e){if(this.removeConfiguredEventHandlers(),!e)return;const{on:t}=e,r=function(e,t){var r={};for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&t.indexOf(o)<0&&(r[o]=e[o]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(o=Object.getOwnPropertySymbols(e);n<o.length;n++)t.indexOf(o[n])<0&&Object.prototype.propertyIsEnumerable.call(e,o[n])&&(r[o[n]]=e[o[n]])}return r}(e,["on"]);if(Object.assign(this,r),t)for(const e of Object.keys(t)){const r=t[e];"function"==typeof r&&(super.on(e,r),this.configuredEventHandlers.push({eventName:e,handler:r}))}}emit(e,...t){return super.emit(e,...t)}once(e,t,r){return super.once(e,t,r)}on(e,t,r){return super.on(e,t,r)}destroy(){return this.removeAllListeners(),this.configuredEventHandlers=[],this}onDestroy(){this.destroy()}removeConfiguredEventHandlers(){for(const{eventName:e,handler:t}of this.configuredEventHandlers)super.off(e,t);this.configuredEventHandlers=[]}}u.componentName="Event",o([t.Field(()=>l),n("design:type",Object)],u.prototype,"hitArea",void 0),exports.Event=u,exports.EventSystem=c;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { Component } from '@eva/eva.js';
|
|
2
|
+
import { ComponentChanged } from '@eva/eva.js';
|
|
3
|
+
import { ContainerManager } from '@eva/plugin-renderer';
|
|
4
|
+
import type { GameObject } from '@eva/eva.js';
|
|
5
|
+
import { Renderer } from '@eva/plugin-renderer';
|
|
6
|
+
import { RendererManager } from '@eva/plugin-renderer';
|
|
7
|
+
import { RendererSystem } from '@eva/plugin-renderer';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 事件组件
|
|
11
|
+
*
|
|
12
|
+
* Event 组件为游戏对象添加交互能力,使其能够响应触摸和鼠标事件。
|
|
13
|
+
* 可以自定义交互热区的形状(矩形、圆形、多边形等),
|
|
14
|
+
* 适用于按钮、拖拽对象、可点击元素等交互场景。
|
|
15
|
+
*
|
|
16
|
+
* 支持的事件类型:
|
|
17
|
+
* - touchstart - 触摸/点击开始
|
|
18
|
+
* - touchmove - 触摸/鼠标移动
|
|
19
|
+
* - touchend - 触摸/点击结束
|
|
20
|
+
* - tap - 点击/轻触
|
|
21
|
+
* - touchendoutside - 在对象外部结束触摸
|
|
22
|
+
* - touchcancel - 触摸取消
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // 基础用法 - 使用默认热区(Transform 尺寸)
|
|
27
|
+
* const button = new GameObject('button');
|
|
28
|
+
* const event = new Event();
|
|
29
|
+
* button.addComponent(event);
|
|
30
|
+
*
|
|
31
|
+
* event.on('tap', (e) => {
|
|
32
|
+
* console.log('按钮被点击', e.data.position);
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // 自定义矩形热区
|
|
36
|
+
* button.addComponent(new Event({
|
|
37
|
+
* hitArea: {
|
|
38
|
+
* type: HIT_AREA_TYPE.Rect,
|
|
39
|
+
* style: { x: 0, y: 0, width: 100, height: 50 }
|
|
40
|
+
* }
|
|
41
|
+
* }));
|
|
42
|
+
*
|
|
43
|
+
* // 圆形热区
|
|
44
|
+
* button.addComponent(new Event({
|
|
45
|
+
* hitArea: {
|
|
46
|
+
* type: HIT_AREA_TYPE.Circle,
|
|
47
|
+
* style: { x: 50, y: 50, radius: 40 }
|
|
48
|
+
* }
|
|
49
|
+
* }));
|
|
50
|
+
*
|
|
51
|
+
* // 多边形热区
|
|
52
|
+
* button.addComponent(new Event({
|
|
53
|
+
* hitArea: {
|
|
54
|
+
* type: HIT_AREA_TYPE.Polygon,
|
|
55
|
+
* style: { paths: [0,0, 100,0, 50,100] }
|
|
56
|
+
* }
|
|
57
|
+
* }));
|
|
58
|
+
*
|
|
59
|
+
* // 事件处理
|
|
60
|
+
* event.on('touchstart', (e) => {
|
|
61
|
+
* console.log('触摸开始', e.data.localPosition);
|
|
62
|
+
* e.stopPropagation(); // 阻止事件冒泡
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
declare class Event_2 extends Component<EventParams> {
|
|
67
|
+
/** 组件名称 */
|
|
68
|
+
static componentName: string;
|
|
69
|
+
/** 交互热区配置 */
|
|
70
|
+
hitArea: HitArea;
|
|
71
|
+
interactive?: boolean;
|
|
72
|
+
cursor?: string;
|
|
73
|
+
stopPropagation?: boolean;
|
|
74
|
+
private configuredEventHandlers;
|
|
75
|
+
constructor(params?: EventParams);
|
|
76
|
+
/**
|
|
77
|
+
* 初始化组件
|
|
78
|
+
* @param params - 初始化参数
|
|
79
|
+
*/
|
|
80
|
+
init(params?: EventParams): void;
|
|
81
|
+
emit(eventName: TouchEventName, ...args: [EventParam]): boolean;
|
|
82
|
+
emit<T extends string>(eventName: Exclude<T, TouchEventName>, ...args: any[]): boolean;
|
|
83
|
+
once(eventName: TouchEventName, fn: (arg: EventParam) => void, context?: any): this;
|
|
84
|
+
once<T extends string>(eventName: Exclude<T, TouchEventName>, fn: (...args: any[]) => void, context?: any): this;
|
|
85
|
+
on(eventName: TouchEventName, fn: (arg: EventParam) => void, context?: any): this;
|
|
86
|
+
on<T extends string>(eventName: Exclude<T, TouchEventName>, fn: (...args: any[]) => void, context?: any): this;
|
|
87
|
+
destroy(): this;
|
|
88
|
+
onDestroy(): void;
|
|
89
|
+
private removeConfiguredEventHandlers;
|
|
90
|
+
}
|
|
91
|
+
export { Event_2 as Event }
|
|
92
|
+
|
|
93
|
+
declare type EventParam = {
|
|
94
|
+
stopPropagation: () => void;
|
|
95
|
+
data: {
|
|
96
|
+
pointerId: number;
|
|
97
|
+
position: {
|
|
98
|
+
x: number;
|
|
99
|
+
y: number;
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* The position related to event target gameobject
|
|
103
|
+
*/
|
|
104
|
+
localPosition: {
|
|
105
|
+
x: number;
|
|
106
|
+
y: number;
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
gameObject: GameObject;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export declare interface EventParams {
|
|
113
|
+
interactive?: boolean;
|
|
114
|
+
cursor?: string;
|
|
115
|
+
stopPropagation?: boolean;
|
|
116
|
+
hitArea?: HitArea;
|
|
117
|
+
on?: Record<string, (...args: any[]) => void>;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export declare class EventSystem extends Renderer<EventSystemParams> {
|
|
121
|
+
static systemName: string;
|
|
122
|
+
name: string;
|
|
123
|
+
renderSystem: RendererSystem;
|
|
124
|
+
rendererManager: RendererManager;
|
|
125
|
+
containerManager: ContainerManager;
|
|
126
|
+
init(params?: EventSystemParams | {
|
|
127
|
+
getSystem?: (...args: any[]) => unknown;
|
|
128
|
+
}): void;
|
|
129
|
+
update(frame?: unknown): void;
|
|
130
|
+
componentChanged(changed: ComponentChanged): void;
|
|
131
|
+
add(changed: ComponentChanged): void;
|
|
132
|
+
private createEventParam;
|
|
133
|
+
remove(changed: ComponentChanged): void;
|
|
134
|
+
change(changed: ComponentChanged): void;
|
|
135
|
+
addHitArea(changed: ComponentChanged, container: any, hitArea: any): void;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export declare interface EventSystemParams {
|
|
139
|
+
moveWhenInside?: boolean;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export declare enum HIT_AREA_TYPE {
|
|
143
|
+
Circle = "Circle",
|
|
144
|
+
Ellipse = "Ellipse",
|
|
145
|
+
Polygon = "Polygon",
|
|
146
|
+
Rect = "Rect",
|
|
147
|
+
RoundedRect = "RoundedRect"
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
declare interface HitArea {
|
|
151
|
+
type: HIT_AREA_TYPE;
|
|
152
|
+
style?: {
|
|
153
|
+
x?: number;
|
|
154
|
+
y?: number;
|
|
155
|
+
radius?: number;
|
|
156
|
+
width?: number;
|
|
157
|
+
height?: number;
|
|
158
|
+
paths?: number[];
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
declare type TouchEventName = 'touchstart' | 'touchmove' | 'touchend' | 'tap' | 'touchendoutside' | 'touchcancel';
|
|
163
|
+
|
|
164
|
+
export { }
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import { Renderer, RendererSystem } from '@eva/plugin-renderer';
|
|
2
|
+
import { OBSERVER_TYPE, decorators, Component, type, step, Field } from '@eva/eva.js';
|
|
3
|
+
import { Circle, Ellipse, Polygon, Rectangle, RoundedRectangle } from 'pixi.js';
|
|
4
|
+
|
|
5
|
+
/******************************************************************************
|
|
6
|
+
Copyright (c) Microsoft Corporation.
|
|
7
|
+
|
|
8
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
9
|
+
purpose with or without fee is hereby granted.
|
|
10
|
+
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
12
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
13
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
14
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
15
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
16
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
17
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
18
|
+
***************************************************************************** */
|
|
19
|
+
|
|
20
|
+
function __rest(s, e) {
|
|
21
|
+
var t = {};
|
|
22
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
23
|
+
t[p] = s[p];
|
|
24
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
25
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
26
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
27
|
+
t[p[i]] = s[p[i]];
|
|
28
|
+
}
|
|
29
|
+
return t;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function __decorate(decorators, target, key, desc) {
|
|
33
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
34
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
35
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
36
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function __metadata(metadataKey, metadataValue) {
|
|
40
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
44
|
+
var e = new Error(message);
|
|
45
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const hitAreaFunc = {
|
|
49
|
+
Circle,
|
|
50
|
+
Ellipse,
|
|
51
|
+
Polygon,
|
|
52
|
+
Rect: Rectangle,
|
|
53
|
+
RoundedRect: RoundedRectangle,
|
|
54
|
+
};
|
|
55
|
+
const propertyForHitArea = {
|
|
56
|
+
Circle: ['x', 'y', 'radius'],
|
|
57
|
+
Ellipse: ['x', 'y', 'width', 'height'],
|
|
58
|
+
Rect: ['x', 'y', 'width', 'height'],
|
|
59
|
+
RoundedRect: ['x', 'y', 'width', 'height', 'radius'],
|
|
60
|
+
Polygon: ['paths'],
|
|
61
|
+
};
|
|
62
|
+
let Event$1 = class Event extends Renderer {
|
|
63
|
+
constructor() {
|
|
64
|
+
super(...arguments);
|
|
65
|
+
this.name = 'EventSystem';
|
|
66
|
+
}
|
|
67
|
+
init(params) {
|
|
68
|
+
var _a;
|
|
69
|
+
const compatibleParams = params;
|
|
70
|
+
const game = this.game || (typeof (compatibleParams === null || compatibleParams === void 0 ? void 0 : compatibleParams.getSystem) === 'function' ? compatibleParams : undefined);
|
|
71
|
+
const renderSystem = (_a = game === null || game === void 0 ? void 0 : game.getSystem) === null || _a === void 0 ? void 0 : _a.call(game, RendererSystem);
|
|
72
|
+
if (!(renderSystem === null || renderSystem === void 0 ? void 0 : renderSystem.rendererManager))
|
|
73
|
+
return;
|
|
74
|
+
this.renderSystem = renderSystem;
|
|
75
|
+
this.renderSystem.rendererManager.register(this);
|
|
76
|
+
}
|
|
77
|
+
update(frame) {
|
|
78
|
+
super.update(frame);
|
|
79
|
+
}
|
|
80
|
+
componentChanged(changed) {
|
|
81
|
+
switch (changed.type) {
|
|
82
|
+
case OBSERVER_TYPE.ADD:
|
|
83
|
+
this.add(changed);
|
|
84
|
+
break;
|
|
85
|
+
case OBSERVER_TYPE.REMOVE:
|
|
86
|
+
this.remove(changed);
|
|
87
|
+
break;
|
|
88
|
+
case OBSERVER_TYPE.CHANGE:
|
|
89
|
+
this.change(changed);
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
add(changed) {
|
|
94
|
+
var _a;
|
|
95
|
+
const container = (_a = this.containerManager) === null || _a === void 0 ? void 0 : _a.getContainer(changed.gameObject.id);
|
|
96
|
+
if (!container)
|
|
97
|
+
return;
|
|
98
|
+
container.interactive = true;
|
|
99
|
+
container.interactiveChildren = true;
|
|
100
|
+
const component = changed.component;
|
|
101
|
+
if (component.hitArea) {
|
|
102
|
+
this.addHitArea(changed, container, component.hitArea);
|
|
103
|
+
}
|
|
104
|
+
container.on('pointertap', e => {
|
|
105
|
+
const eventParam = this.createEventParam(container, component, e);
|
|
106
|
+
component.emit('tap', eventParam);
|
|
107
|
+
component.emit('click', eventParam);
|
|
108
|
+
});
|
|
109
|
+
container.on('pointerdown', e => {
|
|
110
|
+
const eventParam = this.createEventParam(container, component, e);
|
|
111
|
+
component.emit('touchstart', eventParam);
|
|
112
|
+
component.emit('mousedown', eventParam);
|
|
113
|
+
});
|
|
114
|
+
container.on('pointermove', e => {
|
|
115
|
+
const eventParam = this.createEventParam(container, component, e);
|
|
116
|
+
component.emit('touchmove', eventParam);
|
|
117
|
+
component.emit('mousemove', eventParam);
|
|
118
|
+
});
|
|
119
|
+
container.on('pointerup', e => {
|
|
120
|
+
const eventParam = this.createEventParam(container, component, e);
|
|
121
|
+
component.emit('touchend', eventParam);
|
|
122
|
+
component.emit('mouseup', eventParam);
|
|
123
|
+
});
|
|
124
|
+
container.on('pointerupoutside', e => {
|
|
125
|
+
component.emit('touchendoutside', this.createEventParam(container, component, e));
|
|
126
|
+
});
|
|
127
|
+
container.on('pointercancel', e => {
|
|
128
|
+
component.emit('touchcancel', this.createEventParam(container, component, e));
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
createEventParam(container, component, e) {
|
|
132
|
+
return {
|
|
133
|
+
stopPropagation: () => e.stopPropagation(),
|
|
134
|
+
data: {
|
|
135
|
+
// @ts-ignore
|
|
136
|
+
pointerId: e.data.pointerId,
|
|
137
|
+
position: {
|
|
138
|
+
x: e.data.global.x,
|
|
139
|
+
y: e.data.global.y,
|
|
140
|
+
},
|
|
141
|
+
localPosition: container.worldTransform.applyInverse(e.data.global),
|
|
142
|
+
},
|
|
143
|
+
gameObject: component.gameObject,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
remove(changed) {
|
|
147
|
+
var _a;
|
|
148
|
+
const container = (_a = this.containerManager) === null || _a === void 0 ? void 0 : _a.getContainer(changed.gameObject.id);
|
|
149
|
+
if (!container)
|
|
150
|
+
return;
|
|
151
|
+
container.interactive = false;
|
|
152
|
+
container.off('pointertap');
|
|
153
|
+
container.off('pointerdown');
|
|
154
|
+
container.off('pointermove');
|
|
155
|
+
container.off('pointerup');
|
|
156
|
+
container.off('pointerupoutside');
|
|
157
|
+
container.off('pointercancel');
|
|
158
|
+
changed.component.removeAllListeners();
|
|
159
|
+
}
|
|
160
|
+
change(changed) {
|
|
161
|
+
var _a;
|
|
162
|
+
const container = (_a = this.containerManager) === null || _a === void 0 ? void 0 : _a.getContainer(changed.gameObject.id);
|
|
163
|
+
if (!container)
|
|
164
|
+
return;
|
|
165
|
+
container.interactive = true;
|
|
166
|
+
const component = changed.component;
|
|
167
|
+
if (component.hitArea) {
|
|
168
|
+
this.addHitArea(changed, container, component.hitArea);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
component.hitArea = null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
addHitArea(changed, container, hitArea) {
|
|
175
|
+
const { type, style } = hitArea;
|
|
176
|
+
if (!hitAreaFunc[type]) {
|
|
177
|
+
console.error(`${changed.gameObject.name}'s hitArea type is not defined`);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const params = [];
|
|
181
|
+
for (const key of propertyForHitArea[type]) {
|
|
182
|
+
params.push(style[key]);
|
|
183
|
+
}
|
|
184
|
+
const hitAreaShape = new hitAreaFunc[type](...params);
|
|
185
|
+
container.hitArea = hitAreaShape;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
Event$1.systemName = 'EventSystem';
|
|
189
|
+
Event$1 = __decorate([
|
|
190
|
+
decorators.componentObserver({
|
|
191
|
+
Event: [{ prop: ['hitArea'], deep: true }],
|
|
192
|
+
})
|
|
193
|
+
], Event$1);
|
|
194
|
+
var Event$2 = Event$1;
|
|
195
|
+
|
|
196
|
+
var HIT_AREA_TYPE;
|
|
197
|
+
(function (HIT_AREA_TYPE) {
|
|
198
|
+
HIT_AREA_TYPE["Circle"] = "Circle";
|
|
199
|
+
HIT_AREA_TYPE["Ellipse"] = "Ellipse";
|
|
200
|
+
HIT_AREA_TYPE["Polygon"] = "Polygon";
|
|
201
|
+
HIT_AREA_TYPE["Rect"] = "Rect";
|
|
202
|
+
HIT_AREA_TYPE["RoundedRect"] = "RoundedRect";
|
|
203
|
+
})(HIT_AREA_TYPE || (HIT_AREA_TYPE = {}));
|
|
204
|
+
class HitAreaStyleMetadata {
|
|
205
|
+
}
|
|
206
|
+
__decorate([
|
|
207
|
+
type('number'),
|
|
208
|
+
step(1),
|
|
209
|
+
__metadata("design:type", Number)
|
|
210
|
+
], HitAreaStyleMetadata.prototype, "x", void 0);
|
|
211
|
+
__decorate([
|
|
212
|
+
type('number'),
|
|
213
|
+
step(1),
|
|
214
|
+
__metadata("design:type", Number)
|
|
215
|
+
], HitAreaStyleMetadata.prototype, "y", void 0);
|
|
216
|
+
__decorate([
|
|
217
|
+
type('number'),
|
|
218
|
+
step(1),
|
|
219
|
+
__metadata("design:type", Number)
|
|
220
|
+
], HitAreaStyleMetadata.prototype, "radius", void 0);
|
|
221
|
+
__decorate([
|
|
222
|
+
type('number'),
|
|
223
|
+
step(1),
|
|
224
|
+
__metadata("design:type", Number)
|
|
225
|
+
], HitAreaStyleMetadata.prototype, "width", void 0);
|
|
226
|
+
__decorate([
|
|
227
|
+
type('number'),
|
|
228
|
+
step(1),
|
|
229
|
+
__metadata("design:type", Number)
|
|
230
|
+
], HitAreaStyleMetadata.prototype, "height", void 0);
|
|
231
|
+
__decorate([
|
|
232
|
+
type('number'),
|
|
233
|
+
__metadata("design:type", Array)
|
|
234
|
+
], HitAreaStyleMetadata.prototype, "paths", void 0);
|
|
235
|
+
class HitAreaMetadata {
|
|
236
|
+
}
|
|
237
|
+
__decorate([
|
|
238
|
+
type('string'),
|
|
239
|
+
__metadata("design:type", String)
|
|
240
|
+
], HitAreaMetadata.prototype, "type", void 0);
|
|
241
|
+
__decorate([
|
|
242
|
+
Field(() => HitAreaStyleMetadata),
|
|
243
|
+
__metadata("design:type", Object)
|
|
244
|
+
], HitAreaMetadata.prototype, "style", void 0);
|
|
245
|
+
/**
|
|
246
|
+
* 事件组件
|
|
247
|
+
*
|
|
248
|
+
* Event 组件为游戏对象添加交互能力,使其能够响应触摸和鼠标事件。
|
|
249
|
+
* 可以自定义交互热区的形状(矩形、圆形、多边形等),
|
|
250
|
+
* 适用于按钮、拖拽对象、可点击元素等交互场景。
|
|
251
|
+
*
|
|
252
|
+
* 支持的事件类型:
|
|
253
|
+
* - touchstart - 触摸/点击开始
|
|
254
|
+
* - touchmove - 触摸/鼠标移动
|
|
255
|
+
* - touchend - 触摸/点击结束
|
|
256
|
+
* - tap - 点击/轻触
|
|
257
|
+
* - touchendoutside - 在对象外部结束触摸
|
|
258
|
+
* - touchcancel - 触摸取消
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* ```typescript
|
|
262
|
+
* // 基础用法 - 使用默认热区(Transform 尺寸)
|
|
263
|
+
* const button = new GameObject('button');
|
|
264
|
+
* const event = new Event();
|
|
265
|
+
* button.addComponent(event);
|
|
266
|
+
*
|
|
267
|
+
* event.on('tap', (e) => {
|
|
268
|
+
* console.log('按钮被点击', e.data.position);
|
|
269
|
+
* });
|
|
270
|
+
*
|
|
271
|
+
* // 自定义矩形热区
|
|
272
|
+
* button.addComponent(new Event({
|
|
273
|
+
* hitArea: {
|
|
274
|
+
* type: HIT_AREA_TYPE.Rect,
|
|
275
|
+
* style: { x: 0, y: 0, width: 100, height: 50 }
|
|
276
|
+
* }
|
|
277
|
+
* }));
|
|
278
|
+
*
|
|
279
|
+
* // 圆形热区
|
|
280
|
+
* button.addComponent(new Event({
|
|
281
|
+
* hitArea: {
|
|
282
|
+
* type: HIT_AREA_TYPE.Circle,
|
|
283
|
+
* style: { x: 50, y: 50, radius: 40 }
|
|
284
|
+
* }
|
|
285
|
+
* }));
|
|
286
|
+
*
|
|
287
|
+
* // 多边形热区
|
|
288
|
+
* button.addComponent(new Event({
|
|
289
|
+
* hitArea: {
|
|
290
|
+
* type: HIT_AREA_TYPE.Polygon,
|
|
291
|
+
* style: { paths: [0,0, 100,0, 50,100] }
|
|
292
|
+
* }
|
|
293
|
+
* }));
|
|
294
|
+
*
|
|
295
|
+
* // 事件处理
|
|
296
|
+
* event.on('touchstart', (e) => {
|
|
297
|
+
* console.log('触摸开始', e.data.localPosition);
|
|
298
|
+
* e.stopPropagation(); // 阻止事件冒泡
|
|
299
|
+
* });
|
|
300
|
+
* ```
|
|
301
|
+
*/
|
|
302
|
+
class Event extends Component {
|
|
303
|
+
constructor(params) {
|
|
304
|
+
super(params);
|
|
305
|
+
/** 交互热区配置 */
|
|
306
|
+
this.hitArea = undefined;
|
|
307
|
+
this.configuredEventHandlers = [];
|
|
308
|
+
this.init(params);
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* 初始化组件
|
|
312
|
+
* @param params - 初始化参数
|
|
313
|
+
*/
|
|
314
|
+
init(params) {
|
|
315
|
+
this.removeConfiguredEventHandlers();
|
|
316
|
+
if (!params)
|
|
317
|
+
return;
|
|
318
|
+
const { on } = params, options = __rest(params, ["on"]);
|
|
319
|
+
Object.assign(this, options);
|
|
320
|
+
if (on) {
|
|
321
|
+
for (const eventName of Object.keys(on)) {
|
|
322
|
+
const handler = on[eventName];
|
|
323
|
+
if (typeof handler !== 'function')
|
|
324
|
+
continue;
|
|
325
|
+
super.on(eventName, handler);
|
|
326
|
+
this.configuredEventHandlers.push({ eventName, handler });
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
emit(en, ...args) {
|
|
331
|
+
return super.emit(en, ...args);
|
|
332
|
+
}
|
|
333
|
+
once(en, fn, context) {
|
|
334
|
+
return super.once(en, fn, context);
|
|
335
|
+
}
|
|
336
|
+
on(en, fn, context) {
|
|
337
|
+
return super.on(en, fn, context);
|
|
338
|
+
}
|
|
339
|
+
destroy() {
|
|
340
|
+
this.removeAllListeners();
|
|
341
|
+
this.configuredEventHandlers = [];
|
|
342
|
+
return this;
|
|
343
|
+
}
|
|
344
|
+
onDestroy() {
|
|
345
|
+
this.destroy();
|
|
346
|
+
}
|
|
347
|
+
removeConfiguredEventHandlers() {
|
|
348
|
+
for (const { eventName, handler } of this.configuredEventHandlers) {
|
|
349
|
+
super.off(eventName, handler);
|
|
350
|
+
}
|
|
351
|
+
this.configuredEventHandlers = [];
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
/** 组件名称 */
|
|
355
|
+
Event.componentName = 'Event';
|
|
356
|
+
__decorate([
|
|
357
|
+
Field(() => HitAreaMetadata),
|
|
358
|
+
__metadata("design:type", Object)
|
|
359
|
+
], Event.prototype, "hitArea", void 0);
|
|
360
|
+
|
|
361
|
+
export { Event, Event$2 as EventSystem, HIT_AREA_TYPE };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eva/plugin-renderer-event",
|
|
3
|
-
"version": "2.1.0-beta.
|
|
3
|
+
"version": "2.1.0-beta.4",
|
|
4
4
|
"description": "@eva/plugin-renderer-event",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "dist/plugin-renderer-event.esm.js",
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
"license": "MIT",
|
|
19
19
|
"homepage": "https://eva.js.org",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@eva/plugin-renderer": "2.1.0-beta.
|
|
22
|
-
"@eva/eva.js": "2.1.0-beta.
|
|
21
|
+
"@eva/plugin-renderer": "2.1.0-beta.4",
|
|
22
|
+
"@eva/eva.js": "2.1.0-beta.4",
|
|
23
23
|
"pixi.js": "^8.17.0"
|
|
24
24
|
}
|
|
25
25
|
}
|