about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/subway_map.cpp37
-rw-r--r--src/subway_map.h10
2 files changed, 47 insertions, 0 deletions
diff --git a/src/subway_map.cpp b/src/subway_map.cpp index 0aa7df3..230a256 100644 --- a/src/subway_map.cpp +++ b/src/subway_map.cpp
@@ -25,6 +25,13 @@ SubwayMap::SubwayMap(wxWindow *parent) : wxPanel(parent, wxID_ANY) {
25 return; 25 return;
26 } 26 }
27 27
28 tree_ = std::make_unique<quadtree::Quadtree<int, GetItemBox>>(
29 quadtree::Box<float>{0, 0, static_cast<float>(map_image_.GetWidth()),
30 static_cast<float>(map_image_.GetHeight())});
31 for (const SubwayItem &subway_item : GD_GetSubwayItems()) {
32 tree_->add(subway_item.id);
33 }
34
28 Redraw(); 35 Redraw();
29 36
30 Bind(wxEVT_PAINT, &SubwayMap::OnPaint, this); 37 Bind(wxEVT_PAINT, &SubwayMap::OnPaint, this);
@@ -47,6 +54,30 @@ void SubwayMap::OnPaint(wxPaintEvent &event) {
47} 54}
48 55
49void SubwayMap::OnMouseMove(wxMouseEvent &event) { 56void SubwayMap::OnMouseMove(wxMouseEvent &event) {
57 int mouse_x = std::clamp(
58 (event.GetX() - render_x_) * map_image_.GetWidth() / render_width_,
59 0, map_image_.GetWidth() - 1);
60 int mouse_y = std::clamp(
61 (event.GetY() - render_y_) * map_image_.GetWidth() / render_width_,
62 0, map_image_.GetHeight() - 1);
63
64 std::vector<int> hovered = tree_->query(
65 {static_cast<float>(mouse_x), static_cast<float>(mouse_y), 2, 2});
66 std::optional<int> new_hovered_item;
67 if (!hovered.empty()) {
68 new_hovered_item = hovered[0];
69 }
70
71 if (new_hovered_item != hovered_item_) {
72 if (new_hovered_item) {
73 wxLogVerbose("Hovered: %d", *new_hovered_item);
74 } else {
75 wxLogVerbose("Un-hovered: %d", *hovered_item_);
76 }
77
78 hovered_item_ = new_hovered_item;
79 }
80
50 event.Skip(); 81 event.Skip();
51} 82}
52 83
@@ -121,3 +152,9 @@ void SubwayMap::Redraw() {
121 } 152 }
122 } 153 }
123} 154}
155
156quadtree::Box<float> SubwayMap::GetItemBox::operator()(const int& id) const {
157 const SubwayItem &subway_item = GD_GetSubwayItem(id);
158 return {static_cast<float>(subway_item.x), static_cast<float>(subway_item.y),
159 AREA_ACTUAL_SIZE, AREA_ACTUAL_SIZE};
160}
diff --git a/src/subway_map.h b/src/subway_map.h index e375750..6cb5c63 100644 --- a/src/subway_map.h +++ b/src/subway_map.h
@@ -7,8 +7,12 @@
7#include <wx/wx.h> 7#include <wx/wx.h>
8#endif 8#endif
9 9
10#include <memory>
11#include <optional>
10#include <vector> 12#include <vector>
11 13
14#include <quadtree/Quadtree.h>
15
12class SubwayMap : public wxPanel { 16class SubwayMap : public wxPanel {
13 public: 17 public:
14 SubwayMap(wxWindow *parent); 18 SubwayMap(wxWindow *parent);
@@ -29,7 +33,13 @@ class SubwayMap : public wxPanel {
29 int render_y_ = 0; 33 int render_y_ = 0;
30 int render_width_ = 0; 34 int render_width_ = 0;
31 int render_height_ = 0; 35 int render_height_ = 0;
36
37 struct GetItemBox {
38 quadtree::Box<float> operator()(const int &id) const;
39 };
32 40
41 std::unique_ptr<quadtree::Quadtree<int, GetItemBox>> tree_;
42 std::optional<int> hovered_item_;
33}; 43};
34 44
35#endif /* end of include guard: SUBWAY_MAP_H_BD2D843E */ 45#endif /* end of include guard: SUBWAY_MAP_H_BD2D843E */