From 1634db78641526ddb88f844a46b1bdc615a89e59 Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Thu, 26 Mar 2020 14:41:56 +0300 Subject: [PATCH] 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.) --- include/aoscene.h | 11 +++++++++++ src/aoscene.cpp | 39 ++++++++++++++++++++++++++++++++------- src/courtroom.cpp | 4 ++-- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/include/aoscene.h b/include/aoscene.h index ddbefe0..46d8c3b 100644 --- a/include/aoscene.h +++ b/include/aoscene.h @@ -17,12 +17,23 @@ public: void set_image(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: QWidget *m_parent; QMovie *m_movie; AOApplication *ao_app; 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 diff --git a/src/aoscene.cpp b/src/aoscene.cpp index 32e5c35..45eadd5 100644 --- a/src/aoscene.cpp +++ b/src/aoscene.cpp @@ -26,25 +26,35 @@ void AOScene::set_image(QString p_image) if (file_exists(background_path) && background_path == last_image) return; - int w = this->width(); - int h = this->height(); - this->clear(); this->setMovie(nullptr); m_movie->stop(); m_movie->setFileName(background_path); - m_movie->setScaledSize(QSize(w, h)); 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(static_cast(m_movie->frameRect().width()) * scale_factor); + int n_h = static_cast(static_cast(m_movie->frameRect().height()) * scale_factor); + m_movie->setScaledSize(QSize(n_w, n_h)); + this->resize(m_movie->scaledSize()); 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(); } else { 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; } @@ -92,8 +102,23 @@ void AOScene::set_legacy_desk(QString p_image) } else { - this->resize(vp_width, final_h); - this->setPixmap(f_desk.scaled(vp_width, final_h)); + this->resize(vp_width, final_h); + this->setPixmap(f_desk.scaled(vp_width, final_h)); } 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); +} diff --git a/src/courtroom.cpp b/src/courtroom.cpp index 8cf5751..dbe5323 100644 --- a/src/courtroom.cpp +++ b/src/courtroom.cpp @@ -487,7 +487,7 @@ void Courtroom::set_widgets() ui_settings->show(); 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->combo_resize(ui_viewport->width(), ui_viewport->height()); @@ -500,7 +500,7 @@ void Courtroom::set_widgets() //the AO2 desk element 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()