2014-03-11 17:13:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __UICHECKBOX_H__
|
|
|
|
#define __UICHECKBOX_H__
|
|
|
|
|
|
|
|
#include "ui/UIWidget.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-06-04 14:26:21 +08:00
|
|
|
class Sprite;
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
namespace ui {
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
typedef enum
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
CHECKBOX_STATE_EVENT_SELECTED,
|
|
|
|
CHECKBOX_STATE_EVENT_UNSELECTED
|
|
|
|
}CheckBoxEventType;
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
typedef void (Ref::*SEL_SelectedStateEvent)(Ref*,CheckBoxEventType);
|
2014-03-11 17:13:54 +08:00
|
|
|
#define checkboxselectedeventselector(_SELECTOR) (SEL_SelectedStateEvent)(&_SELECTOR)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
class CheckBox : public Widget
|
|
|
|
{
|
|
|
|
|
|
|
|
DECLARE_CLASS_GUI_INFO
|
|
|
|
|
|
|
|
public:
|
2014-05-09 16:04:55 +08:00
|
|
|
enum class EventType
|
|
|
|
{
|
|
|
|
SELECTED,
|
|
|
|
UNSELECTED
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::function<void(Ref*,CheckBox::EventType)> ccCheckBoxCallback;
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
/**
|
|
|
|
* Default constructor
|
|
|
|
*/
|
|
|
|
CheckBox();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default destructor
|
|
|
|
*/
|
|
|
|
virtual ~CheckBox();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocates and initializes.
|
|
|
|
*/
|
|
|
|
static CheckBox* create();
|
2014-04-02 15:58:18 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* create an checkbox
|
|
|
|
*
|
|
|
|
* @param backGround backGround texture.
|
|
|
|
*
|
|
|
|
* @param backGroundSelected backGround selected state texture.
|
|
|
|
*
|
|
|
|
* @param cross cross texture.
|
|
|
|
*
|
|
|
|
* @param frontCrossDisabled cross dark state texture.
|
|
|
|
*
|
|
|
|
* @param texType @see UI_TEX_TYPE_LOCAL
|
|
|
|
*/
|
|
|
|
static CheckBox* create(const std::string& backGround,
|
|
|
|
const std::string& backGroundSeleted,
|
|
|
|
const std::string& cross,
|
|
|
|
const std::string& backGroundDisabled,
|
|
|
|
const std::string& frontCrossDisabled,
|
2014-05-09 14:56:05 +08:00
|
|
|
TextureResType texType = TextureResType::LOCAL);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load textures for checkbox.
|
|
|
|
*
|
|
|
|
* @param backGround backGround texture.
|
|
|
|
*
|
|
|
|
* @param backGroundSelected backGround selected state texture.
|
|
|
|
*
|
|
|
|
* @param cross cross texture.
|
|
|
|
*
|
|
|
|
* @param frontCrossDisabled cross dark state texture.
|
|
|
|
*
|
|
|
|
* @param texType @see UI_TEX_TYPE_LOCAL
|
|
|
|
*/
|
2014-04-02 15:58:18 +08:00
|
|
|
void loadTextures(const std::string& backGround,
|
|
|
|
const std::string& backGroundSelected,
|
|
|
|
const std::string& cross,
|
|
|
|
const std::string& backGroundDisabled,
|
|
|
|
const std::string& frontCrossDisabled,
|
2014-05-09 14:56:05 +08:00
|
|
|
TextureResType texType = TextureResType::LOCAL);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load backGround texture for checkbox.
|
|
|
|
*
|
|
|
|
* @param backGround backGround texture.
|
|
|
|
*
|
|
|
|
* @param texType @see UI_TEX_TYPE_LOCAL
|
|
|
|
*/
|
2014-05-09 14:56:05 +08:00
|
|
|
void loadTextureBackGround(const std::string& backGround,TextureResType type = TextureResType::LOCAL);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load backGroundSelected texture for checkbox.
|
|
|
|
*
|
|
|
|
* @param backGroundSelected backGround selected state texture.
|
|
|
|
*
|
|
|
|
* @param texType @see UI_TEX_TYPE_LOCAL
|
|
|
|
*/
|
2014-05-09 14:56:05 +08:00
|
|
|
void loadTextureBackGroundSelected(const std::string& backGroundSelected,TextureResType texType = TextureResType::LOCAL);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load cross texture for checkbox.
|
|
|
|
*
|
|
|
|
* @param cross cross texture.
|
|
|
|
*
|
|
|
|
* @param texType @see UI_TEX_TYPE_LOCAL
|
|
|
|
*/
|
2014-05-09 14:56:05 +08:00
|
|
|
void loadTextureFrontCross(const std::string&,TextureResType texType = TextureResType::LOCAL);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load backGroundDisabled texture for checkbox.
|
|
|
|
*
|
|
|
|
* @param backGroundDisabled backGroundDisabled texture.
|
|
|
|
*
|
|
|
|
* @param texType @see UI_TEX_TYPE_LOCAL
|
|
|
|
*/
|
2014-05-09 14:56:05 +08:00
|
|
|
void loadTextureBackGroundDisabled(const std::string& backGroundDisabled,TextureResType texType = TextureResType::LOCAL);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load frontCrossDisabled texture for checkbox.
|
|
|
|
*
|
|
|
|
* @param frontCrossDisabled frontCrossDisabled texture.
|
|
|
|
*
|
|
|
|
* @param texType @see UI_TEX_TYPE_LOCAL
|
|
|
|
*/
|
2014-05-09 14:56:05 +08:00
|
|
|
void loadTextureFrontCrossDisabled(const std::string& frontCrossDisabled,TextureResType texType = TextureResType::LOCAL);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets selcted state for checkbox.
|
|
|
|
*
|
|
|
|
* @param selected true that checkbox is selected, false otherwise.
|
|
|
|
*/
|
|
|
|
void setSelectedState(bool selected);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets selcted state of checkbox.
|
|
|
|
*
|
|
|
|
* @return selected true that checkbox is selected, false otherwise.
|
|
|
|
*/
|
2014-05-27 11:08:23 +08:00
|
|
|
bool getSelectedState()const;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
//add a call back function would called when checkbox is selected or unselected.
|
2014-05-09 16:04:55 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE void addEventListenerCheckBox(Ref* target,SEL_SelectedStateEvent selector);
|
2014-05-14 16:14:28 +08:00
|
|
|
void addEventListener(const ccCheckBoxCallback& callback);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
|
2014-04-17 14:08:25 +08:00
|
|
|
//override "getVirtualRendererSize" method of widget.
|
|
|
|
virtual const Size& getVirtualRendererSize() const override;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
//override "getVirtualRenderer" method of widget.
|
|
|
|
virtual Node* getVirtualRenderer() override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the "class name" of widget.
|
|
|
|
*/
|
|
|
|
virtual std::string getDescription() const override;
|
2014-03-21 13:44:29 +08:00
|
|
|
|
|
|
|
CC_CONSTRUCTOR_ACCESS:
|
|
|
|
virtual bool init() override;
|
2014-04-02 15:58:18 +08:00
|
|
|
virtual bool init(const std::string& backGround,
|
|
|
|
const std::string& backGroundSeleted,
|
|
|
|
const std::string& cross,
|
|
|
|
const std::string& backGroundDisabled,
|
|
|
|
const std::string& frontCrossDisabled,
|
2014-05-09 14:56:05 +08:00
|
|
|
TextureResType texType = TextureResType::LOCAL);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void initRenderer() override;
|
|
|
|
virtual void onPressStateChangedToNormal() override;
|
|
|
|
virtual void onPressStateChangedToPressed() override;
|
|
|
|
virtual void onPressStateChangedToDisabled() override;
|
2014-05-26 14:44:28 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void selectedEvent();
|
|
|
|
void unSelectedEvent();
|
2014-05-26 14:44:28 +08:00
|
|
|
|
|
|
|
virtual void releaseUpEvent();
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
virtual void onSizeChanged() override;
|
2014-06-06 16:01:55 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
virtual void updateFlippedX() override;
|
|
|
|
virtual void updateFlippedY() override;
|
2014-06-06 16:01:55 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void backGroundTextureScaleChangedWithSize();
|
|
|
|
void backGroundSelectedTextureScaleChangedWithSize();
|
|
|
|
void frontCrossTextureScaleChangedWithSize();
|
|
|
|
void backGroundDisabledTextureScaleChangedWithSize();
|
|
|
|
void frontCrossDisabledTextureScaleChangedWithSize();
|
2014-06-06 16:01:55 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
virtual Widget* createCloneInstance() override;
|
|
|
|
virtual void copySpecialProperties(Widget* model) override;
|
2014-04-17 14:08:25 +08:00
|
|
|
virtual void adaptRenderers() override;
|
2014-03-11 17:13:54 +08:00
|
|
|
protected:
|
|
|
|
Sprite* _backGroundBoxRenderer;
|
|
|
|
Sprite* _backGroundSelectedBoxRenderer;
|
|
|
|
Sprite* _frontCrossRenderer;
|
|
|
|
Sprite* _backGroundBoxDisabledRenderer;
|
|
|
|
Sprite* _frontCrossDisabledRenderer;
|
|
|
|
bool _isSelected;
|
2014-05-09 16:04:55 +08:00
|
|
|
//if you use the old event callback, it will retain the _checkBoxEventListener
|
2014-03-11 17:13:54 +08:00
|
|
|
Ref* _checkBoxEventListener;
|
2014-05-09 18:37:22 +08:00
|
|
|
|
|
|
|
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
#elif _MSC_VER >= 1400 //vs 2005 or higher
|
|
|
|
#pragma warning (push)
|
|
|
|
#pragma warning (disable: 4996)
|
|
|
|
#endif
|
2014-03-11 17:13:54 +08:00
|
|
|
SEL_SelectedStateEvent _checkBoxEventSelector;
|
2014-05-09 18:37:22 +08:00
|
|
|
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
|
|
|
|
#pragma GCC diagnostic warning "-Wdeprecated-declarations"
|
|
|
|
#elif _MSC_VER >= 1400 //vs 2005 or higher
|
|
|
|
#pragma warning (pop)
|
|
|
|
#endif
|
|
|
|
|
2014-05-09 16:04:55 +08:00
|
|
|
ccCheckBoxCallback _checkBoxEventCallback;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
TextureResType _backGroundTexType;
|
|
|
|
TextureResType _backGroundSelectedTexType;
|
|
|
|
TextureResType _frontCrossTexType;
|
|
|
|
TextureResType _backGroundDisabledTexType;
|
|
|
|
TextureResType _frontCrossDisabledTexType;
|
|
|
|
|
|
|
|
std::string _backGroundFileName;
|
|
|
|
std::string _backGroundSelectedFileName;
|
|
|
|
std::string _frontCrossFileName;
|
|
|
|
std::string _backGroundDisabledFileName;
|
|
|
|
std::string _frontCrossDisabledFileName;
|
2014-04-17 14:08:25 +08:00
|
|
|
|
|
|
|
bool _backGroundBoxRendererAdaptDirty;
|
|
|
|
bool _backGroundSelectedBoxRendererAdaptDirty;
|
|
|
|
bool _frontCrossRendererAdaptDirty;
|
|
|
|
bool _backGroundBoxDisabledRendererAdaptDirty;
|
|
|
|
bool _frontCrossDisabledRendererAdaptDirty;
|
2014-03-11 17:13:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif /* defined(__CocoGUI__CheckBox__) */
|