ADD: Margin System

This commit is contained in:
sebastian 2026-02-12 10:51:55 +01:00
parent f56aed436d
commit 51c490e0b8
2 changed files with 18 additions and 10 deletions

View File

@ -8,15 +8,21 @@
#include "../../../Application.h"
void UiPositioner::compute(const Dimensions &parent) {
const auto screenWidth = static_cast<float>(Application::getInstance().getWindow().GetWidth());
const auto screenHeight = static_cast<float>(Application::getInstance().getWindow().GetHeight());
const float screenWidth = static_cast<float>(Application::getInstance().getWindow().GetWidth());
const float screenHeight = static_cast<float>(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:

View File

@ -23,10 +23,12 @@ void UILayer::onAttach() {
rootContainer = std::make_unique<UiContainer>();
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<UiImage>(
Loader::instance().loadTextureFromFile("assets/textures/texture.png").getTextureID(),