I have created a scene in cocostudio with design size set to 480x320. I positioned all elements pins and percentage, so them should adopt well for different resolutions.
I load the scene using next code:
auto scene = Scene::create();
Node *node = CSLoader::getInstance()->createNode(R::UI::MAIN_MENU_SCENE);
scene->addChild(node);
What are the problems:
if aspect ratio of the device isn’t 3:2 (iphone 5 +) then the scene not resized. Just empty space added to the right (or if design resolution size not set to 480x320);
I found some solution to get expected behaviour, but i believe it should be provided by the CSLoader itself.
namespace GuiUtils {
void iterateRecursively(Node *parent, std::function<void(Node *)>callback) {
callback(parent);
auto &children = parent->getChildren();
for (auto child : children) {
iterateRecursively(child, callback);
}
}
}
void HelloWorld::initialize(cocos2d::Scene *scene) {
_rootNode = CSLoader::createNode("MainScene.csb");
scene->addChild(_rootNode);
//update size of the root node to win size
{
LayoutComponent *layout = LayoutComponent::bindLayoutComponent(_rootNode);
auto size = Director::getInstance()->getWinSize();
layout->setSize(size);
}
//manually refresh layout for all children
GuiUtils::iterateRecursively(_rootNode, [](Node *child) {
LayoutComponent *layout = LayoutComponent::bindLayoutComponent(child);
layout->refreshLayout();
});
}
P.S. All samples that i found also wasn’t adapted for multi resolution support and just use ResolutionPolicy::EXACT_FIT, and there no any discuss about this problem… I couldn’t believe that it’s an expected behaviour.