Make backgrounds preserve aspect ratio when used with different aspect ratio themes

(e.g. a 16:9 theme would not stretch a 4:3 bg and instead have a letterboxing effect. A 4:3 theme using a 16:9 BG will not stretch the BG but instead center it, making it look like the BG is 4:3 all along.)
This commit is contained in:
Crystalwarrior 2020-03-26 14:41:56 +03:00
parent f668d70ac5
commit 1634db7864
3 changed files with 45 additions and 9 deletions

View File

@ -17,12 +17,23 @@ public:
void set_image(QString p_image); void set_image(QString p_image);
void set_legacy_desk(QString p_image); void set_legacy_desk(QString p_image);
//Move the label itself around
void move(int ax, int ay);
//This is somewhat pointless now as there's no "QMovie" object to resize, aka no "combo" to speak of
void combo_resize(int w, int h);
private: private:
QWidget *m_parent; QWidget *m_parent;
QMovie *m_movie; QMovie *m_movie;
AOApplication *ao_app; AOApplication *ao_app;
QString last_image; QString last_image;
// These are the X and Y values before they are fixed based on the sprite's width.
int x = 0;
int y = 0;
// These are the width and height values before they are fixed based on the sprite's width.
int f_w = 0;
int f_h = 0;
}; };
#endif // AOSCENE_H #endif // AOSCENE_H

View File

@ -26,25 +26,35 @@ void AOScene::set_image(QString p_image)
if (file_exists(background_path) && background_path == last_image) if (file_exists(background_path) && background_path == last_image)
return; return;
int w = this->width();
int h = this->height();
this->clear(); this->clear();
this->setMovie(nullptr); this->setMovie(nullptr);
m_movie->stop(); m_movie->stop();
m_movie->setFileName(background_path); m_movie->setFileName(background_path);
m_movie->setScaledSize(QSize(w, h));
if (m_movie->isValid() && m_movie->frameCount() > 1) if (m_movie->isValid() && m_movie->frameCount() > 1)
{ {
float scale_factor = f_h / m_movie->frameRect().height();
//preserve aspect ratio
int n_w = static_cast<int>(static_cast<float>(m_movie->frameRect().width()) * scale_factor);
int n_h = static_cast<int>(static_cast<float>(m_movie->frameRect().height()) * scale_factor);
m_movie->setScaledSize(QSize(n_w, n_h));
this->resize(m_movie->scaledSize());
this->setMovie(m_movie); this->setMovie(m_movie);
QLabel::move(x + (f_w - n_w)/2, y + (f_h - n_h)); //Always center horizontally, always put at the bottom vertically
m_movie->start(); m_movie->start();
} }
else else
{ {
QPixmap background(background_path); QPixmap background(background_path);
this->setPixmap(background.scaled(w, h)); auto transform_mode = Qt::FastTransformation;
if (background.height() > f_h) //We are downscaling, use anti-aliasing.
transform_mode = Qt::SmoothTransformation;
background = background.scaledToHeight(f_h, transform_mode);
this->resize(background.size());
this->setPixmap(background);
QLabel::move(x + (f_w - background.width())/2, y + (f_h - background.height())/2); //Always center horizontally, always center vertically
} }
last_image = background_path; last_image = background_path;
} }
@ -92,8 +102,23 @@ void AOScene::set_legacy_desk(QString p_image)
} }
else else
{ {
this->resize(vp_width, final_h); this->resize(vp_width, final_h);
this->setPixmap(f_desk.scaled(vp_width, final_h)); this->setPixmap(f_desk.scaled(vp_width, final_h));
} }
last_image = desk_path; last_image = desk_path;
} }
void AOScene::combo_resize(int w, int h)
{
QSize f_size(w, h);
f_w = w;
f_h = h;
this->resize(f_size);
}
void AOScene::move(int ax, int ay)
{
x = ax;
y = ay;
QLabel::move(x, y);
}

View File

@ -487,7 +487,7 @@ void Courtroom::set_widgets()
ui_settings->show(); ui_settings->show();
ui_vp_background->move(0, 0); ui_vp_background->move(0, 0);
ui_vp_background->resize(ui_viewport->width(), ui_viewport->height()); ui_vp_background->combo_resize(ui_viewport->width(), ui_viewport->height());
ui_vp_speedlines->move(0, 0); ui_vp_speedlines->move(0, 0);
ui_vp_speedlines->combo_resize(ui_viewport->width(), ui_viewport->height()); ui_vp_speedlines->combo_resize(ui_viewport->width(), ui_viewport->height());
@ -500,7 +500,7 @@ void Courtroom::set_widgets()
//the AO2 desk element //the AO2 desk element
ui_vp_desk->move(0, 0); ui_vp_desk->move(0, 0);
ui_vp_desk->resize(ui_viewport->width(), ui_viewport->height()); ui_vp_desk->combo_resize(ui_viewport->width(), ui_viewport->height());
//the size of the ui_vp_legacy_desk element relies on various factors and is set in set_scene() //the size of the ui_vp_legacy_desk element relies on various factors and is set in set_scene()