CCMenuItem.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga 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. /**
  23. * default size for font size
  24. * @constant
  25. * @type Number
  26. */
  27. cc.ITEM_SIZE = 32;
  28. cc._globalFontSize = cc.ITEM_SIZE;
  29. cc._globalFontName = "Arial";
  30. cc._globalFontNameRelease = false;
  31. /**
  32. * default tag for current item
  33. * @constant
  34. * @type Number
  35. */
  36. cc.CURRENT_ITEM = 0xc0c05001;
  37. /**
  38. * default tag for zoom action tag
  39. * @constant
  40. * @type Number
  41. */
  42. cc.ZOOM_ACTION_TAG = 0xc0c05002;
  43. /**
  44. * default tag for normal
  45. * @constant
  46. * @type Number
  47. */
  48. cc.NORMAL_TAG = 8801;
  49. /**
  50. * default selected tag
  51. * @constant
  52. * @type Number
  53. */
  54. cc.SELECTED_TAG = 8802;
  55. /**
  56. * default disabled tag
  57. * @constant
  58. * @type Number
  59. */
  60. cc.DISABLE_TAG = 8803;
  61. /**
  62. * Subclass cc.MenuItem (or any subclass) to create your custom cc.MenuItem objects.
  63. * @class
  64. * @extends cc.NodeRGBA
  65. */
  66. cc.MenuItem = cc.NodeRGBA.extend(/** @lends cc.MenuItem# */{
  67. _target:null,
  68. _callback:null,
  69. _isSelected:false,
  70. _isEnabled:false,
  71. ctor:function(){
  72. cc.NodeRGBA.prototype.ctor.call(this);
  73. this._target = null;
  74. this._callback = null;
  75. this._isSelected = false;
  76. this._isEnabled = false;
  77. },
  78. /**
  79. * MenuItem is selected
  80. * @return {Boolean}
  81. */
  82. isSelected:function () {
  83. return this._isSelected;
  84. },
  85. setOpacityModifyRGB:function (value) {
  86. },
  87. isOpacityModifyRGB:function () {
  88. return false;
  89. },
  90. /**
  91. * set the target/selector of the menu item
  92. * @param {function|String} selector
  93. * @param {cc.Node} rec
  94. * @deprecated
  95. */
  96. setTarget:function (selector, rec) {
  97. this._target = rec;
  98. this._callback = selector;
  99. },
  100. /**
  101. * MenuItem is Enabled
  102. * @return {Boolean}
  103. */
  104. isEnabled:function () {
  105. return this._isEnabled;
  106. },
  107. /**
  108. * set enable value of MenuItem
  109. * @param {Boolean} enable
  110. */
  111. setEnabled:function (enable) {
  112. this._isEnabled = enable;
  113. },
  114. /**
  115. * @param {function|String} callback
  116. * @param {cc.Node} target
  117. * @return {Boolean}
  118. */
  119. initWithCallback:function (callback, target) {
  120. this.setAnchorPoint(0.5, 0.5);
  121. this._target = target;
  122. this._callback = callback;
  123. this._isEnabled = true;
  124. this._isSelected = false;
  125. return true;
  126. },
  127. /**
  128. * return rect value of cc.MenuItem
  129. * @return {cc.Rect}
  130. */
  131. rect:function () {
  132. var locPosition = this._position, locContentSize = this._contentSize, locAnchorPoint = this._anchorPoint;
  133. return cc.rect(locPosition.x - locContentSize.width * locAnchorPoint.x,
  134. locPosition.y - locContentSize.height * locAnchorPoint.y,
  135. locContentSize.width, locContentSize.height);
  136. },
  137. /**
  138. * same as setIsSelected(true)
  139. */
  140. selected:function () {
  141. this._isSelected = true;
  142. },
  143. /**
  144. * same as setIsSelected(false)
  145. */
  146. unselected:function () {
  147. this._isSelected = false;
  148. },
  149. /**
  150. * set the callback to the menu item
  151. * @param {function|String} callback
  152. * @param {cc.Node} target
  153. */
  154. setCallback:function (callback, target) {
  155. this._target = target;
  156. this._callback = callback;
  157. },
  158. /**
  159. * call the selector with target
  160. */
  161. activate:function () {
  162. if (this._isEnabled) {
  163. cc.doCallback(this._callback, this._target,this);
  164. }
  165. }
  166. });
  167. /**
  168. * creates an empty menu item with target and callback<br/>
  169. * Not recommended to use the base class, should use more defined menu item classes
  170. * @param {function|String} callback callback
  171. * @param {cc.Node} target
  172. * @return {cc.MenuItem}
  173. */
  174. cc.MenuItem.create = function (callback, target) {
  175. var ret = new cc.MenuItem();
  176. ret.initWithCallback(callback,target);
  177. return ret;
  178. };
  179. /**
  180. * Any cc.Node that supports the cc.LabelProtocol protocol can be added.<br/>
  181. * Supported nodes:<br/>
  182. * - cc.BitmapFontAtlas<br/>
  183. * - cc.LabelAtlas<br/>
  184. * - cc.LabelTTF<br/>
  185. * @class
  186. * @extends cc.MenuItem
  187. */
  188. cc.MenuItemLabel = cc.MenuItem.extend(/** @lends cc.MenuItemLabel# */{
  189. _disabledColor: null,
  190. _label: null,
  191. _orginalScale: 0,
  192. _colorBackup: null,
  193. ctor: function () {
  194. cc.MenuItem.prototype.ctor.call(this);
  195. this._disabledColor = null;
  196. this._label = null;
  197. this._orginalScale = 0;
  198. this._colorBackup = null;
  199. },
  200. /**
  201. * @return {cc.Color3B}
  202. */
  203. getDisabledColor:function () {
  204. return this._disabledColor;
  205. },
  206. /**
  207. * @param {cc.Color3B} color
  208. */
  209. setDisabledColor:function (color) {
  210. this._disabledColor = color;
  211. },
  212. /**
  213. * return label of MenuItemLabel
  214. * @return {cc.Node}
  215. */
  216. getLabel:function () {
  217. return this._label;
  218. },
  219. /**
  220. * @param {cc.Node} label
  221. */
  222. setLabel:function (label) {
  223. if (label) {
  224. this.addChild(label);
  225. label.setAnchorPoint(0, 0);
  226. this.setContentSize(label.getContentSize());
  227. }
  228. if (this._label) {
  229. this.removeChild(this._label, true);
  230. }
  231. this._label = label;
  232. },
  233. /**
  234. * @param {Boolean} enabled
  235. */
  236. setEnabled:function (enabled) {
  237. if (this._isEnabled != enabled) {
  238. var locLabel = this._label;
  239. if (!enabled) {
  240. this._colorBackup = locLabel.getColor();
  241. locLabel.setColor(this._disabledColor);
  242. } else {
  243. locLabel.setColor(this._colorBackup);
  244. }
  245. }
  246. cc.MenuItem.prototype.setEnabled.call(this, enabled);
  247. },
  248. /**
  249. * @param {Number} opacity from 0-255
  250. */
  251. setOpacity:function (opacity) {
  252. this._label.setOpacity(opacity);
  253. },
  254. /**
  255. * @return {Number}
  256. */
  257. getOpacity:function () {
  258. return this._label.getOpacity();
  259. },
  260. /**
  261. * @param {cc.Color3B} color
  262. */
  263. setColor:function (color) {
  264. this._label.setColor(color);
  265. },
  266. /**
  267. * @return {cc.Color3B}
  268. */
  269. getColor:function () {
  270. return this._label.getColor();
  271. },
  272. /**
  273. * @param {cc.Node} label
  274. * @param {function|String} selector
  275. * @param {cc.Node} target
  276. * @return {Boolean}
  277. */
  278. initWithLabel:function (label, selector, target) {
  279. this.initWithCallback(selector, target);
  280. this._originalScale = 1.0;
  281. this._colorBackup = cc.white();
  282. this._disabledColor = cc.c3b(126, 126, 126);
  283. this.setLabel(label);
  284. this.setCascadeColorEnabled(true);
  285. this.setCascadeOpacityEnabled(true);
  286. return true;
  287. },
  288. /**
  289. * @param {String} label
  290. */
  291. setString:function (label) {
  292. this._label.setString(label);
  293. this.setContentSize(this._label.getContentSize());
  294. },
  295. /**
  296. * activate the menu item
  297. */
  298. activate:function () {
  299. if (this._isEnabled) {
  300. this.stopAllActions();
  301. this.setScale(this._originalScale);
  302. cc.MenuItem.prototype.activate.call(this);
  303. }
  304. },
  305. /**
  306. * menu item is selected (runs callback)
  307. */
  308. selected:function () {
  309. if (this._isEnabled) {
  310. cc.MenuItem.prototype.selected.call(this);
  311. var action = this.getActionByTag(cc.ZOOM_ACTION_TAG);
  312. if (action)
  313. this.stopAction(action);
  314. else
  315. this._originalScale = this.getScale();
  316. var zoomAction = cc.ScaleTo.create(0.1, this._originalScale * 1.2);
  317. zoomAction.setTag(cc.ZOOM_ACTION_TAG);
  318. this.runAction(zoomAction);
  319. }
  320. },
  321. /**
  322. * menu item goes back to unselected state
  323. */
  324. unselected:function () {
  325. if (this._isEnabled) {
  326. cc.MenuItem.prototype.unselected.call(this);
  327. this.stopActionByTag(cc.ZOOM_ACTION_TAG);
  328. var zoomAction = cc.ScaleTo.create(0.1, this._originalScale);
  329. zoomAction.setTag(cc.ZOOM_ACTION_TAG);
  330. this.runAction(zoomAction);
  331. }
  332. }
  333. });
  334. /**
  335. * @param {cc.Node} label
  336. * @param {function|String|Null} [selector=]
  337. * @param {cc.Node|Null} [target=]
  338. * @return {cc.MenuItemLabel}
  339. */
  340. cc.MenuItemLabel.create = function (label, selector, target) {
  341. var ret = new cc.MenuItemLabel();
  342. ret.initWithLabel(label, selector, target);
  343. return ret;
  344. };
  345. /**
  346. * Helper class that creates a MenuItemLabel class with a LabelAtlas
  347. * @class
  348. * @extends cc.MenuItemLabel
  349. */
  350. cc.MenuItemAtlasFont = cc.MenuItemLabel.extend(/** @lends cc.MenuItemAtlasFont# */{
  351. /**
  352. * @param {String} value
  353. * @param {String} charMapFile
  354. * @param {Number} itemWidth
  355. * @param {Number} itemHeight
  356. * @param {String} startCharMap a single character
  357. * @param {function|String|Null} callback
  358. * @param {cc.Node|Null} target
  359. * @return {Boolean}
  360. */
  361. initWithString:function (value, charMapFile, itemWidth, itemHeight, startCharMap, callback, target) {
  362. if(!value || value.length == 0)
  363. throw "cc.MenuItemAtlasFont.initWithString(): value should be non-null and its length should be greater than 0";
  364. var label = new cc.LabelAtlas();
  365. label.initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap);
  366. if (this.initWithLabel(label, callback, target)) {
  367. // do something ?
  368. }
  369. return true;
  370. }
  371. });
  372. /**
  373. * create menu item from string with font
  374. * @param {String} value the text to display
  375. * @param {String} charMapFile the character map file
  376. * @param {Number} itemWidth
  377. * @param {Number} itemHeight
  378. * @param {String} startCharMap a single character
  379. * @param {function|String|Null} [callback=null]
  380. * @param {cc.Node|Null} [target=]
  381. * @return {cc.MenuItemAtlasFont}
  382. * @example
  383. * // Example
  384. * var item = cc.MenuItemAtlasFont.create('text to display', 'font.fnt', 12, 32, ' ')
  385. *
  386. * //OR
  387. * var item = cc.MenuItemAtlasFont.create('text to display', 'font.fnt', 12, 32, ' ', game.run, game)
  388. */
  389. cc.MenuItemAtlasFont.create = function (value, charMapFile, itemWidth, itemHeight, startCharMap, callback, target) {
  390. var ret = new cc.MenuItemAtlasFont();
  391. ret.initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap, callback, target);
  392. return ret;
  393. };
  394. /**
  395. * Helper class that creates a CCMenuItemLabel class with a Label
  396. * @class
  397. * @extends cc.MenuItemLabel
  398. */
  399. cc.MenuItemFont = cc.MenuItemLabel.extend(/** @lends cc.MenuItemFont# */{
  400. _fontSize:null,
  401. _fontName:null,
  402. ctor:function(){
  403. cc.MenuItemLabel.prototype.ctor.call(this);
  404. this._fontSize = 0;
  405. this._fontName = "";
  406. },
  407. /**
  408. * @param {String} value text for the menu item
  409. * @param {function|String} callback
  410. * @param {cc.Node} target
  411. * @return {Boolean}
  412. */
  413. initWithString:function (value, callback, target) {
  414. if(!value || value.length == 0)
  415. throw "Value should be non-null and its length should be greater than 0";
  416. this._fontName = cc._globalFontName;
  417. this._fontSize = cc._globalFontSize;
  418. var label = cc.LabelTTF.create(value, this._fontName, this._fontSize);
  419. if (this.initWithLabel(label, callback, target)) {
  420. // do something ?
  421. }
  422. return true;
  423. },
  424. /**
  425. * @param {Number} s
  426. */
  427. setFontSize:function (s) {
  428. this._fontSize = s;
  429. this._recreateLabel();
  430. },
  431. /**
  432. *
  433. * @return {Number}
  434. */
  435. fontSize:function () {
  436. return this._fontSize;
  437. },
  438. /**
  439. * @param {String} name
  440. */
  441. setFontName:function (name) {
  442. this._fontName = name;
  443. this._recreateLabel();
  444. },
  445. /**
  446. * @return {String}
  447. */
  448. fontName:function () {
  449. return this._fontName;
  450. },
  451. _recreateLabel:function () {
  452. var label = cc.LabelTTF.create(this._label.getString(),
  453. this._fontName, this._fontSize);
  454. this.setLabel(label);
  455. }
  456. });
  457. /**
  458. * a shared function to set the fontSize for menuitem font
  459. * @param {Number} fontSize
  460. */
  461. cc.MenuItemFont.setFontSize = function (fontSize) {
  462. cc._globalFontSize = fontSize;
  463. };
  464. /**
  465. * a shared function to get the font size for menuitem font
  466. * @return {Number}
  467. */
  468. cc.MenuItemFont.fontSize = function () {
  469. return cc._globalFontSize;
  470. };
  471. /**
  472. * a shared function to set the fontsize for menuitem font
  473. * @param name
  474. */
  475. cc.MenuItemFont.setFontName = function (name) {
  476. if (cc._globalFontNameRelease) {
  477. cc._globalFontName = '';
  478. }
  479. cc._globalFontName = name;
  480. cc._globalFontNameRelease = true;
  481. };
  482. /**
  483. * a shared function to get the font name for menuitem font
  484. * @return {String}
  485. */
  486. cc.MenuItemFont.fontName = function () {
  487. return cc._globalFontName;
  488. };
  489. /**
  490. * create a menu item from string
  491. * @param {String} value the text to display
  492. * @param {String|function|Null} callback the callback to run, either in function name or pass in the actual function
  493. * @param {cc.Node|Null} target the target to run callback
  494. * @return {cc.MenuItemFont}
  495. * @example
  496. * // Example
  497. * var item = cc.MenuItemFont.create("Game start", 'start', Game)
  498. * //creates a menu item from string "Game start", and when clicked, it will run Game.start()
  499. *
  500. * var item = cc.MenuItemFont.create("Game start", game.start, Game)//same as above
  501. *
  502. * var item = cc.MenuItemFont.create("i do nothing")//create a text menu item that does nothing
  503. *
  504. * //you can set font size and name before or after
  505. * cc.MenuItemFont.setFontName('my Fancy Font');
  506. * cc.MenuItemFont.setFontSize(62);
  507. */
  508. cc.MenuItemFont.create = function (value, callback, target) {
  509. var ret = new cc.MenuItemFont();
  510. ret.initWithString(value, callback, target);
  511. return ret;
  512. };
  513. /**
  514. * CCMenuItemSprite accepts CCNode<CCRGBAProtocol> objects as items.<br/>
  515. * The images has 3 different states:<br/>
  516. * - unselected image<br/>
  517. * - selected image<br/>
  518. * - disabled image<br/>
  519. * @class
  520. * @extends cc.MenuItem
  521. */
  522. cc.MenuItemSprite = cc.MenuItem.extend(/** @lends cc.MenuItemSprite# */{
  523. _normalImage:null,
  524. _selectedImage:null,
  525. _disabledImage:null,
  526. ctor: function(){
  527. cc.MenuItem.prototype.ctor.call(this);
  528. this._normalImage = null;
  529. this._selectedImage = null;
  530. this._disabledImage = null;
  531. },
  532. /**
  533. * @return {cc.Sprite}
  534. */
  535. getNormalImage:function () {
  536. return this._normalImage;
  537. },
  538. /**
  539. * @param {cc.Sprite} normalImage
  540. */
  541. setNormalImage:function (normalImage) {
  542. if (this._normalImage == normalImage) {
  543. return;
  544. }
  545. if (normalImage) {
  546. this.addChild(normalImage, 0, cc.NORMAL_TAG);
  547. normalImage.setAnchorPoint(0, 0);
  548. }
  549. if (this._normalImage) {
  550. this.removeChild(this._normalImage, true);
  551. }
  552. this._normalImage = normalImage;
  553. this.setContentSize(this._normalImage.getContentSize());
  554. this._updateImagesVisibility();
  555. if (normalImage.textureLoaded && !normalImage.textureLoaded()) {
  556. normalImage.addLoadedEventListener(function (sender) {
  557. this.setContentSize(sender.getContentSize());
  558. }, this);
  559. }
  560. },
  561. /**
  562. * @return {cc.Sprite}
  563. */
  564. getSelectedImage:function () {
  565. return this._selectedImage;
  566. },
  567. /**
  568. * @param {cc.Sprite} selectedImage
  569. */
  570. setSelectedImage:function (selectedImage) {
  571. if (this._selectedImage == selectedImage)
  572. return;
  573. if (selectedImage) {
  574. this.addChild(selectedImage, 0, cc.SELECTED_TAG);
  575. selectedImage.setAnchorPoint(0, 0);
  576. }
  577. if (this._selectedImage) {
  578. this.removeChild(this._selectedImage, true);
  579. }
  580. this._selectedImage = selectedImage;
  581. this._updateImagesVisibility();
  582. },
  583. /**
  584. * @return {cc.Sprite}
  585. */
  586. getDisabledImage:function () {
  587. return this._disabledImage;
  588. },
  589. /**
  590. * @param {cc.Sprite} disabledImage
  591. */
  592. setDisabledImage:function (disabledImage) {
  593. if (this._disabledImage == disabledImage)
  594. return;
  595. if (disabledImage) {
  596. this.addChild(disabledImage, 0, cc.DISABLE_TAG);
  597. disabledImage.setAnchorPoint(0, 0);
  598. }
  599. if (this._disabledImage)
  600. this.removeChild(this._disabledImage, true);
  601. this._disabledImage = disabledImage;
  602. this._updateImagesVisibility();
  603. },
  604. /**
  605. * @param {cc.Node} normalSprite
  606. * @param {cc.Node} selectedSprite
  607. * @param {cc.Node} disabledSprite
  608. * @param {function|String} callback
  609. * @param {cc.Node} target
  610. * @return {Boolean}
  611. */
  612. initWithNormalSprite:function (normalSprite, selectedSprite, disabledSprite, callback, target) {
  613. this.initWithCallback(callback, target);
  614. this.setNormalImage(normalSprite);
  615. this.setSelectedImage(selectedSprite);
  616. this.setDisabledImage(disabledSprite);
  617. var locNormalImage = this._normalImage;
  618. if (locNormalImage) {
  619. this.setContentSize(locNormalImage.getContentSize());
  620. if (locNormalImage.textureLoaded && !locNormalImage.textureLoaded()) {
  621. locNormalImage.addLoadedEventListener(function (sender) {
  622. this.setContentSize(sender.getContentSize());
  623. this.setCascadeColorEnabled(true);
  624. this.setCascadeOpacityEnabled(true);
  625. }, this);
  626. }
  627. }
  628. this.setCascadeColorEnabled(true);
  629. this.setCascadeOpacityEnabled(true);
  630. return true;
  631. },
  632. /**
  633. * @param {cc.Color3B} color
  634. */
  635. setColor:function (color) {
  636. this._normalImage.setColor(color);
  637. if (this._selectedImage)
  638. this._selectedImage.setColor(color);
  639. if (this._disabledImage)
  640. this._disabledImage.setColor(color);
  641. },
  642. /**
  643. * @return {cc.Color3B}
  644. */
  645. getColor:function () {
  646. return this._normalImage.getColor();
  647. },
  648. /**
  649. * @param {Number} opacity 0 - 255
  650. */
  651. setOpacity:function (opacity) {
  652. this._normalImage.setOpacity(opacity);
  653. if (this._selectedImage)
  654. this._selectedImage.setOpacity(opacity);
  655. if (this._disabledImage)
  656. this._disabledImage.setOpacity(opacity);
  657. },
  658. /**
  659. * @return {Number} opacity from 0 - 255
  660. */
  661. getOpacity:function () {
  662. return this._normalImage.getOpacity();
  663. },
  664. /**
  665. * menu item is selected (runs callback)
  666. */
  667. selected:function () {
  668. cc.MenuItem.prototype.selected.call(this);
  669. if (this._normalImage) {
  670. if (this._disabledImage)
  671. this._disabledImage.setVisible(false);
  672. if (this._selectedImage) {
  673. this._normalImage.setVisible(false);
  674. this._selectedImage.setVisible(true);
  675. } else
  676. this._normalImage.setVisible(true);
  677. }
  678. },
  679. /**
  680. * menu item goes back to unselected state
  681. */
  682. unselected:function () {
  683. cc.MenuItem.prototype.unselected.call(this);
  684. if (this._normalImage) {
  685. this._normalImage.setVisible(true);
  686. if (this._selectedImage)
  687. this._selectedImage.setVisible(false);
  688. if (this._disabledImage)
  689. this._disabledImage.setVisible(false);
  690. }
  691. },
  692. /**
  693. * @param {Boolean} bEnabled
  694. */
  695. setEnabled:function (bEnabled) {
  696. if (this._isEnabled != bEnabled) {
  697. cc.MenuItem.prototype.setEnabled.call(this, bEnabled);
  698. this._updateImagesVisibility();
  699. }
  700. },
  701. _updateImagesVisibility:function () {
  702. var locNormalImage = this._normalImage, locSelImage = this._selectedImage, locDisImage = this._disabledImage;
  703. if (this._isEnabled) {
  704. if (locNormalImage)
  705. locNormalImage.setVisible(true);
  706. if (locSelImage)
  707. locSelImage.setVisible(false);
  708. if (locDisImage)
  709. locDisImage.setVisible(false);
  710. } else {
  711. if (locDisImage) {
  712. if (locNormalImage)
  713. locNormalImage.setVisible(false);
  714. if (locSelImage)
  715. locSelImage.setVisible(false);
  716. if (locDisImage)
  717. locDisImage.setVisible(true);
  718. } else {
  719. if (locNormalImage)
  720. locNormalImage.setVisible(true);
  721. if (locSelImage)
  722. locSelImage.setVisible(false);
  723. }
  724. }
  725. }
  726. });
  727. /**
  728. * create a menu item from sprite
  729. * @param {Image} normalSprite normal state image
  730. * @param {Image|Null} selectedSprite selected state image
  731. * @param {Image|cc.Node|Null} three disabled state image OR target node
  732. * @param {String|function|cc.Node|Null} four callback function name in string or actual function, OR target Node
  733. * @param {String|function|Null} five callback function name in string or actual function
  734. * @return {cc.MenuItemSprite}
  735. * @example
  736. * // Example
  737. * var item = cc.MenuItemSprite.create(normalImage)//create a menu item from a sprite with no functionality
  738. *
  739. * var item = cc.MenuItemSprite.create(normalImage, selectedImage)//create a menu Item, nothing will happen when clicked
  740. *
  741. * var item = cc.MenuItemSprite.create(normalImage, SelectedImage, disabledImage)//same above, but with disabled state image
  742. *
  743. * var item = cc.MenuItemSprite.create(normalImage, SelectedImage, 'callback', targetNode)//create a menu item, when clicked runs targetNode.callback()
  744. *
  745. * var item = cc.MenuItemSprite.create(normalImage, SelectedImage, disabledImage, targetNode.callback, targetNode)
  746. * //same as above, but with disabled image, and passing in callback function
  747. */
  748. cc.MenuItemSprite.create = function (normalSprite, selectedSprite, three, four, five) {
  749. var len = arguments.length;
  750. normalSprite = arguments[0];
  751. selectedSprite = arguments[1];
  752. var disabledImage, target, callback;
  753. var ret = new cc.MenuItemSprite();
  754. //when you send 4 arguments, five is undefined
  755. if (len == 5) {
  756. disabledImage = arguments[2];
  757. callback = arguments[3];
  758. target = arguments[4];
  759. } else if (len == 4 && typeof arguments[3] === "function") {
  760. disabledImage = arguments[2];
  761. callback = arguments[3];
  762. } else if (len == 4 && typeof arguments[2] === "function") {
  763. target = arguments[3];
  764. callback = arguments[2];
  765. } else if (len <= 2) {
  766. disabledImage = arguments[2];
  767. }
  768. ret.initWithNormalSprite(normalSprite, selectedSprite, disabledImage, callback, target);
  769. return ret;
  770. };
  771. /**
  772. * cc.MenuItemImage accepts images as items.<br/>
  773. * The images has 3 different states:<br/>
  774. * - unselected image<br/>
  775. * - selected image<br/>
  776. * - disabled image<br/>
  777. * <br/>
  778. * For best results try that all images are of the same size<br/>
  779. * @class
  780. * @extends cc.MenuItemSprite
  781. */
  782. cc.MenuItemImage = cc.MenuItemSprite.extend(/** @lends cc.MenuItemImage# */{
  783. /**
  784. * sets the sprite frame for the normal image
  785. * @param {cc.SpriteFrame} frame
  786. */
  787. setNormalSpriteFrame:function (frame) {
  788. this.setNormalImage(cc.Sprite.createWithSpriteFrame(frame));
  789. },
  790. /**
  791. * sets the sprite frame for the selected image
  792. * @param {cc.SpriteFrame} frame
  793. */
  794. setSelectedSpriteFrame:function (frame) {
  795. this.setSelectedImage(cc.Sprite.createWithSpriteFrame(frame));
  796. },
  797. /**
  798. * sets the sprite frame for the disabled image
  799. * @param {cc.SpriteFrame} frame
  800. */
  801. setDisabledSpriteFrame:function (frame) {
  802. this.setDisabledImage(cc.Sprite.createWithSpriteFrame(frame));
  803. },
  804. /**
  805. * @param {string|null} normalImage
  806. * @param {string|null} selectedImage
  807. * @param {string|null} disabledImage
  808. * @param {function|string|null} callback
  809. * @param {cc.Node|null} target
  810. * @returns {boolean}
  811. */
  812. initWithNormalImage:function (normalImage, selectedImage, disabledImage, callback, target) {
  813. var normalSprite = null;
  814. var selectedSprite = null;
  815. var disabledSprite = null;
  816. if (normalImage) {
  817. normalSprite = cc.Sprite.create(normalImage);
  818. }
  819. if (selectedImage) {
  820. selectedSprite = cc.Sprite.create(selectedImage);
  821. }
  822. if (disabledImage) {
  823. disabledSprite = cc.Sprite.create(disabledImage);
  824. }
  825. return this.initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, callback, target);
  826. }
  827. });
  828. /**
  829. * creates a new menu item image
  830. * @param {String} normalImage file name for normal state
  831. * @param {String} selectedImage image for selected state
  832. * @param {String|cc.Node} three Disabled image OR callback function
  833. * @param {String|function|Null} [four] callback function, either name in string or pass the whole function OR the target
  834. * @param {cc.Node|String|function|Null} [five] cc.Node target to run callback when clicked
  835. * @return {cc.MenuItemImage}
  836. * @example
  837. * // Example
  838. * //create a dom menu item with normal and selected state, when clicked it will run the run function from gameScene object
  839. * var item = cc.MenuItemImage.create('normal.png', 'selected.png', 'run', gameScene)
  840. *
  841. * //same as above, but pass in the actual function and disabled image
  842. * var item = cc.MenuItemImage.create('normal.png', 'selected.png', 'disabled.png', gameScene.run, gameScene)
  843. */
  844. cc.MenuItemImage.create = function (normalImage, selectedImage, three, four, five) {
  845. if (normalImage === undefined) {
  846. return cc.MenuItemImage.create(null, null, null, null, null);
  847. }
  848. else if (four === undefined) {
  849. return cc.MenuItemImage.create(normalImage, selectedImage, null, three, null);
  850. }
  851. else if (five === undefined) {
  852. return cc.MenuItemImage.create(normalImage, selectedImage, null, three, four);
  853. }
  854. var ret = new cc.MenuItemImage();
  855. if (ret.initWithNormalImage(normalImage, selectedImage, three, four, five))
  856. return ret;
  857. return null;
  858. };
  859. /**
  860. * A simple container class that "toggles" it's inner items<br/>
  861. * The inner items can be any MenuItem
  862. * @class
  863. * @extends cc.MenuItem
  864. */
  865. cc.MenuItemToggle = cc.MenuItem.extend(/** @lends cc.MenuItemToggle# */{
  866. _selectedIndex:0,
  867. _subItems:null,
  868. _opacity:null,
  869. _color:null,
  870. ctor: function(){
  871. cc.MenuItem.prototype.ctor.call(this);
  872. this._selectedIndex = 0;
  873. this._subItems = [];
  874. this._opacity = 0;
  875. this._color = cc.white();
  876. },
  877. /**
  878. * @return {Number}
  879. */
  880. getOpacity:function () {
  881. return this._opacity;
  882. },
  883. /**
  884. * @param {Number} Opacity
  885. */
  886. setOpacity:function (Opacity) {
  887. this._opacity = Opacity;
  888. if (this._subItems && this._subItems.length > 0) {
  889. for (var it = 0; it < this._subItems.length; it++) {
  890. this._subItems[it].setOpacity(Opacity);
  891. }
  892. }
  893. },
  894. /**
  895. * @return {cc.Color3B}
  896. */
  897. getColor:function () {
  898. return this._color;
  899. },
  900. /**
  901. * @param {cc.Color3B} Color
  902. */
  903. setColor:function (Color) {
  904. this._color = Color;
  905. if (this._subItems && this._subItems.length > 0) {
  906. for (var it = 0; it < this._subItems.length; it++) {
  907. this._subItems[it].setColor(Color);
  908. }
  909. }
  910. },
  911. /**
  912. * @return {Number}
  913. */
  914. getSelectedIndex:function () {
  915. return this._selectedIndex;
  916. },
  917. /**
  918. * @param {Number} SelectedIndex
  919. */
  920. setSelectedIndex:function (SelectedIndex) {
  921. if (SelectedIndex != this._selectedIndex) {
  922. this._selectedIndex = SelectedIndex;
  923. var currItem = this.getChildByTag(cc.CURRENT_ITEM);
  924. if (currItem)
  925. currItem.removeFromParent(false);
  926. var item = this._subItems[this._selectedIndex];
  927. this.addChild(item, 0, cc.CURRENT_ITEM);
  928. var s = item.getContentSize();
  929. this.setContentSize(s);
  930. item.setPosition(s.width / 2, s.height / 2);
  931. }
  932. },
  933. /**
  934. * similar to get children
  935. * @return {cc.MenuItem}
  936. */
  937. getSubItems:function () {
  938. return this._subItems;
  939. },
  940. /**
  941. * @param {cc.MenuItem} SubItems
  942. */
  943. setSubItems:function (SubItems) {
  944. this._subItems = SubItems;
  945. },
  946. /**
  947. * @param {cc.MenuItem} args[0...last-2] the rest in the array are cc.MenuItems
  948. * @param {function|String} args[last-1] the second item in the args array is the callback
  949. * @param {cc.Node} args[last] the first item in the args array is a target
  950. * @return {Boolean}
  951. */
  952. initWithItems:function (args) {
  953. var l = args.length;
  954. // passing callback.
  955. if (typeof args[args.length-2] === 'function') {
  956. this.initWithCallback( args[args.length-2], args[args.length-1] );
  957. l = l-2;
  958. } else if(typeof args[args.length-1] === 'function'){
  959. this.initWithCallback( args[args.length-1], null );
  960. l = l-1;
  961. } else {
  962. this.initWithCallback(null, null);
  963. }
  964. var locSubItems = this._subItems;
  965. locSubItems.length = 0;
  966. for (var i = 0; i < l; i++) {
  967. if (args[i])
  968. locSubItems.push(args[i]);
  969. }
  970. this._selectedIndex = cc.UINT_MAX;
  971. this.setSelectedIndex(0);
  972. this.setCascadeColorEnabled(true);
  973. this.setCascadeOpacityEnabled(true);
  974. return true;
  975. },
  976. /**
  977. * @param {cc.MenuItem} item
  978. */
  979. addSubItem:function (item) {
  980. this._subItems.push(item);
  981. },
  982. /**
  983. * activate the menu item
  984. */
  985. activate:function () {
  986. // update index
  987. if (this._isEnabled) {
  988. var newIndex = (this._selectedIndex + 1) % this._subItems.length;
  989. this.setSelectedIndex(newIndex);
  990. }
  991. cc.MenuItem.prototype.activate.call(this);
  992. },
  993. /**
  994. * menu item is selected (runs callback)
  995. */
  996. selected:function () {
  997. cc.MenuItem.prototype.selected.call(this);
  998. this._subItems[this._selectedIndex].selected();
  999. },
  1000. /**
  1001. * menu item goes back to unselected state
  1002. */
  1003. unselected:function () {
  1004. cc.MenuItem.prototype.unselected.call(this);
  1005. this._subItems[this._selectedIndex].unselected();
  1006. },
  1007. /**
  1008. * @param {Boolean} enabled
  1009. */
  1010. setEnabled:function (enabled) {
  1011. if (this._isEnabled != enabled) {
  1012. cc.MenuItem.prototype.setEnabled.call(this, enabled);
  1013. var locItems = this._subItems;
  1014. if (locItems && locItems.length > 0) {
  1015. for (var it = 0; it < locItems.length; it++)
  1016. locItems[it].setEnabled(enabled);
  1017. }
  1018. }
  1019. },
  1020. /**
  1021. * returns the selected item
  1022. * @return {cc.MenuItem}
  1023. */
  1024. selectedItem:function () {
  1025. return this._subItems[this._selectedIndex];
  1026. },
  1027. onEnter:function () {
  1028. cc.Node.prototype.onEnter.call(this);
  1029. this.setSelectedIndex(this._selectedIndex);
  1030. }
  1031. });
  1032. /**
  1033. * create a simple container class that "toggles" it's inner items<br/>
  1034. * The inner items can be any MenuItem
  1035. * @return {cc.MenuItemToggle}
  1036. * @example
  1037. * // Example
  1038. *
  1039. * //create a toggle item with 2 menu items (which you can then toggle between them later)
  1040. * var toggler = cc.MenuItemToggle.create( cc.MenuItemFont.create("On"), cc.MenuItemFont.create("Off"), this.callback, this)
  1041. * //Note: the first param is the target, the second is the callback function, afterwards, you can pass in any number of menuitems
  1042. *
  1043. * //if you pass only 1 variable, then it must be a cc.MenuItem
  1044. * var notYetToggler = cc.MenuItemToggle.create(cc.MenuItemFont.create("On"));//it is useless right now, until you add more stuff to it
  1045. * notYetToggler.addSubItem(cc.MenuItemFont.create("Off"));
  1046. * //this is useful for constructing a toggler without a callback function (you wish to control the behavior from somewhere else)
  1047. */
  1048. cc.MenuItemToggle.create = function (/*Multiple arguments follow*/) {
  1049. if((arguments.length > 0) && (arguments[arguments.length-1] == null))
  1050. cc.log("parameters should not be ending with null in Javascript");
  1051. var ret = new cc.MenuItemToggle();
  1052. ret.initWithItems(arguments);
  1053. return ret;
  1054. };