/
CCToggle.js
243 lines (204 loc) · 7.31 KB
/
CCToggle.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://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 The toggle component is a CheckBox, when it used together with a ToggleGroup, it
* could be treated as a RadioButton.
* !#zh Toggle 是一个 CheckBox,当它和 ToggleGroup 一起使用的时候,可以变成 RadioButton。
* @class Toggle
* @extends Button
*/
var Toggle = cc.Class({
name: 'cc.Toggle',
extends: require('./CCButton'),
editor: CC_EDITOR && {
menu: 'i18n:MAIN_MENU.component.ui/Toggle',
help: 'i18n:COMPONENT.help_url.toggle',
inspector: 'packages://inspector/inspectors/comps/toggle.js',
},
properties: {
/**
* !#en When this value is true, the check mark component will be enabled, otherwise
* the check mark component will be disabled.
* !#zh 如果这个设置为 true,则 check mark 组件会处于 enabled 状态,否则处于 disabled 状态。
* @property {Boolean} isChecked
*/
isChecked: {
default: true,
tooltip: CC_DEV && 'i18n:COMPONENT.toggle.isChecked',
notify: function () {
this._updateCheckMark();
}
},
/**
* !#en The toggle group which the toggle belongs to, when it is null, the toggle is a CheckBox.
* Otherwise, the toggle is a RadioButton.
* !#zh Toggle 所属的 ToggleGroup,这个属性是可选的。如果这个属性为 null,则 Toggle 是一个 CheckBox,
* 否则,Toggle 是一个 RadioButton。
* @property {ToggleGroup} toggleGroup
*/
toggleGroup: {
default: null,
tooltip: CC_DEV && 'i18n:COMPONENT.toggle.toggleGroup',
type: require('./CCToggleGroup')
},
/**
* !#en The image used for the checkmark.
* !#zh Toggle 处于选中状态时显示的图片
* @property {Sprite} checkMark
*/
checkMark: {
default: null,
type: cc.Sprite,
tooltip: CC_DEV && 'i18n:COMPONENT.toggle.checkMark'
},
/**
* !#en If Toggle is clicked, it will trigger event's handler
* !#zh Toggle 按钮的点击事件列表。
* @property {Component.EventHandler[]} checkEvents
*/
checkEvents: {
default: [],
type: cc.Component.EventHandler
},
_resizeToTarget: {
animatable: false,
set: function (value) {
if (value) {
this._resizeNodeToTargetNode();
}
}
},
},
onEnable: function () {
this._super();
if (!CC_EDITOR) {
this._registerToggleEvent();
}
if (this.toggleGroup && this.toggleGroup.enabled) {
this.toggleGroup.addToggle(this);
}
},
onDisable: function () {
this._super();
if (!CC_EDITOR) {
this._unregisterToggleEvent();
}
if (this.toggleGroup && this.toggleGroup.enabled) {
this.toggleGroup.removeToggle(this);
}
},
_updateCheckMark: function () {
if (this.checkMark) {
this.checkMark.node.active = !!this.isChecked;
}
},
_updateDisabledState: function () {
this._super();
if (this.checkMark) {
this.checkMark._sgNode.setState(0);
}
if (this.enableAutoGrayEffect) {
if (this.checkMark && !this.interactable) {
this.checkMark._sgNode.setState(1);
}
}
},
_registerToggleEvent: function () {
this.node.on('click', this.toggle, this);
},
_unregisterToggleEvent: function () {
this.node.off('click', this.toggle, this);
},
toggle: function (event) {
var group = this.toggleGroup || this._toggleContainer;
if (group && group.enabled && this.isChecked) {
if (!group.allowSwitchOff) {
return;
}
}
this.isChecked = !this.isChecked;
this._updateCheckMark();
if (group && group.enabled) {
group.updateToggles(this);
}
this._emitToggleEvents(event);
},
_emitToggleEvents: function () {
this.node.emit('toggle', this);
if (this.checkEvents) {
cc.Component.EventHandler.emitEvents(this.checkEvents, this);
}
},
/**
* !#en Make the toggle button checked.
* !#zh 使 toggle 按钮处于选中状态
* @method check
*/
check: function () {
var group = this.toggleGroup || this._toggleContainer;
if (group && group.enabled && this.isChecked) {
if (!group.allowSwitchOff) {
return;
}
}
this.isChecked = true;
if (group && group.enabled) {
group.updateToggles(this);
}
this._emitToggleEvents();
},
/**
* !#en Make the toggle button unchecked.
* !#zh 使 toggle 按钮处于未选中状态
* @method uncheck
*/
uncheck: function () {
var group = this.toggleGroup || this._toggleContainer;
if (group && group.enabled && this.isChecked) {
if (!group.allowSwitchOff) {
return;
}
}
this.isChecked = false;
this._emitToggleEvents();
}
});
cc.Toggle = module.exports = Toggle;
var JS = require('../platform/js');
JS.get(Toggle.prototype, '_toggleContainer',
function () {
var parent = this.node.parent;
if (cc.Node.isNode(parent)) {
return parent.getComponent(cc.ToggleContainer);
}
return null;
}
);
/**
* !#en
* Note: This event is emitted from the node to which the component belongs.
* !#zh
* 注意:此事件是从该组件所属的 Node 上面派发出来的,需要用 node.on 来监听。
* @event toggle
* @param {Event.EventCustom} event
* @param {Toggle} event.detail - The Toggle component.
*/