ppizarror / pygame-menu

A menu for pygame. Simple, and easy to use
https://pygame-menu.readthedocs.io/
Other
544 stars 141 forks source link

Mouse-selection bug #463

Closed OverLeo007 closed 1 year ago

OverLeo007 commented 1 year ago

Environment Information

Describe the bug When using Pygame-menu as a sidebar on a subSurface, the mouse-hover function is working when the mouse is completely outside the menu's area, but inside another Surface's area.

To Reproduce

import pygame as pg
import pygame_menu as pgm

pg.init()
w, h = 500, 500
left_surf_w, left_surf_h = 300, h
menu_w, menu_h = 200, h

main_surface = pg.display.set_mode((w, h))
left_surface = main_surface.subsurface((0, 0, left_surf_w, left_surf_h))
menu_surface = main_surface.subsurface((300, 0, menu_w, menu_h))
menu = pgm.Menu("Bug menu", menu_w, menu_h,
                mouse_motion_selection=True,
                theme=pgm.themes.THEME_SOLARIZED,
                position=(0, 0))
menu.add.button("Button")
menu.add.button("Button")
menu.add.button("Button")
menu.add.button("Button")
menu.add.button("Button")

while True:
    events = pg.event.get()
    for event in events:
        if event.type == pg.QUIT:
            pg.quit()
            exit()
    menu.update(events)
    menu.draw(menu_surface)
    left_surface.fill((255, 255, 0))
    pg.display.flip()

Expected Behavior The mouse-hover function should only work when the mouse is within the area of the menu surface, and not inside another surface's area.

Additional Context When creating a sidebar with Pygame-menu on a separate Surface, it appears that the mouse-hover function is not working as intended. When the mouse hovers over a different Surface that overlaps with the menu's position, the mouse-hover function still activates.

ppizarror commented 1 year ago

Hi, I never considered that a Menu would be drawn on a subsurface; thus, the (300, 0) offset was not taken into account while retrieving the widget's real position within the window.

Thus, #464 implements a menu surface value, which now remembers the last offset value used to draw the Menu. Your example should work without any change; also, providing the surface value to the Menu constructor also helps:

import pygame as pg
import pygame_menu as pgm

pg.init()
w, h = 500, 500
left_surf_w, left_surf_h = 300, h
menu_w, menu_h = 200, h

main_surface = pg.display.set_mode((w, h))
left_surface = main_surface.subsurface((0, 0, left_surf_w, left_surf_h))
menu_surface = main_surface.subsurface((300, 0, menu_w, menu_h))
menu = pgm.Menu("Bug menu", menu_w, menu_h,
                mouse_motion_selection=True,
                theme=pgm.themes.THEME_SOLARIZED,
                position=(0, 0),
                surface=menu_surface)
menu.add.button("Button")
menu.add.button("Button")
menu.add.button("Button")
menu.add.button("Button")
menu.add.button("Button")

while True:
    events = pg.event.get()
    for event in events:
        if event.type == pg.QUIT:
            pg.quit()
            exit()
    menu.update(events)
    menu.draw()
    left_surface.fill((255, 255, 0))
    pg.display.flip()

Let me know if the new PR solves your issue

OverLeo007 commented 1 year ago

Thank you for quick reply! Now, mouse hover on the menu is displayed correctly. However, I'm still facing the problem with clicks that are only registered within the left_surface and not on the actual menu.

ppizarror commented 1 year ago

Thank you for quick reply! Now, mouse hover on the menu is displayed correctly. However, I'm still facing the problem with clicks that are only registered within the left_surface and not on the actual menu.

Hi, I forgot to propagate the offset to the events. See the updated PR, which now works for all widgets 😄

OverLeo007 commented 1 year ago

Yes, everything is working now. Thank you! Now, I can present a quality practical work to my teacher 🤩