From ba59d40760e2a5a11bfbe2f7cf6b0e2a71b590d7 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Fri, 7 Mar 2025 14:33:11 -0500 Subject: Better high-DPI handling for rest of app --- src/area_popup.cpp | 68 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 25 deletions(-) (limited to 'src/area_popup.cpp') diff --git a/src/area_popup.cpp b/src/area_popup.cpp index 467a2bc..aabf20b 100644 --- a/src/area_popup.cpp +++ b/src/area_popup.cpp @@ -15,20 +15,16 @@ AreaPopup::AreaPopup(wxWindow* parent, int area_id) : wxScrolledCanvas(parent, wxID_ANY), area_id_(area_id) { SetBackgroundStyle(wxBG_STYLE_PAINT); - unchecked_eye_ = - wxBitmap(wxImage(GetAbsolutePath("assets/unchecked.png").c_str(), - wxBITMAP_TYPE_PNG) - .Scale(32, 32)); - checked_eye_ = wxBitmap( - wxImage(GetAbsolutePath("assets/checked.png").c_str(), wxBITMAP_TYPE_PNG) - .Scale(32, 32)); + LoadIcons(); + // TODO: This is slow on high-DPI screens. SetScrollRate(5, 5); SetBackgroundColour(*wxBLACK); Hide(); Bind(wxEVT_PAINT, &AreaPopup::OnPaint, this); + Bind(wxEVT_DPI_CHANGED, &AreaPopup::OnDPIChanged, this); UpdateIndicators(); } @@ -36,15 +32,17 @@ AreaPopup::AreaPopup(wxWindow* parent, int area_id) void AreaPopup::UpdateIndicators() { const MapArea& map_area = GD_GetMapArea(area_id_); + wxFont the_font = GetFont().Scale(GetDPIScaleFactor()); + // Start calculating extents. wxMemoryDC mem_dc; - mem_dc.SetFont(GetFont().Bold()); + mem_dc.SetFont(the_font.Bold()); wxSize header_extent = mem_dc.GetTextExtent(map_area.name); - int acc_height = header_extent.GetHeight() + 20; + int acc_height = header_extent.GetHeight() + FromDIP(20); int col_width = 0; - mem_dc.SetFont(GetFont()); + mem_dc.SetFont(the_font); TrackerPanel* tracker_panel = dynamic_cast(GetParent()); @@ -68,7 +66,8 @@ void AreaPopup::UpdateIndicators() { real_locations.push_back(section_id); wxSize item_extent = mem_dc.GetTextExtent(location.name); - int item_height = std::max(32, item_extent.GetHeight()) + 10; + int item_height = + std::max(FromDIP(32), item_extent.GetHeight()) + FromDIP(10); acc_height += item_height; if (item_extent.GetWidth() > col_width) { @@ -80,7 +79,8 @@ void AreaPopup::UpdateIndicators() { for (int painting_id : map_area.paintings) { const PaintingExit& painting = GD_GetPaintingExit(painting_id); wxSize item_extent = mem_dc.GetTextExtent(painting.display_name); - int item_height = std::max(32, item_extent.GetHeight()) + 10; + int item_height = + std::max(FromDIP(32), item_extent.GetHeight()) + FromDIP(10); acc_height += item_height; if (item_extent.GetWidth() > col_width) { @@ -89,8 +89,8 @@ void AreaPopup::UpdateIndicators() { } } - int item_width = col_width + 10 + 32; - int full_width = std::max(header_extent.GetWidth(), item_width) + 20; + int item_width = col_width + FromDIP(10 + 32); + int full_width = std::max(header_extent.GetWidth(), item_width) + FromDIP(20); Fit(); SetVirtualSize(full_width, acc_height); @@ -101,14 +101,14 @@ void AreaPopup::UpdateIndicators() { mem_dc.SetBrush(*wxBLACK_BRUSH); mem_dc.DrawRectangle({0, 0}, {full_width, acc_height}); - mem_dc.SetFont(GetFont().Bold()); + mem_dc.SetFont(the_font.Bold()); mem_dc.SetTextForeground(*wxWHITE); mem_dc.DrawText(map_area.name, - {(full_width - header_extent.GetWidth()) / 2, 10}); + {(full_width - header_extent.GetWidth()) / 2, FromDIP(10)}); - int cur_height = header_extent.GetHeight() + 20; + int cur_height = header_extent.GetHeight() + FromDIP(20); - mem_dc.SetFont(GetFont()); + mem_dc.SetFont(the_font); for (int section_id : real_locations) { const Location& location = map_area.locations.at(section_id); @@ -131,7 +131,7 @@ void AreaPopup::UpdateIndicators() { wxBitmap* eye_ptr = checked ? &checked_eye_ : &unchecked_eye_; - mem_dc.DrawBitmap(*eye_ptr, {10, cur_height}); + mem_dc.DrawBitmap(*eye_ptr, {FromDIP(10), cur_height}); bool reachable = IsLocationReachable(location.ap_location_id); const wxColour* text_color = reachable ? wxWHITE : wxRED; @@ -140,9 +140,10 @@ void AreaPopup::UpdateIndicators() { wxSize item_extent = mem_dc.GetTextExtent(location.name); mem_dc.DrawText( location.name, - {10 + 32 + 10, cur_height + (32 - mem_dc.GetFontMetrics().height) / 2}); + {FromDIP(10 + 32 + 10), + cur_height + (FromDIP(32) - mem_dc.GetFontMetrics().height) / 2}); - cur_height += 10 + 32; + cur_height += FromDIP(10 + 32); } if (AP_IsPaintingShuffle() && !tracker_panel->IsPanelsMode()) { @@ -155,14 +156,14 @@ void AreaPopup::UpdateIndicators() { bool checked = reachable && AP_IsPaintingChecked(painting.internal_id); wxBitmap* eye_ptr = checked ? &checked_eye_ : &unchecked_eye_; - mem_dc.DrawBitmap(*eye_ptr, {10, cur_height}); + mem_dc.DrawBitmap(*eye_ptr, {FromDIP(10), cur_height}); wxSize item_extent = mem_dc.GetTextExtent(painting.display_name); mem_dc.DrawText(painting.display_name, - {10 + 32 + 10, - cur_height + (32 - mem_dc.GetFontMetrics().height) / 2}); + {FromDIP(10 + 32 + 10), + cur_height + (FromDIP(32) - mem_dc.GetFontMetrics().height) / 2}); - cur_height += 10 + 32; + cur_height += FromDIP(10 + 32); } } } @@ -174,3 +175,20 @@ void AreaPopup::OnPaint(wxPaintEvent& event) { event.Skip(); } + +void AreaPopup::OnDPIChanged(wxDPIChangedEvent& event) { + LoadIcons(); + UpdateIndicators(); +} + +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))); +} -- cgit 1.4.1