Merge pull request #7834 from andyque/improveUI

add click event listener to all Widget classes
This commit is contained in:
minggo 2014-08-22 14:00:10 +08:00
Родитель eef93ccd14 3f51926c5d
Коммит aba973b1ea
3 изменённых файлов: 25 добавлений и 6 удалений

Просмотреть файл

@ -807,6 +807,10 @@ void Widget::releaseUpEvent()
{
(_touchEventListener->*_touchEventSelector)(this,TOUCH_EVENT_ENDED);
}
if (_clickEventListener) {
_clickEventListener(this);
}
this->release();
}
@ -831,10 +835,15 @@ void Widget::addTouchEventListener(Ref *target, SEL_TouchEvent selector)
_touchEventSelector = selector;
}
void Widget::addTouchEventListener(Widget::ccWidgetTouchCallback callback)
void Widget::addTouchEventListener(const ccWidgetTouchCallback& callback)
{
this->_touchEventCallback = callback;
}
void Widget::addClickEventListener(const ccWidgetClickCallback &callback)
{
this->_clickEventListener = callback;
}
bool Widget::hitTest(const Vec2 &pt)
{
@ -1083,6 +1092,7 @@ void Widget::copyProperties(Widget *widget)
_touchEventCallback = widget->_touchEventCallback;
_touchEventListener = widget->_touchEventListener;
_touchEventSelector = widget->_touchEventSelector;
_clickEventListener = widget->_clickEventListener;
_focused = widget->_focused;
_focusEnabled = widget->_focusEnabled;
_propagateTouchEvents = widget->_propagateTouchEvents;

Просмотреть файл

@ -108,6 +108,7 @@ public:
typedef std::function<void(Ref*,Widget::TouchEventType)> ccWidgetTouchCallback;
typedef std::function<void(Ref*)> ccWidgetClickCallback;
/**
* Default constructor
*/
@ -232,11 +233,14 @@ public:
virtual void visit(cocos2d::Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
/**
* Sets the touch event target/selector of the menu item
* Sets the touch event target/selector to the widget
*/
CC_DEPRECATED_ATTRIBUTE void addTouchEventListener(Ref* target,SEL_TouchEvent selector);
void addTouchEventListener(ccWidgetTouchCallback callback);
void addTouchEventListener(const ccWidgetTouchCallback& callback);
/**
* Set a click event handler to the widget
*/
void addClickEventListener(const ccWidgetClickCallback& callback);
/**
* Changes the position (x,y) of the widget in OpenGL coordinates
@ -709,6 +713,7 @@ protected:
#pragma warning (pop)
#endif
ccWidgetTouchCallback _touchEventCallback;
ccWidgetClickCallback _clickEventListener;
private:
class FocusNavigationController;
static FocusNavigationController* _focusNavigationController;

Просмотреть файл

@ -301,7 +301,6 @@ bool UIButtonTest_Title::init()
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
button->setTitleColor(Color3B::YELLOW);
CCASSERT(button->getTitleColor() == Color3B::YELLOW, "Button setTitleColotr & getTitleColor not match!");
// button->addTouchEventListener(this, toucheventselector(UIButtonTest_Title::touchEvent));
button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_Title::touchEvent, this));
_uiLayer->addChild(button);
@ -481,7 +480,7 @@ bool UIButtonTestSwitchScale9::init()
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
button->addTouchEventListener(CC_CALLBACK_2(UIButtonTestSwitchScale9::touchEvent, this));
button->ignoreContentAdaptWithSize(false);
_uiLayer->addChild(button);
@ -559,6 +558,11 @@ bool UIButtonTestZoomScale::init()
"cocosui/animationbuttonpressed.png");
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
button->setPressedActionEnabled(true);
button->addClickEventListener([=](Ref* sender){
CCLOG("Button clicked, position = (%f, %f)", button->getPosition().x,
button->getPosition().y);
});
button->setName("button");
_uiLayer->addChild(button);
button->setZoomScale(-0.5);