/
CCComponentEventHandler.js
178 lines (160 loc) · 5.61 KB
/
CCComponentEventHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
/**
* !#en
* Component will register a event to target component's handler.
* And it will trigger the handler when a certain event occurs.
*
* !@zh
* “EventHandler” 类用来设置场景中的事件回调,
* 该类允许用户设置回调目标节点,目标组件名,组件方法名,
* 并可通过 emit 方法调用目标函数。
* @class Component.EventHandler
* @example
* // Create new EventHandler
* var eventHandler = new cc.Component.EventHandler();
* eventHandler.target = newTarget;
* eventHandler.component = "MainMenu";
* eventHandler.handler = "OnClick";
* eventHandler.customEventData = "my data";
*/
cc.Component.EventHandler = cc.Class({
name: 'cc.ClickEvent',
properties: {
/**
* !#en Event target
* !#zh 目标节点
* @property target
* @type {Node}
* @default null
*/
target: {
default: null,
type: cc.Node,
},
/**
* !#en Component name
* !#zh 目标组件名
* @property component
* @type {String}
* @default ''
*/
// only for deserializing old project component field
component: '',
_componentId: '',
_componentName: {
get () {
this._genCompIdIfNeeded();
return this._compId2Name(this._componentId);
},
set (value) {
this._componentId = this._compName2Id(value);
},
},
/**
* !#en Event handler
* !#zh 响应事件函数名
* @property handler
* @type {String}
* @default ''
*/
handler: {
default: '',
},
/**
* !#en Custom Event Data
* !#zh 自定义事件数据
* @property customEventData
* @default ''
* @type {String}
*/
customEventData: {
default: ''
}
},
statics: {
/**
* @method emitEvents
* @param {Component.EventHandler[]} events
* @param {any} ...params
* @static
*/
emitEvents: function(events) {
'use strict';
let args;
if (arguments.length > 0) {
args = new Array(arguments.length - 1);
for (let i = 0, l = args.length; i < l; i++) {
args[i] = arguments[i+1];
}
}
for (let i = 0, l = events.length; i < l; i++) {
var event = events[i];
if (!(event instanceof cc.Component.EventHandler)) continue;
event.emit(args);
}
}
},
/**
* !#en Emit event with params
* !#zh 触发目标组件上的指定 handler 函数,该参数是回调函数的参数值(可不填)。
* @method emit
* @param {Array} params
* @example
* // Call Function
* var eventHandler = new cc.Component.EventHandler();
* eventHandler.target = newTarget;
* eventHandler.component = "MainMenu";
* eventHandler.handler = "OnClick"
* eventHandler.emit(["param1", "param2", ....]);
*/
emit: function(params) {
var target = this.target;
if (!cc.isValid(target)) return;
this._genCompIdIfNeeded();
var compType = cc.js._getClassById(this._componentId);
var comp = target.getComponent(compType);
if (!cc.isValid(comp)) return;
var handler = comp[this.handler];
if (typeof(handler) !== 'function') return;
if (this.customEventData != null && this.customEventData !== '') {
params = params.slice();
params.push(this.customEventData);
}
handler.apply(comp, params);
},
_compName2Id (compName) {
let comp = cc.js.getClassByName(compName);
return cc.js._getClassId(comp);
},
_compId2Name (compId) {
let comp = cc.js._getClassById(compId);
return cc.js.getClassName(comp);
},
// to be deprecated in the future
_genCompIdIfNeeded () {
if (!this._componentId) {
this._componentName = this.component;
this.component = '';
}
},
});