about summary refs log tree commit diff stats
path: root/src/tracker_state.cpp
blob: e02ee14101c626738869762f932d4bb699d7dc59 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "tracker_state.h"

#include <list>
#include <map>
#include <mutex>
#include <set>
#include <sstream>
#include <tuple>

#include "ap_state.h"
#include "game_data.h"

namespace {

struct TrackerState {
  std::map<int, bool> reachability;
  std::mutex reachability_mutex;
};

enum Decision { kYes, kNo, kMaybe };

TrackerState& GetState() {
  static TrackerState* instance = new TrackerState();
  return *instance;
}

Decision IsDoorReachable_Helper(int door_id,
                                const std::set<int>& reachable_rooms,
                                const std::set<int>& solveable_panels) {
  const Door& door_obj = GD_GetDoor(door_id);

  if (AP_GetDoorShuffleMode() == kNO_DOORS || door_obj.skip_item) {
    if (!reachable_rooms.count(door_obj.room)) {
      return kMaybe;
    }

    for (int panel_id : door_obj.panels) {
      if (!solveable_panels.count(panel_id)) {
        return kMaybe;
      }
    }

    return kYes;
  } else if (AP_GetDoorShuffleMode() == kSIMPLE_DOORS &&
             !door_obj.group_name.empty()) {
    return AP_HasItem(door_obj.group_ap_item_id) ? kYes : kNo;
  } else {
    bool has_item = AP_HasItem(door_obj.ap_item_id);

    if (!has_item) {
      for (const ProgressiveRequirement& prog_req : door_obj.progressives) {
        if (AP_HasItem(prog_req.ap_item_id, prog_req.quantity)) {
          has_item = true;
          break;
        }
      }
    }

    return has_item ? kYes : kNo;
  }
}

Decision IsPanelReachable_Helper(int panel_id,
                                 const std::set<int>& reachable_rooms,
                                 const std::set<int>& solveable_panels) {
  const Panel& panel_obj = GD_GetPanel(panel_id);

  if (!reachable_rooms.count(panel_obj.room)) {
    return kMaybe;
  }

  if (panel_obj.name == "THE MASTER") {
    int achievements_accessible = 0;

    for (int achieve_id : GD_GetAchievementPanels()) {
      if (solveable_panels.count(achieve_id)) {
        achievements_accessible++;

        if (achievements_accessible >= AP_GetMasteryRequirement()) {
          break;
        }
      }
    }

    return (achievements_accessible >= AP_GetMasteryRequirement()) ? kYes
                                                                   : kMaybe;
  }

  if ((panel_obj.name == "ANOTHER TRY" || panel_obj.name == "LEVEL 2") &&
      AP_GetLevel2Requirement() > 1) {
    int counting_panels_accessible = 0;

    for (int solved_panel_id : solveable_panels) {
      const Panel& solved_panel = GD_GetPanel(solved_panel_id);

      if (!solved_panel.non_counting) {
        counting_panels_accessible++;
      }
    }

    return (counting_panels_accessible >= AP_GetLevel2Requirement() - 1)
               ? kYes
               : kMaybe;
  }

  for (int room_id : panel_obj.required_rooms) {
    if (!reachable_rooms.count(room_id)) {
      return kMaybe;
    }
  }

  for (int door_id : panel_obj.required_doors) {
    Decision door_reachable =
        IsDoorReachable_Helper(door_id, reachable_rooms, solveable_panels);
    if (door_reachable == kNo) {
      const Door& door_obj = GD_GetDoor(door_id);
      return (door_obj.is_event || AP_GetDoorShuffleMode() == kNO_DOORS)
                 ? kMaybe
                 : kNo;
    } else if (door_reachable == kMaybe) {
      return kMaybe;
    }
  }

  for (int panel_id : panel_obj.required_panels) {
    if (!solveable_panels.count(panel_id)) {
      return kMaybe;
    }
  }

  if (AP_IsColorShuffle()) {
    for (LingoColor color : panel_obj.colors) {
      if (!AP_HasItem(GD_GetItemIdForColor(color))) {
        return kNo;
      }
    }
  }

  return kYes;
}

}  // namespace

void RecalculateReachability() {
  std::set<int> reachable_rooms;
  std::set<int> solveable_panels;

  std::list<int> panel_boundary;
  std::list<Exit> flood_boundary;
  flood_boundary.push_back({.destination_room = GD_GetRoomByName("Menu")});

  if (AP_HasEarlyColorHallways()) {
    flood_boundary.push_back(
        {.destination_room = GD_GetRoomByName("Outside The Undeterred")});
  }

  bool reachable_changed = true;
  while (reachable_changed) {
    reachable_changed = false;

    std::list<int> new_panel_boundary;
    for (int panel_id : panel_boundary) {
      if (solveable_panels.count(panel_id)) {
        continue;
      }

      Decision panel_reachable =
          IsPanelReachable_Helper(panel_id, reachable_rooms, solveable_panels);
      if (panel_reachable == kYes) {
        solveable_panels.insert(panel_id);
        reachable_changed = true;
      } else if (panel_reachable == kMaybe) {
        new_panel_boundary.push_back(panel_id);
      }
    }

    std::list<Exit> new_boundary;
    for (const Exit& room_exit : flood_boundary) {
      if (reachable_rooms.count(room_exit.destination_room)) {
        continue;
      }

      bool valid_transition = false;
      if (room_exit.door.has_value()) {
        Decision door_reachable = IsDoorReachable_Helper(
            *room_exit.door, reachable_rooms, solveable_panels);
        if (door_reachable == kYes) {
          valid_transition = true;
        } else if (door_reachable == kMaybe) {
          new_boundary.push_back(room_exit);
        }
      } else {
        valid_transition = true;
      }

      if (valid_transition) {
        reachable_rooms.insert(room_exit.destination_room);
        reachable_changed = true;

        const Room& room_obj = GD_GetRoom(room_exit.destination_room);
        for (const Exit& out_edge : room_obj.exits) {
          if (!out_edge.painting || !AP_IsPaintingShuffle()) {
            new_boundary.push_back(out_edge);
          }
        }

        if (AP_IsPaintingShuffle()) {
          for (const PaintingExit& out_edge : room_obj.paintings) {
            if (AP_GetPaintingMapping().count(out_edge.id)) {
              Exit painting_exit;
              painting_exit.destination_room = GD_GetRoomForPainting(
                  AP_GetPaintingMapping().at(out_edge.id));
              painting_exit.door = out_edge.door;

              new_boundary.push_back(painting_exit);
            }
          }
        }

        for (int panel_id : room_obj.panels) {
          new_panel_boundary.push_back(panel_id);
        }
      }
    }

    flood_boundary = new_boundary;
    panel_boundary = new_panel_boundary;
  }

  std::map<int, bool> new_reachability;
  for (const MapArea& map_area : GD_GetMapAreas()) {
    for (size_t section_id = 0; section_id < map_area.locations.size();
         section_id++) {
      const Location& location_section = map_area.locations.at(section_id);
      bool reachable = reachable_rooms.count(location_section.room);
      if (reachable) {
        for (int panel_id : location_section.panels) {
          reachable &= (solveable_panels.count(panel_id) == 1);
        }
      }

      new_reachability[location_section.ap_location_id] = reachable;
    }
  }

  {
    std::lock_guard reachability_guard(GetState().reachability_mutex);
    std::swap(GetState().reachability, new_reachability);
  }
}

bool IsLocationReachable(int location_id) {
  std::lock_guard reachability_guard(GetState().reachability_mutex);

  if (GetState().reachability.count(location_id)) {
    return GetState().reachability.at(location_id);
  } else {
    return false;
  }
}
{ toWarp.dots = mapping.exit_index + 1; toWarp.type = SubwaySunwarpType::kEnter; } else { toWarp.dots = mapping.exit_index - 5; toWarp.type = SubwaySunwarpType::kExit; } tagged[tag.str()].push_back(GD_GetSubwayItemForSunwarp(fromWarp)); tagged[tag.str()].push_back(GD_GetSubwayItemForSunwarp(toWarp)); } } for (const auto &[tag, items] : tagged) { // Pairwise connect all items with the same tag. for (auto tag_it1 = items.begin(); std::next(tag_it1) != items.end(); tag_it1++) { for (auto tag_it2 = std::next(tag_it1); tag_it2 != items.end(); tag_it2++) { networks_.AddLink(*tag_it1, *tag_it2); } } } checked_paintings_.clear(); } void SubwayMap::UpdateIndicators() { if (AP_IsPaintingShuffle()) { for (const std::string &painting_id : AP_GetCheckedPaintings()) { if (!checked_paintings_.count(painting_id)) { checked_paintings_.insert(painting_id); if (AP_GetPaintingMapping().count(painting_id)) { networks_.AddLink(GD_GetSubwayItemForPainting(painting_id), GD_GetSubwayItemForPainting( AP_GetPaintingMapping().at(painting_id))); } } } } Redraw(); } void SubwayMap::UpdateSunwarp(SubwaySunwarp from_sunwarp, SubwaySunwarp to_sunwarp) { networks_.AddLink(GD_GetSubwayItemForSunwarp(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(); wxSize image_size = map_image_.GetSize(); render_x_ = 0; render_y_ = 0; render_width_ = panel_size.GetWidth(); render_height_ = panel_size.GetHeight(); if (image_size.GetWidth() * panel_size.GetHeight() > panel_size.GetWidth() * image_size.GetHeight()) { render_height_ = (panel_size.GetWidth() * image_size.GetHeight()) / image_size.GetWidth(); render_y_ = (panel_size.GetHeight() - render_height_) / 2; } else { render_width_ = (image_size.GetWidth() * panel_size.GetHeight()) / image_size.GetHeight(); render_x_ = (panel_size.GetWidth() - render_width_) / 2; } SetZoomPos({zoom_x_, zoom_y_}); SetUpHelpButton(); } wxBufferedPaintDC dc(this); dc.SetBackground(*wxWHITE_BRUSH); dc.Clear(); { wxMemoryDC rendered_dc; rendered_dc.SelectObject(rendered_); int dst_x; int dst_y; int dst_w; int dst_h; int src_x; int src_y; int src_w; int src_h; int zoomed_width = render_width_ * zoom_; int zoomed_height = render_height_ * zoom_; if (zoomed_width <= GetSize().GetWidth()) { dst_x = (GetSize().GetWidth() - zoomed_width) / 2; dst_w = zoomed_width; src_x = 0; src_w = map_image_.GetWidth(); } else { dst_x = 0; dst_w = GetSize().GetWidth(); src_x = -zoom_x_ * map_image_.GetWidth() / render_width_ / zoom_; src_w = GetSize().GetWidth() * map_image_.GetWidth() / render_width_ / zoom_; } if (zoomed_height <= GetSize().GetHeight()) { dst_y = (GetSize().GetHeight() - zoomed_height) / 2; dst_h = zoomed_height; src_y = 0; src_h = map_image_.GetHeight(); } else { dst_y = 0; dst_h = GetSize().GetHeight(); src_y = -zoom_y_ * map_image_.GetWidth() / render_width_ / zoom_; src_h = GetSize().GetHeight() * map_image_.GetWidth() / render_width_ / zoom_; } wxGCDC gcdc(dc); gcdc.GetGraphicsContext()->SetInterpolationQuality(wxINTERPOLATION_GOOD); gcdc.StretchBlit(dst_x, dst_y, dst_w, dst_h, &rendered_dc, src_x, src_y, src_w, src_h); } if (hovered_item_) { // Note that these requirements are duplicated on OnMouseClick so that it // knows when an item has a hover effect. const SubwayItem &subway_item = GD_GetSubwayItem(*hovered_item_); if (subway_item.door && !GetDoorRequirements(*subway_item.door).empty()) { const std::map<std::string, bool> &report = GetDoorRequirements(*subway_item.door); int acc_height = 10; int col_width = 0; for (const auto &[text, obtained] : report) { wxSize item_extent = dc.GetTextExtent(text); int item_height = std::max(32, item_extent.GetHeight()) + 10; acc_height += item_height; if (item_extent.GetWidth() > col_width) { col_width = item_extent.GetWidth(); } } int item_width = col_width + 10 + 32; int full_width = item_width + 20; wxPoint popup_pos = MapPosToRenderPos({subway_item.x + AREA_ACTUAL_SIZE / 2, subway_item.y + AREA_ACTUAL_SIZE / 2}); if (popup_pos.x + full_width > GetSize().GetWidth()) { popup_pos.x = GetSize().GetWidth() - full_width; } if (popup_pos.y + acc_height > GetSize().GetHeight()) { popup_pos.y = GetSize().GetHeight() - acc_height; } dc.SetPen(*wxTRANSPARENT_PEN); dc.SetBrush(*wxBLACK_BRUSH); dc.DrawRectangle(popup_pos, {full_width, acc_height}); dc.SetFont(GetFont()); int cur_height = 10; for (const auto &[text, obtained] : report) { wxBitmap *eye_ptr = obtained ? &checked_eye_ : &unchecked_eye_; dc.DrawBitmap(*eye_ptr, popup_pos + wxPoint{10, cur_height}); dc.SetTextForeground(obtained ? *wxWHITE : *wxRED); wxSize item_extent = dc.GetTextExtent(text); dc.DrawText( text, popup_pos + wxPoint{10 + 32 + 10, cur_height + (32 - dc.GetFontMetrics().height) / 2}); cur_height += 10 + 32; } } if (networks_.IsItemInNetwork(*hovered_item_)) { dc.SetBrush(*wxTRANSPARENT_BRUSH); for (const auto &[item_id1, item_id2] : networks_.GetNetworkGraph(*hovered_item_)) { const SubwayItem &item1 = GD_GetSubwayItem(item_id1); const SubwayItem &item2 = GD_GetSubwayItem(item_id2); wxPoint item1_pos = MapPosToRenderPos( {item1.x + AREA_ACTUAL_SIZE / 2, item1.y + AREA_ACTUAL_SIZE / 2}); wxPoint item2_pos = MapPosToRenderPos( {item2.x + AREA_ACTUAL_SIZE / 2, item2.y + AREA_ACTUAL_SIZE / 2}); int left = std::min(item1_pos.x, item2_pos.x); int top = std::min(item1_pos.y, item2_pos.y); int right = std::max(item1_pos.x, item2_pos.x); int bottom = std::max(item1_pos.y, item2_pos.y); int halfwidth = right - left; int halfheight = bottom - top; if (halfwidth < 4 || halfheight < 4) { dc.SetPen(*wxThePenList->FindOrCreatePen(*wxBLACK, 4)); dc.DrawLine(item1_pos, item2_pos); dc.SetPen(*wxThePenList->FindOrCreatePen(*wxCYAN, 2)); dc.DrawLine(item1_pos, item2_pos); } else { int ellipse_x; int ellipse_y; double start; double end; if (item1_pos.x > item2_pos.x) { ellipse_y = top; if (item1_pos.y > item2_pos.y) { ellipse_x = left - halfwidth; start = 0; end = 90; } else { ellipse_x = left; start = 90; end = 180; } } else { ellipse_y = top - halfheight; if (item1_pos.y > item2_pos.y) { ellipse_x = left - halfwidth; start = 270; end = 360; } else { ellipse_x = left; start = 180; end = 270; } } dc.SetPen(*wxThePenList->FindOrCreatePen(*wxBLACK, 4)); dc.DrawEllipticArc(ellipse_x, ellipse_y, halfwidth * 2, halfheight * 2, start, end); dc.SetPen(*wxThePenList->FindOrCreatePen(*wxCYAN, 2)); dc.DrawEllipticArc(ellipse_x, ellipse_y, halfwidth * 2, halfheight * 2, start, end); } } } } event.Skip(); } void SubwayMap::OnMouseMove(wxMouseEvent &event) { wxPoint mouse_pos = RenderPosToMapPos(event.GetPosition()); std::vector<int> hovered = tree_->query( {static_cast<float>(mouse_pos.x), static_cast<float>(mouse_pos.y), 2, 2}); if (!hovered.empty()) { actual_hover_= hovered[0]; } else { actual_hover_ = std::nullopt; } if (!sticky_hover_ && actual_hover_ != hovered_item_) { hovered_item_ = actual_hover_; Refresh(); } if (scroll_mode_) { EvaluateScroll(event.GetPosition()); } mouse_position_ = event.GetPosition(); event.Skip(); } void SubwayMap::OnMouseScroll(wxMouseEvent &event) { double new_zoom = zoom_; if (event.GetWheelRotation() > 0) { new_zoom = std::min(3.0, zoom_ + 0.25); } else { new_zoom = std::max(1.0, zoom_ - 0.25); } if (zoom_ != new_zoom) { SetZoom(new_zoom, event.GetPosition()); } event.Skip(); } void SubwayMap::OnMouseLeave(wxMouseEvent &event) { SetScrollSpeed(0, 0); mouse_position_ = std::nullopt; } void SubwayMap::OnMouseClick(wxMouseEvent &event) { bool finished = false; if (actual_hover_) { const SubwayItem &subway_item = GD_GetSubwayItem(*actual_hover_); if ((subway_item.door && !GetDoorRequirements(*subway_item.door).empty()) || networks_.IsItemInNetwork(*hovered_item_)) { if (actual_hover_ != hovered_item_) { hovered_item_ = actual_hover_; if (!hovered_item_) { sticky_hover_ = false; } Refresh(); } else { sticky_hover_ = !sticky_hover_; } finished = true; } } if (!finished) { if (scroll_mode_) { scroll_mode_ = false; SetScrollSpeed(0, 0); SetCursor(wxCURSOR_ARROW); } else if (event.GetPosition().x < GetSize().GetWidth() / 6 || event.GetPosition().x > 5 * GetSize().GetWidth() / 6 || event.GetPosition().y < GetSize().GetHeight() / 6 || event.GetPosition().y > 5 * GetSize().GetHeight() / 6) { scroll_mode_ = true; EvaluateScroll(event.GetPosition()); SetCursor(wxCURSOR_CROSS); } else { sticky_hover_ = false; } } } void SubwayMap::OnTimer(wxTimerEvent &event) { SetZoomPos({zoom_x_ + scroll_x_, zoom_y_ + scroll_y_}); 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::OnClickHelp(wxCommandEvent &event) { wxMessageBox( "Zoom in/out using the mouse wheel, Ctrl +/-, or the slider in the " "corner.\nClick on a side of the screen to start panning. It will follow " "your mouse. Click again to stop.\nHover over a door to see the " "requirements to open it.\nHover over a warp or active painting to see " "what it is connected to.\nIn painting shuffle, paintings that have not " "yet been checked will not show their connections.\nA green shaded owl " "means that there is a painting entrance there.\nA red shaded owl means " "that there are only painting exits there.\nClick on a door or " "warp to make the popup stick until you click again.", "Subway Map Help"); } void SubwayMap::Redraw() { rendered_ = wxBitmap(map_image_); wxMemoryDC dc; dc.SelectObject(rendered_); wxGCDC gcdc(dc); for (const SubwayItem &subway_item : GD_GetSubwayItems()) { ItemDrawType draw_type = ItemDrawType::kNone; const wxBrush *brush_color = wxGREY_BRUSH; std::optional<wxColour> shade_color; if (AP_HasEarlyColorHallways() && (subway_item.special == "starting_room_paintings" || subway_item.special == "early_color_hallways")) { draw_type = ItemDrawType::kOwl; if (subway_item.special == "starting_room_paintings") { shade_color = wxColour(0, 255, 0, 128); } else { shade_color = wxColour(255, 0, 0, 128); } } else if (subway_item.special == "sun_painting") { if (!AP_IsPilgrimageEnabled()) { if (IsDoorOpen(*subway_item.door)) { draw_type = ItemDrawType::kOwl; shade_color = wxColour(0, 255, 0, 128); } else { draw_type = ItemDrawType::kBox; brush_color = wxRED_BRUSH; } } } else if (!subway_item.paintings.empty()) { if (AP_IsPaintingShuffle()) { bool has_checked_painting = false; bool has_unchecked_painting = false; bool has_mapped_painting = false; bool has_codomain_painting = false; for (const std::string &painting_id : subway_item.paintings) { if (checked_paintings_.count(painting_id)) { has_checked_painting = true; if (AP_GetPaintingMapping().count(painting_id)) { has_mapped_painting = true; } else if (AP_IsPaintingMappedTo(painting_id)) { has_codomain_painting = true; } } else { has_unchecked_painting = true; } } if (has_unchecked_painting || has_mapped_painting || has_codomain_painting) { draw_type = ItemDrawType::kOwl; if (has_checked_painting) { if (has_mapped_painting) { shade_color = wxColour(0, 255, 0, 128); } else { shade_color = wxColour(255, 0, 0, 128); } } } } else if (!subway_item.tags.empty()) { draw_type = ItemDrawType::kOwl; } } else if (subway_item.door) { draw_type = ItemDrawType::kBox; if (IsDoorOpen(*subway_item.door)) { brush_color = wxGREEN_BRUSH; } else { brush_color = wxRED_BRUSH; } } wxPoint real_area_pos = {subway_item.x, subway_item.y}; int real_area_size = (draw_type == ItemDrawType::kOwl ? OWL_ACTUAL_SIZE : AREA_ACTUAL_SIZE); if (draw_type == ItemDrawType::kBox) { gcdc.SetPen(*wxThePenList->FindOrCreatePen(*wxBLACK, 1)); gcdc.SetBrush(*brush_color); gcdc.DrawRectangle(real_area_pos, {real_area_size, real_area_size}); } else if (draw_type == ItemDrawType::kOwl) { wxBitmap owl_bitmap = wxBitmap(owl_image_.Scale( real_area_size, real_area_size, wxIMAGE_QUALITY_BILINEAR)); gcdc.DrawBitmap(owl_bitmap, real_area_pos); if (shade_color) { gcdc.SetBrush(wxBrush(*shade_color)); gcdc.DrawRectangle(real_area_pos, {real_area_size, real_area_size}); } } } } void SubwayMap::SetUpHelpButton() { help_button_->SetPosition({ GetSize().GetWidth() - help_button_->GetSize().GetWidth() - 15, 15, }); } void SubwayMap::EvaluateScroll(wxPoint pos) { int scroll_x; int scroll_y; if (pos.x < GetSize().GetWidth() / 9) { scroll_x = 20; } else if (pos.x < GetSize().GetWidth() / 6) { scroll_x = 5; } else if (pos.x > 8 * GetSize().GetWidth() / 9) { scroll_x = -20; } else if (pos.x > 5 * GetSize().GetWidth() / 6) { scroll_x = -5; } else { scroll_x = 0; } if (pos.y < GetSize().GetHeight() / 9) { scroll_y = 20; } else if (pos.y < GetSize().GetHeight() / 6) { scroll_y = 5; } else if (pos.y > 8 * GetSize().GetHeight() / 9) { scroll_y = -20; } else if (pos.y > 5 * GetSize().GetHeight() / 6) { scroll_y = -5; } else { scroll_y = 0; } SetScrollSpeed(scroll_x, scroll_y); } wxPoint SubwayMap::MapPosToRenderPos(wxPoint pos) const { return {static_cast<int>(pos.x * render_width_ * zoom_ / map_image_.GetSize().GetWidth() + zoom_x_), static_cast<int>(pos.y * render_width_ * zoom_ / map_image_.GetSize().GetWidth() + zoom_y_)}; } wxPoint SubwayMap::MapPosToVirtualPos(wxPoint pos) const { return {static_cast<int>(pos.x * render_width_ * zoom_ / map_image_.GetSize().GetWidth()), static_cast<int>(pos.y * render_width_ * zoom_ / map_image_.GetSize().GetWidth())}; } wxPoint SubwayMap::RenderPosToMapPos(wxPoint pos) const { return { std::clamp(static_cast<int>((pos.x - zoom_x_) * map_image_.GetWidth() / render_width_ / zoom_), 0, map_image_.GetWidth() - 1), std::clamp(static_cast<int>((pos.y - zoom_y_) * map_image_.GetWidth() / render_width_ / zoom_), 0, map_image_.GetHeight() - 1)}; } void SubwayMap::SetZoomPos(wxPoint pos) { if (render_width_ * zoom_ <= GetSize().GetWidth()) { zoom_x_ = (GetSize().GetWidth() - render_width_ * zoom_) / 2; } else { zoom_x_ = std::clamp( pos.x, GetSize().GetWidth() - static_cast<int>(render_width_ * zoom_), 0); } if (render_height_ * zoom_ <= GetSize().GetHeight()) { zoom_y_ = (GetSize().GetHeight() - render_height_ * zoom_) / 2; } else { zoom_y_ = std::clamp( pos.y, GetSize().GetHeight() - static_cast<int>(render_height_ * zoom_), 0); } } void SubwayMap::SetScrollSpeed(int scroll_x, int scroll_y) { bool should_timer = (scroll_x != 0 || scroll_y != 0); if (should_timer != scroll_timer_->IsRunning()) { if (should_timer) { scroll_timer_->Start(1000 / 60); } else { scroll_timer_->Stop(); } } scroll_x_ = scroll_x; 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<float> SubwayMap::GetItemBox::operator()(const int &id) const { const SubwayItem &subway_item = GD_GetSubwayItem(id); return {static_cast<float>(subway_item.x), static_cast<float>(subway_item.y), AREA_ACTUAL_SIZE, AREA_ACTUAL_SIZE}; }