CCControlColourPicker.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "CCControlColourPicker.h"
  32. #include "2d/CCSpriteFrameCache.h"
  33. #include "2d/CCSpriteBatchNode.h"
  34. NS_CC_EXT_BEGIN
  35. ControlColourPicker::ControlColourPicker()
  36. : _colourPicker(nullptr)
  37. , _huePicker(nullptr)
  38. , _background(nullptr)
  39. {
  40. }
  41. ControlColourPicker::~ControlColourPicker()
  42. {
  43. CC_SAFE_RELEASE(_background);
  44. CC_SAFE_RELEASE(_huePicker);
  45. CC_SAFE_RELEASE(_colourPicker);
  46. }
  47. bool ControlColourPicker::init()
  48. {
  49. if (Control::init())
  50. {
  51. // Cache the sprites
  52. SpriteFrameCache::getInstance()->addSpriteFramesWithFile("extensions/CCControlColourPickerSpriteSheet.plist");
  53. // Create the sprite batch node
  54. SpriteBatchNode *spriteSheet = SpriteBatchNode::create("extensions/CCControlColourPickerSpriteSheet.png");
  55. addChild(spriteSheet);
  56. // MIPMAP
  57. // ccTexParams params = {GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
  58. /* Comment next line to avoid something like mosaic in 'ControlExtensionTest',
  59. especially the display of 'huePickerBackground.png' when in 800*480 window size with 480*320 design resolution and hd(960*640) resources.
  60. */
  61. // spriteSheet->getTexture()->setAliasTexParameters();
  62. // spriteSheet->getTexture()->setTexParameters(&params);
  63. // spriteSheet->getTexture()->generateMipmap();
  64. // Init default color
  65. _hsv.h = 0;
  66. _hsv.s = 0;
  67. _hsv.v = 0;
  68. // Add image
  69. _background=ControlUtils::addSpriteToTargetWithPosAndAnchor("menuColourPanelBackground.png", spriteSheet, Vec2::ZERO, Vec2(0.5f, 0.5f));
  70. if(!_background) return false;
  71. CC_SAFE_RETAIN(_background);
  72. Vec2 backgroundPointZero = _background->getPosition() - Vec2(_background->getContentSize().width / 2, _background->getContentSize().height / 2);
  73. // Setup panels
  74. float hueShift = 8;
  75. float colourShift = 28;
  76. _huePicker = new (std::nothrow) ControlHuePicker();
  77. _huePicker->initWithTargetAndPos(spriteSheet, Vec2(backgroundPointZero.x + hueShift, backgroundPointZero.y + hueShift));
  78. _colourPicker = new (std::nothrow) ControlSaturationBrightnessPicker();
  79. _colourPicker->initWithTargetAndPos(spriteSheet, Vec2(backgroundPointZero.x + colourShift, backgroundPointZero.y + colourShift));
  80. // Setup events
  81. _huePicker->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlColourPicker::hueSliderValueChanged), Control::EventType::VALUE_CHANGED);
  82. _colourPicker->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlColourPicker::colourSliderValueChanged), Control::EventType::VALUE_CHANGED);
  83. // Set defaults
  84. updateHueAndControlPicker();
  85. addChild(_huePicker);
  86. addChild(_colourPicker);
  87. // Set content size
  88. setContentSize(_background->getContentSize());
  89. return true;
  90. }
  91. else
  92. return false;
  93. }
  94. ControlColourPicker* ControlColourPicker::create()
  95. {
  96. ControlColourPicker *pRet = new (std::nothrow) ControlColourPicker();
  97. pRet->init();
  98. pRet->autorelease();
  99. return pRet;
  100. }
  101. void ControlColourPicker::setColor(const Color3B& color)
  102. {
  103. // FIXME: fixed me if not correct
  104. Control::setColor(color);
  105. RGBA rgba;
  106. rgba.r = color.r / 255.0f;
  107. rgba.g = color.g / 255.0f;
  108. rgba.b = color.b / 255.0f;
  109. rgba.a = 1.0f;
  110. _hsv=ControlUtils::HSVfromRGB(rgba);
  111. updateHueAndControlPicker();
  112. }
  113. void ControlColourPicker::setEnabled(bool enabled)
  114. {
  115. Control::setEnabled(enabled);
  116. if (_huePicker != nullptr)
  117. {
  118. _huePicker->setEnabled(enabled);
  119. }
  120. if (_colourPicker)
  121. {
  122. _colourPicker->setEnabled(enabled);
  123. }
  124. }
  125. //need two events to prevent an infinite loop! (can't update huePicker when the huePicker triggers the callback due to Control::EventType::VALUE_CHANGED)
  126. void ControlColourPicker::updateControlPicker()
  127. {
  128. _huePicker->setHue(_hsv.h);
  129. _colourPicker->updateWithHSV(_hsv);
  130. }
  131. void ControlColourPicker::updateHueAndControlPicker()
  132. {
  133. _huePicker->setHue(_hsv.h);
  134. _colourPicker->updateWithHSV(_hsv);
  135. _colourPicker->updateDraggerWithHSV(_hsv);
  136. }
  137. void ControlColourPicker::hueSliderValueChanged(Ref * sender, Control::EventType /*controlEvent*/)
  138. {
  139. _hsv.h = ((ControlHuePicker*)sender)->getHue();
  140. // Update the value
  141. RGBA rgb = ControlUtils::RGBfromHSV(_hsv);
  142. // FIXME: fixed me if not correct
  143. Control::setColor(Color3B((GLubyte)(rgb.r * 255.0f), (GLubyte)(rgb.g * 255.0f), (GLubyte)(rgb.b * 255.0f)));
  144. // Send Control callback
  145. sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
  146. updateControlPicker();
  147. }
  148. void ControlColourPicker::colourSliderValueChanged(Ref * sender, Control::EventType /*controlEvent*/)
  149. {
  150. _hsv.s=((ControlSaturationBrightnessPicker*)sender)->getSaturation();
  151. _hsv.v=((ControlSaturationBrightnessPicker*)sender)->getBrightness();
  152. // Update the value
  153. RGBA rgb = ControlUtils::RGBfromHSV(_hsv);
  154. // FIXME: fixed me if not correct
  155. Control::setColor(Color3B((GLubyte)(rgb.r * 255.0f), (GLubyte)(rgb.g * 255.0f), (GLubyte)(rgb.b * 255.0f)));
  156. // Send Control callback
  157. sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
  158. }
  159. //ignore all touches, handled by children
  160. bool ControlColourPicker::onTouchBegan(Touch* /*touch*/, Event* /*pEvent*/)
  161. {
  162. return false;
  163. }
  164. NS_CC_EXT_END