123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- #include "CCControlColourPicker.h"
- #include "2d/CCSpriteFrameCache.h"
- #include "2d/CCSpriteBatchNode.h"
- NS_CC_EXT_BEGIN
- ControlColourPicker::ControlColourPicker()
- : _colourPicker(nullptr)
- , _huePicker(nullptr)
- , _background(nullptr)
- {
- }
- ControlColourPicker::~ControlColourPicker()
- {
- CC_SAFE_RELEASE(_background);
- CC_SAFE_RELEASE(_huePicker);
- CC_SAFE_RELEASE(_colourPicker);
- }
- bool ControlColourPicker::init()
- {
- if (Control::init())
- {
-
- SpriteFrameCache::getInstance()->addSpriteFramesWithFile("extensions/CCControlColourPickerSpriteSheet.plist");
-
-
- SpriteBatchNode *spriteSheet = SpriteBatchNode::create("extensions/CCControlColourPickerSpriteSheet.png");
- addChild(spriteSheet);
-
-
-
-
- _hsv.h = 0;
- _hsv.s = 0;
- _hsv.v = 0;
-
-
- _background=ControlUtils::addSpriteToTargetWithPosAndAnchor("menuColourPanelBackground.png", spriteSheet, Vec2::ZERO, Vec2(0.5f, 0.5f));
- if(!_background) return false;
- CC_SAFE_RETAIN(_background);
-
- Vec2 backgroundPointZero = _background->getPosition() - Vec2(_background->getContentSize().width / 2, _background->getContentSize().height / 2);
-
-
- float hueShift = 8;
- float colourShift = 28;
-
- _huePicker = new (std::nothrow) ControlHuePicker();
- _huePicker->initWithTargetAndPos(spriteSheet, Vec2(backgroundPointZero.x + hueShift, backgroundPointZero.y + hueShift));
- _colourPicker = new (std::nothrow) ControlSaturationBrightnessPicker();
- _colourPicker->initWithTargetAndPos(spriteSheet, Vec2(backgroundPointZero.x + colourShift, backgroundPointZero.y + colourShift));
-
-
- _huePicker->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlColourPicker::hueSliderValueChanged), Control::EventType::VALUE_CHANGED);
- _colourPicker->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlColourPicker::colourSliderValueChanged), Control::EventType::VALUE_CHANGED);
-
-
- updateHueAndControlPicker();
- addChild(_huePicker);
- addChild(_colourPicker);
-
- setContentSize(_background->getContentSize());
- return true;
- }
- else
- return false;
- }
- ControlColourPicker* ControlColourPicker::create()
- {
- ControlColourPicker *pRet = new (std::nothrow) ControlColourPicker();
- pRet->init();
- pRet->autorelease();
- return pRet;
- }
- void ControlColourPicker::setColor(const Color3B& color)
- {
-
- Control::setColor(color);
-
- RGBA rgba;
- rgba.r = color.r / 255.0f;
- rgba.g = color.g / 255.0f;
- rgba.b = color.b / 255.0f;
- rgba.a = 1.0f;
-
- _hsv=ControlUtils::HSVfromRGB(rgba);
- updateHueAndControlPicker();
- }
- void ControlColourPicker::setEnabled(bool enabled)
- {
- Control::setEnabled(enabled);
- if (_huePicker != nullptr)
- {
- _huePicker->setEnabled(enabled);
- }
- if (_colourPicker)
- {
- _colourPicker->setEnabled(enabled);
- }
- }
- void ControlColourPicker::updateControlPicker()
- {
- _huePicker->setHue(_hsv.h);
- _colourPicker->updateWithHSV(_hsv);
- }
- void ControlColourPicker::updateHueAndControlPicker()
- {
- _huePicker->setHue(_hsv.h);
- _colourPicker->updateWithHSV(_hsv);
- _colourPicker->updateDraggerWithHSV(_hsv);
- }
- void ControlColourPicker::hueSliderValueChanged(Ref * sender, Control::EventType )
- {
- _hsv.h = ((ControlHuePicker*)sender)->getHue();
-
- RGBA rgb = ControlUtils::RGBfromHSV(_hsv);
-
- Control::setColor(Color3B((GLubyte)(rgb.r * 255.0f), (GLubyte)(rgb.g * 255.0f), (GLubyte)(rgb.b * 255.0f)));
-
-
- sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
- updateControlPicker();
- }
- void ControlColourPicker::colourSliderValueChanged(Ref * sender, Control::EventType )
- {
- _hsv.s=((ControlSaturationBrightnessPicker*)sender)->getSaturation();
- _hsv.v=((ControlSaturationBrightnessPicker*)sender)->getBrightness();
-
- RGBA rgb = ControlUtils::RGBfromHSV(_hsv);
-
- Control::setColor(Color3B((GLubyte)(rgb.r * 255.0f), (GLubyte)(rgb.g * 255.0f), (GLubyte)(rgb.b * 255.0f)));
-
-
- sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
- }
- bool ControlColourPicker::onTouchBegan(Touch* , Event* )
- {
- return false;
- }
- NS_CC_EXT_END
|