ADD: Consider sibling offsets

This commit is contained in:
sebastian 2026-02-12 12:08:00 +01:00
parent e2a80e3924
commit d9a1596ea2
3 changed files with 29 additions and 4 deletions

View File

@ -25,9 +25,9 @@ public:
virtual bool isMouseOver(float mouseX, float mouseY); virtual bool isMouseOver(float mouseX, float mouseY);
UiComponent* parent = nullptr; UiComponent* parent = nullptr;
UiPositioner uiPositioner; UiPositioner uiPositioner;
std::vector<std::unique_ptr<UiComponent>> children;
protected: protected:
std::vector<std::unique_ptr<UiComponent>> children;
bool visible = true; bool visible = true;

View File

@ -11,11 +11,30 @@ void UiPositioner::compute(const Dimensions &parent) {
const float screenWidth = static_cast<float>(Application::getInstance().getWindow().GetWidth()); const float screenWidth = static_cast<float>(Application::getInstance().getWindow().GetWidth());
const float screenHeight = static_cast<float>(Application::getInstance().getWindow().GetHeight()); const float screenHeight = static_cast<float>(Application::getInstance().getWindow().GetHeight());
screenSpace.x = parent.x + resolve(style.margin.left, parent.width, screenWidth); screenSpace.x = parent.x + resolve(style.margin.left, parent.width, screenWidth);
screenSpace.y = parent.y + resolve(style.margin.top, parent.height, screenHeight); screenSpace.y = parent.y + resolve(style.margin.top, parent.height, screenHeight);
screenSpace.width = resolve(style.width, parent.width, screenWidth); screenSpace.width = resolve(style.width, parent.width, screenWidth);
screenSpace.height = resolve(style.height, parent.height, screenHeight); screenSpace.height = resolve(style.height, parent.height, screenHeight);
// 2. Sibling-Offset nur anwenden, wenn parent existiert
if (uiComponent.parent) {
float offsetY = 0.0f;
for (auto& sibling : uiComponent.parent->children) {
if (&sibling->uiPositioner == this) break; // nur bis zu mir
offsetY += sibling->uiPositioner.screenSpace.height;
offsetY += sibling->uiPositioner.resolve(sibling->uiPositioner.style.margin.top, parent.height, screenHeight);
}
screenSpace.y += offsetY;
}
// 3. Kinder rekursiv positionieren
float currentChildOffset = 0.0f;
for (auto& child : uiComponent.children) {
child->uiPositioner.compute(screenSpace);
currentChildOffset += child->uiPositioner.screenSpace.height;
currentChildOffset += child->uiPositioner.resolve(child->uiPositioner.style.margin.top, screenSpace.height, screenHeight);
}
} }

View File

@ -23,8 +23,8 @@ void UILayer::onAttach() {
rootContainer = std::make_unique<UiContainer>(); rootContainer = std::make_unique<UiContainer>();
auto imageStyle = LayoutStyle(); auto imageStyle = LayoutStyle();
imageStyle.width = SizeValue(0.5f, SizeUnit::Percent); imageStyle.width = SizeValue(0.25f, SizeUnit::Percent);
imageStyle.height = SizeValue(0.5f, SizeUnit::Percent); imageStyle.height = SizeValue(0.25f, SizeUnit::Percent);
imageStyle.margin.left = {50.f, SizeUnit::Pixels}; // 50px imageStyle.margin.left = {50.f, SizeUnit::Pixels}; // 50px
imageStyle.margin.top = {0.1f, SizeUnit::Percent}; // 10% vom Parent imageStyle.margin.top = {0.1f, SizeUnit::Percent}; // 10% vom Parent
@ -32,7 +32,13 @@ void UILayer::onAttach() {
Loader::instance().loadTextureFromFile("assets/textures/texture.png").getTextureID(), Loader::instance().loadTextureFromFile("assets/textures/texture.png").getTextureID(),
imageStyle imageStyle
); );
auto image2 = std::make_unique<UiImage>(
Loader::instance().loadTextureFromFile("assets/textures/texture.png").getTextureID(),
imageStyle
);
rootContainer->addChild(std::move(image)); rootContainer->addChild(std::move(image));
rootContainer->addChild(std::move(image2));