From 6e0f691a96fb54d99cc265088d2d8f389519af0a Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Fri, 7 Mar 2025 14:52:13 -0500 Subject: Share icons across popups --- src/area_popup.cpp | 18 +++++++----------- src/area_popup.h | 5 +++-- src/icons.cpp | 22 ++++++++++++++++++++++ src/icons.h | 25 +++++++++++++++++++++++++ src/report_popup.cpp | 16 ++++++---------- src/report_popup.h | 5 +++-- src/tracker_frame.cpp | 2 ++ src/tracker_frame.h | 3 +++ 8 files changed, 71 insertions(+), 25 deletions(-) create mode 100644 src/icons.cpp create mode 100644 src/icons.h (limited to 'src') diff --git a/src/area_popup.cpp b/src/area_popup.cpp index aabf20b..9cc1208 100644 --- a/src/area_popup.cpp +++ b/src/area_popup.cpp @@ -7,6 +7,7 @@ #include "ap_state.h" #include "game_data.h" #include "global.h" +#include "icons.h" #include "tracker_config.h" #include "tracker_panel.h" #include "tracker_state.h" @@ -129,7 +130,7 @@ void AreaPopup::UpdateIndicators() { (location.hunt && AP_HasCheckedHuntPanel(location.ap_location_id)); } - wxBitmap* eye_ptr = checked ? &checked_eye_ : &unchecked_eye_; + const wxBitmap* eye_ptr = checked ? checked_eye_ : unchecked_eye_; mem_dc.DrawBitmap(*eye_ptr, {FromDIP(10), cur_height}); @@ -155,7 +156,7 @@ void AreaPopup::UpdateIndicators() { mem_dc.SetTextForeground(*text_color); bool checked = reachable && AP_IsPaintingChecked(painting.internal_id); - wxBitmap* eye_ptr = checked ? &checked_eye_ : &unchecked_eye_; + const wxBitmap* eye_ptr = checked ? checked_eye_ : unchecked_eye_; mem_dc.DrawBitmap(*eye_ptr, {FromDIP(10), cur_height}); wxSize item_extent = mem_dc.GetTextExtent(painting.display_name); @@ -182,13 +183,8 @@ void AreaPopup::OnDPIChanged(wxDPIChangedEvent& event) { } void AreaPopup::LoadIcons() { - // TODO: We do not have to read these in and scale them for every single - // popup. - unchecked_eye_ = - wxBitmap(wxImage(GetAbsolutePath("assets/unchecked.png").c_str(), - wxBITMAP_TYPE_PNG) - .Scale(FromDIP(32), FromDIP(32))); - checked_eye_ = wxBitmap( - wxImage(GetAbsolutePath("assets/checked.png").c_str(), wxBITMAP_TYPE_PNG) - .Scale(FromDIP(32), FromDIP(32))); + unchecked_eye_ = GetTheIconCache().GetIcon("assets/unchecked.png", + FromDIP(wxSize{32, 32})); + checked_eye_ = + GetTheIconCache().GetIcon("assets/checked.png", FromDIP(wxSize{32, 32})); } diff --git a/src/area_popup.h b/src/area_popup.h index 3326406..d2d50b9 100644 --- a/src/area_popup.h +++ b/src/area_popup.h @@ -21,8 +21,9 @@ class AreaPopup : public wxScrolledCanvas { int area_id_; - wxBitmap unchecked_eye_; - wxBitmap checked_eye_; + const wxBitmap* unchecked_eye_; + const wxBitmap* checked_eye_; + wxBitmap rendered_; }; diff --git a/src/icons.cpp b/src/icons.cpp new file mode 100644 index 0000000..87ba037 --- /dev/null +++ b/src/icons.cpp @@ -0,0 +1,22 @@ +#include "icons.h" + +#include "global.h" + +const wxBitmap* IconCache::GetIcon(const std::string& filename, wxSize size) { + std::tuple key = {filename, size.x, size.y}; + + if (!icons_.count(key)) { + icons_[key] = + wxBitmap(wxImage(GetAbsolutePath(filename).c_str(), + wxBITMAP_TYPE_PNG) + .Scale(size.x, size.y)); + } + + return &icons_.at(key); +} + +static IconCache* ICON_CACHE_INSTANCE = nullptr; + +void SetTheIconCache(IconCache* instance) { ICON_CACHE_INSTANCE = instance; } + +IconCache& GetTheIconCache() { return *ICON_CACHE_INSTANCE; } diff --git a/src/icons.h b/src/icons.h new file mode 100644 index 0000000..23dca2a --- /dev/null +++ b/src/icons.h @@ -0,0 +1,25 @@ +#ifndef ICONS_H_B95159A6 +#define ICONS_H_B95159A6 + +#include + +#ifndef WX_PRECOMP +#include +#endif + +#include +#include +#include + +class IconCache { + public: + const wxBitmap* GetIcon(const std::string& filename, wxSize size); + + private: + std::map, wxBitmap> icons_; +}; + +void SetTheIconCache(IconCache* instance); +IconCache& GetTheIconCache(); + +#endif /* end of include guard: ICONS_H_B95159A6 */ diff --git a/src/report_popup.cpp b/src/report_popup.cpp index 74216c3..cf5b086 100644 --- a/src/report_popup.cpp +++ b/src/report_popup.cpp @@ -6,6 +6,7 @@ #include #include "global.h" +#include "icons.h" #include "tracker_state.h" ReportPopup::ReportPopup(wxWindow* parent) @@ -73,7 +74,7 @@ void ReportPopup::UpdateIndicators() { int cur_height = FromDIP(10); for (const auto& [text, obtained] : report) { - wxBitmap* eye_ptr = obtained ? &checked_eye_ : &unchecked_eye_; + const wxBitmap* eye_ptr = obtained ? checked_eye_ : unchecked_eye_; mem_dc.DrawBitmap(*eye_ptr, wxPoint{FromDIP(10), cur_height}); @@ -104,13 +105,8 @@ void ReportPopup::OnDPIChanged(wxDPIChangedEvent& event) { } void ReportPopup::LoadIcons() { - // TODO: We do not have to read these in and scale them for every single - // popup. - unchecked_eye_ = - wxBitmap(wxImage(GetAbsolutePath("assets/unchecked.png").c_str(), - wxBITMAP_TYPE_PNG) - .Scale(FromDIP(32), FromDIP(32))); - checked_eye_ = wxBitmap( - wxImage(GetAbsolutePath("assets/checked.png").c_str(), wxBITMAP_TYPE_PNG) - .Scale(FromDIP(32), FromDIP(32))); + unchecked_eye_ = GetTheIconCache().GetIcon("assets/unchecked.png", + FromDIP(wxSize{32, 32})); + checked_eye_ = + GetTheIconCache().GetIcon("assets/checked.png", FromDIP(wxSize{32, 32})); } diff --git a/src/report_popup.h b/src/report_popup.h index 4ccc913..2da9b73 100644 --- a/src/report_popup.h +++ b/src/report_popup.h @@ -25,8 +25,9 @@ class ReportPopup : public wxScrolledCanvas { int door_id_ = -1; - wxBitmap unchecked_eye_; - wxBitmap checked_eye_; + const wxBitmap* unchecked_eye_; + const wxBitmap* checked_eye_; + wxBitmap rendered_; }; diff --git a/src/tracker_frame.cpp b/src/tracker_frame.cpp index abc413d..a1bd23e 100644 --- a/src/tracker_frame.cpp +++ b/src/tracker_frame.cpp @@ -63,6 +63,8 @@ TrackerFrame::TrackerFrame() AP_SetTrackerFrame(this); IPC_SetTrackerFrame(this); + SetTheIconCache(&icons_); + updater_ = std::make_unique(this); updater_->Cleanup(); diff --git a/src/tracker_frame.h b/src/tracker_frame.h index 76c071f..9aff0a5 100644 --- a/src/tracker_frame.h +++ b/src/tracker_frame.h @@ -9,6 +9,7 @@ #include +#include "icons.h" #include "updater.h" class AchievementsPane; @@ -94,6 +95,8 @@ class TrackerFrame : public wxFrame { wxMenuItem *zoom_in_menu_item_; wxMenuItem *zoom_out_menu_item_; + + IconCache icons_; }; #endif /* end of include guard: TRACKER_FRAME_H_86BD8DFB */ -- cgit 1.4.1