issue #5183, refator LayoutExecutant

This commit is contained in:
andyque 2014-05-14 18:24:23 +08:00
parent 8c8f9611f7
commit 967c071c68
5 changed files with 67 additions and 58 deletions

View File

@ -1039,10 +1039,10 @@
2905FA8D18CF08D100240AA3 /* UIWidget.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2905FA1318CF08D100240AA3 /* UIWidget.cpp */; };
2905FA8E18CF08D100240AA3 /* UIWidget.h in Headers */ = {isa = PBXBuildFile; fileRef = 2905FA1418CF08D100240AA3 /* UIWidget.h */; };
2905FA8F18CF08D100240AA3 /* UIWidget.h in Headers */ = {isa = PBXBuildFile; fileRef = 2905FA1418CF08D100240AA3 /* UIWidget.h */; };
2928C2001923756F00E77353 /* CCLayoutExecutant.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2928C1FE1923756F00E77353 /* CCLayoutExecutant.cpp */; };
2928C2011923756F00E77353 /* CCLayoutExecutant.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2928C1FE1923756F00E77353 /* CCLayoutExecutant.cpp */; };
2928C2021923756F00E77353 /* CCLayoutExecutant.h in Headers */ = {isa = PBXBuildFile; fileRef = 2928C1FF1923756F00E77353 /* CCLayoutExecutant.h */; };
2928C2031923756F00E77353 /* CCLayoutExecutant.h in Headers */ = {isa = PBXBuildFile; fileRef = 2928C1FF1923756F00E77353 /* CCLayoutExecutant.h */; };
2928C2001923756F00E77353 /* UILayoutExecutant.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2928C1FE1923756F00E77353 /* UILayoutExecutant.cpp */; };
2928C2011923756F00E77353 /* UILayoutExecutant.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2928C1FE1923756F00E77353 /* UILayoutExecutant.cpp */; };
2928C2021923756F00E77353 /* UILayoutExecutant.h in Headers */ = {isa = PBXBuildFile; fileRef = 2928C1FF1923756F00E77353 /* UILayoutExecutant.h */; };
2928C2031923756F00E77353 /* UILayoutExecutant.h in Headers */ = {isa = PBXBuildFile; fileRef = 2928C1FF1923756F00E77353 /* UILayoutExecutant.h */; };
296CAD221915EC8000C64FBF /* CCEventFocus.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 296CAD201915EC8000C64FBF /* CCEventFocus.cpp */; };
296CAD231915EC8000C64FBF /* CCEventFocus.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 296CAD201915EC8000C64FBF /* CCEventFocus.cpp */; };
296CAD241915EC8000C64FBF /* CCEventFocus.h in Headers */ = {isa = PBXBuildFile; fileRef = 296CAD211915EC8000C64FBF /* CCEventFocus.h */; };
@ -2269,8 +2269,8 @@
2905FA1318CF08D100240AA3 /* UIWidget.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UIWidget.cpp; sourceTree = "<group>"; };
2905FA1418CF08D100240AA3 /* UIWidget.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIWidget.h; sourceTree = "<group>"; };
29080DEB191B82CE0066F8DF /* UIDeprecated.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UIDeprecated.h; sourceTree = "<group>"; };
2928C1FE1923756F00E77353 /* CCLayoutExecutant.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCLayoutExecutant.cpp; sourceTree = "<group>"; };
2928C1FF1923756F00E77353 /* CCLayoutExecutant.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCLayoutExecutant.h; sourceTree = "<group>"; };
2928C1FE1923756F00E77353 /* UILayoutExecutant.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UILayoutExecutant.cpp; sourceTree = "<group>"; };
2928C1FF1923756F00E77353 /* UILayoutExecutant.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UILayoutExecutant.h; sourceTree = "<group>"; };
296CAD201915EC8000C64FBF /* CCEventFocus.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCEventFocus.cpp; path = ../base/CCEventFocus.cpp; sourceTree = "<group>"; };
296CAD211915EC8000C64FBF /* CCEventFocus.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCEventFocus.h; path = ../base/CCEventFocus.h; sourceTree = "<group>"; };
296CAD261915EC9900C64FBF /* CCEventListenerFocus.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCEventListenerFocus.cpp; path = ../base/CCEventListenerFocus.cpp; sourceTree = "<group>"; };
@ -4024,8 +4024,8 @@
2905F9F918CF08D000240AA3 /* UILayout.h */,
2905F9FC18CF08D000240AA3 /* UILayoutParameter.cpp */,
2905F9FD18CF08D000240AA3 /* UILayoutParameter.h */,
2928C1FE1923756F00E77353 /* CCLayoutExecutant.cpp */,
2928C1FF1923756F00E77353 /* CCLayoutExecutant.h */,
2928C1FE1923756F00E77353 /* UILayoutExecutant.cpp */,
2928C1FF1923756F00E77353 /* UILayoutExecutant.h */,
);
name = Layout;
sourceTree = "<group>";
@ -4947,7 +4947,7 @@
2905FA6018CF08D100240AA3 /* UILayoutParameter.h in Headers */,
1ABA68B01888D700007D1BB4 /* CCFontCharMap.h in Headers */,
5034CA3F191D591100CE6051 /* ccShader_Position_uColor.vert in Headers */,
2928C2021923756F00E77353 /* CCLayoutExecutant.h in Headers */,
2928C2021923756F00E77353 /* UILayoutExecutant.h in Headers */,
500DC8B419105D41007B91BF /* CCGroupCommand.h in Headers */,
500DC8D319105F7D007B91BF /* CCAffineTransform.h in Headers */,
46A170191807CBFC005B8026 /* CCCommon.h in Headers */,
@ -5546,7 +5546,7 @@
1A5701FE180BCBAD0088DEC7 /* CCMenuItem.h in Headers */,
1A570205180BCBD40088DEC7 /* CCClippingNode.h in Headers */,
5034CA34191D591100CE6051 /* ccShader_PositionTexture_uColor.frag in Headers */,
2928C2031923756F00E77353 /* CCLayoutExecutant.h in Headers */,
2928C2031923756F00E77353 /* UILayoutExecutant.h in Headers */,
1A57020B180BCBDF0088DEC7 /* CCMotionStreak.h in Headers */,
1A570213180BCBF40088DEC7 /* CCProgressTimer.h in Headers */,
B37510821823ACA100B3BA6A /* CCPhysicsJointInfo_chipmunk.h in Headers */,
@ -6354,7 +6354,7 @@
1AAF536A180E3374000584C8 /* HttpClient.cpp in Sources */,
1AAF5372180E3374000584C8 /* SocketIO.cpp in Sources */,
1AAF5376180E3374000584C8 /* WebSocket.cpp in Sources */,
2928C2001923756F00E77353 /* CCLayoutExecutant.cpp in Sources */,
2928C2001923756F00E77353 /* UILayoutExecutant.cpp in Sources */,
1A01C69818F57BE800EFE3A6 /* CCSet.cpp in Sources */,
1AAF584F180E40B9000584C8 /* LocalStorage.cpp in Sources */,
1AAF5853180E40B9000584C8 /* LocalStorageAndroid.cpp in Sources */,
@ -6557,7 +6557,7 @@
296CAD291915EC9900C64FBF /* CCEventListenerFocus.cpp in Sources */,
1A01C69918F57BE800EFE3A6 /* CCSet.cpp in Sources */,
1A01C69D18F57BE800EFE3A6 /* CCString.cpp in Sources */,
2928C2011923756F00E77353 /* CCLayoutExecutant.cpp in Sources */,
2928C2011923756F00E77353 /* UILayoutExecutant.cpp in Sources */,
2986667F18B1B246000E39CA /* CCTweenFunction.cpp in Sources */,
2AC795E318628746005EC8E1 /* SkeletonBounds.cpp in Sources */,
2AC795E418628746005EC8E1 /* Event.cpp in Sources */,

View File

@ -32,6 +32,7 @@ THE SOFTWARE.
#include "renderer/CCRenderer.h"
#include "renderer/CCGroupCommand.h"
#include "renderer/CCCustomCommand.h"
#include "ui/UILayoutExecutant.h"
NS_CC_BEGIN
@ -83,7 +84,6 @@ _currentAlphaTestFunc(GL_ALWAYS),
_currentAlphaTestRef(1),
_backGroundImageColor(Color3B::WHITE),
_backGroundImageOpacity(255),
_curLayoutExecutant(nullptr),
_passFocusToChild(true),
_loopFocus(false)
{
@ -94,7 +94,6 @@ _loopFocus(false)
Layout::~Layout()
{
CC_SAFE_RELEASE(_clippingStencil);
CC_SAFE_RELEASE(_curLayoutExecutant);
}
void Layout::onEnter()
@ -906,9 +905,7 @@ const Size& Layout::getBackGroundImageTextureSize() const
void Layout::setLayoutType(Type type)
{
_layoutType = type;
CC_SAFE_RELEASE_NULL(_curLayoutExecutant);
_curLayoutExecutant = createCurrentLayoutExecutant();
CC_SAFE_RETAIN(_curLayoutExecutant);
for (auto& child : _children)
{
Widget* widgetChild = dynamic_cast<Widget*>(child);
@ -920,25 +917,7 @@ void Layout::setLayoutType(Type type)
_doLayoutDirty = true;
}
LayoutExecutant* Layout::createCurrentLayoutExecutant()
{
LayoutExecutant* exe = nullptr;
switch (_layoutType)
{
case Type::VERTICAL:
exe = LinearVerticalLayoutExecutant::create();
break;
case Type::HORIZONTAL:
exe = LinearHorizontalLayoutExecutant::create();
break;
case Type::RELATIVE:
exe = RelativeLayoutExecutant::create();
break;
default:
break;
}
return exe;
}
Layout::Type Layout::getLayoutType() const
{
@ -956,10 +935,12 @@ void Layout::doLayout()
{
return;
}
if (_curLayoutExecutant)
LayoutExecutant* executant = LayoutExecutantFactory::getInstance()->createExecutant(_layoutType);
if (executant)
{
_curLayoutExecutant->doLayout(getSize(), getChildren());
executant->doLayout(getSize(), getChildren());
}
_doLayoutDirty = false;
}

View File

@ -35,7 +35,6 @@ namespace ui {
* @js NA
* @lua NA
*/
class LayoutExecutant;
class Layout : public Widget
{
@ -313,6 +312,7 @@ protected:
void setStencilClippingSize(const Size& size);
const Rect& getClippingRect();
virtual void doLayout();
//clipping
@ -325,7 +325,6 @@ protected:
void updateBackGroundImageColor();
void updateBackGroundImageOpacity();
void updateBackGroundImageRGBA();
LayoutExecutant* createCurrentLayoutExecutant();
/**
*get the content size of the layout, it will accumulate all its children's content size
@ -477,8 +476,6 @@ protected:
Color3B _backGroundImageColor;
GLubyte _backGroundImageOpacity;
LayoutExecutant* _curLayoutExecutant;
GLint _mask_layer_le;
GroupCommand _groupCommand;
CustomCommand _beforeVisitCmdStencil;

View File

@ -22,24 +22,12 @@
THE SOFTWARE.
****************************************************************************/
#include "CCLayoutExecutant.h"
#include "UILayoutExecutant.h"
#include "UIWidget.h"
NS_CC_BEGIN
namespace ui {
LayoutExecutant* LayoutExecutant::create()
{
LayoutExecutant* exe = new LayoutExecutant();
if (exe)
{
exe->autorelease();
return exe;
}
CC_SAFE_DELETE(exe);
return nullptr;
}
LinearVerticalLayoutExecutant* LinearVerticalLayoutExecutant::create()
{
@ -514,6 +502,37 @@ void RelativeLayoutExecutant::doLayout(const cocos2d::Size &layoutSize, Vector<c
widgetChildren.clear();
}
////////////////////////////////////////////////////////////////////////////
LayoutExecutantFactory* LayoutExecutantFactory::_instance = nullptr;
LayoutExecutantFactory* LayoutExecutantFactory::getInstance()
{
if (nullptr == _instance) {
_instance = new LayoutExecutantFactory;
}
return _instance;
}
LayoutExecutant* LayoutExecutantFactory::createExecutant(Layout::Type type)
{
LayoutExecutant* exe = nullptr;
switch (type)
{
case Layout::Type::VERTICAL:
exe = LinearVerticalLayoutExecutant::create();
break;
case Layout::Type::HORIZONTAL:
exe = LinearHorizontalLayoutExecutant::create();
break;
case Layout::Type::RELATIVE:
exe = RelativeLayoutExecutant::create();
break;
default:
break;
}
return exe;
}
}
NS_CC_END

View File

@ -25,7 +25,7 @@
#ifndef __cocos2d_libs__CCLayoutExecutant__
#define __cocos2d_libs__CCLayoutExecutant__
#include "2d/CCNode.h"
#include "UILayout.h"
NS_CC_BEGIN
@ -36,8 +36,8 @@ class LayoutExecutant : public Ref
public:
LayoutExecutant(){};
virtual ~LayoutExecutant(){};
static LayoutExecutant* create();
virtual void doLayout(const Size& layoutSize, Vector<Node*> container){};
virtual void doLayout(const Size& layoutSize, Vector<Node*> container) = 0;
};
class LinearVerticalLayoutExecutant : public LayoutExecutant
@ -67,6 +67,18 @@ public:
virtual void doLayout(const Size& layoutSize, Vector<Node*> container);
};
class LayoutExecutantFactory
{
public:
static LayoutExecutantFactory* getInstance();
LayoutExecutant* createExecutant(Layout::Type type);
private:
LayoutExecutantFactory(){}
CC_DISALLOW_COPY_AND_ASSIGN(LayoutExecutantFactory);
static LayoutExecutantFactory *_instance;
};
}