UIButton.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "ui/UIButton.h"
  22. #include "ui/UIScale9Sprite.h"
  23. #include "2d/CCLabel.h"
  24. #include "2d/CCSprite.h"
  25. #include "2d/CCActionInterval.h"
  26. #include "platform/CCFileUtils.h"
  27. #include "ui/UIHelper.h"
  28. #include <algorithm>
  29. #include "editor-support/cocostudio/CocosStudioExtension.h"
  30. NS_CC_BEGIN
  31. namespace ui {
  32. static const int NORMAL_RENDERER_Z = (-2);
  33. static const int PRESSED_RENDERER_Z = (-2);
  34. static const int DISABLED_RENDERER_Z = (-2);
  35. static const int TITLE_RENDERER_Z = (-1);
  36. static const float ZOOM_ACTION_TIME_STEP = 0.05f;
  37. IMPLEMENT_CLASS_GUI_INFO(Button)
  38. Button::Button():
  39. _buttonNormalRenderer(nullptr),
  40. _buttonClickedRenderer(nullptr),
  41. _buttonDisabledRenderer(nullptr),
  42. _titleRenderer(nullptr),
  43. _actionNodes(0),
  44. _zoomScale(-0.09f),
  45. _normalFileName(""),
  46. _clickedFileName(""),
  47. _disabledFileName(""),
  48. _normalTexType(TextureResType::LOCAL),
  49. _pressedTexType(TextureResType::LOCAL),
  50. _disabledTexType(TextureResType::LOCAL),
  51. _fontName(""),
  52. _prevIgnoreSize(true),
  53. _scale9Enabled(false),
  54. _pressedActionEnabled(false),
  55. _capInsetsNormal(Rect::ZERO),
  56. _capInsetsPressed(Rect::ZERO),
  57. _capInsetsDisabled(Rect::ZERO),
  58. _normalTextureSize(_contentSize),
  59. _pressedTextureSize(_contentSize),
  60. _disabledTextureSize(_contentSize),
  61. _normalTextureLoaded(false),
  62. _pressedTextureLoaded(false),
  63. _disabledTextureLoaded(false),
  64. _normalTextureAdaptDirty(true),
  65. _pressedTextureAdaptDirty(true),
  66. _disabledTextureAdaptDirty(true),
  67. _fontSize(10),
  68. _type(FontType::SYSTEM)
  69. {
  70. setTouchEnabled(true);
  71. }
  72. Button::~Button()
  73. {
  74. }
  75. Button* Button::create()
  76. {
  77. Button* widget = new (std::nothrow) Button();
  78. if (widget && widget->init())
  79. {
  80. widget->autorelease();
  81. return widget;
  82. }
  83. CC_SAFE_DELETE(widget);
  84. return nullptr;
  85. }
  86. Button* Button::create(const std::string &normalImage,
  87. const std::string& selectedImage ,
  88. const std::string& disableImage,
  89. TextureResType texType)
  90. {
  91. Button *btn = new (std::nothrow) Button;
  92. if (btn && btn->init(normalImage,selectedImage,disableImage,texType))
  93. {
  94. btn->autorelease();
  95. return btn;
  96. }
  97. CC_SAFE_DELETE(btn);
  98. return nullptr;
  99. }
  100. bool Button::init(const std::string &normalImage,
  101. const std::string& selectedImage ,
  102. const std::string& disableImage,
  103. TextureResType texType)
  104. {
  105. bool ret = true;
  106. do
  107. {
  108. if (!Widget::init())
  109. {
  110. ret = false;
  111. break;
  112. }
  113. this->loadTextures(normalImage, selectedImage, disableImage,texType);
  114. } while (0);
  115. return ret;
  116. }
  117. bool Button::init()
  118. {
  119. if (Widget::init())
  120. {
  121. return true;
  122. }
  123. return false;
  124. }
  125. void Button::initRenderer()
  126. {
  127. _buttonNormalRenderer = Scale9Sprite::create();
  128. _buttonClickedRenderer = Scale9Sprite::create();
  129. _buttonDisabledRenderer = Scale9Sprite::create();
  130. _buttonClickedRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
  131. _buttonNormalRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
  132. _buttonDisabledRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
  133. addProtectedChild(_buttonNormalRenderer, NORMAL_RENDERER_Z, -1);
  134. addProtectedChild(_buttonClickedRenderer, PRESSED_RENDERER_Z, -1);
  135. addProtectedChild(_buttonDisabledRenderer, DISABLED_RENDERER_Z, -1);
  136. }
  137. void Button::createTitleRenderer()
  138. {
  139. _titleRenderer = Label::create();
  140. _titleRenderer->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
  141. addProtectedChild(_titleRenderer, TITLE_RENDERER_Z, -1);
  142. }
  143. /** replaces the current Label node with a new one */
  144. void Button::setTitleLabel(Label* label)
  145. {
  146. if (_titleRenderer != label) {
  147. CC_SAFE_RELEASE(_titleRenderer);
  148. _titleRenderer = label;
  149. CC_SAFE_RETAIN(_titleRenderer);
  150. addProtectedChild(_titleRenderer, TITLE_RENDERER_Z, -1);
  151. updateTitleLocation();
  152. }
  153. }
  154. /** returns the current Label being used */
  155. Label* Button::getTitleLabel() const
  156. {
  157. return _titleRenderer;
  158. }
  159. void Button::setScale9Enabled(bool able)
  160. {
  161. if (_scale9Enabled == able)
  162. {
  163. return;
  164. }
  165. _scale9Enabled = able;
  166. if (_scale9Enabled) {
  167. _buttonNormalRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE);
  168. _buttonClickedRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE);
  169. _buttonDisabledRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE);
  170. }else{
  171. _buttonNormalRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
  172. _buttonClickedRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
  173. _buttonDisabledRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
  174. }
  175. if (_scale9Enabled)
  176. {
  177. bool ignoreBefore = _ignoreSize;
  178. ignoreContentAdaptWithSize(false);
  179. _prevIgnoreSize = ignoreBefore;
  180. }
  181. else
  182. {
  183. ignoreContentAdaptWithSize(_prevIgnoreSize);
  184. }
  185. setCapInsetsNormalRenderer(_capInsetsNormal);
  186. setCapInsetsPressedRenderer(_capInsetsPressed);
  187. setCapInsetsDisabledRenderer(_capInsetsDisabled);
  188. _brightStyle = BrightStyle::NONE;
  189. setBright(_bright);
  190. _normalTextureAdaptDirty = true;
  191. _pressedTextureAdaptDirty = true;
  192. _disabledTextureAdaptDirty = true;
  193. }
  194. bool Button::isScale9Enabled()const
  195. {
  196. return _scale9Enabled;
  197. }
  198. void Button::ignoreContentAdaptWithSize(bool ignore)
  199. {
  200. if (_unifySize)
  201. {
  202. this->updateContentSize();
  203. return;
  204. }
  205. if (!_scale9Enabled || (_scale9Enabled && !ignore))
  206. {
  207. Widget::ignoreContentAdaptWithSize(ignore);
  208. _prevIgnoreSize = ignore;
  209. }
  210. }
  211. void Button::loadTextures(const std::string& normal,
  212. const std::string& selected,
  213. const std::string& disabled,
  214. TextureResType texType)
  215. {
  216. loadTextureNormal(normal,texType);
  217. loadTexturePressed(selected,texType);
  218. loadTextureDisabled(disabled,texType);
  219. }
  220. void Button::loadTextureNormal(const std::string& normal,TextureResType texType)
  221. {
  222. bool textureLoaded = true;
  223. if (normal.empty())
  224. {
  225. _buttonNormalRenderer->resetRender();
  226. textureLoaded = false;
  227. }
  228. else
  229. {
  230. if (texType != _normalTexType)
  231. {
  232. _buttonNormalRenderer->resetRender();
  233. }
  234. switch (texType)
  235. {
  236. case TextureResType::LOCAL:
  237. _buttonNormalRenderer->initWithFile(normal);
  238. break;
  239. case TextureResType::PLIST:
  240. _buttonNormalRenderer->initWithSpriteFrameName(normal);
  241. break;
  242. default:
  243. break;
  244. }
  245. }
  246. _normalFileName = normal;
  247. _normalTexType = texType;
  248. //FIXME: https://github.com/cocos2d/cocos2d-x/issues/12249
  249. if (!_ignoreSize && _customSize.equals(Size::ZERO)) {
  250. _customSize = _buttonNormalRenderer->getContentSize();
  251. }
  252. this->setupNormalTexture(textureLoaded);
  253. }
  254. void Button::setupNormalTexture(bool textureLoaded)
  255. {
  256. _normalTextureSize = _buttonNormalRenderer->getContentSize();
  257. this->updateChildrenDisplayedRGBA();
  258. if (_unifySize )
  259. {
  260. if (!_scale9Enabled)
  261. {
  262. updateContentSizeWithTextureSize(this->getNormalSize());
  263. }
  264. }
  265. else
  266. {
  267. updateContentSizeWithTextureSize(_normalTextureSize);
  268. }
  269. _normalTextureLoaded = textureLoaded;
  270. _normalTextureAdaptDirty = true;
  271. }
  272. void Button::loadTextureNormal(SpriteFrame* normalSpriteFrame)
  273. {
  274. _buttonNormalRenderer->initWithSpriteFrame(normalSpriteFrame);
  275. this->setupNormalTexture(nullptr != normalSpriteFrame);
  276. }
  277. void Button::loadTexturePressed(const std::string& selected,TextureResType texType)
  278. {
  279. bool textureLoaded = true;
  280. if (selected.empty())
  281. {
  282. _buttonClickedRenderer->resetRender();
  283. textureLoaded = false;
  284. }
  285. else
  286. {
  287. if (texType != _pressedTexType)
  288. {
  289. _buttonClickedRenderer->resetRender();
  290. }
  291. switch (texType)
  292. {
  293. case TextureResType::LOCAL:
  294. _buttonClickedRenderer->initWithFile(selected);
  295. break;
  296. case TextureResType::PLIST:
  297. _buttonClickedRenderer->initWithSpriteFrameName(selected);
  298. break;
  299. default:
  300. break;
  301. }
  302. }
  303. _clickedFileName = selected;
  304. _pressedTexType = texType;
  305. this->setupPressedTexture(textureLoaded);
  306. }
  307. void Button::setupPressedTexture(bool textureLoaded)
  308. {
  309. _pressedTextureSize = _buttonClickedRenderer->getContentSize();
  310. this->updateChildrenDisplayedRGBA();
  311. _pressedTextureLoaded = textureLoaded;
  312. _pressedTextureAdaptDirty = true;
  313. }
  314. void Button::loadTexturePressed(SpriteFrame* pressedSpriteFrame)
  315. {
  316. _buttonClickedRenderer->initWithSpriteFrame(pressedSpriteFrame);
  317. this->setupPressedTexture(nullptr != pressedSpriteFrame);
  318. }
  319. void Button::loadTextureDisabled(const std::string& disabled,TextureResType texType)
  320. {
  321. bool textureLoaded = true;
  322. if (disabled.empty())
  323. {
  324. _buttonDisabledRenderer->resetRender();
  325. textureLoaded = false;
  326. }
  327. else
  328. {
  329. if (texType != _disabledTexType)
  330. {
  331. _buttonDisabledRenderer->resetRender();
  332. }
  333. switch (texType)
  334. {
  335. case TextureResType::LOCAL:
  336. _buttonDisabledRenderer->initWithFile(disabled);
  337. break;
  338. case TextureResType::PLIST:
  339. _buttonDisabledRenderer->initWithSpriteFrameName(disabled);
  340. break;
  341. default:
  342. break;
  343. }
  344. }
  345. _disabledFileName = disabled;
  346. _disabledTexType = texType;
  347. this->setupDisabledTexture(textureLoaded);
  348. }
  349. void Button::setupDisabledTexture(bool textureLoaded)
  350. {
  351. _disabledTextureSize = _buttonDisabledRenderer->getContentSize();
  352. this->updateChildrenDisplayedRGBA();
  353. _disabledTextureLoaded = textureLoaded;
  354. _disabledTextureAdaptDirty = true;
  355. }
  356. void Button::loadTextureDisabled(SpriteFrame* disabledSpriteFrame)
  357. {
  358. _buttonDisabledRenderer->initWithSpriteFrame(disabledSpriteFrame);
  359. this->setupDisabledTexture(nullptr != disabledSpriteFrame);
  360. }
  361. void Button::setCapInsets(const Rect &capInsets)
  362. {
  363. setCapInsetsNormalRenderer(capInsets);
  364. setCapInsetsPressedRenderer(capInsets);
  365. setCapInsetsDisabledRenderer(capInsets);
  366. }
  367. void Button::setCapInsetsNormalRenderer(const Rect &capInsets)
  368. {
  369. _capInsetsNormal = Helper::restrictCapInsetRect(capInsets, this->_normalTextureSize);
  370. //for performance issue
  371. if (!_scale9Enabled)
  372. {
  373. return;
  374. }
  375. _buttonNormalRenderer->setCapInsets(_capInsetsNormal);
  376. }
  377. void Button::setCapInsetsPressedRenderer(const Rect &capInsets)
  378. {
  379. _capInsetsPressed = Helper::restrictCapInsetRect(capInsets, this->_pressedTextureSize);
  380. //for performance issue
  381. if (!_scale9Enabled)
  382. {
  383. return;
  384. }
  385. _buttonClickedRenderer->setCapInsets(_capInsetsPressed);
  386. }
  387. void Button::setCapInsetsDisabledRenderer(const Rect &capInsets)
  388. {
  389. _capInsetsDisabled = Helper::restrictCapInsetRect(capInsets, this->_disabledTextureSize);
  390. //for performance issue
  391. if (!_scale9Enabled)
  392. {
  393. return;
  394. }
  395. _buttonDisabledRenderer->setCapInsets(_capInsetsDisabled);
  396. }
  397. const Rect& Button::getCapInsetsNormalRenderer()const
  398. {
  399. return _capInsetsNormal;
  400. }
  401. const Rect& Button::getCapInsetsPressedRenderer()const
  402. {
  403. return _capInsetsPressed;
  404. }
  405. const Rect& Button::getCapInsetsDisabledRenderer()const
  406. {
  407. return _capInsetsDisabled;
  408. }
  409. void Button::onPressStateChangedToNormal()
  410. {
  411. _buttonNormalRenderer->setVisible(true);
  412. _buttonClickedRenderer->setVisible(false);
  413. _buttonDisabledRenderer->setVisible(false);
  414. _buttonNormalRenderer->setState(Scale9Sprite::State::NORMAL);
  415. if (_pressedTextureLoaded)
  416. {
  417. if (_pressedActionEnabled)
  418. {
  419. _buttonNormalRenderer->stopAllActions();
  420. _buttonClickedRenderer->stopAllActions();
  421. // Action *zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, _normalTextureScaleXInSize, _normalTextureScaleYInSize);
  422. //fixme: the zoomAction will run in the next frame which will cause the _buttonNormalRenderer to a wrong scale
  423. _buttonNormalRenderer->setScale(1.0);
  424. _buttonClickedRenderer->setScale(1.0);
  425. if (nullptr != _titleRenderer)
  426. {
  427. _titleRenderer->stopAllActions();
  428. if (_unifySize)
  429. {
  430. Action *zoomTitleAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0f, 1.0f);
  431. _titleRenderer->runAction(zoomTitleAction);
  432. }
  433. else
  434. {
  435. _titleRenderer->setScaleX(1.0f);
  436. _titleRenderer->setScaleY(1.0f);
  437. }
  438. }
  439. }
  440. }
  441. else
  442. {
  443. _buttonNormalRenderer->stopAllActions();
  444. _buttonNormalRenderer->setScale(1.0);
  445. if (nullptr != _titleRenderer)
  446. {
  447. _titleRenderer->stopAllActions();
  448. _titleRenderer->setScaleX(1.0f);
  449. _titleRenderer->setScaleY(1.0f);
  450. }
  451. }
  452. if (_actionNodes.size() > 0)
  453. {
  454. Vector<Node*>::iterator it;
  455. for (it = _actionNodes.begin(); it != _actionNodes.end(); ++it)
  456. {
  457. (*it)->stopActionByTag(10001);
  458. (*it)->setScale(1.0f, 1.0f);
  459. }
  460. }
  461. }
  462. void Button::onPressStateChangedToPressed()
  463. {
  464. _buttonNormalRenderer->setState(Scale9Sprite::State::NORMAL);
  465. if (_pressedTextureLoaded)
  466. {
  467. _buttonNormalRenderer->setVisible(false);
  468. _buttonClickedRenderer->setVisible(true);
  469. _buttonDisabledRenderer->setVisible(false);
  470. if (_pressedActionEnabled)
  471. {
  472. _buttonNormalRenderer->stopAllActions();
  473. _buttonClickedRenderer->stopAllActions();
  474. Action *zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP,
  475. 1.0f + _zoomScale,
  476. 1.0f + _zoomScale);
  477. _buttonClickedRenderer->runAction(zoomAction);
  478. _buttonNormalRenderer->setScale(1.0f + _zoomScale,
  479. 1.0f + _zoomScale);
  480. if (_actionNodes.size() > 0)
  481. {
  482. Vector<Node*>::iterator it;
  483. for (it = _actionNodes.begin(); it != _actionNodes.end(); ++it)
  484. {
  485. auto ani = zoomAction->clone();
  486. ani->setTag(10001);
  487. (*it)->stopActionByTag(10001);
  488. (*it)->runAction(ani);
  489. }
  490. }
  491. if (nullptr != _titleRenderer)
  492. {
  493. _titleRenderer->stopAllActions();
  494. Action *zoomTitleAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP,
  495. 1.0f + _zoomScale, 1.0f + _zoomScale);
  496. _titleRenderer->runAction(zoomTitleAction);
  497. }
  498. }
  499. }
  500. else
  501. {
  502. _buttonNormalRenderer->setVisible(true);
  503. _buttonClickedRenderer->setVisible(true);
  504. _buttonDisabledRenderer->setVisible(false);
  505. _buttonNormalRenderer->stopAllActions();
  506. _buttonNormalRenderer->setScale(1.0f + _zoomScale, 1.0f + _zoomScale);
  507. if (_actionNodes.size() > 0)
  508. {
  509. Vector<Node*>::iterator it;
  510. for (it = _actionNodes.begin(); it != _actionNodes.end(); ++it)
  511. {
  512. Action *zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP,
  513. 1.0f + _zoomScale,
  514. 1.0f + _zoomScale);
  515. zoomAction->setTag(10001);
  516. (*it)->stopActionByTag(10001);
  517. (*it)->runAction(zoomAction);
  518. }
  519. }
  520. if (nullptr != _titleRenderer)
  521. {
  522. _titleRenderer->stopAllActions();
  523. _titleRenderer->setScaleX(1.0f + _zoomScale);
  524. _titleRenderer->setScaleY(1.0f + _zoomScale);
  525. }
  526. }
  527. }
  528. void Button::onPressStateChangedToDisabled()
  529. {
  530. //if disable resource is null
  531. if (!_disabledTextureLoaded)
  532. {
  533. if (_normalTextureLoaded)
  534. {
  535. _buttonNormalRenderer->setState(Scale9Sprite::State::GRAY);
  536. }
  537. }
  538. else
  539. {
  540. _buttonNormalRenderer->setVisible(false);
  541. _buttonDisabledRenderer->setVisible(true);
  542. }
  543. _buttonClickedRenderer->setVisible(false);
  544. _buttonNormalRenderer->setScale(1.0);
  545. _buttonClickedRenderer->setScale(1.0);
  546. if (_actionNodes.size() > 0)
  547. {
  548. Vector<Node*>::iterator it;
  549. for (it = _actionNodes.begin(); it != _actionNodes.end(); ++it)
  550. {
  551. (*it)->stopActionByTag(10001);
  552. (*it)->setScale(1.0f, 1.0f);
  553. }
  554. }
  555. }
  556. void Button::addActionNode(Node* node)
  557. {
  558. _actionNodes.pushBack(node);
  559. }
  560. void Button::clearActionNode()
  561. {
  562. _actionNodes.clear();
  563. }
  564. void Button::updateTitleLocation()
  565. {
  566. _titleRenderer->setPosition(_contentSize.width * 0.5f, _contentSize.height * 0.5f);
  567. }
  568. void Button::updateContentSize()
  569. {
  570. if (_unifySize)
  571. {
  572. if (_scale9Enabled)
  573. {
  574. ProtectedNode::setContentSize(_customSize);
  575. }
  576. else
  577. {
  578. Size s = getNormalSize();
  579. ProtectedNode::setContentSize(s);
  580. }
  581. onSizeChanged();
  582. return;
  583. }
  584. if (_ignoreSize)
  585. {
  586. this->setContentSize(getVirtualRendererSize());
  587. }
  588. }
  589. void Button::onSizeChanged()
  590. {
  591. Widget::onSizeChanged();
  592. if (nullptr != _titleRenderer)
  593. {
  594. updateTitleLocation();
  595. }
  596. _normalTextureAdaptDirty = true;
  597. _pressedTextureAdaptDirty = true;
  598. _disabledTextureAdaptDirty = true;
  599. }
  600. void Button::adaptRenderers()
  601. {
  602. if (_normalTextureAdaptDirty)
  603. {
  604. normalTextureScaleChangedWithSize();
  605. _normalTextureAdaptDirty = false;
  606. }
  607. if (_pressedTextureAdaptDirty)
  608. {
  609. pressedTextureScaleChangedWithSize();
  610. _pressedTextureAdaptDirty = false;
  611. }
  612. if (_disabledTextureAdaptDirty)
  613. {
  614. disabledTextureScaleChangedWithSize();
  615. _disabledTextureAdaptDirty = false;
  616. }
  617. }
  618. Size Button::getVirtualRendererSize() const
  619. {
  620. if (_unifySize)
  621. {
  622. return this->getNormalSize();
  623. }
  624. if (nullptr != _titleRenderer)
  625. {
  626. Size titleSize = _titleRenderer->getContentSize();
  627. if (!_normalTextureLoaded && _titleRenderer->getString().size() > 0)
  628. {
  629. return titleSize;
  630. }
  631. }
  632. return _normalTextureSize;
  633. }
  634. Node* Button::getVirtualRenderer()
  635. {
  636. if (_bright)
  637. {
  638. switch (_brightStyle)
  639. {
  640. case BrightStyle::NORMAL:
  641. return _buttonNormalRenderer;
  642. case BrightStyle::HIGHLIGHT:
  643. return _buttonClickedRenderer;
  644. default:
  645. return nullptr;
  646. }
  647. }
  648. else
  649. {
  650. return _buttonDisabledRenderer;
  651. }
  652. }
  653. void Button::normalTextureScaleChangedWithSize()
  654. {
  655. _buttonNormalRenderer->setPreferredSize(_contentSize);
  656. _buttonNormalRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
  657. }
  658. void Button::pressedTextureScaleChangedWithSize()
  659. {
  660. _buttonClickedRenderer->setPreferredSize(_contentSize);
  661. _buttonClickedRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
  662. }
  663. void Button::disabledTextureScaleChangedWithSize()
  664. {
  665. _buttonDisabledRenderer->setPreferredSize(_contentSize);
  666. _buttonDisabledRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
  667. }
  668. void Button::setPressedActionEnabled(bool enabled)
  669. {
  670. _pressedActionEnabled = enabled;
  671. }
  672. void Button::setTitleAlignment(TextHAlignment hAlignment)
  673. {
  674. if (nullptr == _titleRenderer)
  675. {
  676. this->createTitleRenderer();
  677. }
  678. _titleRenderer->setAlignment(hAlignment);
  679. }
  680. void Button::setTitleAlignment(TextHAlignment hAlignment, TextVAlignment vAlignment)
  681. {
  682. if (nullptr == _titleRenderer)
  683. {
  684. this->createTitleRenderer();
  685. }
  686. _titleRenderer->setAlignment(hAlignment, vAlignment);
  687. }
  688. void Button::setTitleText(const std::string& text)
  689. {
  690. if (text == getTitleText())
  691. {
  692. return;
  693. }
  694. if(nullptr == _titleRenderer)
  695. {
  696. this->createTitleRenderer();
  697. }
  698. _titleRenderer->setString(text);
  699. this->setTitleFontSize(_fontSize);
  700. updateContentSize();
  701. updateTitleLocation();
  702. }
  703. std::string Button::getTitleText() const
  704. {
  705. if(nullptr == _titleRenderer)
  706. {
  707. return "";
  708. }
  709. return _titleRenderer->getString();
  710. }
  711. void Button::setTitleColor(const Color3B& color)
  712. {
  713. if(nullptr == _titleRenderer)
  714. {
  715. this->createTitleRenderer();
  716. }
  717. _titleRenderer->setTextColor(Color4B(color));
  718. }
  719. Color3B Button::getTitleColor() const
  720. {
  721. if(nullptr == _titleRenderer)
  722. {
  723. return Color3B::WHITE;
  724. }
  725. return Color3B(_titleRenderer->getTextColor());
  726. }
  727. void Button::setTitleFontSize(float size)
  728. {
  729. if (nullptr == _titleRenderer)
  730. {
  731. this->createTitleRenderer();
  732. }
  733. _fontSize = size;
  734. if (_type == FontType::SYSTEM)
  735. {
  736. _titleRenderer->setSystemFontSize(_fontSize);
  737. }
  738. else if (_type == FontType::TTF)
  739. {
  740. TTFConfig config = _titleRenderer->getTTFConfig();
  741. config.fontSize = _fontSize;
  742. _titleRenderer->setTTFConfig(config);
  743. }
  744. //we can't change font size of BMFont.
  745. if(FontType::BMFONT != _type)
  746. {
  747. updateContentSize();
  748. }
  749. }
  750. float Button::getTitleFontSize() const
  751. {
  752. return _fontSize;
  753. }
  754. void Button::setZoomScale(float scale)
  755. {
  756. _zoomScale = scale;
  757. }
  758. float Button::getZoomScale()const
  759. {
  760. return _zoomScale;
  761. }
  762. void Button::setTitleFontName(const std::string& fontName)
  763. {
  764. if(nullptr == _titleRenderer)
  765. {
  766. this->createTitleRenderer();
  767. }
  768. if(FileUtils::getInstance()->isFileExist(fontName))
  769. {
  770. std::string lowerCasedFontName = fontName;
  771. std::transform(lowerCasedFontName.begin(), lowerCasedFontName.end(), lowerCasedFontName.begin(), ::tolower);
  772. if (lowerCasedFontName.find(".fnt") != std::string::npos)
  773. {
  774. _titleRenderer->setBMFontFilePath(fontName);
  775. _type = FontType::BMFONT;
  776. }
  777. else
  778. {
  779. TTFConfig config = _titleRenderer->getTTFConfig();
  780. config.fontFilePath = fontName;
  781. config.fontSize = _fontSize;
  782. _titleRenderer->setTTFConfig(config);
  783. _type = FontType::TTF;
  784. }
  785. }
  786. else
  787. {
  788. _titleRenderer->setSystemFontName(fontName);
  789. if (_type == FontType::TTF)
  790. {
  791. _titleRenderer->requestSystemFontRefresh();
  792. }
  793. _titleRenderer->setSystemFontSize(_fontSize);
  794. _type = FontType::SYSTEM;
  795. }
  796. _fontName = fontName;
  797. this->updateContentSize();
  798. }
  799. Label* Button::getTitleRenderer()const
  800. {
  801. return _titleRenderer;
  802. }
  803. std::string Button::getTitleFontName() const
  804. {
  805. if (nullptr != _titleRenderer)
  806. {
  807. if (this->_type == FontType::SYSTEM)
  808. {
  809. return _titleRenderer->getSystemFontName();
  810. }
  811. else if (this->_type == FontType::TTF)
  812. {
  813. return _titleRenderer->getTTFConfig().fontFilePath;
  814. }
  815. else
  816. {
  817. return _titleRenderer->getBMFontFilePath();
  818. }
  819. }
  820. else
  821. {
  822. return _fontName;
  823. }
  824. }
  825. std::string Button::getDescription() const
  826. {
  827. return "Button";
  828. }
  829. Widget* Button::createCloneInstance()
  830. {
  831. return Button::create();
  832. }
  833. void Button::copySpecialProperties(Widget *widget)
  834. {
  835. Button* button = dynamic_cast<Button*>(widget);
  836. if (button)
  837. {
  838. _prevIgnoreSize = button->_prevIgnoreSize;
  839. setScale9Enabled(button->_scale9Enabled);
  840. // clone the inner sprite: https://github.com/cocos2d/cocos2d-x/issues/16924
  841. button->_buttonNormalRenderer->copyTo(_buttonNormalRenderer);
  842. _normalFileName = button->_normalFileName;
  843. _normalTextureSize = button->_normalTextureSize;
  844. _normalTexType = button->_normalTexType;
  845. _normalTextureLoaded = button->_normalTextureLoaded;
  846. setupNormalTexture(!_normalFileName.empty());
  847. button->_buttonClickedRenderer->copyTo(_buttonClickedRenderer);
  848. _clickedFileName = button->_clickedFileName;
  849. _pressedTextureSize = button->_pressedTextureSize;
  850. _pressedTexType = button->_pressedTexType;
  851. _pressedTextureLoaded = button->_pressedTextureLoaded;
  852. setupPressedTexture(!_clickedFileName.empty());
  853. button->_buttonDisabledRenderer->copyTo(_buttonDisabledRenderer);
  854. _disabledFileName = button->_disabledFileName;
  855. _disabledTextureSize = button->_disabledTextureSize;
  856. _disabledTexType = button->_disabledTexType;
  857. _disabledTextureLoaded = button->_disabledTextureLoaded;
  858. setupDisabledTexture(!_disabledFileName.empty());
  859. setCapInsetsNormalRenderer(button->_capInsetsNormal);
  860. setCapInsetsPressedRenderer(button->_capInsetsPressed);
  861. setCapInsetsDisabledRenderer(button->_capInsetsDisabled);
  862. if(nullptr != button->getTitleRenderer())
  863. {
  864. setTitleText(button->getTitleText());
  865. setTitleFontName(button->getTitleFontName());
  866. setTitleFontSize(button->getTitleFontSize());
  867. setTitleColor(button->getTitleColor());
  868. }
  869. setPressedActionEnabled(button->_pressedActionEnabled);
  870. setZoomScale(button->_zoomScale);
  871. }
  872. }
  873. Size Button::getNormalSize() const
  874. {
  875. Size titleSize;
  876. if (_titleRenderer != nullptr)
  877. {
  878. titleSize = _titleRenderer->getContentSize();
  879. }
  880. Size imageSize;
  881. if (_buttonNormalRenderer != nullptr)
  882. {
  883. imageSize = _buttonNormalRenderer->getContentSize();
  884. }
  885. float width = titleSize.width > imageSize.width ? titleSize.width : imageSize.width;
  886. float height = titleSize.height > imageSize.height ? titleSize.height : imageSize.height;
  887. return Size(width,height);
  888. }
  889. Size Button::getNormalTextureSize() const
  890. {
  891. return _normalTextureSize;
  892. }
  893. void Button::resetNormalRender()
  894. {
  895. _normalFileName = "";
  896. _normalTexType = TextureResType::LOCAL;
  897. _normalTextureSize = Size(0, 0);
  898. _normalTextureLoaded = false;
  899. _normalTextureAdaptDirty = false;
  900. _buttonNormalRenderer->resetRender();
  901. }
  902. void Button::resetPressedRender()
  903. {
  904. _clickedFileName = "";
  905. _pressedTexType = TextureResType::LOCAL;
  906. _pressedTextureSize = Size(0, 0);
  907. _pressedTextureLoaded = false;
  908. _pressedTextureAdaptDirty = false;
  909. _buttonClickedRenderer->resetRender();
  910. }
  911. void Button::resetDisabledRender()
  912. {
  913. _disabledFileName = "";
  914. _disabledTexType = TextureResType::LOCAL;
  915. _disabledTextureSize = Size(0, 0);
  916. _disabledTextureLoaded = false;
  917. _disabledTextureAdaptDirty = false;
  918. _buttonDisabledRenderer->resetRender();
  919. }
  920. ResourceData Button::getNormalFile()
  921. {
  922. ResourceData rData;
  923. rData.type = (int)_normalTexType;
  924. rData.file = _normalFileName;
  925. return rData;
  926. }
  927. ResourceData Button::getPressedFile()
  928. {
  929. ResourceData rData;
  930. rData.type = (int)_pressedTexType;
  931. rData.file = _clickedFileName;
  932. return rData;
  933. }
  934. ResourceData Button::getDisabledFile()
  935. {
  936. ResourceData rData;
  937. rData.type = (int)_disabledTexType;
  938. rData.file = _disabledFileName;
  939. return rData;
  940. }
  941. }
  942. NS_CC_END