Revert "Revert "Merge pull request #6752 from andyque/refactorLayout""

This reverts commit 1ed9b9749e.
This commit is contained in:
andyque 2014-05-20 16:17:10 +08:00
parent 47482f8a3a
commit b07e82f7c3
17 changed files with 1163 additions and 955 deletions

View File

@ -944,8 +944,6 @@
2905FA6F18CF08D100240AA3 /* UIRichText.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2905FA0418CF08D000240AA3 /* UIRichText.cpp */; };
2905FA7018CF08D100240AA3 /* UIRichText.h in Headers */ = {isa = PBXBuildFile; fileRef = 2905FA0518CF08D000240AA3 /* UIRichText.h */; };
2905FA7118CF08D100240AA3 /* UIRichText.h in Headers */ = {isa = PBXBuildFile; fileRef = 2905FA0518CF08D000240AA3 /* UIRichText.h */; };
2905FA7218CF08D100240AA3 /* UIScrollInterface.h in Headers */ = {isa = PBXBuildFile; fileRef = 2905FA0618CF08D000240AA3 /* UIScrollInterface.h */; };
2905FA7318CF08D100240AA3 /* UIScrollInterface.h in Headers */ = {isa = PBXBuildFile; fileRef = 2905FA0618CF08D000240AA3 /* UIScrollInterface.h */; };
2905FA7418CF08D100240AA3 /* UIScrollView.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2905FA0718CF08D000240AA3 /* UIScrollView.cpp */; };
2905FA7518CF08D100240AA3 /* UIScrollView.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2905FA0718CF08D000240AA3 /* UIScrollView.cpp */; };
2905FA7618CF08D100240AA3 /* UIScrollView.h in Headers */ = {isa = PBXBuildFile; fileRef = 2905FA0818CF08D000240AA3 /* UIScrollView.h */; };
@ -975,6 +973,12 @@
2905FA8E18CF08D100240AA3 /* UIWidget.h in Headers */ = {isa = PBXBuildFile; fileRef = 2905FA1418CF08D100240AA3 /* UIWidget.h */; };
2905FA8F18CF08D100240AA3 /* UIWidget.h in Headers */ = {isa = PBXBuildFile; fileRef = 2905FA1418CF08D100240AA3 /* UIWidget.h */; };
2986667F18B1B246000E39CA /* CCTweenFunction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2986667818B1B079000E39CA /* CCTweenFunction.cpp */; };
29CB8F4C1929D1BB00C841D6 /* UILayoutExecutant.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 29CB8F4A1929D1BB00C841D6 /* UILayoutExecutant.cpp */; };
29CB8F4D1929D1BB00C841D6 /* UILayoutExecutant.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 29CB8F4A1929D1BB00C841D6 /* UILayoutExecutant.cpp */; };
29CB8F4E1929D1BB00C841D6 /* UILayoutExecutant.h in Headers */ = {isa = PBXBuildFile; fileRef = 29CB8F4B1929D1BB00C841D6 /* UILayoutExecutant.h */; };
29CB8F4F1929D1BB00C841D6 /* UILayoutExecutant.h in Headers */ = {isa = PBXBuildFile; fileRef = 29CB8F4B1929D1BB00C841D6 /* UILayoutExecutant.h */; };
29CB8F551929D7A900C841D6 /* UIScrollInterface.h in Headers */ = {isa = PBXBuildFile; fileRef = 29CB8F541929D7A900C841D6 /* UIScrollInterface.h */; };
29CB8F561929D7A900C841D6 /* UIScrollInterface.h in Headers */ = {isa = PBXBuildFile; fileRef = 29CB8F541929D7A900C841D6 /* UIScrollInterface.h */; };
2AC795DB1862870F005EC8E1 /* SkeletonBounds.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2AC795D918628689005EC8E1 /* SkeletonBounds.cpp */; };
2AC795DC1862870F005EC8E1 /* Event.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2AC795D51862867D005EC8E1 /* Event.cpp */; };
2AC795DD1862870F005EC8E1 /* EventData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2AC795D71862867D005EC8E1 /* EventData.cpp */; };
@ -2235,7 +2239,6 @@
2905FA0318CF08D000240AA3 /* UIPageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIPageView.h; sourceTree = "<group>"; };
2905FA0418CF08D000240AA3 /* UIRichText.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UIRichText.cpp; sourceTree = "<group>"; };
2905FA0518CF08D000240AA3 /* UIRichText.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIRichText.h; sourceTree = "<group>"; };
2905FA0618CF08D000240AA3 /* UIScrollInterface.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIScrollInterface.h; sourceTree = "<group>"; };
2905FA0718CF08D000240AA3 /* UIScrollView.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UIScrollView.cpp; sourceTree = "<group>"; };
2905FA0818CF08D000240AA3 /* UIScrollView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIScrollView.h; sourceTree = "<group>"; };
2905FA0918CF08D000240AA3 /* UISlider.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UISlider.cpp; sourceTree = "<group>"; };
@ -2253,6 +2256,9 @@
29080DEB191B82CE0066F8DF /* UIDeprecated.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UIDeprecated.h; sourceTree = "<group>"; };
2986667818B1B079000E39CA /* CCTweenFunction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCTweenFunction.cpp; sourceTree = "<group>"; };
2986667918B1B079000E39CA /* CCTweenFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCTweenFunction.h; sourceTree = "<group>"; };
29CB8F4A1929D1BB00C841D6 /* UILayoutExecutant.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UILayoutExecutant.cpp; sourceTree = "<group>"; };
29CB8F4B1929D1BB00C841D6 /* UILayoutExecutant.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UILayoutExecutant.h; sourceTree = "<group>"; };
29CB8F541929D7A900C841D6 /* UIScrollInterface.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIScrollInterface.h; sourceTree = "<group>"; };
2AC795D318628672005EC8E1 /* BoundingBoxAttachment.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BoundingBoxAttachment.cpp; sourceTree = "<group>"; };
2AC795D418628672005EC8E1 /* BoundingBoxAttachment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BoundingBoxAttachment.h; sourceTree = "<group>"; };
2AC795D51862867D005EC8E1 /* Event.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Event.cpp; sourceTree = "<group>"; };
@ -3902,23 +3908,63 @@
2905F9E618CF08D000240AA3 /* ui */ = {
isa = PBXGroup;
children = (
3EA0FB69191C841D00B170C8 /* UIVideoPlayer.h */,
3EA0FB6A191C841D00B170C8 /* UIVideoPlayerIOS.mm */,
29CB8F531929D67D00C841D6 /* widgets */,
29CB8F521929D65500C841D6 /* experimental */,
29CB8F511929D64500C841D6 /* base */,
29CB8F501929D63600C841D6 /* layout */,
2905F9E918CF08D000240AA3 /* CocosGUI.cpp */,
2905F9EA18CF08D000240AA3 /* CocosGUI.h */,
2905F9EB18CF08D000240AA3 /* GUIDefine.h */,
2905F9F018CF08D000240AA3 /* UIButton.cpp */,
2905F9F118CF08D000240AA3 /* UIButton.h */,
2905F9F218CF08D000240AA3 /* UICheckBox.cpp */,
2905F9F318CF08D000240AA3 /* UICheckBox.h */,
2905F9F418CF08D000240AA3 /* UIHelper.cpp */,
2905F9F518CF08D000240AA3 /* UIHelper.h */,
2905F9F618CF08D000240AA3 /* UIImageView.cpp */,
2905F9F718CF08D000240AA3 /* UIImageView.h */,
);
name = ui;
path = ../cocos/ui;
sourceTree = "<group>";
};
29CB8F501929D63600C841D6 /* layout */ = {
isa = PBXGroup;
children = (
50E6D32E18E174130051CA34 /* UIHBox.cpp */,
50E6D32F18E174130051CA34 /* UIHBox.h */,
50E6D33018E174130051CA34 /* UIRelativeBox.cpp */,
50E6D33118E174130051CA34 /* UIRelativeBox.h */,
50E6D33218E174130051CA34 /* UIVBox.cpp */,
50E6D33318E174130051CA34 /* UIVBox.h */,
2905F9F818CF08D000240AA3 /* UILayout.cpp */,
2905F9F918CF08D000240AA3 /* UILayout.h */,
2905F9FC18CF08D000240AA3 /* UILayoutParameter.cpp */,
2905F9FD18CF08D000240AA3 /* UILayoutParameter.h */,
29CB8F4A1929D1BB00C841D6 /* UILayoutExecutant.cpp */,
29CB8F4B1929D1BB00C841D6 /* UILayoutExecutant.h */,
);
name = layout;
sourceTree = "<group>";
};
29CB8F511929D64500C841D6 /* base */ = {
isa = PBXGroup;
children = (
29080DEB191B82CE0066F8DF /* UIDeprecated.h */,
2905FA1318CF08D100240AA3 /* UIWidget.cpp */,
2905FA1418CF08D100240AA3 /* UIWidget.h */,
50E6D30C18DADB5D0051CA34 /* CCProtectedNode.cpp */,
50E6D30D18DADB5D0051CA34 /* CCProtectedNode.h */,
2905F9F418CF08D000240AA3 /* UIHelper.cpp */,
2905F9F518CF08D000240AA3 /* UIHelper.h */,
2905F9EB18CF08D000240AA3 /* GUIDefine.h */,
);
name = base;
sourceTree = "<group>";
};
29CB8F521929D65500C841D6 /* experimental */ = {
isa = PBXGroup;
children = (
3EA0FB69191C841D00B170C8 /* UIVideoPlayer.h */,
3EA0FB6A191C841D00B170C8 /* UIVideoPlayerIOS.mm */,
);
name = experimental;
sourceTree = "<group>";
};
29CB8F531929D67D00C841D6 /* widgets */ = {
isa = PBXGroup;
children = (
2905F9FE18CF08D000240AA3 /* UIListView.cpp */,
2905F9FF18CF08D000240AA3 /* UIListView.h */,
2905FA0018CF08D000240AA3 /* UILoadingBar.cpp */,
@ -3927,9 +3973,9 @@
2905FA0318CF08D000240AA3 /* UIPageView.h */,
2905FA0418CF08D000240AA3 /* UIRichText.cpp */,
2905FA0518CF08D000240AA3 /* UIRichText.h */,
2905FA0618CF08D000240AA3 /* UIScrollInterface.h */,
2905FA0718CF08D000240AA3 /* UIScrollView.cpp */,
2905FA0818CF08D000240AA3 /* UIScrollView.h */,
29CB8F541929D7A900C841D6 /* UIScrollInterface.h */,
2905FA0918CF08D000240AA3 /* UISlider.cpp */,
2905FA0A18CF08D100240AA3 /* UISlider.h */,
2905FA0B18CF08D100240AA3 /* UIText.cpp */,
@ -3940,20 +3986,14 @@
2905FA1018CF08D100240AA3 /* UITextBMFont.h */,
2905FA1118CF08D100240AA3 /* UITextField.cpp */,
2905FA1218CF08D100240AA3 /* UITextField.h */,
2905FA1318CF08D100240AA3 /* UIWidget.cpp */,
2905FA1418CF08D100240AA3 /* UIWidget.h */,
50E6D30C18DADB5D0051CA34 /* CCProtectedNode.cpp */,
50E6D30D18DADB5D0051CA34 /* CCProtectedNode.h */,
50E6D32E18E174130051CA34 /* UIHBox.cpp */,
50E6D32F18E174130051CA34 /* UIHBox.h */,
50E6D33018E174130051CA34 /* UIRelativeBox.cpp */,
50E6D33118E174130051CA34 /* UIRelativeBox.h */,
50E6D33218E174130051CA34 /* UIVBox.cpp */,
50E6D33318E174130051CA34 /* UIVBox.h */,
29080DEB191B82CE0066F8DF /* UIDeprecated.h */,
2905F9F018CF08D000240AA3 /* UIButton.cpp */,
2905F9F118CF08D000240AA3 /* UIButton.h */,
2905F9F218CF08D000240AA3 /* UICheckBox.cpp */,
2905F9F318CF08D000240AA3 /* UICheckBox.h */,
2905F9F618CF08D000240AA3 /* UIImageView.cpp */,
2905F9F718CF08D000240AA3 /* UIImageView.h */,
);
name = ui;
path = ../cocos/ui;
name = widgets;
sourceTree = "<group>";
};
37936A361869B76800E974DD /* internal */ = {
@ -4806,6 +4846,7 @@
5034CA39191D591100CE6051 /* ccShader_PositionColorLengthTexture.frag in Headers */,
2AC795DF18628723005EC8E1 /* SkeletonBounds.h in Headers */,
50ABBE891925AB6F00A911A9 /* CCMap.h in Headers */,
29CB8F4E1929D1BB00C841D6 /* UILayoutExecutant.h in Headers */,
2AC795E018628723005EC8E1 /* Event.h in Headers */,
2AC795E118628723005EC8E1 /* EventData.h in Headers */,
50E6D33618E174130051CA34 /* UIHBox.h in Headers */,
@ -5010,7 +5051,6 @@
1AD71DED180E26E600808F54 /* CCMenuLoader.h in Headers */,
50ABBE8F1925AB6F00A911A9 /* CCPlatformConfig.h in Headers */,
1AD71DF1180E26E600808F54 /* CCNode+CCBRelativePositioning.h in Headers */,
2905FA7218CF08D100240AA3 /* UIScrollInterface.h in Headers */,
1AD71DF5180E26E600808F54 /* CCNodeLoader.h in Headers */,
1AD71DF9180E26E600808F54 /* CCNodeLoaderLibrary.h in Headers */,
50ABBE291925AB6F00A911A9 /* CCAutoreleasePool.h in Headers */,
@ -5075,6 +5115,7 @@
50ABBD9D1925AB4100A911A9 /* ccGLStateCache.h in Headers */,
50ABBEB91925AB6F00A911A9 /* ccUTF8.h in Headers */,
1AAF536E180E3374000584C8 /* HttpRequest.h in Headers */,
29CB8F551929D7A900C841D6 /* UIScrollInterface.h in Headers */,
1AAF5370180E3374000584C8 /* HttpResponse.h in Headers */,
50ABBE671925AB6F00A911A9 /* CCEventListenerCustom.h in Headers */,
1AAF5374180E3374000584C8 /* SocketIO.h in Headers */,
@ -5357,7 +5398,6 @@
503DD8E41926736A00CD74DD /* CCDirectorCaller.h in Headers */,
50ABBD8A1925AB4100A911A9 /* CCCustomCommand.h in Headers */,
2905FA6D18CF08D100240AA3 /* UIPageView.h in Headers */,
2905FA7318CF08D100240AA3 /* UIScrollInterface.h in Headers */,
50ABBE881925AB6F00A911A9 /* ccMacros.h in Headers */,
50ABC0101926664800A911A9 /* CCFileUtils.h in Headers */,
2905FA4318CF08D100240AA3 /* CocosGUI.h in Headers */,
@ -5528,6 +5568,7 @@
50ABBD921925AB4100A911A9 /* CCGLProgramCache.h in Headers */,
1AD71E0E180E26E600808F54 /* CocosBuilder.h in Headers */,
B29594C91926D61F003EEF37 /* CCObjLoader.h in Headers */,
29CB8F561929D7A900C841D6 /* UIScrollInterface.h in Headers */,
1AD71E98180E26E600808F54 /* Animation.h in Headers */,
1AD71E9C180E26E600808F54 /* AnimationState.h in Headers */,
50ABBE961925AB6F00A911A9 /* CCProfiling.h in Headers */,
@ -5620,6 +5661,7 @@
1A8C59E2180E930E00EF57C3 /* CCInputDelegate.h in Headers */,
503DD8EA1926736A00CD74DD /* CCESRenderer.h in Headers */,
1A8C59E6180E930E00EF57C3 /* CCProcessBase.h in Headers */,
29CB8F4F1929D1BB00C841D6 /* UILayoutExecutant.h in Headers */,
50ABBE581925AB6F00A911A9 /* CCEventFocus.h in Headers */,
50ABC00A1926664800A911A9 /* CCCommon.h in Headers */,
2905FA5918CF08D100240AA3 /* UILayout.h in Headers */,
@ -6200,6 +6242,7 @@
2905FA4018CF08D100240AA3 /* CocosGUI.cpp in Sources */,
1AD71EC1180E26E600808F54 /* extension.cpp in Sources */,
50ABBE511925AB6F00A911A9 /* CCEventDispatcher.cpp in Sources */,
29CB8F4C1929D1BB00C841D6 /* UILayoutExecutant.cpp in Sources */,
50ABC0051926664800A911A9 /* CCThread.mm in Sources */,
1AD71EC5180E26E600808F54 /* Json.cpp in Sources */,
50E6D33C18E174130051CA34 /* UIVBox.cpp in Sources */,
@ -6678,6 +6721,7 @@
50FCEBAC18C72017004AD434 /* PageViewReader.cpp in Sources */,
1A8C598C180E930E00EF57C3 /* CCActionFrame.cpp in Sources */,
1A8C5990180E930E00EF57C3 /* CCActionFrameEasing.cpp in Sources */,
29CB8F4D1929D1BB00C841D6 /* UILayoutExecutant.cpp in Sources */,
50FCEBA418C72017004AD434 /* ListViewReader.cpp in Sources */,
1A8C5994180E930E00EF57C3 /* CCActionManagerEx.cpp in Sources */,
50ABBEBC1925AB6F00A911A9 /* ccUtils.cpp in Sources */,

View File

@ -9,6 +9,7 @@ LOCAL_SRC_FILES := \
UIWidget.cpp \
UILayout.cpp \
UILayoutParameter.cpp \
UILayoutExecutant.cpp \
CocosGUI.cpp \
UIHelper.cpp \
UIListView.cpp \

View File

@ -8,6 +8,7 @@ set(COCOS_UI_SRC
ui/UIImageView.cpp
ui/UILayout.cpp
ui/UILayoutParameter.cpp
ui/UILayoutExecutant.cpp
ui/UIListView.cpp
ui/UILoadingBar.cpp
ui/UIPageView.cpp

View File

@ -32,532 +32,12 @@ THE SOFTWARE.
#include "renderer/CCRenderer.h"
#include "renderer/CCGroupCommand.h"
#include "renderer/CCCustomCommand.h"
#include "ui/UILayoutExecutant.h"
NS_CC_BEGIN
namespace ui {
class LayoutExecutant : public Ref
{
public:
LayoutExecutant(){};
virtual ~LayoutExecutant(){};
static LayoutExecutant* create();
virtual void doLayout(const Size& layoutSize, Vector<Node*> container){};
};
class LinearVerticalLayoutExecutant : public LayoutExecutant
{
public:
LinearVerticalLayoutExecutant(){};
virtual ~LinearVerticalLayoutExecutant(){};
static LinearVerticalLayoutExecutant* create();
virtual void doLayout(const Size& layoutSize, Vector<Node*> container);
};
class LinearHorizontalLayoutExecutant : public LayoutExecutant
{
public:
LinearHorizontalLayoutExecutant(){};
virtual ~LinearHorizontalLayoutExecutant(){};
static LinearHorizontalLayoutExecutant* create();
virtual void doLayout(const Size& layoutSize, Vector<Node*> container);
};
class RelativeLayoutExecutant : public LayoutExecutant
{
public:
RelativeLayoutExecutant(){};
virtual ~RelativeLayoutExecutant(){};
static RelativeLayoutExecutant* create();
virtual void doLayout(const Size& layoutSize, Vector<Node*> container);
};
LayoutExecutant* LayoutExecutant::create()
{
LayoutExecutant* exe = new LayoutExecutant();
if (exe)
{
exe->autorelease();
return exe;
}
CC_SAFE_DELETE(exe);
return nullptr;
}
LinearVerticalLayoutExecutant* LinearVerticalLayoutExecutant::create()
{
LinearVerticalLayoutExecutant* exe = new LinearVerticalLayoutExecutant();
if (exe)
{
exe->autorelease();
return exe;
}
CC_SAFE_DELETE(exe);
return nullptr;
}
LinearHorizontalLayoutExecutant* LinearHorizontalLayoutExecutant::create()
{
LinearHorizontalLayoutExecutant* exe = new LinearHorizontalLayoutExecutant();
if (exe)
{
exe->autorelease();
return exe;
}
CC_SAFE_DELETE(exe);
return nullptr;
}
RelativeLayoutExecutant* RelativeLayoutExecutant::create()
{
RelativeLayoutExecutant* exe = new RelativeLayoutExecutant();
if (exe)
{
exe->autorelease();
return exe;
}
CC_SAFE_DELETE(exe);
return nullptr;
}
void LinearVerticalLayoutExecutant::doLayout(const cocos2d::Size &layoutSize, Vector<cocos2d::Node *> container)
{
float topBoundary = layoutSize.height;
for (auto& subWidget : container)
{
Widget* child = dynamic_cast<Widget*>(subWidget);
if (child)
{
LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter(LayoutParameter::Type::LINEAR));
if (layoutParameter)
{
LinearLayoutParameter::LinearGravity childGravity = layoutParameter->getGravity();
Vec2 ap = child->getAnchorPoint();
Size cs = child->getSize();
float finalPosX = ap.x * cs.width;
float finalPosY = topBoundary - ((1.0f-ap.y) * cs.height);
switch (childGravity)
{
case LinearLayoutParameter::LinearGravity::NONE:
case LinearLayoutParameter::LinearGravity::LEFT:
break;
case LinearLayoutParameter::LinearGravity::RIGHT:
finalPosX = layoutSize.width - ((1.0f - ap.x) * cs.width);
break;
case LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL:
finalPosX = layoutSize.width / 2.0f - cs.width * (0.5f-ap.x);
break;
default:
break;
}
Margin mg = layoutParameter->getMargin();
finalPosX += mg.left;
finalPosY -= mg.top;
child->setPosition(Vec2(finalPosX, finalPosY));
topBoundary = child->getBottomInParent() - mg.bottom;
}
}
}
}
void LinearHorizontalLayoutExecutant::doLayout(const cocos2d::Size &layoutSize, Vector<cocos2d::Node *> container)
{
float leftBoundary = 0.0f;
for (auto& subWidget : container)
{
Widget* child = dynamic_cast<Widget*>(subWidget);
if (child)
{
LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter(LayoutParameter::Type::LINEAR));
if (layoutParameter)
{
LinearLayoutParameter::LinearGravity childGravity = layoutParameter->getGravity();
Vec2 ap = child->getAnchorPoint();
Size cs = child->getSize();
float finalPosX = leftBoundary + (ap.x * cs.width);
float finalPosY = layoutSize.height - (1.0f - ap.y) * cs.height;
switch (childGravity)
{
case LinearLayoutParameter::LinearGravity::NONE:
case LinearLayoutParameter::LinearGravity::TOP:
break;
case LinearLayoutParameter::LinearGravity::BOTTOM:
finalPosY = ap.y * cs.height;
break;
case LinearLayoutParameter::LinearGravity::CENTER_VERTICAL:
finalPosY = layoutSize.height / 2.0f - cs.height * (0.5f - ap.y);
break;
default:
break;
}
Margin mg = layoutParameter->getMargin();
finalPosX += mg.left;
finalPosY -= mg.top;
child->setPosition(Vec2(finalPosX, finalPosY));
leftBoundary = child->getRightInParent() + mg.right;
}
}
}
}
void RelativeLayoutExecutant::doLayout(const cocos2d::Size &layoutSize, Vector<cocos2d::Node *> container)
{
ssize_t unlayoutChildCount = 0;
Vector<Widget*> widgetChildren;
for (auto& subWidget : container)
{
Widget* child = dynamic_cast<Widget*>(subWidget);
if (child)
{
RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter(LayoutParameter::Type::RELATIVE));
layoutParameter->_put = false;
unlayoutChildCount++;
widgetChildren.pushBack(child);
}
}
while (unlayoutChildCount > 0)
{
for (auto& subWidget : widgetChildren)
{
Widget* child = static_cast<Widget*>(subWidget);
RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter(LayoutParameter::Type::RELATIVE));
if (layoutParameter)
{
if (layoutParameter->_put)
{
continue;
}
Vec2 ap = child->getAnchorPoint();
Size cs = child->getSize();
RelativeLayoutParameter::RelativeAlign align = layoutParameter->getAlign();
const std::string relativeName = layoutParameter->getRelativeToWidgetName();
Widget* relativeWidget = nullptr;
RelativeLayoutParameter* relativeWidgetLP = nullptr;
float finalPosX = 0.0f;
float finalPosY = 0.0f;
if (!relativeName.empty())
{
for (auto& sWidget : widgetChildren)
{
if (sWidget)
{
RelativeLayoutParameter* rlayoutParameter = dynamic_cast<RelativeLayoutParameter*>(sWidget->getLayoutParameter(LayoutParameter::Type::RELATIVE));
if (rlayoutParameter && rlayoutParameter->getRelativeName() == relativeName)
{
relativeWidget = sWidget;
relativeWidgetLP = rlayoutParameter;
break;
}
}
}
}
switch (align)
{
case RelativeLayoutParameter::RelativeAlign::NONE:
case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_LEFT:
finalPosX = ap.x * cs.width;
finalPosY = layoutSize.height - ((1.0f - ap.y) * cs.height);
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL:
finalPosX = layoutSize.width * 0.5f - cs.width * (0.5f - ap.x);
finalPosY = layoutSize.height - ((1.0f - ap.y) * cs.height);
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT:
finalPosX = layoutSize.width - ((1.0f - ap.x) * cs.width);
finalPosY = layoutSize.height - ((1.0f - ap.y) * cs.height);
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL:
finalPosX = ap.x * cs.width;
finalPosY = layoutSize.height * 0.5f - cs.height * (0.5f - ap.y);
break;
case RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT:
finalPosX = layoutSize.width * 0.5f - cs.width * (0.5f - ap.x);
finalPosY = layoutSize.height * 0.5f - cs.height * (0.5f - ap.y);
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_CENTER_VERTICAL:
finalPosX = layoutSize.width - ((1.0f - ap.x) * cs.width);
finalPosY = layoutSize.height * 0.5f - cs.height * (0.5f - ap.y);
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_BOTTOM:
finalPosX = ap.x * cs.width;
finalPosY = ap.y * cs.height;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL:
finalPosX = layoutSize.width * 0.5f - cs.width * (0.5f - ap.x);
finalPosY = ap.y * cs.height;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_BOTTOM:
finalPosX = layoutSize.width - ((1.0f - ap.x) * cs.width);
finalPosY = ap.y * cs.height;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_LEFTALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationBottom = relativeWidget->getTopInParent();
float locationLeft = relativeWidget->getLeftInParent();
finalPosY = locationBottom + ap.y * cs.height;
finalPosX = locationLeft + ap.x * cs.width;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_CENTER:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
Size rbs = relativeWidget->getSize();
float locationBottom = relativeWidget->getTopInParent();
finalPosY = locationBottom + ap.y * cs.height;
finalPosX = relativeWidget->getLeftInParent() + rbs.width * 0.5f + ap.x * cs.width - cs.width * 0.5f;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_RIGHTALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationBottom = relativeWidget->getTopInParent();
float locationRight = relativeWidget->getRightInParent();
finalPosY = locationBottom + ap.y * cs.height;
finalPosX = locationRight - (1.0f - ap.x) * cs.width;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_TOPALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationTop = relativeWidget->getTopInParent();
float locationRight = relativeWidget->getLeftInParent();
finalPosY = locationTop - (1.0f - ap.y) * cs.height;
finalPosX = locationRight - (1.0f - ap.x) * cs.width;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_CENTER:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
Size rbs = relativeWidget->getSize();
float locationRight = relativeWidget->getLeftInParent();
finalPosX = locationRight - (1.0f - ap.x) * cs.width;
finalPosY = relativeWidget->getBottomInParent() + rbs.height * 0.5f + ap.y * cs.height - cs.height * 0.5f;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_BOTTOMALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationBottom = relativeWidget->getBottomInParent();
float locationRight = relativeWidget->getLeftInParent();
finalPosY = locationBottom + ap.y * cs.height;
finalPosX = locationRight - (1.0f - ap.x) * cs.width;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_TOPALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationTop = relativeWidget->getTopInParent();
float locationLeft = relativeWidget->getRightInParent();
finalPosY = locationTop - (1.0f - ap.y) * cs.height;
finalPosX = locationLeft + ap.x * cs.width;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_CENTER:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
Size rbs = relativeWidget->getSize();
float locationLeft = relativeWidget->getRightInParent();
finalPosX = locationLeft + ap.x * cs.width;
finalPosY = relativeWidget->getBottomInParent() + rbs.height * 0.5f + ap.y * cs.height - cs.height * 0.5f;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_BOTTOMALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationBottom = relativeWidget->getBottomInParent();
float locationLeft = relativeWidget->getRightInParent();
finalPosY = locationBottom + ap.y * cs.height;
finalPosX = locationLeft + ap.x * cs.width;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_LEFTALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationTop = relativeWidget->getBottomInParent();
float locationLeft = relativeWidget->getLeftInParent();
finalPosY = locationTop - (1.0f - ap.y) * cs.height;
finalPosX = locationLeft + ap.x * cs.width;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_CENTER:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
Size rbs = relativeWidget->getSize();
float locationTop = relativeWidget->getBottomInParent();
finalPosY = locationTop - (1.0f - ap.y) * cs.height;
finalPosX = relativeWidget->getLeftInParent() + rbs.width * 0.5f + ap.x * cs.width - cs.width * 0.5f;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_RIGHTALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationTop = relativeWidget->getBottomInParent();
float locationRight = relativeWidget->getRightInParent();
finalPosY = locationTop - (1.0f - ap.y) * cs.height;
finalPosX = locationRight - (1.0f - ap.x) * cs.width;
}
break;
default:
break;
}
Margin relativeWidgetMargin;
Margin mg = layoutParameter->getMargin();
if (relativeWidgetLP)
{
relativeWidgetMargin = relativeWidgetLP->getMargin();
}
//handle margin
switch (align)
{
case RelativeLayoutParameter::RelativeAlign::NONE:
case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_LEFT:
finalPosX += mg.left;
finalPosY -= mg.top;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL:
finalPosY -= mg.top;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT:
finalPosX -= mg.right;
finalPosY -= mg.top;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL:
finalPosX += mg.left;
break;
case RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT:
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_CENTER_VERTICAL:
finalPosX -= mg.right;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_BOTTOM:
finalPosX += mg.left;
finalPosY += mg.bottom;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL:
finalPosY += mg.bottom;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_BOTTOM:
finalPosX -= mg.right;
finalPosY += mg.bottom;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_LEFTALIGN:
finalPosY += mg.bottom;
finalPosX += mg.left;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_RIGHTALIGN:
finalPosY += mg.bottom;
finalPosX -= mg.right;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_CENTER:
finalPosY += mg.bottom;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_TOPALIGN:
finalPosX -= mg.right;
finalPosY -= mg.top;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_BOTTOMALIGN:
finalPosX -= mg.right;
finalPosY += mg.bottom;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_CENTER:
finalPosX -= mg.right;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_TOPALIGN:
finalPosX += mg.left;
finalPosY -= mg.top;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_BOTTOMALIGN:
finalPosX += mg.left;
finalPosY += mg.bottom;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_CENTER:
finalPosX += mg.left;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_LEFTALIGN:
finalPosY -= mg.top;
finalPosX += mg.left;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_RIGHTALIGN:
finalPosY -= mg.top;
finalPosX -= mg.right;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_CENTER:
finalPosY -= mg.top;
break;
default:
break;
}
child->setPosition(Vec2(finalPosX, finalPosY));
layoutParameter->_put = true;
unlayoutChildCount--;
}
}
}
widgetChildren.clear();
}
static const int BACKGROUNDIMAGE_Z = (-1);
static const int BCAKGROUNDCOLORRENDERER_Z = (-2);
@ -604,7 +84,6 @@ _currentAlphaTestFunc(GL_ALWAYS),
_currentAlphaTestRef(1),
_backGroundImageColor(Color3B::WHITE),
_backGroundImageOpacity(255),
_curLayoutExecutant(nullptr),
_passFocusToChild(true),
_loopFocus(false)
{
@ -615,7 +94,6 @@ _loopFocus(false)
Layout::~Layout()
{
CC_SAFE_RELEASE(_clippingStencil);
CC_SAFE_RELEASE(_curLayoutExecutant);
}
void Layout::onEnter()
@ -1180,7 +658,7 @@ void Layout::supplyTheLayoutParameterLackToChild(Widget *child)
case Type::HORIZONTAL:
case Type::VERTICAL:
{
LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter(LayoutParameter::Type::LINEAR));
LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter());
if (!layoutParameter)
{
child->setLayoutParameter(LinearLayoutParameter::create());
@ -1189,7 +667,7 @@ void Layout::supplyTheLayoutParameterLackToChild(Widget *child)
}
case Type::RELATIVE:
{
RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter(LayoutParameter::Type::RELATIVE));
RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter());
if (!layoutParameter)
{
child->setLayoutParameter(RelativeLayoutParameter::create());
@ -1427,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);
@ -1441,7 +917,29 @@ void Layout::setLayoutType(Type type)
_doLayoutDirty = true;
}
LayoutExecutant* Layout::createCurrentLayoutExecutant()
Layout::Type Layout::getLayoutType() const
{
return _layoutType;
}
void Layout::requestDoLayout()
{
_doLayoutDirty = true;
}
const Size Layout::getLayoutContentSize()
{
return this->getSize();
}
const Vector<Node*>& Layout::getLayoutElements()
{
return this->getChildren();
}
LayoutExecutant* Layout::createLayoutExecutant()
{
LayoutExecutant* exe = nullptr;
switch (_layoutType)
@ -1459,16 +957,7 @@ LayoutExecutant* Layout::createCurrentLayoutExecutant()
break;
}
return exe;
}
Layout::Type Layout::getLayoutType() const
{
return _layoutType;
}
void Layout::requestDoLayout()
{
_doLayoutDirty = true;
}
void Layout::doLayout()
@ -1477,10 +966,13 @@ void Layout::doLayout()
{
return;
}
if (_curLayoutExecutant)
LayoutExecutant* executant = this->createLayoutExecutant();
if (executant)
{
_curLayoutExecutant->doLayout(getSize(), getChildren());
executant->doLayout(this);
}
_doLayoutDirty = false;
}
@ -1539,7 +1031,7 @@ bool Layout::isPassFocusToChild()
return _passFocusToChild;
}
Size Layout::getLayoutContentSize()const
Size Layout::getLayoutAccumulatedSize()const
{
const auto& children = this->getChildren();
Size layoutSize = Size::ZERO;
@ -1549,7 +1041,7 @@ Size Layout::getLayoutContentSize()const
Layout *layout = dynamic_cast<Layout*>(widget);
if (nullptr != layout)
{
layoutSize = layoutSize + layout->getLayoutContentSize();
layoutSize = layoutSize + layout->getLayoutAccumulatedSize();
}
else
{
@ -1557,7 +1049,7 @@ Size Layout::getLayoutContentSize()const
if (w)
{
widgetCount++;
Margin m = w->getLayoutParameter(LayoutParameter::Type::LINEAR)->getMargin();
Margin m = w->getLayoutParameter()->getMargin();
layoutSize = layoutSize + w->getSize() + Size(m.right + m.left, m.top + m.bottom) * 0.5;
}
}
@ -1580,7 +1072,7 @@ Vec2 Layout::getWorldCenterPoint(Widget* widget)
{
Layout *layout = dynamic_cast<Layout*>(widget);
//FIXEDME: we don't need to calculate the content size of layout anymore
Size widgetSize = layout ? layout->getLayoutContentSize() : widget->getSize();
Size widgetSize = layout ? layout->getLayoutAccumulatedSize() : widget->getSize();
// CCLOG("contnet size : width = %f, height = %f", widgetSize.width, widgetSize.height);
return widget->convertToWorldSpace(Vec2(widgetSize.width/2, widgetSize.height/2));
}

View File

@ -31,13 +31,26 @@ NS_CC_BEGIN
namespace ui {
class LayoutExecutant;
class LayoutProtocol
{
public:
LayoutProtocol(){}
virtual ~LayoutProtocol(){}
virtual LayoutExecutant* createLayoutExecutant() = 0;
virtual const Size getLayoutContentSize() = 0;
virtual const Vector<Node*>& getLayoutElements() = 0;
virtual void doLayout() = 0;
};
/**
* @js NA
* @lua NA
*/
class LayoutExecutant;
class Layout : public Widget
class Layout : public Widget, public LayoutProtocol
{
DECLARE_CLASS_GUI_INFO
@ -313,7 +326,11 @@ protected:
void setStencilClippingSize(const Size& size);
const Rect& getClippingRect();
virtual void doLayout();
virtual void doLayout()override;
virtual LayoutExecutant* createLayoutExecutant()override;
virtual const Size getLayoutContentSize()override;
virtual const Vector<Node*>& getLayoutElements() override;
//clipping
void onBeforeVisitStencil();
@ -325,12 +342,11 @@ 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
*/
Size getLayoutContentSize() const;
Size getLayoutAccumulatedSize() const;
/**
* When the layout get focused, it the layout pass the focus to its child, it will use this method to determine which child
@ -477,8 +493,6 @@ protected:
Color3B _backGroundImageColor;
GLubyte _backGroundImageOpacity;
LayoutExecutant* _curLayoutExecutant;
GLint _mask_layer_le;
GroupCommand _groupCommand;
CustomCommand _beforeVisitCmdStencil;

View File

@ -0,0 +1,513 @@
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "UILayoutExecutant.h"
#include "UILayout.h"
NS_CC_BEGIN
namespace ui {
LinearVerticalLayoutExecutant* LinearVerticalLayoutExecutant::create()
{
LinearVerticalLayoutExecutant* exe = new LinearVerticalLayoutExecutant();
if (exe)
{
exe->autorelease();
return exe;
}
CC_SAFE_DELETE(exe);
return nullptr;
}
LinearHorizontalLayoutExecutant* LinearHorizontalLayoutExecutant::create()
{
LinearHorizontalLayoutExecutant* exe = new LinearHorizontalLayoutExecutant();
if (exe)
{
exe->autorelease();
return exe;
}
CC_SAFE_DELETE(exe);
return nullptr;
}
RelativeLayoutExecutant* RelativeLayoutExecutant::create()
{
RelativeLayoutExecutant* exe = new RelativeLayoutExecutant();
if (exe)
{
exe->autorelease();
return exe;
}
CC_SAFE_DELETE(exe);
return nullptr;
}
void LinearVerticalLayoutExecutant::doLayout(LayoutProtocol* layout)
{
Size layoutSize = layout->getLayoutContentSize();
Vector<Node*> container = layout->getLayoutElements();
float topBoundary = layoutSize.height;
for (auto& subWidget : container)
{
LayoutParameterProtocol* child = dynamic_cast<LayoutParameterProtocol*>(subWidget);
if (child)
{
LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter());
if (layoutParameter)
{
LinearLayoutParameter::LinearGravity childGravity = layoutParameter->getGravity();
Vec2 ap = subWidget->getAnchorPoint();
Size cs = subWidget->getContentSize();
float finalPosX = ap.x * cs.width;
float finalPosY = topBoundary - ((1.0f-ap.y) * cs.height);
switch (childGravity)
{
case LinearLayoutParameter::LinearGravity::NONE:
case LinearLayoutParameter::LinearGravity::LEFT:
break;
case LinearLayoutParameter::LinearGravity::RIGHT:
finalPosX = layoutSize.width - ((1.0f - ap.x) * cs.width);
break;
case LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL:
finalPosX = layoutSize.width / 2.0f - cs.width * (0.5f-ap.x);
break;
default:
break;
}
Margin mg = layoutParameter->getMargin();
finalPosX += mg.left;
finalPosY -= mg.top;
subWidget->setPosition(Vec2(finalPosX, finalPosY));
topBoundary = subWidget->getPosition().y - subWidget->getAnchorPoint().y * subWidget->getContentSize().height - mg.bottom;
}
}
}
}
void LinearHorizontalLayoutExecutant::doLayout(LayoutProtocol* layout)
{
Size layoutSize = layout->getLayoutContentSize();
Vector<Node*> container = layout->getLayoutElements();
float leftBoundary = 0.0f;
for (auto& subWidget : container)
{
Widget* child = dynamic_cast<Widget*>(subWidget);
if (child)
{
LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter());
if (layoutParameter)
{
LinearLayoutParameter::LinearGravity childGravity = layoutParameter->getGravity();
Vec2 ap = child->getAnchorPoint();
Size cs = child->getSize();
float finalPosX = leftBoundary + (ap.x * cs.width);
float finalPosY = layoutSize.height - (1.0f - ap.y) * cs.height;
switch (childGravity)
{
case LinearLayoutParameter::LinearGravity::NONE:
case LinearLayoutParameter::LinearGravity::TOP:
break;
case LinearLayoutParameter::LinearGravity::BOTTOM:
finalPosY = ap.y * cs.height;
break;
case LinearLayoutParameter::LinearGravity::CENTER_VERTICAL:
finalPosY = layoutSize.height / 2.0f - cs.height * (0.5f - ap.y);
break;
default:
break;
}
Margin mg = layoutParameter->getMargin();
finalPosX += mg.left;
finalPosY -= mg.top;
child->setPosition(Vec2(finalPosX, finalPosY));
leftBoundary = child->getRightBoundary() + mg.right;
}
}
}
}
void RelativeLayoutExecutant::doLayout(LayoutProtocol *layout)
{
Size layoutSize = layout->getLayoutContentSize();
Vector<Node*> container = layout->getLayoutElements();
ssize_t unlayoutChildCount = 0;
Vector<Widget*> widgetChildren;
for (auto& subWidget : container)
{
Widget* child = dynamic_cast<Widget*>(subWidget);
if (child)
{
RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter());
layoutParameter->_put = false;
unlayoutChildCount++;
widgetChildren.pushBack(child);
}
}
while (unlayoutChildCount > 0)
{
for (auto& subWidget : widgetChildren)
{
Widget* child = static_cast<Widget*>(subWidget);
RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter());
if (layoutParameter)
{
if (layoutParameter->_put)
{
continue;
}
Vec2 ap = child->getAnchorPoint();
Size cs = child->getSize();
RelativeLayoutParameter::RelativeAlign align = layoutParameter->getAlign();
const std::string relativeName = layoutParameter->getRelativeToWidgetName();
Widget* relativeWidget = nullptr;
RelativeLayoutParameter* relativeWidgetLP = nullptr;
float finalPosX = 0.0f;
float finalPosY = 0.0f;
if (!relativeName.empty())
{
for (auto& sWidget : widgetChildren)
{
if (sWidget)
{
RelativeLayoutParameter* rlayoutParameter = dynamic_cast<RelativeLayoutParameter*>(sWidget->getLayoutParameter());
if (rlayoutParameter && rlayoutParameter->getRelativeName() == relativeName)
{
relativeWidget = sWidget;
relativeWidgetLP = rlayoutParameter;
break;
}
}
}
}
switch (align)
{
case RelativeLayoutParameter::RelativeAlign::NONE:
case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_LEFT:
finalPosX = ap.x * cs.width;
finalPosY = layoutSize.height - ((1.0f - ap.y) * cs.height);
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL:
finalPosX = layoutSize.width * 0.5f - cs.width * (0.5f - ap.x);
finalPosY = layoutSize.height - ((1.0f - ap.y) * cs.height);
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT:
finalPosX = layoutSize.width - ((1.0f - ap.x) * cs.width);
finalPosY = layoutSize.height - ((1.0f - ap.y) * cs.height);
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL:
finalPosX = ap.x * cs.width;
finalPosY = layoutSize.height * 0.5f - cs.height * (0.5f - ap.y);
break;
case RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT:
finalPosX = layoutSize.width * 0.5f - cs.width * (0.5f - ap.x);
finalPosY = layoutSize.height * 0.5f - cs.height * (0.5f - ap.y);
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_CENTER_VERTICAL:
finalPosX = layoutSize.width - ((1.0f - ap.x) * cs.width);
finalPosY = layoutSize.height * 0.5f - cs.height * (0.5f - ap.y);
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_BOTTOM:
finalPosX = ap.x * cs.width;
finalPosY = ap.y * cs.height;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL:
finalPosX = layoutSize.width * 0.5f - cs.width * (0.5f - ap.x);
finalPosY = ap.y * cs.height;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_BOTTOM:
finalPosX = layoutSize.width - ((1.0f - ap.x) * cs.width);
finalPosY = ap.y * cs.height;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_LEFTALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationTop = relativeWidget->getTopBoundary();
float locationLeft = relativeWidget->getLeftBoundary();
finalPosY = locationTop + ap.y * cs.height;
finalPosX = locationLeft + ap.x * cs.width;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_CENTER:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
Size rbs = relativeWidget->getSize();
float locationTop = relativeWidget->getTopBoundary();
finalPosY = locationTop + ap.y * cs.height;
finalPosX = relativeWidget->getLeftBoundary() + rbs.width * 0.5f + ap.x * cs.width - cs.width * 0.5f;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_RIGHTALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationTop = relativeWidget->getTopBoundary();
float locationRight = relativeWidget->getRightBoundary();
finalPosY = locationTop + ap.y * cs.height;
finalPosX = locationRight - (1.0f - ap.x) * cs.width;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_TOPALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationTop = relativeWidget->getTopBoundary();
float locationLeft = relativeWidget->getLeftBoundary();
finalPosY = locationTop - (1.0f - ap.y) * cs.height;
finalPosX = locationLeft - (1.0f - ap.x) * cs.width;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_CENTER:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
Size rbs = relativeWidget->getSize();
float locationLeft = relativeWidget->getLeftBoundary();
finalPosX = locationLeft - (1.0f - ap.x) * cs.width;
finalPosY = relativeWidget->getBottomBoundary() + rbs.height * 0.5f + ap.y * cs.height - cs.height * 0.5f;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_BOTTOMALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationBottom = relativeWidget->getBottomBoundary();
float locationLeft = relativeWidget->getLeftBoundary();
finalPosY = locationBottom + ap.y * cs.height;
finalPosX = locationLeft - (1.0f - ap.x) * cs.width;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_TOPALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationTop = relativeWidget->getTopBoundary();
float locationRight = relativeWidget->getRightBoundary();
finalPosY = locationTop - (1.0f - ap.y) * cs.height;
finalPosX = locationRight + ap.x * cs.width;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_CENTER:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
Size rbs = relativeWidget->getSize();
float locationRight = relativeWidget->getRightBoundary();
finalPosX = locationRight + ap.x * cs.width;
finalPosY = relativeWidget->getBottomBoundary() + rbs.height * 0.5f + ap.y * cs.height - cs.height * 0.5f;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_BOTTOMALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationBottom = relativeWidget->getBottomBoundary();
float locationRight = relativeWidget->getRightBoundary();
finalPosY = locationBottom + ap.y * cs.height;
finalPosX = locationRight + ap.x * cs.width;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_LEFTALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationBottom = relativeWidget->getBottomBoundary();
float locationLeft = relativeWidget->getLeftBoundary();
finalPosY = locationBottom - (1.0f - ap.y) * cs.height;
finalPosX = locationLeft + ap.x * cs.width;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_CENTER:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
Size rbs = relativeWidget->getSize();
float locationBottom = relativeWidget->getBottomBoundary();
finalPosY = locationBottom - (1.0f - ap.y) * cs.height;
finalPosX = relativeWidget->getLeftBoundary() + rbs.width * 0.5f + ap.x * cs.width - cs.width * 0.5f;
}
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_RIGHTALIGN:
if (relativeWidget)
{
if (relativeWidgetLP && !relativeWidgetLP->_put)
{
continue;
}
float locationBottom = relativeWidget->getBottomBoundary();
float locationRight = relativeWidget->getRightBoundary();
finalPosY = locationBottom - (1.0f - ap.y) * cs.height;
finalPosX = locationRight - (1.0f - ap.x) * cs.width;
}
break;
default:
break;
}
Margin relativeWidgetMargin;
Margin mg = layoutParameter->getMargin();
if (relativeWidgetLP)
{
relativeWidgetMargin = relativeWidgetLP->getMargin();
}
//handle margin
switch (align)
{
case RelativeLayoutParameter::RelativeAlign::NONE:
case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_LEFT:
finalPosX += mg.left;
finalPosY -= mg.top;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL:
finalPosY -= mg.top;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT:
finalPosX -= mg.right;
finalPosY -= mg.top;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL:
finalPosX += mg.left;
break;
case RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT:
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_CENTER_VERTICAL:
finalPosX -= mg.right;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_BOTTOM:
finalPosX += mg.left;
finalPosY += mg.bottom;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL:
finalPosY += mg.bottom;
break;
case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_BOTTOM:
finalPosX -= mg.right;
finalPosY += mg.bottom;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_LEFTALIGN:
finalPosY += mg.bottom;
finalPosX += mg.left;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_RIGHTALIGN:
finalPosY += mg.bottom;
finalPosX -= mg.right;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_CENTER:
finalPosY += mg.bottom;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_TOPALIGN:
finalPosX -= mg.right;
finalPosY -= mg.top;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_BOTTOMALIGN:
finalPosX -= mg.right;
finalPosY += mg.bottom;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_CENTER:
finalPosX -= mg.right;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_TOPALIGN:
finalPosX += mg.left;
finalPosY -= mg.top;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_BOTTOMALIGN:
finalPosX += mg.left;
finalPosY += mg.bottom;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_CENTER:
finalPosX += mg.left;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_LEFTALIGN:
finalPosY -= mg.top;
finalPosX += mg.left;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_RIGHTALIGN:
finalPosY -= mg.top;
finalPosX -= mg.right;
break;
case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_CENTER:
finalPosY -= mg.top;
break;
default:
break;
}
child->setPosition(Vec2(finalPosX, finalPosY));
layoutParameter->_put = true;
unlayoutChildCount--;
}
}
}
widgetChildren.clear();
}
}
NS_CC_END

View File

@ -0,0 +1,75 @@
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __cocos2d_libs__CCLayoutExecutant__
#define __cocos2d_libs__CCLayoutExecutant__
#include "base/CCRef.h"
NS_CC_BEGIN
namespace ui {
class LayoutProtocol;
class LayoutExecutant : public Ref
{
public:
LayoutExecutant(){};
virtual ~LayoutExecutant(){};
virtual void doLayout(LayoutProtocol *layout) = 0;
};
class LinearVerticalLayoutExecutant : public LayoutExecutant
{
public:
LinearVerticalLayoutExecutant(){};
virtual ~LinearVerticalLayoutExecutant(){};
static LinearVerticalLayoutExecutant* create();
virtual void doLayout(LayoutProtocol *layout) override;
};
class LinearHorizontalLayoutExecutant : public LayoutExecutant
{
public:
LinearHorizontalLayoutExecutant(){};
virtual ~LinearHorizontalLayoutExecutant(){};
static LinearHorizontalLayoutExecutant* create();
virtual void doLayout(LayoutProtocol *layout) override;
};
class RelativeLayoutExecutant : public LayoutExecutant
{
public:
RelativeLayoutExecutant(){};
virtual ~RelativeLayoutExecutant(){};
static RelativeLayoutExecutant* create();
virtual void doLayout(LayoutProtocol *layout) override;
};
}
NS_CC_END
#endif /* defined(__cocos2d_libs__CCLayoutExecutant__) */

View File

@ -130,6 +130,15 @@ protected:
Type _layoutParameterType;
};
class LayoutParameterProtocol
{
public:
LayoutParameterProtocol(){}
virtual ~LayoutParameterProtocol(){}
virtual LayoutParameter* getLayoutParameter() = 0;
};
/**
* @js NA

View File

@ -130,7 +130,7 @@ void ListView::remedyLayoutParameter(Widget *item)
switch (_direction) {
case Direction::VERTICAL:
{
LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter(LayoutParameter::Type::LINEAR));
LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter());
if (!llp)
{
LinearLayoutParameter* defaultLp = LinearLayoutParameter::create();
@ -185,7 +185,7 @@ void ListView::remedyLayoutParameter(Widget *item)
}
case Direction::HORIZONTAL:
{
LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter(LayoutParameter::Type::LINEAR));
LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter());
if (!llp)
{
LinearLayoutParameter* defaultLp = LinearLayoutParameter::create();

View File

@ -435,18 +435,18 @@ bool PageView::scrollPages(float touchOffset)
switch (_touchMoveDir)
{
case TouchDirection::LEFT: // left
if (_rightChild->getRightInParent() + touchOffset <= _rightBoundary)
if (_rightChild->getRightBoundary() + touchOffset <= _rightBoundary)
{
realOffset = _rightBoundary - _rightChild->getRightInParent();
realOffset = _rightBoundary - _rightChild->getRightBoundary();
movePages(realOffset);
return false;
}
break;
case TouchDirection::RIGHT: // right
if (_leftChild->getLeftInParent() + touchOffset >= _leftBoundary)
if (_leftChild->getLeftBoundary() + touchOffset >= _leftBoundary)
{
realOffset = _leftBoundary - _leftChild->getLeftInParent();
realOffset = _leftBoundary - _leftChild->getLeftBoundary();
movePages(realOffset);
return false;
}

View File

@ -210,7 +210,7 @@ void ScrollView::setInnerContainerSize(const Size &size)
}
case Direction::HORIZONTAL:
{
if (_innerContainer->getRightInParent() <= _size.width)
if (_innerContainer->getRightBoundary() <= _size.width)
{
Size newInnerSize = _innerContainer->getSize();
float offset = originalInnerSize.width - newInnerSize.width;
@ -223,7 +223,7 @@ void ScrollView::setInnerContainerSize(const Size &size)
Size newInnerSize = _innerContainer->getSize();
float offsetY = originalInnerSize.height - newInnerSize.height;
float offsetX = 0.0f;
if (_innerContainer->getRightInParent() <= _size.width)
if (_innerContainer->getRightBoundary() <= _size.width)
{
offsetX = originalInnerSize.width - newInnerSize.width;
}
@ -233,11 +233,11 @@ void ScrollView::setInnerContainerSize(const Size &size)
default:
break;
}
if (_innerContainer->getLeftInParent() > 0.0f)
if (_innerContainer->getLeftBoundary() > 0.0f)
{
_innerContainer->setPosition(Vec2(_innerContainer->getAnchorPoint().x * _innerContainer->getSize().width, _innerContainer->getPosition().y));
}
if (_innerContainer->getRightInParent() < _size.width)
if (_innerContainer->getRightBoundary() < _size.width)
{
_innerContainer->setPosition(Vec2(_size.width - ((1.0f - _innerContainer->getAnchorPoint().x) * _innerContainer->getSize().width), _innerContainer->getPosition().y));
}
@ -245,7 +245,7 @@ void ScrollView::setInnerContainerSize(const Size &size)
{
_innerContainer->setPosition(Vec2(_innerContainer->getPosition().x, _innerContainer->getAnchorPoint().y * _innerContainer->getSize().height));
}
if (_innerContainer->getTopInParent() < _size.height)
if (_innerContainer->getTopBoundary() < _size.height)
{
_innerContainer->setPosition(Vec2(_innerContainer->getPosition().x, _size.height - (1.0f - _innerContainer->getAnchorPoint().y) * _innerContainer->getSize().height));
}
@ -390,56 +390,56 @@ bool ScrollView::checkNeedBounce()
{
if (_topBounceNeeded && _leftBounceNeeded)
{
Vec2 scrollVector = Vec2(0.0f, _size.height) - Vec2(_innerContainer->getLeftInParent(), _innerContainer->getTopInParent());
Vec2 scrollVector = Vec2(0.0f, _size.height) - Vec2(_innerContainer->getLeftBoundary(), _innerContainer->getTopBoundary());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
}
else if (_topBounceNeeded && _rightBounceNeeded)
{
Vec2 scrollVector = Vec2(_size.width, _size.height) - Vec2(_innerContainer->getRightInParent(), _innerContainer->getTopInParent());
Vec2 scrollVector = Vec2(_size.width, _size.height) - Vec2(_innerContainer->getRightBoundary(), _innerContainer->getTopBoundary());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
}
else if (_bottomBounceNeeded && _leftBounceNeeded)
{
Vec2 scrollVector = Vec2::ZERO - Vec2(_innerContainer->getLeftInParent(), _innerContainer->getBottomInParent());
Vec2 scrollVector = Vec2::ZERO - Vec2(_innerContainer->getLeftBoundary(), _innerContainer->getBottomBoundary());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
}
else if (_bottomBounceNeeded && _rightBounceNeeded)
{
Vec2 scrollVector = Vec2(_size.width, 0.0f) - Vec2(_innerContainer->getRightInParent(), _innerContainer->getBottomInParent());
Vec2 scrollVector = Vec2(_size.width, 0.0f) - Vec2(_innerContainer->getRightBoundary(), _innerContainer->getBottomBoundary());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
}
else if (_topBounceNeeded)
{
Vec2 scrollVector = Vec2(0.0f, _size.height) - Vec2(0.0f, _innerContainer->getTopInParent());
Vec2 scrollVector = Vec2(0.0f, _size.height) - Vec2(0.0f, _innerContainer->getTopBoundary());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
}
else if (_bottomBounceNeeded)
{
Vec2 scrollVector = Vec2::ZERO - Vec2(0.0f, _innerContainer->getBottomInParent());
Vec2 scrollVector = Vec2::ZERO - Vec2(0.0f, _innerContainer->getBottomBoundary());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
}
else if (_leftBounceNeeded)
{
Vec2 scrollVector = Vec2::ZERO - Vec2(_innerContainer->getLeftInParent(), 0.0f);
Vec2 scrollVector = Vec2::ZERO - Vec2(_innerContainer->getLeftBoundary(), 0.0f);
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
}
else if (_rightBounceNeeded)
{
Vec2 scrollVector = Vec2(_size.width, 0.0f) - Vec2(_innerContainer->getRightInParent(), 0.0f);
Vec2 scrollVector = Vec2(_size.width, 0.0f) - Vec2(_innerContainer->getRightBoundary(), 0.0f);
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
@ -451,7 +451,7 @@ bool ScrollView::checkNeedBounce()
void ScrollView::checkBounceBoundary()
{
float icBottomPos = _innerContainer->getBottomInParent();
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos > _bottomBoundary)
{
scrollToBottomEvent();
@ -461,7 +461,7 @@ void ScrollView::checkBounceBoundary()
{
_bottomBounceNeeded = false;
}
float icTopPos = _innerContainer->getTopInParent();
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos < _topBoundary)
{
scrollToTopEvent();
@ -471,7 +471,7 @@ void ScrollView::checkBounceBoundary()
{
_topBounceNeeded = false;
}
float icRightPos = _innerContainer->getRightInParent();
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos < _rightBoundary)
{
scrollToRightEvent();
@ -481,7 +481,7 @@ void ScrollView::checkBounceBoundary()
{
_rightBounceNeeded = false;
}
float icLeftPos = _innerContainer->getLeftInParent();
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos > _leftBoundary)
{
scrollToLeftEvent();
@ -588,14 +588,14 @@ bool ScrollView::bounceScrollChildren(float touchOffsetX, float touchOffsetY)
{
float realOffsetX = touchOffsetX;
float realOffsetY = touchOffsetY;
float icRightPos = _innerContainer->getRightInParent();
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos + realOffsetX >= _rightBoundary)
{
realOffsetX = _rightBoundary - icRightPos;
bounceRightEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos + touchOffsetY >= _topBoundary)
{
realOffsetY = _topBoundary - icTopPos;
@ -608,14 +608,14 @@ bool ScrollView::bounceScrollChildren(float touchOffsetX, float touchOffsetY)
{
float realOffsetX = touchOffsetX;
float realOffsetY = touchOffsetY;
float icLefrPos = _innerContainer->getLeftInParent();
float icLefrPos = _innerContainer->getLeftBoundary();
if (icLefrPos + realOffsetX <= _leftBoundary)
{
realOffsetX = _leftBoundary - icLefrPos;
bounceLeftEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos + touchOffsetY >= _topBoundary)
{
realOffsetY = _topBoundary - icTopPos;
@ -628,14 +628,14 @@ bool ScrollView::bounceScrollChildren(float touchOffsetX, float touchOffsetY)
{
float realOffsetX = touchOffsetX;
float realOffsetY = touchOffsetY;
float icLefrPos = _innerContainer->getLeftInParent();
float icLefrPos = _innerContainer->getLeftBoundary();
if (icLefrPos + realOffsetX <= _leftBoundary)
{
realOffsetX = _leftBoundary - icLefrPos;
bounceLeftEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + touchOffsetY <= _bottomBoundary)
{
realOffsetY = _bottomBoundary - icBottomPos;
@ -648,14 +648,14 @@ bool ScrollView::bounceScrollChildren(float touchOffsetX, float touchOffsetY)
{
float realOffsetX = touchOffsetX;
float realOffsetY = touchOffsetY;
float icRightPos = _innerContainer->getRightInParent();
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos + realOffsetX >= _rightBoundary)
{
realOffsetX = _rightBoundary - icRightPos;
bounceRightEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + touchOffsetY <= _bottomBoundary)
{
realOffsetY = _bottomBoundary - icBottomPos;
@ -667,7 +667,7 @@ bool ScrollView::bounceScrollChildren(float touchOffsetX, float touchOffsetY)
else if (touchOffsetX == 0.0f && touchOffsetY > 0.0f) // bounce to top
{
float realOffsetY = touchOffsetY;
float icTopPos = _innerContainer->getTopInParent();
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos + touchOffsetY >= _topBoundary)
{
realOffsetY = _topBoundary - icTopPos;
@ -679,7 +679,7 @@ bool ScrollView::bounceScrollChildren(float touchOffsetX, float touchOffsetY)
else if (touchOffsetX == 0.0f && touchOffsetY < 0.0f) //bounce to bottom
{
float realOffsetY = touchOffsetY;
float icBottomPos = _innerContainer->getBottomInParent();
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + touchOffsetY <= _bottomBoundary)
{
realOffsetY = _bottomBoundary - icBottomPos;
@ -691,7 +691,7 @@ bool ScrollView::bounceScrollChildren(float touchOffsetX, float touchOffsetY)
else if (touchOffsetX > 0.0f && touchOffsetY == 0.0f) //bounce to right
{
float realOffsetX = touchOffsetX;
float icRightPos = _innerContainer->getRightInParent();
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos + realOffsetX >= _rightBoundary)
{
realOffsetX = _rightBoundary - icRightPos;
@ -703,7 +703,7 @@ bool ScrollView::bounceScrollChildren(float touchOffsetX, float touchOffsetY)
else if (touchOffsetX < 0.0f && touchOffsetY == 0.0f) //bounce to left
{
float realOffsetX = touchOffsetX;
float icLeftPos = _innerContainer->getLeftInParent();
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos + realOffsetX <= _leftBoundary)
{
realOffsetX = _leftBoundary - icLeftPos;
@ -724,7 +724,7 @@ bool ScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchO
{
if (_autoScrollDir.y > 0)
{
float icBottomPos = _innerContainer->getBottomInParent();
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + *touchOffsetY >= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icBottomPos;
@ -733,7 +733,7 @@ bool ScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchO
}
else
{
float icBottomPos = _innerContainer->getBottomInParent();
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + *touchOffsetY <= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icBottomPos;
@ -746,7 +746,7 @@ bool ScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchO
{
if (_autoScrollDir.x > 0)
{
float icLeftPos = _innerContainer->getLeftInParent();
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos + *touchOffsetX >= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icLeftPos;
@ -755,7 +755,7 @@ bool ScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchO
}
else
{
float icLeftPos = _innerContainer->getLeftInParent();
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos + *touchOffsetX <= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icLeftPos;
@ -768,13 +768,13 @@ bool ScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchO
{
if (*touchOffsetX > 0.0f && *touchOffsetY > 0.0f) // up right
{
float icLeftPos = _innerContainer->getLeftInParent();
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos + *touchOffsetX >= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icLeftPos;
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + *touchOffsetY >= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icBottomPos;
@ -783,13 +783,13 @@ bool ScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchO
}
else if (*touchOffsetX < 0.0f && *touchOffsetY > 0.0f) // up left
{
float icRightPos = _innerContainer->getRightInParent();
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos + *touchOffsetX <= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icRightPos;
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + *touchOffsetY >= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icBottomPos;
@ -798,13 +798,13 @@ bool ScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchO
}
else if (*touchOffsetX < 0.0f && *touchOffsetY < 0.0f) // down left
{
float icRightPos = _innerContainer->getRightInParent();
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos + *touchOffsetX <= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icRightPos;
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos + *touchOffsetY <= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icTopPos;
@ -813,13 +813,13 @@ bool ScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchO
}
else if (*touchOffsetX > 0.0f && *touchOffsetY < 0.0f) // down right
{
float icLeftPos = _innerContainer->getLeftInParent();
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos + *touchOffsetX >= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icLeftPos;
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos + *touchOffsetY <= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icTopPos;
@ -828,7 +828,7 @@ bool ScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchO
}
else if (*touchOffsetX == 0.0f && *touchOffsetY > 0.0f) // up
{
float icBottomPos = _innerContainer->getBottomInParent();
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + *touchOffsetY >= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icBottomPos;
@ -837,7 +837,7 @@ bool ScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchO
}
else if (*touchOffsetX < 0.0f && *touchOffsetY == 0.0f) // left
{
float icRightPos = _innerContainer->getRightInParent();
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos + *touchOffsetX <= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icRightPos;
@ -846,7 +846,7 @@ bool ScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchO
}
else if (*touchOffsetX == 0.0f && *touchOffsetY < 0.0f) // down
{
float icTopPos = _innerContainer->getTopInParent();
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos + *touchOffsetY <= _autoScrollDestination.y)
{
*touchOffsetY = _autoScrollDestination.y - icTopPos;
@ -855,7 +855,7 @@ bool ScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchO
}
else if (*touchOffsetX > 0.0f && *touchOffsetY == 0.0f) // right
{
float icLeftPos = _innerContainer->getLeftInParent();
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos + *touchOffsetX >= _autoScrollDestination.x)
{
*touchOffsetX = _autoScrollDestination.x - icLeftPos;
@ -870,319 +870,345 @@ bool ScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchO
return scrollenabled;
}
bool ScrollView::scrollChilderVertical(float touchOffsetX, float touchOffsetY)
{
bool scrollenabled = true;
float realOffset = touchOffsetY;
if (_bounceEnabled)
{
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + touchOffsetY >= _bounceBottomBoundary)
{
realOffset = _bounceBottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos + touchOffsetY <= _bounceTopBoundary)
{
realOffset = _bounceTopBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else
{
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + touchOffsetY >= _bottomBoundary)
{
realOffset = _bottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos + touchOffsetY <= _topBoundary)
{
realOffset = _topBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
moveChildren(0.0f, realOffset);
return scrollenabled;
}
bool ScrollView::scrollChilderHorizontal(float touchOffsetX, float touchOffsetY)
{
bool scrollenabled = true;
float realOffset = touchOffsetX;
if (_bounceEnabled)
{
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos + touchOffsetX <= _bounceRightBoundary)
{
realOffset = _bounceRightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos + touchOffsetX >= _bounceLeftBoundary)
{
realOffset = _bounceLeftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
}
else
{
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos + touchOffsetX <= _rightBoundary)
{
realOffset = _rightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos + touchOffsetX >= _leftBoundary)
{
realOffset = _leftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
}
moveChildren(realOffset, 0.0f);
return scrollenabled;
}
bool ScrollView::scrollChilderBoth(float touchOffsetX, float touchOffsetY)
{
bool scrollenabled = true;
float realOffsetX = touchOffsetX;
float realOffsetY = touchOffsetY;
if (_bounceEnabled)
{
if (touchOffsetX > 0.0f && touchOffsetY > 0.0f) // up right
{
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos + touchOffsetX >= _bounceLeftBoundary)
{
realOffsetX = _bounceLeftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + touchOffsetY >= _bounceBottomBoundary)
{
realOffsetY = _bounceBottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY > 0.0f) // up left
{
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos + touchOffsetX <= _bounceRightBoundary)
{
realOffsetX = _bounceRightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + touchOffsetY >= _bounceBottomBoundary)
{
realOffsetY = _bounceBottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY < 0.0f) // down left
{
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos + touchOffsetX <= _bounceRightBoundary)
{
realOffsetX = _bounceRightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos + touchOffsetY <= _bounceTopBoundary)
{
realOffsetY = _bounceTopBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX > 0.0f && touchOffsetY < 0.0f) // down right
{
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos + touchOffsetX >= _bounceLeftBoundary)
{
realOffsetX = _bounceLeftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos + touchOffsetY <= _bounceTopBoundary)
{
realOffsetY = _bounceTopBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX == 0.0f && touchOffsetY > 0.0f) // up
{
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + touchOffsetY >= _bounceBottomBoundary)
{
realOffsetY = _bounceBottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY == 0.0f) // left
{
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos + touchOffsetX <= _bounceRightBoundary)
{
realOffsetX = _bounceRightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
}
else if (touchOffsetX == 0.0f && touchOffsetY < 0.0f) // down
{
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos + touchOffsetY <= _bounceTopBoundary)
{
realOffsetY = _bounceTopBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX > 0.0f && touchOffsetY == 0.0f) // right
{
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos + touchOffsetX >= _bounceLeftBoundary)
{
realOffsetX = _bounceLeftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
}
}
else
{
if (touchOffsetX > 0.0f && touchOffsetY > 0.0f) // up right
{
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos + touchOffsetX >= _leftBoundary)
{
realOffsetX = _leftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + touchOffsetY >= _bottomBoundary)
{
realOffsetY = _bottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY > 0.0f) // up left
{
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos + touchOffsetX <= _rightBoundary)
{
realOffsetX = _rightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + touchOffsetY >= _bottomBoundary)
{
realOffsetY = _bottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY < 0.0f) // down left
{
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos + touchOffsetX <= _rightBoundary)
{
realOffsetX = _rightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos + touchOffsetY <= _topBoundary)
{
realOffsetY = _topBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX > 0.0f && touchOffsetY < 0.0f) // down right
{
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos + touchOffsetX >= _leftBoundary)
{
realOffsetX = _leftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos + touchOffsetY <= _topBoundary)
{
realOffsetY = _topBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX == 0.0f && touchOffsetY > 0.0f) // up
{
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos + touchOffsetY >= _bottomBoundary)
{
realOffsetY = _bottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY == 0.0f) // left
{
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos + touchOffsetX <= _rightBoundary)
{
realOffsetX = _rightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
}
else if (touchOffsetX == 0.0f && touchOffsetY < 0.0f) // down
{
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos + touchOffsetY <= _topBoundary)
{
realOffsetY = _topBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX > 0.0f && touchOffsetY == 0.0f) // right
{
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos + touchOffsetX >= _leftBoundary)
{
realOffsetX = _leftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
}
}
moveChildren(realOffsetX, realOffsetY);
return scrollenabled;
}
bool ScrollView::scrollChildren(float touchOffsetX, float touchOffsetY)
{
bool scrollenabled = true;
scrollingEvent();
switch (_direction)
{
case Direction::VERTICAL: // vertical
{
float realOffset = touchOffsetY;
if (_bounceEnabled)
{
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bounceBottomBoundary)
{
realOffset = _bounceBottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _bounceTopBoundary)
{
realOffset = _bounceTopBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else
{
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bottomBoundary)
{
realOffset = _bottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _topBoundary)
{
realOffset = _topBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
moveChildren(0.0f, realOffset);
scrollenabled = this->scrollChilderVertical(touchOffsetX, touchOffsetY);
break;
}
case Direction::HORIZONTAL: // horizontal
{
float realOffset = touchOffsetX;
if (_bounceEnabled)
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _bounceRightBoundary)
{
realOffset = _bounceRightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _bounceLeftBoundary)
{
realOffset = _bounceLeftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
}
else
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _rightBoundary)
{
realOffset = _rightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _leftBoundary)
{
realOffset = _leftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
}
moveChildren(realOffset, 0.0f);
scrollenabled = this->scrollChilderHorizontal(touchOffsetX, touchOffsetY);
break;
}
case Direction::BOTH:
{
float realOffsetX = touchOffsetX;
float realOffsetY = touchOffsetY;
if (_bounceEnabled)
{
if (touchOffsetX > 0.0f && touchOffsetY > 0.0f) // up right
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _bounceLeftBoundary)
{
realOffsetX = _bounceLeftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bounceBottomBoundary)
{
realOffsetY = _bounceBottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY > 0.0f) // up left
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _bounceRightBoundary)
{
realOffsetX = _bounceRightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bounceBottomBoundary)
{
realOffsetY = _bounceBottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY < 0.0f) // down left
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _bounceRightBoundary)
{
realOffsetX = _bounceRightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _bounceTopBoundary)
{
realOffsetY = _bounceTopBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX > 0.0f && touchOffsetY < 0.0f) // down right
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _bounceLeftBoundary)
{
realOffsetX = _bounceLeftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _bounceTopBoundary)
{
realOffsetY = _bounceTopBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX == 0.0f && touchOffsetY > 0.0f) // up
{
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bounceBottomBoundary)
{
realOffsetY = _bounceBottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY == 0.0f) // left
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _bounceRightBoundary)
{
realOffsetX = _bounceRightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
}
else if (touchOffsetX == 0.0f && touchOffsetY < 0.0f) // down
{
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _bounceTopBoundary)
{
realOffsetY = _bounceTopBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX > 0.0f && touchOffsetY == 0.0f) // right
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _bounceLeftBoundary)
{
realOffsetX = _bounceLeftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
}
}
else
{
if (touchOffsetX > 0.0f && touchOffsetY > 0.0f) // up right
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _leftBoundary)
{
realOffsetX = _leftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bottomBoundary)
{
realOffsetY = _bottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY > 0.0f) // up left
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _rightBoundary)
{
realOffsetX = _rightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bottomBoundary)
{
realOffsetY = _bottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY < 0.0f) // down left
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _rightBoundary)
{
realOffsetX = _rightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _topBoundary)
{
realOffsetY = _topBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX > 0.0f && touchOffsetY < 0.0f) // down right
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _leftBoundary)
{
realOffsetX = _leftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _topBoundary)
{
realOffsetY = _topBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX == 0.0f && touchOffsetY > 0.0f) // up
{
float icBottomPos = _innerContainer->getBottomInParent();
if (icBottomPos + touchOffsetY >= _bottomBoundary)
{
realOffsetY = _bottomBoundary - icBottomPos;
scrollToBottomEvent();
scrollenabled = false;
}
}
else if (touchOffsetX < 0.0f && touchOffsetY == 0.0f) // left
{
float icRightPos = _innerContainer->getRightInParent();
if (icRightPos + touchOffsetX <= _rightBoundary)
{
realOffsetX = _rightBoundary - icRightPos;
scrollToRightEvent();
scrollenabled = false;
}
}
else if (touchOffsetX == 0.0f && touchOffsetY < 0.0f) // down
{
float icTopPos = _innerContainer->getTopInParent();
if (icTopPos + touchOffsetY <= _topBoundary)
{
realOffsetY = _topBoundary - icTopPos;
scrollToTopEvent();
scrollenabled = false;
}
}
else if (touchOffsetX > 0.0f && touchOffsetY == 0.0f) // right
{
float icLeftPos = _innerContainer->getLeftInParent();
if (icLeftPos + touchOffsetX >= _leftBoundary)
{
realOffsetX = _leftBoundary - icLeftPos;
scrollToLeftEvent();
scrollenabled = false;
}
}
}
moveChildren(realOffsetX, realOffsetY);
scrollenabled = this->scrollChilderBoth(touchOffsetX, touchOffsetY);
break;
}
default:

View File

@ -357,7 +357,13 @@ protected:
void startBounceChildren(float v);
void stopBounceChildren();
bool checkCustomScrollDestination(float* touchOffsetX, float* touchOffsetY);
virtual bool scrollChildren(float touchOffsetX, float touchOffsetY);
virtual bool scrollChilderHorizontal(float touchOffsetX, float touchOffsetY);
virtual bool scrollChilderVertical(float touchOffsetX, float touchOffsetY);
virtual bool scrollChilderBoth(float touchOffsetX, float touchOffsetY);
bool bounceScrollChildren(float touchOffsetX, float touchOffsetY);
void startRecordSlidAction();
virtual void endRecordSlidAction();

View File

@ -62,7 +62,8 @@ _opacity(255),
_flippedX(false),
_flippedY(false),
_focused(false),
_focusEnabled(true)
_focusEnabled(true),
_layoutParameterType(LayoutParameter::Type::NONE)
{
onFocusChanged = CC_CALLBACK_2(Widget::onFocusChange,this);
onNextFocusedWidget = nullptr;
@ -815,24 +816,24 @@ bool Widget::isEnabled() const
return _enabled;
}
float Widget::getLeftInParent()
float Widget::getLeftBoundary() const
{
return getPosition().x - getAnchorPoint().x * _size.width;;
return getPosition().x - getAnchorPoint().x * _size.width;
}
float Widget::getBottomInParent()
float Widget::getBottomBoundary() const
{
return getPosition().y - getAnchorPoint().y * _size.height;;
return getPosition().y - getAnchorPoint().y * _size.height;
}
float Widget::getRightInParent()
float Widget::getRightBoundary() const
{
return getLeftInParent() + _size.width;
return getLeftBoundary() + _size.width;
}
float Widget::getTopInParent()
float Widget::getTopBoundary() const
{
return getBottomInParent() + _size.height;
return getBottomBoundary() + _size.height;
}
const Vec2& Widget::getTouchStartPos()
@ -868,6 +869,12 @@ void Widget::setLayoutParameter(LayoutParameter *parameter)
return;
}
_layoutParameterDictionary.insert((int)parameter->getLayoutType(), parameter);
_layoutParameterType = parameter->getLayoutType();
}
LayoutParameter* Widget::getLayoutParameter()
{
return dynamic_cast<LayoutParameter*>(_layoutParameterDictionary.at((int)_layoutParameterType));
}
LayoutParameter* Widget::getLayoutParameter(LayoutParameter::Type type)

View File

@ -28,6 +28,7 @@ THE SOFTWARE.
#include "ui/CCProtectedNode.h"
#include "ui/UILayoutParameter.h"
#include "ui/GUIDefine.h"
#include "ui/UILayoutParameter.h"
NS_CC_BEGIN
@ -57,7 +58,7 @@ CC_DEPRECATED_ATTRIBUTE typedef void (Ref::*SEL_TouchEvent)(Ref*,TouchEventType)
#endif
class Widget : public ProtectedNode
class Widget : public ProtectedNode, public LayoutParameterProtocol
{
public:
enum class FocusDirection
@ -200,28 +201,32 @@ public:
*
* @return The left boundary position of this widget.
*/
float getLeftInParent();
CC_DEPRECATED_ATTRIBUTE float getLeftInParent(){return this->getLeftBoundary();}
float getLeftBoundary() const;
/**
* Gets the bottom boundary position of this widget.
*
* @return The bottom boundary position of this widget.
*/
float getBottomInParent();
CC_DEPRECATED_ATTRIBUTE float getBottomInParent(){return this->getBottomBoundary();}
float getBottomBoundary() const;
/**
* Gets the right boundary position of this widget.
*
* @return The right boundary position of this widget.
*/
float getRightInParent();
CC_DEPRECATED_ATTRIBUTE float getRightInParent(){return this->getRightBoundary();}
float getRightBoundary() const;
/**
* Gets the top boundary position of this widget.
*
* @return The top boundary position of this widget.
*/
float getTopInParent();
CC_DEPRECATED_ATTRIBUTE float getTopInParent(){return this->getTopBoundary();}
float getTopBoundary() const;
/**
* Gets a child from the container with its name
@ -479,7 +484,9 @@ public:
*
* @return LayoutParameter
*/
LayoutParameter* getLayoutParameter(LayoutParameter::Type type);
LayoutParameter* getLayoutParameter() override;
CC_DEPRECATED_ATTRIBUTE LayoutParameter* getLayoutParameter(LayoutParameter::Type type);
/**
* Ignore the widget size
@ -667,9 +674,12 @@ protected:
Size _size;
Size _customSize;
bool _ignoreSize;
bool _affectByClipping;
SizeType _sizeType;
Vec2 _sizePercent;
bool _affectByClipping;
PositionType _positionType;
Vec2 _positionPercent;
bool _reorderWidgetChildDirty;
@ -679,7 +689,9 @@ protected:
GLubyte _opacity;
bool _flippedX;
bool _flippedY;
Map<int, LayoutParameter*> _layoutParameterDictionary;
//use map to enble switch back and forth for user layout parameters
Map<int,LayoutParameter*> _layoutParameterDictionary;
LayoutParameter::Type _layoutParameterType;
bool _focused;
bool _focusEnabled;

View File

@ -19,6 +19,7 @@
<ClInclude Include="..\UIHelper.h" />
<ClInclude Include="..\UIImageView.h" />
<ClInclude Include="..\UILayout.h" />
<ClInclude Include="..\UILayoutExecutant.h" />
<ClInclude Include="..\UILayoutParameter.h" />
<ClInclude Include="..\UIListView.h" />
<ClInclude Include="..\UILoadingBar.h" />
@ -44,6 +45,7 @@
<ClCompile Include="..\UIHelper.cpp" />
<ClCompile Include="..\UIImageView.cpp" />
<ClCompile Include="..\UILayout.cpp" />
<ClCompile Include="..\UILayoutExecutant.cpp" />
<ClCompile Include="..\UILayoutParameter.cpp" />
<ClCompile Include="..\UIListView.cpp" />
<ClCompile Include="..\UILoadingBar.cpp" />

View File

@ -87,6 +87,9 @@
<ClInclude Include="..\UIVBox.h">
<Filter>Layouts</Filter>
</ClInclude>
<ClInclude Include="..\UILayoutExecutant.h">
<Filter>Layouts</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\UIScrollView.cpp">
@ -155,5 +158,8 @@
<ClCompile Include="..\UIVBox.cpp">
<Filter>Layouts</Filter>
</ClCompile>
<ClCompile Include="..\UILayoutExecutant.cpp">
<Filter>Layouts</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -60,13 +60,13 @@ bool UIScrollViewTest_Vertical::init()
Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
titleButton->setTitleText("Title Button");
titleButton->setPosition(Vec2(innerWidth / 2.0f, button->getBottomInParent() - button->getSize().height));
titleButton->setPosition(Vec2(innerWidth / 2.0f, button->getBottomBoundary() - button->getSize().height));
scrollView->addChild(titleButton);
Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
button_scale9->setScale9Enabled(true);
button_scale9->setSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
button_scale9->setPosition(Vec2(innerWidth / 2.0f, titleButton->getBottomInParent() - titleButton->getSize().height));
button_scale9->setPosition(Vec2(innerWidth / 2.0f, titleButton->getBottomBoundary() - titleButton->getSize().height));
scrollView->addChild(button_scale9);
imageView->setPosition(Vec2(innerWidth / 2.0f, imageView->getSize().height / 2.0f));
@ -137,19 +137,19 @@ bool UIScrollViewTest_Horizontal::init()
Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
titleButton->setTitleText("Title Button");
titleButton->setPosition(Vec2(button->getRightInParent() + button->getSize().width / 2.0f,
button->getBottomInParent() - button->getSize().height / 2.0f));
titleButton->setPosition(Vec2(button->getRightBoundary() + button->getSize().width / 2.0f,
button->getBottomBoundary() - button->getSize().height / 2.0f));
scrollView->addChild(titleButton);
Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
button_scale9->setScale9Enabled(true);
button_scale9->setSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
button_scale9->setPosition(Vec2(titleButton->getRightInParent() + titleButton->getSize().width / 2.0f,
titleButton->getBottomInParent() - titleButton->getSize().height / 2.0f));
button_scale9->setPosition(Vec2(titleButton->getRightBoundary() + titleButton->getSize().width / 2.0f,
titleButton->getBottomBoundary() - titleButton->getSize().height / 2.0f));
scrollView->addChild(button_scale9);
imageView->setPosition(Vec2(innerWidth - imageView->getSize().width / 2.0f,
button_scale9->getBottomInParent() - button_scale9->getSize().height / 2.0f));
button_scale9->getBottomBoundary() - button_scale9->getSize().height / 2.0f));
scrollView->addChild(imageView);
return true;