From eaa21c53b96b945d8809dc5f4a9353ecaaacc266 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sun, 19 May 2024 11:48:34 -0400 Subject: Zoom slider --- src/subway_map.cpp | 31 ++++++++++++++++++++++++------- src/subway_map.h | 4 ++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/subway_map.cpp b/src/subway_map.cpp index 47ebfdf..98d544c 100644 --- a/src/subway_map.cpp +++ b/src/subway_map.cpp @@ -54,6 +54,9 @@ SubwayMap::SubwayMap(wxWindow *parent) : wxPanel(parent, wxID_ANY) { Bind(wxEVT_MOUSEWHEEL, &SubwayMap::OnMouseScroll, this); Bind(wxEVT_LEAVE_WINDOW, &SubwayMap::OnMouseLeave, this); Bind(wxEVT_TIMER, &SubwayMap::OnTimer, this); + + zoom_slider_ = new wxSlider(this, wxID_ANY, 0, 0, 8, {15, 15}); + zoom_slider_->Bind(wxEVT_SLIDER, &SubwayMap::OnZoomSlide, this); } void SubwayMap::OnConnect() { @@ -381,13 +384,7 @@ void SubwayMap::OnMouseScroll(wxMouseEvent &event) { } if (zoom_ != new_zoom) { - wxPoint map_pos = RenderPosToMapPos(event.GetPosition()); - zoom_ = new_zoom; - - wxPoint virtual_pos = MapPosToVirtualPos(map_pos); - SetZoomPos(-(virtual_pos - event.GetPosition())); - - Refresh(); + SetZoom(new_zoom, event.GetPosition()); } event.Skip(); @@ -400,6 +397,14 @@ void SubwayMap::OnTimer(wxTimerEvent &event) { Refresh(); } +void SubwayMap::OnZoomSlide(wxCommandEvent &event) { + double new_zoom = 1.0 + 0.25 * zoom_slider_->GetValue(); + + if (new_zoom != zoom_) { + SetZoom(new_zoom, {GetSize().GetWidth() / 2, GetSize().GetHeight() / 2}); + } +} + void SubwayMap::Redraw() { rendered_ = wxBitmap(map_image_); @@ -530,6 +535,18 @@ void SubwayMap::SetScrollSpeed(int scroll_x, int scroll_y) { scroll_y_ = scroll_y; } +void SubwayMap::SetZoom(double zoom, wxPoint static_point) { + wxPoint map_pos = RenderPosToMapPos(static_point); + zoom_ = zoom; + + wxPoint virtual_pos = MapPosToVirtualPos(map_pos); + SetZoomPos(-(virtual_pos - static_point)); + + Refresh(); + + zoom_slider_->SetValue((zoom - 1.0) / 0.25); +} + quadtree::Box SubwayMap::GetItemBox::operator()(const int &id) const { const SubwayItem &subway_item = GD_GetSubwayItem(id); return {static_cast(subway_item.x), static_cast(subway_item.y), diff --git a/src/subway_map.h b/src/subway_map.h index 9b0b43f..e891058 100644 --- a/src/subway_map.h +++ b/src/subway_map.h @@ -32,6 +32,7 @@ class SubwayMap : public wxPanel { void OnMouseScroll(wxMouseEvent &event); void OnMouseLeave(wxMouseEvent &event); void OnTimer(wxTimerEvent &event); + void OnZoomSlide(wxCommandEvent &event); void Redraw(); @@ -41,6 +42,7 @@ class SubwayMap : public wxPanel { void SetZoomPos(wxPoint pos); void SetScrollSpeed(int scroll_x, int scroll_y); + void SetZoom(double zoom, wxPoint static_point); wxImage map_image_; wxImage owl_image_; @@ -61,6 +63,8 @@ class SubwayMap : public wxPanel { int scroll_x_ = 0; int scroll_y_ = 0; + wxSlider *zoom_slider_; + struct GetItemBox { quadtree::Box operator()(const int &id) const; }; -- cgit 1.4.1