CCControlPotentiometer.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 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. */
  27. #include "CCControlPotentiometer.h"
  28. NS_CC_EXT_BEGIN
  29. ControlPotentiometer::ControlPotentiometer()
  30. : _value(0.0f)
  31. , _minimumValue(0.0f)
  32. , _maximumValue(0.0f)
  33. , _thumbSprite(nullptr)
  34. , _progressTimer(nullptr)
  35. {
  36. }
  37. ControlPotentiometer::~ControlPotentiometer()
  38. {
  39. CC_SAFE_RELEASE(_thumbSprite);
  40. CC_SAFE_RELEASE(_progressTimer);
  41. }
  42. ControlPotentiometer* ControlPotentiometer::create(const char* backgroundFile, const char* progressFile, const char* thumbFile)
  43. {
  44. ControlPotentiometer* pRet = new (std::nothrow) ControlPotentiometer();
  45. if (pRet != nullptr)
  46. {
  47. // Prepare track for potentiometer
  48. Sprite *backgroundSprite = Sprite::create(backgroundFile);
  49. // Prepare thumb for potentiometer
  50. Sprite *thumbSprite = Sprite::create(thumbFile);
  51. // Prepare progress for potentiometer
  52. ProgressTimer *progressTimer = ProgressTimer::create(Sprite::create(progressFile));
  53. //progressTimer.type = ProgressTimer::RADIALCW;
  54. if (pRet->initWithTrackSprite_ProgressTimer_ThumbSprite(backgroundSprite, progressTimer, thumbSprite))
  55. {
  56. pRet->autorelease();
  57. }
  58. else
  59. {
  60. CC_SAFE_DELETE(pRet);
  61. }
  62. }
  63. return pRet;
  64. }
  65. bool ControlPotentiometer::initWithTrackSprite_ProgressTimer_ThumbSprite(Sprite* trackSprite, ProgressTimer* progressTimer, Sprite* thumbSprite)
  66. {
  67. if (Control::init())
  68. {
  69. setProgressTimer(progressTimer);
  70. setThumbSprite(thumbSprite);
  71. thumbSprite->setPosition(progressTimer->getPosition());
  72. addChild(thumbSprite, 2);
  73. addChild(progressTimer, 1);
  74. addChild(trackSprite);
  75. setContentSize(trackSprite->getContentSize());
  76. // Init default values
  77. _minimumValue = 0.0f;
  78. _maximumValue = 1.0f;
  79. setValue(_minimumValue);
  80. return true;
  81. }
  82. return false;
  83. }
  84. void ControlPotentiometer::setEnabled(bool enabled)
  85. {
  86. Control::setEnabled(enabled);
  87. if (_thumbSprite != nullptr)
  88. {
  89. _thumbSprite->setOpacity((enabled) ? 255 : 128);
  90. }
  91. }
  92. void ControlPotentiometer::setValue(float value)
  93. {
  94. // set new value with sentinel
  95. if (value < _minimumValue)
  96. {
  97. value = _minimumValue;
  98. }
  99. if (value > _maximumValue)
  100. {
  101. value = _maximumValue;
  102. }
  103. _value = value;
  104. // Update thumb and progress position for new value
  105. float percent = (value - _minimumValue) / (_maximumValue - _minimumValue);
  106. _progressTimer->setPercentage(percent * 100.0f);
  107. _thumbSprite->setRotation(percent * 360.0f);
  108. sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
  109. }
  110. float ControlPotentiometer::getValue()
  111. {
  112. return _value;
  113. }
  114. void ControlPotentiometer::setMinimumValue(float minimumValue)
  115. {
  116. _minimumValue = minimumValue;
  117. if (_minimumValue >= _maximumValue)
  118. {
  119. _maximumValue = _minimumValue + 1.0f;
  120. }
  121. setValue(_maximumValue);
  122. }
  123. float ControlPotentiometer::getMinimumValue()
  124. {
  125. return _minimumValue;
  126. }
  127. void ControlPotentiometer::setMaximumValue(float maximumValue)
  128. {
  129. _maximumValue = maximumValue;
  130. if (_maximumValue <= _minimumValue)
  131. {
  132. _minimumValue = _maximumValue - 1.0f;
  133. }
  134. setValue(_minimumValue);
  135. }
  136. float ControlPotentiometer::getMaximumValue()
  137. {
  138. return _maximumValue;
  139. }
  140. bool ControlPotentiometer::isTouchInside(Touch * touch)
  141. {
  142. Vec2 touchLocation = this->getTouchLocation(touch);
  143. float distance = this->distanceBetweenPointAndPoint(_progressTimer->getPosition(), touchLocation);
  144. return distance < MIN(getContentSize().width / 2, getContentSize().height / 2);
  145. }
  146. bool ControlPotentiometer::onTouchBegan(Touch *pTouch, Event* /*pEvent*/)
  147. {
  148. if (!this->isTouchInside(pTouch) || !this->isEnabled() || !isVisible())
  149. {
  150. return false;
  151. }
  152. _previousLocation = this->getTouchLocation(pTouch);
  153. this->potentiometerBegan(_previousLocation);
  154. return true;
  155. }
  156. void ControlPotentiometer::onTouchMoved(Touch *pTouch, Event* /*pEvent*/)
  157. {
  158. Vec2 location = this->getTouchLocation(pTouch);
  159. this->potentiometerMoved(location);
  160. }
  161. void ControlPotentiometer::onTouchEnded(Touch* /*pTouch*/, Event* /*pEvent*/)
  162. {
  163. this->potentiometerEnded(Vec2::ZERO);
  164. }
  165. float ControlPotentiometer::distanceBetweenPointAndPoint(Vec2 point1, Vec2 point2)
  166. {
  167. float dx = point1.x - point2.x;
  168. float dy = point1.y - point2.y;
  169. return sqrt(dx*dx + dy*dy);
  170. }
  171. float ControlPotentiometer::angleInDegreesBetweenLineFromPoint_toPoint_toLineFromPoint_toPoint(
  172. Vec2 beginLineA,
  173. Vec2 endLineA,
  174. Vec2 beginLineB,
  175. Vec2 endLineB)
  176. {
  177. float a = endLineA.x - beginLineA.x;
  178. float b = endLineA.y - beginLineA.y;
  179. float c = endLineB.x - beginLineB.x;
  180. float d = endLineB.y - beginLineB.y;
  181. float atanA = atan2(a, b);
  182. float atanB = atan2(c, d);
  183. // convert radiants to degrees
  184. return (atanA - atanB) * 180 / M_PI;
  185. }
  186. void ControlPotentiometer::potentiometerBegan(Vec2 /*location*/)
  187. {
  188. setSelected(true);
  189. getThumbSprite()->setColor(Color3B::GRAY);
  190. }
  191. void ControlPotentiometer::potentiometerMoved(Vec2 location)
  192. {
  193. float angle = this->angleInDegreesBetweenLineFromPoint_toPoint_toLineFromPoint_toPoint(
  194. _progressTimer->getPosition(),
  195. location,
  196. _progressTimer->getPosition(),
  197. _previousLocation);
  198. // fix value, if the 12 o'clock position is between location and previousLocation
  199. if (angle > 180)
  200. {
  201. angle -= 360;
  202. }
  203. else if (angle < -180)
  204. {
  205. angle += 360;
  206. }
  207. setValue(_value + angle / 360.0f * (_maximumValue - _minimumValue));
  208. _previousLocation = location;
  209. }
  210. void ControlPotentiometer::potentiometerEnded(Vec2 /*location*/)
  211. {
  212. getThumbSprite()->setColor(Color3B::WHITE);
  213. setSelected(false);
  214. }
  215. NS_CC_EXT_END