From 0c2fd977eac6d967277f3bc6dd29cc5f3343a9cf Mon Sep 17 00:00:00 2001 From: andyque Date: Thu, 8 May 2014 18:10:21 +0800 Subject: [PATCH] issue #5057. add UIDeprecated.h and refactor FocusDirection to inner class of Widget and also simplify the enum value --- build/cocos2d_libs.xcodeproj/project.pbxproj | 2 + cocos/ui/CocosGUI.h | 1 + cocos/ui/UIDeprecated.h | 25 +++++++++ cocos/ui/UILayout.cpp | 56 +++++++++---------- cocos/ui/UIWidget.cpp | 3 +- cocos/ui/UIWidget.h | 16 +++--- .../UIFocusTest/UIFocusTest.cpp | 8 +-- 7 files changed, 71 insertions(+), 40 deletions(-) create mode 100644 cocos/ui/UIDeprecated.h diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj b/build/cocos2d_libs.xcodeproj/project.pbxproj index 4b7d3d067b..d5c4ce007d 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -2287,6 +2287,7 @@ 2905FA1218CF08D100240AA3 /* UITextField.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UITextField.h; sourceTree = ""; }; 2905FA1318CF08D100240AA3 /* UIWidget.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UIWidget.cpp; sourceTree = ""; }; 2905FA1418CF08D100240AA3 /* UIWidget.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIWidget.h; sourceTree = ""; }; + 29080DEB191B82CE0066F8DF /* UIDeprecated.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UIDeprecated.h; sourceTree = ""; }; 296CAD201915EC8000C64FBF /* CCEventFocus.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCEventFocus.cpp; path = ../base/CCEventFocus.cpp; sourceTree = ""; }; 296CAD211915EC8000C64FBF /* CCEventFocus.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCEventFocus.h; path = ../base/CCEventFocus.h; sourceTree = ""; }; 296CAD261915EC9900C64FBF /* CCEventListenerFocus.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCEventListenerFocus.cpp; path = ../base/CCEventListenerFocus.cpp; sourceTree = ""; }; @@ -4021,6 +4022,7 @@ 50E6D33118E174130051CA34 /* UIRelativeBox.h */, 50E6D33218E174130051CA34 /* UIVBox.cpp */, 50E6D33318E174130051CA34 /* UIVBox.h */, + 29080DEB191B82CE0066F8DF /* UIDeprecated.h */, ); name = ui; path = ../cocos/ui; diff --git a/cocos/ui/CocosGUI.h b/cocos/ui/CocosGUI.h index 296f0508fb..1df97eb225 100644 --- a/cocos/ui/CocosGUI.h +++ b/cocos/ui/CocosGUI.h @@ -45,6 +45,7 @@ THE SOFTWARE. #include "ui/UIHBox.h" #include "ui/UIVBox.h" #include "ui/UIRelativeBox.h" +#include "ui/UIDeprecated.h" NS_CC_BEGIN namespace ui { diff --git a/cocos/ui/UIDeprecated.h b/cocos/ui/UIDeprecated.h new file mode 100644 index 0000000000..c693101562 --- /dev/null +++ b/cocos/ui/UIDeprecated.h @@ -0,0 +1,25 @@ +// +// UIDeprecated.h +// cocos2d_libs +// +// Created by guanghui on 5/8/14. +// +// + +#ifndef cocos2d_libs_UIDeprecated_h +#define cocos2d_libs_UIDeprecated_h + +#include "base/CCPlatformMacros.h" +#include "UIWidget.h" + +NS_CC_BEGIN + +namespace ui { + + +} + + +NS_CC_END + +#endif diff --git a/cocos/ui/UILayout.cpp b/cocos/ui/UILayout.cpp index 9c32d8d3aa..73682844c6 100644 --- a/cocos/ui/UILayout.cpp +++ b/cocos/ui/UILayout.cpp @@ -1675,7 +1675,7 @@ int Layout::findNearestChildWidgetIndex(FocusDirection direction, Widget* baseWi float distance = FLT_MAX; int found = 0; - if (direction == FocusDirection::FocusDirection_Left || direction == FocusDirection::FocusDirection_Right) + if (direction == FocusDirection::LEFT || direction == FocusDirection::RIGHT) { Vector2 widgetPosition = this->getWorldCenterPoint(baseWidget); while (index < count) @@ -1709,7 +1709,7 @@ int Layout::findNearestChildWidgetIndex(FocusDirection direction, Widget* baseWi index = 0; found = 0; distance = FLT_MAX; - if (direction == FocusDirection::FocusDirection_Down || direction == FocusDirection::FocusDirection_Up) { + if (direction == FocusDirection::DOWN || direction == FocusDirection::UP) { Vector2 widgetPosition = this->getWorldCenterPoint(baseWidget); while (index < count) { @@ -1743,7 +1743,7 @@ int Layout::findNearestChildWidgetIndex(FocusDirection direction, Widget* baseWi return 0; } -int Layout::findFarestChildWidgetIndex(cocos2d::ui::FocusDirection direction, cocos2d::ui::Widget *baseWidget) +int Layout::findFarestChildWidgetIndex(FocusDirection direction, cocos2d::ui::Widget *baseWidget) { if (baseWidget == nullptr || baseWidget == this) { @@ -1754,7 +1754,7 @@ int Layout::findFarestChildWidgetIndex(cocos2d::ui::FocusDirection direction, co float distance = -FLT_MAX; int found = 0; - if (direction == FocusDirection::FocusDirection_Left || direction == FocusDirection::FocusDirection_Right) + if (direction == FocusDirection::LEFT || direction == FocusDirection::RIGHT) { Vector2 widgetPosition = this->getWorldCenterPoint(baseWidget); while (index < count) @@ -1788,7 +1788,7 @@ int Layout::findFarestChildWidgetIndex(cocos2d::ui::FocusDirection direction, co index = 0; found = 0; distance = -FLT_MAX; - if (direction == FocusDirection::FocusDirection_Down || direction == FocusDirection::FocusDirection_Up) { + if (direction == FocusDirection::DOWN || direction == FocusDirection::UP) { Vector2 widgetPosition = this->getWorldCenterPoint(baseWidget); while (index < count) { @@ -1873,27 +1873,27 @@ void Layout::findProperSearchingFunctor(FocusDirection dir, Widget* baseWidget) Vector2 layoutPosition = this->getWorldCenterPoint(this->findFirstNonLayoutWidget()); - if (dir == FocusDirection::FocusDirection_Left) { + if (dir == FocusDirection::LEFT) { if (previousWidgetPosition.x > layoutPosition.x) { onPassFocusToChild = CC_CALLBACK_2(Layout::findNearestChildWidgetIndex, this); } else{ onPassFocusToChild = CC_CALLBACK_2(Layout::findFarestChildWidgetIndex, this); } - }else if(dir == FocusDirection::FocusDirection_Right){ + }else if(dir == FocusDirection::RIGHT){ if (previousWidgetPosition.x > layoutPosition.x) { onPassFocusToChild = CC_CALLBACK_2(Layout::findFarestChildWidgetIndex, this); } else{ onPassFocusToChild = CC_CALLBACK_2(Layout::findNearestChildWidgetIndex, this); } - }else if(dir == FocusDirection::FocusDirection_Down){ + }else if(dir == FocusDirection::DOWN){ if (previousWidgetPosition.y > layoutPosition.y) { onPassFocusToChild = CC_CALLBACK_2(Layout::findNearestChildWidgetIndex, this); }else{ onPassFocusToChild = CC_CALLBACK_2(Layout::findFarestChildWidgetIndex, this); } - }else if(dir == FocusDirection::FocusDirection_Up){ + }else if(dir == FocusDirection::UP){ if (previousWidgetPosition.y < layoutPosition.y) { onPassFocusToChild = CC_CALLBACK_2(Layout::findNearestChildWidgetIndex, this); }else{ @@ -1906,7 +1906,7 @@ void Layout::findProperSearchingFunctor(FocusDirection dir, Widget* baseWidget) } -Widget* Layout::passFocusToChild(cocos2d::ui::FocusDirection dir, cocos2d::ui::Widget *current) +Widget* Layout::passFocusToChild(FocusDirection dir, cocos2d::ui::Widget *current) { if (checkFocusEnabledChild()) { @@ -2188,7 +2188,7 @@ bool Layout::isLastWidgetInContainer(Widget* widget, FocusDirection direction) ssize_t index = container.getIndex(widget); if (parent->getLayoutType() == LAYOUT_LINEAR_HORIZONTAL) { - if (direction == FocusDirection::FocusDirection_Left) { + if (direction == FocusDirection::LEFT) { if (index == 0) { return true * isLastWidgetInContainer(parent, direction); @@ -2198,7 +2198,7 @@ bool Layout::isLastWidgetInContainer(Widget* widget, FocusDirection direction) return false; } } - if (direction == FocusDirection::FocusDirection_Right) { + if (direction == FocusDirection::RIGHT) { if (index == container.size()-1) { return true * isLastWidgetInContainer(parent, direction); @@ -2208,19 +2208,19 @@ bool Layout::isLastWidgetInContainer(Widget* widget, FocusDirection direction) return false; } } - if (direction == FocusDirection::FocusDirection_Down) + if (direction == FocusDirection::DOWN) { return isLastWidgetInContainer(parent, direction); } - if (direction == FocusDirection::FocusDirection_Up) + if (direction == FocusDirection::UP) { return isLastWidgetInContainer(parent, direction); } } else if(parent->getLayoutType() == LAYOUT_LINEAR_VERTICAL) { - if (direction == FocusDirection::FocusDirection_Up) + if (direction == FocusDirection::UP) { if (index == 0) { @@ -2232,7 +2232,7 @@ bool Layout::isLastWidgetInContainer(Widget* widget, FocusDirection direction) return false; } } - if (direction == FocusDirection::FocusDirection_Down) + if (direction == FocusDirection::DOWN) { if (index == container.size() - 1) { @@ -2243,12 +2243,12 @@ bool Layout::isLastWidgetInContainer(Widget* widget, FocusDirection direction) return false; } } - if (direction == FocusDirection::FocusDirection_Left) + if (direction == FocusDirection::LEFT) { return isLastWidgetInContainer(parent, direction); } - if (direction == FocusDirection::FocusDirection_Right) + if (direction == FocusDirection::RIGHT) { return isLastWidgetInContainer(parent, direction); } @@ -2274,7 +2274,7 @@ bool Layout::isWidgetAncestorSupportLoopFocus(Widget* widget, FocusDirection di auto layoutType = parent->getLayoutType(); if (layoutType == LAYOUT_LINEAR_HORIZONTAL) { - if (direction == FocusDirection::FocusDirection_Left || direction == FocusDirection::FocusDirection_Right) + if (direction == FocusDirection::LEFT || direction == FocusDirection::RIGHT) { return true; } @@ -2285,7 +2285,7 @@ bool Layout::isWidgetAncestorSupportLoopFocus(Widget* widget, FocusDirection di } if (layoutType == LAYOUT_LINEAR_VERTICAL) { - if (direction == FocusDirection::FocusDirection_Down || direction == FocusDirection::FocusDirection_Up) + if (direction == FocusDirection::DOWN || direction == FocusDirection::UP) { return true; } @@ -2338,16 +2338,16 @@ Widget* Layout::findNextFocusedWidget(FocusDirection direction, Widget* current) { switch (direction) { - case FocusDirection::FocusDirection_Left: + case FocusDirection::LEFT: { return this->getPreviousFocusedWidget(direction, current); }break; - case FocusDirection::FocusDirection_Right: + case FocusDirection::RIGHT: { return this->getNextFocusedWidget(direction, current); }break; - case FocusDirection::FocusDirection_Down: - case FocusDirection::FocusDirection_Up: + case FocusDirection::DOWN: + case FocusDirection::UP: { if (isLastWidgetInContainer(this, direction)) { @@ -2375,8 +2375,8 @@ Widget* Layout::findNextFocusedWidget(FocusDirection direction, Widget* current) { switch (direction) { - case FocusDirection::FocusDirection_Left: - case FocusDirection::FocusDirection_Right: + case FocusDirection::LEFT: + case FocusDirection::RIGHT: { if (isLastWidgetInContainer(this, direction)) { @@ -2393,12 +2393,12 @@ Widget* Layout::findNextFocusedWidget(FocusDirection direction, Widget* current) return Widget::findNextFocusedWidget(direction, this); } } break; - case FocusDirection::FocusDirection_Down: + case FocusDirection::DOWN: { return getNextFocusedWidget(direction, current); } break; - case FocusDirection::FocusDirection_Up: + case FocusDirection::UP: { return getPreviousFocusedWidget(direction, current); } diff --git a/cocos/ui/UIWidget.cpp b/cocos/ui/UIWidget.cpp index f1cb8a1f02..877f95f13a 100644 --- a/cocos/ui/UIWidget.cpp +++ b/cocos/ui/UIWidget.cpp @@ -984,12 +984,13 @@ bool Widget::isFocusEnabled() return _focusEnabled; } -Widget* Widget::findNextFocusedWidget(cocos2d::ui::FocusDirection direction, Widget* current) +Widget* Widget::findNextFocusedWidget(FocusDirection direction, Widget* current) { if (nullptr == onNextFocusedWidget || nullptr == onNextFocusedWidget(direction) ) { if (this->isFocused() || !current->isFocusEnabled()) { Node* parent = this->getParent(); + Layout* layout = dynamic_cast(parent); if (nullptr == layout) { diff --git a/cocos/ui/UIWidget.h b/cocos/ui/UIWidget.h index 17ed80494d..31d284f3f6 100644 --- a/cocos/ui/UIWidget.h +++ b/cocos/ui/UIWidget.h @@ -73,13 +73,7 @@ typedef enum POSITION_PERCENT }PositionType; -enum class FocusDirection -{ - FocusDirection_Left, - FocusDirection_Right, - FocusDirection_Up, - FocusDirection_Down -}; + typedef void (Ref::*SEL_TouchEvent)(Ref*,TouchEventType); @@ -91,6 +85,14 @@ typedef void (Ref::*SEL_TouchEvent)(Ref*,TouchEventType); class Widget : public ProtectedNode { public: + enum class FocusDirection + { + LEFT, + RIGHT, + UP, + DOWN + }; + /** * Default constructor */ diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIFocusTest/UIFocusTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIFocusTest/UIFocusTest.cpp index f811652b99..4dec52d51d 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIFocusTest/UIFocusTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIFocusTest/UIFocusTest.cpp @@ -80,7 +80,7 @@ void UIFocusTestBase::onLeftKeyPressed() if (!_firstFocusedWidget->isFocused()) { _firstFocusedWidget = _firstFocusedWidget->getCurrentFocusedWidget(false); } - _firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(FocusDirection::FocusDirection_Left, _firstFocusedWidget); + _firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::LEFT, _firstFocusedWidget); } } @@ -90,7 +90,7 @@ void UIFocusTestBase::onRightKeyPressed() if (!_firstFocusedWidget->isFocused()) { _firstFocusedWidget = _firstFocusedWidget->getCurrentFocusedWidget(false); } - _firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(FocusDirection::FocusDirection_Right, _firstFocusedWidget); + _firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::RIGHT, _firstFocusedWidget); } } @@ -100,7 +100,7 @@ void UIFocusTestBase::onUpKeyPressed() if (!_firstFocusedWidget->isFocused()) { _firstFocusedWidget = _firstFocusedWidget->getCurrentFocusedWidget(false); } - _firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(FocusDirection::FocusDirection_Up, _firstFocusedWidget); + _firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::UP, _firstFocusedWidget); } } @@ -111,7 +111,7 @@ void UIFocusTestBase::onDownKeyPressed() if (!_firstFocusedWidget->isFocused()) { _firstFocusedWidget = _firstFocusedWidget->getCurrentFocusedWidget(false); } - _firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(FocusDirection::FocusDirection_Down, _firstFocusedWidget); + _firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::DOWN, _firstFocusedWidget); } }