SoundPlayer: Draw album cover with correct aspect ratio

Instead of drawing the album cover scaled to cover the whole
visualization area, draw it resized to fit the area without altering the
aspect ratio.
This commit is contained in:
Nícolas F. R. A. Prado 2022-02-28 22:28:55 -05:00 committed by Brian Gianforcaro
commit 5bafb80255
Notes: sideshowbarker 2024-07-17 18:00:19 +09:00

View file

@ -10,6 +10,7 @@
#include <AK/LexicalPath.h> #include <AK/LexicalPath.h>
#include <LibCore/File.h> #include <LibCore/File.h>
#include <LibGUI/Painter.h> #include <LibGUI/Painter.h>
#include <LibGfx/Rect.h>
void AlbumCoverVisualizationWidget::paint_event(GUI::PaintEvent& event) void AlbumCoverVisualizationWidget::paint_event(GUI::PaintEvent& event)
{ {
@ -17,7 +18,16 @@ void AlbumCoverVisualizationWidget::paint_event(GUI::PaintEvent& event)
GUI::Painter painter(*this); GUI::Painter painter(*this);
if (m_album_cover) { if (m_album_cover) {
painter.draw_scaled_bitmap(frame_inner_rect(), *m_album_cover, m_album_cover->rect(), 1.0f); auto album_cover_rect = m_album_cover->rect();
auto height_ratio = frame_inner_rect().height() / (float)album_cover_rect.height();
auto width_ratio = frame_inner_rect().width() / (float)album_cover_rect.width();
auto scale = min(height_ratio, width_ratio);
Gfx::IntRect fitted_rect = { 0, 0, (int)(album_cover_rect.width() * scale), (int)(album_cover_rect.height() * scale) };
fitted_rect.center_within(frame_inner_rect());
painter.draw_scaled_bitmap(fitted_rect, *m_album_cover, m_album_cover->rect(), 1.0f);
} else { } else {
if (!m_serenity_bg) if (!m_serenity_bg)
m_serenity_bg = Gfx::Bitmap::try_load_from_file("/res/wallpapers/sunset-retro.png").release_value_but_fixme_should_propagate_errors(); m_serenity_bg = Gfx::Bitmap::try_load_from_file("/res/wallpapers/sunset-retro.png").release_value_but_fixme_should_propagate_errors();