diff options
Diffstat (limited to 'src/report_popup.cpp')
| -rw-r--r-- | src/report_popup.cpp | 131 |
1 files changed, 131 insertions, 0 deletions
| diff --git a/src/report_popup.cpp b/src/report_popup.cpp new file mode 100644 index 0000000..703e87f --- /dev/null +++ b/src/report_popup.cpp | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | #include "report_popup.h" | ||
| 2 | |||
| 3 | #include <wx/dcbuffer.h> | ||
| 4 | |||
| 5 | #include <map> | ||
| 6 | #include <string> | ||
| 7 | |||
| 8 | #include "global.h" | ||
| 9 | #include "icons.h" | ||
| 10 | #include "tracker_state.h" | ||
| 11 | |||
| 12 | ReportPopup::ReportPopup(wxWindow* parent) | ||
| 13 | : wxScrolledCanvas(parent, wxID_ANY) { | ||
| 14 | SetBackgroundStyle(wxBG_STYLE_PAINT); | ||
| 15 | |||
| 16 | LoadIcons(); | ||
| 17 | |||
| 18 | // TODO: This is slow on high-DPI screens. | ||
| 19 | SetScrollRate(5, 5); | ||
| 20 | |||
| 21 | SetBackgroundColour(*wxBLACK); | ||
| 22 | Hide(); | ||
| 23 | |||
| 24 | Bind(wxEVT_PAINT, &ReportPopup::OnPaint, this); | ||
| 25 | Bind(wxEVT_DPI_CHANGED, &ReportPopup::OnDPIChanged, this); | ||
| 26 | } | ||
| 27 | |||
| 28 | void ReportPopup::SetDoorId(int door_id) { | ||
| 29 | door_id_ = door_id; | ||
| 30 | |||
| 31 | ResetIndicators(); | ||
| 32 | } | ||
| 33 | |||
| 34 | void ReportPopup::Reset() { | ||
| 35 | door_id_ = -1; | ||
| 36 | } | ||
| 37 | |||
| 38 | void ReportPopup::ResetIndicators() { | ||
| 39 | if (door_id_ == -1) { | ||
| 40 | return; | ||
| 41 | } | ||
| 42 | |||
| 43 | wxFont the_font = GetFont().Scale(GetDPIScaleFactor()); | ||
| 44 | const std::map<std::string, bool>& report = GetDoorRequirements(door_id_); | ||
| 45 | |||
| 46 | wxMemoryDC mem_dc; | ||
| 47 | mem_dc.SetFont(the_font); | ||
| 48 | |||
| 49 | int acc_height = FromDIP(10); | ||
| 50 | int col_width = 0; | ||
| 51 | |||
| 52 | for (const auto& [text, obtained] : report) { | ||
| 53 | wxSize item_extent = mem_dc.GetTextExtent(text); | ||
| 54 | int item_height = | ||
| 55 | std::max(FromDIP(32), item_extent.GetHeight()) + FromDIP(10); | ||
| 56 | acc_height += item_height; | ||
| 57 | |||
| 58 | if (item_extent.GetWidth() > col_width) { | ||
| 59 | col_width = item_extent.GetWidth(); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | int item_width = col_width + FromDIP(10 + 32); | ||
| 64 | full_width_ = item_width + FromDIP(20); | ||
| 65 | full_height_ = acc_height; | ||
| 66 | |||
| 67 | Fit(); | ||
| 68 | SetVirtualSize(full_width_, full_height_); | ||
| 69 | |||
| 70 | UpdateIndicators(); | ||
| 71 | } | ||
| 72 | |||
| 73 | void ReportPopup::UpdateIndicators() { | ||
| 74 | if (door_id_ == -1) { | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | |||
| 78 | wxFont the_font = GetFont().Scale(GetDPIScaleFactor()); | ||
| 79 | const std::map<std::string, bool>& report = GetDoorRequirements(door_id_); | ||
| 80 | |||
| 81 | rendered_ = wxBitmap(full_width_, full_height_); | ||
| 82 | |||
| 83 | wxMemoryDC mem_dc; | ||
| 84 | mem_dc.SelectObject(rendered_); | ||
| 85 | mem_dc.SetPen(*wxTRANSPARENT_PEN); | ||
| 86 | mem_dc.SetBrush(*wxBLACK_BRUSH); | ||
| 87 | mem_dc.DrawRectangle({0, 0}, {full_width_, full_height_}); | ||
| 88 | |||
| 89 | mem_dc.SetFont(the_font); | ||
| 90 | |||
| 91 | int cur_height = FromDIP(10); | ||
| 92 | |||
| 93 | for (const auto& [text, obtained] : report) { | ||
| 94 | const wxBitmap* eye_ptr = obtained ? checked_eye_ : unchecked_eye_; | ||
| 95 | |||
| 96 | mem_dc.DrawBitmap(*eye_ptr, wxPoint{FromDIP(10), cur_height}); | ||
| 97 | |||
| 98 | mem_dc.SetTextForeground(obtained ? *wxWHITE : *wxRED); | ||
| 99 | wxSize item_extent = mem_dc.GetTextExtent(text); | ||
| 100 | mem_dc.DrawText( | ||
| 101 | text, wxPoint{FromDIP(10 + 32 + 10), | ||
| 102 | cur_height + | ||
| 103 | (FromDIP(32) - mem_dc.GetFontMetrics().height) / 2}); | ||
| 104 | |||
| 105 | cur_height += FromDIP(10 + 32); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | void ReportPopup::OnPaint(wxPaintEvent& event) { | ||
| 110 | if (door_id_ != -1) { | ||
| 111 | wxBufferedPaintDC dc(this); | ||
| 112 | PrepareDC(dc); | ||
| 113 | dc.DrawBitmap(rendered_, 0, 0); | ||
| 114 | } | ||
| 115 | |||
| 116 | event.Skip(); | ||
| 117 | } | ||
| 118 | |||
| 119 | void ReportPopup::OnDPIChanged(wxDPIChangedEvent& event) { | ||
| 120 | LoadIcons(); | ||
| 121 | ResetIndicators(); | ||
| 122 | |||
| 123 | event.Skip(); | ||
| 124 | } | ||
| 125 | |||
| 126 | void ReportPopup::LoadIcons() { | ||
| 127 | unchecked_eye_ = GetTheIconCache().GetIcon("assets/unchecked.png", | ||
| 128 | FromDIP(wxSize{32, 32})); | ||
| 129 | checked_eye_ = | ||
| 130 | GetTheIconCache().GetIcon("assets/checked.png", FromDIP(wxSize{32, 32})); | ||
| 131 | } | ||
