| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- #include "BsGUILayoutX.h"
- #include "BsGUIElement.h"
- #include "BsGUISpace.h"
- #include "BsMath.h"
- #include "BsVector2I.h"
- namespace BansheeEngine
- {
- Vector2I GUILayoutX::_calculateOptimalLayoutSize() const
- {
- UINT32 optimalWidth = 0;
- UINT32 optimalHeight = 0;
- for (auto& child : mChildren)
- {
- Vector2I optimalSize = child->_calculateOptimalLayoutSize();
- UINT32 paddingX = child->_getPadding().left + child->_getPadding().right;
- UINT32 paddingY = child->_getPadding().top + child->_getPadding().bottom;
- optimalWidth += optimalSize.x + paddingX;
- optimalHeight = std::max((UINT32)optimalSize.y, optimalHeight + paddingY);
- }
- return Vector2I(optimalWidth, optimalHeight);
- }
- void GUILayoutX::_updateOptimalLayoutSizes()
- {
- // Update all children first, otherwise we can't determine out 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);
- optimalWidth = fixedSpace->_calculateOptimalLayoutSize().x;
- }
- 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)
- {
- optimalWidth = child->_getOptimalSize().x;
- optimalHeight = child->_getOptimalSize().y;
- }
- UINT32 paddingX = child->_getPadding().left + child->_getPadding().right;
- UINT32 paddingY = child->_getPadding().top + child->_getPadding().bottom;
- mOptimalSizes[childIdx].x = optimalWidth;
- mOptimalWidth += optimalWidth + paddingX;
- mOptimalSizes[childIdx].y = optimalHeight;
- mOptimalHeight = std::max(mOptimalHeight, optimalHeight + paddingY);
- childIdx++;
- }
- }
- void GUILayoutX::getElementAreas(UINT32 width, UINT32 height, RectI* elementAreas, UINT32 numElements, const Vector<Vector2I>& optimalSizes) const
- {
- assert(mChildren.size() == numElements);
- UINT32 totalOptimalSize = _getOptimalSize().x;
- 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].width = optimalSizes[childIdx].x;
- 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.fixedWidth)
- processedElements[childIdx] = true;
- else
- {
- numNonClampedElements++;
- totalNonClampedSize += elementAreas[childIdx].width;
- }
- }
- else if (child->_getType() == GUIElementBase::Type::Layout)
- {
- numNonClampedElements++;
- totalNonClampedSize += elementAreas[childIdx].width;
- }
- 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 (width > totalOptimalSize)
- {
- UINT32 extraSize = width - 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 extraWidth = std::min((UINT32)Math::ceilToInt(avgSize), remainingSize);
- UINT32 elementWidth = elementAreas[childIdx].width + extraWidth;
- // Clamp if needed
- if (child->_getType() == GUIElementBase::Type::FlexibleSpace)
- {
- processedElements[childIdx] = true;
- numNonClampedElements--;
- elementAreas[childIdx].width = elementWidth;
- remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraWidth);
- }
- childIdx++;
- }
- totalOptimalSize = width;
- }
- }
- // 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].width;
- childIdx++;
- }
- // Our optimal size is larger than maximum allowed, so we need to reduce size of some elements
- if (totalOptimalSize > width)
- {
- UINT32 extraSize = totalOptimalSize - width;
- 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 extraWidth = std::min((UINT32)Math::ceilToInt(avgSize), remainingSize);
- UINT32 elementWidth = (UINT32)std::max(0, (INT32)elementAreas[childIdx].width - (INT32)extraWidth);
- // Clamp if needed
- if (child->_getType() == GUIElementBase::Type::Element)
- {
- GUIElement* element = static_cast<GUIElement*>(child);
- const GUILayoutOptions& layoutOptions = element->_getLayoutOptions();
- if (elementWidth == 0)
- {
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- else if (layoutOptions.minWidth > 0 && elementWidth < layoutOptions.minWidth)
- {
- elementWidth = layoutOptions.minWidth;
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- extraWidth = elementAreas[childIdx].width - elementWidth;
- elementAreas[childIdx].width = elementWidth;
- remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraWidth);
- }
- else if (child->_getType() == GUIElementBase::Type::Layout)
- {
- if (elementWidth == 0)
- {
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- extraWidth = elementAreas[childIdx].width - elementWidth;
- elementAreas[childIdx].width = elementWidth;
- remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraWidth);
- }
- else if (child->_getType() == GUIElementBase::Type::FlexibleSpace)
- {
- elementAreas[childIdx].width = 0;
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- childIdx++;
- }
- }
- }
- else // We are smaller than the allowed maximum, so try to expand some elements
- {
- UINT32 extraSize = width - 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 extraWidth = std::min((UINT32)Math::ceilToInt(avgSize), remainingSize);
- UINT32 elementWidth = elementAreas[childIdx].width + extraWidth;
- // Clamp if needed
- if (child->_getType() == GUIElementBase::Type::Element)
- {
- GUIElement* element = static_cast<GUIElement*>(child);
- const GUILayoutOptions& layoutOptions = element->_getLayoutOptions();
- if (elementWidth == 0)
- {
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- else if (layoutOptions.maxWidth > 0 && elementWidth > layoutOptions.maxWidth)
- {
- elementWidth = layoutOptions.maxWidth;
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- extraWidth = elementWidth - elementAreas[childIdx].width;
- elementAreas[childIdx].width = elementWidth;
- remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraWidth);
- }
- else if (child->_getType() == GUIElementBase::Type::Layout)
- {
- elementAreas[childIdx].width = elementWidth;
- remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraWidth);
- }
- else if (child->_getType() == GUIElementBase::Type::FlexibleSpace)
- {
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- childIdx++;
- }
- }
- }
- // Compute offsets and height
- UINT32 xOffset = 0;
- childIdx = 0;
- for (auto& child : mChildren)
- {
- UINT32 elemWidth = elementAreas[childIdx].width;
- xOffset += child->_getPadding().left;
- if (child->_getType() == GUIElementBase::Type::Element)
- {
- GUIElement* element = static_cast<GUIElement*>(child);
- element->_setWidth(elemWidth);
- UINT32 elemHeight = optimalSizes[childIdx].y;
- const GUILayoutOptions& layoutOptions = element->_getLayoutOptions();
- if (!layoutOptions.fixedHeight)
- {
- elemHeight = height;
- if (layoutOptions.minHeight > 0 && elemHeight < layoutOptions.minHeight)
- elemHeight = layoutOptions.minHeight;
- if (layoutOptions.maxHeight > 0 && elemHeight > layoutOptions.maxHeight)
- elemHeight = layoutOptions.maxHeight;
- }
- elementAreas[childIdx].height = elemHeight;
- UINT32 yPadding = element->_getPadding().top + element->_getPadding().bottom;
- INT32 yOffset = Math::ceilToInt(((INT32)height - (INT32)(elemHeight + yPadding)) * 0.5f);
- yOffset = std::max(0, yOffset);
- elementAreas[childIdx].x = xOffset;
- elementAreas[childIdx].y = yOffset;
- }
- else if (child->_getType() == GUIElementBase::Type::Layout)
- {
- GUILayout* layout = static_cast<GUILayout*>(child);
- elementAreas[childIdx].height = height;
- elementAreas[childIdx].x = xOffset;
- elementAreas[childIdx].y = 0;
- }
- xOffset += elemWidth + child->_getPadding().right;
- childIdx++;
- }
- if (elementScaleWeights != nullptr)
- stackDeallocLast(elementScaleWeights);
- if (processedElements != nullptr)
- stackDeallocLast(processedElements);
- }
- void GUILayoutX::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height, RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
- {
- UINT32 numElements = (UINT32)mChildren.size();
- RectI* elementAreas = nullptr;
- if (numElements > 0)
- elementAreas = stackConstructN<RectI>(numElements);
- getElementAreas(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)
- {
- RectI 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(x + childArea.x, y + childArea.y);
- element->_setOffset(offset);
- element->_setWidgetDepth(widgetDepth);
- element->_setAreaDepth(areaDepth);
- RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
- element->_setClipRect(elemClipRect);
- RectI 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);
- mActualHeight = std::max(height, (UINT32)childArea.height);
- }
- else if(child->_getType() == GUIElementBase::Type::Layout)
- {
- GUILayout* layout = static_cast<GUILayout*>(child);
- RectI newClipRect(x + childArea.x, y, childArea.width, height);
- newClipRect.clip(clipRect);
- layout->_updateLayoutInternal(x + childArea.x, y, childArea.width, height, newClipRect, widgetDepth, areaDepth);
- UINT32 childHeight = layout->_getActualHeight();
- mActualHeight = std::max(height, childHeight);
- }
- mActualWidth += childArea.width + child->_getPadding().left + child->_getPadding().right;
- childIdx++;
- }
- if(elementAreas != nullptr)
- stackDeallocLast(elementAreas);
- _markAsClean();
- }
- }
|