issue #5176, refactor passTouchEventToParent method of UIWidget

This commit is contained in:
andyque 2014-05-22 15:23:13 +08:00
parent 4d82d8d26b
commit c8d9ab8122
11 changed files with 72 additions and 54 deletions

View File

@ -308,7 +308,7 @@ void CheckBox::onTouchEnded(Touch *touch, Event *unusedEvent)
Widget* widgetParent = getWidgetParent(); Widget* widgetParent = getWidgetParent();
if (widgetParent) if (widgetParent)
{ {
widgetParent->checkChildInfo(2,this,_touchEndPos); widgetParent->passTouchEventToParent(TouchEventType::ENDED,this,_touchEndPos);
} }
} }

View File

@ -735,6 +735,7 @@ void Layout::visit(Renderer *renderer, const Mat4 &parentTransform, bool parentT
void Layout::sortAllChildren() void Layout::sortAllChildren()
{ {
Widget::sortAllChildren(); Widget::sortAllChildren();
doLayout();
} }
void Layout::stencilClippingVisit(Renderer *renderer, const Mat4 &parentTransform, bool parentTransformUpdated) void Layout::stencilClippingVisit(Renderer *renderer, const Mat4 &parentTransform, bool parentTransformUpdated)

View File

@ -414,11 +414,11 @@ void ListView::addEventListener(const ccListViewCallback& callback)
_eventCallback = callback; _eventCallback = callback;
} }
void ListView::selectedItemEvent(int state) void ListView::selectedItemEvent(TouchEventType event)
{ {
switch (state) switch (event)
{ {
case 0: case TouchEventType::BEGAN:
{ {
if (_listViewEventListener && _listViewEventSelector) if (_listViewEventListener && _listViewEventSelector)
{ {
@ -444,10 +444,10 @@ void ListView::selectedItemEvent(int state)
} }
void ListView::interceptTouchEvent(int handleState, Widget *sender, const Vec2 &touchPoint) void ListView::interceptTouchEvent(TouchEventType event, Widget *sender, const Vec2 &touchPoint)
{ {
ScrollView::interceptTouchEvent(handleState, sender, touchPoint); ScrollView::interceptTouchEvent(event, sender, touchPoint);
if (handleState != 1) if (event != TouchEventType::MOVED)
{ {
Widget* parent = sender; Widget* parent = sender;
while (parent) while (parent)
@ -459,7 +459,7 @@ void ListView::interceptTouchEvent(int handleState, Widget *sender, const Vec2 &
} }
parent = dynamic_cast<Widget*>(parent->getParent()); parent = dynamic_cast<Widget*>(parent->getParent());
} }
selectedItemEvent(handleState); selectedItemEvent(event);
} }
} }

View File

@ -204,8 +204,8 @@ protected:
virtual Widget* createCloneInstance() override; virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override; virtual void copySpecialProperties(Widget* model) override;
virtual void copyClonedWidgetChildren(Widget* model) override; virtual void copyClonedWidgetChildren(Widget* model) override;
void selectedItemEvent(int state); void selectedItemEvent(TouchEventType event);
virtual void interceptTouchEvent(int handleState,Widget* sender,const Vec2 &touchPoint) override; virtual void interceptTouchEvent(Widget::TouchEventType event,Widget* sender,const Vec2 &touchPoint) override;
protected: protected:
Widget* _model; Widget* _model;

View File

@ -344,7 +344,7 @@ void PageView::onTouchMoved(Touch *touch, Event *unusedEvent)
Widget* widgetParent = getWidgetParent(); Widget* widgetParent = getWidgetParent();
if (widgetParent) if (widgetParent)
{ {
widgetParent->checkChildInfo(1,this,_touchMovePos); widgetParent->passTouchEventToParent(TouchEventType::MOVED,this,_touchMovePos);
} }
moveEvent(); moveEvent();
} }
@ -495,19 +495,19 @@ void PageView::handleReleaseLogic(const Vec2 &touchPoint)
} }
} }
void PageView::checkChildInfo(int handleState,Widget* sender, const Vec2 &touchPoint) void PageView::passTouchEventToParent(TouchEventType event,Widget* sender, const Vec2 &touchPoint)
{ {
interceptTouchEvent(handleState, sender, touchPoint); interceptTouchEvent(event, sender, touchPoint);
} }
void PageView::interceptTouchEvent(int handleState, Widget *sender, const Vec2 &touchPoint) void PageView::interceptTouchEvent(TouchEventType event, Widget *sender, const Vec2 &touchPoint)
{ {
switch (handleState) switch (event)
{ {
case 0: case TouchEventType::BEGAN:
handlePressLogic(touchPoint); handlePressLogic(touchPoint);
break; break;
case 1: case TouchEventType::MOVED:
{ {
float offset = 0; float offset = 0;
offset = fabs(sender->getTouchStartPos().x - touchPoint.x); offset = fabs(sender->getTouchStartPos().x - touchPoint.x);
@ -518,11 +518,8 @@ void PageView::interceptTouchEvent(int handleState, Widget *sender, const Vec2 &
} }
} }
break; break;
case 2: case TouchEventType::CANCELED:
handleReleaseLogic(touchPoint); case TouchEventType::ENDED:
break;
case 3:
handleReleaseLogic(touchPoint); handleReleaseLogic(touchPoint);
break; break;
} }

View File

@ -192,10 +192,11 @@ protected:
virtual void handlePressLogic(const Vec2 &touchPoint); virtual void handlePressLogic(const Vec2 &touchPoint);
virtual void handleMoveLogic(const Vec2 &touchPoint) ; virtual void handleMoveLogic(const Vec2 &touchPoint) ;
virtual void handleReleaseLogic(const Vec2 &touchPoint) ; virtual void handleReleaseLogic(const Vec2 &touchPoint) ;
virtual void interceptTouchEvent(int handleState, Widget* sender, const Vec2 &touchPoint) ; virtual void interceptTouchEvent(TouchEventType event, Widget* sender, const Vec2 &touchPoint) ;
//overrided functions //overrided functions
virtual void checkChildInfo(int handleState, Widget* sender, const Vec2 &touchPoint) override; virtual void passTouchEventToParent(TouchEventType event,Widget* sender,const Vec2 &point) override;
virtual void onSizeChanged() override; virtual void onSizeChanged() override;
virtual Widget* createCloneInstance() override; virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override; virtual void copySpecialProperties(Widget* model) override;

View File

@ -40,7 +40,7 @@ protected:
virtual void handlePressLogic(const Vec2 &touchPoint) = 0; virtual void handlePressLogic(const Vec2 &touchPoint) = 0;
virtual void handleMoveLogic(const Vec2 &touchPoint) = 0; virtual void handleMoveLogic(const Vec2 &touchPoint) = 0;
virtual void handleReleaseLogic(const Vec2 &touchPoint) = 0; virtual void handleReleaseLogic(const Vec2 &touchPoint) = 0;
virtual void interceptTouchEvent(int handleState, Widget* sender, const Vec2 &touchPoint) = 0; virtual void interceptTouchEvent(Widget::TouchEventType event, Widget* sender, const Vec2 &touchPoint) = 0;
}; };
} }

View File

@ -1515,15 +1515,15 @@ void ScrollView::recordSlidTime(float dt)
} }
} }
void ScrollView::interceptTouchEvent(int handleState, Widget *sender, const Vec2 &touchPoint) void ScrollView::interceptTouchEvent(Widget::TouchEventType event, Widget *sender, const Vec2 &touchPoint)
{ {
switch (handleState) switch (event)
{ {
case 0: case TouchEventType::BEGAN:
handlePressLogic(touchPoint); handlePressLogic(touchPoint);
break; break;
case 1: case TouchEventType::MOVED:
{ {
float offset = (sender->getTouchStartPos() - touchPoint).getLength(); float offset = (sender->getTouchStartPos() - touchPoint).getLength();
if (offset > _childFocusCancelOffset) if (offset > _childFocusCancelOffset)
@ -1534,19 +1534,17 @@ void ScrollView::interceptTouchEvent(int handleState, Widget *sender, const Vec2
} }
break; break;
case 2: case TouchEventType::CANCELED:
case TouchEventType::ENDED:
handleReleaseLogic(touchPoint); handleReleaseLogic(touchPoint);
break;
case 3:
handleReleaseLogic(touchPoint); handleReleaseLogic(touchPoint);
break; break;
} }
} }
void ScrollView::checkChildInfo(int handleState,Widget* sender,const Vec2 &touchPoint) void ScrollView::passTouchEventToParent(cocos2d::ui::Widget::TouchEventType event, cocos2d::ui::Widget *sender, const cocos2d::Vec2 &point)
{ {
interceptTouchEvent(handleState, sender, touchPoint); interceptTouchEvent(event, sender, point);
} }
void ScrollView::scrollToTopEvent() void ScrollView::scrollToTopEvent()

View File

@ -320,6 +320,16 @@ CC_CONSTRUCTOR_ACCESS:
protected: protected:
virtual void initRenderer() override; virtual void initRenderer() override;
virtual void onSizeChanged() override;
virtual void doLayout() override;
virtual void passTouchEventToParent(TouchEventType event,Widget* sender,const Vec2 &point) override;
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
virtual void copyClonedWidgetChildren(Widget* model) override;
void moveChildren(float offsetX, float offsetY); void moveChildren(float offsetX, float offsetY);
void autoScrollChildren(float dt); void autoScrollChildren(float dt);
void bounceChildren(float dt); void bounceChildren(float dt);
@ -336,27 +346,28 @@ protected:
bool bounceScrollChildren(float touchOffsetX, float touchOffsetY); bool bounceScrollChildren(float touchOffsetX, float touchOffsetY);
void startRecordSlidAction(); void startRecordSlidAction();
virtual void endRecordSlidAction(); virtual void endRecordSlidAction();
//ScrollViewProtocol
virtual void handlePressLogic(const Vec2 &touchPoint) override; virtual void handlePressLogic(const Vec2 &touchPoint) override;
virtual void handleMoveLogic(const Vec2 &touchPoint) override; virtual void handleMoveLogic(const Vec2 &touchPoint) override;
virtual void handleReleaseLogic(const Vec2 &touchPoint) override; virtual void handleReleaseLogic(const Vec2 &touchPoint) override;
virtual void interceptTouchEvent(int handleState,Widget* sender,const Vec2 &touchPoint) override; virtual void interceptTouchEvent(Widget::TouchEventType event,Widget* sender,const Vec2 &touchPoint) override;
virtual void checkChildInfo(int handleState,Widget* sender,const Vec2 &touchPoint) override;
void recordSlidTime(float dt); void recordSlidTime(float dt);
void scrollToTopEvent(); void scrollToTopEvent();
void scrollToBottomEvent(); void scrollToBottomEvent();
void scrollToLeftEvent(); void scrollToLeftEvent();
void scrollToRightEvent(); void scrollToRightEvent();
void scrollingEvent(); void scrollingEvent();
void bounceTopEvent(); void bounceTopEvent();
void bounceBottomEvent(); void bounceBottomEvent();
void bounceLeftEvent(); void bounceLeftEvent();
void bounceRightEvent(); void bounceRightEvent();
virtual void onSizeChanged() override;
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
virtual void copyClonedWidgetChildren(Widget* model) override;
virtual void setClippingEnabled(bool able) override{Layout::setClippingEnabled(able);};
virtual void doLayout() override;
protected: protected:
Layout* _innerContainer; Layout* _innerContainer;

View File

@ -586,7 +586,7 @@ bool Widget::onTouchBegan(Touch *touch, Event *unusedEvent)
Widget* widgetParent = getWidgetParent(); Widget* widgetParent = getWidgetParent();
if (widgetParent) if (widgetParent)
{ {
widgetParent->checkChildInfo(0,this,_touchStartPos); widgetParent->passTouchEventToParent(TouchEventType::BEGAN, this, _touchStartPos);
} }
pushDownEvent(); pushDownEvent();
return true; return true;
@ -599,7 +599,7 @@ void Widget::onTouchMoved(Touch *touch, Event *unusedEvent)
Widget* widgetParent = getWidgetParent(); Widget* widgetParent = getWidgetParent();
if (widgetParent) if (widgetParent)
{ {
widgetParent->checkChildInfo(1,this,_touchMovePos); widgetParent->passTouchEventToParent(TouchEventType::MOVED, this, _touchMovePos);
} }
moveEvent(); moveEvent();
} }
@ -612,7 +612,7 @@ void Widget::onTouchEnded(Touch *touch, Event *unusedEvent)
Widget* widgetParent = getWidgetParent(); Widget* widgetParent = getWidgetParent();
if (widgetParent) if (widgetParent)
{ {
widgetParent->checkChildInfo(2,this,_touchEndPos); widgetParent->passTouchEventToParent(TouchEventType::ENDED, this, _touchEndPos);
} }
if (highlight) if (highlight)
{ {
@ -746,13 +746,14 @@ bool Widget::clippingParentAreaContainPoint(const Vec2 &pt)
return true; return true;
} }
void Widget::checkChildInfo(int handleState, Widget *sender, const Vec2 &touchPoint) void Widget::passTouchEventToParent(cocos2d::ui::Widget::TouchEventType event, cocos2d::ui::Widget *sender, const cocos2d::Vec2 &point)
{ {
Widget* widgetParent = getWidgetParent(); Widget* widgetParent = getWidgetParent();
if (widgetParent) if (widgetParent)
{ {
widgetParent->checkChildInfo(handleState,sender,touchPoint); widgetParent->passTouchEventToParent(event,sender,point);
} }
} }
void Widget::setPosition(const Vec2 &pos) void Widget::setPosition(const Vec2 &pos)

View File

@ -353,11 +353,6 @@ public:
*/ */
bool clippingParentAreaContainPoint(const Vec2 &pt); bool clippingParentAreaContainPoint(const Vec2 &pt);
/*
* Sends the touch event to widget's parent
*/
virtual void checkChildInfo(int handleState,Widget* sender,const Vec2 &touchPoint);
/* /*
* Gets the touch began point of widget when widget is selected. * Gets the touch began point of widget when widget is selected.
* *
@ -453,6 +448,14 @@ public:
* @return true if the point is in widget's space, flase otherwise. * @return true if the point is in widget's space, flase otherwise.
*/ */
virtual bool hitTest(const Vec2 &pt); virtual bool hitTest(const Vec2 &pt);
/*
* Sends the touch event to widget's parent
* @param event the touch event type, it could be BEGAN/MOVED/CANCELED/ENDED
* @param parent
* @param point
*/
virtual void passTouchEventToParent(TouchEventType event,Widget* sender,const Vec2 &point);
virtual bool onTouchBegan(Touch *touch, Event *unusedEvent); virtual bool onTouchBegan(Touch *touch, Event *unusedEvent);
virtual void onTouchMoved(Touch *touch, Event *unusedEvent); virtual void onTouchMoved(Touch *touch, Event *unusedEvent);
@ -615,21 +618,27 @@ protected:
void moveEvent(); void moveEvent();
void releaseUpEvent(); void releaseUpEvent();
void cancelUpEvent(); void cancelUpEvent();
virtual void updateTextureColor(){}; virtual void updateTextureColor(){};
virtual void updateTextureOpacity(){}; virtual void updateTextureOpacity(){};
virtual void updateTextureRGBA(){}; virtual void updateTextureRGBA(){};
virtual void updateFlippedX(){}; virtual void updateFlippedX(){};
virtual void updateFlippedY(){}; virtual void updateFlippedY(){};
virtual void adaptRenderers(){};
void updateColorToRenderer(Node* renderer); void updateColorToRenderer(Node* renderer);
void updateOpacityToRenderer(Node* renderer); void updateOpacityToRenderer(Node* renderer);
void updateRGBAToRenderer(Node* renderer); void updateRGBAToRenderer(Node* renderer);
void copyProperties(Widget* model); void copyProperties(Widget* model);
virtual Widget* createCloneInstance(); virtual Widget* createCloneInstance();
virtual void copySpecialProperties(Widget* model); virtual void copySpecialProperties(Widget* model);
virtual void copyClonedWidgetChildren(Widget* model); virtual void copyClonedWidgetChildren(Widget* model);
Widget* getWidgetParent(); Widget* getWidgetParent();
void updateContentSizeWithTextureSize(const Size& size); void updateContentSizeWithTextureSize(const Size& size);
virtual void adaptRenderers(){};
bool isAncestorsEnabled(); bool isAncestorsEnabled();
Widget* getAncensterWidget(Node* node); Widget* getAncensterWidget(Node* node);
bool isAncestorsVisible(Node* node); bool isAncestorsVisible(Node* node);