mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 18:10:05 +00:00
Render placefile Place statement
This commit is contained in:
parent
1c39464228
commit
9f5de14f6b
5 changed files with 148 additions and 29 deletions
|
|
@ -61,6 +61,24 @@ public:
|
|||
PlacefileManager::PlacefileManager() : p(std::make_unique<Impl>(this)) {}
|
||||
PlacefileManager::~PlacefileManager() = default;
|
||||
|
||||
std::vector<std::shared_ptr<gr::Placefile>>
|
||||
PlacefileManager::GetActivePlacefiles()
|
||||
{
|
||||
std::vector<std::shared_ptr<gr::Placefile>> placefiles;
|
||||
|
||||
std::shared_lock lock {p->placefileRecordLock_};
|
||||
|
||||
for (const auto& record : p->placefileRecords_)
|
||||
{
|
||||
if (record->enabled_)
|
||||
{
|
||||
placefiles.emplace_back(record->placefile_);
|
||||
}
|
||||
}
|
||||
|
||||
return placefiles;
|
||||
}
|
||||
|
||||
void PlacefileManager::LoadFile(const std::string& filename)
|
||||
{
|
||||
logger_->debug("LoadFile: {}", filename);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <scwx/gr/placefile.hpp>
|
||||
|
||||
#include <QObject>
|
||||
|
||||
|
|
@ -20,6 +19,13 @@ public:
|
|||
explicit PlacefileManager();
|
||||
~PlacefileManager();
|
||||
|
||||
/**
|
||||
* @brief Gets a list of active placefiles
|
||||
*
|
||||
* @return Vector of placefile pointers
|
||||
*/
|
||||
std::vector<std::shared_ptr<gr::Placefile>> GetActivePlacefiles();
|
||||
|
||||
void LoadFile(const std::string& filename);
|
||||
|
||||
static std::shared_ptr<PlacefileManager> Instance();
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
#include <scwx/qt/map/placefile_layer.hpp>
|
||||
#include <scwx/qt/manager/placefile_manager.hpp>
|
||||
#include <scwx/qt/util/geographic_lib.hpp>
|
||||
#include <scwx/qt/util/maplibre.hpp>
|
||||
#include <scwx/util/logger.hpp>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <imgui.h>
|
||||
#include <mbgl/util/constants.hpp>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
|
|
@ -20,13 +24,21 @@ public:
|
|||
explicit Impl(std::shared_ptr<MapContext> context) {};
|
||||
~Impl() = default;
|
||||
|
||||
void RenderText(const std::string& text,
|
||||
void RenderPlace(const QMapLibreGL::CustomLayerRenderParameters& params,
|
||||
std::shared_ptr<gr::Placefile::PlaceDrawItem> place);
|
||||
|
||||
void RenderText(const QMapLibreGL::CustomLayerRenderParameters& params,
|
||||
const std::string& text,
|
||||
const std::string& hoverText,
|
||||
boost::gil::rgba8_pixel_t color,
|
||||
float x,
|
||||
float y);
|
||||
|
||||
std::uint32_t textId_ {};
|
||||
glm::vec2 mapScreenCoordLocation_ {};
|
||||
float mapScale_ {1.0f};
|
||||
float halfWidth_ {};
|
||||
float halfHeight_ {};
|
||||
};
|
||||
|
||||
PlacefileLayer::PlacefileLayer(std::shared_ptr<MapContext> context) :
|
||||
|
|
@ -43,7 +55,33 @@ void PlacefileLayer::Initialize()
|
|||
DrawLayer::Initialize();
|
||||
}
|
||||
|
||||
void PlacefileLayer::Impl::RenderText(const std::string& text,
|
||||
void PlacefileLayer::Impl::RenderPlace(
|
||||
const QMapLibreGL::CustomLayerRenderParameters& params,
|
||||
std::shared_ptr<gr::Placefile::PlaceDrawItem> place)
|
||||
{
|
||||
auto distance = util::GeographicLib::GetDistance(
|
||||
params.latitude, params.longitude, place->latitude_, place->longitude_);
|
||||
|
||||
if (distance < place->threshold_)
|
||||
{
|
||||
const auto screenCoordinates =
|
||||
(util::maplibre::LatLongToScreenCoordinate(
|
||||
{place->latitude_, place->longitude_}) -
|
||||
mapScreenCoordLocation_) *
|
||||
mapScale_;
|
||||
|
||||
RenderText(params,
|
||||
place->text_,
|
||||
"",
|
||||
place->color_,
|
||||
screenCoordinates.x + place->x_ + halfWidth_,
|
||||
screenCoordinates.y + place->y_ + halfHeight_);
|
||||
}
|
||||
}
|
||||
|
||||
void PlacefileLayer::Impl::RenderText(
|
||||
const QMapLibreGL::CustomLayerRenderParameters& params,
|
||||
const std::string& text,
|
||||
const std::string& hoverText,
|
||||
boost::gil::rgba8_pixel_t color,
|
||||
float x,
|
||||
|
|
@ -51,6 +89,9 @@ void PlacefileLayer::Impl::RenderText(const std::string& text,
|
|||
{
|
||||
const std::string windowName {fmt::format("PlacefileText-{}", ++textId_)};
|
||||
|
||||
// Convert screen to ImGui coordinates
|
||||
y = params.height - y;
|
||||
|
||||
// Setup "window" to hold text
|
||||
ImGui::SetNextWindowPos(
|
||||
ImVec2 {x, y}, ImGuiCond_Always, ImVec2 {0.5f, 0.5f});
|
||||
|
|
@ -87,7 +128,33 @@ void PlacefileLayer::Render(
|
|||
// Reset text ID per frame
|
||||
p->textId_ = 0;
|
||||
|
||||
// Update map screen coordinate and scale information
|
||||
p->mapScreenCoordLocation_ = util::maplibre::LatLongToScreenCoordinate(
|
||||
{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;
|
||||
|
||||
std::shared_ptr<manager::PlacefileManager> placefileManager =
|
||||
manager::PlacefileManager::Instance();
|
||||
|
||||
// Render text
|
||||
for (auto& placefile : placefileManager->GetActivePlacefiles())
|
||||
{
|
||||
for (auto& drawItem : placefile->GetDrawItems())
|
||||
{
|
||||
switch (drawItem->itemType_)
|
||||
{
|
||||
case gr::Placefile::ItemType::Place:
|
||||
p->RenderPlace(
|
||||
params,
|
||||
std::static_pointer_cast<gr::Placefile::PlaceDrawItem>(
|
||||
drawItem));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SCWX_GL_CHECK_ERROR();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@
|
|||
#include <vector>
|
||||
|
||||
#include <boost/gil/typedefs.hpp>
|
||||
#include <boost/units/quantity.hpp>
|
||||
#include <boost/units/systems/si/length.hpp>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
|
|
@ -33,8 +35,46 @@ public:
|
|||
Placefile(Placefile&&) noexcept;
|
||||
Placefile& operator=(Placefile&&) noexcept;
|
||||
|
||||
enum class ItemType
|
||||
{
|
||||
Place,
|
||||
Icon,
|
||||
Font,
|
||||
Text,
|
||||
Line,
|
||||
Triangles,
|
||||
Image,
|
||||
Polygon,
|
||||
Unknown
|
||||
};
|
||||
|
||||
struct DrawItem
|
||||
{
|
||||
ItemType itemType_ {ItemType::Unknown};
|
||||
boost::units::quantity<boost::units::si::length> threshold_ {};
|
||||
};
|
||||
|
||||
struct PlaceDrawItem : DrawItem
|
||||
{
|
||||
PlaceDrawItem() { itemType_ = ItemType::Place; }
|
||||
|
||||
boost::gil::rgba8_pixel_t color_ {};
|
||||
double latitude_ {};
|
||||
double longitude_ {};
|
||||
double x_ {};
|
||||
double y_ {};
|
||||
std::string text_ {};
|
||||
};
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
/**
|
||||
* @brief Gets the list of draw items defined in the placefile
|
||||
*
|
||||
* @return vector of draw item pointers
|
||||
*/
|
||||
std::vector<std::shared_ptr<DrawItem>> GetDrawItems();
|
||||
|
||||
static std::shared_ptr<Placefile> Load(const std::string& filename);
|
||||
static std::shared_ptr<Placefile> Load(std::istream& is);
|
||||
|
||||
|
|
|
|||
|
|
@ -10,8 +10,6 @@
|
|||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/units/base_units/metric/nautical_mile.hpp>
|
||||
#include <boost/units/quantity.hpp>
|
||||
#include <boost/units/systems/si/length.hpp>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
|
|
@ -42,21 +40,6 @@ public:
|
|||
double y_ {};
|
||||
};
|
||||
|
||||
struct DrawItem
|
||||
{
|
||||
boost::units::quantity<boost::units::si::length> threshold_ {};
|
||||
};
|
||||
|
||||
struct PlaceDrawItem : DrawItem
|
||||
{
|
||||
boost::gil::rgba8_pixel_t color_ {};
|
||||
double latitude_ {};
|
||||
double longitude_ {};
|
||||
double x_ {};
|
||||
double y_ {};
|
||||
std::string text_ {};
|
||||
};
|
||||
|
||||
void ParseLocation(const std::string& latitudeToken,
|
||||
const std::string& longitudeToken,
|
||||
double& latitude,
|
||||
|
|
@ -93,6 +76,11 @@ bool Placefile::IsValid() const
|
|||
return p->drawItems_.size() > 0;
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<Placefile::DrawItem>> Placefile::GetDrawItems()
|
||||
{
|
||||
return p->drawItems_;
|
||||
}
|
||||
|
||||
std::shared_ptr<Placefile> Placefile::Load(const std::string& filename)
|
||||
{
|
||||
logger_->debug("Loading placefile: {}", filename);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue