CCControlSaturationBrightnessPicker.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "CCControlSaturationBrightnessPicker.h"
  32. NS_CC_EXT_BEGIN
  33. ControlSaturationBrightnessPicker::ControlSaturationBrightnessPicker()
  34. : _saturation(0.0f)
  35. , _brightness(0.0f)
  36. , _background(nullptr)
  37. , _overlay(nullptr)
  38. , _shadow(nullptr)
  39. , _slider(nullptr)
  40. , boxPos(0)
  41. , boxSize(0)
  42. {
  43. }
  44. ControlSaturationBrightnessPicker::~ControlSaturationBrightnessPicker()
  45. {
  46. removeAllChildrenWithCleanup(true);
  47. _background = nullptr;
  48. _overlay = nullptr;
  49. _shadow = nullptr;
  50. _slider = nullptr;
  51. }
  52. bool ControlSaturationBrightnessPicker::initWithTargetAndPos(Node* target, Vec2 pos)
  53. {
  54. if (Control::init())
  55. {
  56. // Add background and slider sprites
  57. _background=ControlUtils::addSpriteToTargetWithPosAndAnchor("colourPickerBackground.png", target, pos, Vec2(0.0f, 0.0f));
  58. _overlay=ControlUtils::addSpriteToTargetWithPosAndAnchor("colourPickerOverlay.png", target, pos, Vec2(0.0f, 0.0f));
  59. _shadow=ControlUtils::addSpriteToTargetWithPosAndAnchor("colourPickerShadow.png", target, pos, Vec2(0.0f, 0.0f));
  60. _slider=ControlUtils::addSpriteToTargetWithPosAndAnchor("colourPicker.png", target, pos, Vec2(0.5f, 0.5f));
  61. _startPos=pos; // starting position of the colour picker
  62. boxPos = 35; // starting position of the virtual box area for picking a colour
  63. boxSize = _background->getContentSize().width / 2; // the size (width and height) of the virtual box for picking a colour from
  64. return true;
  65. }
  66. else
  67. {
  68. return false;
  69. }
  70. }
  71. ControlSaturationBrightnessPicker* ControlSaturationBrightnessPicker::create(Node* target, Vec2 pos)
  72. {
  73. ControlSaturationBrightnessPicker *pRet = new (std::nothrow) ControlSaturationBrightnessPicker();
  74. pRet->initWithTargetAndPos(target, pos);
  75. pRet->autorelease();
  76. return pRet;
  77. }
  78. void ControlSaturationBrightnessPicker::setEnabled(bool enabled)
  79. {
  80. Control::setEnabled(enabled);
  81. if (_slider != nullptr)
  82. {
  83. _slider->setOpacity(enabled ? 255 : 128);
  84. }
  85. }
  86. void ControlSaturationBrightnessPicker::updateWithHSV(HSV hsv)
  87. {
  88. HSV hsvTemp;
  89. hsvTemp.s = 1;
  90. hsvTemp.h = hsv.h;
  91. hsvTemp.v = 1;
  92. RGBA rgb = ControlUtils::RGBfromHSV(hsvTemp);
  93. _background->setColor(Color3B((GLubyte)(rgb.r * 255.0f), (GLubyte)(rgb.g * 255.0f), (GLubyte)(rgb.b * 255.0f)));
  94. }
  95. void ControlSaturationBrightnessPicker::updateDraggerWithHSV(HSV hsv)
  96. {
  97. // Set the position of the slider to the correct saturation and brightness
  98. Vec2 pos(_startPos.x + boxPos + (boxSize*(1 - hsv.s)),
  99. _startPos.y + boxPos + (boxSize*hsv.v));
  100. // update
  101. updateSliderPosition(pos);
  102. }
  103. void ControlSaturationBrightnessPicker::updateSliderPosition(Vec2 sliderPosition)
  104. {
  105. // Clamp the position of the icon within the circle
  106. // Get the center point of the bkgd image
  107. float centerX = _startPos.x + _background->getBoundingBox().size.width*0.5f;
  108. float centerY = _startPos.y + _background->getBoundingBox().size.height*0.5f;
  109. // Work out the distance difference between the location and center
  110. float dx = sliderPosition.x - centerX;
  111. float dy = sliderPosition.y - centerY;
  112. float dist = sqrtf(dx * dx + dy * dy);
  113. // Update angle by using the direction of the location
  114. float angle = atan2f(dy, dx);
  115. // Set the limit to the slider movement within the colour picker
  116. float limit = _background->getBoundingBox().size.width*0.5f;
  117. // Check distance doesn't exceed the bounds of the circle
  118. if (dist > limit)
  119. {
  120. sliderPosition.x = centerX + limit * cosf(angle);
  121. sliderPosition.y = centerY + limit * sinf(angle);
  122. }
  123. // Set the position of the dragger
  124. _slider->setPosition(sliderPosition);
  125. // Clamp the position within the virtual box for colour selection
  126. if (sliderPosition.x < _startPos.x + boxPos) sliderPosition.x = _startPos.x + boxPos;
  127. else if (sliderPosition.x > _startPos.x + boxPos + boxSize - 1) sliderPosition.x = _startPos.x + boxPos + boxSize - 1;
  128. if (sliderPosition.y < _startPos.y + boxPos) sliderPosition.y = _startPos.y + boxPos;
  129. else if (sliderPosition.y > _startPos.y + boxPos + boxSize) sliderPosition.y = _startPos.y + boxPos + boxSize;
  130. // Use the position / slider width to determine the percentage the dragger is at
  131. _saturation = 1.0f - fabs((_startPos.x + (float)boxPos - sliderPosition.x)/(float)boxSize);
  132. _brightness = fabs((_startPos.y + (float)boxPos - sliderPosition.y)/(float)boxSize);
  133. }
  134. bool ControlSaturationBrightnessPicker::checkSliderPosition(Vec2 location)
  135. {
  136. // Clamp the position of the icon within the circle
  137. // get the center point of the bkgd image
  138. float centerX = _startPos.x + _background->getBoundingBox().size.width*0.5f;
  139. float centerY = _startPos.y + _background->getBoundingBox().size.height*0.5f;
  140. // work out the distance difference between the location and center
  141. float dx = location.x - centerX;
  142. float dy = location.y - centerY;
  143. float dist = sqrtf(dx*dx+dy*dy);
  144. // check that the touch location is within the bounding rectangle before sending updates
  145. if (dist <= _background->getBoundingBox().size.width*0.5f)
  146. {
  147. updateSliderPosition(location);
  148. sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
  149. return true;
  150. }
  151. return false;
  152. }
  153. bool ControlSaturationBrightnessPicker::onTouchBegan(Touch* touch, Event* /*event*/)
  154. {
  155. if (!isEnabled() || !isVisible())
  156. {
  157. return false;
  158. }
  159. // Get the touch location
  160. Vec2 touchLocation=getTouchLocation(touch);
  161. // Check the touch position on the slider
  162. return checkSliderPosition(touchLocation);
  163. }
  164. void ControlSaturationBrightnessPicker::onTouchMoved(Touch* touch, Event* /*event*/)
  165. {
  166. // Get the touch location
  167. Vec2 touchLocation=getTouchLocation(touch);
  168. //small modification: this allows changing of the colour, even if the touch leaves the bounding area
  169. // updateSliderPosition(touchLocation);
  170. // sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
  171. // Check the touch position on the slider
  172. checkSliderPosition(touchLocation);
  173. }
  174. NS_CC_EXT_END