Merge pull request #4671 from nutty898/develop_nutty_modify_framework_newrender_modify_projectfiles

Add refresh layout method and uses Range loop for GUI codes
This commit is contained in:
James Chen 2013-12-26 00:49:49 -08:00
commit 8dfc428599
7 changed files with 50 additions and 53 deletions

View File

@ -38,7 +38,7 @@ Widget* UIHelper::seekWidgetByTag(Widget* root, int tag)
{ {
return root; return root;
} }
Vector<Node*> arrayRootChildren = root->getChildren(); const auto& arrayRootChildren = root->getChildren();
int length = arrayRootChildren.size(); int length = arrayRootChildren.size();
for (int i=0;i<length;i++) for (int i=0;i<length;i++)
{ {
@ -62,11 +62,10 @@ Widget* UIHelper::seekWidgetByName(Widget* root, const char *name)
{ {
return root; return root;
} }
Vector<Node*> arrayRootChildren = root->getChildren(); const auto& arrayRootChildren = root->getChildren();
int length = arrayRootChildren.size(); for (auto& subWidget : arrayRootChildren)
for (int i=0;i<length;i++)
{ {
Widget* child = static_cast<Widget*>(arrayRootChildren.at(i)); Widget* child = static_cast<Widget*>(subWidget);
Widget* res = seekWidgetByName(child,name); Widget* res = seekWidgetByName(child,name);
if (res != nullptr) if (res != nullptr)
{ {
@ -82,11 +81,10 @@ Widget* UIHelper::seekWidgetByRelativeName(Widget *root, const char *name)
{ {
return nullptr; return nullptr;
} }
Vector<Node*> arrayRootChildren = root->getChildren(); const auto& arrayRootChildren = root->getChildren();
int length = arrayRootChildren.size(); for (auto& subWidget : arrayRootChildren)
for (int i=0;i<length;i++)
{ {
Widget* child = static_cast<Widget*>(arrayRootChildren.at(i)); Widget* child = static_cast<Widget*>(subWidget);
RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)); RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE));
if (layoutParameter && strcmp(layoutParameter->getRelativeName(), name) == 0) if (layoutParameter && strcmp(layoutParameter->getRelativeName(), name) == 0)
{ {
@ -107,11 +105,10 @@ Widget* UIHelper::seekActionWidgetByActionTag(Widget* root, int tag)
{ {
return root; return root;
} }
Vector<Node*> arrayRootChildren = root->getChildren(); const auto& arrayRootChildren = root->getChildren();
int length = arrayRootChildren.size(); for (auto& subWidget : arrayRootChildren)
for (int i=0;i<length;i++)
{ {
Widget* child = static_cast<Widget*>(arrayRootChildren.at(i)); Widget* child = static_cast<Widget*>(subWidget);
Widget* res = seekActionWidgetByActionTag(child,tag); Widget* res = seekActionWidgetByActionTag(child,tag);
if (res != nullptr) if (res != nullptr)
{ {

View File

@ -710,6 +710,11 @@ LayoutType Layout::getLayoutType() const
return _layoutType; return _layoutType;
} }
void Layout::requestDoLayout()
{
_doLayoutDirty = true;
}
void Layout::doLayout() void Layout::doLayout()
{ {
if (!_doLayoutDirty) if (!_doLayoutDirty)
@ -722,12 +727,12 @@ void Layout::doLayout()
break; break;
case LAYOUT_LINEAR_VERTICAL: case LAYOUT_LINEAR_VERTICAL:
{ {
int length = _widgetChildren.size();
Size layoutSize = getSize(); Size layoutSize = getSize();
float topBoundary = layoutSize.height; float topBoundary = layoutSize.height;
for (int i=0; i<length; ++i)
for (auto& subWidget : _widgetChildren)
{ {
Widget* child = static_cast<Widget*>(_widgetChildren.at(i)); Widget* child = static_cast<Widget*>(subWidget);
LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter(LAYOUT_PARAMETER_LINEAR));
if (layoutParameter) if (layoutParameter)
@ -762,12 +767,11 @@ void Layout::doLayout()
} }
case LAYOUT_LINEAR_HORIZONTAL: case LAYOUT_LINEAR_HORIZONTAL:
{ {
int length = _widgetChildren.size();
Size layoutSize = getSize(); Size layoutSize = getSize();
float leftBoundary = 0.0f; float leftBoundary = 0.0f;
for (int i=0; i<length; ++i) for (auto& subWidget : _widgetChildren)
{ {
Widget* child = static_cast<Widget*>(_widgetChildren.at(i)); Widget* child = static_cast<Widget*>(subWidget);
LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter(LAYOUT_PARAMETER_LINEAR));
if (layoutParameter) if (layoutParameter)
@ -802,22 +806,19 @@ void Layout::doLayout()
} }
case LAYOUT_RELATIVE: case LAYOUT_RELATIVE:
{ {
int length = _widgetChildren.size(); int unlayoutChildCount = _widgetChildren.size();
int unlayoutChildCount = length;
Size layoutSize = getSize(); Size layoutSize = getSize();
for (auto& subWidget : _widgetChildren)
for (int i=0; i<length; i++)
{ {
Widget* child = static_cast<Widget*>(_widgetChildren.at(i)); Widget* child = static_cast<Widget*>(subWidget);
RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)); RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE));
layoutParameter->_put = false; layoutParameter->_put = false;
} }
while (unlayoutChildCount > 0) while (unlayoutChildCount > 0)
{ {
for (int i=0; i<length; i++) for (auto& subWidget : _widgetChildren)
{ {
Widget* child = static_cast<Widget*>(_widgetChildren.at(i)); Widget* child = static_cast<Widget*>(subWidget);
RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)); RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE));
if (layoutParameter) if (layoutParameter)

View File

@ -212,6 +212,8 @@ public:
virtual void visit(); virtual void visit();
virtual void sortAllChildren() override; virtual void sortAllChildren() override;
void requestDoLayout();
protected: protected:
//override "init" method of widget. //override "init" method of widget.
virtual bool init() override; virtual bool init() override;

View File

@ -90,9 +90,8 @@ void ListView::updateInnerContainerSize()
{ {
int length = _items.size(); int length = _items.size();
float totalHeight = (length - 1) * _itemsMargin; float totalHeight = (length - 1) * _itemsMargin;
for (int i=0; i<length; i++) for (auto& item : _items)
{ {
Widget* item = _items.at(i);
totalHeight += item->getSize().height; totalHeight += item->getSize().height;
} }
float finalWidth = _size.width; float finalWidth = _size.width;
@ -104,9 +103,8 @@ void ListView::updateInnerContainerSize()
{ {
int length = _items.size(); int length = _items.size();
float totalWidth = (length - 1) * _itemsMargin; float totalWidth = (length - 1) * _itemsMargin;
for (int i=0; i<length; i++) for (auto& item : _items)
{ {
Widget* item = _items.at(i);
totalWidth += item->getSize().width; totalWidth += item->getSize().width;
} }
float finalWidth = totalWidth; float finalWidth = totalWidth;
@ -363,6 +361,11 @@ void ListView::setDirection(SCROLLVIEW_DIR dir)
ScrollView::setDirection(dir); ScrollView::setDirection(dir);
} }
void ListView::requestRefreshView()
{
_refreshViewDirty = true;
}
void ListView::refreshView() void ListView::refreshView()
{ {
int length = _items.size(); int length = _items.size();
@ -441,12 +444,9 @@ Widget* ListView::createCloneInstance()
void ListView::copyClonedWidgetChildren(Widget* model) void ListView::copyClonedWidgetChildren(Widget* model)
{ {
Vector<Widget*> arrayItems = getItems(); auto& arrayItems = getItems();
for (auto& item : arrayItems)
int length = arrayItems.size();
for (int i=0; i<length; i++)
{ {
Widget* item = arrayItems.at(i);
pushBackCustomItem(item->clone()); pushBackCustomItem(item->clone());
} }
} }

View File

@ -165,6 +165,8 @@ public:
virtual std::string getDescription() const override; virtual std::string getDescription() const override;
void requestRefreshView();
protected: protected:
virtual void addChild(Node* child) override{ScrollView::addChild(child);}; virtual void addChild(Node* child) override{ScrollView::addChild(child);};
virtual void addChild(Node * child, int zOrder) override{ScrollView::addChild(child, zOrder);}; virtual void addChild(Node * child, int zOrder) override{ScrollView::addChild(child, zOrder);};

View File

@ -181,7 +181,7 @@ void PageView::insertPage(Layout* page, int idx)
page->setSize(pvSize); page->setSize(pvSize);
} }
int length = _pages.size(); int length = _pages.size();
for (int i=(idx+1); i<length; i++) { for (int i=(idx+1); i<length; i++){
Widget* behindPage = _pages.at(i); Widget* behindPage = _pages.at(i);
Point formerPos = behindPage->getPosition(); Point formerPos = behindPage->getPosition();
behindPage->setPosition(Point(formerPos.x+getSize().width, 0)); behindPage->setPosition(Point(formerPos.x+getSize().width, 0));
@ -268,10 +268,8 @@ void PageView::onSizeChanged()
void PageView::updateChildrenSize() void PageView::updateChildrenSize()
{ {
Size selfSize = getSize(); Size selfSize = getSize();
int length = _pages.size(); for (auto& page : _pages)
for (long i=0; i<length; i++)
{ {
Layout* page = _pages.at(i);
page->setSize(selfSize); page->setSize(selfSize);
} }
} }
@ -410,13 +408,11 @@ void PageView::onTouchCancelled(Touch *touch, Event *unusedEvent)
void PageView::movePages(float offset) void PageView::movePages(float offset)
{ {
int length = _pages.size(); for (auto& page : _pages)
for (int i = 0; i < length; i++)
{ {
Widget* child = _pages.at(i); _movePagePoint.x = page->getPosition().x + offset;
_movePagePoint.x = child->getPosition().x + offset; _movePagePoint.y = page->getPosition().y;
_movePagePoint.y = child->getPosition().y; page->setPosition(_movePagePoint);
child->setPosition(_movePagePoint);
} }
} }
@ -606,11 +602,9 @@ Widget* PageView::createCloneInstance()
void PageView::copyClonedWidgetChildren(Widget* model) void PageView::copyClonedWidgetChildren(Widget* model)
{ {
Vector<Layout*> modelPages = dynamic_cast<PageView*>(model)->getPages(); auto& modelPages = dynamic_cast<PageView*>(model)->getPages();
int length = modelPages.size(); for (auto& page : modelPages)
for (int i=0; i<length; i++)
{ {
Layout* page = modelPages.at(i);
addPage(dynamic_cast<Layout*>(page->clone())); addPage(dynamic_cast<Layout*>(page->clone()));
} }
} }

View File

@ -977,10 +977,11 @@ Widget* Widget::createCloneInstance()
void Widget::copyClonedWidgetChildren(Widget* model) void Widget::copyClonedWidgetChildren(Widget* model)
{ {
int length = model->getChildren().size(); auto& modelChildren = model->getChildren();
for (int i=0; i<length; i++)
for (auto& subWidget : modelChildren)
{ {
Widget* child = static_cast<Widget*>(model->getChildren().at(i)); Widget* child = static_cast<Widget*>(subWidget);
addChild(child->clone()); addChild(child->clone());
} }
} }