diff options
Diffstat (limited to 'src/tracker_panel.cpp')
-rw-r--r-- | src/tracker_panel.cpp | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/tracker_panel.cpp b/src/tracker_panel.cpp new file mode 100644 index 0000000..0e0569b --- /dev/null +++ b/src/tracker_panel.cpp | |||
@@ -0,0 +1,149 @@ | |||
1 | #include "tracker_panel.h" | ||
2 | |||
3 | #include "ap_state.h" | ||
4 | #include "area_popup.h" | ||
5 | #include "game_data.h" | ||
6 | #include "tracker_state.h" | ||
7 | |||
8 | constexpr int AREA_ACTUAL_SIZE = 64; | ||
9 | constexpr int AREA_BORDER_SIZE = 5; | ||
10 | constexpr int AREA_EFFECTIVE_SIZE = AREA_ACTUAL_SIZE + AREA_BORDER_SIZE * 2; | ||
11 | |||
12 | TrackerPanel::TrackerPanel(wxWindow *parent) : wxPanel(parent, wxID_ANY) { | ||
13 | map_image_ = wxImage("assets/lingo_map.png", wxBITMAP_TYPE_PNG); | ||
14 | if (!map_image_.IsOk()) { | ||
15 | return; | ||
16 | } | ||
17 | |||
18 | for (const MapArea &map_area : GetGameData().GetMapAreas()) { | ||
19 | AreaIndicator area; | ||
20 | area.area_id = map_area.id; | ||
21 | |||
22 | area.popup = new AreaPopup(this, map_area.id); | ||
23 | area.popup->SetPosition({0, 0}); | ||
24 | |||
25 | areas_.push_back(area); | ||
26 | } | ||
27 | |||
28 | Redraw(); | ||
29 | |||
30 | Bind(wxEVT_PAINT, &TrackerPanel::OnPaint, this); | ||
31 | Bind(wxEVT_MOTION, &TrackerPanel::OnMouseMove, this); | ||
32 | } | ||
33 | |||
34 | void TrackerPanel::UpdateIndicators() { | ||
35 | Redraw(); | ||
36 | |||
37 | for (AreaIndicator &area : areas_) { | ||
38 | area.popup->UpdateIndicators(); | ||
39 | } | ||
40 | } | ||
41 | |||
42 | void TrackerPanel::OnPaint(wxPaintEvent &event) { | ||
43 | if (GetSize() != rendered_.GetSize()) { | ||
44 | Redraw(); | ||
45 | } | ||
46 | |||
47 | wxPaintDC dc(this); | ||
48 | dc.DrawBitmap(rendered_, 0, 0); | ||
49 | |||
50 | event.Skip(); | ||
51 | } | ||
52 | |||
53 | void TrackerPanel::OnMouseMove(wxMouseEvent &event) { | ||
54 | for (AreaIndicator &area : areas_) { | ||
55 | if (area.real_x1 <= event.GetX() && event.GetX() < area.real_x2 && | ||
56 | area.real_y1 <= event.GetY() && event.GetY() < area.real_y2) { | ||
57 | area.popup->Show(); | ||
58 | } else { | ||
59 | area.popup->Hide(); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | event.Skip(); | ||
64 | } | ||
65 | |||
66 | void TrackerPanel::Redraw() { | ||
67 | wxSize panel_size = GetSize(); | ||
68 | wxSize image_size = map_image_.GetSize(); | ||
69 | |||
70 | int final_x = 0; | ||
71 | int final_y = 0; | ||
72 | int final_width = panel_size.GetWidth(); | ||
73 | int final_height = panel_size.GetHeight(); | ||
74 | |||
75 | if (image_size.GetWidth() * panel_size.GetHeight() > | ||
76 | panel_size.GetWidth() * image_size.GetHeight()) { | ||
77 | final_height = (panel_size.GetWidth() * image_size.GetHeight()) / | ||
78 | image_size.GetWidth(); | ||
79 | final_y = (panel_size.GetHeight() - final_height) / 2; | ||
80 | } else { | ||
81 | final_width = (image_size.GetWidth() * panel_size.GetHeight()) / | ||
82 | image_size.GetHeight(); | ||
83 | final_x = (panel_size.GetWidth() - final_width) / 2; | ||
84 | } | ||
85 | |||
86 | rendered_ = wxBitmap( | ||
87 | map_image_.Scale(final_width, final_height, wxIMAGE_QUALITY_NORMAL) | ||
88 | .Size(panel_size, {final_x, final_y}, 0, 0, 0)); | ||
89 | |||
90 | wxMemoryDC dc; | ||
91 | dc.SelectObject(rendered_); | ||
92 | |||
93 | for (AreaIndicator &area : areas_) { | ||
94 | const wxBrush *brush_color = wxGREY_BRUSH; | ||
95 | |||
96 | const MapArea &map_area = GetGameData().GetMapArea(area.area_id); | ||
97 | bool has_reachable_unchecked = false; | ||
98 | bool has_unreachable_unchecked = false; | ||
99 | for (int section_id = 0; section_id < map_area.locations.size(); | ||
100 | section_id++) { | ||
101 | if (!AP_HasCheckedGameLocation(area.area_id, section_id)) { | ||
102 | if (GetTrackerState().IsLocationReachable(area.area_id, section_id)) { | ||
103 | has_reachable_unchecked = true; | ||
104 | } else { | ||
105 | has_unreachable_unchecked = true; | ||
106 | } | ||
107 | } | ||
108 | } | ||
109 | |||
110 | if (has_reachable_unchecked && has_unreachable_unchecked) { | ||
111 | brush_color = wxYELLOW_BRUSH; | ||
112 | } else if (has_reachable_unchecked) { | ||
113 | brush_color = wxGREEN_BRUSH; | ||
114 | } else if (has_unreachable_unchecked) { | ||
115 | brush_color = wxRED_BRUSH; | ||
116 | } | ||
117 | |||
118 | int real_area_size = | ||
119 | final_width * AREA_EFFECTIVE_SIZE / image_size.GetWidth(); | ||
120 | int actual_border_size = | ||
121 | real_area_size * AREA_BORDER_SIZE / AREA_EFFECTIVE_SIZE; | ||
122 | int real_area_x = final_x + (map_area.map_x - (AREA_EFFECTIVE_SIZE / 2)) * | ||
123 | final_width / image_size.GetWidth(); | ||
124 | int real_area_y = final_y + (map_area.map_y - (AREA_EFFECTIVE_SIZE / 2)) * | ||
125 | final_width / image_size.GetWidth(); | ||
126 | |||
127 | dc.SetPen(*wxThePenList->FindOrCreatePen(*wxBLACK, actual_border_size)); | ||
128 | dc.SetBrush(*brush_color); | ||
129 | dc.DrawRectangle({real_area_x, real_area_y}, | ||
130 | {real_area_size, real_area_size}); | ||
131 | |||
132 | area.real_x1 = real_area_x; | ||
133 | area.real_x2 = real_area_x + real_area_size; | ||
134 | area.real_y1 = real_area_y; | ||
135 | area.real_y2 = real_area_y + real_area_size; | ||
136 | |||
137 | int popup_x = | ||
138 | final_x + map_area.map_x * final_width / image_size.GetWidth(); | ||
139 | int popup_y = | ||
140 | final_y + map_area.map_y * final_width / image_size.GetWidth(); | ||
141 | if (popup_x + area.popup->GetSize().GetWidth() > panel_size.GetWidth()) { | ||
142 | popup_x = panel_size.GetWidth() - area.popup->GetSize().GetWidth(); | ||
143 | } | ||
144 | if (popup_y + area.popup->GetSize().GetHeight() > panel_size.GetHeight()) { | ||
145 | popup_y = panel_size.GetHeight() - area.popup->GetSize().GetHeight(); | ||
146 | } | ||
147 | area.popup->SetPosition({popup_x, popup_y}); | ||
148 | } | ||
149 | } | ||