axmol/extensions/GUI/CCControlExtension/CCControlColourPicker.cpp

201 lines
6.9 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2012 cocos2d-x.org
2022-08-08 18:02:17 +08:00
* https://axys1.github.io/
*
* Copyright 2012 Stewart Hamilton-Arrandale.
* http://creativewax.co.uk
*
* Modified by Yannick Loriot.
* http://yannickloriot.com
2021-12-25 10:04:45 +08:00
*
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
*
* 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:
2021-12-25 10:04:45 +08:00
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
*
* 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 "2d/CCSpriteFrameCache.h"
#include "2d/CCSpriteBatchNode.h"
NS_AX_EXT_BEGIN
2021-12-25 10:04:45 +08:00
ControlColourPicker::ControlColourPicker() : _colourPicker(nullptr), _huePicker(nullptr), _background(nullptr) {}
ControlColourPicker::~ControlColourPicker()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(_background);
AX_SAFE_RELEASE(_huePicker);
AX_SAFE_RELEASE(_colourPicker);
}
bool ControlColourPicker::init()
{
if (Control::init())
{
// Cache the sprites
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("extensions/CCControlColourPickerSpriteSheet.plist");
2021-12-25 10:04:45 +08:00
// Create the sprite batch node
2021-12-25 10:04:45 +08:00
SpriteBatchNode* spriteSheet = SpriteBatchNode::create("extensions/CCControlColourPickerSpriteSheet.png");
addChild(spriteSheet);
2021-12-25 10:04:45 +08:00
// MIPMAP
2021-12-25 10:04:45 +08:00
// ccTexParams params = {GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
/* Comment next line to avoid something like mosaic in 'ControlExtensionTest',
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()->setSamplerDescriptor(&params);
// spriteSheet->getTexture()->generateMipmap();
// Init default color
_hsv.h = 0;
_hsv.s = 0;
_hsv.v = 0;
2021-12-25 10:04:45 +08:00
// Add image
2021-12-25 10:04:45 +08:00
_background = ControlUtils::addSpriteToTargetWithPosAndAnchor("menuColourPanelBackground.png", spriteSheet,
Vec2::ZERO, Vec2(0.5f, 0.5f));
if (!_background)
return false;
2022-07-16 10:43:05 +08:00
AX_SAFE_RETAIN(_background);
2021-12-25 10:04:45 +08:00
Vec2 backgroundPointZero = _background->getPosition() - Vec2(_background->getContentSize().width / 2,
_background->getContentSize().height / 2);
// Setup panels
2021-12-25 10:04:45 +08:00
float hueShift = 8;
float colourShift = 28;
2021-12-08 00:11:53 +08:00
_huePicker = new ControlHuePicker();
2021-12-25 10:04:45 +08:00
_huePicker->initWithTargetAndPos(spriteSheet,
Vec2(backgroundPointZero.x + hueShift, backgroundPointZero.y + hueShift));
2021-12-08 00:11:53 +08:00
_colourPicker = new ControlSaturationBrightnessPicker();
2021-12-25 10:04:45 +08:00
_colourPicker->initWithTargetAndPos(
spriteSheet, Vec2(backgroundPointZero.x + colourShift, backgroundPointZero.y + colourShift));
// Setup events
2021-12-25 10:04:45 +08:00
_huePicker->addTargetWithActionForControlEvents(
this, cccontrol_selector(ControlColourPicker::hueSliderValueChanged), Control::EventType::VALUE_CHANGED);
_colourPicker->addTargetWithActionForControlEvents(
this, cccontrol_selector(ControlColourPicker::colourSliderValueChanged), Control::EventType::VALUE_CHANGED);
// Set defaults
updateHueAndControlPicker();
addChild(_huePicker);
addChild(_colourPicker);
// Set content size
setContentSize(_background->getContentSize());
return true;
}
else
return false;
}
ControlColourPicker* ControlColourPicker::create()
{
2021-12-25 10:04:45 +08:00
ControlColourPicker* pRet = new ControlColourPicker();
pRet->init();
pRet->autorelease();
return pRet;
}
void ControlColourPicker::setColor(const Color3B& color)
{
// FIXME: fixed me if not correct
Control::setColor(color);
2021-12-25 10:04:45 +08:00
RGBA rgba;
2021-12-25 10:04:45 +08:00
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);
2021-12-25 10:04:45 +08:00
}
}
2021-12-25 10:04:45 +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)
void ControlColourPicker::updateControlPicker()
{
_huePicker->setHue(_hsv.h);
_colourPicker->updateWithHSV(_hsv);
}
void ControlColourPicker::updateHueAndControlPicker()
{
_huePicker->setHue(_hsv.h);
_colourPicker->updateWithHSV(_hsv);
_colourPicker->updateDraggerWithHSV(_hsv);
}
2021-12-25 10:04:45 +08:00
void ControlColourPicker::hueSliderValueChanged(Ref* sender, Control::EventType /*controlEvent*/)
{
2021-12-25 10:04:45 +08:00
_hsv.h = ((ControlHuePicker*)sender)->getHue();
// Update the value
2021-12-25 10:04:45 +08:00
RGBA rgb = ControlUtils::RGBfromHSV(_hsv);
// FIXME: fixed me if not correct
Control::setColor(Color3B((uint8_t)(rgb.r * 255.0f), (uint8_t)(rgb.g * 255.0f), (uint8_t)(rgb.b * 255.0f)));
2021-12-25 10:04:45 +08:00
// Send Control callback
sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
updateControlPicker();
}
2021-12-25 10:04:45 +08:00
void ControlColourPicker::colourSliderValueChanged(Ref* sender, Control::EventType /*controlEvent*/)
{
2021-12-25 10:04:45 +08:00
_hsv.s = ((ControlSaturationBrightnessPicker*)sender)->getSaturation();
_hsv.v = ((ControlSaturationBrightnessPicker*)sender)->getBrightness();
2021-12-25 10:04:45 +08:00
// Update the value
RGBA rgb = ControlUtils::RGBfromHSV(_hsv);
// FIXME: fixed me if not correct
Control::setColor(Color3B((uint8_t)(rgb.r * 255.0f), (uint8_t)(rgb.g * 255.0f), (uint8_t)(rgb.b * 255.0f)));
2021-12-25 10:04:45 +08:00
// Send Control callback
sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
}
2021-12-25 10:04:45 +08:00
// ignore all touches, handled by children
bool ControlColourPicker::onTouchBegan(Touch* /*touch*/, Event* /*pEvent*/)
{
return false;
}
NS_AX_EXT_END