2012-04-19 14:35:52 +08:00
|
|
|
/*
|
2012-09-25 17:26:09 +08:00
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
|
|
|
* http://www.cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
*
|
|
|
|
* Copyright 2012 Stewart Hamilton-Arrandale.
|
|
|
|
* http://creativewax.co.uk
|
|
|
|
*
|
|
|
|
* Modified by Yannick Loriot.
|
|
|
|
* http://yannickloriot.com
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* converted to c++ / cocos2d-x by Angus C
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CCControlColourPicker.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "support/CCPointExtension.h"
|
|
|
|
#include "sprite_nodes/CCSpriteFrameCache.h"
|
|
|
|
#include "sprite_nodes/CCSpriteBatchNode.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_BEGIN
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
CCControlColourPicker::CCControlColourPicker()
|
|
|
|
: m_colourPicker(NULL)
|
|
|
|
, m_huePicker(NULL)
|
|
|
|
, m_background(NULL)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CCControlColourPicker::~CCControlColourPicker()
|
|
|
|
{
|
|
|
|
if (m_background)
|
|
|
|
{
|
|
|
|
m_background->removeFromParentAndCleanup(true);
|
|
|
|
}
|
2012-09-25 17:26:09 +08:00
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
if (m_huePicker)
|
|
|
|
{
|
|
|
|
m_huePicker->removeFromParentAndCleanup(true);
|
2012-09-25 17:26:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_colourPicker)
|
|
|
|
{
|
|
|
|
m_colourPicker->removeFromParentAndCleanup(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_background = NULL;
|
|
|
|
m_huePicker = NULL;
|
2012-09-25 16:57:51 +08:00
|
|
|
m_colourPicker = NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
bool CCControlColourPicker::init()
|
|
|
|
{
|
|
|
|
if (CCControl::init())
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled(true);
|
2012-04-19 14:35:52 +08:00
|
|
|
// Cache the sprites
|
|
|
|
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("extensions/CCControlColourPickerSpriteSheet.plist");
|
|
|
|
|
|
|
|
// Create the sprite batch node
|
2012-06-14 15:13:16 +08:00
|
|
|
CCSpriteBatchNode *spriteSheet = CCSpriteBatchNode::create("extensions/CCControlColourPickerSpriteSheet.png");
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(spriteSheet);
|
|
|
|
|
|
|
|
// MIPMAP
|
2012-09-25 16:57:51 +08:00
|
|
|
// ccTexParams params = {GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
|
2012-10-14 22:03:23 +08:00
|
|
|
/* Comment next line to avoid something like mosaic in 'CCControlExtensionTest',
|
|
|
|
especially the display of 'huePickerBackground.png' when in 800*480 window size with 480*320 design resolution and hd(960*640) resources.
|
|
|
|
*/
|
|
|
|
// spriteSheet->getTexture()->setAliasTexParameters();
|
2012-09-25 16:57:51 +08:00
|
|
|
// spriteSheet->getTexture()->setTexParameters(¶ms);
|
|
|
|
// spriteSheet->getTexture()->generateMipmap();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Init default color
|
|
|
|
m_hsv.h = 0;
|
|
|
|
m_hsv.s = 0;
|
|
|
|
m_hsv.v = 0;
|
|
|
|
|
|
|
|
// Add image
|
|
|
|
m_background=CCControlUtils::addSpriteToTargetWithPosAndAnchor("menuColourPanelBackground.png", spriteSheet, CCPointZero, ccp(0.5f, 0.5f));
|
|
|
|
|
|
|
|
CCPoint backgroundPointZero = ccpSub(m_background->getPosition(), ccp (m_background->getContentSize().width / 2, m_background->getContentSize().height / 2));
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
// Setup panels
|
2012-04-19 14:35:52 +08:00
|
|
|
float hueShift = 8;
|
|
|
|
float colourShift = 28;
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
m_huePicker = new CCControlHuePicker();
|
|
|
|
m_huePicker->initWithTargetAndPos(spriteSheet, ccp(backgroundPointZero.x + hueShift, backgroundPointZero.y + hueShift));
|
|
|
|
m_colourPicker = new CCControlSaturationBrightnessPicker();
|
|
|
|
m_colourPicker->initWithTargetAndPos(spriteSheet, ccp(backgroundPointZero.x + colourShift, backgroundPointZero.y + colourShift));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Setup events
|
2012-06-07 05:31:14 +08:00
|
|
|
m_huePicker->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlColourPicker::hueSliderValueChanged), CCControlEventValueChanged);
|
|
|
|
m_colourPicker->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlColourPicker::colourSliderValueChanged), CCControlEventValueChanged);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Set defaults
|
|
|
|
updateHueAndControlPicker();
|
|
|
|
addChild(m_huePicker);
|
|
|
|
addChild(m_colourPicker);
|
|
|
|
|
|
|
|
// Set content size
|
|
|
|
setContentSize(m_background->getContentSize());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCControlColourPicker* CCControlColourPicker::colourPicker()
|
|
|
|
{
|
|
|
|
return CCControlColourPicker::create();
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCControlColourPicker* CCControlColourPicker::create()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCControlColourPicker *pRet = new CCControlColourPicker();
|
|
|
|
pRet->init();
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
void CCControlColourPicker::setColor(const ccColor3B& color)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
m_tColor = color;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
RGBA rgba;
|
2012-09-25 16:57:51 +08:00
|
|
|
rgba.r = color.r / 255.0f;
|
|
|
|
rgba.g = color.g / 255.0f;
|
|
|
|
rgba.b = color.b / 255.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
rgba.a = 1.0f;
|
|
|
|
|
|
|
|
m_hsv=CCControlUtils::HSVfromRGB(rgba);
|
|
|
|
updateHueAndControlPicker();
|
|
|
|
}
|
|
|
|
|
2012-09-25 17:26:09 +08:00
|
|
|
void CCControlColourPicker::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
CCControl::setEnabled(enabled);
|
2012-09-25 16:57:51 +08:00
|
|
|
if (m_huePicker != NULL)
|
|
|
|
{
|
|
|
|
m_huePicker->setEnabled(enabled);
|
2012-09-25 17:26:09 +08:00
|
|
|
}
|
|
|
|
if (m_colourPicker)
|
|
|
|
{
|
|
|
|
m_colourPicker->setEnabled(enabled);
|
|
|
|
}
|
|
|
|
}
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//need two events to prevent an infinite loop! (can't update huePicker when the huePicker triggers the callback due to CCControlEventValueChanged)
|
|
|
|
void CCControlColourPicker::updateControlPicker()
|
|
|
|
{
|
|
|
|
m_huePicker->setHue(m_hsv.h);
|
|
|
|
m_colourPicker->updateWithHSV(m_hsv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlColourPicker::updateHueAndControlPicker()
|
|
|
|
{
|
|
|
|
m_huePicker->setHue(m_hsv.h);
|
|
|
|
m_colourPicker->updateWithHSV(m_hsv);
|
|
|
|
m_colourPicker->updateDraggerWithHSV(m_hsv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-14 18:32:44 +08:00
|
|
|
void CCControlColourPicker::hueSliderValueChanged(CCObject * sender, CCControlEvent controlEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
m_hsv.h = ((CCControlHuePicker*)sender)->getHue();
|
|
|
|
|
|
|
|
// Update the value
|
|
|
|
RGBA rgb = CCControlUtils::RGBfromHSV(m_hsv);
|
2012-09-25 16:57:51 +08:00
|
|
|
m_tColor= ccc3((GLubyte)(rgb.r * 255.0f), (GLubyte)(rgb.g * 255.0f), (GLubyte)(rgb.b * 255.0f));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Send CCControl callback
|
|
|
|
sendActionsForControlEvents(CCControlEventValueChanged);
|
|
|
|
updateControlPicker();
|
|
|
|
}
|
|
|
|
|
2012-06-14 18:32:44 +08:00
|
|
|
void CCControlColourPicker::colourSliderValueChanged(CCObject * sender, CCControlEvent controlEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
m_hsv.s=((CCControlSaturationBrightnessPicker*)sender)->getSaturation();
|
|
|
|
m_hsv.v=((CCControlSaturationBrightnessPicker*)sender)->getBrightness();
|
|
|
|
|
|
|
|
|
|
|
|
// Update the value
|
|
|
|
RGBA rgb = CCControlUtils::RGBfromHSV(m_hsv);
|
2012-09-25 16:57:51 +08:00
|
|
|
m_tColor=ccc3((GLubyte)(rgb.r * 255.0f), (GLubyte)(rgb.g * 255.0f), (GLubyte)(rgb.b * 255.0f));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Send CCControl callback
|
|
|
|
sendActionsForControlEvents(CCControlEventValueChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
//ignore all touches, handled by children
|
|
|
|
bool CCControlColourPicker::ccTouchBegan(CCTouch* touch, CCEvent* pEvent)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_END
|