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);
}
bool DrawItem::RunMousePicking(
const QMapLibreGL::CustomLayerRenderParameters& /* params */)
{
// By default, the draw item is not picked
return false;
}
void DrawItem::UseDefaultProjection(
const QMapLibreGL::CustomLayerRenderParameters& params,
GLint uMVPMatrixLocation)

View file

@ -33,6 +33,16 @@ public:
bool textureAtlasChanged);
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:
void
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)
{
p->drawList_.push_back(drawItem);

View file

@ -18,9 +18,13 @@ public:
explicit DrawLayer(const std::shared_ptr<MapContext>& context);
virtual ~DrawLayer();
virtual void Initialize();
virtual void Render(const QMapLibreGL::CustomLayerRenderParameters&);
virtual void Deinitialize();
virtual void Initialize() override;
virtual void
Render(const QMapLibreGL::CustomLayerRenderParameters&) override;
virtual void Deinitialize() override;
virtual bool RunMousePicking(
const QMapLibreGL::CustomLayerRenderParameters& params) override;
protected:
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;
bool GenericLayer::RunMousePicking(
const QMapLibreGL::CustomLayerRenderParameters& /* params */)
{
// By default, the layer has nothing to pick
return false;
}
std::shared_ptr<MapContext> GenericLayer::context() const
{
return p->context_;

View file

@ -28,6 +28,16 @@ public:
virtual void Render(const QMapLibreGL::CustomLayerRenderParameters&) = 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:
std::shared_ptr<MapContext> context() const;

View file

@ -23,13 +23,14 @@ public:
~Impl() {}
std::weak_ptr<QMapLibreGL::Map> map_;
MapSettings settings_;
float pixelRatio_;
std::shared_ptr<view::RadarProductView> radarProductView_;
common::RadarProductGroup radarProductGroup_;
std::string radarProduct_;
int16_t radarProductCode_;
std::weak_ptr<QMapLibreGL::Map> map_;
MapSettings settings_;
float pixelRatio_;
std::shared_ptr<view::RadarProductView> radarProductView_;
common::RadarProductGroup radarProductGroup_;
std::string radarProduct_;
int16_t radarProductCode_;
QMapLibreGL::CustomLayerRenderParameters renderParameters_ {};
};
MapContext::MapContext(
@ -77,6 +78,11 @@ int16_t MapContext::radar_product_code() const
return p->radarProductCode_;
}
QMapLibreGL::CustomLayerRenderParameters MapContext::render_parameters() const
{
return p->renderParameters_;
}
void MapContext::set_map(std::shared_ptr<QMapLibreGL::Map> map)
{
p->map_ = map;
@ -109,6 +115,12 @@ void MapContext::set_radar_product_code(int16_t radarProductCode)
p->radarProductCode_ = radarProductCode;
}
void MapContext::set_render_parameters(
const QMapLibreGL::CustomLayerRenderParameters& params)
{
p->renderParameters_ = params;
}
} // namespace map
} // namespace qt
} // namespace scwx

View file

@ -26,13 +26,14 @@ public:
MapContext(MapContext&&) noexcept;
MapContext& operator=(MapContext&&) noexcept;
std::weak_ptr<QMapLibreGL::Map> map() const;
MapSettings& settings();
float pixel_ratio() const;
std::shared_ptr<view::RadarProductView> radar_product_view() const;
common::RadarProductGroup radar_product_group() const;
std::string radar_product() const;
int16_t radar_product_code() const;
std::weak_ptr<QMapLibreGL::Map> map() const;
MapSettings& settings();
float pixel_ratio() const;
std::shared_ptr<view::RadarProductView> radar_product_view() const;
common::RadarProductGroup radar_product_group() const;
std::string radar_product() const;
int16_t radar_product_code() const;
QMapLibreGL::CustomLayerRenderParameters render_parameters() const;
void set_map(std::shared_ptr<QMapLibreGL::Map> map);
void set_pixel_ratio(float pixelRatio);
@ -41,6 +42,8 @@ public:
void set_radar_product_group(common::RadarProductGroup radarProductGroup);
void set_radar_product(const std::string& radarProduct);
void set_radar_product_code(int16_t radarProductCode);
void set_render_parameters(
const QMapLibreGL::CustomLayerRenderParameters& params);
private:
class Impl;

View file

@ -126,6 +126,7 @@ public:
void RadarProductViewConnect();
void RadarProductViewDisconnect();
void RemovePlacefileLayer(const std::string& placefileName);
void RunMousePicking();
void SetRadarSite(const std::string& radarSite);
void UpdatePlacefileLayers();
bool UpdateStoredMapParameters();
@ -1022,6 +1023,9 @@ void MapWidget::paintGL()
size() * pixelRatio());
p->map_->render();
// Perform mouse picking
p->RunMousePicking();
// Render ImGui Frame
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
@ -1030,6 +1034,25 @@ void MapWidget::paintGL()
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)
{
switch (mapChange)

View file

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