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