issue #5057, refactor UICheckBox event callback

This commit is contained in:
andyque 2014-05-09 16:04:55 +08:00
parent c5a505ec4c
commit 451b7a1b6b
8 changed files with 37 additions and 27 deletions

View File

@ -65,7 +65,6 @@ _frontCrossDisabledRendererAdaptDirty(true)
CheckBox::~CheckBox()
{
_checkBoxEventListener = nullptr;
_checkBoxEventSelector = nullptr;
}
@ -359,6 +358,11 @@ bool CheckBox::getSelectedState()
void CheckBox::selectedEvent()
{
if (_checkBoxEventCallback)
{
_checkBoxEventCallback(this, EventType::SELECTED);
}
if (_checkBoxEventListener && _checkBoxEventSelector)
{
(_checkBoxEventListener->*_checkBoxEventSelector)(this,CHECKBOX_STATE_EVENT_SELECTED);
@ -367,6 +371,9 @@ void CheckBox::selectedEvent()
void CheckBox::unSelectedEvent()
{
if (_checkBoxEventCallback) {
_checkBoxEventCallback(this, EventType::UNSELECTED);
}
if (_checkBoxEventListener && _checkBoxEventSelector)
{
(_checkBoxEventListener->*_checkBoxEventSelector)(this,CHECKBOX_STATE_EVENT_UNSELECTED);
@ -378,6 +385,11 @@ void CheckBox::addEventListenerCheckBox(Ref *target, SEL_SelectedStateEvent sele
_checkBoxEventListener = target;
_checkBoxEventSelector = selector;
}
void CheckBox::addEventListener(ccCheckBoxCallback callback)
{
_checkBoxEventCallback = callback;
}
void CheckBox::updateFlippedX()
{

View File

@ -31,13 +31,13 @@ NS_CC_BEGIN
namespace ui {
typedef enum
CC_DEPRECATED_ATTRIBUTE typedef enum
{
CHECKBOX_STATE_EVENT_SELECTED,
CHECKBOX_STATE_EVENT_UNSELECTED
}CheckBoxEventType;
typedef void (Ref::*SEL_SelectedStateEvent)(Ref*,CheckBoxEventType);
CC_DEPRECATED_ATTRIBUTE typedef void (Ref::*SEL_SelectedStateEvent)(Ref*,CheckBoxEventType);
#define checkboxselectedeventselector(_SELECTOR) (SEL_SelectedStateEvent)(&_SELECTOR)
/**
@ -50,6 +50,14 @@ class CheckBox : public Widget
DECLARE_CLASS_GUI_INFO
public:
enum class EventType
{
SELECTED,
UNSELECTED
};
typedef std::function<void(Ref*,CheckBox::EventType)> ccCheckBoxCallback;
/**
* Default constructor
*/
@ -165,7 +173,8 @@ public:
bool getSelectedState();
//add a call back function would called when checkbox is selected or unselected.
void addEventListenerCheckBox(Ref* target,SEL_SelectedStateEvent selector);
CC_DEPRECATED_ATTRIBUTE void addEventListenerCheckBox(Ref* target,SEL_SelectedStateEvent selector);
void addEventListener(ccCheckBoxCallback callback);
//override "onTouchEnded" method of widget.
virtual void onTouchEnded(Touch *touch, Event *unusedEvent);
@ -218,9 +227,10 @@ protected:
Sprite* _backGroundBoxDisabledRenderer;
Sprite* _frontCrossDisabledRenderer;
bool _isSelected;
//if you use the old event callback, it will retain the _checkBoxEventListener
Ref* _checkBoxEventListener;
SEL_SelectedStateEvent _checkBoxEventSelector;
ccCheckBoxCallback _checkBoxEventCallback;
TextureResType _backGroundTexType;
TextureResType _backGroundSelectedTexType;

View File

@ -26,7 +26,6 @@
#define cocos2d_libs_UIDeprecated_h
#include "base/CCPlatformMacros.h"
//#include "ui/UIWidget.h"
NS_CC_BEGIN
@ -45,7 +44,6 @@ CC_DEPRECATED_ATTRIBUTE const Widget::BrightStyle BRIGHT_NONE = Widget::BrightSt
CC_DEPRECATED_ATTRIBUTE const Widget::BrightStyle BRIGHT_NORMAL = Widget::BrightStyle::NORMAL;
CC_DEPRECATED_ATTRIBUTE const Widget::BrightStyle BRIGHT_HIGHLIGHT = Widget::BrightStyle::HIGHLIGHT;
CC_DEPRECATED_ATTRIBUTE typedef Widget::TextureResType TextureResType;
CC_DEPRECATED_ATTRIBUTE typedef Widget::PositionType PositionType;
@ -53,10 +51,6 @@ CC_DEPRECATED_ATTRIBUTE typedef Widget::SizeType SizeType;
CC_DEPRECATED_ATTRIBUTE typedef Widget::Type WidgetType;
CC_DEPRECATED_ATTRIBUTE typedef Widget::BrightStyle BrightStyle;
}

View File

@ -81,9 +81,6 @@ Widget::~Widget()
if (_realFocusedWidget == this) {
_realFocusedWidget = nullptr;
}
CC_SAFE_RELEASE_NULL(_touchEventListener);
}
Widget* Widget::create()
@ -661,9 +658,6 @@ void Widget::addTouchEventListener(Ref *target, SEL_TouchEvent selector)
{
_touchEventListener = target;
_touchEventSelector = selector;
CC_SAFE_RETAIN(target);
}
void Widget::addTouchEventListener(Widget::ccWidgetTouchCallback callback)

View File

@ -40,7 +40,7 @@ bool UICheckBoxTest::init()
"cocosui/check_box_active_disable.png");
checkBox->setPosition(Vector2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
checkBox->addEventListenerCheckBox(this, checkboxselectedeventselector(UICheckBoxTest::selectedEvent));
checkBox->addEventListener(CC_CALLBACK_2(UICheckBoxTest::selectedEvent, this));
_uiLayer->addChild(checkBox);
return true;
@ -48,15 +48,15 @@ bool UICheckBoxTest::init()
return false;
}
void UICheckBoxTest::selectedEvent(Ref* pSender,CheckBoxEventType type)
void UICheckBoxTest::selectedEvent(Ref* pSender,CheckBox::EventType type)
{
switch (type)
{
case CHECKBOX_STATE_EVENT_SELECTED:
case CheckBox::EventType::SELECTED:
_displayValueLabel->setText(String::createWithFormat("Selected")->getCString());
break;
case CHECKBOX_STATE_EVENT_UNSELECTED:
case CheckBox::EventType::UNSELECTED:
_displayValueLabel->setText(String::createWithFormat("Unselected")->getCString());
break;

View File

@ -33,7 +33,7 @@ public:
UICheckBoxTest();
~UICheckBoxTest();
bool init();
void selectedEvent(Ref* pSender,CheckBoxEventType type);
void selectedEvent(Ref* pSender,CheckBox::EventType type);
protected:
UI_SCENE_CREATE_FUNC(UICheckBoxTest)

View File

@ -35,7 +35,7 @@ bool UICheckBoxTest_Editor::init()
_sceneTitle = static_cast<Text*>(Helper::seekWidgetByName(root, "UItest"));
CheckBox* checkbox = static_cast<CheckBox*>(Helper::seekWidgetByName(root, "CheckBox_540"));
checkbox->addEventListenerCheckBox(this, checkboxselectedeventselector(UICheckBoxTest_Editor::selectedStateEvent));
checkbox->addEventListener(CC_CALLBACK_2(UICheckBoxTest_Editor::selectedStateEvent, this));
_displayValueLabel = Text::create();
_displayValueLabel->setFontName("fonts/Marker Felt.ttf");
@ -51,15 +51,15 @@ bool UICheckBoxTest_Editor::init()
return false;
}
void UICheckBoxTest_Editor::selectedStateEvent(Ref *pSender, CheckBoxEventType type)
void UICheckBoxTest_Editor::selectedStateEvent(Ref *pSender, CheckBox::EventType type)
{
switch (type)
{
case CHECKBOX_STATE_EVENT_UNSELECTED:
case CheckBox::EventType::UNSELECTED:
_displayValueLabel->setText("Unselected");
break;
case CHECKBOX_STATE_EVENT_SELECTED:
case CheckBox::EventType::SELECTED:
_displayValueLabel->setText("Selected");
break;

View File

@ -33,7 +33,7 @@ public:
UICheckBoxTest_Editor();
~UICheckBoxTest_Editor();
bool init();
void selectedStateEvent(Ref* pSender, CheckBoxEventType type);
void selectedStateEvent(Ref* pSender, CheckBox::EventType type);
protected:
UI_SCENE_EDITOR_CREATE_FUNC(UICheckBoxTest_Editor)