From 0a3c0c6882db2976c2b6fdbebbc127747ed63703 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Fri, 19 Jan 2024 18:16:41 -0500 Subject: Area popups are now painted Instead of being a bunch of controls. This fixes the problem with the window being slow to drag around, and with items in lists disappearing. Overall W for me. --- src/area_popup.cpp | 118 +++++++++++++++++++++++++++++++++----------------- src/area_popup.h | 11 ++--- src/eye_indicator.cpp | 51 ---------------------- src/eye_indicator.h | 30 ------------- src/tracker_panel.cpp | 10 +++-- 5 files changed, 91 insertions(+), 129 deletions(-) delete mode 100644 src/eye_indicator.cpp delete mode 100644 src/eye_indicator.h (limited to 'src') diff --git a/src/area_popup.cpp b/src/area_popup.cpp index 6bbc0cf..8f694bb 100644 --- a/src/area_popup.cpp +++ b/src/area_popup.cpp @@ -2,73 +2,113 @@ #include "ap_state.h" #include "game_data.h" +#include "global.h" #include "tracker_config.h" #include "tracker_state.h" AreaPopup::AreaPopup(wxWindow* parent, int area_id) - : wxScrolledWindow(parent, wxID_ANY), area_id_(area_id) { - const MapArea& map_area = GD_GetMapArea(area_id); - - wxFlexGridSizer* section_sizer = new wxFlexGridSizer(2, 10, 10); - - for (const Location& location : map_area.locations) { - EyeIndicator* eye_indicator = new EyeIndicator(this); - section_sizer->Add(eye_indicator, wxSizerFlags().Expand()); - eye_indicators_.push_back(eye_indicator); - - wxStaticText* section_label = new wxStaticText(this, -1, location.name); - section_label->SetForegroundColour(*wxWHITE); - section_sizer->Add( - section_label, - wxSizerFlags().Align(wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL)); - section_labels_.push_back(section_label); - } - - wxBoxSizer* top_sizer = new wxBoxSizer(wxVERTICAL); - - wxStaticText* top_label = new wxStaticText(this, -1, map_area.name); - top_label->SetForegroundColour(*wxWHITE); - top_label->SetFont(top_label->GetFont().Bold()); - top_sizer->Add(top_label, - wxSizerFlags().Center().DoubleBorder(wxUP | wxLEFT | wxRIGHT)); - - top_sizer->Add(section_sizer, wxSizerFlags().DoubleBorder(wxALL).Expand()); + : wxScrolledCanvas(parent, wxID_ANY), area_id_(area_id) { + 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)); - SetSizerAndFit(top_sizer); SetScrollRate(5, 5); SetBackgroundColour(*wxBLACK); Hide(); + + Bind(wxEVT_PAINT, &AreaPopup::OnPaint, this); + + UpdateIndicators(); } void AreaPopup::UpdateIndicators() { const MapArea& map_area = GD_GetMapArea(area_id_); + + // Start calculating extents. + wxMemoryDC mem_dc; + mem_dc.SetFont(GetFont().Bold()); + wxSize header_extent = mem_dc.GetTextExtent(map_area.name); + + int acc_height = header_extent.GetHeight() + 20; + int col_width = 0; + + mem_dc.SetFont(GetFont()); + + std::vector real_locations; + for (int section_id = 0; section_id < map_area.locations.size(); section_id++) { const Location& location = map_area.locations.at(section_id); - wxSizer* container_sizer = - section_labels_[section_id]->GetContainingSizer(); if (!AP_IsLocationVisible(location.classification) && !(location.hunt && GetTrackerConfig().show_hunt_panels)) { - container_sizer->Hide(section_labels_[section_id]); - container_sizer->Hide(eye_indicators_[section_id]); continue; - } else { - container_sizer->Show(section_labels_[section_id]); - container_sizer->Show(eye_indicators_[section_id]); } + real_locations.push_back(section_id); + + wxSize item_extent = mem_dc.GetTextExtent(location.name); + int item_height = std::max(32, item_extent.GetHeight()) + 10; + acc_height += item_height; + + if (item_extent.GetWidth() > col_width) { + col_width = item_extent.GetWidth(); + } + } + + int item_width = col_width + 10 + 32; + int full_width = std::max(header_extent.GetWidth(), item_width) + 20; + + SetVirtualSize(full_width, acc_height); + + rendered_ = wxBitmap(full_width, acc_height); + mem_dc.SelectObject(rendered_); + mem_dc.SetPen(*wxTRANSPARENT_PEN); + mem_dc.SetBrush(*wxBLACK_BRUSH); + mem_dc.DrawRectangle({0, 0}, {full_width, acc_height}); + + mem_dc.SetFont(GetFont().Bold()); + mem_dc.SetTextForeground(*wxWHITE); + mem_dc.DrawText(map_area.name, + {(full_width - header_extent.GetWidth()) / 2, 10}); + + int cur_height = header_extent.GetHeight() + 20; + + mem_dc.SetFont(GetFont()); + + for (int section_id : real_locations) { + const Location& location = map_area.locations.at(section_id); + bool checked = AP_HasCheckedGameLocation(location.ap_location_id) || (location.hunt && AP_HasCheckedHuntPanel(location.ap_location_id)); + + wxBitmap* eye_ptr = checked ? &checked_eye_ : &unchecked_eye_; + + mem_dc.DrawBitmap(*eye_ptr, {10, cur_height}); + bool reachable = IsLocationReachable(location.ap_location_id); const wxColour* text_color = reachable ? wxWHITE : wxRED; + mem_dc.SetTextForeground(*text_color); - section_labels_[section_id]->SetForegroundColour(*text_color); - eye_indicators_[section_id]->SetChecked(checked); + wxSize item_extent = mem_dc.GetTextExtent(location.name); + mem_dc.DrawText( + location.name, + {10 + 32 + 10, cur_height + (32 - mem_dc.GetFontMetrics().height) / 2}); + + cur_height += 10 + 32; } +} + +void AreaPopup::OnPaint(wxPaintEvent& event) { + wxPaintDC dc(this); + PrepareDC(dc); + dc.DrawBitmap(rendered_, 0, 0); - section_labels_[0]->GetContainingSizer()->Layout(); - GetSizer()->Fit(this); + event.Skip(); } diff --git a/src/area_popup.h b/src/area_popup.h index d5f6a50..00c644d 100644 --- a/src/area_popup.h +++ b/src/area_popup.h @@ -7,19 +7,20 @@ #include #endif -#include "eye_indicator.h" - -class AreaPopup : public wxScrolledWindow { +class AreaPopup : public wxScrolledCanvas { public: AreaPopup(wxWindow* parent, int area_id); void UpdateIndicators(); private: + void OnPaint(wxPaintEvent& event); + int area_id_; - std::vector section_labels_; - std::vector eye_indicators_; + wxBitmap unchecked_eye_; + wxBitmap checked_eye_; + wxBitmap rendered_; }; #endif /* end of include guard: AREA_POPUP_H_03FAC988 */ diff --git a/src/eye_indicator.cpp b/src/eye_indicator.cpp deleted file mode 100644 index 61ad780..0000000 --- a/src/eye_indicator.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "eye_indicator.h" - -#include "global.h" - -EyeIndicator::EyeIndicator(wxWindow* parent) : wxWindow(parent, wxID_ANY) { - SetMinSize({32, 32}); - - Redraw(); - - Bind(wxEVT_PAINT, &EyeIndicator::OnPaint, this); -} - -void EyeIndicator::SetChecked(bool checked) { - if (intended_checked_ != checked) { - intended_checked_ = checked; - - Redraw(); - } -} - -const wxImage& EyeIndicator::GetUncheckedImage() { - static wxImage* unchecked_image = new wxImage( - GetAbsolutePath("assets/unchecked.png").c_str(), wxBITMAP_TYPE_PNG); - return *unchecked_image; -} - -const wxImage& EyeIndicator::GetCheckedImage() { - static wxImage* checked_image = new wxImage( - GetAbsolutePath("assets/checked.png").c_str(), wxBITMAP_TYPE_PNG); - return *checked_image; -} - -void EyeIndicator::OnPaint(wxPaintEvent& event) { - if (GetSize() != rendered_.GetSize() || - intended_checked_ != rendered_checked_) { - Redraw(); - } - - wxPaintDC dc(this); - dc.DrawBitmap(rendered_, 0, 0); - - event.Skip(); -} - -void EyeIndicator::Redraw() { - rendered_ = - wxBitmap((intended_checked_ ? GetCheckedImage() : GetUncheckedImage()) - .Scale(GetSize().GetWidth(), GetSize().GetHeight(), - wxIMAGE_QUALITY_NORMAL)); - rendered_checked_ = intended_checked_; -} diff --git a/src/eye_indicator.h b/src/eye_indicator.h deleted file mode 100644 index e8fd890..0000000 --- a/src/eye_indicator.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef EYE_INDICATOR_H_778150F2 -#define EYE_INDICATOR_H_778150F2 - -#include - -#ifndef WX_PRECOMP -#include -#endif - -class EyeIndicator : public wxWindow { - public: - EyeIndicator(wxWindow* parent); - - void SetChecked(bool checked); - - private: - static const wxImage& GetUncheckedImage(); - static const wxImage& GetCheckedImage(); - - void OnPaint(wxPaintEvent& event); - - void Redraw(); - - bool intended_checked_ = false; - - wxBitmap rendered_; - bool rendered_checked_ = false; -}; - -#endif /* end of include guard: EYE_INDICATOR_H_778150F2 */ diff --git a/src/tracker_panel.cpp b/src/tracker_panel.cpp index 81cb595..3102110 100644 --- a/src/tracker_panel.cpp +++ b/src/tracker_panel.cpp @@ -35,11 +35,11 @@ TrackerPanel::TrackerPanel(wxWindow *parent) : wxPanel(parent, wxID_ANY) { } void TrackerPanel::UpdateIndicators() { - Redraw(); - for (AreaIndicator &area : areas_) { area.popup->UpdateIndicators(); } + + Redraw(); } void TrackerPanel::OnPaint(wxPaintEvent &event) { @@ -178,8 +178,10 @@ void TrackerPanel::Redraw() { int popup_y = final_y + map_area.map_y * final_width / image_size.GetWidth(); - area.popup->SetMaxSize(panel_size); - area.popup->GetSizer()->Fit(area.popup); + area.popup->SetClientSize( + area.popup->GetVirtualSize().GetWidth(), + std::min(panel_size.GetHeight(), + area.popup->GetVirtualSize().GetHeight())); if (popup_x + area.popup->GetSize().GetWidth() > panel_size.GetWidth()) { popup_x = panel_size.GetWidth() - area.popup->GetSize().GetWidth(); -- cgit 1.4.1