CCMenuItem.js 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. cc._globalFontSize = cc.ITEM_SIZE;
  23. cc._globalFontName = "Arial";
  24. cc._globalFontNameRelease = false;
  25. /**
  26. * Subclass cc.MenuItem (or any subclass) to create your custom cc.MenuItem objects.
  27. * @class
  28. * @extends cc.Node
  29. * @param {function|String} callback
  30. * @param {cc.Node} target
  31. */
  32. cc.MenuItem = cc.Node.extend(/** @lends cc.MenuItem# */{
  33. _enabled: false,
  34. _target: null,
  35. _callback: null,
  36. _isSelected: false,
  37. _className: "MenuItem",
  38. /**
  39. * Constructor of cc.MenuItem
  40. * @param {function|String} callback
  41. * @param {cc.Node} target
  42. */
  43. ctor: function (callback, target) {
  44. var nodeP = cc.Node.prototype;
  45. nodeP.ctor.call(this);
  46. this._target = null;
  47. this._callback = null;
  48. this._isSelected = false;
  49. this._enabled = false;
  50. nodeP.setAnchorPoint.call(this, 0.5, 0.5);
  51. this._target = target || null;
  52. this._callback = callback || null;
  53. if (this._callback) {
  54. this._enabled = true;
  55. }
  56. },
  57. /**
  58. * return whether MenuItem is selected
  59. * @return {Boolean}
  60. */
  61. isSelected: function () {
  62. return this._isSelected;
  63. },
  64. /**
  65. * only use for jsbinding
  66. * @param value
  67. */
  68. setOpacityModifyRGB: function (value) {
  69. },
  70. /**
  71. * only use for jsbinding
  72. * @returns {boolean}
  73. */
  74. isOpacityModifyRGB: function () {
  75. return false;
  76. },
  77. /**
  78. * set the target/selector of the menu item
  79. * @param {function|String} selector
  80. * @param {cc.Node} rec
  81. * @deprecated since v3.0
  82. */
  83. setTarget: function (selector, rec) {
  84. this._target = rec;
  85. this._callback = selector;
  86. },
  87. /**
  88. * return whether MenuItem is Enabled
  89. * @return {Boolean}
  90. */
  91. isEnabled: function () {
  92. return this._enabled;
  93. },
  94. /**
  95. * set enable value of MenuItem
  96. * @param {Boolean} enable
  97. */
  98. setEnabled: function (enable) {
  99. this._enabled = enable;
  100. },
  101. /**
  102. * initializes a cc.MenuItem with callback
  103. * @param {function|String} callback
  104. * @param {cc.Node} target
  105. * @return {Boolean}
  106. */
  107. initWithCallback: function (callback, target) {
  108. this.anchorX = 0.5;
  109. this.anchorY = 0.5;
  110. this._target = target;
  111. this._callback = callback;
  112. this._enabled = true;
  113. this._isSelected = false;
  114. return true;
  115. },
  116. /**
  117. * return rect value of cc.MenuItem
  118. * @return {cc.Rect}
  119. */
  120. rect: function () {
  121. var locPosition = this._position, locContentSize = this._contentSize, locAnchorPoint = this._anchorPoint;
  122. return cc.rect(locPosition.x - locContentSize.width * locAnchorPoint.x,
  123. locPosition.y - locContentSize.height * locAnchorPoint.y,
  124. locContentSize.width, locContentSize.height);
  125. },
  126. /**
  127. * set the cc.MenuItem selected same as setIsSelected(true)
  128. */
  129. selected: function () {
  130. this._isSelected = true;
  131. },
  132. /**
  133. * set the cc.MenuItem unselected same as setIsSelected(false)
  134. */
  135. unselected: function () {
  136. this._isSelected = false;
  137. },
  138. /**
  139. * set the callback to the menu item
  140. * @param {function|String} callback
  141. * @param {cc.Node} target
  142. */
  143. setCallback: function (callback, target) {
  144. this._target = target;
  145. this._callback = callback;
  146. },
  147. /**
  148. * call the selector with target
  149. */
  150. activate: function () {
  151. if (this._enabled) {
  152. var locTarget = this._target, locCallback = this._callback;
  153. if (!locCallback)
  154. return;
  155. if (locTarget && (typeof(locCallback) == "string")) {
  156. locTarget[locCallback](this);
  157. } else if (locTarget && (typeof(locCallback) == "function")) {
  158. locCallback.call(locTarget, this);
  159. } else
  160. locCallback(this);
  161. }
  162. }
  163. });
  164. var _p = cc.MenuItem.prototype;
  165. // Extended properties
  166. /** @expose */
  167. _p.enabled;
  168. cc.defineGetterSetter(_p, "enabled", _p.isEnabled, _p.setEnabled);
  169. /**
  170. * creates an empty menu item with target and callback<br/>
  171. * Not recommended to use the base class, should use more defined menu item classes
  172. * @deprecated since v3.0, please use new cc.MenuItem(callback,target) instead
  173. * @param {function|String} callback callback
  174. * @param {cc.Node} target
  175. * @return {cc.MenuItem}
  176. */
  177. cc.MenuItem.create = function (callback, target) {
  178. return new cc.MenuItem(callback, target);
  179. };
  180. /**
  181. * Any cc.Node that supports the cc.LabelProtocol protocol can be added.<br/>
  182. * Supported nodes:<br/>
  183. * - cc.BitmapFontAtlas<br/>
  184. * - cc.LabelAtlas<br/>
  185. * - cc.LabelTTF<br/>
  186. * @class
  187. * @extends cc.MenuItem
  188. * @param {cc.Node} label
  189. * @param {function|String} selector
  190. * @param {cc.Node} target
  191. * @example
  192. * var menuitemLabel = new cc.MenuItemLabel(label,selector,target);
  193. *
  194. * @property {String} string - Content string of label item
  195. * @property {cc.Node} label - Label of label item
  196. * @property {cc.Color} disabledColor - Color of label when it's diabled
  197. */
  198. cc.MenuItemLabel = cc.MenuItem.extend(/** @lends cc.MenuItemLabel# */{
  199. _disabledColor: null,
  200. _label: null,
  201. _orginalScale: 0,
  202. _colorBackup: null,
  203. /**
  204. * Constructor of cc.MenuItemLabel
  205. * @param {cc.Node} label
  206. * @param {function|String} selector
  207. * @param {cc.Node} target
  208. */
  209. ctor: function (label, selector, target) {
  210. cc.MenuItem.prototype.ctor.call(this, selector, target);
  211. this._disabledColor = null;
  212. this._label = null;
  213. this._orginalScale = 0;
  214. this._colorBackup = null;
  215. if (label) {
  216. this._originalScale = 1.0;
  217. this._colorBackup = cc.color.WHITE;
  218. this._disabledColor = cc.color(126, 126, 126);
  219. this.setLabel(label);
  220. this.cascadeColor = true;
  221. this.cascadeOpacity = true;
  222. }
  223. },
  224. /**
  225. * return the disable color for this cc.MenuItemLabel
  226. * @return {cc.Color}
  227. */
  228. getDisabledColor: function () {
  229. return this._disabledColor;
  230. },
  231. /**
  232. * set the disable color for this cc.MenuItemLabel
  233. * @param {cc.Color} color
  234. */
  235. setDisabledColor: function (color) {
  236. this._disabledColor = color;
  237. },
  238. /**
  239. * return label of cc.MenuItemLabel
  240. * @return {cc.Node}
  241. */
  242. getLabel: function () {
  243. return this._label;
  244. },
  245. /**
  246. * set a label for cc.MenuItemLabel
  247. * @param {cc.Node} label
  248. */
  249. setLabel: function (label) {
  250. if (label) {
  251. this.addChild(label);
  252. label.anchorX = 0;
  253. label.anchorY = 0;
  254. this.width = label.width;
  255. this.height = label.height;
  256. }
  257. if (this._label) {
  258. this.removeChild(this._label, true);
  259. }
  260. this._label = label;
  261. },
  262. /**
  263. * set enable value to cc.MenuItemLabel
  264. * @param {Boolean} enabled
  265. */
  266. setEnabled: function (enabled) {
  267. if (this._enabled != enabled) {
  268. var locLabel = this._label;
  269. if (!enabled) {
  270. this._colorBackup = locLabel.color;
  271. locLabel.color = this._disabledColor;
  272. } else {
  273. locLabel.color = this._colorBackup;
  274. }
  275. }
  276. cc.MenuItem.prototype.setEnabled.call(this, enabled);
  277. },
  278. /**
  279. * set opacity for cc.MenuItemLabel
  280. * @param {Number} opacity from 0-255
  281. */
  282. setOpacity: function (opacity) {
  283. this._label.opacity = opacity;
  284. },
  285. /**
  286. * return the opacity of cc.MenuItemLabel
  287. * @return {Number}
  288. */
  289. getOpacity: function () {
  290. return this._label.opacity;
  291. },
  292. /**
  293. * set the opacity for cc.MenuItemLabel
  294. * @param {cc.Color} color
  295. */
  296. setColor: function (color) {
  297. this._label.color = color;
  298. },
  299. /**
  300. * return the color of cc.MenuItemLabel
  301. * @return {cc.Color}
  302. */
  303. getColor: function () {
  304. return this._label.color;
  305. },
  306. /**
  307. * initializes a cc.MenuItemLabel with a label
  308. * @param {cc.Node} label
  309. * @param {function|String} selector
  310. * @param {cc.Node} target
  311. * @return {Boolean}
  312. */
  313. initWithLabel: function (label, selector, target) {
  314. this.initWithCallback(selector, target);
  315. this._originalScale = 1.0;
  316. this._colorBackup = cc.color.WHITE;
  317. this._disabledColor = cc.color(126, 126, 126);
  318. this.setLabel(label);
  319. this.cascadeColor = true;
  320. this.cascadeOpacity = true;
  321. return true;
  322. },
  323. /**
  324. * set the string for cc.MenuItemLabel
  325. * @param {String} label
  326. */
  327. setString: function (label) {
  328. this._label.string = label;
  329. this.width = this._label.width;
  330. this.height = this._label.height;
  331. },
  332. /**
  333. * return the string of cc.MenuItemLabel
  334. * @returns {*|string|_p.string|ret.string|q.string|String}
  335. */
  336. getString: function () {
  337. return this._label.string;
  338. },
  339. /**
  340. * activate the menu item
  341. */
  342. activate: function () {
  343. if (this._enabled) {
  344. this.stopAllActions();
  345. this.scale = this._originalScale;
  346. cc.MenuItem.prototype.activate.call(this);
  347. }
  348. },
  349. /**
  350. * menu item is selected (runs callback)
  351. */
  352. selected: function () {
  353. if (this._enabled) {
  354. cc.MenuItem.prototype.selected.call(this);
  355. var action = this.getActionByTag(cc.ZOOM_ACTION_TAG);
  356. if (action)
  357. this.stopAction(action);
  358. else
  359. this._originalScale = this.scale;
  360. var zoomAction = cc.ScaleTo.create(0.1, this._originalScale * 1.2);
  361. zoomAction.setTag(cc.ZOOM_ACTION_TAG);
  362. this.runAction(zoomAction);
  363. }
  364. },
  365. /**
  366. * menu item goes back to unselected state
  367. */
  368. unselected: function () {
  369. if (this._enabled) {
  370. cc.MenuItem.prototype.unselected.call(this);
  371. this.stopActionByTag(cc.ZOOM_ACTION_TAG);
  372. var zoomAction = cc.ScaleTo.create(0.1, this._originalScale);
  373. zoomAction.setTag(cc.ZOOM_ACTION_TAG);
  374. this.runAction(zoomAction);
  375. }
  376. }
  377. });
  378. var _p = cc.MenuItemLabel.prototype;
  379. // Extended properties
  380. /** @expose */
  381. _p.string;
  382. cc.defineGetterSetter(_p, "string", _p.getString, _p.setString);
  383. /** @expose */
  384. _p.disabledColor;
  385. cc.defineGetterSetter(_p, "disabledColor", _p.getDisabledColor, _p.setDisabledColor);
  386. /** @expose */
  387. _p.label;
  388. cc.defineGetterSetter(_p, "label", _p.getLabel, _p.setLabel);
  389. /**
  390. * @deprecated since v3.0 ,please use new cc.MenuItemLabel(label,selector,target) instead
  391. * @param {cc.Node} label
  392. * @param {function|String|Null} [selector=]
  393. * @param {cc.Node|Null} [target=]
  394. * @return {cc.MenuItemLabel}
  395. */
  396. cc.MenuItemLabel.create = function (label, selector, target) {
  397. return new cc.MenuItemLabel(label, selector, target);
  398. };
  399. /**
  400. * Helper class that creates a MenuItemLabel class with a LabelAtlas
  401. * @class
  402. * @extends cc.MenuItemLabel
  403. * @param {String} value
  404. * @param {String} charMapFile
  405. * @param {Number} itemWidth
  406. * @param {Number} itemHeight
  407. * @param {String} startCharMap a single character
  408. * @param {function|String|Null} callback
  409. * @param {cc.Node|Null} target
  410. * @example
  411. * var menuItem = new cc.MenuItemAtlasFont(param1,param2...);
  412. */
  413. cc.MenuItemAtlasFont = cc.MenuItemLabel.extend(/** @lends cc.MenuItemAtlasFont# */{
  414. /**
  415. * the contructor of cc.MenuItemAtlasFont
  416. * @param {String} value
  417. * @param {String} charMapFile
  418. * @param {Number} itemWidth
  419. * @param {Number} itemHeight
  420. * @param {String} startCharMap a single character
  421. * @param {function|String|Null} callback
  422. * @param {cc.Node|Null} target
  423. */
  424. ctor: function (value, charMapFile, itemWidth, itemHeight, startCharMap, callback, target) {
  425. var label;
  426. if (value && value.length > 0) {
  427. label = cc.LabelAtlas.create(value, charMapFile, itemWidth, itemHeight, startCharMap);
  428. }
  429. cc.MenuItemLabel.prototype.ctor.call(this, label, callback, target);
  430. },
  431. /**
  432. * initializes a cc.MenuItemAtlasFont with string
  433. * @param {String} value
  434. * @param {String} charMapFile
  435. * @param {Number} itemWidth
  436. * @param {Number} itemHeight
  437. * @param {String} startCharMap a single character
  438. * @param {function|String|Null} callback
  439. * @param {cc.Node|Null} target
  440. * @return {Boolean}
  441. */
  442. initWithString: function (value, charMapFile, itemWidth, itemHeight, startCharMap, callback, target) {
  443. if (!value || value.length == 0)
  444. throw "cc.MenuItemAtlasFont.initWithString(): value should be non-null and its length should be greater than 0";
  445. var label = new cc.LabelAtlas();
  446. label.initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap);
  447. if (this.initWithLabel(label, callback, target)) {
  448. // do something ?
  449. }
  450. return true;
  451. }
  452. });
  453. /**
  454. * create menu item from string with font
  455. * @deprecated since v3.0 ,please use new cc.MenuItemAtlasFont() instead.
  456. * @param {String} value the text to display
  457. * @param {String} charMapFile the character map file
  458. * @param {Number} itemWidth
  459. * @param {Number} itemHeight
  460. * @param {String} startCharMap a single character
  461. * @param {function|String|Null} [callback=null]
  462. * @param {cc.Node|Null} [target=]
  463. * @return {cc.MenuItemAtlasFont}
  464. * @example
  465. * // Example
  466. * var item = cc.MenuItemAtlasFont.create('text to display', 'font.fnt', 12, 32, ' ')
  467. *
  468. * //OR
  469. * var item = cc.MenuItemAtlasFont.create('text to display', 'font.fnt', 12, 32, ' ', game.run, game)
  470. */
  471. cc.MenuItemAtlasFont.create = function (value, charMapFile, itemWidth, itemHeight, startCharMap, callback, target) {
  472. return new cc.MenuItemAtlasFont(value, charMapFile, itemWidth, itemHeight, startCharMap, callback, target);
  473. };
  474. /**
  475. * Helper class that creates a CCMenuItemLabel class with a Label
  476. * @class
  477. * @extends cc.MenuItemLabel
  478. * @param {String} value text for the menu item
  479. * @param {function|String} callback
  480. * @param {cc.Node} target
  481. * @example
  482. * var menuItem = new cc.MenuItemFont(value, callback, target);
  483. *
  484. * @property {Number} fontSize - Font size of font item
  485. * @property {String} fontName - Font name of font item
  486. */
  487. cc.MenuItemFont = cc.MenuItemLabel.extend(/** @lends cc.MenuItemFont# */{
  488. _fontSize: null,
  489. _fontName: null,
  490. /**
  491. * Constructor of cc.MenuItemFont
  492. * @param {String} value text for the menu item
  493. * @param {function|String} callback
  494. * @param {cc.Node} target
  495. */
  496. ctor: function (value, callback, target) {
  497. var label;
  498. if (value && value.length > 0) {
  499. this._fontName = cc._globalFontName;
  500. this._fontSize = cc._globalFontSize;
  501. label = cc.LabelTTF.create(value, this._fontName, this._fontSize);
  502. }
  503. else {
  504. this._fontSize = 0;
  505. this._fontName = "";
  506. }
  507. cc.MenuItemLabel.prototype.ctor.call(this, label, callback, target);
  508. },
  509. /**
  510. * initializes cc.MenuItemFont with string
  511. * @param {String} value text for the menu item
  512. * @param {function|String} callback
  513. * @param {cc.Node} target
  514. * @return {Boolean}
  515. */
  516. initWithString: function (value, callback, target) {
  517. if (!value || value.length == 0)
  518. throw "Value should be non-null and its length should be greater than 0";
  519. this._fontName = cc._globalFontName;
  520. this._fontSize = cc._globalFontSize;
  521. var label = cc.LabelTTF.create(value, this._fontName, this._fontSize);
  522. if (this.initWithLabel(label, callback, target)) {
  523. // do something ?
  524. }
  525. return true;
  526. },
  527. /**
  528. * set the font size for cc.MenuItemFont
  529. * @param {Number} s
  530. */
  531. setFontSize: function (s) {
  532. this._fontSize = s;
  533. this._recreateLabel();
  534. },
  535. /**
  536. *return the font size of cc.MenuItemFont
  537. * @return {Number}
  538. */
  539. getFontSize: function () {
  540. return this._fontSize;
  541. },
  542. /**
  543. * set the font name for cc.MenuItemFont
  544. * @param {String} name
  545. */
  546. setFontName: function (name) {
  547. this._fontName = name;
  548. this._recreateLabel();
  549. },
  550. /**
  551. * return the font name for cc.MenuItemFont
  552. * @return {String}
  553. */
  554. getFontName: function () {
  555. return this._fontName;
  556. },
  557. _recreateLabel: function () {
  558. var label = cc.LabelTTF.create(this._label.string, this._fontName, this._fontSize);
  559. this.setLabel(label);
  560. }
  561. });
  562. /**
  563. * a shared function to set the fontSize for menuitem font
  564. * @param {Number} fontSize
  565. */
  566. cc.MenuItemFont.setFontSize = function (fontSize) {
  567. cc._globalFontSize = fontSize;
  568. };
  569. /**
  570. * a shared function to get the font size for menuitem font
  571. * @return {Number}
  572. */
  573. cc.MenuItemFont.fontSize = function () {
  574. return cc._globalFontSize;
  575. };
  576. /**
  577. * a shared function to set the fontsize for menuitem font
  578. * @param name
  579. */
  580. cc.MenuItemFont.setFontName = function (name) {
  581. if (cc._globalFontNameRelease) {
  582. cc._globalFontName = '';
  583. }
  584. cc._globalFontName = name;
  585. cc._globalFontNameRelease = true;
  586. };
  587. var _p = cc.MenuItemFont.prototype;
  588. // Extended properties
  589. /** @expose */
  590. _p.fontSize;
  591. cc.defineGetterSetter(_p, "fontSize", _p.getFontSize, _p.setFontSize);
  592. /** @expose */
  593. _p.fontName;
  594. cc.defineGetterSetter(_p, "fontName", _p.getFontName, _p.setFontName);
  595. /**
  596. * a shared function to get the font name for menuitem font
  597. * @return {String}
  598. */
  599. cc.MenuItemFont.fontName = function () {
  600. return cc._globalFontName;
  601. };
  602. /**
  603. * create a menu item from string
  604. * @deprecated
  605. * @param {String} value the text to display
  606. * @param {String|function|Null} callback the callback to run, either in function name or pass in the actual function
  607. * @param {cc.Node|Null} target the target to run callback
  608. * @return {cc.MenuItemFont}
  609. * @example
  610. * // Example
  611. * var item = cc.MenuItemFont.create("Game start", 'start', Game)
  612. * //creates a menu item from string "Game start", and when clicked, it will run Game.start()
  613. *
  614. * var item = cc.MenuItemFont.create("Game start", game.start, Game)//same as above
  615. *
  616. * var item = cc.MenuItemFont.create("i do nothing")//create a text menu item that does nothing
  617. *
  618. * //you can set font size and name before or after
  619. * cc.MenuItemFont.setFontName('my Fancy Font');
  620. * cc.MenuItemFont.setFontSize(62);
  621. */
  622. cc.MenuItemFont.create = function (value, callback, target) {
  623. return new cc.MenuItemFont(value, callback, target);
  624. };
  625. /**
  626. * CCMenuItemSprite accepts CCNode<CCRGBAProtocol> objects as items.<br/>
  627. * The images has 3 different states:<br/>
  628. * - unselected image<br/>
  629. * - selected image<br/>
  630. * - disabled image<br/>
  631. * @class
  632. * @extends cc.MenuItem
  633. * @param {Image|Null} normalSprite normal state image
  634. * @param {Image|Null} selectedSprite selected state image
  635. * @param {Image|cc.Node|Null} three disabled state image OR target node
  636. * @param {String|function|cc.Node|Null} four callback function name in string or actual function, OR target Node
  637. * @param {String|function|Null} five callback function name in string or actual function
  638. *
  639. * @example
  640. * var item = new cc.MenuItemSprite(normalImage)//create a menu item from a sprite with no functionality
  641. * var item = new cc.MenuItemSprite(normalImage, selectedImage)//create a menu Item, nothing will happen when clicked
  642. * var item = new cc.MenuItemSprite(normalImage, SelectedImage, disabledImage)//same above, but with disabled state image
  643. * var item = new cc.MenuItemSprite(normalImage, SelectedImage, 'callback', targetNode)//create a menu item, when clicked runs targetNode.callback()
  644. * var item = new cc.MenuItemSprite(normalImage, SelectedImage, disabledImage, targetNode.callback, targetNode)
  645. * //same as above, but with disabled image, and passing in callback function
  646. *
  647. * @property {cc.Sprite} normalImage - Sprite in normal state
  648. * @property {cc.Sprite} selectedImage - Sprite in selected state
  649. * @property {cc.Sprite} disabledImage - Sprite in disabled state
  650. */
  651. cc.MenuItemSprite = cc.MenuItem.extend(/** @lends cc.MenuItemSprite# */{
  652. _normalImage: null,
  653. _selectedImage: null,
  654. _disabledImage: null,
  655. /**
  656. * Constructor of cc.MenuItemSprite
  657. * @param {Image|Null} normalSprite normal state image
  658. * @param {Image|Null} selectedSprite selected state image
  659. * @param {Image|cc.Node|Null} three disabled state image OR target node
  660. * @param {String|function|cc.Node|Null} four callback function name in string or actual function, OR target Node
  661. * @param {String|function|Null} five callback function name in string or actual function
  662. */
  663. ctor: function (normalSprite, selectedSprite, three, four, five) {
  664. cc.MenuItem.prototype.ctor.call(this);
  665. this._normalImage = null;
  666. this._selectedImage = null;
  667. this._disabledImage = null;
  668. if (selectedSprite !== undefined) {
  669. normalSprite = normalSprite;
  670. selectedSprite = selectedSprite;
  671. var disabledImage, target, callback;
  672. //when you send 4 arguments, five is undefined
  673. if (five !== undefined) {
  674. disabledImage = three;
  675. callback = four;
  676. target = five;
  677. } else if (four !== undefined && typeof four === "function") {
  678. disabledImage = three;
  679. callback = four;
  680. } else if (four !== undefined && typeof three === "function") {
  681. target = four;
  682. callback = three;
  683. disabledImage = cc.Sprite.create(selectedSprite);
  684. } else if (three === undefined) {
  685. disabledImage = cc.Sprite.create(selectedSprite);
  686. }
  687. this.initWithNormalSprite(normalSprite, selectedSprite, disabledImage, callback, target);
  688. }
  689. },
  690. /**
  691. * return the normal status image(cc.Sprite)
  692. * @return {cc.Sprite}
  693. */
  694. getNormalImage: function () {
  695. return this._normalImage;
  696. },
  697. /**
  698. * set the normal status image(cc.Sprite)
  699. * @param {cc.Sprite} normalImage
  700. */
  701. setNormalImage: function (normalImage) {
  702. if (this._normalImage == normalImage) {
  703. return;
  704. }
  705. if (normalImage) {
  706. this.addChild(normalImage, 0, cc.NORMAL_TAG);
  707. normalImage.anchorX = 0;
  708. normalImage.anchorY = 0;
  709. }
  710. if (this._normalImage) {
  711. this.removeChild(this._normalImage, true);
  712. }
  713. this._normalImage = normalImage;
  714. this.width = this._normalImage.width;
  715. this.height = this._normalImage.height;
  716. this._updateImagesVisibility();
  717. if (normalImage.textureLoaded && !normalImage.textureLoaded()) {
  718. normalImage.addLoadedEventListener(function (sender) {
  719. this.width = sender.width;
  720. this.height = sender.height;
  721. }, this);
  722. }
  723. },
  724. /**
  725. * return the selected status image(cc.Sprite) of cc.MenuItemSprite
  726. * @return {cc.Sprite}
  727. */
  728. getSelectedImage: function () {
  729. return this._selectedImage;
  730. },
  731. /**
  732. * set the selected status image(cc.Sprite)
  733. * @param {cc.Sprite} selectedImage
  734. */
  735. setSelectedImage: function (selectedImage) {
  736. if (this._selectedImage == selectedImage)
  737. return;
  738. if (selectedImage) {
  739. this.addChild(selectedImage, 0, cc.SELECTED_TAG);
  740. selectedImage.anchorX = 0;
  741. selectedImage.anchorY = 0;
  742. }
  743. if (this._selectedImage) {
  744. this.removeChild(this._selectedImage, true);
  745. }
  746. this._selectedImage = selectedImage;
  747. this._updateImagesVisibility();
  748. },
  749. /**
  750. * return the disabled status of cc.MenuItemSprite
  751. * @return {cc.Sprite}
  752. */
  753. getDisabledImage: function () {
  754. return this._disabledImage;
  755. },
  756. /**
  757. * set the disabled status image(cc.Sprite)
  758. * @param {cc.Sprite} disabledImage
  759. */
  760. setDisabledImage: function (disabledImage) {
  761. if (this._disabledImage == disabledImage)
  762. return;
  763. if (disabledImage) {
  764. this.addChild(disabledImage, 0, cc.DISABLE_TAG);
  765. disabledImage.anchorX = 0;
  766. disabledImage.anchorY = 0;
  767. }
  768. if (this._disabledImage)
  769. this.removeChild(this._disabledImage, true);
  770. this._disabledImage = disabledImage;
  771. this._updateImagesVisibility();
  772. },
  773. /**
  774. * initializes cc.MenuItemSprite with a cc.Sprite
  775. * @param {cc.Node} normalSprite
  776. * @param {cc.Node} selectedSprite
  777. * @param {cc.Node} disabledSprite
  778. * @param {function|String} callback
  779. * @param {cc.Node} target
  780. * @return {Boolean}
  781. */
  782. initWithNormalSprite: function (normalSprite, selectedSprite, disabledSprite, callback, target) {
  783. this.initWithCallback(callback, target);
  784. this.setNormalImage(normalSprite);
  785. this.setSelectedImage(selectedSprite);
  786. this.setDisabledImage(disabledSprite);
  787. var locNormalImage = this._normalImage;
  788. if (locNormalImage) {
  789. this.width = locNormalImage.width;
  790. this.height = locNormalImage.height;
  791. if (locNormalImage.textureLoaded && !locNormalImage.textureLoaded()) {
  792. locNormalImage.addLoadedEventListener(function (sender) {
  793. this.width = sender.width;
  794. this.height = sender.height;
  795. this.cascadeColor = true;
  796. this.cascadeOpacity = true;
  797. }, this);
  798. }
  799. }
  800. this.cascadeColor = true;
  801. this.cascadeOpacity = true;
  802. return true;
  803. },
  804. /**
  805. * set the color for cc.MenuItemSprite
  806. * @param {cc.Color} color
  807. */
  808. setColor: function (color) {
  809. this._normalImage.color = color;
  810. if (this._selectedImage)
  811. this._selectedImage.color = color;
  812. if (this._disabledImage)
  813. this._disabledImage.color = color;
  814. },
  815. /**
  816. * return the color of cc.MenuItemSprite
  817. * @return {cc.Color}
  818. */
  819. getColor: function () {
  820. return this._normalImage.color;
  821. },
  822. /**
  823. * set the opacity for cc.MenuItemSprite
  824. * @param {Number} opacity 0 - 255
  825. */
  826. setOpacity: function (opacity) {
  827. this._normalImage.opacity = opacity;
  828. if (this._selectedImage)
  829. this._selectedImage.opacity = opacity;
  830. if (this._disabledImage)
  831. this._disabledImage.opacity = opacity;
  832. },
  833. /**
  834. * return the opacity of cc.MenuItemSprite
  835. * @return {Number} opacity from 0 - 255
  836. */
  837. getOpacity: function () {
  838. return this._normalImage.opacity;
  839. },
  840. /**
  841. * menu item is selected (runs callback)
  842. */
  843. selected: function () {
  844. cc.MenuItem.prototype.selected.call(this);
  845. if (this._normalImage) {
  846. if (this._disabledImage)
  847. this._disabledImage.visible = false;
  848. if (this._selectedImage) {
  849. this._normalImage.visible = false;
  850. this._selectedImage.visible = true;
  851. } else
  852. this._normalImage.visible = true;
  853. }
  854. },
  855. /**
  856. * menu item goes back to unselected state
  857. */
  858. unselected: function () {
  859. cc.MenuItem.prototype.unselected.call(this);
  860. if (this._normalImage) {
  861. this._normalImage.visible = true;
  862. if (this._selectedImage)
  863. this._selectedImage.visible = false;
  864. if (this._disabledImage)
  865. this._disabledImage.visible = false;
  866. }
  867. },
  868. /**
  869. * set cc.MenuItemSprite enable to receive the touch event
  870. * @param {Boolean} bEnabled
  871. */
  872. setEnabled: function (bEnabled) {
  873. if (this._enabled != bEnabled) {
  874. cc.MenuItem.prototype.setEnabled.call(this, bEnabled);
  875. this._updateImagesVisibility();
  876. }
  877. },
  878. _updateImagesVisibility: function () {
  879. var locNormalImage = this._normalImage, locSelImage = this._selectedImage, locDisImage = this._disabledImage;
  880. if (this._enabled) {
  881. if (locNormalImage)
  882. locNormalImage.visible = true;
  883. if (locSelImage)
  884. locSelImage.visible = false;
  885. if (locDisImage)
  886. locDisImage.visible = false;
  887. } else {
  888. if (locDisImage) {
  889. if (locNormalImage)
  890. locNormalImage.visible = false;
  891. if (locSelImage)
  892. locSelImage.visible = false;
  893. if (locDisImage)
  894. locDisImage.visible = true;
  895. } else {
  896. if (locNormalImage)
  897. locNormalImage.visible = true;
  898. if (locSelImage)
  899. locSelImage.visible = false;
  900. }
  901. }
  902. }
  903. });
  904. var _p = cc.MenuItemSprite.prototype;
  905. // Extended properties
  906. /** @expose */
  907. _p.normalImage;
  908. cc.defineGetterSetter(_p, "normalImage", _p.getNormalImage, _p.setNormalImage);
  909. /** @expose */
  910. _p.selectedImage;
  911. cc.defineGetterSetter(_p, "selectedImage", _p.getSelectedImage, _p.setSelectedImage);
  912. /** @expose */
  913. _p.disabledImage;
  914. cc.defineGetterSetter(_p, "disabledImage", _p.getDisabledImage, _p.setDisabledImage);
  915. /**
  916. * create a menu item from sprite
  917. * @deprecated since v3.0 please use new cc.MenuItemSprite(normalSprite, selectedSprite, three, four, five) instead
  918. * @param {Image} normalSprite normal state image
  919. * @param {Image|Null} selectedSprite selected state image
  920. * @param {Image|cc.Node|Null} three disabled state image OR target node
  921. * @param {String|function|cc.Node|Null} four callback function name in string or actual function, OR target Node
  922. * @param {String|function|Null} five callback function name in string or actual function
  923. * @return {cc.MenuItemSprite}
  924. * @example
  925. * // Example
  926. * var item = cc.MenuItemSprite.create(normalImage)//create a menu item from a sprite with no functionality
  927. *
  928. * var item = cc.MenuItemSprite.create(normalImage, selectedImage)//create a menu Item, nothing will happen when clicked
  929. *
  930. * var item = cc.MenuItemSprite.create(normalImage, SelectedImage, disabledImage)//same above, but with disabled state image
  931. *
  932. * var item = cc.MenuItemSprite.create(normalImage, SelectedImage, 'callback', targetNode)//create a menu item, when clicked runs targetNode.callback()
  933. *
  934. * var item = cc.MenuItemSprite.create(normalImage, SelectedImage, disabledImage, targetNode.callback, targetNode)
  935. * //same as above, but with disabled image, and passing in callback function
  936. */
  937. cc.MenuItemSprite.create = function (normalSprite, selectedSprite, three, four, five) {
  938. return new cc.MenuItemSprite(normalSprite, selectedSprite, three, four, five || undefined);
  939. };
  940. /**
  941. * cc.MenuItemImage accepts images as items.<br/>
  942. * The images has 3 different states:<br/>
  943. * - unselected image<br/>
  944. * - selected image<br/>
  945. * - disabled image<br/>
  946. * <br/>
  947. * For best results try that all images are of the same size<br/>
  948. * @class
  949. * @extends cc.MenuItemSprite
  950. * @param {string|null} normalImage
  951. * @param {string|null} selectedImage
  952. * @param {string|null} disabledImage
  953. * @param {function|string|null} callback
  954. * @param {cc.Node|null} target
  955. * @example
  956. * var menuItem = new cc.MenuItemImage(normalImage, selectedImage, three, four, five);
  957. */
  958. cc.MenuItemImage = cc.MenuItemSprite.extend(/** @lends cc.MenuItemImage# */{
  959. /**
  960. * Constructor of cc.MenuItemImage
  961. * @param {string|null} normalImage
  962. * @param {string|null} selectedImage
  963. * @param {string|null} disabledImage
  964. * @param {function|string|null} callback
  965. * @param {cc.Node|null} target
  966. */
  967. ctor: function (normalImage, selectedImage, three, four, five) {
  968. var normalSprite = null,
  969. selectedSprite = null,
  970. disabledSprite = null,
  971. callback = null,
  972. target = null;
  973. if (normalImage === undefined) {
  974. cc.MenuItemSprite.prototype.ctor.call(this);
  975. }
  976. else {
  977. normalSprite = cc.Sprite.create(normalImage);
  978. selectedImage &&
  979. (selectedSprite = cc.Sprite.create(selectedImage));
  980. if (four === undefined) {
  981. callback = three;
  982. }
  983. else if (five === undefined) {
  984. callback = three;
  985. target = four;
  986. }
  987. else if (five) {
  988. disabledSprite = cc.Sprite.create(three);
  989. callback = four;
  990. target = five;
  991. }
  992. cc.MenuItemSprite.prototype.ctor.call(this, normalSprite, selectedSprite, disabledSprite, callback, target);
  993. }
  994. },
  995. /**
  996. * sets the sprite frame for the normal image
  997. * @param {cc.SpriteFrame} frame
  998. */
  999. setNormalSpriteFrame: function (frame) {
  1000. this.setNormalImage(cc.Sprite.create(frame));
  1001. },
  1002. /**
  1003. * sets the sprite frame for the selected image
  1004. * @param {cc.SpriteFrame} frame
  1005. */
  1006. setSelectedSpriteFrame: function (frame) {
  1007. this.setSelectedImage(cc.Sprite.create(frame));
  1008. },
  1009. /**
  1010. * sets the sprite frame for the disabled image
  1011. * @param {cc.SpriteFrame} frame
  1012. */
  1013. setDisabledSpriteFrame: function (frame) {
  1014. this.setDisabledImage(cc.Sprite.create(frame));
  1015. },
  1016. /**
  1017. * initializes a cc.MenuItemImage
  1018. * @param {string|null} normalImage
  1019. * @param {string|null} selectedImage
  1020. * @param {string|null} disabledImage
  1021. * @param {function|string|null} callback
  1022. * @param {cc.Node|null} target
  1023. * @returns {boolean}
  1024. */
  1025. initWithNormalImage: function (normalImage, selectedImage, disabledImage, callback, target) {
  1026. var normalSprite = null;
  1027. var selectedSprite = null;
  1028. var disabledSprite = null;
  1029. if (normalImage) {
  1030. normalSprite = cc.Sprite.create(normalImage);
  1031. }
  1032. if (selectedImage) {
  1033. selectedSprite = cc.Sprite.create(selectedImage);
  1034. }
  1035. if (disabledImage) {
  1036. disabledSprite = cc.Sprite.create(disabledImage);
  1037. }
  1038. return this.initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, callback, target);
  1039. }
  1040. });
  1041. /**
  1042. * creates a new menu item image
  1043. * @deprecated since v3.0, please use new cc.MenuItemImage(normalImage, selectedImage, three, four, five) instead.
  1044. * @param {String} normalImage file name for normal state
  1045. * @param {String} selectedImage image for selected state
  1046. * @param {String|cc.Node} three Disabled image OR callback function
  1047. * @param {String|function|Null} [four] callback function, either name in string or pass the whole function OR the target
  1048. * @param {cc.Node|String|function|Null} [five] cc.Node target to run callback when clicked
  1049. * @return {cc.MenuItemImage}
  1050. * @example
  1051. * // Example
  1052. * //create a dom menu item with normal and selected state, when clicked it will run the run function from gameScene object
  1053. * var item = cc.MenuItemImage.create('normal.png', 'selected.png', 'run', gameScene)
  1054. *
  1055. * //same as above, but pass in the actual function and disabled image
  1056. * var item = cc.MenuItemImage.create('normal.png', 'selected.png', 'disabled.png', gameScene.run, gameScene)
  1057. */
  1058. cc.MenuItemImage.create = function (normalImage, selectedImage, three, four, five) {
  1059. return new cc.MenuItemImage(normalImage, selectedImage, three, four, five);
  1060. };
  1061. /**
  1062. * A simple container class that "toggles" it's inner items<br/>
  1063. * The inner items can be any MenuItem
  1064. * @class
  1065. * @extends cc.MenuItem
  1066. *
  1067. * @property {Array} subItems - Sub items
  1068. * @property {Number} selectedIndex - Index of selected sub item
  1069. *
  1070. *@example
  1071. * // Example
  1072. * //create a toggle item with 2 menu items (which you can then toggle between them later)
  1073. * var toggler = new cc.MenuItemToggle( cc.MenuItemFont.create("On"), cc.MenuItemFont.create("Off"), this.callback, this)
  1074. * //Note: the first param is the target, the second is the callback function, afterwards, you can pass in any number of menuitems
  1075. *
  1076. * //if you pass only 1 variable, then it must be a cc.MenuItem
  1077. * var notYetToggler = new cc.MenuItemToggle(cc.MenuItemFont.create("On"));//it is useless right now, until you add more stuff to it
  1078. * notYetToggler.addSubItem(cc.MenuItemFont.create("Off"));
  1079. * //this is useful for constructing a toggler without a callback function (you wish to control the behavior from somewhere else)
  1080. */
  1081. cc.MenuItemToggle = cc.MenuItem.extend(/** @lends cc.MenuItemToggle# */{
  1082. subItems: null,
  1083. _selectedIndex: 0,
  1084. _opacity: null,
  1085. _color: null,
  1086. /**
  1087. * Constructor of cc.MenuItemToggle
  1088. */
  1089. ctor: function (/*Multiple arguments follow*/) {
  1090. cc.MenuItem.prototype.ctor.call(this);
  1091. this._selectedIndex = 0;
  1092. this.subItems = [];
  1093. this._opacity = 0;
  1094. this._color = cc.color.WHITE;
  1095. if(arguments.length > 0)
  1096. this.initWithItems(Array.prototype.slice.apply(arguments));
  1097. },
  1098. /**
  1099. * return the opacity of cc.MenuItemToggle
  1100. * @return {Number}
  1101. */
  1102. getOpacity: function () {
  1103. return this._opacity;
  1104. },
  1105. /**
  1106. * set the opacity for cc.MenuItemToggle
  1107. * @param {Number} opacity
  1108. */
  1109. setOpacity: function (opacity) {
  1110. this._opacity = opacity;
  1111. if (this.subItems && this.subItems.length > 0) {
  1112. for (var it = 0; it < this.subItems.length; it++) {
  1113. this.subItems[it].opacity = opacity;
  1114. }
  1115. }
  1116. this._color.a = opacity;
  1117. },
  1118. /**
  1119. * return the color of cc.MenuItemToggle
  1120. * @return {cc.Color}
  1121. */
  1122. getColor: function () {
  1123. var locColor = this._color;
  1124. return cc.color(locColor.r, locColor.g, locColor.b, locColor.a);
  1125. },
  1126. /**
  1127. * set the color for cc.MenuItemToggle
  1128. * @param {cc.Color} Color
  1129. */
  1130. setColor: function (color) {
  1131. var locColor = this._color;
  1132. locColor.r = color.r;
  1133. locColor.g = color.g;
  1134. locColor.b = color.b;
  1135. if (this.subItems && this.subItems.length > 0) {
  1136. for (var it = 0; it < this.subItems.length; it++) {
  1137. this.subItems[it].setColor(color);
  1138. }
  1139. }
  1140. if (color.a !== undefined && !color.a_undefined) {
  1141. this.setOpacity(color.a);
  1142. }
  1143. },
  1144. /**
  1145. * return the index of selected
  1146. * @return {Number}
  1147. */
  1148. getSelectedIndex: function () {
  1149. return this._selectedIndex;
  1150. },
  1151. /**
  1152. * set the seleceted index for cc.MenuItemToggle
  1153. * @param {Number} SelectedIndex
  1154. */
  1155. setSelectedIndex: function (SelectedIndex) {
  1156. if (SelectedIndex != this._selectedIndex) {
  1157. this._selectedIndex = SelectedIndex;
  1158. var currItem = this.getChildByTag(cc.CURRENT_ITEM);
  1159. if (currItem)
  1160. currItem.removeFromParent(false);
  1161. var item = this.subItems[this._selectedIndex];
  1162. this.addChild(item, 0, cc.CURRENT_ITEM);
  1163. var w = item.width, h = item.height;
  1164. this.width = w;
  1165. this.height = h;
  1166. item.setPosition(w / 2, h / 2);
  1167. }
  1168. },
  1169. /**
  1170. * similar to get children,return the sumItem array.
  1171. * @return {Array}
  1172. */
  1173. getSubItems: function () {
  1174. return this.subItems;
  1175. },
  1176. /**
  1177. * set the subitem for cc.MenuItemToggle
  1178. * @param {cc.MenuItem} subItems
  1179. */
  1180. setSubItems: function (subItems) {
  1181. this.subItems = subItems;
  1182. },
  1183. /**
  1184. * initializes a cc.MenuItemToggle with items
  1185. * @param {cc.MenuItem} args[0...last-2] the rest in the array are cc.MenuItems
  1186. * @param {function|String} args[last-1] the second item in the args array is the callback
  1187. * @param {cc.Node} args[last] the first item in the args array is a target
  1188. * @return {Boolean}
  1189. */
  1190. initWithItems: function (args) {
  1191. var l = args.length;
  1192. // passing callback.
  1193. if (typeof args[args.length - 2] === 'function') {
  1194. this.initWithCallback(args[args.length - 2], args[args.length - 1]);
  1195. l = l - 2;
  1196. } else if (typeof args[args.length - 1] === 'function') {
  1197. this.initWithCallback(args[args.length - 1], null);
  1198. l = l - 1;
  1199. } else {
  1200. this.initWithCallback(null, null);
  1201. }
  1202. var locSubItems = this.subItems;
  1203. locSubItems.length = 0;
  1204. for (var i = 0; i < l; i++) {
  1205. if (args[i])
  1206. locSubItems.push(args[i]);
  1207. }
  1208. this._selectedIndex = cc.UINT_MAX;
  1209. this.setSelectedIndex(0);
  1210. this.cascadeColor = true;
  1211. this.cascadeOpacity = true;
  1212. return true;
  1213. },
  1214. /**
  1215. * add the subitem for cc.MenuItemToggle
  1216. * @param {cc.MenuItem} item
  1217. */
  1218. addSubItem: function (item) {
  1219. this.subItems.push(item);
  1220. },
  1221. /**
  1222. * activate the menu item
  1223. */
  1224. activate: function () {
  1225. // update index
  1226. if (this._enabled) {
  1227. var newIndex = (this._selectedIndex + 1) % this.subItems.length;
  1228. this.setSelectedIndex(newIndex);
  1229. }
  1230. cc.MenuItem.prototype.activate.call(this);
  1231. },
  1232. /**
  1233. * menu item is selected (runs callback)
  1234. */
  1235. selected: function () {
  1236. cc.MenuItem.prototype.selected.call(this);
  1237. this.subItems[this._selectedIndex].selected();
  1238. },
  1239. /**
  1240. * menu item goes back to unselected state
  1241. */
  1242. unselected: function () {
  1243. cc.MenuItem.prototype.unselected.call(this);
  1244. this.subItems[this._selectedIndex].unselected();
  1245. },
  1246. /**
  1247. * set the enable status for cc.MenuItemToggle
  1248. * @param {Boolean} enabled
  1249. */
  1250. setEnabled: function (enabled) {
  1251. if (this._enabled != enabled) {
  1252. cc.MenuItem.prototype.setEnabled.call(this, enabled);
  1253. var locItems = this.subItems;
  1254. if (locItems && locItems.length > 0) {
  1255. for (var it = 0; it < locItems.length; it++)
  1256. locItems[it].enabled = enabled;
  1257. }
  1258. }
  1259. },
  1260. /**
  1261. * returns the selected item
  1262. * @return {cc.MenuItem}
  1263. */
  1264. selectedItem: function () {
  1265. return this.subItems[this._selectedIndex];
  1266. },
  1267. /**
  1268. * * <p>
  1269. * Event callback that is invoked every time when cc.MenuItemToggle enters the 'stage'. <br/>
  1270. * If the cc.MenuItemToggle enters the 'stage' with a transition, this event is called when the transition starts. <br/>
  1271. * During onEnter you can't access a "sister/brother" node. <br/>
  1272. * If you override onEnter, you must call its parent's onEnter function with this._super().
  1273. * </p>
  1274. */
  1275. onEnter: function () {
  1276. cc.Node.prototype.onEnter.call(this);
  1277. this.setSelectedIndex(this._selectedIndex);
  1278. }
  1279. });
  1280. var _p = cc.MenuItemToggle.prototype;
  1281. // Extended properties
  1282. /** @expose */
  1283. _p.selectedIndex;
  1284. cc.defineGetterSetter(_p, "selectedIndex", _p.getSelectedIndex, _p.setSelectedIndex);
  1285. /**
  1286. * create a simple container class that "toggles" it's inner items<br/>
  1287. * The inner items can be any MenuItem
  1288. * @deprecated since v3.0 please use new cc.MenuItemToggle(params) instead
  1289. * @return {cc.MenuItemToggle}
  1290. * @example
  1291. * // Example
  1292. *
  1293. * //create a toggle item with 2 menu items (which you can then toggle between them later)
  1294. * var toggler = cc.MenuItemToggle.create( cc.MenuItemFont.create("On"), cc.MenuItemFont.create("Off"), this.callback, this)
  1295. * //Note: the first param is the target, the second is the callback function, afterwards, you can pass in any number of menuitems
  1296. *
  1297. * //if you pass only 1 variable, then it must be a cc.MenuItem
  1298. * var notYetToggler = cc.MenuItemToggle.create(cc.MenuItemFont.create("On"));//it is useless right now, until you add more stuff to it
  1299. * notYetToggler.addSubItem(cc.MenuItemFont.create("Off"));
  1300. * //this is useful for constructing a toggler without a callback function (you wish to control the behavior from somewhere else)
  1301. */
  1302. cc.MenuItemToggle.create = function (/*Multiple arguments follow*/) {
  1303. if ((arguments.length > 0) && (arguments[arguments.length - 1] == null))
  1304. cc.log("parameters should not be ending with null in Javascript");
  1305. var ret = new cc.MenuItemToggle();
  1306. ret.initWithItems(Array.prototype.slice.apply(arguments));
  1307. return ret;
  1308. };