CCMenuItem.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #include "2d/CCMenuItem.h"
  25. #include "2d/CCActionInterval.h"
  26. #include "2d/CCSprite.h"
  27. #include "2d/CCLabelAtlas.h"
  28. #include "2d/CCLabel.h"
  29. #include "base/ccUTF8.h"
  30. #include <stdarg.h>
  31. NS_CC_BEGIN
  32. static int _globalFontSize = kItemSize;
  33. static std::string _globalFontName = "Marker Felt";
  34. static bool _globalFontNameRelease = false;
  35. const unsigned int kZoomActionTag = 0xc0c05002;
  36. //
  37. // MenuItem
  38. //
  39. MenuItem* MenuItem::create()
  40. {
  41. return MenuItem::create((const ccMenuCallback&)nullptr);
  42. }
  43. // FIXME: deprecated
  44. MenuItem* MenuItem::create(Ref *target, SEL_MenuHandler selector)
  45. {
  46. MenuItem *ret = new (std::nothrow) MenuItem();
  47. ret->initWithCallback(std::bind(selector, target, std::placeholders::_1));
  48. ret->autorelease();
  49. return ret;
  50. }
  51. MenuItem* MenuItem::create( const ccMenuCallback& callback)
  52. {
  53. MenuItem *ret = new (std::nothrow) MenuItem();
  54. ret->initWithCallback(callback);
  55. ret->autorelease();
  56. return ret;
  57. }
  58. // FIXME: deprecated
  59. bool MenuItem::initWithTarget(cocos2d::Ref *target, SEL_MenuHandler selector )
  60. {
  61. return initWithCallback( std::bind(selector,target, std::placeholders::_1) );
  62. }
  63. bool MenuItem::initWithCallback(const ccMenuCallback& callback)
  64. {
  65. setAnchorPoint(Vec2(0.5f, 0.5f));
  66. _callback = callback;
  67. _enabled = true;
  68. _selected = false;
  69. return true;
  70. }
  71. MenuItem::~MenuItem()
  72. {
  73. }
  74. void MenuItem::selected()
  75. {
  76. _selected = true;
  77. }
  78. void MenuItem::unselected()
  79. {
  80. _selected = false;
  81. }
  82. void MenuItem::activate()
  83. {
  84. if (_enabled)
  85. {
  86. if( _callback )
  87. {
  88. _callback(this);
  89. }
  90. #if CC_ENABLE_SCRIPT_BINDING
  91. if (kScriptTypeLua == _scriptType)
  92. {
  93. BasicScriptData data(this);
  94. ScriptEvent scriptEvent(kMenuClickedEvent, &data);
  95. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
  96. }
  97. #endif
  98. }
  99. }
  100. void MenuItem::setEnabled(bool enabled)
  101. {
  102. _enabled = enabled;
  103. }
  104. bool MenuItem::isEnabled() const
  105. {
  106. return _enabled;
  107. }
  108. Rect MenuItem::rect() const
  109. {
  110. return Rect( _position.x - _contentSize.width * _anchorPoint.x,
  111. _position.y - _contentSize.height * _anchorPoint.y,
  112. _contentSize.width, _contentSize.height);
  113. }
  114. bool MenuItem::isSelected() const
  115. {
  116. return _selected;
  117. }
  118. // FIXME: deprecated
  119. void MenuItem::setTarget(Ref *target, SEL_MenuHandler selector)
  120. {
  121. setCallback( std::bind( selector, target, std::placeholders::_1) );
  122. }
  123. void MenuItem::setCallback(const ccMenuCallback& callback)
  124. {
  125. _callback = callback;
  126. }
  127. std::string MenuItem::getDescription() const
  128. {
  129. return StringUtils::format("<MenuItem | tag = %d>", _tag);
  130. }
  131. //
  132. //CCMenuItemLabel
  133. //
  134. void MenuItemLabel::setLabel(Node* var)
  135. {
  136. if (var)
  137. {
  138. var->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
  139. setContentSize(var->getContentSize());
  140. addChild(var);
  141. }
  142. if (_label)
  143. {
  144. removeChild(_label, true);
  145. }
  146. _label = var;
  147. }
  148. // FIXME:: deprecated
  149. MenuItemLabel * MenuItemLabel::create(Node*label, Ref* target, SEL_MenuHandler selector)
  150. {
  151. MenuItemLabel *ret = new (std::nothrow) MenuItemLabel();
  152. ret->initWithLabel(label, std::bind(selector, target, std::placeholders::_1));
  153. ret->autorelease();
  154. return ret;
  155. }
  156. MenuItemLabel * MenuItemLabel::create(Node*label, const ccMenuCallback& callback)
  157. {
  158. MenuItemLabel *ret = new (std::nothrow) MenuItemLabel();
  159. ret->initWithLabel(label, callback);
  160. ret->autorelease();
  161. return ret;
  162. }
  163. MenuItemLabel* MenuItemLabel::create(Node *label)
  164. {
  165. MenuItemLabel *ret = new (std::nothrow) MenuItemLabel();
  166. ret->initWithLabel(label, (const ccMenuCallback&) nullptr);
  167. ret->autorelease();
  168. return ret;
  169. }
  170. // FIXME:: deprecated
  171. bool MenuItemLabel::initWithLabel(Node* label, Ref* target, SEL_MenuHandler selector)
  172. {
  173. return initWithLabel(label, std::bind(selector,target, std::placeholders::_1) );
  174. }
  175. bool MenuItemLabel::initWithLabel(Node* label, const ccMenuCallback& callback)
  176. {
  177. MenuItem::initWithCallback(callback);
  178. _originalScale = 1.0f;
  179. _colorBackup = Color3B::WHITE;
  180. setDisabledColor(Color3B(126,126,126));
  181. this->setLabel(label);
  182. setCascadeColorEnabled(true);
  183. setCascadeOpacityEnabled(true);
  184. return true;
  185. }
  186. MenuItemLabel::~MenuItemLabel()
  187. {
  188. }
  189. void MenuItemLabel::setString(const std::string& label)
  190. {
  191. dynamic_cast<LabelProtocol*>(_label)->setString(label);
  192. this->setContentSize(_label->getContentSize());
  193. }
  194. std::string MenuItemLabel::getString() const
  195. {
  196. auto label = dynamic_cast<LabelProtocol*>(_label);
  197. return label->getString();
  198. }
  199. void MenuItemLabel::activate()
  200. {
  201. if(_enabled)
  202. {
  203. this->stopAllActions();
  204. this->setScale( _originalScale );
  205. MenuItem::activate();
  206. }
  207. }
  208. void MenuItemLabel::selected()
  209. {
  210. // subclass to change the default action
  211. if(_enabled)
  212. {
  213. MenuItem::selected();
  214. Action *action = getActionByTag(kZoomActionTag);
  215. if (action)
  216. {
  217. this->stopAction(action);
  218. }
  219. else
  220. {
  221. _originalScale = this->getScale();
  222. }
  223. Action *zoomAction = ScaleTo::create(0.1f, _originalScale * 1.2f);
  224. zoomAction->setTag(kZoomActionTag);
  225. this->runAction(zoomAction);
  226. }
  227. }
  228. void MenuItemLabel::unselected()
  229. {
  230. // subclass to change the default action
  231. if(_enabled)
  232. {
  233. MenuItem::unselected();
  234. this->stopActionByTag(kZoomActionTag);
  235. Action *zoomAction = ScaleTo::create(0.1f, _originalScale);
  236. zoomAction->setTag(kZoomActionTag);
  237. this->runAction(zoomAction);
  238. }
  239. }
  240. void MenuItemLabel::setEnabled(bool enabled)
  241. {
  242. if( _enabled != enabled )
  243. {
  244. if(enabled == false)
  245. {
  246. _colorBackup = this->getColor();
  247. this->setColor(_disabledColor);
  248. }
  249. else
  250. {
  251. this->setColor(_colorBackup);
  252. }
  253. }
  254. MenuItem::setEnabled(enabled);
  255. }
  256. //
  257. //CCMenuItemAtlasFont
  258. //
  259. MenuItemAtlasFont * MenuItemAtlasFont::create(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap)
  260. {
  261. return MenuItemAtlasFont::create(value, charMapFile, itemWidth, itemHeight, startCharMap, (const ccMenuCallback&)nullptr);
  262. }
  263. // FIXME:: deprecated
  264. MenuItemAtlasFont * MenuItemAtlasFont::create(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, Ref* target, SEL_MenuHandler selector)
  265. {
  266. MenuItemAtlasFont *ret = new (std::nothrow) MenuItemAtlasFont();
  267. ret->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap, std::bind(selector, target, std::placeholders::_1));
  268. ret->autorelease();
  269. return ret;
  270. }
  271. MenuItemAtlasFont * MenuItemAtlasFont::create(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback)
  272. {
  273. MenuItemAtlasFont *ret = new (std::nothrow) MenuItemAtlasFont();
  274. ret->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap, callback);
  275. ret->autorelease();
  276. return ret;
  277. }
  278. // FIXME:: deprecated
  279. bool MenuItemAtlasFont::initWithString(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, Ref* target, SEL_MenuHandler selector)
  280. {
  281. return initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap, std::bind(selector,target, std::placeholders::_1) );
  282. }
  283. bool MenuItemAtlasFont::initWithString(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback)
  284. {
  285. CCASSERT( value.size() != 0, "value length must be greater than 0");
  286. LabelAtlas *label = LabelAtlas::create();
  287. label->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap);
  288. if (MenuItemLabel::initWithLabel(label, callback))
  289. {
  290. // do something ?
  291. }
  292. return true;
  293. }
  294. //
  295. //CCMenuItemFont
  296. //
  297. void MenuItemFont::setFontSize(int s)
  298. {
  299. _globalFontSize = s;
  300. }
  301. int MenuItemFont::getFontSize()
  302. {
  303. return _globalFontSize;
  304. }
  305. void MenuItemFont::setFontName(const std::string& name)
  306. {
  307. if (_globalFontNameRelease)
  308. {
  309. _globalFontName.clear();
  310. }
  311. _globalFontName = name;
  312. _globalFontNameRelease = true;
  313. }
  314. const std::string& MenuItemFont::getFontName()
  315. {
  316. return _globalFontName;
  317. }
  318. // FIXME:: deprecated
  319. MenuItemFont * MenuItemFont::create(const std::string& value, Ref* target, SEL_MenuHandler selector)
  320. {
  321. MenuItemFont *ret = new (std::nothrow) MenuItemFont();
  322. ret->initWithString(value, std::bind(selector, target, std::placeholders::_1));
  323. ret->autorelease();
  324. return ret;
  325. }
  326. MenuItemFont * MenuItemFont::create(const std::string& value, const ccMenuCallback& callback)
  327. {
  328. MenuItemFont *ret = new (std::nothrow) MenuItemFont();
  329. ret->initWithString(value, callback);
  330. ret->autorelease();
  331. return ret;
  332. }
  333. MenuItemFont * MenuItemFont::create(const std::string& value)
  334. {
  335. MenuItemFont *ret = new (std::nothrow) MenuItemFont();
  336. ret->initWithString(value, (const ccMenuCallback&)nullptr);
  337. ret->autorelease();
  338. return ret;
  339. }
  340. MenuItemFont::MenuItemFont()
  341. : _fontSize(0), _fontName("")
  342. {}
  343. MenuItemFont::~MenuItemFont()
  344. {
  345. CCLOGINFO("In the destructor of MenuItemFont (%p).", this);
  346. }
  347. // FIXME:: deprecated
  348. bool MenuItemFont::initWithString(const std::string& value, Ref* target, SEL_MenuHandler selector)
  349. {
  350. CCASSERT( !value.empty(), "Value length must be greater than 0");
  351. return initWithString(value, std::bind(selector,target, std::placeholders::_1) );
  352. }
  353. bool MenuItemFont::initWithString(const std::string& value, const ccMenuCallback& callback)
  354. {
  355. CCASSERT( !value.empty(), "Value length must be greater than 0");
  356. _fontName = _globalFontName;
  357. _fontSize = _globalFontSize;
  358. Label *label = Label::createWithSystemFont(value, _fontName, _fontSize);
  359. if (MenuItemLabel::initWithLabel(label, callback))
  360. {
  361. // do something ?
  362. }
  363. return true;
  364. }
  365. void MenuItemFont::setFontSizeObj(int s)
  366. {
  367. _fontSize = s;
  368. dynamic_cast<Label*>(_label)->setSystemFontSize(_fontSize);
  369. this->setContentSize(dynamic_cast<Label*>(_label)->getContentSize());
  370. }
  371. int MenuItemFont::getFontSizeObj() const
  372. {
  373. return _fontSize;
  374. }
  375. void MenuItemFont::setFontNameObj(const std::string& name)
  376. {
  377. _fontName = name;
  378. dynamic_cast<Label*>(_label)->setSystemFontName(_fontName);
  379. this->setContentSize(dynamic_cast<Label*>(_label)->getContentSize());
  380. }
  381. const std::string& MenuItemFont::getFontNameObj() const
  382. {
  383. return _fontName;
  384. }
  385. //
  386. //CCMenuItemSprite
  387. //
  388. void MenuItemSprite::setNormalImage(Node* image)
  389. {
  390. if (image != _normalImage)
  391. {
  392. if (image)
  393. {
  394. addChild(image);
  395. image->setAnchorPoint(Vec2(0, 0));
  396. }
  397. if (_normalImage)
  398. {
  399. removeChild(_normalImage, true);
  400. }
  401. _normalImage = image;
  402. if (_normalImage != nullptr)
  403. {
  404. this->setContentSize(_normalImage->getContentSize());
  405. }
  406. this->updateImagesVisibility();
  407. }
  408. }
  409. void MenuItemSprite::setSelectedImage(Node* image)
  410. {
  411. if (image != _normalImage)
  412. {
  413. if (image)
  414. {
  415. addChild(image);
  416. image->setAnchorPoint(Vec2(0, 0));
  417. }
  418. if (_selectedImage)
  419. {
  420. removeChild(_selectedImage, true);
  421. }
  422. _selectedImage = image;
  423. this->updateImagesVisibility();
  424. }
  425. }
  426. void MenuItemSprite::setDisabledImage(Node* image)
  427. {
  428. if (image != _normalImage)
  429. {
  430. if (image)
  431. {
  432. addChild(image);
  433. image->setAnchorPoint(Vec2(0, 0));
  434. }
  435. if (_disabledImage)
  436. {
  437. removeChild(_disabledImage, true);
  438. }
  439. _disabledImage = image;
  440. this->updateImagesVisibility();
  441. }
  442. }
  443. //
  444. //CCMenuItemSprite
  445. //
  446. MenuItemSprite * MenuItemSprite::create(Node* normalSprite, Node* selectedSprite, Node* disabledSprite)
  447. {
  448. return MenuItemSprite::create(normalSprite, selectedSprite, disabledSprite, (const ccMenuCallback&)nullptr);
  449. }
  450. // FIXME: deprecated
  451. MenuItemSprite * MenuItemSprite::create(Node* normalSprite, Node* selectedSprite, Ref* target, SEL_MenuHandler selector)
  452. {
  453. return MenuItemSprite::create(normalSprite, selectedSprite, nullptr, std::bind(selector, target, std::placeholders::_1));
  454. }
  455. MenuItemSprite * MenuItemSprite::create(Node* normalSprite, Node* selectedSprite, const ccMenuCallback& callback)
  456. {
  457. return MenuItemSprite::create(normalSprite, selectedSprite, nullptr, callback);
  458. }
  459. // FIXME: deprecated
  460. MenuItemSprite * MenuItemSprite::create(Node *normalSprite, Node *selectedSprite, Node *disabledSprite, Ref *target, SEL_MenuHandler selector)
  461. {
  462. MenuItemSprite *ret = new (std::nothrow) MenuItemSprite();
  463. ret->initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, std::bind(selector, target, std::placeholders::_1));
  464. ret->autorelease();
  465. return ret;
  466. }
  467. MenuItemSprite * MenuItemSprite::create(Node *normalSprite, Node *selectedSprite, Node *disabledSprite, const ccMenuCallback& callback)
  468. {
  469. MenuItemSprite *ret = new (std::nothrow) MenuItemSprite();
  470. ret->initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, callback);
  471. ret->autorelease();
  472. return ret;
  473. }
  474. // FIXME: deprecated
  475. bool MenuItemSprite::initWithNormalSprite(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, Ref* target, SEL_MenuHandler selector)
  476. {
  477. return initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, std::bind(selector,target, std::placeholders::_1) );
  478. }
  479. bool MenuItemSprite::initWithNormalSprite(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, const ccMenuCallback& callback)
  480. {
  481. MenuItem::initWithCallback(callback);
  482. setNormalImage(normalSprite);
  483. setSelectedImage(selectedSprite);
  484. setDisabledImage(disabledSprite);
  485. if(_normalImage)
  486. {
  487. this->setContentSize(_normalImage->getContentSize());
  488. }
  489. setCascadeColorEnabled(true);
  490. setCascadeOpacityEnabled(true);
  491. return true;
  492. }
  493. /**
  494. @since v0.99.5
  495. */
  496. void MenuItemSprite::selected()
  497. {
  498. MenuItem::selected();
  499. if (_normalImage)
  500. {
  501. if (_disabledImage)
  502. {
  503. _disabledImage->setVisible(false);
  504. }
  505. if (_selectedImage)
  506. {
  507. _normalImage->setVisible(false);
  508. _selectedImage->setVisible(true);
  509. }
  510. else
  511. {
  512. _normalImage->setVisible(true);
  513. }
  514. }
  515. }
  516. void MenuItemSprite::unselected()
  517. {
  518. MenuItem::unselected();
  519. this->updateImagesVisibility();
  520. }
  521. void MenuItemSprite::setEnabled(bool bEnabled)
  522. {
  523. if( _enabled != bEnabled )
  524. {
  525. MenuItem::setEnabled(bEnabled);
  526. this->updateImagesVisibility();
  527. }
  528. }
  529. // Helper
  530. void MenuItemSprite::updateImagesVisibility()
  531. {
  532. if (_enabled)
  533. {
  534. if (_normalImage) _normalImage->setVisible(true);
  535. if (_selectedImage) _selectedImage->setVisible(false);
  536. if (_disabledImage) _disabledImage->setVisible(false);
  537. }
  538. else
  539. {
  540. if (_disabledImage)
  541. {
  542. if (_normalImage) _normalImage->setVisible(false);
  543. if (_selectedImage) _selectedImage->setVisible(false);
  544. if (_disabledImage) _disabledImage->setVisible(true);
  545. }
  546. else
  547. {
  548. if (_normalImage) _normalImage->setVisible(true);
  549. if (_selectedImage) _selectedImage->setVisible(false);
  550. if (_disabledImage) _disabledImage->setVisible(false);
  551. }
  552. }
  553. }
  554. ///
  555. /// MenuItemImage
  556. ///
  557. MenuItemImage* MenuItemImage::create()
  558. {
  559. MenuItemImage *ret = new (std::nothrow) MenuItemImage();
  560. if (ret && ret->init())
  561. {
  562. ret->autorelease();
  563. return ret;
  564. }
  565. CC_SAFE_DELETE(ret);
  566. return nullptr;
  567. }
  568. bool MenuItemImage::init(void)
  569. {
  570. return initWithNormalImage("", "", "", (const ccMenuCallback&)nullptr);
  571. }
  572. MenuItemImage * MenuItemImage::create(const std::string& normalImage, const std::string& selectedImage)
  573. {
  574. return MenuItemImage::create(normalImage, selectedImage, "", (const ccMenuCallback&)nullptr);
  575. }
  576. // FIXME: deprecated
  577. MenuItemImage * MenuItemImage::create(const std::string& normalImage, const std::string& selectedImage, Ref* target, SEL_MenuHandler selector)
  578. {
  579. return MenuItemImage::create(normalImage, selectedImage, "", std::bind(selector, target, std::placeholders::_1));
  580. }
  581. MenuItemImage * MenuItemImage::create(const std::string& normalImage, const std::string& selectedImage, const ccMenuCallback& callback)
  582. {
  583. return MenuItemImage::create(normalImage, selectedImage, "", callback);
  584. }
  585. // FIXME: deprecated
  586. MenuItemImage * MenuItemImage::create(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, Ref* target, SEL_MenuHandler selector)
  587. {
  588. MenuItemImage *ret = new (std::nothrow) MenuItemImage();
  589. if (ret && ret->initWithNormalImage(normalImage, selectedImage, disabledImage, std::bind(selector, target, std::placeholders::_1)))
  590. {
  591. ret->autorelease();
  592. return ret;
  593. }
  594. CC_SAFE_DELETE(ret);
  595. return nullptr;
  596. }
  597. MenuItemImage * MenuItemImage::create(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, const ccMenuCallback& callback)
  598. {
  599. MenuItemImage *ret = new (std::nothrow) MenuItemImage();
  600. if (ret && ret->initWithNormalImage(normalImage, selectedImage, disabledImage, callback))
  601. {
  602. ret->autorelease();
  603. return ret;
  604. }
  605. CC_SAFE_DELETE(ret);
  606. return nullptr;
  607. }
  608. MenuItemImage * MenuItemImage::create(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage)
  609. {
  610. MenuItemImage *ret = new (std::nothrow) MenuItemImage();
  611. if (ret && ret->initWithNormalImage(normalImage, selectedImage, disabledImage, (const ccMenuCallback&)nullptr))
  612. {
  613. ret->autorelease();
  614. return ret;
  615. }
  616. CC_SAFE_DELETE(ret);
  617. return nullptr;
  618. }
  619. // FIXME:: deprecated
  620. bool MenuItemImage::initWithNormalImage(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, Ref* target, SEL_MenuHandler selector)
  621. {
  622. return initWithNormalImage(normalImage, selectedImage, disabledImage, std::bind(selector,target, std::placeholders::_1) );
  623. }
  624. bool MenuItemImage::initWithNormalImage(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, const ccMenuCallback& callback)
  625. {
  626. Node *normalSprite = nullptr;
  627. Node *selectedSprite = nullptr;
  628. Node *disabledSprite = nullptr;
  629. if (normalImage.size() >0)
  630. {
  631. normalSprite = Sprite::create(normalImage);
  632. }
  633. if (selectedImage.size() >0)
  634. {
  635. selectedSprite = Sprite::create(selectedImage);
  636. }
  637. if(disabledImage.size() >0)
  638. {
  639. disabledSprite = Sprite::create(disabledImage);
  640. }
  641. return initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, callback);
  642. }
  643. //
  644. // Setter of sprite frames
  645. //
  646. void MenuItemImage::setNormalSpriteFrame(SpriteFrame * frame)
  647. {
  648. setNormalImage(Sprite::createWithSpriteFrame(frame));
  649. }
  650. void MenuItemImage::setSelectedSpriteFrame(SpriteFrame * frame)
  651. {
  652. setSelectedImage(Sprite::createWithSpriteFrame(frame));
  653. }
  654. void MenuItemImage::setDisabledSpriteFrame(SpriteFrame * frame)
  655. {
  656. setDisabledImage(Sprite::createWithSpriteFrame(frame));
  657. }
  658. //
  659. // MenuItemToggle
  660. //
  661. // FIXME:: deprecated
  662. MenuItemToggle * MenuItemToggle::createWithTarget(Ref* target, SEL_MenuHandler selector, const Vector<MenuItem*>& menuItems)
  663. {
  664. MenuItemToggle *ret = new (std::nothrow) MenuItemToggle();
  665. ret->MenuItem::initWithCallback(std::bind(selector, target, std::placeholders::_1));
  666. ret->autorelease();
  667. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  668. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  669. if (sEngine)
  670. {
  671. for (const auto &item : menuItems)
  672. {
  673. if (item)
  674. {
  675. sEngine->retainScriptObject(ret, item);
  676. }
  677. }
  678. }
  679. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  680. ret->_subItems = menuItems;
  681. ret->_selectedIndex = UINT_MAX;
  682. ret->setSelectedIndex(0);
  683. return ret;
  684. }
  685. MenuItemToggle * MenuItemToggle::createWithCallback(const ccMenuCallback &callback, const Vector<MenuItem*>& menuItems)
  686. {
  687. MenuItemToggle *ret = new (std::nothrow) MenuItemToggle();
  688. ret->MenuItem::initWithCallback(callback);
  689. ret->autorelease();
  690. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  691. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  692. if (sEngine)
  693. {
  694. for (const auto &item : menuItems)
  695. {
  696. if (item)
  697. {
  698. sEngine->retainScriptObject(ret, item);
  699. }
  700. }
  701. }
  702. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  703. ret->_subItems = menuItems;
  704. ret->_selectedIndex = UINT_MAX;
  705. ret->setSelectedIndex(0);
  706. return ret;
  707. }
  708. // FIXME:: deprecated
  709. MenuItemToggle * MenuItemToggle::createWithTarget(Ref* target, SEL_MenuHandler selector, MenuItem* item, ...)
  710. {
  711. va_list args;
  712. va_start(args, item);
  713. MenuItemToggle *ret = new (std::nothrow) MenuItemToggle();
  714. ret->initWithCallback(std::bind(selector, target, std::placeholders::_1), item, args);
  715. ret->autorelease();
  716. va_end(args);
  717. return ret;
  718. }
  719. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  720. MenuItemToggle * MenuItemToggle::createWithCallbackVA(const ccMenuCallback &callback, MenuItem* item, ...)
  721. {
  722. va_list args;
  723. va_start(args, item);
  724. MenuItemToggle *ret = new (std::nothrow) MenuItemToggle();
  725. ret->initWithCallback(callback, item, args);
  726. ret->autorelease();
  727. va_end(args);
  728. return ret;
  729. }
  730. #else
  731. MenuItemToggle * MenuItemToggle::createWithCallback(const ccMenuCallback &callback, MenuItem* item, ...)
  732. {
  733. va_list args;
  734. va_start(args, item);
  735. MenuItemToggle *ret = new (std::nothrow) MenuItemToggle();
  736. ret->initWithCallback(callback, item, args);
  737. ret->autorelease();
  738. va_end(args);
  739. return ret;
  740. }
  741. #endif
  742. MenuItemToggle * MenuItemToggle::create()
  743. {
  744. MenuItemToggle *ret = new (std::nothrow) MenuItemToggle();
  745. ret->initWithItem(nullptr);
  746. ret->autorelease();
  747. return ret;
  748. }
  749. // FIXME:: deprecated
  750. bool MenuItemToggle::initWithTarget(Ref* target, SEL_MenuHandler selector, MenuItem* item, va_list args)
  751. {
  752. return initWithCallback(std::bind( selector, target, std::placeholders::_1), item, args);
  753. }
  754. bool MenuItemToggle::initWithCallback(const ccMenuCallback &callback, MenuItem *item, va_list args)
  755. {
  756. MenuItem::initWithCallback(callback);
  757. int z = 0;
  758. MenuItem *i = item;
  759. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  760. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  761. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  762. while(i)
  763. {
  764. z++;
  765. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  766. if (sEngine)
  767. {
  768. sEngine->retainScriptObject(this, i);
  769. }
  770. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  771. _subItems.pushBack(i);
  772. i = va_arg(args, MenuItem*);
  773. }
  774. _selectedIndex = UINT_MAX;
  775. this->setSelectedIndex(0);
  776. return true;
  777. }
  778. MenuItemToggle* MenuItemToggle::create(MenuItem *item)
  779. {
  780. MenuItemToggle *ret = new (std::nothrow) MenuItemToggle();
  781. ret->initWithItem(item);
  782. ret->autorelease();
  783. return ret;
  784. }
  785. bool MenuItemToggle::initWithItem(MenuItem *item)
  786. {
  787. MenuItem::initWithCallback((const ccMenuCallback&)nullptr);
  788. if (item)
  789. {
  790. addSubItem(item);
  791. }
  792. _selectedIndex = UINT_MAX;
  793. this->setSelectedIndex(0);
  794. setCascadeColorEnabled(true);
  795. setCascadeOpacityEnabled(true);
  796. return true;
  797. }
  798. void MenuItemToggle::addSubItem(MenuItem *item)
  799. {
  800. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  801. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  802. if (sEngine)
  803. {
  804. sEngine->retainScriptObject(this, item);
  805. }
  806. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  807. _subItems.pushBack(item);
  808. }
  809. void MenuItemToggle::cleanup()
  810. {
  811. for(const auto &item : _subItems) {
  812. #if defined(CC_NATIVE_CONTROL_SCRIPT) && !CC_NATIVE_CONTROL_SCRIPT
  813. ScriptEngineManager::getInstance()->getScriptEngine()->releaseScriptObject(this, item);
  814. #endif
  815. item->cleanup();
  816. }
  817. MenuItem::cleanup();
  818. }
  819. void MenuItemToggle::setSelectedIndex(unsigned int index)
  820. {
  821. if( index != _selectedIndex && _subItems.size() > 0 )
  822. {
  823. _selectedIndex = index;
  824. if (_selectedItem)
  825. {
  826. _selectedItem->removeFromParentAndCleanup(false);
  827. }
  828. _selectedItem = _subItems.at(_selectedIndex);
  829. this->addChild(_selectedItem);
  830. Size s = _selectedItem->getContentSize();
  831. this->setContentSize(s);
  832. _selectedItem->setPosition(s.width / 2, s.height / 2);
  833. }
  834. }
  835. void MenuItemToggle::selected()
  836. {
  837. MenuItem::selected();
  838. _subItems.at(_selectedIndex)->selected();
  839. }
  840. void MenuItemToggle::unselected()
  841. {
  842. MenuItem::unselected();
  843. _subItems.at(_selectedIndex)->unselected();
  844. }
  845. void MenuItemToggle::activate()
  846. {
  847. // update index
  848. if( _enabled )
  849. {
  850. unsigned int newIndex = (_selectedIndex + 1) % _subItems.size();
  851. this->setSelectedIndex(newIndex);
  852. }
  853. MenuItem::activate();
  854. }
  855. void MenuItemToggle::setEnabled(bool enabled)
  856. {
  857. if (_enabled != enabled)
  858. {
  859. MenuItem::setEnabled(enabled);
  860. for(const auto &item : _subItems) {
  861. item->setEnabled(enabled);
  862. }
  863. }
  864. }
  865. MenuItem* MenuItemToggle::getSelectedItem()
  866. {
  867. return _subItems.at(_selectedIndex);
  868. }
  869. NS_CC_END