From 0c0f18e7f3966ddecbb2b7e1474708cc428f752f Mon Sep 17 00:00:00 2001 From: andyque Date: Fri, 9 May 2014 17:38:05 +0800 Subject: [PATCH] issue #5047, refactor LayoutParameterType --- cocos/ui/UIDeprecated.h | 4 ++++ cocos/ui/UILayout.cpp | 16 ++++++++-------- cocos/ui/UILayoutParameter.cpp | 2 +- cocos/ui/UILayoutParameter.h | 35 +++++++++++++++++++++++----------- cocos/ui/UIListView.cpp | 4 ++-- cocos/ui/UIWidget.cpp | 7 ++++--- cocos/ui/UIWidget.h | 2 +- 7 files changed, 44 insertions(+), 26 deletions(-) diff --git a/cocos/ui/UIDeprecated.h b/cocos/ui/UIDeprecated.h index 509b472248..d44f36c8ae 100644 --- a/cocos/ui/UIDeprecated.h +++ b/cocos/ui/UIDeprecated.h @@ -52,6 +52,9 @@ CC_DEPRECATED_ATTRIBUTE const Layout::BackGroundColorType LAYOUT_COLOR_NONE = La CC_DEPRECATED_ATTRIBUTE const Layout::BackGroundColorType LAYOUT_COLOR_SOLID = Layout::BackGroundColorType::SOLID; CC_DEPRECATED_ATTRIBUTE const Layout::BackGroundColorType LAYOUT_COLOR_GRADIENT = Layout::BackGroundColorType::GRADIENT; +CC_DEPRECATED_ATTRIBUTE const LayoutParameter::Type LAYOUT_PARAMETER_NONE = LayoutParameter::Type::NONE; +CC_DEPRECATED_ATTRIBUTE const LayoutParameter::Type LAYOUT_PARAMETER_LINEAR = LayoutParameter::Type::LINEAR; +CC_DEPRECATED_ATTRIBUTE const LayoutParameter::Type LAYOUT_PARAMETER_RELATIVE = LayoutParameter::Type::RELATIVE; @@ -63,6 +66,7 @@ CC_DEPRECATED_ATTRIBUTE typedef Widget::BrightStyle BrightStyle; CC_DEPRECATED_ATTRIBUTE typedef Layout::ClippingType LayoutClippingType; CC_DEPRECATED_ATTRIBUTE typedef Layout::LayoutType LayoutType; CC_DEPRECATED_ATTRIBUTE typedef Layout::BackGroundColorType LayoutBackGroundColorType; +CC_DEPRECATED_ATTRIBUTE typedef LayoutParameter::Type LayoutParameterType; } diff --git a/cocos/ui/UILayout.cpp b/cocos/ui/UILayout.cpp index 3de8a03df6..5440581334 100644 --- a/cocos/ui/UILayout.cpp +++ b/cocos/ui/UILayout.cpp @@ -130,7 +130,7 @@ void LinearVerticalLayoutExecutant::doLayout(const cocos2d::Size &layoutSize, Ve Widget* child = dynamic_cast(subWidget); if (child) { - LinearLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); + LinearLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LayoutParameter::Type::LINEAR)); if (layoutParameter) { @@ -171,7 +171,7 @@ void LinearHorizontalLayoutExecutant::doLayout(const cocos2d::Size &layoutSize, Widget* child = dynamic_cast(subWidget); if (child) { - LinearLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); + LinearLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LayoutParameter::Type::LINEAR)); if (layoutParameter) { LinearGravity childGravity = layoutParameter->getGravity(); @@ -212,7 +212,7 @@ void RelativeLayoutExecutant::doLayout(const cocos2d::Size &layoutSize, Vector(subWidget); if (child) { - RelativeLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)); + RelativeLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LayoutParameter::Type::RELATIVE)); layoutParameter->_put = false; unlayoutChildCount++; widgetChildren.pushBack(child); @@ -223,7 +223,7 @@ void RelativeLayoutExecutant::doLayout(const cocos2d::Size &layoutSize, Vector(subWidget); - RelativeLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)); + RelativeLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LayoutParameter::Type::RELATIVE)); if (layoutParameter) { @@ -245,7 +245,7 @@ void RelativeLayoutExecutant::doLayout(const cocos2d::Size &layoutSize, Vector(sWidget->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)); + RelativeLayoutParameter* rlayoutParameter = dynamic_cast(sWidget->getLayoutParameter(LayoutParameter::Type::RELATIVE)); if (rlayoutParameter && strcmp(rlayoutParameter->getRelativeName(), relativeName) == 0) { relativeWidget = sWidget; @@ -1180,7 +1180,7 @@ void Layout::supplyTheLayoutParameterLackToChild(Widget *child) case LayoutType::HORIZONTAL: case LayoutType::VERTICAL: { - LinearLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); + LinearLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LayoutParameter::Type::LINEAR)); if (!layoutParameter) { child->setLayoutParameter(LinearLayoutParameter::create()); @@ -1189,7 +1189,7 @@ void Layout::supplyTheLayoutParameterLackToChild(Widget *child) } case LayoutType::RELATIVE: { - RelativeLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)); + RelativeLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LayoutParameter::Type::RELATIVE)); if (!layoutParameter) { child->setLayoutParameter(RelativeLayoutParameter::create()); @@ -1557,7 +1557,7 @@ Size Layout::getLayoutContentSize()const if (w) { widgetCount++; - Margin m = w->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)->getMargin(); + Margin m = w->getLayoutParameter(LayoutParameter::Type::LINEAR)->getMargin(); layoutSize = layoutSize + w->getSize() + Size(m.right + m.left, m.top + m.bottom) * 0.5; } } diff --git a/cocos/ui/UILayoutParameter.cpp b/cocos/ui/UILayoutParameter.cpp index 5fd32c589a..ca2eb798df 100644 --- a/cocos/ui/UILayoutParameter.cpp +++ b/cocos/ui/UILayoutParameter.cpp @@ -83,7 +83,7 @@ const Margin& LayoutParameter::getMargin() const return _margin; } -LayoutParameterType LayoutParameter::getLayoutType() const +LayoutParameter::Type LayoutParameter::getLayoutType() const { return _layoutParameterType; } diff --git a/cocos/ui/UILayoutParameter.h b/cocos/ui/UILayoutParameter.h index 4f135cfd3c..8b48e2d23d 100644 --- a/cocos/ui/UILayoutParameter.h +++ b/cocos/ui/UILayoutParameter.h @@ -56,12 +56,6 @@ public: const Margin MarginZero = Margin(); -typedef enum -{ - LAYOUT_PARAMETER_NONE, - LAYOUT_PARAMETER_LINEAR, - LAYOUT_PARAMETER_RELATIVE -}LayoutParameterType; typedef enum { @@ -107,10 +101,19 @@ typedef enum class LayoutParameter : public Ref { public: + enum class Type : int + { + NONE = 0, + LINEAR, + RELATIVE + }; /** * Default constructor */ - LayoutParameter() : _margin(Margin()){_layoutParameterType = LAYOUT_PARAMETER_NONE;}; + LayoutParameter() : _margin(Margin()) + { + _layoutParameterType = Type::NONE; + }; /** * Default destructor @@ -148,14 +151,14 @@ public: * * @return LayoutParameterType */ - LayoutParameterType getLayoutType() const; + Type getLayoutType() const; LayoutParameter* clone(); virtual LayoutParameter* createCloneInstance(); virtual void copyProperties(LayoutParameter* model); protected: Margin _margin; - LayoutParameterType _layoutParameterType; + Type _layoutParameterType; }; /** * @js NA @@ -167,7 +170,10 @@ public: /** * Default constructor */ - LinearLayoutParameter() : _linearGravity(LINEAR_GRAVITY_NONE){_layoutParameterType = LAYOUT_PARAMETER_LINEAR;}; + LinearLayoutParameter() : _linearGravity(LINEAR_GRAVITY_NONE) + { + _layoutParameterType = Type::LINEAR; + }; /** * Default destructor @@ -213,7 +219,14 @@ public: /** * Default constructor */ - RelativeLayoutParameter() : _relativeAlign(RELATIVE_ALIGN_NONE),_relativeWidgetName(""),_relativeLayoutName(""),_put(false){_layoutParameterType = LAYOUT_PARAMETER_RELATIVE;}; + RelativeLayoutParameter() + : _relativeAlign(RELATIVE_ALIGN_NONE), + _relativeWidgetName(""), + _relativeLayoutName(""), + _put(false) + { + _layoutParameterType = Type::RELATIVE; + }; /** * Default destructor diff --git a/cocos/ui/UIListView.cpp b/cocos/ui/UIListView.cpp index 1edbd4270b..02a48d23bc 100644 --- a/cocos/ui/UIListView.cpp +++ b/cocos/ui/UIListView.cpp @@ -129,7 +129,7 @@ void ListView::remedyLayoutParameter(Widget *item) switch (_direction) { case SCROLLVIEW_DIR_VERTICAL: { - LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); + LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter(LayoutParameter::Type::LINEAR)); if (!llp) { LinearLayoutParameter* defaultLp = LinearLayoutParameter::create(); @@ -184,7 +184,7 @@ void ListView::remedyLayoutParameter(Widget *item) } case SCROLLVIEW_DIR_HORIZONTAL: { - LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); + LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter(LayoutParameter::Type::LINEAR)); if (!llp) { LinearLayoutParameter* defaultLp = LinearLayoutParameter::create(); diff --git a/cocos/ui/UIWidget.cpp b/cocos/ui/UIWidget.cpp index 61362399c3..bd3d4c7e27 100644 --- a/cocos/ui/UIWidget.cpp +++ b/cocos/ui/UIWidget.cpp @@ -845,12 +845,12 @@ void Widget::setLayoutParameter(LayoutParameter *parameter) { return; } - _layoutParameterDictionary.insert(parameter->getLayoutType(), parameter); + _layoutParameterDictionary.insert((int)parameter->getLayoutType(), parameter); } -LayoutParameter* Widget::getLayoutParameter(LayoutParameterType type) +LayoutParameter* Widget::getLayoutParameter(LayoutParameter::Type type) { - return dynamic_cast(_layoutParameterDictionary.at(type)); + return dynamic_cast(_layoutParameterDictionary.at((int)type)); } std::string Widget::getDescription() const @@ -920,6 +920,7 @@ void Widget::copyProperties(Widget *widget) setFlippedY(widget->isFlippedY()); setColor(widget->getColor()); setOpacity(widget->getOpacity()); + //FIXME:copy focus properties, also make sure all the subclass the copy behavior is correct Map& layoutParameterDic = widget->_layoutParameterDictionary; for (auto iter = layoutParameterDic.begin(); iter != layoutParameterDic.end(); ++iter) { diff --git a/cocos/ui/UIWidget.h b/cocos/ui/UIWidget.h index b304ecc123..7e2a2ee058 100644 --- a/cocos/ui/UIWidget.h +++ b/cocos/ui/UIWidget.h @@ -490,7 +490,7 @@ public: * * @return LayoutParameter */ - LayoutParameter* getLayoutParameter(LayoutParameterType type); + LayoutParameter* getLayoutParameter(LayoutParameter::Type type); /** * Ignore the widget size