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 "CCControlSaturationBrightnessPicker.h"
|
|
|
|
|
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
|
|
|
ControlSaturationBrightnessPicker::ControlSaturationBrightnessPicker()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _saturation(0.0f)
|
|
|
|
, _brightness(0.0f)
|
2014-04-09 22:53:59 +08:00
|
|
|
, _background(nullptr)
|
|
|
|
, _overlay(nullptr)
|
|
|
|
, _shadow(nullptr)
|
|
|
|
, _slider(nullptr)
|
2012-09-25 16:57:51 +08:00
|
|
|
, boxPos(0)
|
|
|
|
, boxSize(0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlSaturationBrightnessPicker::~ControlSaturationBrightnessPicker()
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2012-09-25 17:26:09 +08:00
|
|
|
removeAllChildrenWithCleanup(true);
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
_background = nullptr;
|
|
|
|
_overlay = nullptr;
|
|
|
|
_shadow = nullptr;
|
|
|
|
_slider = nullptr;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool ControlSaturationBrightnessPicker::initWithTargetAndPos(Node* target, Vec2 pos)
|
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
|
|
|
{
|
|
|
|
// Add background and slider sprites
|
2014-05-15 01:07:09 +08:00
|
|
|
_background=ControlUtils::addSpriteToTargetWithPosAndAnchor("colourPickerBackground.png", target, pos, Vec2(0.0f, 0.0f));
|
|
|
|
_overlay=ControlUtils::addSpriteToTargetWithPosAndAnchor("colourPickerOverlay.png", target, pos, Vec2(0.0f, 0.0f));
|
|
|
|
_shadow=ControlUtils::addSpriteToTargetWithPosAndAnchor("colourPickerShadow.png", target, pos, Vec2(0.0f, 0.0f));
|
|
|
|
_slider=ControlUtils::addSpriteToTargetWithPosAndAnchor("colourPicker.png", target, pos, Vec2(0.5f, 0.5f));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_startPos=pos; // starting position of the colour picker
|
2012-04-19 14:35:52 +08:00
|
|
|
boxPos = 35; // starting position of the virtual box area for picking a colour
|
2016-09-12 09:45:34 +08:00
|
|
|
boxSize = _background->getContentSize().width / 2; // the size (width and height) of the virtual box for picking a colour from
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
ControlSaturationBrightnessPicker* ControlSaturationBrightnessPicker::create(Node* target, Vec2 pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
ControlSaturationBrightnessPicker *pRet = new (std::nothrow) ControlSaturationBrightnessPicker();
|
2012-04-19 14:35:52 +08:00
|
|
|
pRet->initWithTargetAndPos(target, pos);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSaturationBrightnessPicker::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 (_slider != nullptr)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_slider->setOpacity(enabled ? 255 : 128);
|
2012-09-25 17:26:09 +08:00
|
|
|
}
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSaturationBrightnessPicker::updateWithHSV(HSV hsv)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
HSV hsvTemp;
|
|
|
|
hsvTemp.s = 1;
|
|
|
|
hsvTemp.h = hsv.h;
|
|
|
|
hsvTemp.v = 1;
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
RGBA rgb = ControlUtils::RGBfromHSV(hsvTemp);
|
2013-07-05 16:49:22 +08:00
|
|
|
_background->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
|
|
|
void ControlSaturationBrightnessPicker::updateDraggerWithHSV(HSV hsv)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Set the position of the slider to the correct saturation and brightness
|
2015-04-20 01:40:52 +08:00
|
|
|
Vec2 pos(_startPos.x + boxPos + (boxSize*(1 - hsv.s)),
|
2013-06-15 14:03:30 +08:00
|
|
|
_startPos.y + boxPos + (boxSize*hsv.v));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// update
|
|
|
|
updateSliderPosition(pos);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void ControlSaturationBrightnessPicker::updateSliderPosition(Vec2 sliderPosition)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Clamp the position of the icon within the circle
|
|
|
|
|
|
|
|
// Get the center point of the bkgd image
|
2013-07-17 09:16:04 +08:00
|
|
|
float centerX = _startPos.x + _background->getBoundingBox().size.width*0.5f;
|
|
|
|
float centerY = _startPos.y + _background->getBoundingBox().size.height*0.5f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Work out the distance difference between the location and center
|
|
|
|
float dx = sliderPosition.x - centerX;
|
|
|
|
float dy = sliderPosition.y - centerY;
|
|
|
|
float dist = sqrtf(dx * dx + dy * dy);
|
|
|
|
|
|
|
|
// Update angle by using the direction of the location
|
|
|
|
float angle = atan2f(dy, dx);
|
|
|
|
|
|
|
|
// Set the limit to the slider movement within the colour picker
|
2013-07-17 09:16:04 +08:00
|
|
|
float limit = _background->getBoundingBox().size.width*0.5f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Check distance doesn't exceed the bounds of the circle
|
|
|
|
if (dist > limit)
|
|
|
|
{
|
|
|
|
sliderPosition.x = centerX + limit * cosf(angle);
|
|
|
|
sliderPosition.y = centerY + limit * sinf(angle);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the position of the dragger
|
2013-06-15 14:03:30 +08:00
|
|
|
_slider->setPosition(sliderPosition);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Clamp the position within the virtual box for colour selection
|
2013-06-15 14:03:30 +08:00
|
|
|
if (sliderPosition.x < _startPos.x + boxPos) sliderPosition.x = _startPos.x + boxPos;
|
|
|
|
else if (sliderPosition.x > _startPos.x + boxPos + boxSize - 1) sliderPosition.x = _startPos.x + boxPos + boxSize - 1;
|
|
|
|
if (sliderPosition.y < _startPos.y + boxPos) sliderPosition.y = _startPos.y + boxPos;
|
|
|
|
else if (sliderPosition.y > _startPos.y + boxPos + boxSize) sliderPosition.y = _startPos.y + boxPos + boxSize;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2016-10-17 10:12:54 +08:00
|
|
|
// Use the position / slider width to determine the percentage the dragger is at
|
2013-06-15 14:03:30 +08:00
|
|
|
_saturation = 1.0f - fabs((_startPos.x + (float)boxPos - sliderPosition.x)/(float)boxSize);
|
|
|
|
_brightness = fabs((_startPos.y + (float)boxPos - sliderPosition.y)/(float)boxSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool ControlSaturationBrightnessPicker::checkSliderPosition(Vec2 location)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Clamp the position of the icon within the circle
|
|
|
|
|
|
|
|
// get the center point of the bkgd image
|
2013-07-17 09:16:04 +08:00
|
|
|
float centerX = _startPos.x + _background->getBoundingBox().size.width*0.5f;
|
|
|
|
float centerY = _startPos.y + _background->getBoundingBox().size.height*0.5f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// work out the distance difference between the location and center
|
|
|
|
float dx = location.x - centerX;
|
|
|
|
float dy = location.y - centerY;
|
|
|
|
float dist = sqrtf(dx*dx+dy*dy);
|
|
|
|
|
|
|
|
// check that the touch location is within the bounding rectangle before sending updates
|
2013-07-17 09:16:04 +08:00
|
|
|
if (dist <= _background->getBoundingBox().size.width*0.5f)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
updateSliderPosition(location);
|
2013-07-26 14:37:26 +08:00
|
|
|
sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
bool ControlSaturationBrightnessPicker::onTouchBegan(Touch* touch, Event* event)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-10-30 23:10:05 +08:00
|
|
|
if (!isEnabled() || !isVisible())
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Get the touch location
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 touchLocation=getTouchLocation(touch);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Check the touch position on the slider
|
|
|
|
return checkSliderPosition(touchLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void ControlSaturationBrightnessPicker::onTouchMoved(Touch* touch, Event* event)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Get the touch location
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 touchLocation=getTouchLocation(touch);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//small modification: this allows changing of the colour, even if the touch leaves the bounding area
|
2012-09-25 16:57:51 +08:00
|
|
|
// updateSliderPosition(touchLocation);
|
2013-07-26 14:37:26 +08:00
|
|
|
// sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
|
2012-04-19 14:35:52 +08:00
|
|
|
// Check the touch position on the slider
|
2012-09-25 16:57:51 +08:00
|
|
|
checkSliderPosition(touchLocation);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_END
|