From ea16cff14ff4faf5782da8ff684a6ec412b7b6ac Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sun, 12 May 2024 17:48:02 -0400 Subject: Started making subway map --- src/subway_map.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/subway_map.cpp (limited to 'src/subway_map.cpp') diff --git a/src/subway_map.cpp b/src/subway_map.cpp new file mode 100644 index 0000000..c58b2d1 --- /dev/null +++ b/src/subway_map.cpp @@ -0,0 +1,89 @@ +#include "subway_map.h" + +#include "game_data.h" +#include "global.h" +#include "tracker_state.h" + +constexpr int AREA_ACTUAL_SIZE = 21; + +SubwayMap::SubwayMap(wxWindow *parent) : wxPanel(parent, wxID_ANY) { + map_image_ = + wxImage(GetAbsolutePath("assets/subway.png").c_str(), wxBITMAP_TYPE_PNG); + if (!map_image_.IsOk()) { + return; + } + + Redraw(); + Resize(); + + Bind(wxEVT_PAINT, &SubwayMap::OnPaint, this); + Bind(wxEVT_MOTION, &SubwayMap::OnMouseMove, this); +} + +void SubwayMap::UpdateIndicators() { + Redraw(); + Resize(); +} + +void SubwayMap::OnPaint(wxPaintEvent &event) { + if (GetSize() != resized_.GetSize()) { + Resize(); + } + + wxPaintDC dc(this); + dc.DrawBitmap(resized_, 0, 0); + + event.Skip(); +} + +void SubwayMap::OnMouseMove(wxMouseEvent &event) { + event.Skip(); +} + +void SubwayMap::Redraw() { + rendered_ = wxBitmap(map_image_); + + wxMemoryDC dc; + dc.SelectObject(rendered_); + + for (const SubwayItem &subway_item : GD_GetSubwayItems()) { + const wxBrush *brush_color = wxGREY_BRUSH; + if (subway_item.door) { + if (IsDoorOpen(*subway_item.door)) { + brush_color = wxGREEN_BRUSH; + } else { + brush_color = wxRED_BRUSH; + } + } + + dc.SetPen(*wxThePenList->FindOrCreatePen(*wxBLACK, 1)); + dc.SetBrush(*brush_color); + dc.DrawRectangle({subway_item.x, subway_item.y}, + {AREA_ACTUAL_SIZE, AREA_ACTUAL_SIZE}); + } +} + +void SubwayMap::Resize() { + wxSize panel_size = GetSize(); + wxSize image_size = rendered_.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; + } + + resized_ = wxBitmap(rendered_.ConvertToImage() + .Scale(render_width_, render_height_, wxIMAGE_QUALITY_BILINEAR) + .Size(panel_size, {render_x_, render_y_}, 0, 0, 0)); +} -- cgit 1.4.1