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"
2014-04-27 01:11:22 +08:00
# include "2d/CCSpriteFrameCache.h"
# include "2d/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
2013-06-20 14:15:53 +08:00
ControlColourPicker : : ControlColourPicker ( )
2014-04-09 22:53:59 +08:00
: _colourPicker ( nullptr )
, _huePicker ( nullptr )
, _background ( nullptr )
2012-09-25 16:57:51 +08:00
{
}
2013-06-20 14:15:53 +08:00
ControlColourPicker : : ~ ControlColourPicker ( )
2012-09-25 16:57:51 +08:00
{
2013-06-15 14:03:30 +08:00
CC_SAFE_RELEASE ( _background ) ;
CC_SAFE_RELEASE ( _huePicker ) ;
CC_SAFE_RELEASE ( _colourPicker ) ;
2012-09-25 16:57:51 +08:00
}
2013-06-20 14:15:53 +08:00
bool ControlColourPicker : : init ( )
2012-04-19 14:35:52 +08:00
{
2013-06-20 14:15:53 +08:00
if ( Control : : init ( ) )
2012-04-19 14:35:52 +08:00
{
// Cache the sprites
2013-07-12 06:24:23 +08:00
SpriteFrameCache : : getInstance ( ) - > addSpriteFramesWithFile ( " extensions/CCControlColourPickerSpriteSheet.plist " ) ;
2012-04-19 14:35:52 +08:00
// Create the sprite batch node
2013-06-20 14:15:53 +08:00
SpriteBatchNode * spriteSheet = SpriteBatchNode : : 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};
2013-06-20 14:15:53 +08:00
/* Comment next line to avoid something like mosaic in 'ControlExtensionTest',
2012-10-14 22:03:23 +08:00
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
2013-06-15 14:03:30 +08:00
_hsv . h = 0 ;
_hsv . s = 0 ;
_hsv . v = 0 ;
2012-04-19 14:35:52 +08:00
// Add image
2014-05-15 01:07:09 +08:00
_background = ControlUtils : : addSpriteToTargetWithPosAndAnchor ( " menuColourPanelBackground.png " , spriteSheet , Vec2 : : ZERO , Vec2 ( 0.5f , 0.5f ) ) ;
2014-06-09 07:12:17 +08:00
if ( ! _background ) return false ;
2013-06-15 14:03:30 +08:00
CC_SAFE_RETAIN ( _background ) ;
2013-06-09 16:37:15 +08:00
2014-05-15 01:07:09 +08:00
Vec2 backgroundPointZero = _background - > getPosition ( ) - Vec2 ( _background - > getContentSize ( ) . width / 2 , _background - > getContentSize ( ) . height / 2 ) ;
2012-04-19 14:35:52 +08:00
2012-09-25 16:57:51 +08:00
// Setup panels
2012-04-19 14:35:52 +08:00
float hueShift = 8 ;
float colourShift = 28 ;
2013-06-20 14:15:53 +08:00
_huePicker = new ControlHuePicker ( ) ;
2014-05-15 01:07:09 +08:00
_huePicker - > initWithTargetAndPos ( spriteSheet , Vec2 ( backgroundPointZero . x + hueShift , backgroundPointZero . y + hueShift ) ) ;
2013-06-20 14:15:53 +08:00
_colourPicker = new ControlSaturationBrightnessPicker ( ) ;
2014-05-15 01:07:09 +08:00
_colourPicker - > initWithTargetAndPos ( spriteSheet , Vec2 ( backgroundPointZero . x + colourShift , backgroundPointZero . y + colourShift ) ) ;
2012-04-19 14:35:52 +08:00
// Setup events
2013-07-26 14:37:26 +08:00
_huePicker - > addTargetWithActionForControlEvents ( this , cccontrol_selector ( ControlColourPicker : : hueSliderValueChanged ) , Control : : EventType : : VALUE_CHANGED ) ;
_colourPicker - > addTargetWithActionForControlEvents ( this , cccontrol_selector ( ControlColourPicker : : colourSliderValueChanged ) , Control : : EventType : : VALUE_CHANGED ) ;
2012-04-19 14:35:52 +08:00
// Set defaults
updateHueAndControlPicker ( ) ;
2013-06-15 14:03:30 +08:00
addChild ( _huePicker ) ;
addChild ( _colourPicker ) ;
2012-04-19 14:35:52 +08:00
// Set content size
2013-06-15 14:03:30 +08:00
setContentSize ( _background - > getContentSize ( ) ) ;
2012-04-19 14:35:52 +08:00
return true ;
}
else
return false ;
}
2013-06-20 14:15:53 +08:00
ControlColourPicker * ControlColourPicker : : create ( )
2012-04-19 14:35:52 +08:00
{
2013-06-20 14:15:53 +08:00
ControlColourPicker * pRet = new ControlColourPicker ( ) ;
2012-04-19 14:35:52 +08:00
pRet - > init ( ) ;
pRet - > autorelease ( ) ;
return pRet ;
}
2013-07-05 16:49:22 +08:00
void ControlColourPicker : : setColor ( const Color3B & color )
2012-04-19 14:35:52 +08:00
{
2013-02-28 11:55:36 +08:00
// XXX fixed me if not correct
2013-06-20 14:15:53 +08:00
Control : : setColor ( 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 ;
2013-06-20 14:15:53 +08:00
_hsv = ControlUtils : : HSVfromRGB ( rgba ) ;
2012-04-19 14:35:52 +08:00
updateHueAndControlPicker ( ) ;
}
2013-06-20 14:15:53 +08:00
void ControlColourPicker : : setEnabled ( bool enabled )
2012-09-25 17:26:09 +08:00
{
2013-06-20 14:15:53 +08:00
Control : : setEnabled ( enabled ) ;
2014-07-10 00:45:27 +08:00
if ( _huePicker ! = nullptr )
2012-09-25 16:57:51 +08:00
{
2013-06-15 14:03:30 +08:00
_huePicker - > setEnabled ( enabled ) ;
2012-09-25 17:26:09 +08:00
}
2013-06-15 14:03:30 +08:00
if ( _colourPicker )
2012-09-25 17:26:09 +08:00
{
2013-06-15 14:03:30 +08:00
_colourPicker - > setEnabled ( enabled ) ;
2012-09-25 17:26:09 +08:00
}
}
2012-09-25 16:57:51 +08:00
2013-07-26 14:37:26 +08:00
//need two events to prevent an infinite loop! (can't update huePicker when the huePicker triggers the callback due to Control::EventType::VALUE_CHANGED)
2013-06-20 14:15:53 +08:00
void ControlColourPicker : : updateControlPicker ( )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
_huePicker - > setHue ( _hsv . h ) ;
_colourPicker - > updateWithHSV ( _hsv ) ;
2012-04-19 14:35:52 +08:00
}
2013-06-20 14:15:53 +08:00
void ControlColourPicker : : updateHueAndControlPicker ( )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
_huePicker - > setHue ( _hsv . h ) ;
_colourPicker - > updateWithHSV ( _hsv ) ;
_colourPicker - > updateDraggerWithHSV ( _hsv ) ;
2012-04-19 14:35:52 +08:00
}
2014-02-20 10:53:49 +08:00
void ControlColourPicker : : hueSliderValueChanged ( Ref * sender , Control : : EventType controlEvent )
2012-04-19 14:35:52 +08:00
{
2013-06-20 14:15:53 +08:00
_hsv . h = ( ( ControlHuePicker * ) sender ) - > getHue ( ) ;
2012-04-19 14:35:52 +08:00
// Update the value
2013-06-20 14:15:53 +08:00
RGBA rgb = ControlUtils : : RGBfromHSV ( _hsv ) ;
2013-02-28 11:55:36 +08:00
// XXX fixed me if not correct
2013-07-05 16:49:22 +08:00
Control : : setColor ( Color3B ( ( GLubyte ) ( rgb . r * 255.0f ) , ( GLubyte ) ( rgb . g * 255.0f ) , ( GLubyte ) ( rgb . b * 255.0f ) ) ) ;
2012-04-19 14:35:52 +08:00
2013-06-20 14:15:53 +08:00
// Send Control callback
2013-07-26 14:37:26 +08:00
sendActionsForControlEvents ( Control : : EventType : : VALUE_CHANGED ) ;
2012-04-19 14:35:52 +08:00
updateControlPicker ( ) ;
}
2014-02-20 10:53:49 +08:00
void ControlColourPicker : : colourSliderValueChanged ( Ref * sender , Control : : EventType controlEvent )
2012-04-19 14:35:52 +08:00
{
2013-06-20 14:15:53 +08:00
_hsv . s = ( ( ControlSaturationBrightnessPicker * ) sender ) - > getSaturation ( ) ;
_hsv . v = ( ( ControlSaturationBrightnessPicker * ) sender ) - > getBrightness ( ) ;
2012-04-19 14:35:52 +08:00
// Update the value
2013-06-20 14:15:53 +08:00
RGBA rgb = ControlUtils : : RGBfromHSV ( _hsv ) ;
2013-02-28 11:55:36 +08:00
// XXX fixed me if not correct
2013-07-05 16:49:22 +08:00
Control : : setColor ( Color3B ( ( GLubyte ) ( rgb . r * 255.0f ) , ( GLubyte ) ( rgb . g * 255.0f ) , ( GLubyte ) ( rgb . b * 255.0f ) ) ) ;
2012-04-19 14:35:52 +08:00
2013-06-20 14:15:53 +08:00
// Send Control callback
2013-07-26 14:37:26 +08:00
sendActionsForControlEvents ( Control : : EventType : : VALUE_CHANGED ) ;
2012-04-19 14:35:52 +08:00
}
//ignore all touches, handled by children
2013-09-03 18:22:03 +08:00
bool ControlColourPicker : : onTouchBegan ( Touch * touch , Event * pEvent )
2012-04-19 14:35:52 +08:00
{
return false ;
}
2012-04-27 18:47:49 +08:00
NS_CC_EXT_END