Recalculate widget position after changing width

This commit is contained in:
Jasper Blanckenburg 2022-05-20 14:49:52 +02:00
parent a904f0cdaf
commit c0b56f9f76
2 changed files with 6 additions and 4 deletions

View File

@ -33,7 +33,7 @@ public:
virtual void draw() = 0; virtual void draw() = 0;
protected: protected:
void recalculate_rect(); void recalculate_pos();
SDL_Renderer* renderer; SDL_Renderer* renderer;
SDL_Rect rect; SDL_Rect rect;

View File

@ -21,12 +21,14 @@ void Widget::set_width(int width, bool preserve_aspect_ratio) {
rect.h = round(rect.h * scale); rect.h = round(rect.h * scale);
} }
rect.w = width; rect.w = width;
recalculate_pos();
} }
void Widget::set_height(int height, bool preserve_aspect_ratio) { void Widget::set_height(int height, bool preserve_aspect_ratio) {
if (preserve_aspect_ratio) { if (preserve_aspect_ratio) {
float scale = ((float)height) / rect.h; float scale = ((float)height) / rect.h;
rect.w = round(rect.w * scale); rect.w = round(rect.w * scale);
recalculate_pos();
} }
rect.h = height; 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) { void Widget::set_position(int x, int y) {
pos.x = x; pos.x = x;
pos.y = y; pos.y = y;
recalculate_rect(); recalculate_pos();
} }
void Widget::set_alignment(Alignment align) { void Widget::set_alignment(Alignment align) {
pos.align = align; pos.align = align;
recalculate_rect(); recalculate_pos();
} }
int Widget::get_width() { return rect.w; } 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; } const PositionInfo& Widget::get_position() { return pos; }
void Widget::recalculate_rect() { void Widget::recalculate_pos() {
int x = pos.x; int x = pos.x;
int y = pos.y; int y = pos.y;
switch (pos.align) { switch (pos.align) {