CCControlStepper.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2012 cocos2d-x.org
  3. * http://www.cocos2d-x.org
  4. *
  5. * Copyright 2012 Yannick Loriot. All rights reserved.
  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 falseT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND falseNINFRINGEMENT. IN false 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. */
  27. #include "CCControlStepper.h"
  28. NS_CC_EXT_BEGIN
  29. #define ControlStepperLabelColorEnabled Color3B(55, 55, 55)
  30. #define ControlStepperLabelColorDisabled Color3B(147, 147, 147)
  31. #define ControlStepperLabelFont "CourierNewPSMT"
  32. #define kAutorepeatDeltaTime 0.15f
  33. #define kAutorepeatIncreaseTimeIncrement 12
  34. ControlStepper::ControlStepper()
  35. : _value(0.0)
  36. , _continuous(false)
  37. , _autorepeat(false)
  38. , _wraps(false)
  39. , _minimumValue(0.0)
  40. , _maximumValue(0.0)
  41. , _stepValue(0.0)
  42. , _touchInsideFlag(false)
  43. , _touchedPart(Part::NONE)
  44. , _autorepeatCount(0)
  45. , _minusSprite(nullptr)
  46. , _plusSprite(nullptr)
  47. , _minusLabel(nullptr)
  48. , _plusLabel(nullptr)
  49. {
  50. }
  51. ControlStepper::~ControlStepper()
  52. {
  53. unscheduleAllCallbacks();
  54. CC_SAFE_RELEASE(_minusSprite);
  55. CC_SAFE_RELEASE(_plusSprite);
  56. CC_SAFE_RELEASE(_minusLabel);
  57. CC_SAFE_RELEASE(_plusLabel);
  58. }
  59. bool ControlStepper::initWithMinusSpriteAndPlusSprite(Sprite *minusSprite, Sprite *plusSprite)
  60. {
  61. if (Control::init())
  62. {
  63. CCASSERT(minusSprite, "Minus sprite must be not nil");
  64. CCASSERT(plusSprite, "Plus sprite must be not nil");
  65. // Set the default values
  66. _autorepeat = true;
  67. _continuous = true;
  68. _minimumValue = 0;
  69. _maximumValue = 100;
  70. _value = 0;
  71. _stepValue = 1;
  72. _wraps = false;
  73. this->setIgnoreAnchorPointForPosition( false );
  74. // Add the minus components
  75. this->setMinusSprite(minusSprite);
  76. _minusSprite->setPosition(minusSprite->getContentSize().width / 2, minusSprite->getContentSize().height / 2);
  77. this->addChild(_minusSprite);
  78. this->setMinusLabel( Label::createWithSystemFont("-", ControlStepperLabelFont, 40));
  79. _minusLabel->setColor(ControlStepperLabelColorDisabled);
  80. _minusLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
  81. _minusLabel->setPosition(_minusSprite->getContentSize().width / 2, _minusSprite->getContentSize().height / 2);
  82. _minusSprite->addChild(_minusLabel);
  83. // Add the plus components
  84. this->setPlusSprite( plusSprite );
  85. _plusSprite->setPosition(minusSprite->getContentSize().width + plusSprite->getContentSize().width / 2,
  86. minusSprite->getContentSize().height / 2);
  87. this->addChild(_plusSprite);
  88. this->setPlusLabel( Label::createWithSystemFont("+", ControlStepperLabelFont, 40 ));
  89. _plusLabel->setColor( ControlStepperLabelColorEnabled );
  90. _plusLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
  91. _plusLabel->setPosition(_plusSprite->getContentSize().width / 2, _plusSprite->getContentSize().height / 2);
  92. _plusSprite->addChild(_plusLabel);
  93. // Defines the content size
  94. Rect maxRect = ControlUtils::RectUnion(_minusSprite->getBoundingBox(), _plusSprite->getBoundingBox());
  95. this->setContentSize( Size(_minusSprite->getContentSize().width + _plusSprite->getContentSize().height, maxRect.size.height) );
  96. return true;
  97. }
  98. return false;
  99. }
  100. ControlStepper* ControlStepper::create(Sprite *minusSprite, Sprite *plusSprite)
  101. {
  102. ControlStepper* pRet = new (std::nothrow) ControlStepper();
  103. if (pRet != nullptr && pRet->initWithMinusSpriteAndPlusSprite(minusSprite, plusSprite))
  104. {
  105. pRet->autorelease();
  106. }
  107. else
  108. {
  109. CC_SAFE_DELETE(pRet);
  110. }
  111. return pRet;
  112. }
  113. //// Properties
  114. void ControlStepper::setWraps(bool wraps)
  115. {
  116. _wraps = wraps;
  117. if (_wraps)
  118. {
  119. _minusLabel->setColor( ControlStepperLabelColorEnabled );
  120. _plusLabel->setColor(ControlStepperLabelColorEnabled );
  121. }
  122. this->setValue( _value );
  123. }
  124. void ControlStepper::setMinimumValue(double minimumValue)
  125. {
  126. if (minimumValue >= _maximumValue)
  127. {
  128. CCASSERT(0, "Must be numerically less than maximumValue.");
  129. }
  130. _minimumValue = minimumValue;
  131. this->setValue( _value );
  132. }
  133. void ControlStepper::setMaximumValue(double maximumValue)
  134. {
  135. if (maximumValue <= _minimumValue)
  136. {
  137. CCASSERT(0, "Must be numerically greater than minimumValue.");
  138. }
  139. _maximumValue = maximumValue;
  140. this->setValue(_value);
  141. }
  142. void ControlStepper::setValue(double value)
  143. {
  144. this->setValueWithSendingEvent(value, true);
  145. }
  146. double ControlStepper::getValue() const
  147. {
  148. return _value;
  149. }
  150. void ControlStepper::setStepValue(double stepValue)
  151. {
  152. if (stepValue <= 0)
  153. {
  154. CCASSERT(0,"Must be numerically greater than 0.");
  155. }
  156. _stepValue = stepValue;
  157. }
  158. bool ControlStepper::isContinuous() const
  159. {
  160. return _continuous;
  161. }
  162. //
  163. //// ControlStepper Public Methods
  164. void ControlStepper::setValueWithSendingEvent(double value, bool send)
  165. {
  166. if (value < _minimumValue)
  167. {
  168. value = _wraps ? _maximumValue : _minimumValue;
  169. } else if (value > _maximumValue)
  170. {
  171. value = _wraps ? _minimumValue : _maximumValue;
  172. }
  173. _value = value;
  174. if (!_wraps)
  175. {
  176. _minusLabel->setColor((value == _minimumValue) ? ControlStepperLabelColorDisabled : ControlStepperLabelColorEnabled);
  177. _plusLabel->setColor((value == _maximumValue) ? ControlStepperLabelColorDisabled : ControlStepperLabelColorEnabled);
  178. }
  179. if (send)
  180. {
  181. this->sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
  182. }
  183. }
  184. void ControlStepper::startAutorepeat()
  185. {
  186. _autorepeatCount = -1;
  187. this->schedule(CC_SCHEDULE_SELECTOR(ControlStepper::update), kAutorepeatDeltaTime, CC_REPEAT_FOREVER, kAutorepeatDeltaTime * 3);
  188. }
  189. /** Stop the autorepeat. */
  190. void ControlStepper::stopAutorepeat()
  191. {
  192. this->unschedule(CC_SCHEDULE_SELECTOR(ControlStepper::update));
  193. }
  194. void ControlStepper::update(float /*dt*/)
  195. {
  196. _autorepeatCount++;
  197. if ((_autorepeatCount < kAutorepeatIncreaseTimeIncrement) && (_autorepeatCount % 3) != 0)
  198. return;
  199. if (_touchedPart == Part::MINUS)
  200. {
  201. this->setValueWithSendingEvent(_value - _stepValue, _continuous);
  202. } else if (_touchedPart == Part::PLUS)
  203. {
  204. this->setValueWithSendingEvent(_value + _stepValue, _continuous);
  205. }
  206. }
  207. //// ControlStepper Private Methods
  208. void ControlStepper::updateLayoutUsingTouchLocation(Vec2 location)
  209. {
  210. if (location.x < _minusSprite->getContentSize().width
  211. && _value > _minimumValue)
  212. {
  213. _touchedPart = Part::MINUS;
  214. _minusSprite->setColor(Color3B::GRAY);
  215. _plusSprite->setColor(Color3B::WHITE);
  216. } else if (location.x >= _minusSprite->getContentSize().width
  217. && _value < _maximumValue)
  218. {
  219. _touchedPart = Part::PLUS;
  220. _minusSprite->setColor(Color3B::WHITE);
  221. _plusSprite->setColor(Color3B::GRAY);
  222. } else
  223. {
  224. _touchedPart = Part::NONE;
  225. _minusSprite->setColor(Color3B::WHITE);
  226. _plusSprite->setColor(Color3B::WHITE);
  227. }
  228. }
  229. bool ControlStepper::onTouchBegan(Touch *pTouch, Event* /*pEvent*/)
  230. {
  231. if (!isTouchInside(pTouch) || !isEnabled() || !isVisible())
  232. {
  233. return false;
  234. }
  235. Vec2 location = this->getTouchLocation(pTouch);
  236. this->updateLayoutUsingTouchLocation(location);
  237. _touchInsideFlag = true;
  238. if (_autorepeat)
  239. {
  240. this->startAutorepeat();
  241. }
  242. return true;
  243. }
  244. void ControlStepper::onTouchMoved(Touch *pTouch, Event* /*pEvent*/)
  245. {
  246. if (this->isTouchInside(pTouch))
  247. {
  248. Vec2 location = this->getTouchLocation(pTouch);
  249. this->updateLayoutUsingTouchLocation(location);
  250. if (!_touchInsideFlag)
  251. {
  252. _touchInsideFlag = true;
  253. if (_autorepeat)
  254. {
  255. this->startAutorepeat();
  256. }
  257. }
  258. }
  259. else
  260. {
  261. _touchInsideFlag = false;
  262. _touchedPart = Part::NONE;
  263. _minusSprite->setColor(Color3B::WHITE);
  264. _plusSprite->setColor(Color3B::WHITE);
  265. if (_autorepeat)
  266. {
  267. this->stopAutorepeat();
  268. }
  269. }
  270. }
  271. void ControlStepper::onTouchEnded(Touch *pTouch, Event* /*pEvent*/)
  272. {
  273. _minusSprite->setColor(Color3B::WHITE);
  274. _plusSprite->setColor(Color3B::WHITE);
  275. if (_autorepeat)
  276. {
  277. this->stopAutorepeat();
  278. }
  279. if (this->isTouchInside(pTouch))
  280. {
  281. Vec2 location = this->getTouchLocation(pTouch);
  282. this->setValue(_value + ((location.x < _minusSprite->getContentSize().width) ? (0.0-_stepValue) : _stepValue));
  283. }
  284. }
  285. NS_CC_EXT_END