Fix click event z-order handling in PyScene
Changed click detection to properly respect z-index by: - Sorting ui_elements in-place when needed (same as render order) - Using reverse iterators to check highest z-index elements first - This ensures top-most elements receive clicks before lower ones 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
dcd1b0ca33
commit
9c8d6c4591
|
@ -31,13 +31,18 @@ void PyScene::do_mouse_input(std::string button, std::string type)
|
||||||
// Convert window coordinates to game coordinates using the viewport
|
// Convert window coordinates to game coordinates using the viewport
|
||||||
auto mousepos = game->windowToGameCoords(sf::Vector2f(unscaledmousepos));
|
auto mousepos = game->windowToGameCoords(sf::Vector2f(unscaledmousepos));
|
||||||
|
|
||||||
// Create a sorted copy by z-index (highest first)
|
// Only sort if z_index values have changed
|
||||||
std::vector<std::shared_ptr<UIDrawable>> sorted_elements(*ui_elements);
|
if (ui_elements_need_sort) {
|
||||||
std::sort(sorted_elements.begin(), sorted_elements.end(),
|
// Sort in ascending order (same as render)
|
||||||
[](const auto& a, const auto& b) { return a->z_index > b->z_index; });
|
std::sort(ui_elements->begin(), ui_elements->end(),
|
||||||
|
[](const auto& a, const auto& b) { return a->z_index < b->z_index; });
|
||||||
|
ui_elements_need_sort = false;
|
||||||
|
}
|
||||||
|
|
||||||
// Check elements in z-order (top to bottom)
|
// Check elements in reverse z-order (highest z_index first, top to bottom)
|
||||||
for (const auto& element : sorted_elements) {
|
// Use reverse iterators to go from end to beginning
|
||||||
|
for (auto it = ui_elements->rbegin(); it != ui_elements->rend(); ++it) {
|
||||||
|
const auto& element = *it;
|
||||||
if (!element->visible) continue;
|
if (!element->visible) continue;
|
||||||
|
|
||||||
if (auto target = element->click_at(sf::Vector2f(mousepos))) {
|
if (auto target = element->click_at(sf::Vector2f(mousepos))) {
|
||||||
|
|
Loading…
Reference in New Issue