Mouse picking boilerplate

This commit is contained in:
Dan Paulat 2023-08-27 00:29:17 -05:00
parent c7487281ad
commit 38b56be7c4
10 changed files with 115 additions and 17 deletions

View file

@ -53,6 +53,13 @@ void DrawItem::Render(const QMapLibreGL::CustomLayerRenderParameters& params,
Render(params); Render(params);
} }
bool DrawItem::RunMousePicking(
const QMapLibreGL::CustomLayerRenderParameters& /* params */)
{
// By default, the draw item is not picked
return false;
}
void DrawItem::UseDefaultProjection( void DrawItem::UseDefaultProjection(
const QMapLibreGL::CustomLayerRenderParameters& params, const QMapLibreGL::CustomLayerRenderParameters& params,
GLint uMVPMatrixLocation) GLint uMVPMatrixLocation)

View file

@ -33,6 +33,16 @@ public:
bool textureAtlasChanged); bool textureAtlasChanged);
virtual void Deinitialize() = 0; virtual void Deinitialize() = 0;
/**
* @brief Run mouse picking on the draw item.
*
* @param [in] params Custom layer render parameters
*
* @return true if the draw item was picked, otherwise false
*/
virtual bool
RunMousePicking(const QMapLibreGL::CustomLayerRenderParameters& params);
protected: protected:
void void
UseDefaultProjection(const QMapLibreGL::CustomLayerRenderParameters& params, UseDefaultProjection(const QMapLibreGL::CustomLayerRenderParameters& params,

View file

@ -76,6 +76,26 @@ void DrawLayer::Deinitialize()
} }
} }
bool DrawLayer::RunMousePicking(
const QMapLibreGL::CustomLayerRenderParameters& params)
{
bool itemPicked = false;
// For each draw item in the draw list in reverse
for (auto it = p->drawList_.rbegin(); it != p->drawList_.rend(); ++it)
{
// Run mouse picking on each draw item
if ((*it)->RunMousePicking(params))
{
// If a draw item was picked, don't process additional items
itemPicked = true;
break;
}
}
return itemPicked;
}
void DrawLayer::AddDrawItem(const std::shared_ptr<gl::draw::DrawItem>& drawItem) void DrawLayer::AddDrawItem(const std::shared_ptr<gl::draw::DrawItem>& drawItem)
{ {
p->drawList_.push_back(drawItem); p->drawList_.push_back(drawItem);

View file

@ -18,9 +18,13 @@ public:
explicit DrawLayer(const std::shared_ptr<MapContext>& context); explicit DrawLayer(const std::shared_ptr<MapContext>& context);
virtual ~DrawLayer(); virtual ~DrawLayer();
virtual void Initialize(); virtual void Initialize() override;
virtual void Render(const QMapLibreGL::CustomLayerRenderParameters&); virtual void
virtual void Deinitialize(); Render(const QMapLibreGL::CustomLayerRenderParameters&) override;
virtual void Deinitialize() override;
virtual bool RunMousePicking(
const QMapLibreGL::CustomLayerRenderParameters& params) override;
protected: protected:
void AddDrawItem(const std::shared_ptr<gl::draw::DrawItem>& drawItem); void AddDrawItem(const std::shared_ptr<gl::draw::DrawItem>& drawItem);

View file

@ -26,6 +26,13 @@ GenericLayer::GenericLayer(std::shared_ptr<MapContext> context) :
} }
GenericLayer::~GenericLayer() = default; GenericLayer::~GenericLayer() = default;
bool GenericLayer::RunMousePicking(
const QMapLibreGL::CustomLayerRenderParameters& /* params */)
{
// By default, the layer has nothing to pick
return false;
}
std::shared_ptr<MapContext> GenericLayer::context() const std::shared_ptr<MapContext> GenericLayer::context() const
{ {
return p->context_; return p->context_;

View file

@ -28,6 +28,16 @@ public:
virtual void Render(const QMapLibreGL::CustomLayerRenderParameters&) = 0; virtual void Render(const QMapLibreGL::CustomLayerRenderParameters&) = 0;
virtual void Deinitialize() = 0; virtual void Deinitialize() = 0;
/**
* @brief Run mouse picking on the layer.
*
* @param [in] params Custom layer render parameters
*
* @return true if a draw item was picked, otherwise false
*/
virtual bool
RunMousePicking(const QMapLibreGL::CustomLayerRenderParameters& params);
protected: protected:
std::shared_ptr<MapContext> context() const; std::shared_ptr<MapContext> context() const;

View file

@ -30,6 +30,7 @@ public:
common::RadarProductGroup radarProductGroup_; common::RadarProductGroup radarProductGroup_;
std::string radarProduct_; std::string radarProduct_;
int16_t radarProductCode_; int16_t radarProductCode_;
QMapLibreGL::CustomLayerRenderParameters renderParameters_ {};
}; };
MapContext::MapContext( MapContext::MapContext(
@ -77,6 +78,11 @@ int16_t MapContext::radar_product_code() const
return p->radarProductCode_; return p->radarProductCode_;
} }
QMapLibreGL::CustomLayerRenderParameters MapContext::render_parameters() const
{
return p->renderParameters_;
}
void MapContext::set_map(std::shared_ptr<QMapLibreGL::Map> map) void MapContext::set_map(std::shared_ptr<QMapLibreGL::Map> map)
{ {
p->map_ = map; p->map_ = map;
@ -109,6 +115,12 @@ void MapContext::set_radar_product_code(int16_t radarProductCode)
p->radarProductCode_ = radarProductCode; p->radarProductCode_ = radarProductCode;
} }
void MapContext::set_render_parameters(
const QMapLibreGL::CustomLayerRenderParameters& params)
{
p->renderParameters_ = params;
}
} // namespace map } // namespace map
} // namespace qt } // namespace qt
} // namespace scwx } // namespace scwx

View file

@ -33,6 +33,7 @@ public:
common::RadarProductGroup radar_product_group() const; common::RadarProductGroup radar_product_group() const;
std::string radar_product() const; std::string radar_product() const;
int16_t radar_product_code() const; int16_t radar_product_code() const;
QMapLibreGL::CustomLayerRenderParameters render_parameters() const;
void set_map(std::shared_ptr<QMapLibreGL::Map> map); void set_map(std::shared_ptr<QMapLibreGL::Map> map);
void set_pixel_ratio(float pixelRatio); void set_pixel_ratio(float pixelRatio);
@ -41,6 +42,8 @@ public:
void set_radar_product_group(common::RadarProductGroup radarProductGroup); void set_radar_product_group(common::RadarProductGroup radarProductGroup);
void set_radar_product(const std::string& radarProduct); void set_radar_product(const std::string& radarProduct);
void set_radar_product_code(int16_t radarProductCode); void set_radar_product_code(int16_t radarProductCode);
void set_render_parameters(
const QMapLibreGL::CustomLayerRenderParameters& params);
private: private:
class Impl; class Impl;

View file

@ -126,6 +126,7 @@ public:
void RadarProductViewConnect(); void RadarProductViewConnect();
void RadarProductViewDisconnect(); void RadarProductViewDisconnect();
void RemovePlacefileLayer(const std::string& placefileName); void RemovePlacefileLayer(const std::string& placefileName);
void RunMousePicking();
void SetRadarSite(const std::string& radarSite); void SetRadarSite(const std::string& radarSite);
void UpdatePlacefileLayers(); void UpdatePlacefileLayers();
bool UpdateStoredMapParameters(); bool UpdateStoredMapParameters();
@ -1022,6 +1023,9 @@ void MapWidget::paintGL()
size() * pixelRatio()); size() * pixelRatio());
p->map_->render(); p->map_->render();
// Perform mouse picking
p->RunMousePicking();
// Render ImGui Frame // Render ImGui Frame
ImGui::Render(); ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
@ -1030,6 +1034,25 @@ void MapWidget::paintGL()
Q_EMIT WidgetPainted(); Q_EMIT WidgetPainted();
} }
void MapWidgetImpl::RunMousePicking()
{
const QMapLibreGL::CustomLayerRenderParameters params =
context_->render_parameters();
// For each layer in reverse
// TODO: All Generic Layers, not just Placefile Layers
for (auto it = placefileLayers_.rbegin(); it != placefileLayers_.rend();
++it)
{
// Run mouse picking for each layer
if ((*it)->RunMousePicking(params))
{
// If a draw item was picked, don't process additional layers
break;
}
}
}
void MapWidget::mapChanged(QMapLibreGL::Map::MapChange mapChange) void MapWidget::mapChanged(QMapLibreGL::Map::MapChange mapChange)
{ {
switch (mapChange) switch (mapChange)

View file

@ -95,6 +95,8 @@ void OverlayLayer::Render(
auto& settings = context()->settings(); auto& settings = context()->settings();
const float pixelRatio = context()->pixel_ratio(); const float pixelRatio = context()->pixel_ratio();
context()->set_render_parameters(params);
if (p->sweepTimeNeedsUpdate_ && radarProductView != nullptr) if (p->sweepTimeNeedsUpdate_ && radarProductView != nullptr)
{ {
const scwx::util::time_zone* currentZone; const scwx::util::time_zone* currentZone;