MyreMylar / pygame_gui

A GUI system for pygame.
MIT License
698 stars 83 forks source link

Boolean which keeps track if mouse is over any GUI element #152

Closed void4 closed 2 years ago

void4 commented 4 years ago

Is your feature request related to a problem? Please describe. I'm implementing some custom movable UI elements unrelated to the pygame_gui hierarchy, but I only want them triggered when the mouse is clicked while not over any pygame_gui elements.

Describe the solution you'd like Either a pygame_gui.mouseOverGUI - style module variable or possibly even better pygame_gui.is_point_over_gui(xy) to which the user then can pass the result of pygame.mouse.get_pos()

Perhaps uimanager.process_events(event) could return this boolean, too

Describe alternatives you've considered Manually tracking all pygame_gui events isn't elegant, exhausting and possibly not complete

manager.focused_element is only not None when something has already been clicked on

manager.get_root_container().is_focused might actually work, but it also appears to be only updated on clicks

MyreMylar commented 4 years ago

Hmm... you could probably just override the UIManager's process_events() method in a child UIManager class and make it return the consumed_event variable instead of nothing. that way you'd know if a given event had been consumed by one of the pygame_gui elements already.

That's probably useful general behaviour for pygame_gui too, though I might have forgotten some reason why it is the way it is right now.

mohitcodess commented 3 years ago
def is_mouse_on_ui_element(manager_height,manager_width,manager):
    #height and width of the manager will be same as display dimensions
    pos=pygame.mouse.get_pos()
    for sprite_surface in manager.ui_group:
        if sprite_surface.get_relative_rect().width==manager_width and sprite_surface.get_relative_rect().height==manager_height:
            continue  
        if sprite_surface.get_relative_rect().collidepoint(pos) and sprite_surface.visible:
            return True
    return False