#pragma once #include #include namespace settlement::viewer { struct ScreenPoint { double x; double y; }; class MapCamera { public: [[nodiscard]] double scale() const noexcept { return scale_; } [[nodiscard]] ScreenPoint offset() const noexcept { return offset_; } [[nodiscard]] ScreenPoint to_scene(ScreenPoint screen) const noexcept { return {(screen.x - offset_.x) / scale_, (screen.y - offset_.y) / scale_}; } void zoom_at(ScreenPoint cursor, double steps) noexcept { const double next_scale = std::clamp(scale_ * std::pow(1.25, steps), 0.65, 4.0); if (next_scale == scale_) return; const ScreenPoint anchor = to_scene(cursor); scale_ = next_scale; offset_ = {cursor.x - anchor.x * scale_, cursor.y - anchor.y * scale_}; } void pan(double dx, double dy) noexcept { offset_.x += dx; offset_.y += dy; } private: double scale_ = 1.0; ScreenPoint offset_{0.0, 0.0}; }; } // namespace settlement::viewer