From f0c2f8eec023c7cddc6ac03e0ac18a295948a9f3 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Sun, 6 Aug 2023 22:49:18 -0500 Subject: [PATCH] Correct text not following map rotation --- .../source/scwx/qt/map/placefile_layer.cpp | 24 +++++++++++++++---- wxdata/include/scwx/common/geographic.hpp | 2 ++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/scwx-qt/source/scwx/qt/map/placefile_layer.cpp b/scwx-qt/source/scwx/qt/map/placefile_layer.cpp index 37254a0d..2117932b 100644 --- a/scwx-qt/source/scwx/qt/map/placefile_layer.cpp +++ b/scwx-qt/source/scwx/qt/map/placefile_layer.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -59,6 +60,8 @@ public: std::uint32_t textId_ {}; glm::vec2 mapScreenCoordLocation_ {}; float mapScale_ {1.0f}; + float mapBearingCos_ {1.0f}; + float mapBearingSin_ {0.0f}; float halfWidth_ {}; float halfHeight_ {}; bool thresholded_ {true}; @@ -149,12 +152,23 @@ void PlacefileLayer::Impl::RenderTextDrawItem( mapScreenCoordLocation_) * mapScale_; + // Rotate text according to map rotation + float rotatedX = screenCoordinates.x; + float rotatedY = screenCoordinates.y; + if (params.bearing != 0.0) + { + rotatedX = screenCoordinates.x * mapBearingCos_ - + screenCoordinates.y * mapBearingSin_; + rotatedY = screenCoordinates.x * mapBearingSin_ + + screenCoordinates.y * mapBearingCos_; + } + RenderText(params, di->text_, di->hoverText_, di->color_, - screenCoordinates.x + di->x_ + halfWidth_, - screenCoordinates.y + di->y_ + halfHeight_); + rotatedX + di->x_ + halfWidth_, + rotatedY + di->y_ + halfHeight_); } } @@ -217,8 +231,10 @@ void PlacefileLayer::Render( {params.latitude, params.longitude}); p->mapScale_ = std::pow(2.0, params.zoom) * mbgl::util::tileSize_D / mbgl::util::DEGREES_MAX; - p->halfWidth_ = params.width * 0.5f; - p->halfHeight_ = params.height * 0.5f; + p->mapBearingCos_ = std::cosf(params.bearing * common::kDegreesToRadians); + p->mapBearingSin_ = std::sinf(params.bearing * common::kDegreesToRadians); + p->halfWidth_ = params.width * 0.5f; + p->halfHeight_ = params.height * 0.5f; // Get monospace font pointer std::size_t fontSize = 16; diff --git a/wxdata/include/scwx/common/geographic.hpp b/wxdata/include/scwx/common/geographic.hpp index 1318f43b..8945db17 100644 --- a/wxdata/include/scwx/common/geographic.hpp +++ b/wxdata/include/scwx/common/geographic.hpp @@ -11,6 +11,8 @@ namespace common constexpr double kMilesPerMeter = 0.00062137119; constexpr double kKilometersPerMeter = 0.001; +constexpr double kDegreesToRadians = 0.0174532925199432957692369055556; + /** * @brief Coordinate type to hold latitude and longitude of a location. */