void UIComponentTreeView::updateItemPositions() { float offset_y = 0.f; for(size_t i = 0; i < mParent->getChildCount(); ++i) { UIView* child = mParent->getChild(i); child->setPosition(child->getPosition().x, mParent->getPosition().y + offset_y); offset_y += child->getSize().y + 2.f; if(mSubTreeView) { Log("Child %d sizeY %f stays at %f", i, child->getSize().y, child->getPosition().y); } } }
/// This is called ONLY when a new item is added to the tree view /// All items added are in the same level in the tree void UIComponentTreeView::onChildAttached(UIView* child) { Log("CHILD ADDED %s", child->getName().c_str()); // Immediately set this children to its right spot float offsetY = 0.f; for(size_t i = 0; i < mParent->getChildCount(); ++i) { offsetY += mParent->getChild(i)->getSize().y + 2.f; } if(mSubTreeView) child->setRect(mParent->getPosition().x, mParent->getPosition().y + offsetY, mParent->getSize().x, mLineHeight); else child->setRect(mParent->getPosition().x, mParent->getPosition().y + offsetY, mParent->getSize().x, mLineHeight); //if(mSubTreeLevel == 1) // child->addComponent(new UIComponentDebugColor(Color::Grass)); //if(mSubTreeLevel == 0) //child->addComponent(new UIComponentDebugColor(Color::Blue)); //if(mSubTreeLevel == 2) // child->addComponent(new UIComponentDebugColor(Color::Bittersweet)); // All children added to a tree view become TreeViewItem, whether they are collapsible or not child->addComponent(new UIComponentTreeViewItem()); child->onNewChild.connect(sigc::bind(sigc::mem_fun(this, &UIComponentTreeView::onChildAddedToChild), child)); // I want to be informed when any of the items change size to reposition things.. //child->onSizeChanged.connect(sigc::mem_fun(this, &UIComponentTreeView::updateItemPositions)); child->mPadding.left = (mSubTreeLevel + 1) * 20.f; // If this is a subgroup tree view, needs to expand and shrink if(mSubTreeView) { // Grow the container to fit one more item mParent->setSize(mParent->getSize().x, mParent->getSize().y + 32.f); // Grow the actual item too which contains this container mParent->getParent()->setSize(mParent->getParent()->getSize().x, mParent->getParent()->getSize().y + 32.f); // For level 1 tree views, there is no need to update the parent, but for any deeper, the increase needs to propagate int treeLevel = mSubTreeLevel; UIView* subTreeContainer = mParent; UIView* itemContainer = subTreeContainer->getParent(); while(treeLevel >= 1) { // propagate the growth subTreeContainer = itemContainer->getParent(); itemContainer = subTreeContainer->getParent(); // Grow the container to fit one more item subTreeContainer->setSize(subTreeContainer->getSize().x, subTreeContainer->getSize().y + 32.f); // Grow the actual item too which contains this container itemContainer->getParent()->setSize(itemContainer->getParent()->getSize().x, itemContainer->getParent()->getSize().y + 32.f); treeLevel--; } //Log("ADDED ITEM TO A SUBTREE, SUBTREE CONTAINER NOW %f", mParent->getSize().y); } }