diff --git a/src/engine/core/gui/uiComponent/UiComponent.h b/src/engine/core/gui/uiComponent/UiComponent.h index acdfafa..c301782 100644 --- a/src/engine/core/gui/uiComponent/UiComponent.h +++ b/src/engine/core/gui/uiComponent/UiComponent.h @@ -25,9 +25,9 @@ public: virtual bool isMouseOver(float mouseX, float mouseY); UiComponent* parent = nullptr; UiPositioner uiPositioner; + std::vector> children; protected: - std::vector> children; bool visible = true; diff --git a/src/engine/core/gui/uiComponent/layout/UiPositioner.cpp b/src/engine/core/gui/uiComponent/layout/UiPositioner.cpp index 3c96079..e529012 100644 --- a/src/engine/core/gui/uiComponent/layout/UiPositioner.cpp +++ b/src/engine/core/gui/uiComponent/layout/UiPositioner.cpp @@ -11,11 +11,30 @@ void UiPositioner::compute(const Dimensions &parent) { const float screenWidth = static_cast(Application::getInstance().getWindow().GetWidth()); const float screenHeight = static_cast(Application::getInstance().getWindow().GetHeight()); - screenSpace.x = parent.x + resolve(style.margin.left, parent.width, screenWidth); screenSpace.y = parent.y + resolve(style.margin.top, parent.height, screenHeight); screenSpace.width = resolve(style.width, parent.width, screenWidth); 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); + } + } diff --git a/src/game/UILayer.cpp b/src/game/UILayer.cpp index 18fa8ff..9f547e4 100644 --- a/src/game/UILayer.cpp +++ b/src/game/UILayer.cpp @@ -23,8 +23,8 @@ void UILayer::onAttach() { rootContainer = std::make_unique(); auto imageStyle = LayoutStyle(); - imageStyle.width = SizeValue(0.5f, SizeUnit::Percent); - imageStyle.height = SizeValue(0.5f, SizeUnit::Percent); + imageStyle.width = SizeValue(0.25f, SizeUnit::Percent); + imageStyle.height = SizeValue(0.25f, SizeUnit::Percent); imageStyle.margin.left = {50.f, SizeUnit::Pixels}; // 50px 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(), imageStyle ); + + auto image2 = std::make_unique( + Loader::instance().loadTextureFromFile("assets/textures/texture.png").getTextureID(), + imageStyle + ); rootContainer->addChild(std::move(image)); + rootContainer->addChild(std::move(image2));