UICheckBox.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /****************************************************************************
  2. Copyright (c) 2011-2012 cocos2d-x.org
  3. Copyright (c) 2013-2014 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. /**
  22. * The CheckBox control of Cocos UI.
  23. * @class
  24. * @extends ccui.Widget
  25. *
  26. * @property {Boolean} selected - Indicate whether the check box has been selected
  27. */
  28. ccui.CheckBox = ccui.Widget.extend(/** @lends ccui.CheckBox# */{
  29. _backGroundBoxRenderer: null,
  30. _backGroundSelectedBoxRenderer: null,
  31. _frontCrossRenderer: null,
  32. _backGroundBoxDisabledRenderer: null,
  33. _frontCrossDisabledRenderer: null,
  34. _isSelected: true,
  35. _checkBoxEventListener: null,
  36. _checkBoxEventSelector:null,
  37. _backGroundTexType: ccui.Widget.LOCAL_TEXTURE,
  38. _backGroundSelectedTexType: ccui.Widget.LOCAL_TEXTURE,
  39. _frontCrossTexType: ccui.Widget.LOCAL_TEXTURE,
  40. _backGroundDisabledTexType: ccui.Widget.LOCAL_TEXTURE,
  41. _frontCrossDisabledTexType: ccui.Widget.LOCAL_TEXTURE,
  42. _backGroundFileName: "",
  43. _backGroundSelectedFileName: "",
  44. _frontCrossFileName: "",
  45. _backGroundDisabledFileName: "",
  46. _frontCrossDisabledFileName: "",
  47. _className: "CheckBox",
  48. _backGroundBoxRendererAdaptDirty:true,
  49. _backGroundSelectedBoxRendererAdaptDirty:true,
  50. _frontCrossRendererAdaptDirty: true,
  51. _backGroundBoxDisabledRendererAdaptDirty: true,
  52. _frontCrossDisabledRendererAdaptDirty: true,
  53. /**
  54. * allocates and initializes a UICheckBox.
  55. * Constructor of ccui.CheckBox, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  56. * @param {String} backGround
  57. * @param {String} backGroundSelected
  58. * @param {String} cross
  59. * @param {String} backGroundDisabled
  60. * @param {String} frontCrossDisabled
  61. * @param {Number} [texType=ccui.Widget.LOCAL_TEXTURE]
  62. * @example
  63. * // example
  64. * var uiCheckBox = new ccui.CheckBox();
  65. */
  66. ctor: function (backGround, backGroundSelected,cross,backGroundDisabled,frontCrossDisabled,texType) {
  67. ccui.Widget.prototype.ctor.call(this);
  68. this.setTouchEnabled(true);
  69. texType && this.init(backGround, backGroundSelected,cross,backGroundDisabled,frontCrossDisabled,texType);
  70. },
  71. /**
  72. * Initializes a checkBox. please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
  73. * @param {String} backGround
  74. * @param {String} backGroundSelected
  75. * @param {String} cross
  76. * @param {String} backGroundDisabled
  77. * @param {String} frontCrossDisabled
  78. * @param {Number} [texType=ccui.Widget.LOCAL_TEXTURE]
  79. * @returns {boolean}
  80. * @override
  81. */
  82. init: function (backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType) {
  83. if (ccui.Widget.prototype.init.call(this)) {
  84. this._isSelected = true;
  85. this.setSelectedState(false);
  86. if(backGround === undefined)
  87. this.loadTextures(backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType);
  88. return true;
  89. }
  90. return false;
  91. },
  92. _initRenderer: function () {
  93. this._backGroundBoxRenderer = cc.Sprite.create();
  94. this._backGroundSelectedBoxRenderer = cc.Sprite.create();
  95. this._frontCrossRenderer = cc.Sprite.create();
  96. this._backGroundBoxDisabledRenderer = cc.Sprite.create();
  97. this._frontCrossDisabledRenderer = cc.Sprite.create();
  98. this.addProtectedChild(this._backGroundBoxRenderer, ccui.CheckBox.BOX_RENDERER_ZORDER, -1);
  99. this.addProtectedChild(this._backGroundSelectedBoxRenderer, ccui.CheckBox.BOX_SELECTED_RENDERER_ZORDER, -1);
  100. this.addProtectedChild(this._frontCrossRenderer, ccui.CheckBox.FRONT_CROSS_RENDERER_ZORDER, -1);
  101. this.addProtectedChild(this._backGroundBoxDisabledRenderer, ccui.CheckBox.BOX_DISABLED_RENDERER_ZORDER, -1);
  102. this.addProtectedChild(this._frontCrossDisabledRenderer, ccui.CheckBox.FRONT_CROSS_DISABLED_RENDERER_ZORDER, -1);
  103. },
  104. /**
  105. * Loads textures for checkbox.
  106. * @param {String} backGround
  107. * @param {String} backGroundSelected
  108. * @param {String} cross
  109. * @param {String} backGroundDisabled
  110. * @param {String} frontCrossDisabled
  111. * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
  112. */
  113. loadTextures: function (backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType) {
  114. this.loadTextureBackGround(backGround, texType);
  115. this.loadTextureBackGroundSelected(backGroundSelected, texType);
  116. this.loadTextureFrontCross(cross, texType);
  117. this.loadTextureBackGroundDisabled(backGroundDisabled, texType);
  118. this.loadTextureFrontCrossDisabled(frontCrossDisabled, texType);
  119. },
  120. /**
  121. * Loads background texture for checkbox.
  122. * @param {String} backGround background filename
  123. * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
  124. */
  125. loadTextureBackGround: function (backGround, texType) {
  126. if (!backGround)
  127. return;
  128. texType = texType || ccui.Widget.LOCAL_TEXTURE;
  129. this._backGroundFileName = backGround;
  130. this._backGroundTexType = texType;
  131. var bgBoxRenderer = this._backGroundBoxRenderer;
  132. var self = this;
  133. if(!bgBoxRenderer.texture || !bgBoxRenderer.texture.isLoaded()){
  134. bgBoxRenderer.addLoadedEventListener(function(){
  135. self._findLayout();
  136. self._updateFlippedX();
  137. self._updateFlippedY();
  138. self._updateChildrenDisplayedRGBA();
  139. self._updateContentSizeWithTextureSize(self._backGroundBoxRenderer.getContentSize());
  140. self._backGroundBoxRendererAdaptDirty = true;
  141. });
  142. }
  143. switch (this._backGroundTexType) {
  144. case ccui.Widget.LOCAL_TEXTURE:
  145. //SetTexture cannot load resource
  146. bgBoxRenderer.initWithFile(backGround);
  147. break;
  148. case ccui.Widget.PLIST_TEXTURE:
  149. //SetTexture cannot load resource
  150. bgBoxRenderer.initWithSpriteFrameName(backGround);
  151. break;
  152. default:
  153. break;
  154. }
  155. if (!bgBoxRenderer.textureLoaded()) {
  156. this._backGroundBoxRenderer.setContentSize(this._customSize);
  157. bgBoxRenderer.addLoadedEventListener(function () {
  158. this._updateContentSizeWithTextureSize(this._backGroundBoxRenderer.getContentSize());
  159. }, this);
  160. }
  161. this._updateFlippedX();
  162. this._updateFlippedY();
  163. this._updateChildrenDisplayedRGBA();
  164. this._updateContentSizeWithTextureSize(this._backGroundBoxRenderer.getContentSize());
  165. this._backGroundBoxRendererAdaptDirty = true;
  166. },
  167. /**
  168. * Loads selected state of background texture for checkbox.
  169. * @param {String} backGroundSelected
  170. * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
  171. */
  172. loadTextureBackGroundSelected: function (backGroundSelected, texType) {
  173. if (!backGroundSelected)
  174. return;
  175. texType = texType || ccui.Widget.LOCAL_TEXTURE;
  176. this._backGroundSelectedFileName = backGroundSelected;
  177. this._backGroundSelectedTexType = texType;
  178. var self = this;
  179. if(!this._backGroundSelectedBoxRenderer.texture || !this._backGroundSelectedBoxRenderer.texture.isLoaded()){
  180. this._backGroundSelectedBoxRenderer.addLoadedEventListener(function(){
  181. self._findLayout();
  182. self._updateFlippedX();
  183. self._updateFlippedY();
  184. self._updateChildrenDisplayedRGBA();
  185. self._backGroundSelectedBoxRendererAdaptDirty = true;
  186. });
  187. }
  188. switch (this._backGroundSelectedTexType) {
  189. case ccui.Widget.LOCAL_TEXTURE:
  190. //SetTexture cannot load resource
  191. this._backGroundSelectedBoxRenderer.initWithFile(backGroundSelected);
  192. break;
  193. case ccui.Widget.PLIST_TEXTURE:
  194. //SetTexture cannot load resource
  195. this._backGroundSelectedBoxRenderer.initWithSpriteFrameName(backGroundSelected);
  196. break;
  197. default:
  198. break;
  199. }
  200. this._updateFlippedX();
  201. this._updateFlippedY();
  202. this._updateChildrenDisplayedRGBA();
  203. this._backGroundSelectedBoxRendererAdaptDirty = true;
  204. },
  205. /**
  206. * Loads cross texture for checkbox.
  207. * @param {String} cross
  208. * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
  209. */
  210. loadTextureFrontCross: function (cross, texType) {
  211. if (!cross)
  212. return;
  213. texType = texType || ccui.Widget.LOCAL_TEXTURE;
  214. this._frontCrossFileName = cross;
  215. this._frontCrossTexType = texType;
  216. var self = this;
  217. if(!this._frontCrossRenderer.texture || !this._frontCrossRenderer.texture.isLoaded()){
  218. this._frontCrossRenderer.addLoadedEventListener(function(){
  219. self._findLayout();
  220. self._updateFlippedX();
  221. self._updateFlippedY();
  222. self._updateChildrenDisplayedRGBA();
  223. self._frontCrossRendererAdaptDirty = true;
  224. });
  225. }
  226. switch (this._frontCrossTexType) {
  227. case ccui.Widget.LOCAL_TEXTURE:
  228. //SetTexture cannot load resource
  229. this._frontCrossRenderer.initWithFile(cross);
  230. break;
  231. case ccui.Widget.PLIST_TEXTURE:
  232. //SetTexture cannot load resource
  233. this._frontCrossRenderer.initWithSpriteFrameName(cross);
  234. break;
  235. default:
  236. break;
  237. }
  238. this._updateFlippedX();
  239. this._updateFlippedY();
  240. this._updateChildrenDisplayedRGBA();
  241. this._frontCrossRendererAdaptDirty = true;
  242. },
  243. /**
  244. * Loads disabled state of backGround texture for checkbox.
  245. * @param {String} backGroundDisabled
  246. * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
  247. */
  248. loadTextureBackGroundDisabled: function (backGroundDisabled, texType) {
  249. if (!backGroundDisabled)
  250. return;
  251. texType = texType || ccui.Widget.LOCAL_TEXTURE;
  252. this._backGroundDisabledFileName = backGroundDisabled;
  253. this._backGroundDisabledTexType = texType;
  254. var self = this;
  255. if(!this._backGroundBoxDisabledRenderer.texture || !this._backGroundBoxDisabledRenderer.texture.isLoaded()){
  256. this._backGroundBoxDisabledRenderer.addLoadedEventListener(function(){
  257. self._findLayout();
  258. self._updateFlippedX();
  259. self._updateFlippedY();
  260. self._updateChildrenDisplayedRGBA();
  261. self._backGroundBoxDisabledRendererAdaptDirty = true;
  262. });
  263. }
  264. switch (this._backGroundDisabledTexType) {
  265. case ccui.Widget.LOCAL_TEXTURE:
  266. //SetTexture cannot load resource
  267. this._backGroundBoxDisabledRenderer.initWithFile(backGroundDisabled);
  268. break;
  269. case ccui.Widget.PLIST_TEXTURE:
  270. //SetTexture cannot load resource
  271. this._backGroundBoxDisabledRenderer.initWithSpriteFrameName(backGroundDisabled);
  272. break;
  273. default:
  274. break;
  275. }
  276. this._updateFlippedX();
  277. this._updateFlippedY();
  278. this._updateChildrenDisplayedRGBA();
  279. this._backGroundBoxDisabledRendererAdaptDirty = true;
  280. },
  281. /**
  282. * Loads frontCrossDisabled texture for checkbox.
  283. * @param {String} frontCrossDisabled
  284. * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
  285. */
  286. loadTextureFrontCrossDisabled: function (frontCrossDisabled, texType) {
  287. if (!frontCrossDisabled)
  288. return;
  289. texType = texType || ccui.Widget.LOCAL_TEXTURE;
  290. this._frontCrossDisabledFileName = frontCrossDisabled;
  291. this._frontCrossDisabledTexType = texType;
  292. var self = this;
  293. if(!this._frontCrossDisabledRenderer.texture || !this._frontCrossDisabledRenderer.texture.isLoaded()){
  294. this._frontCrossDisabledRenderer.addLoadedEventListener(function(){
  295. self._findLayout();
  296. self._updateFlippedX();
  297. self._updateFlippedY();
  298. self._updateChildrenDisplayedRGBA();
  299. self._frontCrossDisabledRendererAdaptDirty = true;
  300. });
  301. }
  302. switch (this._frontCrossDisabledTexType) {
  303. case ccui.Widget.LOCAL_TEXTURE:
  304. //SetTexture cannot load resource
  305. this._frontCrossDisabledRenderer.initWithFile(frontCrossDisabled);
  306. break;
  307. case ccui.Widget.PLIST_TEXTURE:
  308. //SetTexture cannot load resource
  309. this._frontCrossDisabledRenderer.initWithSpriteFrameName(frontCrossDisabled);
  310. break;
  311. default:
  312. break;
  313. }
  314. this._updateFlippedX();
  315. this._updateFlippedY();
  316. this._updateChildrenDisplayedRGBA();
  317. this._frontCrossDisabledRendererAdaptDirty = true;
  318. },
  319. _onPressStateChangedToNormal: function () {
  320. this._backGroundBoxRenderer.setVisible(true);
  321. this._backGroundSelectedBoxRenderer.setVisible(false);
  322. this._backGroundBoxDisabledRenderer.setVisible(false);
  323. this._frontCrossDisabledRenderer.setVisible(false);
  324. },
  325. _onPressStateChangedToPressed: function () {
  326. this._backGroundBoxRenderer.setVisible(false);
  327. this._backGroundSelectedBoxRenderer.setVisible(true);
  328. this._backGroundBoxDisabledRenderer.setVisible(false);
  329. this._frontCrossDisabledRenderer.setVisible(false);
  330. },
  331. _onPressStateChangedToDisabled: function () {
  332. this._backGroundBoxRenderer.setVisible(false);
  333. this._backGroundSelectedBoxRenderer.setVisible(false);
  334. this._backGroundBoxDisabledRenderer.setVisible(true);
  335. this._frontCrossRenderer.setVisible(false);
  336. if (this._isSelected) {
  337. this._frontCrossDisabledRenderer.setVisible(true);
  338. }
  339. },
  340. /**
  341. * Sets the selected state to ccui.CheckBox
  342. * @param {Boolean} selected
  343. */
  344. setSelectedState: function (selected) {
  345. if (selected == this._isSelected)
  346. return;
  347. this._isSelected = selected;
  348. this._frontCrossRenderer.setVisible(this._isSelected);
  349. },
  350. /**
  351. * Returns the selected state of ccui.CheckBox.
  352. * @returns {boolean}
  353. */
  354. getSelectedState: function () {
  355. return this._isSelected;
  356. },
  357. _selectedEvent: function () {
  358. if(this._checkBoxEventSelector){
  359. if (this._checkBoxEventListener)
  360. this._checkBoxEventSelector.call(this._checkBoxEventListener, this, ccui.CheckBox.EVENT_SELECTED);
  361. else
  362. this._checkBoxEventSelector(this, ccui.CheckBox.EVENT_SELECTED);
  363. }
  364. },
  365. _unSelectedEvent: function () {
  366. if(this._checkBoxEventSelector){
  367. if (this._checkBoxEventListener)
  368. this._checkBoxEventSelector.call(this._checkBoxEventListener, this, ccui.CheckBox.EVENT_UNSELECTED);
  369. else
  370. this._checkBoxEventSelector(this, ccui.CheckBox.EVENT_UNSELECTED);
  371. }
  372. },
  373. _releaseUpEvent: function(){
  374. ccui.Widget.prototype._releaseUpEvent.call(this);
  375. if (this._isSelected){
  376. this.setSelectedState(false);
  377. this._unSelectedEvent();
  378. } else {
  379. this.setSelectedState(true);
  380. this._selectedEvent();
  381. }
  382. },
  383. /**
  384. * add event listener to ccui.CheckBox. it would called when checkbox is selected or unselected.
  385. * @param {Function} selector
  386. * @param {Object} [target=]
  387. * @deprecated since v3.0, please use addEventListener instead.
  388. */
  389. addEventListenerCheckBox: function (selector, target) {
  390. this.addEventListener(selector, target);
  391. },
  392. /**
  393. * add a call back function would called when checkbox is selected or unselected.
  394. * @param {Function} selector
  395. * @param {Object} [target=]
  396. */
  397. addEventListener: function(selector, target){
  398. this._checkBoxEventSelector = selector;
  399. this._checkBoxEventListener = target;
  400. },
  401. /**
  402. * Returns the content size of Renderer.
  403. * @returns {cc.Size}
  404. */
  405. getVirtualRendererSize: function(){
  406. return this._backGroundBoxRenderer.getContentSize();
  407. },
  408. _updateFlippedX: function () {
  409. this._backGroundBoxRenderer.setFlippedX(this._flippedX);
  410. this._backGroundSelectedBoxRenderer.setFlippedX(this._flippedX);
  411. this._frontCrossRenderer.setFlippedX(this._flippedX);
  412. this._backGroundBoxDisabledRenderer.setFlippedX(this._flippedX);
  413. this._frontCrossDisabledRenderer.setFlippedX(this._flippedX);
  414. },
  415. _updateFlippedY: function () {
  416. this._backGroundBoxRenderer.setFlippedY(this._flippedY);
  417. this._backGroundSelectedBoxRenderer.setFlippedY(this._flippedY);
  418. this._frontCrossRenderer.setFlippedY(this._flippedY);
  419. this._backGroundBoxDisabledRenderer.setFlippedY(this._flippedY);
  420. this._frontCrossDisabledRenderer.setFlippedY(this._flippedY);
  421. },
  422. _onSizeChanged: function () {
  423. ccui.Widget.prototype._onSizeChanged.call(this);
  424. this._backGroundBoxRendererAdaptDirty = true;
  425. this._backGroundSelectedBoxRendererAdaptDirty = true;
  426. this._frontCrossRendererAdaptDirty = true;
  427. this._backGroundBoxDisabledRendererAdaptDirty = true;
  428. this._frontCrossDisabledRendererAdaptDirty = true;
  429. },
  430. /**
  431. * override "getVirtualRenderer" method of widget.
  432. * @override
  433. * @returns {cc.Node} the renderer of ccui.CheckBox.
  434. */
  435. getVirtualRenderer: function () {
  436. return this._backGroundBoxRenderer;
  437. },
  438. _backGroundTextureScaleChangedWithSize: function () {
  439. var locRenderer = this._backGroundBoxRenderer, locContentSize = this._contentSize;
  440. if (this._ignoreSize)
  441. locRenderer.setScale(1.0);
  442. else{
  443. var textureSize = locRenderer.getContentSize();
  444. if (textureSize.width <= 0.0 || textureSize.height <= 0.0){
  445. locRenderer.setScale(1.0);
  446. return;
  447. }
  448. var scaleX = locContentSize.width / textureSize.width;
  449. var scaleY = locContentSize.height / textureSize.height;
  450. locRenderer.setScaleX(scaleX);
  451. locRenderer.setScaleY(scaleY);
  452. }
  453. locRenderer.setPosition(locContentSize.width * 0.5, locContentSize.height * 0.5);
  454. },
  455. _backGroundSelectedTextureScaleChangedWithSize: function () {
  456. var locRenderer = this._backGroundSelectedBoxRenderer, locContentSize = this._contentSize;
  457. if (this._ignoreSize)
  458. locRenderer.setScale(1.0);
  459. else {
  460. var textureSize = locRenderer.getContentSize();
  461. if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
  462. locRenderer.setScale(1.0);
  463. return;
  464. }
  465. var scaleX = locContentSize.width / textureSize.width;
  466. var scaleY = locContentSize.height / textureSize.height;
  467. locRenderer.setScaleX(scaleX);
  468. locRenderer.setScaleY(scaleY);
  469. }
  470. locRenderer.setPosition(locContentSize.width * 0.5, locContentSize.height * 0.5);
  471. },
  472. _frontCrossTextureScaleChangedWithSize: function () {
  473. var locRenderer = this._frontCrossRenderer, locContentSize = this._contentSize;
  474. if (this._ignoreSize)
  475. locRenderer.setScale(1.0);
  476. else {
  477. var textureSize = locRenderer.getContentSize();
  478. if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
  479. locRenderer.setScale(1.0);
  480. return;
  481. }
  482. var scaleX = locContentSize.width / textureSize.width;
  483. var scaleY = locContentSize.height / textureSize.height;
  484. locRenderer.setScaleX(scaleX);
  485. locRenderer.setScaleY(scaleY);
  486. }
  487. locRenderer.setPosition(locContentSize.width * 0.5, locContentSize.height * 0.5);
  488. },
  489. _backGroundDisabledTextureScaleChangedWithSize: function () {
  490. var locRenderer = this._backGroundBoxDisabledRenderer, locContentSize = this._contentSize;
  491. if (this._ignoreSize)
  492. locRenderer.setScale(1.0);
  493. else {
  494. var textureSize = locRenderer.getContentSize();
  495. if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
  496. locRenderer.setScale(1.0);
  497. return;
  498. }
  499. var scaleX = locContentSize.width / textureSize.width;
  500. var scaleY = locContentSize.height / textureSize.height;
  501. locRenderer.setScaleX(scaleX);
  502. locRenderer.setScaleY(scaleY);
  503. }
  504. locRenderer.setPosition(locContentSize.width * 0.5, locContentSize.height * 0.5);
  505. },
  506. _frontCrossDisabledTextureScaleChangedWithSize: function () {
  507. var locRenderer = this._frontCrossDisabledRenderer, locContentSize = this._contentSize;
  508. if (this._ignoreSize) {
  509. locRenderer.setScale(1.0);
  510. } else {
  511. var textureSize = locRenderer.getContentSize();
  512. if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
  513. locRenderer.setScale(1.0);
  514. return;
  515. }
  516. var scaleX = locContentSize.width / textureSize.width;
  517. var scaleY = locContentSize.height / textureSize.height;
  518. locRenderer.setScaleX(scaleX);
  519. locRenderer.setScaleY(scaleY);
  520. }
  521. locRenderer.setPosition(locContentSize.width * 0.5, locContentSize.height * 0.5);
  522. },
  523. /**
  524. * Returns the "class name" of widget.
  525. * @override
  526. * @returns {string}
  527. */
  528. getDescription: function () {
  529. return "CheckBox";
  530. },
  531. _createCloneInstance: function () {
  532. return ccui.CheckBox.create();
  533. },
  534. _copySpecialProperties: function (uiCheckBox) {
  535. if (uiCheckBox instanceof ccui.CheckBox) {
  536. this.loadTextureBackGround(uiCheckBox._backGroundFileName, uiCheckBox._backGroundTexType);
  537. this.loadTextureBackGroundSelected(uiCheckBox._backGroundSelectedFileName, uiCheckBox._backGroundSelectedTexType);
  538. this.loadTextureFrontCross(uiCheckBox._frontCrossFileName, uiCheckBox._frontCrossTexType);
  539. this.loadTextureBackGroundDisabled(uiCheckBox._backGroundDisabledFileName, uiCheckBox._backGroundDisabledTexType);
  540. this.loadTextureFrontCrossDisabled(uiCheckBox._frontCrossDisabledFileName, uiCheckBox._frontCrossDisabledTexType);
  541. this.setSelectedState(uiCheckBox._isSelected);
  542. this._checkBoxEventListener = uiCheckBox._checkBoxEventListener;
  543. this._checkBoxEventSelector = uiCheckBox._checkBoxEventSelector;
  544. }
  545. },
  546. _adaptRenderers: function(){
  547. if (this._backGroundBoxRendererAdaptDirty){
  548. this._backGroundTextureScaleChangedWithSize();
  549. this._backGroundBoxRendererAdaptDirty = false;
  550. }
  551. if (this._backGroundSelectedBoxRendererAdaptDirty) {
  552. this._backGroundSelectedTextureScaleChangedWithSize();
  553. this._backGroundSelectedBoxRendererAdaptDirty = false;
  554. }
  555. if (this._frontCrossRendererAdaptDirty){
  556. this._frontCrossTextureScaleChangedWithSize();
  557. this._frontCrossRendererAdaptDirty = false;
  558. }
  559. if (this._backGroundBoxDisabledRendererAdaptDirty) {
  560. this._backGroundDisabledTextureScaleChangedWithSize();
  561. this._backGroundBoxDisabledRendererAdaptDirty = false;
  562. }
  563. if (this._frontCrossDisabledRendererAdaptDirty) {
  564. this._frontCrossDisabledTextureScaleChangedWithSize();
  565. this._frontCrossDisabledRendererAdaptDirty = false;
  566. }
  567. }
  568. });
  569. var _p = ccui.CheckBox.prototype;
  570. // Extended properties
  571. /** @expose */
  572. _p.selected;
  573. cc.defineGetterSetter(_p, "selected", _p.getSelectedState, _p.setSelectedState);
  574. _p = null;
  575. /**
  576. * allocates and initializes a UICheckBox.
  577. * @deprecated since v3.0, please use new ccui.CheckBox() instead.
  578. * @param {string} [backGround] backGround texture.
  579. * @param {string} [backGroundSeleted] backGround selected state texture.
  580. * @param {string} [cross] cross texture.
  581. * @param {string} [backGroundDisabled] cross dark state texture.
  582. * @param {string} [frontCrossDisabled] cross dark state texture.
  583. * @param {Number} [texType]
  584. * @return {ccui.CheckBox}
  585. * @example
  586. * // example
  587. * var uiCheckBox = ccui.CheckBox.create();
  588. */
  589. ccui.CheckBox.create = function (backGround, backGroundSeleted, cross, backGroundDisabled, frontCrossDisabled, texType) {
  590. return new ccui.CheckBox(backGround, backGroundSeleted,cross,backGroundDisabled,frontCrossDisabled,texType);
  591. };
  592. // Constants
  593. //CheckBoxEvent type
  594. /**
  595. * The selected state of ccui.CheckBox's event.
  596. * @constant
  597. * @type {number}
  598. */
  599. ccui.CheckBox.EVENT_SELECTED = 0;
  600. /**
  601. * The unselected state of ccui.CheckBox's event.
  602. * @constant
  603. * @type {number}
  604. */
  605. ccui.CheckBox.EVENT_UNSELECTED = 1;
  606. //Render zorder
  607. /**
  608. * The normal background renderer's zOrder
  609. * @constant
  610. * @type {number}
  611. */
  612. ccui.CheckBox.BOX_RENDERER_ZORDER = -1;
  613. /**
  614. * The selected Background renderer's zOrder
  615. * @constant
  616. * @type {number}
  617. */
  618. ccui.CheckBox.BOX_SELECTED_RENDERER_ZORDER = -1;
  619. /**
  620. * The disabled Background renderer's zOrder
  621. * @constant
  622. * @type {number}
  623. */
  624. ccui.CheckBox.BOX_DISABLED_RENDERER_ZORDER = -1;
  625. /**
  626. * The normal front renderer's zOrder
  627. * @constant
  628. * @type {number}
  629. */
  630. ccui.CheckBox.FRONT_CROSS_RENDERER_ZORDER = -1;
  631. /**
  632. * The disabled front renderer's zOrder
  633. * @constant
  634. * @type {number}
  635. */
  636. ccui.CheckBox.FRONT_CROSS_DISABLED_RENDERER_ZORDER = -1;