UISlider.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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/UISlider.h"
  22. #include "ui/UIScale9Sprite.h"
  23. #include "ui/UIHelper.h"
  24. #include "2d/CCSprite.h"
  25. #include "2d/CCCamera.h"
  26. #include "editor-support/cocostudio/CocosStudioExtension.h"
  27. NS_CC_BEGIN
  28. /* FIXME:
  29. Code could be simplified by using Sprite's setContentSize feature.
  30. Instead of scaling the sprite, set call setContentSize both in scale9 and non-scale9 modes
  31. */
  32. namespace ui {
  33. static const int BASEBAR_RENDERER_Z = (-2);
  34. static const int PROGRESSBAR_RENDERER_Z = (-2);
  35. static const int SLIDBALL_RENDERER_Z = (-1);
  36. IMPLEMENT_CLASS_GUI_INFO(Slider)
  37. Slider::Slider():
  38. _barRenderer(nullptr),
  39. _progressBarRenderer(nullptr),
  40. _barTextureSize(Size::ZERO),
  41. _originalBarRect(Rect::ZERO),
  42. _progressBarTextureSize(Size::ZERO),
  43. _originalProgressBarRect(Rect::ZERO),
  44. _slidBallNormalRenderer(nullptr),
  45. _slidBallPressedRenderer(nullptr),
  46. _slidBallDisabledRenderer(nullptr),
  47. _slidBallRenderer(nullptr),
  48. _barLength(0.0),
  49. _percent(0),
  50. _maxPercent(100),
  51. _scale9Enabled(false),
  52. _prevIgnoreSize(true),
  53. _zoomScale(0.1f),
  54. _sliderBallNormalTextureScaleX(1.0),
  55. _sliderBallNormalTextureScaleY(1.0),
  56. _isSliderBallPressedTextureLoaded(false),
  57. _isSliderBallDisabledTexturedLoaded(false),
  58. _capInsetsBarRenderer(Rect::ZERO),
  59. _capInsetsProgressBarRenderer(Rect::ZERO),
  60. _sliderEventListener(nullptr),
  61. _sliderEventSelector(nullptr),
  62. _eventCallback(nullptr),
  63. _barTexType(TextureResType::LOCAL),
  64. _progressBarTexType(TextureResType::LOCAL),
  65. _ballNTexType(TextureResType::LOCAL),
  66. _ballPTexType(TextureResType::LOCAL),
  67. _ballDTexType(TextureResType::LOCAL),
  68. _barRendererAdaptDirty(true),
  69. _progressBarRendererDirty(true),
  70. _textureFile(""),
  71. _progressBarTextureFile(""),
  72. _slidBallNormalTextureFile(""),
  73. _slidBallPressedTextureFile(""),
  74. _slidBallDisabledTextureFile("")
  75. {
  76. setTouchEnabled(true);
  77. }
  78. Slider::~Slider()
  79. {
  80. _sliderEventListener = nullptr;
  81. _sliderEventSelector = nullptr;
  82. }
  83. Slider* Slider::create()
  84. {
  85. Slider* widget = new (std::nothrow) Slider();
  86. if (widget && widget->init())
  87. {
  88. widget->autorelease();
  89. return widget;
  90. }
  91. CC_SAFE_DELETE(widget);
  92. return nullptr;
  93. }
  94. Slider* Slider::create(const std::string& barTextureName,
  95. const std::string& normalBallTextureName,
  96. TextureResType resType)
  97. {
  98. Slider* widget = new (std::nothrow) Slider();
  99. if (widget && widget->init())
  100. {
  101. widget->loadBarTexture(barTextureName, resType);
  102. widget->loadSlidBallTextureNormal(normalBallTextureName, resType);
  103. widget->autorelease();
  104. return widget;
  105. }
  106. CC_SAFE_DELETE(widget);
  107. return nullptr;
  108. }
  109. bool Slider::init()
  110. {
  111. if (Widget::init())
  112. {
  113. return true;
  114. }
  115. return false;
  116. }
  117. void Slider::initRenderer()
  118. {
  119. _barRenderer = Scale9Sprite::create();
  120. _progressBarRenderer = Scale9Sprite::create();
  121. _barRenderer->setScale9Enabled(false);
  122. _progressBarRenderer->setScale9Enabled(false);
  123. _progressBarRenderer->setAnchorPoint(Vec2(0.0f, 0.5f));
  124. addProtectedChild(_barRenderer, BASEBAR_RENDERER_Z, -1);
  125. addProtectedChild(_progressBarRenderer, PROGRESSBAR_RENDERER_Z, -1);
  126. _slidBallNormalRenderer = Sprite::create();
  127. _slidBallPressedRenderer = Sprite::create();
  128. _slidBallPressedRenderer->setVisible(false);
  129. _slidBallDisabledRenderer = Sprite::create();
  130. _slidBallDisabledRenderer->setVisible(false);
  131. _slidBallRenderer = Node::create();
  132. _slidBallRenderer->addChild(_slidBallNormalRenderer);
  133. _slidBallRenderer->addChild(_slidBallPressedRenderer);
  134. _slidBallRenderer->addChild(_slidBallDisabledRenderer);
  135. _slidBallRenderer->setCascadeColorEnabled(true);
  136. _slidBallRenderer->setCascadeOpacityEnabled(true);
  137. addProtectedChild(_slidBallRenderer, SLIDBALL_RENDERER_Z, -1);
  138. }
  139. void Slider::loadBarTexture(const std::string& fileName, TextureResType texType)
  140. {
  141. _textureFile = fileName;
  142. _barTexType = texType;
  143. if (fileName.empty())
  144. {
  145. _barRenderer->resetRender();
  146. }
  147. else
  148. {
  149. switch (_barTexType)
  150. {
  151. case TextureResType::LOCAL:
  152. _barRenderer->setTexture(fileName);
  153. break;
  154. case TextureResType::PLIST:
  155. _barRenderer->setSpriteFrame(fileName);
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. //FIXME: https://github.com/cocos2d/cocos2d-x/issues/12249
  162. if (!_ignoreSize && _customSize.equals(Size::ZERO)) {
  163. _customSize = _barRenderer->getContentSize();
  164. }
  165. this->setupBarTexture();
  166. }
  167. void Slider::loadBarTexture(SpriteFrame* spriteframe)
  168. {
  169. _barRenderer->setSpriteFrame(spriteframe);
  170. this->setupBarTexture();
  171. }
  172. void Slider::setupBarTexture()
  173. {
  174. this->updateChildrenDisplayedRGBA();
  175. _barRendererAdaptDirty = true;
  176. _progressBarRendererDirty = true;
  177. updateContentSizeWithTextureSize(_barRenderer->getContentSize());
  178. _barTextureSize = _barRenderer->getContentSize();
  179. _originalBarRect = _barRenderer->getTextureRect();
  180. }
  181. void Slider::loadProgressBarTexture(const std::string& fileName, TextureResType texType)
  182. {
  183. _progressBarTextureFile = fileName;
  184. _progressBarTexType = texType;
  185. if (fileName.empty())
  186. {
  187. _progressBarRenderer->resetRender();
  188. }
  189. else
  190. {
  191. switch (_progressBarTexType)
  192. {
  193. case TextureResType::LOCAL:
  194. _progressBarRenderer->setTexture(fileName);
  195. break;
  196. case TextureResType::PLIST:
  197. _progressBarRenderer->setSpriteFrame(fileName);
  198. break;
  199. default:
  200. break;
  201. }
  202. }
  203. this->setupProgressBarTexture();
  204. }
  205. void Slider::loadProgressBarTexture(SpriteFrame* spriteframe)
  206. {
  207. _progressBarRenderer->setSpriteFrame(spriteframe);
  208. this->setupProgressBarTexture();
  209. }
  210. void Slider::setupProgressBarTexture()
  211. {
  212. this->updateChildrenDisplayedRGBA();
  213. _progressBarRenderer->setAnchorPoint(Vec2(0.0f, 0.5f));
  214. _progressBarTextureSize = _progressBarRenderer->getContentSize();
  215. _originalProgressBarRect = _progressBarRenderer->getTextureRect();
  216. _progressBarRendererDirty = true;
  217. }
  218. void Slider::setScale9Enabled(bool able)
  219. {
  220. if (_scale9Enabled == able)
  221. {
  222. return;
  223. }
  224. _scale9Enabled = able;
  225. _barRenderer->setScale9Enabled(_scale9Enabled);
  226. _progressBarRenderer->setScale9Enabled(_scale9Enabled);
  227. if (_scale9Enabled)
  228. {
  229. bool ignoreBefore = _ignoreSize;
  230. ignoreContentAdaptWithSize(false);
  231. _prevIgnoreSize = ignoreBefore;
  232. }
  233. else
  234. {
  235. ignoreContentAdaptWithSize(_prevIgnoreSize);
  236. }
  237. setCapInsetsBarRenderer(_capInsetsBarRenderer);
  238. setCapInsetProgressBarRenderer(_capInsetsProgressBarRenderer);
  239. _barRendererAdaptDirty = true;
  240. _progressBarRendererDirty = true;
  241. }
  242. bool Slider::isScale9Enabled()const
  243. {
  244. return _scale9Enabled;
  245. }
  246. void Slider::ignoreContentAdaptWithSize(bool ignore)
  247. {
  248. if (!_scale9Enabled || (_scale9Enabled && !ignore))
  249. {
  250. Widget::ignoreContentAdaptWithSize(ignore);
  251. _prevIgnoreSize = ignore;
  252. }
  253. }
  254. void Slider::setCapInsets(const Rect &capInsets)
  255. {
  256. setCapInsetsBarRenderer(capInsets);
  257. setCapInsetProgressBarRenderer(capInsets);
  258. }
  259. void Slider::setCapInsetsBarRenderer(const Rect &capInsets)
  260. {
  261. _capInsetsBarRenderer = ui::Helper::restrictCapInsetRect(capInsets, _barRenderer->getContentSize());
  262. if (!_scale9Enabled)
  263. {
  264. return;
  265. }
  266. // textureRect should be restored in order to calculate the scale9 correctly
  267. // https://github.com/cocos2d/cocos2d-x/issues/16928
  268. _barRenderer->setTextureRect(_originalBarRect, _barRenderer->isTextureRectRotated(), _barTextureSize);
  269. _barRenderer->setCapInsets(_capInsetsBarRenderer);
  270. }
  271. const Rect& Slider::getCapInsetsBarRenderer()const
  272. {
  273. return _capInsetsBarRenderer;
  274. }
  275. void Slider::setCapInsetProgressBarRenderer(const Rect &capInsets)
  276. {
  277. _capInsetsProgressBarRenderer = ui::Helper::restrictCapInsetRect(capInsets, _progressBarRenderer->getContentSize());
  278. if (!_scale9Enabled)
  279. {
  280. return;
  281. }
  282. // textureRect should be restored in order to calculate the scale9 correctly
  283. // https://github.com/cocos2d/cocos2d-x/issues/16928
  284. _progressBarRenderer->setTextureRect(_originalProgressBarRect, _progressBarRenderer->isTextureRectRotated(), _progressBarTextureSize);
  285. _progressBarRenderer->setCapInsets(_capInsetsProgressBarRenderer);
  286. }
  287. const Rect& Slider::getCapInsetsProgressBarRenderer()const
  288. {
  289. return _capInsetsProgressBarRenderer;
  290. }
  291. void Slider::loadSlidBallTextures(const std::string& normal,
  292. const std::string& pressed,
  293. const std::string& disabled,
  294. TextureResType texType)
  295. {
  296. loadSlidBallTextureNormal(normal, texType);
  297. loadSlidBallTexturePressed(pressed,texType);
  298. loadSlidBallTextureDisabled(disabled,texType);
  299. }
  300. void Slider::loadSlidBallTextureNormal(const std::string& normal,TextureResType texType)
  301. {
  302. _slidBallNormalTextureFile = normal;
  303. _ballNTexType = texType;
  304. if (normal.empty())
  305. {
  306. _slidBallNormalRenderer->init();
  307. }
  308. else
  309. {
  310. switch (_ballNTexType)
  311. {
  312. case TextureResType::LOCAL:
  313. _slidBallNormalRenderer->setTexture(normal);
  314. break;
  315. case TextureResType::PLIST:
  316. _slidBallNormalRenderer->setSpriteFrame(normal);
  317. break;
  318. default:
  319. break;
  320. }
  321. }
  322. this->updateChildrenDisplayedRGBA();
  323. }
  324. void Slider::loadSlidBallTextureNormal(SpriteFrame* spriteframe)
  325. {
  326. _slidBallNormalRenderer->setSpriteFrame(spriteframe);
  327. this->updateChildrenDisplayedRGBA();
  328. }
  329. void Slider::loadSlidBallTexturePressed(const std::string& pressed,TextureResType texType)
  330. {
  331. _slidBallPressedTextureFile = pressed;
  332. _isSliderBallPressedTextureLoaded = !pressed.empty();
  333. _ballPTexType = texType;
  334. if (pressed.empty())
  335. {
  336. _slidBallPressedRenderer->init();
  337. }
  338. else
  339. {
  340. switch (_ballPTexType)
  341. {
  342. case TextureResType::LOCAL:
  343. _slidBallPressedRenderer->setTexture(pressed);
  344. break;
  345. case TextureResType::PLIST:
  346. _slidBallPressedRenderer->setSpriteFrame(pressed);
  347. break;
  348. default:
  349. break;
  350. }
  351. }
  352. this->updateChildrenDisplayedRGBA();
  353. }
  354. void Slider::loadSlidBallTexturePressed(SpriteFrame* spriteframe)
  355. {
  356. _slidBallPressedRenderer->setSpriteFrame(spriteframe);
  357. this->updateChildrenDisplayedRGBA();
  358. }
  359. void Slider::loadSlidBallTextureDisabled(const std::string& disabled,TextureResType texType)
  360. {
  361. _slidBallDisabledTextureFile = disabled;
  362. _isSliderBallDisabledTexturedLoaded = !disabled.empty();
  363. _ballDTexType = texType;
  364. if (disabled.empty())
  365. {
  366. _slidBallDisabledRenderer->init();
  367. }
  368. else
  369. {
  370. switch (_ballDTexType)
  371. {
  372. case TextureResType::LOCAL:
  373. _slidBallDisabledRenderer->setTexture(disabled);
  374. break;
  375. case TextureResType::PLIST:
  376. _slidBallDisabledRenderer->setSpriteFrame(disabled);
  377. break;
  378. default:
  379. break;
  380. }
  381. }
  382. this->updateChildrenDisplayedRGBA();
  383. }
  384. void Slider::loadSlidBallTextureDisabled(SpriteFrame* spriteframe)
  385. {
  386. _slidBallDisabledRenderer->setSpriteFrame(spriteframe);
  387. this->updateChildrenDisplayedRGBA();
  388. }
  389. void Slider::setMaxPercent(int percent)
  390. {
  391. _maxPercent = percent;
  392. }
  393. int Slider::getMaxPercent()const
  394. {
  395. return _maxPercent;
  396. }
  397. void Slider::setPercent(int percent)
  398. {
  399. if (percent > _maxPercent)
  400. {
  401. percent = _maxPercent;
  402. }
  403. if (percent < 0)
  404. {
  405. percent = 0;
  406. }
  407. _percent = percent;
  408. float res = 1.0 * percent / _maxPercent;
  409. float dis = _barLength * res;
  410. _slidBallRenderer->setPosition(dis, _contentSize.height / 2.0f);
  411. if (_scale9Enabled)
  412. {
  413. _progressBarRenderer->setPreferredSize(Size(dis,_contentSize.height));
  414. }
  415. else
  416. {
  417. Rect rect = _progressBarRenderer->getTextureRect();
  418. rect.size.width = _progressBarTextureSize.width * res;
  419. _progressBarRenderer->setTextureRect(rect, _progressBarRenderer->isTextureRectRotated(), rect.size);
  420. }
  421. }
  422. bool Slider::hitTest(const cocos2d::Vec2 &pt, const Camera *camera, Vec3* /*p*/) const
  423. {
  424. Rect rect;
  425. rect.size = _slidBallNormalRenderer->getContentSize();
  426. auto w2l = _slidBallNormalRenderer->getWorldToNodeTransform();
  427. Rect sliderBarRect;
  428. sliderBarRect.size = this->_barRenderer->getContentSize();
  429. auto barW2l = this->_barRenderer->getWorldToNodeTransform();
  430. return isScreenPointInRect(pt, camera, w2l, rect, nullptr) || isScreenPointInRect(pt, camera, barW2l, sliderBarRect, nullptr);
  431. }
  432. bool Slider::onTouchBegan(Touch *touch, Event *unusedEvent)
  433. {
  434. bool pass = Widget::onTouchBegan(touch, unusedEvent);
  435. if (_hitted)
  436. {
  437. setPercent(getPercentWithBallPos(_touchBeganPosition));
  438. percentChangedEvent(EventType::ON_SLIDEBALL_DOWN);
  439. }
  440. return pass;
  441. }
  442. void Slider::onTouchMoved(Touch *touch, Event* /*unusedEvent*/)
  443. {
  444. _touchMovePosition = touch->getLocation();
  445. setPercent(getPercentWithBallPos(_touchMovePosition));
  446. percentChangedEvent(EventType::ON_PERCENTAGE_CHANGED);
  447. }
  448. void Slider::onTouchEnded(Touch *touch, Event *unusedEvent)
  449. {
  450. Widget::onTouchEnded(touch, unusedEvent);
  451. percentChangedEvent(EventType::ON_PERCENTAGE_CHANGED);
  452. percentChangedEvent(EventType::ON_SLIDEBALL_UP);
  453. }
  454. void Slider::onTouchCancelled(Touch *touch, Event *unusedEvent)
  455. {
  456. Widget::onTouchCancelled(touch, unusedEvent);
  457. percentChangedEvent(EventType::ON_SLIDEBALL_CANCEL);
  458. }
  459. float Slider::getPercentWithBallPos(const Vec2 &pt) const
  460. {
  461. Vec3 p;
  462. Widget::hitTest(pt, _hittedByCamera, &p);
  463. return ((p.x/_barLength) * static_cast<float>(_maxPercent));
  464. }
  465. void Slider::addEventListenerSlider(Ref *target, SEL_SlidPercentChangedEvent selector)
  466. {
  467. _sliderEventListener = target;
  468. _sliderEventSelector = selector;
  469. }
  470. void Slider::addEventListener(const ccSliderCallback& callback)
  471. {
  472. _eventCallback = callback;
  473. }
  474. void Slider::percentChangedEvent(EventType event)
  475. {
  476. this->retain();
  477. if (_sliderEventListener && _sliderEventSelector)
  478. {
  479. (_sliderEventListener->*_sliderEventSelector)(this,SLIDER_PERCENTCHANGED);
  480. }
  481. if (_eventCallback)
  482. {
  483. _eventCallback(this,event);
  484. }
  485. if (_ccEventCallback)
  486. {
  487. _ccEventCallback(this, static_cast<int>(EventType::ON_PERCENTAGE_CHANGED));
  488. }
  489. this->release();
  490. }
  491. int Slider::getPercent()const
  492. {
  493. return _percent;
  494. }
  495. void Slider::onSizeChanged()
  496. {
  497. Widget::onSizeChanged();
  498. _barRendererAdaptDirty = true;
  499. _progressBarRendererDirty = true;
  500. }
  501. void Slider::adaptRenderers()
  502. {
  503. if (_barRendererAdaptDirty)
  504. {
  505. barRendererScaleChangedWithSize();
  506. _barRendererAdaptDirty = false;
  507. }
  508. if (_progressBarRendererDirty)
  509. {
  510. progressBarRendererScaleChangedWithSize();
  511. _progressBarRendererDirty = false;
  512. }
  513. }
  514. Size Slider::getVirtualRendererSize() const
  515. {
  516. return _barRenderer->getContentSize();
  517. }
  518. Node* Slider::getVirtualRenderer()
  519. {
  520. return _barRenderer;
  521. }
  522. void Slider::barRendererScaleChangedWithSize()
  523. {
  524. if (_unifySize)
  525. {
  526. _barLength = _contentSize.width;
  527. _barRenderer->setPreferredSize(_contentSize);
  528. }
  529. else if (_ignoreSize)
  530. {
  531. _barRenderer->setScale(1.0f);
  532. _barLength = _contentSize.width;
  533. }
  534. else
  535. {
  536. _barLength = _contentSize.width;
  537. if (_scale9Enabled)
  538. {
  539. _barRenderer->setPreferredSize(_contentSize);
  540. _barRenderer->setScale(1.0f);
  541. }
  542. else
  543. {
  544. Size btextureSize = _barTextureSize;
  545. if (btextureSize.width <= 0.0f || btextureSize.height <= 0.0f)
  546. {
  547. _barRenderer->setScale(1.0f);
  548. }
  549. else
  550. {
  551. float bscaleX = _contentSize.width / btextureSize.width;
  552. float bscaleY = _contentSize.height / btextureSize.height;
  553. _barRenderer->setScaleX(bscaleX);
  554. _barRenderer->setScaleY(bscaleY);
  555. }
  556. }
  557. }
  558. _barRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
  559. setPercent(_percent);
  560. }
  561. void Slider::progressBarRendererScaleChangedWithSize()
  562. {
  563. if (_unifySize)
  564. {
  565. _progressBarRenderer->setPreferredSize(_contentSize);
  566. }
  567. else if (_ignoreSize)
  568. {
  569. if (!_scale9Enabled)
  570. {
  571. Size ptextureSize = _progressBarTextureSize;
  572. float pscaleX = _contentSize.width / ptextureSize.width;
  573. float pscaleY = _contentSize.height / ptextureSize.height;
  574. _progressBarRenderer->setScaleX(pscaleX);
  575. _progressBarRenderer->setScaleY(pscaleY);
  576. }
  577. }
  578. else
  579. {
  580. if (_scale9Enabled)
  581. {
  582. _progressBarRenderer->setPreferredSize(_contentSize);
  583. _progressBarRenderer->setScale(1.0);
  584. }
  585. else
  586. {
  587. Size ptextureSize = _progressBarTextureSize;
  588. if (ptextureSize.width <= 0.0f || ptextureSize.height <= 0.0f)
  589. {
  590. _progressBarRenderer->setScale(1.0f);
  591. return;
  592. }
  593. float pscaleX = _contentSize.width / ptextureSize.width;
  594. float pscaleY = _contentSize.height / ptextureSize.height;
  595. _progressBarRenderer->setScaleX(pscaleX);
  596. _progressBarRenderer->setScaleY(pscaleY);
  597. }
  598. }
  599. _progressBarRenderer->setPosition(0.0f, _contentSize.height / 2.0f);
  600. setPercent(_percent);
  601. }
  602. void Slider::onPressStateChangedToNormal()
  603. {
  604. _slidBallNormalRenderer->setVisible(true);
  605. _slidBallPressedRenderer->setVisible(false);
  606. _slidBallDisabledRenderer->setVisible(false);
  607. _slidBallNormalRenderer->setGLProgramState(this->getNormalGLProgramState(_slidBallNormalRenderer->getTexture()));
  608. _slidBallNormalRenderer->setScale(_sliderBallNormalTextureScaleX, _sliderBallNormalTextureScaleY);
  609. }
  610. void Slider::onPressStateChangedToPressed()
  611. {
  612. _slidBallNormalRenderer->setGLProgramState(this->getNormalGLProgramState(_slidBallNormalRenderer->getTexture()));
  613. if (!_isSliderBallPressedTextureLoaded)
  614. {
  615. _slidBallNormalRenderer->setScale(_sliderBallNormalTextureScaleX + _zoomScale,
  616. _sliderBallNormalTextureScaleY + _zoomScale);
  617. }
  618. else
  619. {
  620. _slidBallNormalRenderer->setVisible(false);
  621. _slidBallPressedRenderer->setVisible(true);
  622. _slidBallDisabledRenderer->setVisible(false);
  623. }
  624. }
  625. void Slider::onPressStateChangedToDisabled()
  626. {
  627. if (!_isSliderBallDisabledTexturedLoaded)
  628. {
  629. _slidBallNormalRenderer->setGLProgramState(this->getGrayGLProgramState(_slidBallNormalRenderer->getTexture()));
  630. _slidBallNormalRenderer->setVisible(true);
  631. }
  632. else
  633. {
  634. _slidBallNormalRenderer->setVisible(false);
  635. _slidBallDisabledRenderer->setVisible(true);
  636. }
  637. _slidBallNormalRenderer->setScale(_sliderBallNormalTextureScaleX, _sliderBallNormalTextureScaleY);
  638. _slidBallPressedRenderer->setVisible(false);
  639. }
  640. void Slider::setZoomScale(float scale)
  641. {
  642. _zoomScale = scale;
  643. }
  644. float Slider::getZoomScale()const
  645. {
  646. return _zoomScale;
  647. }
  648. std::string Slider::getDescription() const
  649. {
  650. return "Slider";
  651. }
  652. Widget* Slider::createCloneInstance()
  653. {
  654. return Slider::create();
  655. }
  656. void Slider::copySpecialProperties(Widget *widget)
  657. {
  658. Slider* slider = dynamic_cast<Slider*>(widget);
  659. if (slider)
  660. {
  661. _prevIgnoreSize = slider->_prevIgnoreSize;
  662. setScale9Enabled(slider->_scale9Enabled);
  663. // clone the inner sprite: https://github.com/cocos2d/cocos2d-x/issues/16928
  664. slider->_barRenderer->copyTo(_barRenderer);
  665. setupBarTexture();
  666. slider->_progressBarRenderer->copyTo(_progressBarRenderer);
  667. setupProgressBarTexture();
  668. loadSlidBallTextureNormal(slider->_slidBallNormalRenderer->getSpriteFrame());
  669. loadSlidBallTexturePressed(slider->_slidBallPressedRenderer->getSpriteFrame());
  670. loadSlidBallTextureDisabled(slider->_slidBallDisabledRenderer->getSpriteFrame());
  671. setPercent(slider->getPercent());
  672. setMaxPercent(slider->getMaxPercent());
  673. _isSliderBallPressedTextureLoaded = slider->_isSliderBallPressedTextureLoaded;
  674. _isSliderBallDisabledTexturedLoaded = slider->_isSliderBallDisabledTexturedLoaded;
  675. _sliderEventListener = slider->_sliderEventListener;
  676. _sliderEventSelector = slider->_sliderEventSelector;
  677. _eventCallback = slider->_eventCallback;
  678. _ccEventCallback = slider->_ccEventCallback;
  679. }
  680. }
  681. Sprite* Slider::getSlidBallNormalRenderer() const {
  682. return _slidBallNormalRenderer;
  683. }
  684. Sprite* Slider::getSlidBallPressedRenderer() const {
  685. return _slidBallPressedRenderer;
  686. }
  687. Sprite* Slider::getSlidBallDisabledRenderer() const {
  688. return _slidBallDisabledRenderer;
  689. }
  690. Node* Slider::getSlidBallRenderer() const {
  691. return _slidBallRenderer;
  692. }
  693. ResourceData Slider::getBackFile()
  694. {
  695. ResourceData rData;
  696. rData.type = (int)_barTexType;
  697. rData.file = _textureFile;
  698. return rData;
  699. }
  700. ResourceData Slider::getProgressBarFile()
  701. {
  702. ResourceData rData;
  703. rData.type = (int)_progressBarTexType;
  704. rData.file = _progressBarTextureFile;
  705. return rData;
  706. }
  707. ResourceData Slider::getBallNormalFile()
  708. {
  709. ResourceData rData;
  710. rData.type = (int)_ballNTexType;
  711. rData.file = _slidBallNormalTextureFile;
  712. return rData;
  713. }
  714. ResourceData Slider::getBallPressedFile()
  715. {
  716. ResourceData rData;
  717. rData.type = (int)_ballPTexType;
  718. rData.file = _slidBallPressedTextureFile;
  719. return rData;
  720. }
  721. ResourceData Slider::getBallDisabledFile()
  722. {
  723. ResourceData rData;
  724. rData.type = (int)_ballDTexType;
  725. rData.file = _slidBallDisabledTextureFile;
  726. return rData;
  727. }
  728. }
  729. NS_CC_END