UPD: Simplify Margin System to top and left

This commit is contained in:
sebastian 2026-02-12 11:07:29 +01:00
parent 51c490e0b8
commit e2a80e3924
3 changed files with 7 additions and 16 deletions

View File

@ -17,9 +17,7 @@ struct SizeValue {
struct Margin { struct Margin {
SizeValue left; SizeValue left;
SizeValue right;
SizeValue top; SizeValue top;
SizeValue bottom;
}; };
enum class FlexDirection { enum class FlexDirection {
@ -47,9 +45,7 @@ struct LayoutStyle {
Margin margin = { Margin margin = {
.left = {0.0f, SizeUnit::Percent}, .left = {0.0f, SizeUnit::Percent},
.right = {0.0f, SizeUnit::Percent},
.top = {0.0f, SizeUnit::Percent}, .top = {0.0f, SizeUnit::Percent},
.bottom = {0.0f, SizeUnit::Percent}
}; };
float flexGrow = 0.0f; float flexGrow = 0.0f;

View File

@ -11,15 +11,11 @@ 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());
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.x = parent.x + resolve(style.margin.left, parent.width, screenWidth);
screenSpace.y = parent.y + top; screenSpace.y = parent.y + resolve(style.margin.top, parent.height, screenHeight);
screenSpace.width = resolve(style.width, parent.width, screenWidth) - left - right; screenSpace.width = resolve(style.width, parent.width, screenWidth);
screenSpace.height = resolve(style.height, parent.height, screenHeight) - top - bottom; screenSpace.height = resolve(style.height, parent.height, screenHeight);
} }

View File

@ -23,12 +23,10 @@ void UILayer::onAttach() {
rootContainer = std::make_unique<UiContainer>(); rootContainer = std::make_unique<UiContainer>();
auto imageStyle = LayoutStyle(); auto imageStyle = LayoutStyle();
imageStyle.width = SizeValue(1.f, SizeUnit::Percent); imageStyle.width = SizeValue(0.5f, SizeUnit::Percent);
imageStyle.height = SizeValue(1.f, SizeUnit::Percent); imageStyle.height = SizeValue(0.5f, 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
imageStyle.margin.right = {20.f, SizeUnit::Pixels}; // 20px
imageStyle.margin.bottom = {0.05f, SizeUnit::Percent}; // 5% vom Parent
auto image = std::make_unique<UiImage>( auto image = std::make_unique<UiImage>(
Loader::instance().loadTextureFromFile("assets/textures/texture.png").getTextureID(), Loader::instance().loadTextureFromFile("assets/textures/texture.png").getTextureID(),
@ -37,6 +35,7 @@ void UILayer::onAttach() {
rootContainer->addChild(std::move(image)); rootContainer->addChild(std::move(image));
Font myFont("/usr/share/fonts/TTF/DejaVuSans.ttf", 48); Font myFont("/usr/share/fonts/TTF/DejaVuSans.ttf", 48);
font = std::make_unique<Font>(myFont); font = std::make_unique<Font>(myFont);
//auto text = std::make_unique<UiText>(*font, "Hello World!", glm::vec2(0.5f, 0.5f), glm::vec3(1,1,1)); //auto text = std::make_unique<UiText>(*font, "Hello World!", glm::vec2(0.5f, 0.5f), glm::vec3(1,1,1));