mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
add CCToggleContainer
This commit is contained in:
+2
-3
@@ -50,6 +50,7 @@ if (CC_DEBUG) {
|
||||
"1402": "The 'visible' property of %s is deprecated, use 'enabled' instead please.", //visible
|
||||
"1403": "Sorry, cc.audioEngine.willPlayMusic is removed.", //willPlayMusicError
|
||||
"1404": "cc.spriteFrameCache is removed, please use cc.loader to load and cache sprite frames of atlas format.",
|
||||
"1405": "The '%s' will be removed in v2.0, please use '%s' instead. \uD83D\uDE30",
|
||||
//Scheduler: 1500
|
||||
"1500": "cc.Scheduler#schedule: Callback already scheduled. Updating interval from:%s to %s", //scheduleCallbackForTarget
|
||||
"1501": "cc.Scheduler#scheduleCallbackForTarget(): callback_fn should be non-null.", //scheduleCallbackForTarget_2
|
||||
@@ -340,7 +341,7 @@ if (CC_DEBUG) {
|
||||
"3700": "internal error: _prefab is undefined", //_doInstantiate
|
||||
"3701": "Failed to load prefab asset for node '%s'", //syncWithPrefab
|
||||
//Game: 3800
|
||||
"3800": "The target can not be made persist because it\'s not a cc.Node or it doesn\'t have _id property.", //addPersistRootNode
|
||||
"3800": "The target can not be made persist because it\'s invalid or it doesn\'t have _id property.", //addPersistRootNode
|
||||
"3801": "The node can not be made persist because it\'s not under root node.", //addPersistRootNode_2
|
||||
"3802": "The node can not be made persist because it\'s not in current scene.", //addPersistRootNode_3
|
||||
"3803": "The target can not be made persist because it\'s not a cc.Node or it doesn\'t have _id property.", //addPersistRootNode_4
|
||||
@@ -618,8 +619,6 @@ if (CC_DEBUG) {
|
||||
"8301": "Camera does not support Canvas Mode.", //cc.Camera.enabled
|
||||
// AudioEngine: 8400
|
||||
"8400": "Wrong type arguments, 'filePath' must be a String.", // cc.audioEngine.play
|
||||
// ToggleGroup: 8500
|
||||
"8500": "The ToggleGroup.toggleItems api will be removed in v2.0, please obtain toggles in other ways. \uD83D\uDE30",
|
||||
};
|
||||
cc._LogInfos = logs;
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ var Toggle = cc.Class({
|
||||
isChecked: {
|
||||
default: true,
|
||||
tooltip: CC_DEV && 'i18n:COMPONENT.toggle.isChecked',
|
||||
notify: function() {
|
||||
notify: function () {
|
||||
this._updateCheckMark();
|
||||
}
|
||||
},
|
||||
@@ -91,7 +91,7 @@ var Toggle = cc.Class({
|
||||
_resizeToTarget: {
|
||||
animatable: false,
|
||||
set: function (value) {
|
||||
if(value) {
|
||||
if (value) {
|
||||
this._resizeNodeToTargetNode();
|
||||
}
|
||||
}
|
||||
@@ -99,32 +99,32 @@ var Toggle = cc.Class({
|
||||
|
||||
},
|
||||
|
||||
__preload: function () {
|
||||
this._super();
|
||||
onLoad: function () {
|
||||
this._toggleContainer = this.node.parent.getComponent(cc.ToggleContainer);
|
||||
},
|
||||
|
||||
onEnable: function () {
|
||||
this._super();
|
||||
if(!CC_EDITOR) {
|
||||
if (!CC_EDITOR) {
|
||||
this._registerToggleEvent();
|
||||
}
|
||||
if(this.toggleGroup && this.toggleGroup.enabled) {
|
||||
if (this.toggleGroup && this.toggleGroup.enabled) {
|
||||
this.toggleGroup.addToggle(this);
|
||||
}
|
||||
},
|
||||
|
||||
onDisable: function () {
|
||||
this._super();
|
||||
if(!CC_EDITOR) {
|
||||
if (!CC_EDITOR) {
|
||||
this._unregisterToggleEvent();
|
||||
}
|
||||
if(this.toggleGroup && this.toggleGroup.enabled) {
|
||||
if (this.toggleGroup && this.toggleGroup.enabled) {
|
||||
this.toggleGroup.removeToggle(this);
|
||||
}
|
||||
},
|
||||
|
||||
_updateCheckMark: function () {
|
||||
if(this.checkMark) {
|
||||
if (this.checkMark) {
|
||||
this.checkMark.node.active = !!this.isChecked;
|
||||
}
|
||||
},
|
||||
@@ -132,11 +132,11 @@ var Toggle = cc.Class({
|
||||
_updateDisabledState: function () {
|
||||
this._super();
|
||||
|
||||
if(this.checkMark) {
|
||||
if (this.checkMark) {
|
||||
this.checkMark._sgNode.setState(0);
|
||||
}
|
||||
if(this.enableAutoGrayEffect) {
|
||||
if(this.checkMark && !this.interactable) {
|
||||
if (this.enableAutoGrayEffect) {
|
||||
if (this.checkMark && !this.interactable) {
|
||||
this.checkMark._sgNode.setState(1);
|
||||
}
|
||||
}
|
||||
@@ -151,18 +151,20 @@ var Toggle = cc.Class({
|
||||
},
|
||||
|
||||
toggle: function (event) {
|
||||
if(this.toggleGroup && this.toggleGroup.enabled && this.isChecked) {
|
||||
if(!this.toggleGroup.allowSwitchOff) {
|
||||
var group = this.toggleGroup || this._toggleContainer;
|
||||
|
||||
if (group && group.enabled && this.isChecked) {
|
||||
if (!group.allowSwitchOff) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this.isChecked = !this.isChecked;
|
||||
|
||||
this._updateCheckMark();
|
||||
|
||||
|
||||
if(this.toggleGroup && this.toggleGroup.enabled) {
|
||||
this.toggleGroup.updateToggles(this);
|
||||
if (group && group.enabled) {
|
||||
group.updateToggles(this);
|
||||
}
|
||||
|
||||
this._emitToggleEvents(event);
|
||||
@@ -170,7 +172,7 @@ var Toggle = cc.Class({
|
||||
|
||||
_emitToggleEvents: function () {
|
||||
this.node.emit('toggle', this);
|
||||
if(this.checkEvents) {
|
||||
if (this.checkEvents) {
|
||||
cc.Component.EventHandler.emitEvents(this.checkEvents, this);
|
||||
}
|
||||
},
|
||||
@@ -181,16 +183,18 @@ var Toggle = cc.Class({
|
||||
* @method check
|
||||
*/
|
||||
check: function () {
|
||||
if(this.toggleGroup && this.toggleGroup.enabled && this.isChecked) {
|
||||
if(!this.toggleGroup.allowSwitchOff) {
|
||||
var group = this.toggleGroup || this._toggleContainer;
|
||||
|
||||
if (group && group.enabled && this.isChecked) {
|
||||
if (!group.allowSwitchOff) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this.isChecked = true;
|
||||
|
||||
if(this.toggleGroup && this.toggleGroup.enabled) {
|
||||
this.toggleGroup.updateToggles(this);
|
||||
if (group && group.enabled) {
|
||||
group.updateToggles(this);
|
||||
}
|
||||
|
||||
this._emitToggleEvents();
|
||||
@@ -202,8 +206,10 @@ var Toggle = cc.Class({
|
||||
* @method uncheck
|
||||
*/
|
||||
uncheck: function () {
|
||||
if(this.toggleGroup && this.toggleGroup.enabled && this.isChecked) {
|
||||
if(!this.toggleGroup.allowSwitchOff) {
|
||||
var group = this.toggleGroup || this._toggleContainer;
|
||||
|
||||
if (group && group.enabled && this.isChecked) {
|
||||
if (!group.allowSwitchOff) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -212,8 +218,6 @@ var Toggle = cc.Class({
|
||||
|
||||
this._emitToggleEvents();
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
cc.Toggle = module.exports = Toggle;
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
|
||||
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.
|
||||
Chukong Aipu 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 ToggleContainer is not a visiable UI component but a way to modify the behavior of a set of Toggles. </br>
|
||||
* Toggles that belong to the same group could only have one of them to be switched on at a time.</br>
|
||||
* Note: All the first layer child node containing the toggle component will auto be added to the container
|
||||
* !#zh ToggleContainer 不是一个可见的 UI 组件,它可以用来修改一组 Toggle 组件的行为。</br>
|
||||
* 当一组 Toggle 属于同一个 ToggleContainer 的时候,任何时候只能有一个 Toggle 处于选中状态。</br>
|
||||
* 注意:所有包含 Toggle 组件的一级子节点都会自动被添加到该容器中
|
||||
* @class ToggleContainer
|
||||
* @extends Component
|
||||
*/
|
||||
var ToggleContainer = cc.Class({
|
||||
name: 'cc.ToggleContainer',
|
||||
extends: cc.Component,
|
||||
editor: CC_EDITOR && {
|
||||
menu: 'i18n:MAIN_MENU.component.ui/ToggleContainer',
|
||||
help: 'i18n:COMPONENT.help_url.toggleContainer',
|
||||
executeInEditMode: true
|
||||
},
|
||||
|
||||
properties: {
|
||||
/**
|
||||
* !#en If this setting is true, a toggle could be switched off and on when pressed.
|
||||
* If it is false, it will make sure there is always only one toggle could be switched on
|
||||
* and the already switched on toggle can't be switched off.
|
||||
* !#zh 如果这个设置为 true, 那么 toggle 按钮在被点击的时候可以反复地被选中和未选中。
|
||||
* @property {Boolean} allowSwitchOff
|
||||
*/
|
||||
allowSwitchOff: {
|
||||
tooltip: CC_DEV && 'i18n:COMPONENT.toggle_group.allowSwitchOff',
|
||||
default: false
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Read only property, return the toggle items array reference managed by ToggleContainer.
|
||||
* !#zh 只读属性,返回 ToggleContainer 管理的 toggle 数组引用
|
||||
* @property {Array} toggleItems
|
||||
*/
|
||||
toggleItems: {
|
||||
get: function () {
|
||||
return this.node.getComponentsInChildren(cc.Toggle);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
updateToggles: function (toggle) {
|
||||
if(!this.enabledInHierarchy) return;
|
||||
|
||||
this.toggleItems.forEach(function (item){
|
||||
if(toggle.isChecked) {
|
||||
if (item !== toggle && item.isChecked && item.enabled) {
|
||||
item.isChecked = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
_allowOnlyOneToggleChecked: function () {
|
||||
var isChecked = false;
|
||||
this.toggleItems.forEach(function (item) {
|
||||
if(isChecked && item.enabled) {
|
||||
item.isChecked = false;
|
||||
}
|
||||
|
||||
if (item.isChecked && item.enabled) {
|
||||
isChecked = true;
|
||||
}
|
||||
});
|
||||
|
||||
return isChecked;
|
||||
},
|
||||
|
||||
_makeAtLeastOneToggleChecked: function () {
|
||||
var isChecked = this._allowOnlyOneToggleChecked();
|
||||
|
||||
if(!isChecked && !this.allowSwitchOff) {
|
||||
if(this.toggleItems.length > 0) {
|
||||
this.toggleItems[0].check();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
_childAdded: function (event) {
|
||||
var toggle = event.detail.getComponent(cc.Toggle);
|
||||
if (!toggle) {
|
||||
return;
|
||||
}
|
||||
var index = this.toggleItems.indexOf(toggle);
|
||||
if (index === -1) {
|
||||
this.toggleItems.push(toggle);
|
||||
}
|
||||
this._allowOnlyOneToggleChecked();
|
||||
},
|
||||
|
||||
_childRemoved: function (event) {
|
||||
var toggle = event.detail.getComponent(cc.Toggle);
|
||||
if (!toggle) {
|
||||
return;
|
||||
}
|
||||
var index = this.toggleItems.indexOf(toggle);
|
||||
if(index > -1) {
|
||||
this.toggleItems.splice(index, 1);
|
||||
}
|
||||
this._makeAtLeastOneToggleChecked();
|
||||
},
|
||||
|
||||
onEnable: function () {
|
||||
this.node.on('child-added', this._childAdded, this);
|
||||
this.node.on('child-removed', this._childRemoved, this);
|
||||
},
|
||||
|
||||
onDisable: function () {
|
||||
this.node.off('child-added', this._childAdded, this);
|
||||
this.node.off('child-removed', this._childRemoved, this);
|
||||
},
|
||||
|
||||
start: function () {
|
||||
this._makeAtLeastOneToggleChecked();
|
||||
}
|
||||
});
|
||||
|
||||
cc.ToggleContainer = module.exports = ToggleContainer;
|
||||
@@ -38,8 +38,7 @@ var ToggleGroup = cc.Class({
|
||||
this._toggleItems = [];
|
||||
},
|
||||
editor: CC_EDITOR && {
|
||||
menu: 'i18n:MAIN_MENU.component.ui/ToggleGroup',
|
||||
help: 'i18n:COMPONENT.help_url.toggle_group'
|
||||
help: 'i18n:COMPONENT.help_url.toggleGroup'
|
||||
},
|
||||
|
||||
properties: {
|
||||
@@ -54,6 +53,17 @@ var ToggleGroup = cc.Class({
|
||||
tooltip: CC_DEV && 'i18n:COMPONENT.toggle_group.allowSwitchOff',
|
||||
default: false
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Read only property, return the toggle items array reference managed by toggleGroup.
|
||||
* !#zh 只读属性,返回 toggleGroup 管理的 toggle 数组引用
|
||||
* @property {Array} toggleItems
|
||||
*/
|
||||
toggleItems: {
|
||||
get: function () {
|
||||
return this._toggleItems;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
updateToggles: function (toggle) {
|
||||
@@ -112,14 +122,16 @@ var ToggleGroup = cc.Class({
|
||||
start: function () {
|
||||
this._makeAtLeastOneToggleChecked();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
var JS = require('../platform/js');
|
||||
|
||||
JS.get(ToggleGroup.prototype, 'toggleItems', function () {
|
||||
cc.warnID(8500);
|
||||
return this._toggleItems;
|
||||
var showed = false;
|
||||
cc.js.get(cc, 'ToggleGroup', function () {
|
||||
if (!showed) {
|
||||
cc.logID(1405, 'cc.ToggleGroup', 'cc.ToggleContainer');
|
||||
showed = true;
|
||||
}
|
||||
return ToggleGroup;
|
||||
});
|
||||
|
||||
cc.ToggleGroup = module.exports = ToggleGroup;
|
||||
|
||||
@@ -51,6 +51,7 @@ module.exports = [
|
||||
require('./CCSpriteDistortion'),
|
||||
require('./CCLabelOutline'),
|
||||
require('./CCRichText'),
|
||||
require('./CCToggleContainer'),
|
||||
require('./CCToggleGroup'),
|
||||
require('./CCToggle'),
|
||||
require('./CCBlockInputEvents'),
|
||||
|
||||
Reference in New Issue
Block a user