UI Testing and fixes
This commit is contained in:
		| @@ -26,8 +26,11 @@ void UIAlignableElement::updateSelfAlignment( | |||||||
|     const enum UIAlignmentUnit unit, |     const enum UIAlignmentUnit unit, | ||||||
|     const float_t alignment, |     const float_t alignment, | ||||||
|     const float_t parentSize, |     const float_t parentSize, | ||||||
|     const float_t ratioSize |     const float_t ratioSize, | ||||||
|  |     const float_t contentSize | ||||||
|   ) { |   ) { | ||||||
|  |     if(alignment == UI_ALIGN_SIZE_AUTO) return contentSize; | ||||||
|  |  | ||||||
|     switch(unit) { |     switch(unit) { | ||||||
|       case UIAlignmentUnit::PIXEL: |       case UIAlignmentUnit::PIXEL: | ||||||
|         return alignment; |         return alignment; | ||||||
| @@ -55,7 +58,7 @@ void UIAlignableElement::updateSelfAlignment( | |||||||
|     const float_t alignment1, |     const float_t alignment1, | ||||||
|     const float_t parentSize, |     const float_t parentSize, | ||||||
|     const float_t ratioSize, |     const float_t ratioSize, | ||||||
|     const float_t selfSize, |     const float_t contentSize, | ||||||
|     float_t &outPosition, |     float_t &outPosition, | ||||||
|     float_t &outSize |     float_t &outSize | ||||||
|   ) { |   ) { | ||||||
| @@ -65,13 +68,15 @@ void UIAlignableElement::updateSelfAlignment( | |||||||
|           unit0, |           unit0, | ||||||
|           alignment0, |           alignment0, | ||||||
|           parentSize, |           parentSize, | ||||||
|           ratioSize |           ratioSize, | ||||||
|  |           contentSize | ||||||
|         ); |         ); | ||||||
|         outSize = valueAxis( |         outSize = valueAxis( | ||||||
|           unit1, |           unit1, | ||||||
|           alignment1, |           alignment1, | ||||||
|           parentSize, |           parentSize, | ||||||
|           ratioSize |           ratioSize, | ||||||
|  |           contentSize | ||||||
|         ); |         ); | ||||||
|         break; |         break; | ||||||
|  |  | ||||||
| @@ -80,13 +85,15 @@ void UIAlignableElement::updateSelfAlignment( | |||||||
|           unit1, |           unit1, | ||||||
|           alignment1, |           alignment1, | ||||||
|           parentSize, |           parentSize, | ||||||
|           ratioSize |           ratioSize, | ||||||
|  |           contentSize | ||||||
|         ); |         ); | ||||||
|         outPosition = (parentSize / 2.0f) - (selfSize / 2.0f) + valueAxis( |         outPosition = (parentSize / 2.0f) - (contentSize / 2.0f) + valueAxis( | ||||||
|           unit0, |           unit0, | ||||||
|           alignment0, |           alignment0, | ||||||
|           parentSize, |           parentSize, | ||||||
|           ratioSize |           ratioSize, | ||||||
|  |           contentSize | ||||||
|         ); |         ); | ||||||
|         break; |         break; | ||||||
|  |  | ||||||
| @@ -95,13 +102,15 @@ void UIAlignableElement::updateSelfAlignment( | |||||||
|           unit0, |           unit0, | ||||||
|           alignment0, |           alignment0, | ||||||
|           parentSize, |           parentSize, | ||||||
|           ratioSize |           ratioSize, | ||||||
|  |           contentSize | ||||||
|         ); |         ); | ||||||
|         outPosition = parentSize - outSize - valueAxis( |         outPosition = parentSize - outSize - valueAxis( | ||||||
|           unit1, |           unit1, | ||||||
|           alignment1, |           alignment1, | ||||||
|           parentSize, |           parentSize, | ||||||
|           ratioSize |           ratioSize, | ||||||
|  |           contentSize | ||||||
|         ); |         ); | ||||||
|         break; |         break; | ||||||
|  |  | ||||||
| @@ -110,13 +119,15 @@ void UIAlignableElement::updateSelfAlignment( | |||||||
|           unit0, |           unit0, | ||||||
|           alignment0, |           alignment0, | ||||||
|           parentSize, |           parentSize, | ||||||
|           ratioSize |           ratioSize, | ||||||
|  |           contentSize | ||||||
|         ); |         ); | ||||||
|         outSize = parentSize - (outPosition + valueAxis( |         outSize = parentSize - (outPosition + valueAxis( | ||||||
|           unit1, |           unit1, | ||||||
|           alignment1, |           alignment1, | ||||||
|           parentSize, |           parentSize, | ||||||
|           ratioSize |           ratioSize, | ||||||
|  |           contentSize | ||||||
|         )); |         )); | ||||||
|         break; |         break; | ||||||
|  |  | ||||||
| @@ -178,7 +189,7 @@ void UIAlignableElement::updateSelfAlignment( | |||||||
|       align[3], |       align[3], | ||||||
|       pSize.y, |       pSize.y, | ||||||
|       size.x, |       size.x, | ||||||
|       this->getContentWidth(), |       this->getContentHeight(), | ||||||
|       position.y, |       position.y, | ||||||
|       size.y |       size.y | ||||||
|     ); |     ); | ||||||
| @@ -188,6 +199,39 @@ void UIAlignableElement::updateSelfAlignment( | |||||||
|   this->eventAlignmentUpdated(position, size); |   this->eventAlignmentUpdated(position, size); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | bool_t UIAlignableElement::hasExplicitWidth() { | ||||||
|  |   if(size.x == 0.0f) return false; | ||||||
|  |   if( | ||||||
|  |     (alignX == UIAlignmentType::STRETCH) || | ||||||
|  |     (alignX == UIAlignmentType::END) | ||||||
|  |   ) { | ||||||
|  |     return align[0] != UI_ALIGN_SIZE_AUTO; | ||||||
|  |   } | ||||||
|  |   return align[2] != UI_ALIGN_SIZE_AUTO; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | bool_t UIAlignableElement::hasExplicitHeight() { | ||||||
|  |   if(size.y == 0.0f) return false; | ||||||
|  |   if( | ||||||
|  |     (alignY == UIAlignmentType::STRETCH) || | ||||||
|  |     (alignY == UIAlignmentType::END) | ||||||
|  |   ) { | ||||||
|  |     return align[1] != UI_ALIGN_SIZE_AUTO; | ||||||
|  |   } | ||||||
|  |   return align[3] != UI_ALIGN_SIZE_AUTO; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | float_t UIAlignableElement::getWidth() { | ||||||
|  |   if(hasExplicitWidth()) return size.x; | ||||||
|  |   return getContentWidth(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | float_t UIAlignableElement::getHeight() { | ||||||
|  |   if(hasExplicitHeight()) return size.y; | ||||||
|  |   return getContentHeight(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
| void UIAlignableElement::updateAlignment( | void UIAlignableElement::updateAlignment( | ||||||
|   const glm::vec2 pPos, |   const glm::vec2 pPos, | ||||||
|   const glm::vec2 pSize, |   const glm::vec2 pSize, | ||||||
|   | |||||||
| @@ -7,6 +7,8 @@ | |||||||
| #include "ui/UIElement.hpp" | #include "ui/UIElement.hpp" | ||||||
|  |  | ||||||
| namespace Dawn { | namespace Dawn { | ||||||
|  |   #define UI_ALIGN_SIZE_AUTO -1.0f | ||||||
|  |  | ||||||
|   enum class UIAlignmentType { |   enum class UIAlignmentType { | ||||||
|     START, |     START, | ||||||
|     MIDDLE, |     MIDDLE, | ||||||
| @@ -39,6 +41,20 @@ namespace Dawn { | |||||||
|         const float_t canvasScale |         const float_t canvasScale | ||||||
|       ); |       ); | ||||||
|  |  | ||||||
|  |       /** | ||||||
|  |        * Returns true only if the width of this component is explicitly set. | ||||||
|  |        *  | ||||||
|  |        * @return True if the width of this component is explicitly set. | ||||||
|  |        */ | ||||||
|  |       bool_t hasExplicitWidth(); | ||||||
|  |  | ||||||
|  |       /** | ||||||
|  |        * Returns true only if the height of this component is explicitly set. | ||||||
|  |        *  | ||||||
|  |        * @return True if the height of this component is explicitly set. | ||||||
|  |        */ | ||||||
|  |       bool_t hasExplicitHeight(); | ||||||
|  |  | ||||||
|     public: |     public: | ||||||
|       // Primary alignment controls |       // Primary alignment controls | ||||||
|       glm::vec4 align = glm::vec4(0, 0, 0, 0); |       glm::vec4 align = glm::vec4(0, 0, 0, 0); | ||||||
| @@ -55,6 +71,9 @@ namespace Dawn { | |||||||
|        */ |        */ | ||||||
|       UIAlignableElement(); |       UIAlignableElement(); | ||||||
|  |  | ||||||
|  |       float_t getWidth() override; | ||||||
|  |       float_t getHeight() override; | ||||||
|  |  | ||||||
|       void updateAlignment( |       void updateAlignment( | ||||||
|         const glm::vec2 parentPosition, |         const glm::vec2 parentPosition, | ||||||
|         const glm::vec2 parentSize, |         const glm::vec2 parentSize, | ||||||
|   | |||||||
| @@ -12,23 +12,23 @@ std::vector<std::shared_ptr<UIElement>> UIContainer::getChildren() { | |||||||
|   return this->children; |   return this->children; | ||||||
| } | } | ||||||
|  |  | ||||||
| // float_t UIContainer::getSelfWidth() { | float_t UIContainer::getContentWidth() { | ||||||
| //   float_t width = 0; |   float_t width = 0; | ||||||
| //   auto children = this->getChildren(); |   auto children = this->getChildren(); | ||||||
| //   for(auto child : children) { |   for(auto child : children) { | ||||||
| //     width = Math::max(width, child->getWidth()); |     width = Math::max(width, child->getWidth()); | ||||||
| //   } |   } | ||||||
| //   return width; |   return width; | ||||||
| // } | } | ||||||
|  |  | ||||||
| // float_t UIContainer::getSelfHeight() { | float_t UIContainer::getContentHeight() { | ||||||
| //   float_t height = 0; |   float_t height = 0; | ||||||
| //   auto children = this->getChildren(); |   auto children = this->getChildren(); | ||||||
| //   for(auto child : children) { |   for(auto child : children) { | ||||||
| //     height = Math::max(height, child->getHeight()); |     height = Math::max(height, child->getHeight()); | ||||||
| //   } |   } | ||||||
| //   return height; |   return height; | ||||||
| // } | } | ||||||
|  |  | ||||||
| void UIContainer::appendChild(std::shared_ptr<UIElement> child) { | void UIContainer::appendChild(std::shared_ptr<UIElement> child) { | ||||||
|   assertNotNull(child, "Cannot append a null child!"); |   assertNotNull(child, "Cannot append a null child!"); | ||||||
|   | |||||||
| @@ -14,6 +14,9 @@ namespace Dawn { | |||||||
|     public: |     public: | ||||||
|       std::vector<std::shared_ptr<UIElement>> getChildren() override; |       std::vector<std::shared_ptr<UIElement>> getChildren() override; | ||||||
|  |  | ||||||
|  |       float_t getContentWidth() override; | ||||||
|  |       float_t getContentHeight() override; | ||||||
|  |        | ||||||
|       /** |       /** | ||||||
|        * Appends a child to this container. |        * Appends a child to this container. | ||||||
|        *  |        *  | ||||||
|   | |||||||
| @@ -56,10 +56,10 @@ void UILabel::getSelfQuads(UICanvas &ctx) { | |||||||
|  |  | ||||||
|         lastCharWasSpace = false; |         lastCharWasSpace = false; | ||||||
|       } |       } | ||||||
|     } else if(pos.x + info.size.x > subAlignedPosition.x + size.x) { |     // } else if(pos.x + info.size.x > subAlignedPosition.x + size.x) { | ||||||
|       // Not word wrap, but instead just overflow characters. |     //   // Not word wrap, but instead just overflow characters. | ||||||
|       pos.x = 0; |     //   pos.x = 0; | ||||||
|       pos.y += this->texture->fontSize; |     //   pos.y += this->texture->fontSize; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     ctx.addQuad( |     ctx.addQuad( | ||||||
| @@ -97,10 +97,13 @@ float_t UILabel::getContentWidth() { | |||||||
|  |  | ||||||
|     auto info = texture->getCharacterData(c); |     auto info = texture->getCharacterData(c); | ||||||
|     lineWidth += info.advance.x; |     lineWidth += info.advance.x; | ||||||
|     if(lineWidth >= size.x) return size.x; |     if( | ||||||
|  |       this->hasExplicitWidth() && | ||||||
|  |       lineWidth >= size.x | ||||||
|  |     ) return size.x; | ||||||
|   } |   } | ||||||
|  |   width = Math::max<float_t>(width, lineWidth); | ||||||
|   return Math::max<float_t>(width, lineWidth); |   return width; | ||||||
| } | } | ||||||
|  |  | ||||||
| float_t UILabel::getContentHeight() { | float_t UILabel::getContentHeight() { | ||||||
| @@ -146,9 +149,9 @@ float_t UILabel::getContentHeight() { | |||||||
|  |  | ||||||
|         lastCharWasSpace = false; |         lastCharWasSpace = false; | ||||||
|       } |       } | ||||||
|     } else if(lineWidth + info.size.x > size.x) { |     // } else if(lineWidth + info.size.x > size.x) { | ||||||
|       height += this->texture->fontSize; |     //   height += this->texture->fontSize; | ||||||
|       lineWidth = 0.0f; |     //   lineWidth = 0.0f; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -59,7 +59,7 @@ void Dawn::helloWorldScene(Scene &s) { | |||||||
|   auto uiCanvas = uiCanvasItem->addComponent<UICanvas>(); |   auto uiCanvas = uiCanvasItem->addComponent<UICanvas>(); | ||||||
|  |  | ||||||
|   auto container = std::make_shared<UIContainer>(); |   auto container = std::make_shared<UIContainer>(); | ||||||
|   container->align = { 0, 0, 9999, 9999 }; |   container->align = { 32, 32, UI_ALIGN_SIZE_AUTO, UI_ALIGN_SIZE_AUTO }; | ||||||
|   container->alignX = UIAlignmentType::START; |   container->alignX = UIAlignmentType::START; | ||||||
|   container->alignY = UIAlignmentType::START; |   container->alignY = UIAlignmentType::START; | ||||||
|   uiCanvas->addElement(container); |   uiCanvas->addElement(container); | ||||||
| @@ -82,24 +82,20 @@ void Dawn::helloWorldScene(Scene &s) { | |||||||
|     L"Exit" |     L"Exit" | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   float_t maxX = 0; |  | ||||||
|   float_t height = 0; |  | ||||||
|   for(auto &label : labels) { |   for(auto &label : labels) { | ||||||
|     auto padding = std::make_shared<UIPaddingContainer>(); |     auto padding = std::make_shared<UIPaddingContainer>(); | ||||||
|  |     padding->align = { 0, 0, UI_ALIGN_SIZE_AUTO, UI_ALIGN_SIZE_AUTO }; | ||||||
|  |     padding->alignX = UIAlignmentType::START; | ||||||
|  |     padding->alignY = UIAlignmentType::START; | ||||||
|     padding->padding = { 8, 8, 8, 8 }; |     padding->padding = { 8, 8, 8, 8 }; | ||||||
|  |  | ||||||
|     auto labelElement = std::make_shared<UILabel>(); |     auto labelElement = std::make_shared<UILabel>(); | ||||||
|  |     labelElement->wordWrap = false; | ||||||
|  |     labelElement->align = { 0, 0, UI_ALIGN_SIZE_AUTO, UI_ALIGN_SIZE_AUTO }; | ||||||
|     labelElement->setText(label); |     labelElement->setText(label); | ||||||
|     labelElement->setFont(texture); |     labelElement->setFont(texture); | ||||||
|     labelElement->subAlignX = UISubAlignment::MIDDLE; |  | ||||||
|     labelElement->subAlignY = UISubAlignment::MIDDLE; |  | ||||||
|      |      | ||||||
|     padding->appendChild(labelElement); |     padding->appendChild(labelElement); | ||||||
|     rowContainer->appendChild(padding); |     rowContainer->appendChild(padding); | ||||||
|      |  | ||||||
|     maxX = Math::max(maxX, padding->getWidth()); |  | ||||||
|     height += padding->getHeight(); |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   container->align = { 32, 32, maxX, height }; |  | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user