Skip to content

Allow moving polygon selectors independently #29027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ericpre
Copy link
Member

@ericpre ericpre commented Oct 27, 2024

PR summary

Allow moving polygon selectors independently:

import matplotlib.pyplot as plt
from matplotlib.widgets import PolygonSelector

fig, ax = plt.subplots()
fig.show()

selector = PolygonSelector(ax, lambda *args: None)
# Add three vertices
selector.verts = [(0.1, 0.4), (0.5, 0.9), (0.3, 0.2)]

selector2 = PolygonSelector(ax, lambda *args: None)
# Add three vertices
selector2.verts = [(0.5, 0.4), (1.0, 0.9), (0.8, 0.2)]

# Press shift and move selector when pointer is on the selector

PR checklist

@ericpre ericpre changed the title Move polygon only when the pointer is on the polygon Allow moving polygon selectors independently Oct 27, 2024
@ericpre ericpre mentioned this pull request Oct 27, 2024
7 tasks
@ericpre ericpre force-pushed the fix_polygon_ignore_event branch from 28fac64 to 0d10940 Compare October 27, 2024 18:17
'move_all' in self._state and self._eventpress and
# Allow `move_all` when not completed
# or if the event is contained in the polygon
(not self._selection_completed or self._contains(event))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work? If I have an inner an an outer polygon on top of each other, I assume, shift-click dragging somewhere in the inner polygon will still move both polygons?

Also, I have slight concerns that self._contains can be expensive and _onmove is called often, which may lead to noticable lag during dragging.

Overall, I suspect that the approach should be slightly different, e.g. determine the polygon to manipulate on mouse press - likely you should click a node or edge, and store that information for the subsequent _onmove.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work? If I have an inner an an outer polygon on top of each other, I assume, shift-click dragging somewhere in the inner polygon will still move both polygons?

When the pointer is over two overlapping polygons and on both of them, both will move - if the pointer is only over only one of them, then only that one will move. Not the best, but better than the current behaviour, where all selectors will regardless of where the pointer is.

Also, I have slight concerns that self._contains can be expensive and _onmove is called often, which may lead to noticable lag during dragging.

When I run the example above this is fine, but it may not scale well with the number of selectors... This could be moved to key_press handler but the potential performance issue may occur when pressing key.

Overall, I suspect that the approach should be slightly different, e.g. determine the polygon to manipulate on mouse press - likely you should click a node or edge, and store that information for the subsequent _onmove.

I don't think that the selector have a "selected" state, and once they are drawn, they are always selected. This may be the best approach in this case. Will it be appetite to use this approach?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the best, but better than the current behaviour, where all selectors will regardless of where the pointer is.

Agreed. One could do better, but it's an improvement over the current state.

When I run the example above this is fine, but it may not scale well with the number of selectors... This could be moved to key_press handler but the potential performance issue may occur when pressing key.

I'm more concerned on complex selector shapes. The point is that key_press is only executed once, but _onmove is a continuously updating function - it should be in the low-10ms regime, whereas, it would be ok in complex scenarios when key_press takes some 100ms.
Note: a quick timing of a path with 100 points takes ~ 70µs to contains-check, so this might be fast enough. Though I feel a "selected" state is architecturally cleaner than always checking whether the move event is in the polygon? (e.g. can it happen through glitches/fast movements that mouse and Polygon get out of sync and the mouse slips out of the polygon, which would result in loosing the grip).

I don't think that the selector have a "selected" state, and once they are drawn, they are always selected. This may be the best approach in this case. Will it be appetite to use this approach?

What is this? A "selected" state - yes I belive this makes sense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the "selected" state to the polygon selector - the handle are visible only when the polygon is selected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants