From bf432f8a3ad08d905dfda7c47218f285cefa8e33 Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Wed, 10 Mar 2021 11:54:09 -0500 Subject: [PATCH] LibGUI: Add opacity to ImageWidget --- Userland/Libraries/LibGUI/ImageWidget.cpp | 14 ++++++++++++-- Userland/Libraries/LibGUI/ImageWidget.h | 5 +++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibGUI/ImageWidget.cpp b/Userland/Libraries/LibGUI/ImageWidget.cpp index 019994224e..deb6e94d68 100644 --- a/Userland/Libraries/LibGUI/ImageWidget.cpp +++ b/Userland/Libraries/LibGUI/ImageWidget.cpp @@ -128,12 +128,22 @@ void ImageWidget::paint_event(PaintEvent& event) } Painter painter(*this); + painter.add_clip_rect(event.rect()); if (m_should_stretch) { - painter.draw_scaled_bitmap(frame_inner_rect(), *m_bitmap, m_bitmap->rect()); + painter.draw_scaled_bitmap(frame_inner_rect(), *m_bitmap, m_bitmap->rect(), (float)opacity_percent() / 100.0f); } else { auto location = frame_inner_rect().center().translated(-(m_bitmap->width() / 2), -(m_bitmap->height() / 2)); - painter.blit(location, *m_bitmap, m_bitmap->rect()); + painter.blit(location, *m_bitmap, m_bitmap->rect(), (float)opacity_percent() / 100.0f); } } + +void ImageWidget::set_opacity_percent(int percent) +{ + if (m_opacity_percent == percent) + return; + m_opacity_percent = percent; + update(); +} + } diff --git a/Userland/Libraries/LibGUI/ImageWidget.h b/Userland/Libraries/LibGUI/ImageWidget.h index 6ee492ed50..8433368639 100644 --- a/Userland/Libraries/LibGUI/ImageWidget.h +++ b/Userland/Libraries/LibGUI/ImageWidget.h @@ -48,6 +48,9 @@ public: void animate(); void load_from_file(const StringView&); + int opacity_percent() const { return m_opacity_percent; } + void set_opacity_percent(int percent); + Function on_click; protected: @@ -65,6 +68,8 @@ private: size_t m_current_frame_index { 0 }; size_t m_loops_completed { 0 }; NonnullRefPtr m_timer; + + int m_opacity_percent { 100 }; }; }