| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "GUI/BsGUILayoutX.h"
- #include "GUI/BsGUIElement.h"
- #include "GUI/BsGUISpace.h"
- #include "Math/BsMath.h"
- #include "Math/BsVector2I.h"
- #include "Profiling/BsProfilerCPU.h"
- namespace bs
- {
- GUILayoutX::GUILayoutX(const GUIDimensions& dimensions)
- : GUILayout(dimensions)
- { }
- LayoutSizeRange GUILayoutX::_calculateLayoutSizeRange() const
- {
- Vector2I optimalSize;
- Vector2I minSize;
- for (auto& child : mChildren)
- {
- if (!child->_isActive())
- continue;
- LayoutSizeRange sizeRange = child->_calculateLayoutSizeRange();
- if (child->_getType() == GUIElementBase::Type::FixedSpace)
- sizeRange.optimal.y = sizeRange.min.y = 0;
- UINT32 paddingX = child->_getPadding().left + child->_getPadding().right;
- UINT32 paddingY = child->_getPadding().top + child->_getPadding().bottom;
- optimalSize.x += sizeRange.optimal.x + paddingX;
- optimalSize.y = std::max((UINT32)optimalSize.y, sizeRange.optimal.y + paddingY);
- minSize.x += sizeRange.min.x + paddingX;
- minSize.y = std::max((UINT32)minSize.y, sizeRange.min.y + paddingY);
- }
- LayoutSizeRange sizeRange = _getDimensions().calculateSizeRange(optimalSize);
- sizeRange.min.x = std::max(sizeRange.min.x, minSize.x);
- sizeRange.min.y = std::max(sizeRange.min.y, minSize.y);
- return sizeRange;
- }
- void GUILayoutX::_updateOptimalLayoutSizes()
- {
- // Update all children first, otherwise we can't determine our own optimal size
- GUIElementBase::_updateOptimalLayoutSizes();
- if(mChildren.size() != mChildSizeRanges.size())
- mChildSizeRanges.resize(mChildren.size());
- Vector2I optimalSize;
- Vector2I minSize;
- UINT32 childIdx = 0;
- for(auto& child : mChildren)
- {
- LayoutSizeRange& childSizeRange = mChildSizeRanges[childIdx];
- if (child->_isActive())
- {
- childSizeRange = child->_getLayoutSizeRange();
- if (child->_getType() == GUIElementBase::Type::FixedSpace)
- {
- childSizeRange.optimal.y = 0;
- childSizeRange.min.y = 0;
- }
- UINT32 paddingX = child->_getPadding().left + child->_getPadding().right;
- UINT32 paddingY = child->_getPadding().top + child->_getPadding().bottom;
- optimalSize.x += childSizeRange.optimal.x + paddingX;
- optimalSize.y = std::max((UINT32)optimalSize.y, childSizeRange.optimal.y + paddingY);
- minSize.x += childSizeRange.min.x + paddingX;
- minSize.y = std::max((UINT32)minSize.y, childSizeRange.min.y + paddingY);
- }
- else
- childSizeRange = LayoutSizeRange();
- childIdx++;
- }
- mSizeRange = _getDimensions().calculateSizeRange(optimalSize);
- mSizeRange.min.x = std::max(mSizeRange.min.x, minSize.x);
- mSizeRange.min.y = std::max(mSizeRange.min.y, minSize.y);
- }
- void GUILayoutX::_getElementAreas(const Rect2I& layoutArea, Rect2I* elementAreas, UINT32 numElements,
- const Vector<LayoutSizeRange>& sizeRanges, const LayoutSizeRange& mySizeRange) const
- {
- assert(mChildren.size() == numElements);
- UINT32 totalOptimalSize = mySizeRange.optimal.x;
- UINT32 totalNonClampedSize = 0;
- UINT32 numNonClampedElements = 0;
- UINT32 numFlexibleSpaces = 0;
- bool* processedElements = nullptr;
- float* elementScaleWeights = nullptr;
- if (mChildren.size() > 0)
- {
- processedElements = bs_stack_alloc<bool>((UINT32)mChildren.size());
- memset(processedElements, 0, mChildren.size() * sizeof(bool));
- elementScaleWeights = bs_stack_alloc<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 = sizeRanges[childIdx].optimal.x;
- if (child->_getType() == GUIElementBase::Type::FixedSpace)
- {
- processedElements[childIdx] = true;
- }
- else if (child->_getType() == GUIElementBase::Type::FlexibleSpace)
- {
- if (child->_isActive())
- {
- numFlexibleSpaces++;
- numNonClampedElements++;
- }
- else
- processedElements[childIdx] = true;
- }
- else
- {
- const GUIDimensions& dimensions = child->_getDimensions();
- if (dimensions.fixedWidth())
- processedElements[childIdx] = true;
- else
- {
- if (elementAreas[childIdx].width > 0)
- {
- numNonClampedElements++;
- totalNonClampedSize += elementAreas[childIdx].width;
- }
- else
- processedElements[childIdx] = true;
- }
- }
- childIdx++;
- }
- // If there is some room left, calculate flexible space sizes (since they will fill up all that extra room)
- if ((UINT32)layoutArea.width > totalOptimalSize)
- {
- UINT32 extraSize = layoutArea.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 = layoutArea.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;
- UINT32 childCount = (UINT32)mChildren.size();
- for (UINT32 i = 0; i < childCount; i++)
- {
- 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 > (UINT32)layoutArea.width)
- {
- UINT32 extraSize = totalOptimalSize - layoutArea.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
- switch (child->_getType())
- {
- case GUIElementBase::Type::FlexibleSpace:
- elementAreas[childIdx].width = 0;
- processedElements[childIdx] = true;
- numNonClampedElements--;
- break;
- case GUIElementBase::Type::Element:
- case GUIElementBase::Type::Layout:
- case GUIElementBase::Type::Panel:
- {
- const LayoutSizeRange& childSizeRange = sizeRanges[childIdx];
- if (elementWidth == 0)
- {
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- else if (childSizeRange.min.x > 0 && (INT32)elementWidth < childSizeRange.min.x)
- {
- elementWidth = childSizeRange.min.x;
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- extraWidth = elementAreas[childIdx].width - elementWidth;
- elementAreas[childIdx].width = elementWidth;
- remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraWidth);
- }
- break;
- case GUIElementBase::Type::FixedSpace:
- break;
- }
- childIdx++;
- }
- }
- }
- else // We are smaller than the allowed maximum, so try to expand some elements
- {
- UINT32 extraSize = layoutArea.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
- switch (child->_getType())
- {
- case GUIElementBase::Type::FlexibleSpace:
- processedElements[childIdx] = true;
- numNonClampedElements--;
- break;
- case GUIElementBase::Type::Element:
- case GUIElementBase::Type::Layout:
- case GUIElementBase::Type::Panel:
- {
- const LayoutSizeRange& childSizeRange = sizeRanges[childIdx];
- if (elementWidth == 0)
- {
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- else if (childSizeRange.max.x > 0 && (INT32)elementWidth > childSizeRange.max.x)
- {
- elementWidth = childSizeRange.max.x;
- processedElements[childIdx] = true;
- numNonClampedElements--;
- }
- extraWidth = elementWidth - elementAreas[childIdx].width;
- elementAreas[childIdx].width = elementWidth;
- remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraWidth);
- }
- break;
- case GUIElementBase::Type::FixedSpace:
- break;
- }
- childIdx++;
- }
- }
- }
- // Compute offsets and height
- UINT32 xOffset = 0;
- childIdx = 0;
- for (auto& child : mChildren)
- {
- UINT32 elemWidth = elementAreas[childIdx].width;
- xOffset += child->_getPadding().left;
- const LayoutSizeRange& sizeRange = sizeRanges[childIdx];
- UINT32 elemHeight = (UINT32)sizeRange.optimal.y;
- const GUIDimensions& dimensions = child->_getDimensions();
- if (!dimensions.fixedHeight())
- {
- elemHeight = layoutArea.height;
- if (sizeRange.min.y > 0 && elemHeight < (UINT32)sizeRange.min.y)
- elemHeight = (UINT32)sizeRange.min.y;
- if (sizeRange.max.y > 0 && elemHeight > (UINT32)sizeRange.max.y)
- elemHeight = (UINT32)sizeRange.max.y;
- }
- elementAreas[childIdx].height = elemHeight;
- if (child->_getType() == GUIElementBase::Type::Element)
- {
- GUIElement* element = static_cast<GUIElement*>(child);
- UINT32 yPadding = element->_getPadding().top + element->_getPadding().bottom;
- INT32 yOffset = Math::ceilToInt(((INT32)layoutArea.height - (INT32)(elemHeight + yPadding)) * 0.5f);
- yOffset = std::max(0, yOffset);
- elementAreas[childIdx].x = layoutArea.x + xOffset;
- elementAreas[childIdx].y = layoutArea.y + yOffset;
- }
- else
- {
- elementAreas[childIdx].x = layoutArea.x + xOffset;
- elementAreas[childIdx].y = layoutArea.y;
- }
- xOffset += elemWidth + child->_getPadding().right;
- childIdx++;
- }
- if (elementScaleWeights != nullptr)
- bs_stack_free(elementScaleWeights);
- if (processedElements != nullptr)
- bs_stack_free(processedElements);
- }
- void GUILayoutX::_updateLayoutInternal(const GUILayoutData& data)
- {
- UINT32 numElements = (UINT32)mChildren.size();
- Rect2I* elementAreas = nullptr;
- if (numElements > 0)
- elementAreas = bs_stack_new<Rect2I>(numElements);
- _getElementAreas(data.area, elementAreas, numElements, mChildSizeRanges, mSizeRange);
- // Now that we have all the areas, actually assign them
- UINT32 childIdx = 0;
- GUILayoutData childData = data;
- for(auto& child : mChildren)
- {
- if (child->_isActive())
- {
- childData.area = elementAreas[childIdx];
- childData.clipRect = childData.area;
- childData.clipRect.clip(data.clipRect);
- child->_setLayoutData(childData);
- child->_updateLayoutInternal(childData);
- }
- childIdx++;
- }
- if(elementAreas != nullptr)
- bs_stack_free(elementAreas);
- }
- GUILayoutX* GUILayoutX::create()
- {
- return bs_new<GUILayoutX>();
- }
- GUILayoutX* GUILayoutX::create(const GUIOptions& options)
- {
- return bs_new<GUILayoutX>(GUIDimensions::create(options));
- }
- }
|