From c0b56f9f763151c31f6bcf63706060ebbd3bca64 Mon Sep 17 00:00:00 2001 From: Jasper Date: Fri, 20 May 2022 14:49:52 +0200 Subject: [PATCH] Recalculate widget position after changing width --- include/widgets.h | 2 +- src/widgets.cpp | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/include/widgets.h b/include/widgets.h index f5c9b59..98bf83b 100644 --- a/include/widgets.h +++ b/include/widgets.h @@ -33,7 +33,7 @@ public: virtual void draw() = 0; protected: - void recalculate_rect(); + void recalculate_pos(); SDL_Renderer* renderer; SDL_Rect rect; diff --git a/src/widgets.cpp b/src/widgets.cpp index da7361e..d2861a5 100644 --- a/src/widgets.cpp +++ b/src/widgets.cpp @@ -21,12 +21,14 @@ void Widget::set_width(int width, bool preserve_aspect_ratio) { rect.h = round(rect.h * scale); } rect.w = width; + recalculate_pos(); } void Widget::set_height(int height, bool preserve_aspect_ratio) { if (preserve_aspect_ratio) { float scale = ((float)height) / rect.h; rect.w = round(rect.w * scale); + recalculate_pos(); } rect.h = height; } @@ -34,12 +36,12 @@ void Widget::set_height(int height, bool preserve_aspect_ratio) { void Widget::set_position(int x, int y) { pos.x = x; pos.y = y; - recalculate_rect(); + recalculate_pos(); } void Widget::set_alignment(Alignment align) { pos.align = align; - recalculate_rect(); + recalculate_pos(); } int Widget::get_width() { return rect.w; } @@ -48,7 +50,7 @@ int Widget::get_height() { return rect.h; } const PositionInfo& Widget::get_position() { return pos; } -void Widget::recalculate_rect() { +void Widget::recalculate_pos() { int x = pos.x; int y = pos.y; switch (pos.align) {