Fixing placefile line hover offsets

This commit is contained in:
Dan Paulat 2023-08-28 23:09:05 -05:00
parent 8dfb9f1105
commit 5bb5093579

View file

@ -34,10 +34,14 @@ class PlacefileLines::Impl
public: public:
struct LineHoverEntry struct LineHoverEntry
{ {
std::string hoverText_; std::shared_ptr<const gr::Placefile::LineDrawItem> di_;
glm::vec2 p1_; glm::vec2 p1_;
glm::vec2 p2_; glm::vec2 p2_;
glm::mat2 rotate_; glm::vec2 otl_;
glm::vec2 otr_;
glm::vec2 obl_;
glm::vec2 obr_;
float width_; float width_;
}; };
@ -56,13 +60,14 @@ public:
~Impl() {} ~Impl() {}
void BufferLine(const gr::Placefile::LineDrawItem::Element& e1, void BufferLine(const std::shared_ptr<const gr::Placefile::LineDrawItem>& di,
const gr::Placefile::LineDrawItem::Element& e1,
const gr::Placefile::LineDrawItem::Element& e2, const gr::Placefile::LineDrawItem::Element& e2,
const float width, const float width,
const units::angle::degrees<double> angle, const units::angle::degrees<double> angle,
const boost::gil::rgba8_pixel_t color, const boost::gil::rgba8_pixel_t color,
const GLint threshold, const GLint threshold,
const std::string& hoverText = {}); bool bufferHover = false);
void void
UpdateBuffers(const std::shared_ptr<const gr::Placefile::LineDrawItem>& di); UpdateBuffers(const std::shared_ptr<const gr::Placefile::LineDrawItem>& di);
void Update(); void Update();
@ -298,7 +303,7 @@ bool PlacefileLines::RunMousePicking(
// Calculate map scale, remove width and height from original calculation // Calculate map scale, remove width and height from original calculation
glm::vec2 scale = util::maplibre::GetMapScale(params); glm::vec2 scale = util::maplibre::GetMapScale(params);
scale = 1.0f / glm::vec2 {scale.x * params.width, scale.y * params.height}; scale = 2.0f / glm::vec2 {scale.x * params.width, scale.y * params.height};
// Scale and rotate the identity matrix to create the map matrix // Scale and rotate the identity matrix to create the map matrix
glm::mat4 mapMatrix {1.0f}; glm::mat4 mapMatrix {1.0f};
@ -317,19 +322,12 @@ bool PlacefileLines::RunMousePicking(
glm::vec2 tr = tl; glm::vec2 tr = tl;
// Calculate offsets // Calculate offsets
// - Offset is half the line width (pixels) in each direction // - Pre-rotated offset is half the line width (pixels) in each direction
// - Rotate the offset at each vertex // - Multiply the offset by the scaled and rotated map matrix
// - Multiply the offset by the map matrix const glm::vec2 otl = mapMatrix * glm::vec4 {line.otl_, 0.0f, 1.0f};
const float hw = line.width_ * 0.5f; const glm::vec2 obl = mapMatrix * glm::vec4 {line.obl_, 0.0f, 1.0f};
const glm::vec2 otl = const glm::vec2 obr = mapMatrix * glm::vec4 {line.obr_, 0.0f, 1.0f};
mapMatrix * const glm::vec2 otr = mapMatrix * glm::vec4 {line.otr_, 0.0f, 1.0f};
glm::vec4 {line.rotate_ * glm::vec2 {-hw, -hw}, 0.0f, 1.0f};
const glm::vec2 obl =
mapMatrix * glm::vec4 {line.rotate_ * glm::vec2 {-hw, hw}, 0.0f, 1.0f};
const glm::vec2 obr =
mapMatrix * glm::vec4 {line.rotate_ * glm::vec2 {hw, hw}, 0.0f, 1.0f};
const glm::vec2 otr =
mapMatrix * glm::vec4 {line.rotate_ * glm::vec2 {hw, -hw}, 0.0f, 1.0f};
// Offset vertices // Offset vertices
tl += otl; tl += otl;
@ -343,7 +341,7 @@ bool PlacefileLines::RunMousePicking(
if (IsPointInPolygon({tl, bl, br, tr}, mousePos)) if (IsPointInPolygon({tl, bl, br, tr}, mousePos))
{ {
itemPicked = true; itemPicked = true;
DrawTooltip(line.hoverText_); DrawTooltip(line.di_->hoverText_);
break; break;
} }
} }
@ -419,13 +417,14 @@ void PlacefileLines::Impl::UpdateBuffers(
angles.push_back(angle); angles.push_back(angle);
// Buffer line with hover text // Buffer line with hover text
BufferLine(di->elements_[i], BufferLine(di,
di->elements_[i],
di->elements_[i + 1], di->elements_[i + 1],
di->width_ + 2, di->width_ + 2,
angle, angle,
kBlack_, kBlack_,
thresholdValue, thresholdValue,
di->hoverText_); true);
} }
// For each element pair inside a Line statement, render a colored line // For each element pair inside a Line statement, render a colored line
@ -433,7 +432,8 @@ void PlacefileLines::Impl::UpdateBuffers(
{ {
auto angle = angles[i]; auto angle = angles[i];
BufferLine(di->elements_[i], BufferLine(di,
di->elements_[i],
di->elements_[i + 1], di->elements_[i + 1],
di->width_, di->width_,
angle, angle,
@ -443,13 +443,14 @@ void PlacefileLines::Impl::UpdateBuffers(
} }
void PlacefileLines::Impl::BufferLine( void PlacefileLines::Impl::BufferLine(
const std::shared_ptr<const gr::Placefile::LineDrawItem>& di,
const gr::Placefile::LineDrawItem::Element& e1, const gr::Placefile::LineDrawItem::Element& e1,
const gr::Placefile::LineDrawItem::Element& e2, const gr::Placefile::LineDrawItem::Element& e2,
const float width, const float width,
const units::angle::degrees<double> angle, const units::angle::degrees<double> angle,
const boost::gil::rgba8_pixel_t color, const boost::gil::rgba8_pixel_t color,
const GLint threshold, const GLint threshold,
const std::string& hoverText) bool bufferHover)
{ {
// Latitude and longitude coordinates in degrees // Latitude and longitude coordinates in degrees
const float lat1 = static_cast<float>(e1.latitude_); const float lat1 = static_cast<float>(e1.latitude_);
@ -494,7 +495,7 @@ void PlacefileLines::Impl::BufferLine(
newThresholdBuffer_.end(), newThresholdBuffer_.end(),
{threshold, threshold, threshold, threshold, threshold, threshold}); {threshold, threshold, threshold, threshold, threshold, threshold});
if (!hoverText.empty()) if (bufferHover && !di->hoverText_.empty())
{ {
const units::angle::radians<double> radians = angle; const units::angle::radians<double> radians = angle;
@ -506,8 +507,13 @@ void PlacefileLines::Impl::BufferLine(
const glm::mat2 rotate {cosAngle, -sinAngle, sinAngle, cosAngle}; const glm::mat2 rotate {cosAngle, -sinAngle, sinAngle, cosAngle};
const glm::vec2 otl = rotate * glm::vec2 {-hw, +hw};
const glm::vec2 otr = rotate * glm::vec2 {+hw, +hw};
const glm::vec2 obl = rotate * glm::vec2 {-hw, -hw};
const glm::vec2 obr = rotate * glm::vec2 {+hw, -hw};
newHoverLines_.emplace_back( newHoverLines_.emplace_back(
LineHoverEntry {hoverText, sc1, sc2, rotate, width}); LineHoverEntry {di, sc1, sc2, otl, otr, obl, obr, width});
} }
} }