| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- #include "BsGUILayoutY.h"
- #include "BsGUIElement.h"
- #include "BsGUISpace.h"
- #include "BsMath.h"
- #include "BsVector2I.h"
- namespace BansheeEngine
- {
- GUILayoutY::GUILayoutY(GUIArea* parentArea)
- :GUILayout(parentArea)
- { }
- Vector2I GUILayoutY::_calculateOptimalLayoutSize() const
- {
- if (mIsDisabled)
- return Vector2I(0, 0);
- UINT32 optimalWidth = 0;
- UINT32 optimalHeight = 0;
- for (auto& child : mChildren)
- {
- Vector2I optimalSize = child->_calculateOptimalLayoutSize();
-
- if (child->_getType() == GUIElementBase::Type::FixedSpace)
- optimalSize.x = 0;
- UINT32 paddingX = child->_getPadding().left + child->_getPadding().right;
- UINT32 paddingY = child->_getPadding().top + child->_getPadding().bottom;
- optimalHeight += optimalSize.y + paddingY;
- optimalWidth = std::max((UINT32)optimalSize.x, optimalWidth + paddingX);
- }
- return Vector2I(optimalWidth, optimalHeight);
- }
- void GUILayoutY::_updateOptimalLayoutSizes()
- {
- // Update all children first, otherwise we can't determine our own optimal size
- GUIElementBase::_updateOptimalLayoutSizes();
- if(mChildren.size() != mOptimalSizes.size())
- mOptimalSizes.resize(mChildren.size());
- mOptimalWidth = 0;
- mOptimalHeight = 0;
- UINT32 childIdx = 0;
- for(auto& child : mChildren)
- {
- UINT32 optimalWidth = 0;
- UINT32 optimalHeight = 0;
- if(child->_getType() == GUIElementBase::Type::FixedSpace)
- {
- GUIFixedSpace* fixedSpace = static_cast<GUIFixedSpace*>(child);
- optimalHeight = fixedSpace->_calculateOptimalLayoutSize().y;
- }
- else if(child->_getType() == GUIElementBase::Type::Element)
- {
- GUIElement* element = static_cast<GUIElement*>(child);
- const GUILayoutOptions& layoutOptions = element->_getLayoutOptions();
- Vector2I optimalSize = child->_calculateOptimalLayoutSize();
- optimalWidth = optimalSize.x;
- optimalHeight = optimalSize.y;
- }
- else if(child->_getType() == GUIElementBase::Type::Layout)
- {
- GUILayout* layout = static_cast<GUILayout*>(child);
- optimalWidth = layout->_getOptimalSize().x;
- optimalHeight = layout->_getOptimalSize().y;
- }
- UINT32 paddingX = child->_getPadding().left + child->_getPadding().right;
- UINT32 paddingY = child->_getPadding().top + child->_getPadding().bottom;
- mOptimalSizes[childIdx].y = optimalHeight;
- mOptimalHeight += optimalHeight + paddingY;
- mOptimalSizes[childIdx].x = optimalWidth;
- mOptimalWidth = std::max(mOptimalWidth, optimalWidth + paddingX);
- childIdx++;
- }
- }
- void GUILayoutY::_getElementAreas(INT32 x, INT32 y, UINT32 width, UINT32 height, Rect2I* elementAreas, UINT32 numElements, const Vector<Vector2I>& optimalSizes) const
- {
- assert(mChildren.size() == numElements);
- UINT32 totalOptimalSize = _getOptimalSize().y;
- UINT32 totalNonClampedSize = 0;
- UINT32 numNonClampedElements = 0;
- UINT32 numFlexibleSpaces = 0;
- bool* processedElements = nullptr;
- float* elementScaleWeights = nullptr;
- if (mChildren.size() > 0)
- {
- processedElements = stackAllocN<bool>((UINT32)mChildren.size());
- memset(processedElements, 0, mChildren.size() * sizeof(bool));
- elementScaleWeights = stackAllocN<float>((UINT32)mChildren.size());
- memset(elementScaleWeights, 0, mChildren.size() * sizeof(float));
- }
- // Set initial sizes, count number of children per type and mark fixed elements as already processed
- UINT32 childIdx = 0;
- for (auto& child : mChildren)
- {
- elementAreas[childIdx].height = optimalSizes[childIdx].y;
- if (child->_getType() == GUIElementBase::Type::FixedSpace)
- {
- processedElements[childIdx] = true;
- }
- else if (child->_getType() == GUIElementBase::Type::Element)
- {
- GUIElement* element = static_cast<GUIElement*>(child);
- const GUILayoutOptions& layoutOptions = element->_getLayoutOptions();
- if (layoutOptions.fixedHeight)
- processedElements[childIdx] = true;
- else
- {
- numNonClampedElements++;
- totalNonClampedSize += elementAreas[childIdx].height;
- }
- }
- else if (child->_getType() == GUIElementBase::Type::Layout)
- {
- numNonClampedElements++;
- totalNonClampedSize += elementAreas[childIdx].height;
- }
- else if (child->_getType() == GUIElementBase::Type::FlexibleSpace)
- {
- numFlexibleSpaces++;
- numNonClampedElements++;
- }
- childIdx++;
- }
- // If there is some room left, calculate flexible space sizes (since they will fill up all that extra room)
- if (height > totalOptimalSize)
- {
- UINT32 extraSize = height - totalOptimalSize;
- UINT32 remainingSize = extraSize;
- // Flexible spaces always expand to fill up all unused space
- if (numFlexibleSpaces > 0)
- {
- float avgSize = remainingSize / (float)numFlexibleSpaces;
- childIdx = 0;
- for (auto& child : mChildren)
- {
- if (processedElements[childIdx])
- {
- childIdx++;
- continue;
- }
- UINT32 extraHeight = std::min((UINT32)Math::ceilToInt(avgSize), remainingSize);
- UINT32 elementHeight = elementAreas[childIdx].height + extraHeight;
- // Clamp if needed
- if (child->_getType() == GUIElementBase::Type::FlexibleSpace)
- {
- processedElements[childIdx] = true;
- numNonClampedElements--;
- elementAreas[childIdx].height = elementHeight;
- remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraHeight);
- }
- childIdx++;
- }
- totalOptimalSize = height;
- }
- }
- // Determine weight scale for every element. When scaling elements up/down they will be scaled based on this weight.
- // Weight is to ensure all elements are scaled fairly, so elements that are large will get effected more than smaller elements.
- childIdx = 0;
- float invOptimalSize = 1.0f / totalNonClampedSize;
- for (auto& child : mChildren)
- {
- if (processedElements[childIdx])
- {
- childIdx++;
- continue;
- }
- elementScaleWeights[childIdx] = invOptimalSize * elementAreas[childIdx].height;
- childIdx++;
- }
- // Our optimal size is larger than maximum allowed, so we need to reduce size of some elements
- if (totalOptimalSize > height)
- {
- UINT32 extraSize = totalOptimalSize - height;
- UINT32 remainingSize = extraSize;
- // Iterate until we reduce everything so it fits, while maintaining
- // equal average sizes using the weights we calculated earlier
- while (remainingSize > 0 && numNonClampedElements > 0)
- {
- UINT32 totalRemainingSize = remainingSize;
- childIdx = 0;
- for (auto& child : mChildren)
- {
- if (processedElements[childIdx])
- {
- childIdx++;
- continue;
- }
- float avgSize = totalRemainingSize * elementScaleWeights[childIdx];
- UINT32 extraHeight = std::min((UINT32)Math::ceilToInt(avgSize), remainingSize);
- UINT32 elementHeight = (UINT32)std::max(0, (INT32)elementAreas[childIdx].height - (INT32)extraHeight);
- // Clamp if needed
- if (child->_getType() == GUIElementBase::Type::Element)
- {
- GUIElement* element = static_cast<GUIElement*>(child);
- const GUILayoutOptions& layoutOptions = element->_getLayoutOptions();
- if (elementHeight == 0)
- {
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- else if (layoutOptions.minHeight > 0 && elementHeight < layoutOptions.minHeight)
- {
- elementHeight = layoutOptions.minHeight;
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- extraHeight = elementAreas[childIdx].height - elementHeight;
- elementAreas[childIdx].height = elementHeight;
- remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraHeight);
- }
- else if (child->_getType() == GUIElementBase::Type::Layout)
- {
- if (elementHeight == 0)
- {
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- extraHeight = elementAreas[childIdx].height - elementHeight;
- elementAreas[childIdx].height = elementHeight;
- remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraHeight);
- }
- else if (child->_getType() == GUIElementBase::Type::FlexibleSpace)
- {
- elementAreas[childIdx].height = 0;
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- childIdx++;
- }
- }
- }
- else // We are smaller than the allowed maximum, so try to expand some elements
- {
- UINT32 extraSize = height - totalOptimalSize;
- UINT32 remainingSize = extraSize;
- // Iterate until we reduce everything so it fits, while maintaining
- // equal average sizes using the weights we calculated earlier
- while (remainingSize > 0 && numNonClampedElements > 0)
- {
- UINT32 totalRemainingSize = remainingSize;
- childIdx = 0;
- for (auto& child : mChildren)
- {
- if (processedElements[childIdx])
- {
- childIdx++;
- continue;
- }
- float avgSize = totalRemainingSize * elementScaleWeights[childIdx];
- UINT32 extraHeight = std::min((UINT32)Math::ceilToInt(avgSize), remainingSize);
- UINT32 elementHeight = elementAreas[childIdx].height + extraHeight;
- // Clamp if needed
- if (child->_getType() == GUIElementBase::Type::Element)
- {
- GUIElement* element = static_cast<GUIElement*>(child);
- const GUILayoutOptions& layoutOptions = element->_getLayoutOptions();
- if (elementHeight == 0)
- {
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- else if (layoutOptions.maxHeight > 0 && elementHeight > layoutOptions.maxHeight)
- {
- elementHeight = layoutOptions.maxHeight;
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- extraHeight = elementHeight - elementAreas[childIdx].height;
- elementAreas[childIdx].height = elementHeight;
- remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraHeight);
- }
- else if (child->_getType() == GUIElementBase::Type::Layout)
- {
- elementAreas[childIdx].height = elementHeight;
- remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraHeight);
- }
- else if (child->_getType() == GUIElementBase::Type::FlexibleSpace)
- {
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- childIdx++;
- }
- }
- }
- if (elementScaleWeights != nullptr)
- stackDeallocLast(elementScaleWeights);
- if (processedElements != nullptr)
- stackDeallocLast(processedElements);
- // Compute offsets and width
- UINT32 yOffset = 0;
- childIdx = 0;
- for (auto& child : mChildren)
- {
- UINT32 elemHeight = elementAreas[childIdx].height;
- yOffset += child->_getPadding().top;
- if (child->_getType() == GUIElementBase::Type::Element)
- {
- GUIElement* element = static_cast<GUIElement*>(child);
- UINT32 elemWidth = optimalSizes[childIdx].x;
- const GUILayoutOptions& layoutOptions = element->_getLayoutOptions();
- if (!layoutOptions.fixedWidth)
- {
- elemWidth = width;
- if (layoutOptions.minWidth > 0 && elemWidth < layoutOptions.minWidth)
- elemWidth = layoutOptions.minWidth;
- if (layoutOptions.maxWidth > 0 && elemWidth > layoutOptions.maxWidth)
- elemWidth = layoutOptions.maxWidth;
- }
- elementAreas[childIdx].width = elemWidth;
- UINT32 xPadding = element->_getPadding().left + element->_getPadding().right;
- INT32 xOffset = Math::ceilToInt((INT32)(width - (INT32)(elemWidth + xPadding)) * 0.5f);
- xOffset = std::max(0, xOffset);
- elementAreas[childIdx].x = x + xOffset;
- elementAreas[childIdx].y = y + yOffset;
- }
- else if (child->_getType() == GUIElementBase::Type::Layout)
- {
- elementAreas[childIdx].width = width;
- elementAreas[childIdx].x = x;
- elementAreas[childIdx].y = y + yOffset;
- }
- yOffset += elemHeight + child->_getPadding().bottom;
- childIdx++;
- }
- }
- void GUILayoutY::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height, Rect2I clipRect, UINT8 widgetDepth, UINT16 areaDepth)
- {
- UINT32 numElements = (UINT32)mChildren.size();
- Rect2I* elementAreas = nullptr;
-
- if (numElements > 0)
- elementAreas = stackConstructN<Rect2I>(numElements);
- _getElementAreas(x, y, width, height, elementAreas, numElements, mOptimalSizes);
- // Now that we have all the areas, actually assign them
- UINT32 childIdx = 0;
- mActualWidth = 0;
- mActualHeight = 0;
- for(auto& child : mChildren)
- {
- Rect2I childArea = elementAreas[childIdx];
- if(child->_getType() == GUIElementBase::Type::Element)
- {
- GUIElement* element = static_cast<GUIElement*>(child);
- element->setWidth(childArea.width);
- element->setHeight(childArea.height);
- Vector2I offset(childArea.x, childArea.y);
- element->setOffset(offset);
- element->_setWidgetDepth(widgetDepth);
- element->_setAreaDepth(areaDepth);
- Rect2I elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
- element->_setClipRect(elemClipRect);
- Rect2I newClipRect(offset.x, offset.y, childArea.width, childArea.height);
- newClipRect.clip(clipRect);
- element->_updateLayoutInternal(offset.x, offset.y, childArea.width, childArea.height, newClipRect, widgetDepth, areaDepth);
- mActualWidth = std::max(mActualWidth, (UINT32)childArea.width);
- }
- else if(child->_getType() == GUIElementBase::Type::Layout)
- {
- GUILayout* layout = static_cast<GUILayout*>(child);
- Rect2I newClipRect(childArea.x, childArea.y, width, childArea.height);
- newClipRect.clip(clipRect);
- layout->_updateLayoutInternal(childArea.x, childArea.y, width, childArea.height, newClipRect, widgetDepth, areaDepth);
- mActualWidth = std::max(mActualWidth, layout->_getActualWidth());
- }
- mActualHeight += childArea.height + child->_getPadding().top + child->_getPadding().bottom;
- childIdx++;
- }
- if (elementAreas != nullptr)
- stackDeallocLast(elementAreas);
- _markAsClean();
- }
- }
|