CCControlSlider.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (c) 2012 cocos2d-x.org
  3. * http://www.cocos2d-x.org
  4. *
  5. * Copyright 2011 Yannick Loriot.
  6. * http://yannickloriot.com
  7. *
  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. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. * Converted to c++ / cocos2d-x by Angus C
  27. *
  28. */
  29. #include "CCControlSlider.h"
  30. #include "base/CCTouch.h"
  31. #include "base/CCDirector.h"
  32. NS_CC_EXT_BEGIN
  33. ControlSlider::ControlSlider()
  34. : _value(0.0f)
  35. , _minimumValue(0.0f)
  36. , _maximumValue(0.0f)
  37. , _minimumAllowedValue(0.0f)
  38. , _maximumAllowedValue(0.0f)
  39. , _thumbSprite(nullptr)
  40. , _selectedThumbSprite(nullptr)
  41. , _progressSprite(nullptr)
  42. , _backgroundSprite(nullptr)
  43. {
  44. }
  45. ControlSlider::~ControlSlider()
  46. {
  47. CC_SAFE_RELEASE(_thumbSprite);
  48. CC_SAFE_RELEASE(_selectedThumbSprite);
  49. CC_SAFE_RELEASE(_progressSprite);
  50. CC_SAFE_RELEASE(_backgroundSprite);
  51. }
  52. ControlSlider* ControlSlider::create(const char* bgFile, const char* progressFile, const char* thumbFile)
  53. {
  54. // Prepare background for slider
  55. Sprite *backgroundSprite = Sprite::create(bgFile);
  56. // Prepare progress for slider
  57. Sprite *progressSprite = Sprite::create(progressFile);
  58. // Prepare thumb (menuItem) for slider
  59. Sprite *thumbSprite = Sprite::create(thumbFile);
  60. return ControlSlider::create(backgroundSprite, progressSprite, thumbSprite);
  61. }
  62. ControlSlider* ControlSlider::create(const char* bgFile, const char* progressFile, const char* thumbFile,
  63. const char* selectedThumbSpriteFile)
  64. {
  65. // Prepare background for slider
  66. Sprite *backgroundSprite = Sprite::create(bgFile);
  67. // Prepare progress for slider
  68. Sprite *progressSprite = Sprite::create(progressFile);
  69. // Prepare thumb (menuItem) for slider
  70. Sprite *thumbSprite = Sprite::create(thumbFile);
  71. // Prepare selected thumb (menuItem) for slider
  72. Sprite *selectedThumbSprite = Sprite::create(selectedThumbSpriteFile);
  73. return ControlSlider::create(backgroundSprite, progressSprite, thumbSprite, selectedThumbSprite);
  74. }
  75. ControlSlider* ControlSlider::create(Sprite * backgroundSprite, Sprite* pogressSprite, Sprite* thumbSprite)
  76. {
  77. ControlSlider *pRet = new (std::nothrow) ControlSlider();
  78. pRet->initWithSprites(backgroundSprite, pogressSprite, thumbSprite);
  79. pRet->autorelease();
  80. return pRet;
  81. }
  82. ControlSlider* ControlSlider::create(Sprite * backgroundSprite, Sprite* pogressSprite, Sprite* thumbSprite,
  83. Sprite* selectedThumbSprite)
  84. {
  85. ControlSlider *pRet = new (std::nothrow) ControlSlider();
  86. pRet->initWithSprites(backgroundSprite, pogressSprite, thumbSprite, selectedThumbSprite);
  87. pRet->autorelease();
  88. return pRet;
  89. }
  90. bool ControlSlider::initWithSprites(Sprite * backgroundSprite, Sprite* progressSprite, Sprite* thumbSprite)
  91. {
  92. Sprite* selectedThumbSprite = Sprite::createWithTexture(thumbSprite->getTexture(),
  93. thumbSprite->getTextureRect());
  94. selectedThumbSprite->setColor(Color3B::GRAY);
  95. return this->initWithSprites(backgroundSprite, progressSprite, thumbSprite, selectedThumbSprite);
  96. }
  97. bool ControlSlider::initWithSprites(Sprite * backgroundSprite, Sprite* progressSprite, Sprite* thumbSprite,
  98. Sprite* selectedThumbSprite)
  99. {
  100. if (Control::init())
  101. {
  102. CCASSERT(backgroundSprite, "Background sprite must be not nil");
  103. CCASSERT(progressSprite, "Progress sprite must be not nil");
  104. CCASSERT(thumbSprite, "Thumb sprite must be not nil");
  105. CCASSERT(selectedThumbSprite, "Thumb sprite must be not nil");
  106. setIgnoreAnchorPointForPosition(false);
  107. this->setBackgroundSprite(backgroundSprite);
  108. this->setProgressSprite(progressSprite);
  109. this->setThumbSprite(thumbSprite);
  110. this->setSelectedThumbSprite(selectedThumbSprite);
  111. // Defines the content size
  112. Rect maxRect = ControlUtils::RectUnion(backgroundSprite->getBoundingBox(), thumbSprite->getBoundingBox());
  113. setContentSize(Size(maxRect.size.width, maxRect.size.height));
  114. // Add the slider background
  115. _backgroundSprite->setAnchorPoint(Vec2(0.5f, 0.5f));
  116. _backgroundSprite->setPosition(this->getContentSize().width / 2, this->getContentSize().height / 2);
  117. addChild(_backgroundSprite);
  118. // Add the progress bar
  119. _progressSprite->setAnchorPoint(Vec2(0.0f, 0.5f));
  120. _progressSprite->setPosition(0.0f, this->getContentSize().height / 2);
  121. addChild(_progressSprite);
  122. // Add the slider thumb
  123. _thumbSprite->setPosition(0.0f, this->getContentSize().height / 2);
  124. addChild(_thumbSprite);
  125. _selectedThumbSprite->setPosition(0.0f, this->getContentSize().height / 2);
  126. _selectedThumbSprite->setVisible(false);
  127. addChild(_selectedThumbSprite);
  128. // Init default values
  129. _minimumValue = 0.0f;
  130. _maximumValue = 1.0f;
  131. setValue(_minimumValue);
  132. return true;
  133. }
  134. else
  135. {
  136. return false;
  137. }
  138. }
  139. void ControlSlider::setEnabled(bool enabled)
  140. {
  141. Control::setEnabled(enabled);
  142. if (_thumbSprite != nullptr)
  143. {
  144. _thumbSprite->setOpacity((enabled) ? 255 : 128);
  145. }
  146. }
  147. void ControlSlider::setValue(float value)
  148. {
  149. // set new value with sentinel
  150. if (value < _minimumValue)
  151. {
  152. value = _minimumValue;
  153. }
  154. if (value > _maximumValue)
  155. {
  156. value = _maximumValue;
  157. }
  158. _value = value;
  159. this->needsLayout();
  160. this->sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
  161. }
  162. void ControlSlider::setMinimumValue(float minimumValue)
  163. {
  164. _minimumValue=minimumValue;
  165. _minimumAllowedValue = minimumValue;
  166. if (_minimumValue >= _maximumValue)
  167. {
  168. _maximumValue = _minimumValue + 1.0f;
  169. }
  170. setValue(_value);
  171. }
  172. void ControlSlider::setMaximumValue(float maximumValue)
  173. {
  174. _maximumValue=maximumValue;
  175. _maximumAllowedValue = maximumValue;
  176. if (_maximumValue <= _minimumValue)
  177. {
  178. _minimumValue = _maximumValue - 1.0f;
  179. }
  180. setValue(_value);
  181. }
  182. bool ControlSlider::isTouchInside(Touch * touch)
  183. {
  184. Vec2 touchLocation = touch->getLocation();
  185. touchLocation = this->getParent()->convertToNodeSpace(touchLocation);
  186. Rect rect = this->getBoundingBox();
  187. rect.size.width += _thumbSprite->getContentSize().width;
  188. rect.origin.x -= _thumbSprite->getContentSize().width / 2;
  189. return rect.containsPoint(touchLocation);
  190. }
  191. Vec2 ControlSlider::locationFromTouch(Touch* touch)
  192. {
  193. Vec2 touchLocation = touch->getLocation(); // Get the touch position
  194. touchLocation = this->convertToNodeSpace(touchLocation); // Convert to the node space of this class
  195. if (touchLocation.x < 0)
  196. {
  197. touchLocation.x = 0;
  198. } else if (touchLocation.x > _backgroundSprite->getContentSize().width)
  199. {
  200. touchLocation.x = _backgroundSprite->getContentSize().width;
  201. }
  202. return touchLocation;
  203. }
  204. bool ControlSlider::onTouchBegan(Touch* touch, Event* /*pEvent*/)
  205. {
  206. if (!isTouchInside(touch) || !isEnabled() || !isVisible())
  207. {
  208. return false;
  209. }
  210. Vec2 location = locationFromTouch(touch);
  211. sliderBegan(location);
  212. return true;
  213. }
  214. void ControlSlider::onTouchMoved(Touch *pTouch, Event* /*pEvent*/)
  215. {
  216. Vec2 location = locationFromTouch(pTouch);
  217. sliderMoved(location);
  218. }
  219. void ControlSlider::onTouchEnded(Touch* /*pTouch*/, Event* /*pEvent*/)
  220. {
  221. sliderEnded(Vec2::ZERO);
  222. }
  223. void ControlSlider::needsLayout()
  224. {
  225. if (nullptr == _thumbSprite || nullptr == _selectedThumbSprite || nullptr == _backgroundSprite
  226. || nullptr == _progressSprite)
  227. {
  228. return;
  229. }
  230. // Update thumb position for new value
  231. float percent = (_value - _minimumValue) / (_maximumValue - _minimumValue);
  232. Vec2 pos = _thumbSprite->getPosition();
  233. pos.x = percent * _backgroundSprite->getContentSize().width;
  234. _thumbSprite->setPosition(pos);
  235. _selectedThumbSprite->setPosition(pos);
  236. // Stretches content proportional to newLevel
  237. Rect textureRect = _progressSprite->getTextureRect();
  238. textureRect = Rect(textureRect.origin.x, textureRect.origin.y, pos.x, textureRect.size.height);
  239. _progressSprite->setTextureRect(textureRect, _progressSprite->isTextureRectRotated(), textureRect.size);
  240. }
  241. void ControlSlider::sliderBegan(Vec2 location)
  242. {
  243. this->setSelected(true);
  244. _thumbSprite->setVisible(false);
  245. _selectedThumbSprite->setVisible(true);
  246. setValue(valueForLocation(location));
  247. }
  248. void ControlSlider::sliderMoved(Vec2 location)
  249. {
  250. setValue(valueForLocation(location));
  251. }
  252. void ControlSlider::sliderEnded(Vec2 /*location*/)
  253. {
  254. if (this->isSelected())
  255. {
  256. setValue(valueForLocation(_thumbSprite->getPosition()));
  257. }
  258. _thumbSprite->setVisible(true);
  259. _selectedThumbSprite->setVisible(false);
  260. this->setSelected(false);
  261. }
  262. float ControlSlider::valueForLocation(Vec2 location)
  263. {
  264. float percent = location.x/ _backgroundSprite->getContentSize().width;
  265. return MAX(MIN(_minimumValue + percent * (_maximumValue - _minimumValue), _maximumAllowedValue), _minimumAllowedValue);
  266. }
  267. NS_CC_EXT_END