Recalculate widget position after changing width
This commit is contained in:
		@ -33,7 +33,7 @@ public:
 | 
			
		||||
  virtual void draw() = 0;
 | 
			
		||||
 | 
			
		||||
protected:
 | 
			
		||||
  void recalculate_rect();
 | 
			
		||||
  void recalculate_pos();
 | 
			
		||||
 | 
			
		||||
  SDL_Renderer* renderer;
 | 
			
		||||
  SDL_Rect rect;
 | 
			
		||||
 | 
			
		||||
@ -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) {
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user