Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export Creator Widget component to Cocos2d-x Layout Node #160

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
aa83e85
fix Animation reverse not effect
drelaptop Mar 20, 2018
15b7f05
fix loop reverse bug
drelaptop Mar 20, 2018
6e6e7c0
remove a while not needed
drelaptop Mar 20, 2018
1b44b7c
PingPong is a Loop mode
drelaptop Mar 20, 2018
4e87cbe
add comment
drelaptop Mar 20, 2018
62b5f58
add export option
drelaptop Mar 21, 2018
84c03d3
add js logic to copy dynamic res
drelaptop Mar 21, 2018
d4be795
improve var name
drelaptop Mar 21, 2018
6827e85
roolback unrelated changes
drelaptop Mar 21, 2018
ed809fa
improve UI/var name
drelaptop Mar 23, 2018
cbe246a
improve mod method
drelaptop Mar 23, 2018
23eb44f
remove useless duration twice
drelaptop Mar 23, 2018
449c242
fix loop animate memory leak
drelaptop Mar 26, 2018
a59da3b
Merge branch 'root-master' into fix-animation-wrapmode
drelaptop Mar 26, 2018
ba726bc
fix PlayOnLoad loop animation leaks
drelaptop Mar 26, 2018
cdb4ca8
Merge branch 'export_all_resources' into export-widget-to-layout
drelaptop Mar 27, 2018
f68eab8
Merge branch 'fix-animation-wrapmode' into export-widget-to-layout
drelaptop Mar 27, 2018
914d731
init add widget components
drelaptop Mar 27, 2018
8943712
widget property collect
drelaptop Mar 27, 2018
f88de5f
add necessary flatbuffer info
drelaptop Mar 27, 2018
32a1b47
init cpp to support widget
drelaptop Mar 29, 2018
2697f40
change the adapt way, add WidgetAdapter class
drelaptop Mar 30, 2018
ce7b6ea
add widget adapter
drelaptop Mar 30, 2018
c0b32d5
linear layout effect wrong
drelaptop Apr 2, 2018
ecc726f
change to relative layout, correct
drelaptop Apr 2, 2018
f2a70b7
add widget manager, run fail
drelaptop Apr 2, 2018
dcc88d2
correct WidgetManager
drelaptop Apr 3, 2018
48c4830
add comments
drelaptop Apr 3, 2018
d60dd67
add widget release
drelaptop Apr 3, 2018
042ef74
Merge remote-tracking branch 'root/master' into export-widget-to-layout
drelaptop Apr 3, 2018
2b0e0ac
update comments
drelaptop Apr 3, 2018
1b1aee1
support 9 align size comb
drelaptop Apr 4, 2018
1ceed70
support 9 auto align in node
Kuovane May 11, 2018
975fc03
support auto align in node if it add widget in creator .
drelaptop May 11, 2018
5acd01c
improve widget support from Kuovane, solve conflict
drelaptop May 11, 2018
6ad5b2c
Merge remote-tracking branch 'root/master' into export-widget-to-layout
drelaptop May 11, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,9 @@ quick_gen_project_*_autogen.sh.meta
#/////////////////////////////////////////////////////////////////////////////

.idea/

#/////////////////////////////////////////////////////////////////////////////
# vscode files
#/////////////////////////////////////////////////////////////////////////////

.vscode/
19 changes: 19 additions & 0 deletions creator_project/packages/creator-luacpp-support/CreatorReader.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ table Node
tag:int = 0;
anim:AnimationRef;
colliders:[Collider];
widget:Widget;
groupIndex:int;
}

Expand Down Expand Up @@ -354,6 +355,24 @@ table Collider
radius:float; // CircleCollider
}

table Widget
{
isAlignOnce:bool;
alignFlags:int;
left:float;
right:float;
top:float;
bottom:float;
verticalCenter:float;
horizontalCenter:float;
isAbsLeft:bool;
isAbsRight:bool;
isAbsTop:bool;
isAbsBottom:bool;
isAbsHorizontalCenter:bool;
isAbsVerticalCenter:bool;
}

table DragonBones
{
node:Node;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const state = require('./Global').state;
const Collider = require('./Collider');
const Widget = require('./Widget');
let Utils = require('./Utils');
const fs = require('fs');

Expand Down Expand Up @@ -194,6 +195,7 @@ class Node {

this.parse_clip();
this.parse_colliders();
this.parse_widget();
}

parse_child(node_idx) {
Expand Down Expand Up @@ -224,6 +226,13 @@ class Node {
}
}

parse_widget() {
let component = Node.get_node_components_of_type(this._node_data, 'cc.Widget');
if(component.length === 1) {
this._properties.widget = Widget.parse(component[0]);
}
}

parse_clip() {
let component = Node.get_node_component_of_type(this._node_data, 'cc.Animation');
if (component) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ let get_tiledmap_path_by_uuid = function (uuid) {
}
}

let DEBUG = false;
let DEBUG = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably don't need this in the final PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review this, I will change it to false in the final. Are you satisfied with this implement of Widget Component?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code itself looks fine to me, but I'm not familiar with positional layouts enough to say one way or the other

log = function(s) {
if (DEBUG)
Utils.log(s);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
let Utils = require('./Utils');

class Widget {
static parse(data) {
let result = {};

result.isAlignOnce = data.isAlignOnce;
result.alignFlags = data._alignFlags;

// margin value, only support pixel, didn't support percentage
result.left = data._left;
result.right = data._right;
result.top = data._top;
result.bottom = data._bottom;
result.verticalCenter = data._verticalCenter;
result.horizontalCenter = data._horizontalCenter;

// If true, value is pixel, otherwise is percentage (0 - 1)
// invalid value in cocos2d-x
result.isAbsLeft = data._isAbsLeft;
result.isAbsRight = data._isAbsRight;
result.isAbsTop = data._isAbsTop;
result.isAbsBottom = data._isAbsBottom;
result.isAbsHorizontalCenter = data._isAbsHorizontalCenter;
result.isAbsVerticalCenter = data._isAbsVerticalCenter;

Utils.log("parse widget result value:" + JSON.stringify(result));

return result;
}
}


module.exports = Widget;
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set(READER_HEADER
Macros.h
ui/RichtextStringVisitor.h
ui/PageView.h
ui/WidgetExport.h
)

set(READER_SOURCE
Expand All @@ -46,6 +47,7 @@ set(READER_SOURCE
CreatorReader.cpp
ui/PageView.cpp
ui/RichtextStringVisitor.cpp
ui/WidgetExport.cpp
)

if(BUILD_LUA_LIBS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,20 @@ CreatorReader::CreatorReader()
{
_animationManager = new AnimationManager();
_collisionManager = new ColliderManager();
_widgetManager = new WidgetManager();
_animationManager->autorelease();
_collisionManager->autorelease();
_widgetManager->autorelease();
CC_SAFE_RETAIN(_animationManager);
CC_SAFE_RETAIN(_collisionManager);
CC_SAFE_RETAIN(_widgetManager);
}

CreatorReader::~CreatorReader()
{
CC_SAFE_RELEASE_NULL(_collisionManager);
CC_SAFE_RELEASE_NULL(_animationManager);
CC_SAFE_RELEASE_NULL(_widgetManager);
}

CreatorReader* CreatorReader::createWithFilename(const std::string& filename)
Expand Down Expand Up @@ -274,11 +282,14 @@ cocos2d::Scene* CreatorReader::getSceneGraph() const
child->setPosition(child->getPosition() + _positionDiffDesignResolution);

_animationManager->playOnLoad();

node->addChild(_collisionManager);
node->addChild(_animationManager);
_collisionManager->start();

_widgetManager->setupWidgets();
node->addChild(_widgetManager);

return static_cast<cocos2d::Scene*>(node);
}

Expand All @@ -292,6 +303,11 @@ ColliderManager* CreatorReader::getColliderManager() const
return _collisionManager;
}

WidgetManager* CreatorReader::getWidgetManager() const
{
return _widgetManager;
}

std::string CreatorReader::getVersion() const
{
return _version;
Expand Down Expand Up @@ -465,6 +481,8 @@ void CreatorReader::parseNode(cocos2d::Node* node, const buffers::Node* nodeBuff
parseNodeAnimation(node, nodeBuffer);

parseColliders(node, nodeBuffer);

parseWidget(node, nodeBuffer);
}

void CreatorReader::parseNodeAnimation(cocos2d::Node* node, const buffers::Node* nodeBuffer) const
Expand Down Expand Up @@ -599,6 +617,62 @@ void CreatorReader::parseColliders(cocos2d::Node* node, const buffers::Node* nod
_collisionManager->addCollider(collider);
}

void CreatorReader::parseWidget(cocos2d::Node *node, const buffers::Node *nodeBuffer) const
{
const auto& info = nodeBuffer->widget();
auto widgetNode = dynamic_cast<ui::Widget*>(node);
if ((info != nullptr) && (widgetNode != nullptr)) {
// save the widget margin info
const auto& margin = ui::Margin(info->left(),info->top(),info->right(), info->bottom());
auto parameter = ui::RelativeLayoutParameter::create();
parameter->setMargin(margin);

WidgetAdapter::AlignComb alignComb = static_cast<WidgetAdapter::AlignComb>(info->alignFlags());
switch (alignComb) {
case WidgetAdapter::AlignComb::TOP_LEFT :
parameter->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_TOP_LEFT);
break;
case WidgetAdapter::AlignComb::TOP_RIGHT :
parameter->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT);
break;
case WidgetAdapter::AlignComb::RIGHT_BOTTOM :
parameter->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_BOTTOM);
break;
case WidgetAdapter::AlignComb::LEFT_BOTTOM :
parameter->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_BOTTOM);
break;
case WidgetAdapter::AlignComb::LEFT_CENTER_VERTICAL :
parameter->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL);
break;
case WidgetAdapter::AlignComb::RIGHT_CENTER_VERTICAL :
parameter->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_CENTER_VERTICAL);
break;
case WidgetAdapter::AlignComb::TOP_CENTER_HORIZONTAL :
parameter->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL);
break;
case WidgetAdapter::AlignComb::BOTTOM_CENTER_HORIZONTAL :
parameter->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL);
break;
case WidgetAdapter::AlignComb::CENTER_IN_PARENT :
parameter->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT);
break;
default:
CCLOG("align combination of UI Node: %s isn't supported", node->getName().c_str());
CCASSERT(false, "Only 9 creator align combinations are supported by cocos2d-x");
break;
}

auto widgetInfo = WidgetAdapter::create();
// TODO: support Layout target, how to get the layout target?
// parameter->setRelativeToWidgetName(const std::string &name);
// widgetInfo->setLayoutTarget(cocos2d::Node *layoutTarget);
widgetNode->setLayoutParameter(parameter);
widgetInfo->setAdaptNode(widgetNode);
widgetInfo->setIsAlignOnce(info->isAlignOnce());
_widgetManager->_needAdaptWidgets.pushBack(widgetInfo);
}
}

cocos2d::Sprite* CreatorReader::createSprite(const buffers::Sprite* spriteBuffer) const
{
cocos2d::Sprite* sprite = cocos2d::Sprite::create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "collider/ColliderManager.h"
#include "dragonbones/DragonBonesHeaders.h"
#include "dragonbones/cocos2dx/CCDragonBonesHeaders.h"
#include "ui/WidgetExport.h"



Expand Down Expand Up @@ -62,6 +63,12 @@ class CreatorReader: public cocos2d::Ref
*/
ColliderManager* getColliderManager() const;

/**
Return the WidgetManager. It is added as a child of the Scene to make Creator Widget component take effect.
@return The `WidgetManager` of the scene
*/
WidgetManager* getWidgetManager() const;

/**
Returns the FlatBuffers Schema version.
@return a string containing the flatbuffer's schema version
Expand All @@ -88,6 +95,7 @@ class CreatorReader: public cocos2d::Ref
void parseNode(cocos2d::Node* node, const buffers::Node* nodeBuffer) const;
void parseNodeAnimation(cocos2d::Node* node, const buffers::Node* nodeBuffer) const;
void parseColliders(cocos2d::Node* node, const buffers::Node* nodeBuffer) const;
void parseWidget(cocos2d::Node* node, const buffers::Node* nodeBuffer) const;

cocos2d::Sprite* createSprite(const buffers::Sprite* spriteBuffer) const;
void parseSprite(cocos2d::Sprite* sprite, const buffers::Sprite* spriteBuffer) const;
Expand Down Expand Up @@ -160,14 +168,17 @@ class CreatorReader: public cocos2d::Ref
// variables
cocos2d::Data _data;
std::string _version;

AnimationManager *_animationManager;
ColliderManager *_collisionManager;

// Widget in creator is a component used to do Layout
WidgetManager *_widgetManager;

// creator will make scene at the center of screen when apply design solution strategy, cocos2d-x doesn't do it like this
// this value record the diff
cocos2d::Vec2 _positionDiffDesignResolution;

CREATOR_DISALLOW_COPY_ASSIGN_AND_MOVE(CreatorReader);
};

Expand Down
Loading