-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
CCControlColourPicker.cpp
199 lines (164 loc) · 6.75 KB
/
CCControlColourPicker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Copyright (c) 2012 cocos2d-x.org
* http://www.cocos2d-x.org
*
* 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"
#include "support/CCPointExtension.h"
#include "sprite_nodes/CCSpriteFrameCache.h"
#include "sprite_nodes/CCSpriteBatchNode.h"
NS_CC_EXT_BEGIN
CCControlColourPicker::CCControlColourPicker()
: m_colourPicker(NULL)
, m_huePicker(NULL)
, m_background(NULL)
{
}
CCControlColourPicker::~CCControlColourPicker()
{
CC_SAFE_RELEASE(m_background);
CC_SAFE_RELEASE(m_huePicker);
CC_SAFE_RELEASE(m_colourPicker);
}
bool CCControlColourPicker::init()
{
if (CCControl::init())
{
setTouchEnabled(true);
// Cache the sprites
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("extensions/CCControlColourPickerSpriteSheet.plist");
// Create the sprite batch node
CCSpriteBatchNode *spriteSheet = CCSpriteBatchNode::create("extensions/CCControlColourPickerSpriteSheet.png");
addChild(spriteSheet);
// MIPMAP
// ccTexParams params = {GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
/* 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();
// spriteSheet->getTexture()->setTexParameters(¶ms);
// spriteSheet->getTexture()->generateMipmap();
// 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));
// Setup panels
float hueShift = 8;
float colourShift = 28;
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));
// Setup events
m_huePicker->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlColourPicker::hueSliderValueChanged), CCControlEventValueChanged);
m_colourPicker->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlColourPicker::colourSliderValueChanged), CCControlEventValueChanged);
// Set defaults
updateHueAndControlPicker();
addChild(m_huePicker);
addChild(m_colourPicker);
// Set content size
setContentSize(m_background->getContentSize());
return true;
}
else
return false;
}
CCControlColourPicker* CCControlColourPicker::create()
{
CCControlColourPicker *pRet = new CCControlColourPicker();
pRet->init();
pRet->autorelease();
return pRet;
}
void CCControlColourPicker::setColor(const ccColor3B& color)
{
// XXX fixed me if not correct
CCControl::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;
m_hsv=CCControlUtils::HSVfromRGB(rgba);
updateHueAndControlPicker();
}
void CCControlColourPicker::setEnabled(bool enabled)
{
CCControl::setEnabled(enabled);
if (m_huePicker != NULL)
{
m_huePicker->setEnabled(enabled);
}
if (m_colourPicker)
{
m_colourPicker->setEnabled(enabled);
}
}
//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);
}
void CCControlColourPicker::hueSliderValueChanged(CCObject * sender, CCControlEvent controlEvent)
{
m_hsv.h = ((CCControlHuePicker*)sender)->getHue();
// Update the value
RGBA rgb = CCControlUtils::RGBfromHSV(m_hsv);
// XXX fixed me if not correct
CCControl::setColor(ccc3((GLubyte)(rgb.r * 255.0f), (GLubyte)(rgb.g * 255.0f), (GLubyte)(rgb.b * 255.0f)));
// Send CCControl callback
sendActionsForControlEvents(CCControlEventValueChanged);
updateControlPicker();
}
void CCControlColourPicker::colourSliderValueChanged(CCObject * sender, CCControlEvent controlEvent)
{
m_hsv.s=((CCControlSaturationBrightnessPicker*)sender)->getSaturation();
m_hsv.v=((CCControlSaturationBrightnessPicker*)sender)->getBrightness();
// Update the value
RGBA rgb = CCControlUtils::RGBfromHSV(m_hsv);
// XXX fixed me if not correct
CCControl::setColor(ccc3((GLubyte)(rgb.r * 255.0f), (GLubyte)(rgb.g * 255.0f), (GLubyte)(rgb.b * 255.0f)));
// Send CCControl callback
sendActionsForControlEvents(CCControlEventValueChanged);
}
//ignore all touches, handled by children
bool CCControlColourPicker::ccTouchBegan(CCTouch* touch, CCEvent* pEvent)
{
return false;
}
NS_CC_EXT_END