CCControlHuePicker.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2012 cocos2d-x.org
  3. * http://www.cocos2d-x.org
  4. *
  5. * Copyright 2012 Stewart Hamilton-Arrandale.
  6. * http://creativewax.co.uk
  7. *
  8. * Modified by Yannick Loriot.
  9. * http://yannickloriot.com
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * Converted to c++ / cocos2d-x by Angus C
  30. */
  31. #include "CCControlHuePicker.h"
  32. NS_CC_EXT_BEGIN
  33. ControlHuePicker::ControlHuePicker()
  34. : _hue(0.0f)
  35. , _huePercentage(0.0f)
  36. , _background(nullptr)
  37. , _slider(nullptr)
  38. {
  39. }
  40. ControlHuePicker::~ControlHuePicker()
  41. {
  42. removeAllChildrenWithCleanup(true);
  43. CC_SAFE_RELEASE(_background);
  44. CC_SAFE_RELEASE(_slider);
  45. }
  46. ControlHuePicker* ControlHuePicker::create(Node* target, Vec2 pos)
  47. {
  48. ControlHuePicker *pRet = new (std::nothrow) ControlHuePicker();
  49. pRet->initWithTargetAndPos(target, pos);
  50. pRet->autorelease();
  51. return pRet;
  52. }
  53. bool ControlHuePicker::initWithTargetAndPos(Node* target, Vec2 pos)
  54. {
  55. if (Control::init())
  56. {
  57. // Add background and slider sprites
  58. this->setBackground(ControlUtils::addSpriteToTargetWithPosAndAnchor("huePickerBackground.png", target, pos, Vec2(0.0f, 0.0f)));
  59. this->setSlider(ControlUtils::addSpriteToTargetWithPosAndAnchor("colourPicker.png", target, pos, Vec2(0.5f, 0.5f)));
  60. _slider->setPosition(pos.x, pos.y + _background->getBoundingBox().size.height * 0.5f);
  61. _startPos=pos;
  62. // Sets the default value
  63. _hue=0.0f;
  64. _huePercentage=0.0f;
  65. return true;
  66. }
  67. else
  68. {
  69. return false;
  70. }
  71. }
  72. void ControlHuePicker::setHue(float hueValue)
  73. {
  74. _hue=hueValue;
  75. // Set the position of the slider to the correct hue
  76. // We need to divide it by 360 as its taken as an angle in degrees
  77. float huePercentage = hueValue / 360.0f;
  78. setHuePercentage(huePercentage);
  79. }
  80. void ControlHuePicker::setHuePercentage(float hueValueInPercent)
  81. {
  82. _huePercentage=hueValueInPercent;
  83. _hue=_huePercentage*360.0f;
  84. // Clamp the position of the icon within the circle
  85. Rect backgroundBox=_background->getBoundingBox();
  86. // Get the center point of the background image
  87. float centerX = _startPos.x + backgroundBox.size.width * 0.5f;
  88. float centerY = _startPos.y + backgroundBox.size.height * 0.5f;
  89. // Work out the limit to the distance of the picker when moving around the hue bar
  90. float limit = backgroundBox.size.width * 0.5f - 15.0f;
  91. // Update angle
  92. float angleDeg = _huePercentage * 360.0f - 180.0f;
  93. float angle = CC_DEGREES_TO_RADIANS(angleDeg);
  94. // Set new position of the slider
  95. float x = centerX + limit * cosf(angle);
  96. float y = centerY + limit * sinf(angle);
  97. _slider->setPosition(x, y);
  98. }
  99. void ControlHuePicker::setEnabled(bool enabled)
  100. {
  101. Control::setEnabled(enabled);
  102. if (_slider != nullptr)
  103. {
  104. _slider->setOpacity(enabled ? 255 : 128);
  105. }
  106. }
  107. void ControlHuePicker::updateSliderPosition(Vec2 location)
  108. {
  109. // Clamp the position of the icon within the circle
  110. Rect backgroundBox=_background->getBoundingBox();
  111. // Get the center point of the background image
  112. float centerX = _startPos.x + backgroundBox.size.width * 0.5f;
  113. float centerY = _startPos.y + backgroundBox.size.height * 0.5f;
  114. // Work out the distance difference between the location and center
  115. float dx = location.x - centerX;
  116. float dy = location.y - centerY;
  117. // Update angle by using the direction of the location
  118. float angle = atan2f(dy, dx);
  119. float angleDeg = CC_RADIANS_TO_DEGREES(angle) + 180.0f;
  120. // use the position / slider width to determine the percentage the dragger is at
  121. setHue(angleDeg);
  122. // send Control callback
  123. sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
  124. }
  125. bool ControlHuePicker::checkSliderPosition(Vec2 location)
  126. {
  127. // compute the distance between the current location and the center
  128. double distance = sqrt(pow (location.x + 10, 2) + pow(location.y, 2));
  129. // check that the touch location is within the circle
  130. if (80 > distance && distance > 59)
  131. {
  132. updateSliderPosition(location);
  133. return true;
  134. }
  135. return false;
  136. }
  137. bool ControlHuePicker::onTouchBegan(Touch* touch, Event* /*event*/)
  138. {
  139. if (!isEnabled() || !isVisible())
  140. {
  141. return false;
  142. }
  143. // Get the touch location
  144. Vec2 touchLocation=getTouchLocation(touch);
  145. // Check the touch position on the slider
  146. return checkSliderPosition(touchLocation);
  147. }
  148. void ControlHuePicker::onTouchMoved(Touch* touch, Event* /*event*/)
  149. {
  150. // Get the touch location
  151. Vec2 touchLocation=getTouchLocation(touch);
  152. //small modification: this allows changing of the colour, even if the touch leaves the bounding area
  153. // updateSliderPosition(touchLocation);
  154. // sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
  155. // Check the touch position on the slider
  156. checkSliderPosition(touchLocation);
  157. }
  158. NS_CC_EXT_END