From 51c490e0b8552cf939dc5bf2e756c0b72183ef11 Mon Sep 17 00:00:00 2001 From: sebastian Date: Thu, 12 Feb 2026 10:51:55 +0100 Subject: [PATCH] ADD: Margin System --- .../gui/uiComponent/layout/UiPositioner.cpp | 18 ++++++++++++------ src/game/UILayer.cpp | 10 ++++++---- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/engine/core/gui/uiComponent/layout/UiPositioner.cpp b/src/engine/core/gui/uiComponent/layout/UiPositioner.cpp index eb8196f..9038f25 100644 --- a/src/engine/core/gui/uiComponent/layout/UiPositioner.cpp +++ b/src/engine/core/gui/uiComponent/layout/UiPositioner.cpp @@ -8,15 +8,21 @@ #include "../../../Application.h" void UiPositioner::compute(const Dimensions &parent) { - const auto screenWidth = static_cast(Application::getInstance().getWindow().GetWidth()); - const auto screenHeight = static_cast(Application::getInstance().getWindow().GetHeight()); + 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); + float left = resolve(style.margin.left, parent.width, screenWidth); + float right = resolve(style.margin.right, parent.width, screenWidth); + float top = resolve(style.margin.top, parent.height, screenHeight); + float bottom = resolve(style.margin.bottom, parent.height, screenHeight); + + screenSpace.x = parent.x + left; + screenSpace.y = parent.y + top; + screenSpace.width = resolve(style.width, parent.width, screenWidth) - left - right; + screenSpace.height = resolve(style.height, parent.height, screenHeight) - top - bottom; } + float UiPositioner::resolve(const SizeValue &size, float parentSize, float axisLength) { switch (size.unit) { case SizeUnit::Percent: diff --git a/src/game/UILayer.cpp b/src/game/UILayer.cpp index 09fdb2d..40417cc 100644 --- a/src/game/UILayer.cpp +++ b/src/game/UILayer.cpp @@ -23,10 +23,12 @@ 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.margin.left = {50.f, SizeUnit::Pixels}; - imageStyle.margin.top = {0.f, SizeUnit::Percent}; + imageStyle.width = SizeValue(1.f, SizeUnit::Percent); + imageStyle.height = SizeValue(1.f, SizeUnit::Percent); + imageStyle.margin.left = {50.f, SizeUnit::Pixels}; // 50px + imageStyle.margin.top = {0.1f, SizeUnit::Percent}; // 10% vom Parent + imageStyle.margin.right = {20.f, SizeUnit::Pixels}; // 20px + imageStyle.margin.bottom = {0.05f, SizeUnit::Percent}; // 5% vom Parent auto image = std::make_unique( Loader::instance().loadTextureFromFile("assets/textures/texture.png").getTextureID(),