Skip to content

Commit

Permalink
Updated GUIObjectsInPosition. Refs #13894
Browse files Browse the repository at this point in the history
  • Loading branch information
palvarezlopez committed Dec 19, 2023
1 parent 2e164f5 commit 9e79839
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 18 deletions.
59 changes: 43 additions & 16 deletions src/netedit/elements/GNEContour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,50 @@ GNEContour::drawDottedContourCircle(const GUIVisualizationSettings& s, const GUI
void
GNEContour::drawDottedContourGeometryPoints(const GUIVisualizationSettings& s, const GUIVisualizationSettings::Detail d,
const PositionVector& shape, GeometryPoint geometryPoints, double radius, const double scale, const double lineWidth) const {
// iterate over all geometry points
for (int i = 0; i < (int)shape.size(); i++) {
const bool first = (i == 0);
const bool last = ((i + 1) == (int)shape.size());
// check conditions
if ((geometryPoints == GeometryPoint::ALL) ||
(first && (geometryPoints == GeometryPoint::FROM)) ||
(last && (geometryPoints == GeometryPoint::TO)) ||
(!first && !last && (geometryPoints == GeometryPoint::MIDDLE))) {
// continue depending if we're checking position within geometry point or drawing dotted contour
if (s.drawForObjectUnderCursor) {
// build dotted contour
gObjectsInPosition.positionWithinGeometryPoint(myAC->getGUIGlObject(), myAC->getNet()->getViewNet()->getPositionInformation(), i, shape[i], (radius * scale));
} else if (!s.disableDottedContours && (d <= GUIVisualizationSettings::Detail::DottedContours) &&
gObjectsInPosition.isGeometryPointUnderCursor(myAC->getGUIGlObject(), i)) {
// get object
const auto &glObject = myAC->getGUIGlObject();
// declare distance
const auto scaledRadius = (radius * scale);
// check if we're calculating the mouse position over geometry or drawing dotted geometry
if (s.drawForObjectUnderCursor) {
// declare pos
const auto pos = myAC->getNet()->getViewNet()->getPositionInformation();
// iterate over all geometry points
for (int i = 0; i < (int)shape.size(); i++) {
const bool first = (i == 0);
const bool last = ((i + 1) == (int)shape.size());
// check conditions
if ((geometryPoints == GeometryPoint::ALL) ||
(first && (geometryPoints == GeometryPoint::FROM)) ||
(last && (geometryPoints == GeometryPoint::TO)) ||
(!first && !last && (geometryPoints == GeometryPoint::MIDDLE))) {
// check position within geometry
gObjectsInPosition.positionWithinGeometryPoint(glObject, pos, i, shape[i], scaledRadius);
}
}
// check if mouse is over shape
if (s.drawForObjectUnderCursor) {
// check position over shape
gObjectsInPosition.positionOverShape(glObject, pos, shape, scaledRadius);
}
} else if (!s.disableDottedContours && (d <= GUIVisualizationSettings::Detail::DottedContours)) {
// get all geometry points
const auto &geometryPointIndexes = gObjectsInPosition.getGeometryPoints(glObject);
// either draw geometry point indexes or pos over shape, but not together
if (geometryPointIndexes.size() > 0) {
// draw all geometry points
for (const auto &geometryPointIndex : geometryPointIndexes) {
// build dotted contour circle
buildDottedContourCircle(s, shape[geometryPointIndex], radius, scale);
// draw geometry point
drawDottedContour(s, GUIDottedGeometry::DottedContourType::MOVE, 0, lineWidth);
}
} else {
// check if draw dotted contour over shape
const auto &posOverShape = gObjectsInPosition.getPositionOverShape(glObject);
if (posOverShape != Position::INVALID) {
// build dotted contour circle
buildDottedContourCircle(s, shape[i], radius, scale);
buildDottedContourCircle(s, posOverShape, radius, scale);
// draw geometry point
drawDottedContour(s, GUIDottedGeometry::DottedContourType::MOVE, 0, lineWidth);
}
Expand Down
58 changes: 58 additions & 0 deletions src/utils/gui/settings/GUIObjectsInPosition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ GUIObjectsInPosition::positionWithinGeometryPoint(const GUIGlObject* GLObject, c
}


bool
GUIObjectsInPosition::positionOverShape(const GUIGlObject* GLObject, const Position &pos, const PositionVector &shape, const double radius) {
// check if mouse is over shape
const auto nearestPos = shape.positionAtOffset2D(shape.nearest_offset_to_point2D(pos));
if (pos.distanceSquaredTo2D(nearestPos) <= (radius * radius)) {
return addPositionOverShape(GLObject, nearestPos);
} else {
return false;
}
}


bool
GUIObjectsInPosition::positionWithinShape(const GUIGlObject* GLObject, const Position &pos, const PositionVector &shape) {
if (shape.around(pos)) {
Expand Down Expand Up @@ -122,6 +134,20 @@ GUIObjectsInPosition::getGeometryPoints(const GUIGlObject* GLObject) const {
}


const Position&
GUIObjectsInPosition::getPositionOverShape(const GUIGlObject* GLObject) const {
// avoid to insert duplicated elements
for (auto &elementLayer : myElementsUnderCursor) {
for (auto &element : elementLayer.second) {
if (element.object == GLObject) {
return element.posOverShape;
}
}
}
return Position::INVALID;
}


void
GUIObjectsInPosition::updateFrontElement(const GUIGlObject* GLObject) {
ObjectContainer frontElement(nullptr);
Expand Down Expand Up @@ -199,4 +225,36 @@ GUIObjectsInPosition::addGeometryPointUnderCursor(const GUIGlObject* GLObject, c
return true;
}


bool
GUIObjectsInPosition::addPositionOverShape(const GUIGlObject* GLObject, const Position &pos) {
// avoid to insert duplicated elements
for (auto &elementLayer : myElementsUnderCursor) {
for (auto &element : elementLayer.second) {
if (element.object == GLObject) {
if (element.posOverShape != Position::INVALID) {
return false;
} else {
element.posOverShape = pos;
return true;
}
return true;
}
}
}
// no element found then add it
const auto layer = dynamic_cast<const Shape*>(GLObject);
if (layer) {
auto &layerContainer = myElementsUnderCursor[layer->getShapeLayer() * -1];
auto it = layerContainer.insert(layerContainer.begin(), ObjectContainer(GLObject));
it->posOverShape = pos;
} else {
auto &layerContainer = myElementsUnderCursor[GLObject->getType() * -1];
auto it = layerContainer.insert(layerContainer.begin(), ObjectContainer(GLObject));
it->posOverShape = pos;
}
return true;
}


/****************************************************************************/
13 changes: 11 additions & 2 deletions src/utils/gui/settings/GUIObjectsInPosition.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class GUIObjectsInPosition {
/// @brief vector with geometry points
std::vector<int> geometryPoints;

/// @brief new position
Position newPosition;
/// @brief pos over shape
Position posOverShape = Position::INVALID;
};

/// @brief typedef
Expand All @@ -77,6 +77,9 @@ class GUIObjectsInPosition {
/// @brief check if mouse is within geometry point
bool positionWithinGeometryPoint(const GUIGlObject* GLObject, const Position &pos, const int index, const Position &center, const double radius);

/// @brief check if mouse is within geometry point
bool positionOverShape(const GUIGlObject* GLObject, const Position &pos, const PositionVector &shape, const double radius);

/// @brief check if mouse is within closed shapes (for filled shapes)
bool positionWithinShape(const GUIGlObject* GLObject, const Position &pos, const PositionVector &shape);

Expand All @@ -86,6 +89,9 @@ class GUIObjectsInPosition {
/// @brief get geometry points for the given glObject
const std::vector<int>& getGeometryPoints(const GUIGlObject* GLObject) const;

/// @brief get position over shape
const Position& getPositionOverShape(const GUIGlObject* GLObject) const;

/// @brief move front element in elements under cursor (currently used only in netedit)
void updateFrontElement(const GUIGlObject* GLObject);

Expand Down Expand Up @@ -123,6 +129,9 @@ class GUIObjectsInPosition {
/// @brief add geometryPoint into list of elements under cursor
bool addGeometryPointUnderCursor(const GUIGlObject* GLObject, const int newIndex);

/// @brief add position over shape
bool addPositionOverShape(const GUIGlObject* GLObject, const Position &pos);

private:
/// @brief set copy constructor private
GUIObjectsInPosition(const GUIObjectsInPosition&) = default;
Expand Down

0 comments on commit 9e79839

Please sign in to comment.