Refactoring map and ImGui functions from hover text

This commit is contained in:
Dan Paulat 2023-08-28 23:43:07 -05:00
parent 5bb5093579
commit 2c3de1a28f
7 changed files with 192 additions and 89 deletions

View file

@ -0,0 +1,105 @@
#include <scwx/qt/util/imgui.hpp>
#include <scwx/qt/manager/resource_manager.hpp>
#include <scwx/qt/manager/settings_manager.hpp>
#include <scwx/util/logger.hpp>
#include <mutex>
#include <imgui.h>
namespace scwx
{
namespace qt
{
namespace util
{
static const std::string logPrefix_ = "scwx::qt::util::imgui";
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
class ImGui::Impl
{
public:
explicit Impl() {}
~Impl() {}
void Initialize();
void UpdateMonospaceFont();
bool initialized_ {false};
ImFont* monospaceFont_ {nullptr};
};
ImGui::ImGui() : p(std::make_unique<Impl>()) {}
ImGui::~ImGui() = default;
ImGui::ImGui(ImGui&&) noexcept = default;
ImGui& ImGui::operator=(ImGui&&) noexcept = default;
void ImGui::Impl::Initialize()
{
if (initialized_)
{
return;
}
logger_->debug("Initialize");
// Configure monospace font
UpdateMonospaceFont();
manager::SettingsManager::general_settings()
.font_sizes()
.RegisterValueChangedCallback([this](const std::vector<std::int64_t>&)
{ UpdateMonospaceFont(); });
initialized_ = true;
}
void ImGui::Impl::UpdateMonospaceFont()
{
// Get monospace font size
std::size_t fontSize = 16;
auto fontSizes =
manager::SettingsManager::general_settings().font_sizes().GetValue();
if (fontSizes.size() > 1)
{
fontSize = fontSizes[1];
}
else if (fontSizes.size() > 0)
{
fontSize = fontSizes[0];
}
// Get monospace font pointer
auto monospace =
manager::ResourceManager::Font(types::Font::Inconsolata_Regular);
auto monospaceFont = monospace->ImGuiFont(fontSize);
// Store monospace font pointer if not null
if (monospaceFont != nullptr)
{
monospaceFont_ = monospace->ImGuiFont(fontSize);
}
}
void ImGui::DrawTooltip(const std::string& hoverText)
{
p->Initialize();
::ImGui::BeginTooltip();
::ImGui::PushFont(p->monospaceFont_);
::ImGui::TextUnformatted(hoverText.c_str());
::ImGui::PopFont();
::ImGui::EndTooltip();
}
ImGui& ImGui::Instance()
{
static ImGui instance_ {};
return instance_;
}
} // namespace util
} // namespace qt
} // namespace scwx

View file

@ -0,0 +1,37 @@
#pragma once
#include <memory>
#include <string>
namespace scwx
{
namespace qt
{
namespace util
{
class ImGui
{
public:
explicit ImGui();
~ImGui();
ImGui(const ImGui&) = delete;
ImGui& operator=(const ImGui&) = delete;
ImGui(ImGui&&) noexcept;
ImGui& operator=(ImGui&&) noexcept;
void DrawTooltip(const std::string& hoverText);
static ImGui& Instance();
private:
class Impl;
std::unique_ptr<Impl> p;
};
} // namespace util
} // namespace qt
} // namespace scwx

View file

@ -43,6 +43,36 @@ glm::vec2 GetMapScale(const QMapLibreGL::CustomLayerRenderParameters& params)
return glm::vec2 {xScale, yScale};
}
bool IsPointInPolygon(const std::vector<glm::vec2>& vertices,
const glm::vec2& point)
{
bool inPolygon = true;
// For each vertex, assume counterclockwise order
for (std::size_t i = 0; i < vertices.size(); ++i)
{
const auto& p1 = vertices[i];
const auto& p2 =
(i == vertices.size() - 1) ? vertices[0] : vertices[i + 1];
// Test which side of edge point lies on
const float a = -(p2.y - p1.y);
const float b = p2.x - p1.x;
const float c = -(a * p1.x + b * p1.y);
const float d = a * point.x + b * point.y + c;
// If d < 0, the point is on the right-hand side, and outside of the
// polygon
if (d < 0)
{
inPolygon = false;
break;
}
}
return inPolygon;
}
glm::vec2 LatLongToScreenCoordinate(const QMapLibreGL::Coordinate& coordinate)
{
static constexpr double RAD2DEG_D = 180.0 / M_PI;

View file

@ -17,6 +17,18 @@ units::length::meters<double>
GetMapDistance(const QMapLibreGL::CustomLayerRenderParameters& params);
glm::mat4 GetMapMatrix(const QMapLibreGL::CustomLayerRenderParameters& params);
glm::vec2 GetMapScale(const QMapLibreGL::CustomLayerRenderParameters& params);
/**
* @brief Determine whether a point lies within a polygon
*
* @param [in] vertices Counterclockwise vertices
* @param [in] point Point to test
*
* @return Whether the point lies within the polygon
*/
bool IsPointInPolygon(const std::vector<glm::vec2>& vertices,
const glm::vec2& point);
glm::vec2 LatLongToScreenCoordinate(const QMapLibreGL::Coordinate& coordinate);
} // namespace maplibre