mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
4a19c75688
commit 88cc46ea71258d32698157c8b6cbd5453d98bffe
Merge: e4323ec 91c87a8
Author: zilongshanren <guanghui8827@126.com>
Date: Wed Sep 21 14:16:52 2016 +0800
Merge branch 'master' into addCheckBox
commit e4323ecf3b21f54850228ff9a6bb3d81ce86462a
Author: zilongshanren <guanghui8827@126.com>
Date: Wed Sep 21 14:11:26 2016 +0800
fix toggle group tooltip
commit 897fe28ec0f750c0c0c7fa01b19bd95f6ea7a6c6
Author: zilongshanren <guanghui8827@126.com>
Date: Tue Sep 20 17:44:00 2016 +0800
update component doc string
commit 90559147b74d581b229c916521265c92f22b6205
Author: zilongshanren <guanghui8827@126.com>
Date: Tue Sep 20 11:33:05 2016 +0800
remove executeInEditMode
commit 2c60439c4166f36c785c04d74e6d0d02aa6d7eb0
Author: zilongshanren <guanghui8827@126.com>
Date: Tue Sep 20 10:55:19 2016 +0800
refactor checkmark
commit e91cab956f93df0460be7de960ad27fab23dbe42
Author: zilongshanren <guanghui8827@126.com>
Date: Tue Sep 20 10:19:38 2016 +0800
add missing class docs
commit bf800a799873e0afa8026f98833cbbf73b00863c
Author: zilongshanren <guanghui8827@126.com>
Date: Mon Sep 19 18:33:37 2016 +0800
rename member variables
commit a4f0e859f033a78a76797e3fb820ac897521aaf0
Author: zilongshanren <guanghui8827@126.com>
Date: Mon Sep 19 18:20:11 2016 +0800
refine the Toggle and ToggleGroup
commit 54204c36c219f770ad58148b766f0419d25bf21c
Author: zilongshanren <guanghui8827@126.com>
Date: Mon Sep 19 09:24:05 2016 +0800
add ToggleGroup
refactor toggle
fix button zoom scale
finish checkbox
finish toggle and togglegroup
commit 3a18c096ef49ada7e8cc9757cd2df2900743890c
Author: zilongshanren <guanghui8827@126.com>
Date: Wed Sep 14 10:18:43 2016 +0800
add toggle.js
214 lines
6.4 KiB
JavaScript
214 lines
6.4 KiB
JavaScript
/****************************************************************************
|
||
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 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.js'),
|
||
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: '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 {cc.ToggleGroup} toggleGroup
|
||
*/
|
||
toggleGroup: {
|
||
default: null,
|
||
tooltip: 'i18n:COMPONENT.toggle.toggleGroup',
|
||
type: cc.ToggleGroup
|
||
},
|
||
|
||
/**
|
||
* !#en The image used for the checkmark.
|
||
* !#zh Toggle 处于选中状态时显示的图片
|
||
* @property {cc.Sprite} checkMark
|
||
*/
|
||
checkMark: {
|
||
default: null,
|
||
type: cc.Sprite,
|
||
tooltip: '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();
|
||
}
|
||
}
|
||
},
|
||
|
||
},
|
||
|
||
__preload: function () {
|
||
this._super();
|
||
this._registerToggleEvent();
|
||
},
|
||
|
||
onEnable: function () {
|
||
this._super();
|
||
if(this.toggleGroup) {
|
||
this.toggleGroup.addToggle(this);
|
||
}
|
||
},
|
||
|
||
onDisable: function () {
|
||
this._super();
|
||
if(this.toggleGroup) {
|
||
this.toggleGroup.removeToggle(this);
|
||
}
|
||
},
|
||
|
||
_updateCheckMark: function () {
|
||
if(this.checkMark) {
|
||
this.checkMark.enabled = !!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 () {
|
||
var event = new cc.Component.EventHandler();
|
||
event.target = this.node;
|
||
event.component = 'cc.Toggle';
|
||
event.handler = 'toggle';
|
||
this.clickEvents = [event];
|
||
|
||
},
|
||
|
||
toggle: function (event) {
|
||
if(this.toggleGroup && this.isChecked) {
|
||
if(!this.toggleGroup.allowSwitchOff) {
|
||
return;
|
||
}
|
||
}
|
||
this.isChecked = !this.isChecked;
|
||
|
||
this._updateCheckMark();
|
||
|
||
this._emitToggleEvents(event);
|
||
|
||
if(this.toggleGroup) {
|
||
this.toggleGroup.updateToggles(this);
|
||
}
|
||
},
|
||
|
||
_emitToggleEvents: function (event) {
|
||
this.node.emit('toggle-event', this);
|
||
if(this.checkEvents) {
|
||
cc.Component.EventHandler.emitEvents(this.checkEvents, event, this);
|
||
}
|
||
},
|
||
|
||
/**
|
||
* !#en Make the toggle button checked.
|
||
* !#zh 使 toggle 按钮处于选中状态
|
||
* @method check
|
||
*/
|
||
check: function () {
|
||
if(this.toggleGroup && this.isChecked) {
|
||
if(!this.toggleGroup.allowSwitchOff) {
|
||
return;
|
||
}
|
||
}
|
||
|
||
this.isChecked = true;
|
||
this.node.emit('toggle-event', this);
|
||
|
||
if(this.toggleGroup) {
|
||
this.toggleGroup.updateToggles(this);
|
||
}
|
||
},
|
||
|
||
/**
|
||
* !#en Make the toggle button unchecked.
|
||
* !#zh 使 toggle 按钮处于未选中状态
|
||
* @method uncheck
|
||
*/
|
||
uncheck: function () {
|
||
if(this.toggleGroup && this.isChecked) {
|
||
if(!this.toggleGroup.allowSwitchOff) {
|
||
return;
|
||
}
|
||
}
|
||
|
||
this.isChecked = false;
|
||
|
||
this.node.emit('toggle-event', this);
|
||
}
|
||
|
||
|
||
});
|
||
|
||
cc.Toggle = module.exports = Toggle;
|