From 983f01cb8a2eaecd162e5734de88c461ef197b34 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sun, 19 May 2024 12:24:00 -0400 Subject: Zoom in/out menu items with keyboard shortcuts --- src/subway_map.cpp | 30 ++++++++++++++++++++++++++++-- src/subway_map.h | 3 +++ src/tracker_frame.cpp | 45 ++++++++++++++++++++++++++++++++++++++------- src/tracker_frame.h | 9 +++++++++ 4 files changed, 78 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/subway_map.cpp b/src/subway_map.cpp index 98d544c..69bf51b 100644 --- a/src/subway_map.cpp +++ b/src/subway_map.cpp @@ -119,6 +119,26 @@ void SubwayMap::UpdateSunwarp(SubwaySunwarp from_sunwarp, GD_GetSubwayItemForSunwarp(to_sunwarp)); } +void SubwayMap::Zoom(bool in) { + wxPoint focus_point; + + if (mouse_position_) { + focus_point = *mouse_position_; + } else { + focus_point = {GetSize().GetWidth() / 2, GetSize().GetHeight() / 2}; + } + + if (in) { + if (zoom_ < 3.0) { + SetZoom(zoom_ + 0.25, focus_point); + } + } else { + if (zoom_ > 1.0) { + SetZoom(zoom_ - 0.25, focus_point); + } + } +} + void SubwayMap::OnPaint(wxPaintEvent &event) { if (GetSize() != rendered_.GetSize()) { wxSize panel_size = GetSize(); @@ -372,6 +392,8 @@ void SubwayMap::OnMouseMove(wxMouseEvent &event) { SetScrollSpeed(scroll_x, scroll_y); + mouse_position_ = event.GetPosition(); + event.Skip(); } @@ -390,7 +412,10 @@ void SubwayMap::OnMouseScroll(wxMouseEvent &event) { event.Skip(); } -void SubwayMap::OnMouseLeave(wxMouseEvent &event) { SetScrollSpeed(0, 0); } +void SubwayMap::OnMouseLeave(wxMouseEvent &event) { + SetScrollSpeed(0, 0); + mouse_position_ = std::nullopt; +} void SubwayMap::OnTimer(wxTimerEvent &event) { SetZoomPos({zoom_x_ + scroll_x_, zoom_y_ + scroll_y_}); @@ -464,7 +489,8 @@ void SubwayMap::Redraw() { wxPoint real_area_pos = {subway_item.x, subway_item.y}; - int real_area_size = (draw_type == ItemDrawType::kOwl ? OWL_ACTUAL_SIZE : AREA_ACTUAL_SIZE); + int real_area_size = + (draw_type == ItemDrawType::kOwl ? OWL_ACTUAL_SIZE : AREA_ACTUAL_SIZE); if (draw_type == ItemDrawType::kBox) { dc.SetPen(*wxThePenList->FindOrCreatePen(*wxBLACK, 1)); diff --git a/src/subway_map.h b/src/subway_map.h index e891058..986998a 100644 --- a/src/subway_map.h +++ b/src/subway_map.h @@ -25,6 +25,7 @@ class SubwayMap : public wxPanel { void OnConnect(); void UpdateIndicators(); void UpdateSunwarp(SubwaySunwarp from_sunwarp, SubwaySunwarp to_sunwarp); + void Zoom(bool in); private: void OnPaint(wxPaintEvent &event); @@ -65,6 +66,8 @@ class SubwayMap : public wxPanel { wxSlider *zoom_slider_; + std::optional mouse_position_; + struct GetItemBox { quadtree::Box operator()(const int &id) const; }; diff --git a/src/tracker_frame.cpp b/src/tracker_frame.cpp index e944704..a15a6b4 100644 --- a/src/tracker_frame.cpp +++ b/src/tracker_frame.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -19,7 +20,9 @@ enum TrackerFrameIds { ID_CONNECT = 1, ID_CHECK_FOR_UPDATES = 2, - ID_SETTINGS = 3 + ID_SETTINGS = 3, + ID_ZOOM_IN = 4, + ID_ZOOM_OUT = 5, }; wxDEFINE_EVENT(STATE_RESET, wxCommandEvent); @@ -38,12 +41,20 @@ TrackerFrame::TrackerFrame() menuFile->Append(ID_SETTINGS, "&Settings"); menuFile->Append(wxID_EXIT); + wxMenu *menuView = new wxMenu(); + zoom_in_menu_item_ = menuView->Append(ID_ZOOM_IN, "Zoom In\tCtrl-+"); + zoom_out_menu_item_ = menuView->Append(ID_ZOOM_OUT, "Zoom Out\tCtrl--"); + + zoom_in_menu_item_->Enable(false); + zoom_out_menu_item_->Enable(false); + wxMenu *menuHelp = new wxMenu(); menuHelp->Append(wxID_ABOUT); menuHelp->Append(ID_CHECK_FOR_UPDATES, "Check for Updates"); wxMenuBar *menuBar = new wxMenuBar(); menuBar->Append(menuFile, "&File"); + menuBar->Append(menuView, "&View"); menuBar->Append(menuHelp, "&Help"); SetMenuBar(menuBar); @@ -57,6 +68,9 @@ TrackerFrame::TrackerFrame() Bind(wxEVT_MENU, &TrackerFrame::OnSettings, this, ID_SETTINGS); Bind(wxEVT_MENU, &TrackerFrame::OnCheckForUpdates, this, ID_CHECK_FOR_UPDATES); + Bind(wxEVT_MENU, &TrackerFrame::OnZoomIn, this, ID_ZOOM_IN); + Bind(wxEVT_MENU, &TrackerFrame::OnZoomOut, this, ID_ZOOM_OUT); + Bind(wxEVT_NOTEBOOK_PAGE_CHANGED, &TrackerFrame::OnChangePage, this); Bind(STATE_RESET, &TrackerFrame::OnStateReset, this); Bind(STATE_CHANGED, &TrackerFrame::OnStateChanged, this); Bind(STATUS_CHANGED, &TrackerFrame::OnStatusChanged, this); @@ -66,15 +80,15 @@ TrackerFrame::TrackerFrame() wxChoicebook *choicebook = new wxChoicebook(this, wxID_ANY); choicebook->AddPage(achievements_pane_, "Achievements"); - wxNotebook *rightpane = new wxNotebook(this, wxID_ANY); - tracker_panel_ = new TrackerPanel(rightpane); - subway_map_ = new SubwayMap(rightpane); - rightpane->AddPage(tracker_panel_, "Map"); - rightpane->AddPage(subway_map_, "Subway"); + notebook_ = new wxNotebook(this, wxID_ANY); + tracker_panel_ = new TrackerPanel(notebook_); + subway_map_ = new SubwayMap(notebook_); + notebook_->AddPage(tracker_panel_, "Map"); + notebook_->AddPage(subway_map_, "Subway"); wxBoxSizer *top_sizer = new wxBoxSizer(wxHORIZONTAL); top_sizer->Add(choicebook, wxSizerFlags().Expand().Proportion(1)); - top_sizer->Add(rightpane, wxSizerFlags().Expand().Proportion(3)); + top_sizer->Add(notebook_, wxSizerFlags().Expand().Proportion(3)); SetSizerAndFit(top_sizer); SetSize(1280, 728); @@ -174,6 +188,23 @@ void TrackerFrame::OnCheckForUpdates(wxCommandEvent &event) { CheckForUpdates(/*manual=*/true); } +void TrackerFrame::OnZoomIn(wxCommandEvent &event) { + if (notebook_->GetSelection() == 1) { + subway_map_->Zoom(true); + } +} + +void TrackerFrame::OnZoomOut(wxCommandEvent& event) { + if (notebook_->GetSelection() == 1) { + subway_map_->Zoom(false); + } +} + +void TrackerFrame::OnChangePage(wxBookCtrlEvent &event) { + zoom_in_menu_item_->Enable(event.GetSelection() == 1); + zoom_out_menu_item_->Enable(event.GetSelection() == 1); +} + void TrackerFrame::OnStateReset(wxCommandEvent& event) { tracker_panel_->UpdateIndicators(); achievements_pane_->UpdateIndicators(); diff --git a/src/tracker_frame.h b/src/tracker_frame.h index f1d7171..f7cb3f2 100644 --- a/src/tracker_frame.h +++ b/src/tracker_frame.h @@ -10,6 +10,8 @@ class AchievementsPane; class SubwayMap; class TrackerPanel; +class wxBookCtrlEvent; +class wxNotebook; wxDECLARE_EVENT(STATE_RESET, wxCommandEvent); wxDECLARE_EVENT(STATE_CHANGED, wxCommandEvent); @@ -30,6 +32,9 @@ class TrackerFrame : public wxFrame { void OnConnect(wxCommandEvent &event); void OnSettings(wxCommandEvent &event); void OnCheckForUpdates(wxCommandEvent &event); + void OnZoomIn(wxCommandEvent &event); + void OnZoomOut(wxCommandEvent &event); + void OnChangePage(wxBookCtrlEvent &event); void OnStateReset(wxCommandEvent &event); void OnStateChanged(wxCommandEvent &event); @@ -37,9 +42,13 @@ class TrackerFrame : public wxFrame { void CheckForUpdates(bool manual); + wxNotebook *notebook_; TrackerPanel *tracker_panel_; AchievementsPane *achievements_pane_; SubwayMap *subway_map_; + + wxMenuItem *zoom_in_menu_item_; + wxMenuItem *zoom_out_menu_item_; }; #endif /* end of include guard: TRACKER_FRAME_H_86BD8DFB */ -- cgit 1.4.1