diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2024-05-12 18:45:21 -0400 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2024-05-12 18:45:21 -0400 |
commit | e3fbcc9f29a1b1c83b23a4cef3819631fd3117d0 (patch) | |
tree | ececdc789eda39a9d07da037a839a9906591864e /src/subway_map.cpp | |
parent | 00658b38bd65e2cb81a502bd72e98ad9c411a7b4 (diff) | |
download | lingo-ap-tracker-e3fbcc9f29a1b1c83b23a4cef3819631fd3117d0.tar.gz lingo-ap-tracker-e3fbcc9f29a1b1c83b23a4cef3819631fd3117d0.tar.bz2 lingo-ap-tracker-e3fbcc9f29a1b1c83b23a4cef3819631fd3117d0.zip |
Subway map hover detection with a quadtree
Diffstat (limited to 'src/subway_map.cpp')
-rw-r--r-- | src/subway_map.cpp | 37 |
1 files changed, 37 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 | ||
49 | void SubwayMap::OnMouseMove(wxMouseEvent &event) { | 56 | void 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 | |||
156 | quadtree::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 | } | ||