Merge pull request #9591 from liamcindy/Layout

update for lay out
This commit is contained in:
minggo 2014-12-24 14:27:08 +08:00
commit bb9fb9f0d6
29 changed files with 1763 additions and 360 deletions

View File

@ -762,6 +762,9 @@ Node* CSLoader::nodeWithFlatBuffersFile(const std::string &fileName)
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(textures->Get(i)->c_str());
}
auto v = csparsebinary->version();
if (v) _csdVersion = v->c_str();
Node* node = nodeWithFlatBuffers(csparsebinary->nodeTree());
return node;
@ -1089,6 +1092,10 @@ Node* CSLoader::createNodeWithFlatBuffersForSimulator(const std::string& filenam
}
auto nodeTree = csparsebinary->nodeTree();
auto v = csparsebinary->version();
if (v) _csdVersion = v->c_str();
Node* node = nodeWithFlatBuffersForSimulator(nodeTree);
_rootNode = nullptr;
@ -1201,6 +1208,7 @@ Node* CSLoader::nodeWithFlatBuffersForSimulator(const flatbuffers::NodeTree *nod
node->addChild(child);
}
}
Helper::doLayout(node);
}
// _loadingNodeParentHierarchy.pop_back();

View File

@ -100,6 +100,7 @@ public:
cocos2d::Node* createNodeWithFlatBuffersForSimulator(const std::string& filename);
cocos2d::Node* nodeWithFlatBuffersForSimulator(const flatbuffers::NodeTree* nodetree);
std::string getCsdVersion() { return _csdVersion; }
protected:
@ -147,6 +148,7 @@ protected:
Node* _rootNode;
// std::vector<Node*> _loadingNodeParentHierarchy;
std::string _csdVersion;
};
NS_CC_END

View File

@ -12,6 +12,7 @@ struct CSParseBinary;
struct NodeTree;
struct Options;
struct WidgetOptions;
struct LayoutComponentTable;
struct SingleNodeOptions;
struct SpriteOptions;
struct ParticleSystemOptions;
@ -176,6 +177,7 @@ struct CSParseBinary : private flatbuffers::Table {
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *texturePngs() const { return GetPointer<const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *>(6); }
const NodeTree *nodeTree() const { return GetPointer<const NodeTree *>(8); }
const NodeAction *action() const { return GetPointer<const NodeAction *>(10); }
const flatbuffers::String *version() const { return GetPointer<const flatbuffers::String *>(12); }
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<flatbuffers::uoffset_t>(verifier, 4 /* textures */) &&
@ -188,6 +190,8 @@ struct CSParseBinary : private flatbuffers::Table {
verifier.VerifyTable(nodeTree()) &&
VerifyField<flatbuffers::uoffset_t>(verifier, 10 /* action */) &&
verifier.VerifyTable(action()) &&
VerifyField<flatbuffers::uoffset_t>(verifier, 12 /* version */) &&
verifier.Verify(version()) &&
verifier.EndTable();
}
};
@ -199,10 +203,11 @@ struct CSParseBinaryBuilder {
void add_texturePngs(flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> texturePngs) { fbb_.AddOffset(6, texturePngs); }
void add_nodeTree(flatbuffers::Offset<NodeTree> nodeTree) { fbb_.AddOffset(8, nodeTree); }
void add_action(flatbuffers::Offset<NodeAction> action) { fbb_.AddOffset(10, action); }
void add_version(flatbuffers::Offset<flatbuffers::String> version) { fbb_.AddOffset(12, version); }
CSParseBinaryBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
CSParseBinaryBuilder &operator=(const CSParseBinaryBuilder &);
flatbuffers::Offset<CSParseBinary> Finish() {
auto o = flatbuffers::Offset<CSParseBinary>(fbb_.EndTable(start_, 4));
auto o = flatbuffers::Offset<CSParseBinary>(fbb_.EndTable(start_, 5));
return o;
}
};
@ -211,8 +216,10 @@ inline flatbuffers::Offset<CSParseBinary> CreateCSParseBinary(flatbuffers::FlatB
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> textures = 0,
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> texturePngs = 0,
flatbuffers::Offset<NodeTree> nodeTree = 0,
flatbuffers::Offset<NodeAction> action = 0) {
flatbuffers::Offset<NodeAction> action = 0,
flatbuffers::Offset<flatbuffers::String> version = 0) {
CSParseBinaryBuilder builder_(_fbb);
builder_.add_version(version);
builder_.add_action(action);
builder_.add_nodeTree(nodeTree);
builder_.add_texturePngs(texturePngs);
@ -318,6 +325,7 @@ struct WidgetOptions : private flatbuffers::Table {
const flatbuffers::String *customProperty() const { return GetPointer<const flatbuffers::String *>(38); }
const flatbuffers::String *callBackType() const { return GetPointer<const flatbuffers::String *>(40); }
const flatbuffers::String *callBackName() const { return GetPointer<const flatbuffers::String *>(42); }
const LayoutComponentTable *layoutComponent() const { return GetPointer<const LayoutComponentTable *>(44); }
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<flatbuffers::uoffset_t>(verifier, 4 /* name */) &&
@ -345,6 +353,8 @@ struct WidgetOptions : private flatbuffers::Table {
verifier.Verify(callBackType()) &&
VerifyField<flatbuffers::uoffset_t>(verifier, 42 /* callBackName */) &&
verifier.Verify(callBackName()) &&
VerifyField<flatbuffers::uoffset_t>(verifier, 44 /* layoutComponent */) &&
verifier.VerifyTable(layoutComponent()) &&
verifier.EndTable();
}
};
@ -372,10 +382,11 @@ struct WidgetOptionsBuilder {
void add_customProperty(flatbuffers::Offset<flatbuffers::String> customProperty) { fbb_.AddOffset(38, customProperty); }
void add_callBackType(flatbuffers::Offset<flatbuffers::String> callBackType) { fbb_.AddOffset(40, callBackType); }
void add_callBackName(flatbuffers::Offset<flatbuffers::String> callBackName) { fbb_.AddOffset(42, callBackName); }
void add_layoutComponent(flatbuffers::Offset<LayoutComponentTable> layoutComponent) { fbb_.AddOffset(44, layoutComponent); }
WidgetOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
WidgetOptionsBuilder &operator=(const WidgetOptionsBuilder &);
flatbuffers::Offset<WidgetOptions> Finish() {
auto o = flatbuffers::Offset<WidgetOptions>(fbb_.EndTable(start_, 20));
auto o = flatbuffers::Offset<WidgetOptions>(fbb_.EndTable(start_, 21));
return o;
}
};
@ -400,8 +411,10 @@ inline flatbuffers::Offset<WidgetOptions> CreateWidgetOptions(flatbuffers::FlatB
flatbuffers::Offset<flatbuffers::String> frameEvent = 0,
flatbuffers::Offset<flatbuffers::String> customProperty = 0,
flatbuffers::Offset<flatbuffers::String> callBackType = 0,
flatbuffers::Offset<flatbuffers::String> callBackName = 0) {
flatbuffers::Offset<flatbuffers::String> callBackName = 0,
flatbuffers::Offset<LayoutComponentTable> layoutComponent = 0) {
WidgetOptionsBuilder builder_(_fbb);
builder_.add_layoutComponent(layoutComponent);
builder_.add_callBackName(callBackName);
builder_.add_callBackType(callBackType);
builder_.add_customProperty(customProperty);
@ -425,6 +438,111 @@ inline flatbuffers::Offset<WidgetOptions> CreateWidgetOptions(flatbuffers::FlatB
return builder_.Finish();
}
struct LayoutComponentTable : private flatbuffers::Table {
uint8_t positionXPercentEnabled() const { return GetField<uint8_t>(4, 0); }
uint8_t positionYPercentEnabled() const { return GetField<uint8_t>(6, 0); }
float positionXPercent() const { return GetField<float>(8, 0); }
float positionYPercent() const { return GetField<float>(10, 0); }
uint8_t sizeXPercentEnable() const { return GetField<uint8_t>(12, 0); }
uint8_t sizeYPercentEnable() const { return GetField<uint8_t>(14, 0); }
float sizeXPercent() const { return GetField<float>(16, 0); }
float sizeYPercent() const { return GetField<float>(18, 0); }
uint8_t stretchHorizontalEnabled() const { return GetField<uint8_t>(20, 0); }
uint8_t stretchVerticalEnabled() const { return GetField<uint8_t>(22, 0); }
const flatbuffers::String *horizontalEdge() const { return GetPointer<const flatbuffers::String *>(24); }
const flatbuffers::String *verticalEdge() const { return GetPointer<const flatbuffers::String *>(26); }
float leftMargin() const { return GetField<float>(28, 0); }
float rightMargin() const { return GetField<float>(30, 0); }
float topMargin() const { return GetField<float>(32, 0); }
float bottomMargin() const { return GetField<float>(34, 0); }
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<uint8_t>(verifier, 4 /* positionXPercentEnabled */) &&
VerifyField<uint8_t>(verifier, 6 /* positionYPercentEnabled */) &&
VerifyField<float>(verifier, 8 /* positionXPercent */) &&
VerifyField<float>(verifier, 10 /* positionYPercent */) &&
VerifyField<uint8_t>(verifier, 12 /* sizeXPercentEnable */) &&
VerifyField<uint8_t>(verifier, 14 /* sizeYPercentEnable */) &&
VerifyField<float>(verifier, 16 /* sizeXPercent */) &&
VerifyField<float>(verifier, 18 /* sizeYPercent */) &&
VerifyField<uint8_t>(verifier, 20 /* stretchHorizontalEnabled */) &&
VerifyField<uint8_t>(verifier, 22 /* stretchVerticalEnabled */) &&
VerifyField<flatbuffers::uoffset_t>(verifier, 24 /* horizontalEdge */) &&
verifier.Verify(horizontalEdge()) &&
VerifyField<flatbuffers::uoffset_t>(verifier, 26 /* verticalEdge */) &&
verifier.Verify(verticalEdge()) &&
VerifyField<float>(verifier, 28 /* leftMargin */) &&
VerifyField<float>(verifier, 30 /* rightMargin */) &&
VerifyField<float>(verifier, 32 /* topMargin */) &&
VerifyField<float>(verifier, 34 /* bottomMargin */) &&
verifier.EndTable();
}
};
struct LayoutComponentTableBuilder {
flatbuffers::FlatBufferBuilder &fbb_;
flatbuffers::uoffset_t start_;
void add_positionXPercentEnabled(uint8_t positionXPercentEnabled) { fbb_.AddElement<uint8_t>(4, positionXPercentEnabled, 0); }
void add_positionYPercentEnabled(uint8_t positionYPercentEnabled) { fbb_.AddElement<uint8_t>(6, positionYPercentEnabled, 0); }
void add_positionXPercent(float positionXPercent) { fbb_.AddElement<float>(8, positionXPercent, 0); }
void add_positionYPercent(float positionYPercent) { fbb_.AddElement<float>(10, positionYPercent, 0); }
void add_sizeXPercentEnable(uint8_t sizeXPercentEnable) { fbb_.AddElement<uint8_t>(12, sizeXPercentEnable, 0); }
void add_sizeYPercentEnable(uint8_t sizeYPercentEnable) { fbb_.AddElement<uint8_t>(14, sizeYPercentEnable, 0); }
void add_sizeXPercent(float sizeXPercent) { fbb_.AddElement<float>(16, sizeXPercent, 0); }
void add_sizeYPercent(float sizeYPercent) { fbb_.AddElement<float>(18, sizeYPercent, 0); }
void add_stretchHorizontalEnabled(uint8_t stretchHorizontalEnabled) { fbb_.AddElement<uint8_t>(20, stretchHorizontalEnabled, 0); }
void add_stretchVerticalEnabled(uint8_t stretchVerticalEnabled) { fbb_.AddElement<uint8_t>(22, stretchVerticalEnabled, 0); }
void add_horizontalEdge(flatbuffers::Offset<flatbuffers::String> horizontalEdge) { fbb_.AddOffset(24, horizontalEdge); }
void add_verticalEdge(flatbuffers::Offset<flatbuffers::String> verticalEdge) { fbb_.AddOffset(26, verticalEdge); }
void add_leftMargin(float leftMargin) { fbb_.AddElement<float>(28, leftMargin, 0); }
void add_rightMargin(float rightMargin) { fbb_.AddElement<float>(30, rightMargin, 0); }
void add_topMargin(float topMargin) { fbb_.AddElement<float>(32, topMargin, 0); }
void add_bottomMargin(float bottomMargin) { fbb_.AddElement<float>(34, bottomMargin, 0); }
LayoutComponentTableBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
LayoutComponentTableBuilder &operator=(const LayoutComponentTableBuilder &);
flatbuffers::Offset<LayoutComponentTable> Finish() {
auto o = flatbuffers::Offset<LayoutComponentTable>(fbb_.EndTable(start_, 16));
return o;
}
};
inline flatbuffers::Offset<LayoutComponentTable> CreateLayoutComponentTable(flatbuffers::FlatBufferBuilder &_fbb,
uint8_t positionXPercentEnabled = 0,
uint8_t positionYPercentEnabled = 0,
float positionXPercent = 0,
float positionYPercent = 0,
uint8_t sizeXPercentEnable = 0,
uint8_t sizeYPercentEnable = 0,
float sizeXPercent = 0,
float sizeYPercent = 0,
uint8_t stretchHorizontalEnabled = 0,
uint8_t stretchVerticalEnabled = 0,
flatbuffers::Offset<flatbuffers::String> horizontalEdge = 0,
flatbuffers::Offset<flatbuffers::String> verticalEdge = 0,
float leftMargin = 0,
float rightMargin = 0,
float topMargin = 0,
float bottomMargin = 0) {
LayoutComponentTableBuilder builder_(_fbb);
builder_.add_bottomMargin(bottomMargin);
builder_.add_topMargin(topMargin);
builder_.add_rightMargin(rightMargin);
builder_.add_leftMargin(leftMargin);
builder_.add_verticalEdge(verticalEdge);
builder_.add_horizontalEdge(horizontalEdge);
builder_.add_sizeYPercent(sizeYPercent);
builder_.add_sizeXPercent(sizeXPercent);
builder_.add_positionYPercent(positionYPercent);
builder_.add_positionXPercent(positionXPercent);
builder_.add_stretchVerticalEnabled(stretchVerticalEnabled);
builder_.add_stretchHorizontalEnabled(stretchHorizontalEnabled);
builder_.add_sizeYPercentEnable(sizeYPercentEnable);
builder_.add_sizeXPercentEnable(sizeXPercentEnable);
builder_.add_positionYPercentEnabled(positionYPercentEnabled);
builder_.add_positionXPercentEnabled(positionXPercentEnabled);
return builder_.Finish();
}
struct SingleNodeOptions : private flatbuffers::Table {
const WidgetOptions *nodeOptions() const { return GetPointer<const WidgetOptions *>(4); }
bool Verify(flatbuffers::Verifier &verifier) const {
@ -1557,12 +1675,16 @@ inline flatbuffers::Offset<ListViewOptions> CreateListViewOptions(flatbuffers::F
struct ProjectNodeOptions : private flatbuffers::Table {
const WidgetOptions *nodeOptions() const { return GetPointer<const WidgetOptions *>(4); }
const flatbuffers::String *fileName() const { return GetPointer<const flatbuffers::String *>(6); }
uint8_t isLoop() const { return GetField<uint8_t>(8, 1); }
uint8_t isAutoPlay() const { return GetField<uint8_t>(10, 1); }
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<flatbuffers::uoffset_t>(verifier, 4 /* nodeOptions */) &&
verifier.VerifyTable(nodeOptions()) &&
VerifyField<flatbuffers::uoffset_t>(verifier, 6 /* fileName */) &&
verifier.Verify(fileName()) &&
VerifyField<uint8_t>(verifier, 8 /* isLoop */) &&
VerifyField<uint8_t>(verifier, 10 /* isAutoPlay */) &&
verifier.EndTable();
}
};
@ -1572,20 +1694,26 @@ struct ProjectNodeOptionsBuilder {
flatbuffers::uoffset_t start_;
void add_nodeOptions(flatbuffers::Offset<WidgetOptions> nodeOptions) { fbb_.AddOffset(4, nodeOptions); }
void add_fileName(flatbuffers::Offset<flatbuffers::String> fileName) { fbb_.AddOffset(6, fileName); }
void add_isLoop(uint8_t isLoop) { fbb_.AddElement<uint8_t>(8, isLoop, 1); }
void add_isAutoPlay(uint8_t isAutoPlay) { fbb_.AddElement<uint8_t>(10, isAutoPlay, 1); }
ProjectNodeOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
ProjectNodeOptionsBuilder &operator=(const ProjectNodeOptionsBuilder &);
flatbuffers::Offset<ProjectNodeOptions> Finish() {
auto o = flatbuffers::Offset<ProjectNodeOptions>(fbb_.EndTable(start_, 2));
auto o = flatbuffers::Offset<ProjectNodeOptions>(fbb_.EndTable(start_, 4));
return o;
}
};
inline flatbuffers::Offset<ProjectNodeOptions> CreateProjectNodeOptions(flatbuffers::FlatBufferBuilder &_fbb,
flatbuffers::Offset<WidgetOptions> nodeOptions = 0,
flatbuffers::Offset<flatbuffers::String> fileName = 0) {
flatbuffers::Offset<flatbuffers::String> fileName = 0,
uint8_t isLoop = 1,
uint8_t isAutoPlay = 1) {
ProjectNodeOptionsBuilder builder_(_fbb);
builder_.add_fileName(fileName);
builder_.add_nodeOptions(nodeOptions);
builder_.add_isAutoPlay(isAutoPlay);
builder_.add_isLoop(isLoop);
return builder_.Finish();
}

View File

@ -167,6 +167,14 @@ std::string FlatBuffersSerialize::serializeFlatBuffersWithXMLFile(const std::str
while (element)
{
// CCLOG("entity name = %s", element->Name());
if (strcmp("PropertyGroup", element->Name()) == 0)
{
const tinyxml2::XMLAttribute* attribute = element->FirstAttribute();
while (attribute && strcmp("Version", attribute->Name()) != 0)
attribute = attribute->Next();
if (attribute)
_csdVersion = attribute->Value();
}
if (strcmp("Content", element->Name()) == 0)
{
@ -248,10 +256,11 @@ std::string FlatBuffersSerialize::serializeFlatBuffersWithXMLFile(const std::str
auto csparsebinary = CreateCSParseBinary(*_builder,
_builder->CreateVector(_textures),
_builder->CreateVector(_texturePngs),
_builder->CreateVector(_texturePngs),
nodeTree,
aciton);
_builder->Finish(csparsebinary);
aciton,
_builder->CreateString(_csdVersion));
_builder->Finish(csparsebinary);
_textures.clear();
_texturePngs.clear();
@ -988,6 +997,14 @@ FlatBufferBuilder* FlatBuffersSerialize::createFlatBuffersWithXMLFileForSimulato
while (element)
{
// CCLOG("entity name = %s", element->Name());
if (strcmp("PropertyGroup", element->Name()) == 0)
{
const tinyxml2::XMLAttribute* attribute = element->FirstAttribute();
while (attribute && strcmp("Version", attribute->Name()) != 0)
attribute = attribute->Next();
if (attribute)
_csdVersion = attribute->Value();
}
if (strcmp("Content", element->Name()) == 0)
{
@ -1046,10 +1063,11 @@ FlatBufferBuilder* FlatBuffersSerialize::createFlatBuffersWithXMLFileForSimulato
}
auto csparsebinary = CreateCSParseBinary(*_builder,
_builder->CreateVector(_textures),
_builder->CreateVector(_texturePngs),
_builder->CreateVector(_textures),
_builder->CreateVector(_texturePngs),
nodeTree,
aciton);
aciton,
_builder->CreateString(_csdVersion));
_builder->Finish(csparsebinary);
_textures.clear();

View File

@ -128,6 +128,7 @@ public:
std::string classType);
flatbuffers::Offset<flatbuffers::ProjectNodeOptions> createProjectNodeOptionsForSimulator(const tinyxml2::XMLElement* objectData);
/**/
std::string getCsdVersion() { return _csdVersion; }
public:
std::vector<flatbuffers::Offset<flatbuffers::String>> _textures;
@ -137,7 +138,7 @@ public:
private:
flatbuffers::FlatBufferBuilder* _builder;
flatbuffers::Offset<flatbuffers::CSParseBinary>* _csparsebinary;
std::string _csdVersion;
};
}

View File

@ -796,6 +796,11 @@ namespace cocostudio
Size scale9Size(options->scale9Size()->width(), options->scale9Size()->height());
button->setContentSize(scale9Size);
}
else
{
Size contentSize(options->widgetOptions()->size()->width(), options->widgetOptions()->size()->height());
button->setContentSize(contentSize);
}
}
Node* ButtonReader::createNodeWithFlatBuffers(const flatbuffers::Table *buttonOptions)

View File

@ -375,7 +375,11 @@ namespace cocostudio
Rect capInsets(f_capInset->x(), f_capInset->y(), f_capInset->width(), f_capInset->height());
imageView->setCapInsets(capInsets);
}
else
{
Size contentSize(options->widgetOptions()->size()->width(), options->widgetOptions()->size()->height());
imageView->setContentSize(contentSize);
}
}
Node* ImageViewReader::createNodeWithFlatBuffers(const flatbuffers::Table *imageViewOptions)

View File

@ -29,13 +29,31 @@
#include "tinyxml2.h"
#include "flatbuffers/flatbuffers.h"
#include "ui/UILayoutComponent.h"
USING_NS_CC;
using namespace flatbuffers;
namespace cocostudio
{
const char* Layout_PositionPercentXEnabled = "PositionPercentXEnable";
const char* Layout_PositionPercentYEnabled = "PositionPercentYEnable";
const char* Layout_PercentWidthEnable = "PercentWidthEnable";
const char* Layout_PercentHeightEnable = "PercentHeightEnable";
const char* Layout_StretchWidthEnable = "StretchWidthEnable";
const char* Layout_StretchHeightEnable = "StretchHeightEnable";
const char* Layout_HorizontalEdge = "HorizontalEdge";
const char* Layout_VerticalEdge = "VerticalEdge";
const char* Layout_LeftMargin = "LeftMargin";
const char* Layout_RightMargin = "RightMargin";
const char* Layout_TopMargin = "TopMargin";
const char* Layout_BottomMargin = "BottomMargin";
const char* Layout_BothEdge = "BothEdge";
const char* Layout_LeftEdge = "LeftEdge";
const char* Layout_RightEdge = "RightEdge";
const char* Layout_TopEdge = "TopEdge";
const char* Layout_BottomEdge = "BottomEdge";
IMPLEMENT_CLASS_NODE_READER_INFO(NodeReader)
NodeReader::NodeReader()
@ -88,6 +106,23 @@ namespace cocostudio
std::string frameEvent = "";
std::string customProperty = "";
bool positionXPercentEnabled = false;
bool positionYPercentEnabled = false;
float positionXPercent = 0;
float positionYPercent = 0;
bool sizeXPercentEnable = false;
bool sizeYPercentEnable = false;
float sizeXPercent = 0;
float sizeYPercent = 0;
bool stretchHorizontalEnabled = false;
bool stretchVerticalEnabled = false;
std::string horizontalEdge;
std::string verticalEdge;
float leftMargin = 0;
float rightMargin = 0;
float topMargin = 0;
float bottomMargin = 0;
// attributes
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
@ -151,6 +186,54 @@ namespace cocostudio
{
frameEvent = value;
}
else if (attriname == Layout_PositionPercentXEnabled)
{
positionXPercentEnabled = value == "True";
}
else if (attriname == Layout_PositionPercentYEnabled)
{
positionYPercentEnabled = value == "True";
}
else if (attriname == Layout_PercentWidthEnable)
{
sizeXPercentEnable = value == "True";
}
else if (attriname == Layout_PercentHeightEnable)
{
sizeYPercentEnable = value == "True";
}
else if (attriname == Layout_StretchWidthEnable)
{
stretchHorizontalEnabled = value == "True";
}
else if (attriname == Layout_StretchHeightEnable)
{
stretchVerticalEnabled = value == "True";
}
else if (attriname == Layout_HorizontalEdge)
{
horizontalEdge = value;
}
else if (attriname == Layout_VerticalEdge)
{
verticalEdge = value;
}
else if (attriname == Layout_LeftMargin)
{
leftMargin = atof(value.c_str());
}
else if (attriname == Layout_RightMargin)
{
rightMargin = atof(value.c_str());
}
else if (attriname == Layout_TopMargin)
{
topMargin = atof(value.c_str());
}
else if (attriname == Layout_BottomMargin)
{
bottomMargin = atof(value.c_str());
}
attribute = attribute->Next();
}
@ -276,7 +359,48 @@ namespace cocostudio
attribute = attribute->Next();
}
}
else if (attriname == "PrePosition")
{
attribute = child->FirstAttribute();
while (attribute)
{
attriname = attribute->Name();
std::string value = attribute->Value();
if (attriname == "X")
{
positionXPercent = atof(value.c_str());
}
else if (attriname == "Y")
{
positionYPercent = atof(value.c_str());
}
attribute = attribute->Next();
}
}
else if (attriname == "PreSize")
{
attribute = child->FirstAttribute();
while (attribute)
{
attriname = attribute->Name();
std::string value = attribute->Value();
if (attriname == "X")
{
sizeXPercent = atof(value.c_str());
}
else if (attriname == "Y")
{
sizeYPercent = atof(value.c_str());
}
attribute = attribute->Next();
}
}
child = child->NextSiblingElement();
}
@ -286,7 +410,23 @@ namespace cocostudio
AnchorPoint f_anchortpoint(anchorPoint.x, anchorPoint.y);
Color f_color(color.a, color.r, color.g, color.b);
FlatSize f_size(size.x, size.y);
auto f_layoutComponent = CreateLayoutComponentTable(*builder,
positionXPercentEnabled,
positionYPercentEnabled,
positionXPercent,
positionYPercent,
sizeXPercentEnable,
sizeYPercentEnable,
sizeXPercent,
sizeYPercent,
stretchHorizontalEnabled,
stretchVerticalEnabled,
builder->CreateString(horizontalEdge),
builder->CreateString(verticalEdge),
leftMargin,
rightMargin,
topMargin,
bottomMargin);
auto options = CreateWidgetOptions(*builder,
builder->CreateString(name),
@ -306,8 +446,10 @@ namespace cocostudio
ignoreSize,
touchEnabled,
builder->CreateString(frameEvent),
builder->CreateString(customProperty)
);
builder->CreateString(customProperty),
0,
0,
f_layoutComponent);
return *(Offset<Table>*)(&options);
}
@ -368,6 +510,78 @@ namespace cocostudio
node->setCascadeColorEnabled(true);
node->setCascadeOpacityEnabled(true);
setLayoutComponentPropsWithFlatBuffers(node, nodeOptions);
}
void NodeReader::setLayoutComponentPropsWithFlatBuffers(cocos2d::Node* node, const flatbuffers::Table* nodeOptions)
{
auto layoutComponentTable = ((WidgetOptions*)nodeOptions)->layoutComponent();
if (!layoutComponentTable) return;
bool positionXPercentEnabled = layoutComponentTable->positionXPercentEnabled();
bool positionYPercentEnabled = layoutComponentTable->positionYPercentEnabled();
float positionXPercent = layoutComponentTable->positionXPercent();
float positionYPercent = layoutComponentTable->positionYPercent();
bool sizeXPercentEnable = layoutComponentTable->sizeXPercentEnable();
bool sizeYPercentEnable = layoutComponentTable->sizeYPercentEnable();
float sizeXPercent = layoutComponentTable->sizeXPercent();
float sizeYPercent = layoutComponentTable->sizeYPercent();
bool stretchHorizontalEnabled = layoutComponentTable->stretchHorizontalEnabled();
bool stretchVerticalEnabled = layoutComponentTable->stretchVerticalEnabled();
std::string horizontalEdge = layoutComponentTable->horizontalEdge()->c_str();
std::string verticalEdge = layoutComponentTable->verticalEdge()->c_str();
float leftMargin = layoutComponentTable->leftMargin();
float rightMargin = layoutComponentTable->rightMargin();
float topMargin = layoutComponentTable->topMargin();
float bottomMargin = layoutComponentTable->bottomMargin();
auto layoutComponent = ui::LayoutComponent::create();
node->addComponent(layoutComponent);
layoutComponent->setPositionPercentXEnabled(positionXPercentEnabled);
layoutComponent->setPositionPercentYEnabled(positionYPercentEnabled);
layoutComponent->setPositionPercentX(positionXPercent);
layoutComponent->setPositionPercentY(positionYPercent);
layoutComponent->setPercentWidthEnabled(sizeXPercentEnable);
layoutComponent->setPercentHeightEnabled(sizeYPercentEnable);
layoutComponent->setPercentWidth(sizeXPercent);
layoutComponent->setPercentHeight(sizeYPercent);
layoutComponent->setStretchWidthEnabled(stretchHorizontalEnabled);
layoutComponent->setStretchHeightEnabled(stretchVerticalEnabled);
ui::LayoutComponent::HorizontalEdge horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::None;
if (horizontalEdge == Layout_LeftEdge)
{
horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::Left;
}
else if (horizontalEdge == Layout_RightEdge)
{
horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::Right;
}
else if (horizontalEdge == Layout_BothEdge)
{
horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::Center;
}
layoutComponent->setHorizontalEdge(horizontalEdgeType);
ui::LayoutComponent::VerticalEdge verticalEdgeType = ui::LayoutComponent::VerticalEdge::None;
if (verticalEdge == Layout_TopEdge)
{
verticalEdgeType = ui::LayoutComponent::VerticalEdge::Top;
}
else if (verticalEdge == Layout_BottomEdge)
{
verticalEdgeType = ui::LayoutComponent::VerticalEdge::Bottom;
}
else if (verticalEdge == Layout_BothEdge)
{
verticalEdgeType = ui::LayoutComponent::VerticalEdge::Center;
}
layoutComponent->setVerticalEdge(verticalEdgeType);
layoutComponent->setTopMargin(topMargin);
layoutComponent->setBottomMargin(bottomMargin);
layoutComponent->setLeftMargin(leftMargin);
layoutComponent->setRightMargin(rightMargin);
}
Node* NodeReader::createNodeWithFlatBuffers(const flatbuffers::Table *nodeOptions)

View File

@ -46,6 +46,7 @@ namespace cocostudio
flatbuffers::Offset<flatbuffers::Table> createOptionsWithFlatBuffers(const tinyxml2::XMLElement* objectData,
flatbuffers::FlatBufferBuilder* builder);
void setPropsWithFlatBuffers(cocos2d::Node* node, const flatbuffers::Table* nodeOptions);
void setLayoutComponentPropsWithFlatBuffers(cocos2d::Node* node, const flatbuffers::Table* nodeOptions);
cocos2d::Node* createNodeWithFlatBuffers(const flatbuffers::Table* nodeOptions);
};
}

View File

@ -9,6 +9,8 @@
#include "tinyxml2.h"
#include "flatbuffers/flatbuffers.h"
#include "ui/UILayoutComponent.h"
#include "cocostudio/ActionTimeline/CSLoader.h"
USING_NS_CC;
using namespace ui;
@ -20,7 +22,6 @@ using namespace flatbuffers;
namespace cocostudio
{
const char* P_IgnoreSize = "ignoreSize";
const char* P_SizeType = "sizeType";
const char* P_PositionType = "positionType";
@ -66,6 +67,24 @@ namespace cocostudio
const char* P_ResourceType = "resourceType";
const char* P_Path = "path";
const char* P_Layout_PositionPercentXEnabled = "PositionPercentXEnable";
const char* P_Layout_PositionPercentYEnabled = "PositionPercentYEnable";
const char* P_Layout_PercentWidthEnable = "PercentWidthEnable";
const char* P_Layout_PercentHeightEnable = "PercentHeightEnable";
const char* P_Layout_StretchWidthEnable = "StretchWidthEnable";
const char* P_Layout_StretchHeightEnable = "StretchHeightEnable";
const char* P_Layout_HorizontalEdge = "HorizontalEdge";
const char* P_Layout_VerticalEdge = "VerticalEdge";
const char* P_Layout_LeftMargin = "LeftMargin";
const char* P_Layout_RightMargin = "RightMargin";
const char* P_Layout_TopMargin = "TopMargin";
const char* P_Layout_BottomMargin = "BottomMargin";
const char* P_Layout_BothEdge = "BothEdge";
const char* P_Layout_LeftEdge = "LeftEdge";
const char* P_Layout_RightEdge = "RightEdge";
const char* P_Layout_TopEdge = "TopEdge";
const char* P_Layout_BottomEdge = "BottomEdge";
static WidgetReader* instanceWidgetReader = nullptr;
@ -385,6 +404,23 @@ namespace cocostudio
std::string callbackType = "";
std::string callbackName = "";
bool positionXPercentEnabled = false;
bool positionYPercentEnabled = false;
float positionXPercent = 0;
float positionYPercent = 0;
bool sizeXPercentEnable = false;
bool sizeYPercentEnable = false;
float sizeXPercent = 0;
float sizeYPercent = 0;
bool stretchHorizontalEnabled = false;
bool stretchVerticalEnabled = false;
std::string horizontalEdge;
std::string verticalEdge;
float leftMargin = 0;
float rightMargin = 0;
float topMargin = 0;
float bottomMargin = 0;
// attributes
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
@ -456,6 +492,54 @@ namespace cocostudio
{
callbackName = value;
}
else if (attriname == P_Layout_PositionPercentXEnabled)
{
positionXPercentEnabled = value == "True";
}
else if (attriname == P_Layout_PositionPercentYEnabled)
{
positionYPercentEnabled = value == "True";
}
else if (attriname == P_Layout_PercentWidthEnable)
{
sizeXPercentEnable = value == "True";
}
else if (attriname == P_Layout_PercentHeightEnable)
{
sizeYPercentEnable = value == "True";
}
else if (attriname == P_Layout_StretchWidthEnable)
{
stretchHorizontalEnabled = value == "True";
}
else if (attriname == P_Layout_StretchHeightEnable)
{
stretchVerticalEnabled = value == "True";
}
else if (attriname == P_Layout_HorizontalEdge)
{
horizontalEdge = value;
}
else if (attriname == P_Layout_VerticalEdge)
{
verticalEdge = value;
}
else if (attriname == P_Layout_LeftMargin)
{
leftMargin = atof(value.c_str());
}
else if (attriname == P_Layout_RightMargin)
{
rightMargin = atof(value.c_str());
}
else if (attriname == P_Layout_TopMargin)
{
topMargin = atof(value.c_str());
}
else if (attriname == P_Layout_BottomMargin)
{
bottomMargin = atof(value.c_str());
}
attribute = attribute->Next();
}
@ -581,6 +665,48 @@ namespace cocostudio
attribute = attribute->Next();
}
}
else if (attriname == "PrePosition")
{
attribute = child->FirstAttribute();
while (attribute)
{
attriname = attribute->Name();
std::string value = attribute->Value();
if (attriname == "X")
{
positionXPercent = atof(value.c_str());
}
else if (attriname == "Y")
{
positionYPercent = atof(value.c_str());
}
attribute = attribute->Next();
}
}
else if (attriname == "PreSize")
{
attribute = child->FirstAttribute();
while (attribute)
{
attriname = attribute->Name();
std::string value = attribute->Value();
if (attriname == "X")
{
sizeXPercent = atof(value.c_str());
}
else if (attriname == "Y")
{
sizeYPercent = atof(value.c_str());
}
attribute = attribute->Next();
}
}
child = child->NextSiblingElement();
}
@ -591,6 +717,23 @@ namespace cocostudio
AnchorPoint f_anchortpoint(anchorPoint.x, anchorPoint.y);
Color f_color(color.a, color.r, color.g, color.b);
FlatSize f_size(size.x, size.y);
auto f_layoutComponent = CreateLayoutComponentTable(*builder,
positionXPercentEnabled,
positionYPercentEnabled,
positionXPercent,
positionYPercent,
sizeXPercentEnable,
sizeYPercentEnable,
sizeXPercent,
sizeYPercent,
stretchHorizontalEnabled,
stretchVerticalEnabled,
builder->CreateString(horizontalEdge),
builder->CreateString(verticalEdge),
leftMargin,
rightMargin,
topMargin,
bottomMargin);
auto options = CreateWidgetOptions(*builder,
builder->CreateString(name),
@ -612,8 +755,8 @@ namespace cocostudio
builder->CreateString(frameEvent),
builder->CreateString(customProperty),
builder->CreateString(callbackType),
builder->CreateString(callbackName)
);
builder->CreateString(callbackName),
f_layoutComponent);
return *(Offset<Table>*)(&options);
}
@ -629,6 +772,35 @@ namespace cocostudio
widget->setAnchorPoint(Vec2::ZERO);
widget->setUnifySizeEnabled(true);
std::string versionString = CSLoader::getInstance()->getCsdVersion();
//assume versionString is like "2.0.6.0"
if (versionString.length() > 0)
{
int p1, p2, p3, v1, v2, v3;
p1 = p2 = p3 = v1 = v2 = v3 = 0;
p1 = versionString.find('.');
if (p1 > 0)
{
p2 = versionString.find('.', p1 + 1);
v1 = atoi(versionString.substr(0, p1).c_str());
}
if (p2 > p1)
{
p3 = versionString.find('.', p2 + 1);
v2 = atoi(versionString.substr(p1 + 1, p2 - p1 - 1).c_str());
}
if (p3 > p2)
{
v3 = atoi(versionString.substr(p2 + 1, p3 - p2 - 1).c_str());
}
if (!(v1 <= 2 && v2 == 0 && v3 <= 6))
{
widget->setUnifySizeEnabled(false);
widget->setLayoutComponentEnabled(true);
}
}
bool ignoreSize = options->ignoreSize();
widget->ignoreContentAdaptWithSize(ignoreSize);
@ -691,6 +863,77 @@ namespace cocostudio
std::string callbackName = options->callBackName()->c_str();
widget->setCallbackName(callbackName);
setLayoutComponentPropsWithFlatBuffers(widget, widgetOptions);
}
void WidgetReader::setLayoutComponentPropsWithFlatBuffers(cocos2d::Node* node, const flatbuffers::Table* nodeOptions)
{
auto layoutComponentTable = ((WidgetOptions*)nodeOptions)->layoutComponent();
if (!layoutComponentTable) return;
bool positionXPercentEnabled = layoutComponentTable->positionXPercentEnabled();
bool positionYPercentEnabled = layoutComponentTable->positionYPercentEnabled();
float positionXPercent = layoutComponentTable->positionXPercent();
float positionYPercent = layoutComponentTable->positionYPercent();
bool sizeXPercentEnable = layoutComponentTable->sizeXPercentEnable();
bool sizeYPercentEnable = layoutComponentTable->sizeYPercentEnable();
float sizeXPercent = layoutComponentTable->sizeXPercent();
float sizeYPercent = layoutComponentTable->sizeYPercent();
bool stretchHorizontalEnabled = layoutComponentTable->stretchHorizontalEnabled();
bool stretchVerticalEnabled = layoutComponentTable->stretchVerticalEnabled();
std::string horizontalEdge = layoutComponentTable->horizontalEdge()->c_str();
std::string verticalEdge = layoutComponentTable->verticalEdge()->c_str();
float leftMargin = layoutComponentTable->leftMargin();
float rightMargin = layoutComponentTable->rightMargin();
float topMargin = layoutComponentTable->topMargin();
float bottomMargin = layoutComponentTable->bottomMargin();
auto layoutComponent = ui::LayoutComponent::create();
node->addComponent(layoutComponent);
layoutComponent->setPositionPercentXEnabled(positionXPercentEnabled);
layoutComponent->setPositionPercentYEnabled(positionYPercentEnabled);
layoutComponent->setPositionPercentX(positionXPercent);
layoutComponent->setPositionPercentY(positionYPercent);
layoutComponent->setPercentWidthEnabled(sizeXPercentEnable);
layoutComponent->setPercentHeightEnabled(sizeYPercentEnable);
layoutComponent->setPercentWidth(sizeXPercent);
layoutComponent->setPercentHeight(sizeYPercent);
layoutComponent->setStretchWidthEnabled(stretchHorizontalEnabled);
layoutComponent->setStretchHeightEnabled(stretchVerticalEnabled);
ui::LayoutComponent::HorizontalEdge horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::None;
if (horizontalEdge == P_Layout_LeftEdge)
{
horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::Left;
}
else if (horizontalEdge == P_Layout_RightEdge)
{
horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::Right;
}
else if (horizontalEdge == P_Layout_BothEdge)
{
horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::Center;
}
layoutComponent->setHorizontalEdge(horizontalEdgeType);
ui::LayoutComponent::VerticalEdge verticalEdgeType = ui::LayoutComponent::VerticalEdge::None;
if (verticalEdge == P_Layout_TopEdge)
{
verticalEdgeType = ui::LayoutComponent::VerticalEdge::Top;
}
else if (verticalEdge == P_Layout_BottomEdge)
{
verticalEdgeType = ui::LayoutComponent::VerticalEdge::Bottom;
}
else if (verticalEdge == P_Layout_BothEdge)
{
verticalEdgeType = ui::LayoutComponent::VerticalEdge::Center;
}
layoutComponent->setVerticalEdge(verticalEdgeType);
layoutComponent->setTopMargin(topMargin);
layoutComponent->setBottomMargin(bottomMargin);
layoutComponent->setLeftMargin(leftMargin);
layoutComponent->setRightMargin(rightMargin);
}
Node* WidgetReader::createNodeWithFlatBuffers(const flatbuffers::Table *widgetOptions)

View File

@ -60,6 +60,7 @@ namespace cocostudio
flatbuffers::Offset<flatbuffers::Table> createOptionsWithFlatBuffers(const tinyxml2::XMLElement* objectData,
flatbuffers::FlatBufferBuilder* builder);
void setPropsWithFlatBuffers(cocos2d::Node* node, const flatbuffers::Table* widgetOptions);
void setLayoutComponentPropsWithFlatBuffers(cocos2d::Node* node, const flatbuffers::Table* widgetOptions);
cocos2d::Node* createNodeWithFlatBuffers(const flatbuffers::Table* widgetOptions);
/**/

View File

@ -167,19 +167,7 @@ void Helper::doLayout(cocos2d::Node *rootNode)
if (nullptr != com && nullptr != parent) {
LayoutComponent* layoutComponent = (LayoutComponent*)com;
if (layoutComponent->isUsingPercentPosition())
{
layoutComponent->setPercentPosition(layoutComponent->getPercentPosition());
}
else if (layoutComponent->getReferencePoint() != LayoutComponent::ReferencePoint::BOTTOM_LEFT)
{
layoutComponent->setRelativePosition(layoutComponent->getRelativePosition());
}
if (layoutComponent->isUsingPercentContentSize())
{
layoutComponent->setPercentContentSize(layoutComponent->getPercentContentSize());
}
layoutComponent->refreshLayout();
}
}
}

View File

@ -79,6 +79,12 @@ public:
std::string::size_type start,
std::string::size_type length);
/**
* Refresh object and it's children layout state
*
*@param rootNode object which will be changed
*
*/
static void doLayout(Node *rootNode);
static void changeLayoutSystemActiveState(bool bActive);

View File

@ -25,16 +25,29 @@
#include "UILayoutComponent.h"
#include "2d/CCNode.h"
#include "GUIDefine.h"
#include "UIHelper.h"
NS_CC_BEGIN
namespace ui {
LayoutComponent::LayoutComponent()
: _usingPercentContentSize(false)
, _referencePoint(ReferencePoint::BOTTOM_LEFT)
, _usingPercentPosition(false)
, _actived(true)
:_horizontalEdge(HorizontalEdge::None)
, _verticalEdge(VerticalEdge::None)
, _leftMargin(0)
, _rightMargin(0)
, _bottomMargin(0)
, _topMargin(0)
, _usingPositionPercentX(false)
, _positionPercentX(0)
, _usingPositionPercentY(false)
, _positionPercentY(0)
, _usingStretchWidth(false)
, _usingStretchHeight(false)
, _percentWidth(0)
, _usingPercentWidth(false)
, _percentHeight(0)
, _usingPercentHeight(false)
, _actived(true)
{
_name = __LAYOUT_COMPONENT_NAME;
}
@ -44,6 +57,23 @@ namespace ui {
}
LayoutComponent* LayoutComponent::boundingLayoutComponent(Node* node)
{
LayoutComponent * layout = (LayoutComponent*)node->getComponent(__LAYOUT_COMPONENT_NAME);
if (layout != nullptr)
return layout;
layout = new (std::nothrow) LayoutComponent();
if (layout && layout->init())
{
layout->autorelease();
node->addComponent(layout);
return layout;
}
CC_SAFE_DELETE(layout);
return nullptr;
}
bool LayoutComponent::init()
{
bool ret = true;
@ -61,246 +91,564 @@ namespace ui {
return ret;
}
//Size
Vec2 LayoutComponent::getOwnerContentSize()const
Node* LayoutComponent::getOwnerParent()
{
Node* parent = _owner->getParent();
return parent;
}
void LayoutComponent::refreshHorizontalMargin()
{
Node* parent = this->getOwnerParent();
if (parent == nullptr)
return;
const Point& ownerPoint = _owner->getPosition();
const Point& ownerAnchor = _owner->getAnchorPoint();
const Size& ownerSize = _owner->getContentSize();
const Size& parentSize = parent->getContentSize();
_leftMargin = ownerPoint.x - ownerAnchor.x * ownerSize.width;
_rightMargin = parentSize.width - (ownerPoint.x + (1 - ownerAnchor.x) * ownerSize.width);
}
void LayoutComponent::refreshVerticalMargin()
{
Node* parent = this->getOwnerParent();
if (parent == nullptr)
return;
const Point& ownerPoint = _owner->getPosition();
const Point& ownerAnchor = _owner->getAnchorPoint();
const Size& ownerSize = _owner->getContentSize();
const Size& parentSize = parent->getContentSize();
_bottomMargin = ownerPoint.y - ownerAnchor.y * ownerSize.height;
_topMargin = parentSize.height - (ownerPoint.y + (1 - ownerAnchor.y) * ownerSize.height);
}
//OldVersion
void LayoutComponent::setUsingPercentContentSize(bool isUsed)
{
_usingPercentWidth = _usingPercentHeight = isUsed;
}
bool LayoutComponent::getUsingPercentContentSize()const
{
return _usingPercentWidth && _usingPercentHeight;
}
void LayoutComponent::setPercentContentSize(const Vec2 &percent)
{
this->setPercentWidth(percent.x);
this->setPercentHeight(percent.y);
}
Vec2 LayoutComponent::getPercentContentSize()const
{
Vec2 vec2=Vec2(_percentWidth,_percentHeight);
return vec2;
}
//Position & Margin
const Point& LayoutComponent::getAnchorPosition()const
{
return _owner->getAnchorPoint();
}
void LayoutComponent::setAnchorPosition(const Point& point)
{
Rect oldRect = _owner->getBoundingBox();
_owner->setAnchorPoint(point);
Rect newRect = _owner->getBoundingBox();
float offSetX = oldRect.origin.x - newRect.origin.x;
float offSetY = oldRect.origin.y - newRect.origin.y;
Point ownerPosition = _owner->getPosition();
ownerPosition.x += offSetX;
ownerPosition.y += offSetY;
this->setPosition(ownerPosition);
}
const Point& LayoutComponent::getPosition()const
{
return _owner->getPosition();
}
void LayoutComponent::setPosition(const Point& position)
{
Node* parent = this->getOwnerParent();
if (parent != nullptr)
{
Point ownerPoint = position;
const Size& parentSize = parent->getContentSize();
if (parentSize.width != 0)
_positionPercentX = ownerPoint.x / parentSize.width;
else
{
_positionPercentX = 0;
if (_usingPositionPercentX)
ownerPoint.x = 0;
}
if (parentSize.height != 0)
_positionPercentY = ownerPoint.y / parentSize.height;
else
{
_positionPercentY = 0;
if (_usingPositionPercentY)
ownerPoint.y = 0;
}
_owner->setPosition(ownerPoint);
this->refreshHorizontalMargin();
this->refreshVerticalMargin();
}
else
_owner->setPosition(position);
}
bool LayoutComponent::isPositionPercentXEnabled()const
{
return _usingPositionPercentX;
}
void LayoutComponent::setPositionPercentXEnabled(bool isUsed)
{
_usingPositionPercentX = isUsed;
if (_usingPositionPercentX)
{
_horizontalEdge = HorizontalEdge::None;
}
}
float LayoutComponent::getPositionPercentX()const
{
return _positionPercentX;
}
void LayoutComponent::setPositionPercentX(float percentMargin)
{
_positionPercentX = percentMargin;
Node* parent = this->getOwnerParent();
if (parent != nullptr)
{
_owner->setPositionX(parent->getContentSize().width * _positionPercentX);
this->refreshHorizontalMargin();
}
}
bool LayoutComponent::isPositionPercentYEnabled()const
{
return _usingPositionPercentY;
}
void LayoutComponent::setPositionPercentYEnabled(bool isUsed)
{
_usingPositionPercentY = isUsed;
if (_usingPositionPercentY)
{
_verticalEdge = VerticalEdge::None;
}
}
float LayoutComponent::getPositionPercentY()const
{
return _positionPercentY;
}
void LayoutComponent::setPositionPercentY(float percentMargin)
{
_positionPercentY = percentMargin;
Node* parent = this->getOwnerParent();
if (parent != nullptr)
{
_owner->setPositionY(parent->getContentSize().height * _positionPercentY);
this->refreshVerticalMargin();
}
}
LayoutComponent::HorizontalEdge LayoutComponent::getHorizontalEdge()const
{
return _horizontalEdge;
}
void LayoutComponent::setHorizontalEdge(HorizontalEdge hEage)
{
_horizontalEdge = hEage;
if (_horizontalEdge != HorizontalEdge::None)
{
_usingPositionPercentX = false;
}
Node* parent = this->getOwnerParent();
if (parent != nullptr)
{
Point ownerPoint = _owner->getPosition();
const Size& parentSize = parent->getContentSize();
if (parentSize.width != 0)
_positionPercentX = ownerPoint.x / parentSize.width;
else
{
_positionPercentX = 0;
ownerPoint.x = 0;
if (_usingPositionPercentX)
_owner->setPosition(ownerPoint);
}
this->refreshHorizontalMargin();
}
}
LayoutComponent::VerticalEdge LayoutComponent::getVerticalEdge()const
{
return _verticalEdge;
}
void LayoutComponent::setVerticalEdge(VerticalEdge vEage)
{
_verticalEdge = vEage;
if (_verticalEdge != VerticalEdge::None)
{
_usingPositionPercentY = false;
}
Node* parent = this->getOwnerParent();
if (parent != nullptr)
{
Point ownerPoint = _owner->getPosition();
const Size& parentSize = parent->getContentSize();
if (parentSize.height != 0)
_positionPercentY = ownerPoint.y / parentSize.height;
else
{
_positionPercentY = 0;
ownerPoint.y = 0;
if (_usingPositionPercentY)
_owner->setPosition(ownerPoint);
}
this->refreshVerticalMargin();
}
}
float LayoutComponent::getLeftMargin()const
{
return _leftMargin;
}
void LayoutComponent::setLeftMargin(float margin)
{
_leftMargin = margin;
}
float LayoutComponent::getRightMargin()const
{
return _rightMargin;
}
void LayoutComponent::setRightMargin(float margin)
{
_rightMargin = margin;
}
float LayoutComponent::getTopMargin()const
{
return _topMargin;
}
void LayoutComponent::setTopMargin(float margin)
{
_topMargin = margin;
}
float LayoutComponent::getBottomMargin()const
{
return _bottomMargin;
}
void LayoutComponent::setBottomMargin(float margin)
{
_bottomMargin = margin;
}
//Size & Percent
const Size& LayoutComponent::getSize()const
{
return this->getOwner()->getContentSize();
}
void LayoutComponent::setOwnerContentSize(const Vec2& size)
void LayoutComponent::setSize(const Size& size)
{
this->getOwner()->setContentSize(Size(size.x,size.y));
Node* parentNode = this->getOwner()->getParent();
if (parentNode != NULL && _actived)
Node* parent = this->getOwnerParent();
if (parent != nullptr)
{
Size parentSize = parentNode->getContentSize();
Size ownerSize = size;
const Size& parentSize = parent->getContentSize();
if (parentSize.width != 0 && parentSize.height != 0)
{
_percentContentSize = Point(size.x/parentSize.width,size.y/parentSize.height);
}
if (parentSize.width != 0)
_percentWidth = ownerSize.width / parentSize.width;
else
{
_percentContentSize = Point(0,0);
}
}
}
const Vec2& LayoutComponent::getPercentContentSize()const
{
return _percentContentSize;
}
void LayoutComponent::setPercentContentSize(const Vec2& percent)
{
_percentContentSize = percent;
Node* parentNode = this->getOwner()->getParent();
if (parentNode != NULL && _actived)
{
Size parentSize = parentNode->getContentSize();
if (_usingPercentContentSize)
{
this->getOwner()->setContentSize(Size(percent.x*parentSize.width,percent.y*parentSize.height));
}
}
}
bool LayoutComponent::isUsingPercentContentSize()
{
return _usingPercentContentSize;
}
void LayoutComponent::setUsingPercentContentSize(bool flag)
{
_usingPercentContentSize = flag;
Node* parentNode = this->getOwner()->getParent();
if (parentNode != NULL && _actived)
{
Size parentSize = parentNode->getContentSize();
if (_usingPercentContentSize)
{
Size baseSize = this->getOwner()->getContentSize();
if (parentSize.width != 0)
{
_percentContentSize.x = baseSize.width/parentSize.width;
}
else
{
_percentContentSize.x = 0;
baseSize.width = 0;
}
if (parentSize.height != 0)
{
_percentContentSize.y = baseSize.height/parentSize.height;
}
else
{
_percentContentSize.y = 0;
baseSize.height = 0;
}
this->getOwner()->setContentSize(baseSize);
}
}
}
//Position
bool LayoutComponent::isUsingPercentPosition()
{
return _usingPercentPosition;
}
void LayoutComponent::setUsingPercentPosition(bool flag)
{
_usingPercentPosition = flag;
Node* parentNode = this->getOwner()->getParent();
if (parentNode != NULL && _actived)
{
Size parentSize = parentNode->getContentSize();
if (_usingPercentPosition)
{
if (parentSize.width != 0)
{
_percentPosition.x = _relativePosition.x/parentSize.width;
}
else
{
_percentPosition.x = 0;
_relativePosition.x = 0;
}
if (parentSize.height != 0)
{
_percentPosition.y = _relativePosition.y/parentSize.height;
}
else
{
_percentPosition.y = 0;
_relativePosition.y = 0;
}
_percentWidth = 0;
if (_usingPercentWidth)
ownerSize.width = 0;
}
Point inversePoint = this->converPointWithReferencePointAndSize(_relativePosition,parentSize);
this->getOwner()->setPosition(inversePoint);
}
}
const Vec2& LayoutComponent::getPercentPosition()
{
return _percentPosition;
}
void LayoutComponent::setPercentPosition(const Vec2& percent)
{
_percentPosition = percent;
Node* parentNode = this->getOwner()->getParent();
if (parentNode != NULL && _actived)
{
Size parentSize = parentNode->getContentSize();
_relativePosition = Point(_percentPosition.x*parentSize.width,_percentPosition.y*parentSize.height);
Point inversePoint = this->converPointWithReferencePointAndSize(_relativePosition,parentSize);
this->getOwner()->setPosition(inversePoint);
}
}
const Vec2& LayoutComponent::getOwnerPosition()const
{
return this->getOwner()->getPosition();
}
void LayoutComponent::setOwnerPosition(const Vec2& point)
{
Node* parentNode = this->getOwner()->getParent();
if (parentNode != NULL && _actived)
{
Size parentSize = parentNode->getContentSize();
Point inversePoint = this->converPointWithReferencePointAndSize(point,parentSize);
this->getOwner()->setPosition(point);
_relativePosition = inversePoint;
if (parentSize.width != 0 && parentSize.height != 0)
{
_percentPosition = Point(_relativePosition.x/parentSize.width,_relativePosition.y/parentSize.height);
}
if (parentSize.height != 0)
_percentHeight = ownerSize.height / parentSize.height;
else
{
_percentPosition = Point(0,0);
_percentHeight = 0;
if (_usingPercentHeight)
ownerSize.height = 0;
}
_owner->setContentSize(ownerSize);
this->refreshHorizontalMargin();
this->refreshVerticalMargin();
}
else
_owner->setContentSize(size);
}
bool LayoutComponent::isPercentWidthEnabled()const
{
return _usingPercentWidth;
}
void LayoutComponent::setPercentWidthEnabled(bool isUsed)
{
_usingPercentWidth = isUsed;
if (_usingPercentWidth)
{
this->getOwner()->setPosition(point);
if (_referencePoint == ReferencePoint::BOTTOM_LEFT)
{
_relativePosition = point;
}
_usingStretchWidth = false;
}
}
const Vec2& LayoutComponent::getRelativePosition()
float LayoutComponent::getSizeWidth()const
{
return _relativePosition;
return _owner->getContentSize().width;
}
void LayoutComponent::setRelativePosition(const Vec2& position)
void LayoutComponent::setSizeWidth(float width)
{
_relativePosition = position;
Size ownerSize = _owner->getContentSize();
ownerSize.width = width;
Node* parentNode = this->getOwner()->getParent();
if (parentNode != NULL && _actived)
Node* parent = this->getOwnerParent();
if (parent != nullptr)
{
Size parentSize = parentNode->getContentSize();
Point inversePoint = this->converPointWithReferencePointAndSize(_relativePosition,parentSize);
this->getOwner()->setPosition(inversePoint);
if (parentSize.width != 0 && parentSize.height != 0)
const Size& parentSize = parent->getContentSize();
if (parentSize.width != 0)
_percentWidth = ownerSize.width / parentSize.width;
else
{
_percentPosition = Point(_relativePosition.x/parentSize.width,_relativePosition.y/parentSize.height);
_percentWidth = 0;
if (_usingPercentWidth)
ownerSize.width = 0;
}
_owner->setContentSize(ownerSize);
this->refreshHorizontalMargin();
}
else
_owner->setContentSize(ownerSize);
}
float LayoutComponent::getPercentWidth()const
{
return _percentWidth;
}
void LayoutComponent::setPercentWidth(float percentWidth)
{
_percentWidth = percentWidth;
Node* parent = this->getOwnerParent();
if (parent != nullptr)
{
Size ownerSize = _owner->getContentSize();
ownerSize.width = parent->getContentSize().width * _percentWidth;
_owner->setContentSize(ownerSize);
this->refreshHorizontalMargin();
}
}
bool LayoutComponent::isPercentHeightEnabled()const
{
return _usingPercentHeight;
}
void LayoutComponent::setPercentHeightEnabled(bool isUsed)
{
_usingPercentHeight = isUsed;
if (_usingPercentHeight)
{
_usingStretchHeight = false;
}
}
float LayoutComponent::getSizeHeight()const
{
return _owner->getContentSize().height;
}
void LayoutComponent::setSizeHeight(float height)
{
Size ownerSize = _owner->getContentSize();
ownerSize.height = height;
Node* parent = this->getOwnerParent();
if (parent != nullptr)
{
const Size& parentSize = parent->getContentSize();
if (parentSize.height != 0)
_percentHeight = ownerSize.height / parentSize.height;
else
{
_percentHeight = 0;
if (_usingPercentHeight)
ownerSize.height = 0;
}
_owner->setContentSize(ownerSize);
this->refreshVerticalMargin();
}
else
_owner->setContentSize(ownerSize);
}
float LayoutComponent::getPercentHeight()const
{
return _percentHeight;
}
void LayoutComponent::setPercentHeight(float percentHeight)
{
_percentHeight = percentHeight;
Node* parent = this->getOwnerParent();
if (parent != nullptr)
{
Size ownerSize = _owner->getContentSize();
ownerSize.height = parent->getContentSize().height * _percentHeight;
_owner->setContentSize(ownerSize);
this->refreshVerticalMargin();
}
}
bool LayoutComponent::isStretchWidthEnabled()const
{
return _usingStretchWidth;
}
void LayoutComponent::setStretchWidthEnabled(bool isUsed)
{
_usingStretchWidth = isUsed;
if (_usingStretchWidth)
{
_usingPercentWidth = false;
}
}
bool LayoutComponent::isStretchHeightEnabled()const
{
return _usingStretchHeight;
}
void LayoutComponent::setStretchHeightEnabled(bool isUsed)
{
_usingStretchHeight = isUsed;
if (_usingStretchHeight)
{
_usingPercentHeight = false;
}
}
void LayoutComponent::refreshLayout()
{
Node* parent = this->getOwnerParent();
if (parent == nullptr)
return;
const Size& parentSize = parent->getContentSize();
const Point& ownerAnchor = _owner->getAnchorPoint();
Size ownerSize = _owner->getContentSize();
Point ownerPosition = _owner->getPosition();
switch (this->_horizontalEdge)
{
case HorizontalEdge::None:
if (_usingStretchWidth)
{
ownerSize.width = parentSize.width * _percentWidth;
ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width;
}
else
{
_percentPosition = Point(0,0);
if (_usingPositionPercentX)
ownerPosition.x = parentSize.width * _positionPercentX;
if (_usingPercentWidth)
ownerSize.width = parentSize.width * _percentWidth;
}
}
}
LayoutComponent::ReferencePoint LayoutComponent::getReferencePoint()
{
return _referencePoint;
}
void LayoutComponent::setReferencePoint(ReferencePoint point)
{
_referencePoint = point;
this->setRelativePosition(_relativePosition);
}
void LayoutComponent::setActiveEnable(bool enable)
{
_actived = enable;
}
Vec2 LayoutComponent::converPointWithReferencePointAndSize(const Vec2& point,const Size& size)
{
Point inversePoint = point;
switch (_referencePoint)
{
case ReferencePoint::TOP_LEFT:
inversePoint.y = size.height - inversePoint.y;
break;
case ReferencePoint::BOTTOM_RIGHT:
inversePoint.x = size.width - inversePoint.x;
case HorizontalEdge::Left:
if (_usingPercentWidth || _usingStretchWidth)
ownerSize.width = parentSize.width * _percentWidth;
ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width;
break;
case ReferencePoint::TOP_RIGHT:
inversePoint.x = size.width - inversePoint.x;
inversePoint.y = size.height - inversePoint.y;
case HorizontalEdge::Right:
if (_usingPercentWidth || _usingStretchWidth)
ownerSize.width = parentSize.width * _percentWidth;
ownerPosition.x = parentSize.width - (_rightMargin + (1 - ownerAnchor.x) * ownerSize.width);
break;
case HorizontalEdge::Center:
if (_usingPercentWidth || _usingStretchWidth)
{
ownerSize.width = parentSize.width - _leftMargin - _rightMargin;
if (ownerSize.width < 0)
ownerSize.width = 0;
ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width;
}
else
ownerPosition.x = parentSize.width * _positionPercentX;
break;
default:
break;
}
return inversePoint;
switch (this->_verticalEdge)
{
case VerticalEdge::None:
if (_usingStretchHeight)
{
ownerSize.height = parentSize.height * _percentHeight;
ownerPosition.y = _bottomMargin + ownerAnchor.y * ownerSize.height;
}
else
{
if (_usingPositionPercentY)
ownerPosition.y = parentSize.height * _positionPercentY;
if (_usingPercentHeight)
ownerSize.height = parentSize.height * _percentHeight;
}
break;
case VerticalEdge::Bottom:
if (_usingPercentHeight || _usingStretchHeight)
ownerSize.height = parentSize.height * _percentHeight;
ownerPosition.y = _bottomMargin + ownerAnchor.y * ownerSize.height;
break;
case VerticalEdge::Top:
if (_usingPercentHeight || _usingStretchHeight)
ownerSize.height = parentSize.height * _percentHeight;
ownerPosition.y = parentSize.height - (_topMargin + (1 - ownerAnchor.y) * ownerSize.height);
break;
case VerticalEdge::Center:
if (_usingPercentHeight || _usingStretchHeight)
{
ownerSize.height = parentSize.height - _topMargin - _bottomMargin;
if (ownerSize.height < 0)
ownerSize.height = 0;
ownerPosition.y = _bottomMargin + ownerAnchor.y * ownerSize.height;
}
else
ownerPosition.y = parentSize.height* _positionPercentY;
break;
default:
break;
}
_owner->setPosition(ownerPosition);
_owner->setContentSize(ownerSize);
ui::Helper::doLayout(_owner);
}
void LayoutComponent::setActiveEnabled(bool enable)
{
_actived = enable;
}
}

View File

@ -29,68 +29,136 @@ THE SOFTWARE.
NS_CC_BEGIN
namespace ui {
class CC_GUI_DLL LayoutComponent : public Component
namespace ui {
class CC_GUI_DLL LayoutComponent : public Component
{
public:
LayoutComponent();
~LayoutComponent();
virtual bool init()override;
CREATE_FUNC(LayoutComponent);
static LayoutComponent* boundingLayoutComponent(Node* node);
enum class HorizontalEdge
{
public:
LayoutComponent();
~LayoutComponent();
virtual bool init()override;
CREATE_FUNC(LayoutComponent);
/**
* When a node has a ReferencePositoin with value equals LEFT_BOTTOM,
* it will treat the left bottom corner of its parent as the origin(0,0) when positioning itself
* which is the same as cocos2d-x does. But you can change it by assigning a
* different ReferencePosition.
* For example: If you use ReferencePosition with value equals RIGHT_TOP,
* then it will treat the right top corner of its parent as the origin(0,0) when positioning itself.
*/
enum class ReferencePoint
{
BOTTOM_LEFT,
TOP_LEFT,
BOTTOM_RIGHT,
TOP_RIGHT
};
bool isUsingPercentPosition();
void setUsingPercentPosition(bool flag);
const Vec2& getPercentPosition();
void setPercentPosition(const Vec2& percent);
const Vec2& getRelativePosition();
void setRelativePosition(const Vec2& position);
void setReferencePoint(ReferencePoint point);
ReferencePoint getReferencePoint();
const Vec2& getOwnerPosition()const;
void setOwnerPosition(const Vec2& point);
Vec2 getOwnerContentSize()const;
void setOwnerContentSize(const Vec2& size);
const Vec2& getPercentContentSize()const;
void setPercentContentSize(const Vec2& percent);
bool isUsingPercentContentSize();
void setUsingPercentContentSize(bool flag);
void setActiveEnable(bool enable);
private:
Vec2 converPointWithReferencePointAndSize(const Vec2& point,const Size& size);
private:
Vec2 _percentContentSize;
bool _usingPercentContentSize;
ReferencePoint _referencePoint;
Vec2 _relativePosition;
Vec2 _percentPosition;
bool _usingPercentPosition;
bool _actived;
None,
Left,
Right,
Center
};
enum class VerticalEdge
{
None,
Bottom,
Top,
Center
};
// OldVersion
void setUsingPercentContentSize(bool isUsed);
bool getUsingPercentContentSize()const;
void setPercentContentSize(const Vec2 &percent);
Vec2 getPercentContentSize()const;
// Position & Margin
const Point& getAnchorPosition()const;
void setAnchorPosition(const Point& point);
const Point& getPosition()const;
void setPosition(const Point& position);
bool isPositionPercentXEnabled()const;
void setPositionPercentXEnabled(bool isUsed);
float getPositionPercentX()const;
void setPositionPercentX(float percentMargin);
bool isPositionPercentYEnabled()const;
void setPositionPercentYEnabled(bool isUsed);
float getPositionPercentY()const;
void setPositionPercentY(float percentMargin);
HorizontalEdge getHorizontalEdge()const;
void setHorizontalEdge(HorizontalEdge hEage);
VerticalEdge getVerticalEdge()const;
void setVerticalEdge(VerticalEdge vEage);
float getLeftMargin()const;
void setLeftMargin(float margin);
float getRightMargin()const;
void setRightMargin(float margin);
float getTopMargin()const;
void setTopMargin(float margin);
float getBottomMargin()const;
void setBottomMargin(float margin);
// Size & Percent
const Size& getSize()const;
void setSize(const Size& size);
bool isPercentWidthEnabled()const;
void setPercentWidthEnabled(bool isUsed);
float getSizeWidth()const;
void setSizeWidth(float width);
float getPercentWidth()const;
void setPercentWidth(float percentWidth);
bool isPercentHeightEnabled()const;
void setPercentHeightEnabled(bool isUsed);
float getSizeHeight()const;
void setSizeHeight(float height);
float getPercentHeight()const;
void setPercentHeight(float percentHeight);
bool isStretchWidthEnabled()const;
void setStretchWidthEnabled(bool isUsed);
bool isStretchHeightEnabled()const;
void setStretchHeightEnabled(bool isUsed);
void setActiveEnabled(bool enable);
void refreshLayout();
protected:
Node* getOwnerParent();
void refreshHorizontalMargin();
void refreshVerticalMargin();
protected:
HorizontalEdge _horizontalEdge;
VerticalEdge _verticalEdge;
float _leftMargin;
float _rightMargin;
float _bottomMargin;
float _topMargin;
bool _usingPositionPercentX;
float _positionPercentX;
bool _usingPositionPercentY;
float _positionPercentY;
bool _usingStretchWidth;
bool _usingStretchHeight;
float _percentWidth;
bool _usingPercentWidth;
float _percentHeight;
bool _usingPercentHeight;
bool _actived;
};
}
NS_CC_END

View File

@ -136,6 +136,7 @@ Widget* Widget::_focusedWidget = nullptr;
Widget::FocusNavigationController* Widget::_focusNavigationController = nullptr;
Widget::Widget():
_usingLayoutComponent(false),
_unifySize(false),
_enabled(true),
_bright(true),
@ -222,7 +223,8 @@ bool Widget::init()
void Widget::onEnter()
{
updateSizeAndPosition();
if (!_usingLayoutComponent)
updateSizeAndPosition();
ProtectedNode::onEnter();
}
@ -281,7 +283,7 @@ void Widget::setContentSize(const cocos2d::Size &contentSize)
{
_contentSize = getVirtualRendererSize();
}
if (_running)
if (!_usingLayoutComponent && _running)
{
Widget* widgetParent = getWidgetParent();
Size pSize;
@ -315,29 +317,39 @@ void Widget::setSize(const Size &size)
void Widget::setSizePercent(const Vec2 &percent)
{
_sizePercent = percent;
Size cSize = _customSize;
if (_running)
if (_usingLayoutComponent)
{
Widget* widgetParent = getWidgetParent();
if (widgetParent)
{
cSize = Size(widgetParent->getContentSize().width * percent.x , widgetParent->getContentSize().height * percent.y);
}
else
{
cSize = Size(_parent->getContentSize().width * percent.x , _parent->getContentSize().height * percent.y);
}
}
if (_ignoreSize)
{
this->setContentSize(getVirtualRendererSize());
auto component = this->getOrCreateLayoutComponent();
component->setUsingPercentContentSize(true);
component->setPercentContentSize(percent);
component->refreshLayout();
}
else
{
this->setContentSize(cSize);
_sizePercent = percent;
Size cSize = _customSize;
if (_running)
{
Widget* widgetParent = getWidgetParent();
if (widgetParent)
{
cSize = Size(widgetParent->getContentSize().width * percent.x, widgetParent->getContentSize().height * percent.y);
}
else
{
cSize = Size(_parent->getContentSize().width * percent.x, _parent->getContentSize().height * percent.y);
}
}
if (_ignoreSize)
{
this->setContentSize(getVirtualRendererSize());
}
else
{
this->setContentSize(cSize);
}
_customSize = cSize;
}
_customSize = cSize;
}
void Widget::updateSizeAndPosition()
@ -422,8 +434,21 @@ void Widget::updateSizeAndPosition(const cocos2d::Size &parentSize)
void Widget::setSizeType(SizeType type)
{
_sizeType = type;
}
if (_usingLayoutComponent)
{
auto component = this->getOrCreateLayoutComponent();
if (_sizeType == Widget::SizeType::PERCENT)
{
component->setUsingPercentContentSize(true);
}
else
{
component->setUsingPercentContentSize(false);
}
}
}
Widget::SizeType Widget::getSizeType() const
{
return _sizeType;
@ -469,6 +494,12 @@ const Size& Widget::getCustomSize() const
const Vec2& Widget::getSizePercent()
{
if (_usingLayoutComponent)
{
auto component = this->getOrCreateLayoutComponent();
_sizePercent = component->getPercentContentSize();
}
return _sizePercent;
}
@ -484,12 +515,15 @@ Node* Widget::getVirtualRenderer()
void Widget::onSizeChanged()
{
for (auto& child : getChildren())
if (!_usingLayoutComponent)
{
Widget* widgetChild = dynamic_cast<Widget*>(child);
if (widgetChild)
for (auto& child : getChildren())
{
widgetChild->updateSizeAndPosition();
Widget* widgetChild = dynamic_cast<Widget*>(child);
if (widgetChild)
{
widgetChild->updateSizeAndPosition();
}
}
}
}
@ -948,7 +982,7 @@ void Widget::interceptTouchEvent(cocos2d::ui::Widget::TouchEventType event, coco
void Widget::setPosition(const Vec2 &pos)
{
if (_running)
if (!_usingLayoutComponent && _running)
{
Widget* widgetParent = getWidgetParent();
if (widgetParent)
@ -969,26 +1003,60 @@ void Widget::setPosition(const Vec2 &pos)
void Widget::setPositionPercent(const Vec2 &percent)
{
_positionPercent = percent;
if (_running)
if (_usingLayoutComponent)
{
Widget* widgetParent = getWidgetParent();
if (widgetParent)
auto component = this->getOrCreateLayoutComponent();
component->setPositionPercentX(percent.x);
component->setPositionPercentY(percent.y);
component->refreshLayout();
}
else
{
_positionPercent = percent;
if (_running)
{
Size parentSize = widgetParent->getContentSize();
Vec2 absPos = Vec2(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y);
setPosition(absPos);
Widget* widgetParent = getWidgetParent();
if (widgetParent)
{
Size parentSize = widgetParent->getContentSize();
Vec2 absPos = Vec2(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y);
setPosition(absPos);
}
}
}
}
const Vec2& Widget::getPositionPercent()const{
const Vec2& Widget::getPositionPercent(){
if (_usingLayoutComponent)
{
auto component = this->getOrCreateLayoutComponent();
float percentX = component->getPositionPercentX();
float percentY = component->getPositionPercentY();
_positionPercent = Vec2(percentX, percentY);
}
return _positionPercent;
}
void Widget::setPositionType(PositionType type)
{
_positionType = type;
if (_usingLayoutComponent)
{
auto component = this->getOrCreateLayoutComponent();
if (type == Widget::PositionType::ABSOLUTE)
{
component->setPositionPercentXEnabled(false);
component->setPositionPercentYEnabled(false);
}
else
{
component->setPositionPercentXEnabled(true);
component->setPositionPercentYEnabled(true);
}
}
}
Widget::PositionType Widget::getPositionType() const
@ -1380,5 +1448,17 @@ void Widget::setUnifySizeEnabled(bool enable)
}
void Widget::setLayoutComponentEnabled(bool enable)
{
_usingLayoutComponent = enable;
}
bool Widget::isLayoutComponentEnabled()const
{
return _usingLayoutComponent;
}
}
NS_CC_END

View File

@ -272,7 +272,7 @@ public:
*
* @return The percent (x,y) of the widget in OpenGL coordinates
*/
const Vec2& getPositionPercent()const;
const Vec2& getPositionPercent();
/**
* Changes the position type of the widget
@ -635,6 +635,16 @@ public:
void setCallbackType(const std::string& callbackType) { _callbackType = callbackType; }
const std::string& getCallbackType() const{ return _callbackType; }
/**
*@param enable Layout Component of a widget
*@return void
*/
void setLayoutComponentEnabled(bool enable);
/**
*@return true represent the widget use Layout Component, false represent the widget couldn't use Layout Component.
*/
bool isLayoutComponentEnabled()const;
CC_CONSTRUCTOR_ACCESS:
//initializes state of widget.
@ -711,6 +721,7 @@ protected:
LayoutComponent* getOrCreateLayoutComponent();
protected:
bool _usingLayoutComponent;
bool _unifySize;
bool _enabled;
bool _bright;

View File

@ -110,6 +110,7 @@ bool AppDelegate::applicationDidFinishLaunching()
searchPaths.push_back("ccs-res/hd/cocosui/UIEditorTest/UILayout/Layout");
searchPaths.push_back("ccs-res/hd/cocosui/UIEditorTest/UILayout/Gradient_Color");
searchPaths.push_back("ccs-res/hd/cocosui/UIEditorTest/UILayout/Scale9_BackgroundImage");
searchPaths.push_back("ccs-res/hd/cocosui/UIEditorTest/UILayout/LayoutComponent");
searchPaths.push_back("ccs-res/hd/cocosui/UIEditorTest/UILoadingBar");
searchPaths.push_back("ccs-res/hd/cocosui/UIEditorTest/UIPageView");
searchPaths.push_back("ccs-res/hd/cocosui/UIEditorTest/UIScrollView/Both");
@ -149,6 +150,7 @@ bool AppDelegate::applicationDidFinishLaunching()
searchPaths.push_back("ccs-res/cocosui/UIEditorTest/UILayout/Layout");
searchPaths.push_back("ccs-res/cocosui/UIEditorTest/UILayout/Gradient_Color");
searchPaths.push_back("ccs-res/cocosui/UIEditorTest/UILayout/Scale9_BackgroundImage");
searchPaths.push_back("ccs-res/cocosui/UIEditorTest/UILayout/LayoutComponent");
searchPaths.push_back("ccs-res/cocosui/UIEditorTest/UILoadingBar");
searchPaths.push_back("ccs-res/cocosui/UIEditorTest/UIPageView");
searchPaths.push_back("ccs-res/cocosui/UIEditorTest/UIScrollView/Both");

View File

@ -199,7 +199,7 @@ g_guisTests[] =
UISceneManager* sceneManager = UISceneManager::sharedUISceneManager();
sceneManager->setCurrentUISceneId(kUILayoutTest);
sceneManager->setMinUISceneId(kUILayoutTest);
sceneManager->setMaxUISceneId(kUILayoutTest_Layout_Relative_Location);
sceneManager->setMaxUISceneId(kUILayoutComponent_Berth_Stretch_Test);
Scene* scene = sceneManager->currentUIScene();
Director::getInstance()->replaceScene(scene);
}

View File

@ -177,7 +177,7 @@ g_guisTests[] =
UISceneManager_Editor* pManager = UISceneManager_Editor::sharedUISceneManager_Editor();
pManager->setCurrentUISceneId(kUILayoutTest_Editor);
pManager->setMinUISceneId(kUILayoutTest_Editor);
pManager->setMaxUISceneId(kUILayoutTest_BackGroundImage_Scale9_Editor);
pManager->setMaxUISceneId(kUILayoutComponentTest_Editor);
// pManager->setMaxUISceneId(kUILayoutTest_Layout_Relative_Location_Editor);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);

View File

@ -694,3 +694,153 @@ bool UILayoutTest_Layout_Relative_Location::init()
return false;
}
bool UILayoutComponentTest::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
_baseLayer = LayerColor::create();
_baseLayer->setColor(Color3B(50, 100, 0));
_baseLayer->setOpacity(100);
_baseLayer->setContentSize(Size(200, 200));
_uiLayer->addChild(_baseLayer);
Button* button = Button::create("cocosui/animationbuttonnormal.png");
CCLOG("content size should be greater than 0: width = %f, height = %f", button->getContentSize().width,
button->getContentSize().height);
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
button->addTouchEventListener(CC_CALLBACK_2(UILayoutComponentTest::touchEvent, this));
button->setZoomScale(0.4f);
button->setPressedActionEnabled(true);
_uiLayer->addChild(button);
return true;
}
return false;
}
void UILayoutComponentTest::touchEvent(Ref *pSender, Widget::TouchEventType type)
{
switch (type)
{
case Widget::TouchEventType::BEGAN:
break;
case Widget::TouchEventType::MOVED:
break;
case Widget::TouchEventType::ENDED:
{
Size widgetSize = _widget->getContentSize();
Size layerSize = _baseLayer->getContentSize();
if (layerSize.width == widgetSize.width && layerSize.height == widgetSize.height)
_baseLayer->setContentSize(Size(200, 200));
else
_baseLayer->setContentSize(widgetSize);
ui:Helper::doLayout(_baseLayer);
}
break;
case Widget::TouchEventType::CANCELED:
break;
default:
break;
}
}
bool UILayoutComponent_Berth_Test::init()
{
if (UILayoutComponentTest::init())
{
Sprite* leftTopSprite = Sprite::create("cocosui/CloseSelected.png");
LayoutComponent* leftTop = LayoutComponent::boundingLayoutComponent(leftTopSprite);
leftTop->setHorizontalEdge(LayoutComponent::HorizontalEdge::Left);
leftTop->setVerticalEdge(LayoutComponent::VerticalEdge::Top);
_baseLayer->addChild(leftTopSprite);
Sprite* leftBottomSprite = Sprite::create("cocosui/CloseSelected.png");
LayoutComponent* leftBottom = LayoutComponent::boundingLayoutComponent(leftBottomSprite);
leftBottom->setHorizontalEdge(LayoutComponent::HorizontalEdge::Left);
leftBottom->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom);
_baseLayer->addChild(leftBottomSprite);
Sprite* rightTopSprite = Sprite::create("cocosui/CloseSelected.png");
LayoutComponent* rightTop = LayoutComponent::boundingLayoutComponent(rightTopSprite);
rightTop->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right);
rightTop->setVerticalEdge(LayoutComponent::VerticalEdge::Top);
_baseLayer->addChild(rightTopSprite);
Sprite* rightBottomSprite = Sprite::create("cocosui/CloseSelected.png");
LayoutComponent* rightBottom = LayoutComponent::boundingLayoutComponent(rightBottomSprite);
rightBottom->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right);
rightBottom->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom);
_baseLayer->addChild(rightBottomSprite);
ui::Helper::doLayout(_baseLayer);
return true;
}
return false;
}
bool UILayoutComponent_Berth_Stretch_Test::init()
{
if (UILayoutComponentTest::init())
{
ImageView* leftTopSprite = ImageView::create("cocosui/CloseSelected.png");
leftTopSprite->ignoreContentAdaptWithSize(false);
LayoutComponent* leftTop = LayoutComponent::boundingLayoutComponent(leftTopSprite);
leftTop->setHorizontalEdge(LayoutComponent::HorizontalEdge::Left);
leftTop->setVerticalEdge(LayoutComponent::VerticalEdge::Top);
leftTop->setStretchWidthEnabled(true);
leftTop->setStretchHeightEnabled(true);
_baseLayer->addChild(leftTopSprite);
leftTop->setSize(leftTopSprite->getContentSize());
leftTop->setLeftMargin(0);
leftTop->setTopMargin(0);
ImageView* leftBottomSprite = ImageView::create("cocosui/CloseSelected.png");
leftBottomSprite->ignoreContentAdaptWithSize(false);
LayoutComponent* leftBottom = LayoutComponent::boundingLayoutComponent(leftBottomSprite);
leftBottom->setHorizontalEdge(LayoutComponent::HorizontalEdge::Left);
leftBottom->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom);
leftBottom->setStretchWidthEnabled(true);
leftBottom->setStretchHeightEnabled(true);
_baseLayer->addChild(leftBottomSprite);
leftBottom->setSize(leftBottomSprite->getContentSize());
leftBottom->setLeftMargin(0);
leftBottom->setBottomMargin(0);
ImageView* rightTopSprite = ImageView::create("cocosui/CloseSelected.png");
rightTopSprite->ignoreContentAdaptWithSize(false);
LayoutComponent* rightTop = LayoutComponent::boundingLayoutComponent(rightTopSprite);
rightTop->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right);
rightTop->setVerticalEdge(LayoutComponent::VerticalEdge::Top);
rightTop->setStretchWidthEnabled(true);
rightTop->setStretchHeightEnabled(true);
_baseLayer->addChild(rightTopSprite);
rightTop->setSize(rightTopSprite->getContentSize());
rightTop->setTopMargin(0);
rightTop->setRightMargin(0);
ImageView* rightBottomSprite = ImageView::create("cocosui/CloseSelected.png");
rightBottomSprite->ignoreContentAdaptWithSize(false);
LayoutComponent* rightBottom = LayoutComponent::boundingLayoutComponent(rightBottomSprite);
rightBottom->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right);
rightBottom->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom);
rightBottom->setStretchWidthEnabled(true);
rightBottom->setStretchHeightEnabled(true);
_baseLayer->addChild(rightBottomSprite);
rightBottom->setSize(rightBottomSprite->getContentSize());
rightBottom->setBottomMargin(0);
rightBottom->setRightMargin(0);
ui::Helper::doLayout(_baseLayer);
return true;
}
return false;
}

View File

@ -126,6 +126,32 @@ protected:
UI_SCENE_CREATE_FUNC(UILayoutTest_Layout_Relative_Location)
};
class UILayoutComponentTest : public UIScene
{
public:
virtual bool init();
void touchEvent(Ref *pSender, Widget::TouchEventType type);
protected:
LayerColor* _baseLayer;
UI_SCENE_CREATE_FUNC(UILayoutComponentTest)
};
class UILayoutComponent_Berth_Test : public UILayoutComponentTest
{
public:
virtual bool init() override;
protected:
UI_SCENE_CREATE_FUNC(UILayoutComponent_Berth_Test)
};
class UILayoutComponent_Berth_Stretch_Test : public UILayoutComponentTest
{
public:
virtual bool init() override;
protected:
UI_SCENE_CREATE_FUNC(UILayoutComponent_Berth_Stretch_Test)
};
/*
class UILayoutTest_Layout_Grid : public UIScene
{

View File

@ -1214,3 +1214,71 @@ bool UILayoutTest_Layout_Relative_Location_Editor::init()
return false;
}
UILayoutComponentTest_Editor::UILayoutComponentTest_Editor()
{
}
UILayoutComponentTest_Editor::~UILayoutComponentTest_Editor()
{
}
void UILayoutComponentTest_Editor::configureGUIScene()
{
Size screenSize = CCDirector::getInstance()->getWinSize();
_sceneTitle = Text::create("UILayoutComponentTest_Editor", "", 20);
_sceneTitle->setPosition(Vec2(screenSize.width / 2, screenSize.height - _sceneTitle->getContentSize().height / 2));
_layout->addChild(_sceneTitle);
Text* back_label = Text::create("Back", "", 20);
back_label->setTouchEnabled(true);
auto labelLayout = LayoutComponent::boundingLayoutComponent(back_label);
labelLayout->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right);
labelLayout->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom);
back_label->addTouchEventListener(CC_CALLBACK_2(UIScene_Editor::toGUIEditorTestScene, this));
_layout->addChild(back_label);
Button* left_button = Button::create();
left_button->loadTextures("Images/b1.png", "Images/b2.png", "");
left_button->setPosition(Vec2(_layout->getContentSize().width / 2 - left_button->getContentSize().width,
left_button->getContentSize().height * 0.625));
left_button->setTouchEnabled(true);
left_button->addTouchEventListener(CC_CALLBACK_2(UIScene_Editor::previousCallback, this));
left_button->setLocalZOrder(_layout->getLocalZOrder() + 1);
_layout->addChild(left_button);
Button* right_button = Button::create();
right_button->loadTextures("Images/f1.png", "Images/f2.png", "");
right_button->setPosition(Vec2(_layout->getContentSize().width / 2 + right_button->getContentSize().width,
right_button->getContentSize().height * 0.625));
right_button->setTouchEnabled(true);
right_button->setLocalZOrder(_layout->getLocalZOrder() + 1);
right_button->addTouchEventListener(CC_CALLBACK_2(UIScene_Editor::nextCallback, this));
_layout->addChild(right_button);
}
bool UILayoutComponentTest_Editor::init()
{
if (UIScene_Editor::init())
{
Size screenSize = CCDirector::getInstance()->getWinSize();
_layout = Layout::create();
_layout->setContentSize(screenSize);
_touchGroup->addChild(_layout);
Node* node = CSLoader::createNode("cocosui/UIEditorTest/UILayout/LayoutComponent/Scene.csb");
node->setContentSize(screenSize);
_layout->addChild(node);
this->configureGUIScene();
ui::Helper::doLayout(_layout);
return true;
}
return false;
}

View File

@ -144,4 +144,16 @@ protected:
UI_SCENE_EDITOR_CREATE_FUNC(UILayoutTest_Layout_Relative_Location_Editor)
};
class UILayoutComponentTest_Editor : public UIScene_Editor
{
public:
UILayoutComponentTest_Editor();
~UILayoutComponentTest_Editor();
virtual bool init();
void configureGUIScene();
protected:
LayerColor* _baseLayer;
UI_SCENE_EDITOR_CREATE_FUNC(UILayoutComponentTest_Editor)
};
#endif /* defined(__TestCpp__UILayoutTest_Editor__) */

View File

@ -87,6 +87,8 @@ static const char* s_testArray[] =
"UILayoutTest_Layout_Linear_Horizontal",
"UILayoutTest_Layout_Relative_Align_Parent",
"UILayoutTest_Layout_Relative_Location",
"UILayoutComponent_Berth_Test",
"UILayoutComponent_Berth_Stretch_Test",
"UIScrollViewTest_Vertical",
"UIScrollViewTest_Horizontal",
@ -317,6 +319,12 @@ Scene *UISceneManager::currentUIScene()
case kUILayoutTest_Layout_Relative_Location:
return UILayoutTest_Layout_Relative_Location::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUILayoutComponent_Berth_Test:
return UILayoutComponent_Berth_Test::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUILayoutComponent_Berth_Stretch_Test:
return UILayoutComponent_Berth_Stretch_Test::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUIScrollViewTest_Vertical:
return UIScrollViewTest_Vertical::sceneWithTitle(s_testArray[_currentUISceneId]);

View File

@ -80,6 +80,8 @@ enum
kUILayoutTest_Layout_Linear_Horizontal,
kUILayoutTest_Layout_Relative_Align_Parent,
kUILayoutTest_Layout_Relative_Location,
kUILayoutComponent_Berth_Test,
kUILayoutComponent_Berth_Stretch_Test,
kUIScrollViewTest_Vertical,
kUIScrollViewTest_Horizontal,
kUIScrollViewTest_Both,

View File

@ -36,12 +36,15 @@ static const char* s_testArray[] =
"UILayoutTest_Gradient_Editor",
"UILayoutTest_BackGroundImage_Editor",
"UILayoutTest_BackGroundImage_Scale9_Editor",
"UILayoutComponentTest_Editor",
/*
"UILayoutTest_Layout_Linear_Vertical_Editor",
"UILayoutTest_Layout_Linear_Horizontal_Editor",
"UILayoutTest_Layout_Relative_Align_Parent_Editor",
"UILayoutTest_Layout_Relative_Location_Editor",
*/
"UIListViewTest_Vertical_Editor",
"UIListViewTest_Horizontal_Editor",
"UIPageViewTest_Editor",
@ -149,6 +152,8 @@ Scene* UISceneManager_Editor::currentUIScene()
case kUILayoutTest_BackGroundImage_Scale9_Editor:
return UILayoutTest_BackGroundImage_Scale9_Editor::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUILayoutComponentTest_Editor:
return UILayoutComponentTest_Editor::sceneWithTitle(s_testArray[_currentUISceneId]);
/*
case kUILayoutTest_Layout_Linear_Vertical_Editor:
return UILayoutTest_Layout_Linear_Vertical_Editor::sceneWithTitle(s_testArray[_currentUISceneId]);
@ -162,6 +167,7 @@ Scene* UISceneManager_Editor::currentUIScene()
case kUILayoutTest_Layout_Relative_Location_Editor:
return UILayoutTest_Layout_Relative_Location_Editor::sceneWithTitle(s_testArray[_currentUISceneId]);
*/
case kUIListViewTest_Vertical_Editor:
return UIListViewTest_Vertical_Editor::sceneWithTitle(s_testArray[_currentUISceneId]);

View File

@ -46,12 +46,15 @@ enum
kUILayoutTest_Gradient_Editor,
kUILayoutTest_BackGroundImage_Editor,
kUILayoutTest_BackGroundImage_Scale9_Editor,
kUILayoutComponentTest_Editor,
/*
kUILayoutTest_Layout_Linear_Vertical_Editor,
kUILayoutTest_Layout_Linear_Horizontal_Editor,
kUILayoutTest_Layout_Relative_Align_Parent_Editor,
kUILayoutTest_Layout_Relative_Location_Editor,
*/
kUIListViewTest_Vertical_Editor,
kUIListViewTest_Horizontal_Editor,
kUIPageViewTest_Editor,

@ -1 +1 @@
Subproject commit 1d8bede8e900625bc47972c4a925763e43f70ba2
Subproject commit 84e5fd6000d7439082f6a8b507f520a52c50ad4e