MyreMylar / pygame_gui

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

Clicking close button of UIWindow makes it unable to be toggled #471

Closed bennett-nguyen closed 1 year ago

bennett-nguyen commented 1 year ago

Describe the bug I'm fairly new to this library, but I'll try to explain the bug as concise as possible. I'm implementing a settings window where you can toggle/untoggle it via pressing the Q key on the keyboard, you can also untoggle the settings window via its close button. However, after I clicked the window's close button, I'm not able to toggle it anymore.

To Reproduce Implement this code:

import pygame as pg
import pygame_gui as pg_gui

pg.init()

WIDTH: int = 1300
HEIGHT: int = 700
RESOLUTION = (WIDTH, HEIGHT)

HALF_WIDTH: int = WIDTH//2
HALF_HEIGHT: int = HEIGHT//2

WINDOW_WIDTH: int = 700
WINDOW_HEIGHT: int = 500    
WINDOW_RESOLUTION = (WINDOW_WIDTH, WINDOW_HEIGHT)

screen = pg.display.set_mode(RESOLUTION)
clock = pg.time.Clock()
manager = pg_gui.UIManager(RESOLUTION)

window_rect = pg.Rect((0, 0), WINDOW_RESOLUTION)
window_rect.center = (HALF_WIDTH, HALF_HEIGHT)

settings_window = pg_gui.elements.UIWindow(
    window_rect,
    manager=manager,
    window_display_title="Settings",
)

while True:
    dt = clock.tick(60) / 1000.0
    screen.fill("white")

    for event in pg.event.get():
        if event.type == pg.QUIT:
            pg.quit()
            exit(0)

        if event.type == pg.KEYDOWN and event.key == pg.K_q:
            print("pressed q")
            if settings_window.visible:
                settings_window.hide()
            else:
                settings_window.show()

        if event.type == pg_gui.UI_WINDOW_CLOSE:
            if event.ui_element == settings_window:
                settings_window.hide()

        manager.process_events(event)

    manager.update(dt)
    manager.draw_ui(screen)

    pg.display.update()

Expected behaviour I'm supposed to be able to toggle it after I closed it.

Screenshots ScFpu.gif

Platform and software:

MyreMylar commented 1 year ago

See:

def on_close_window_button_pressed(self):
    """
    Override this method to call 'hide()' instead if you want to hide a window when the
    close button is pressed.
    """
    self.kill()

https://github.com/MyreMylar/pygame_gui/blob/7f26d3b44aee4c655869e232d86677889b3b7ab7/pygame_gui/elements/ui_window.py#L420C5-L425C20

To be clear:

You would do:


class SettingsWindow(pg_gui.elements.UIWindow):
    def on_close_window_button_pressed(self):
        self.hide()

settings_window = SettingsWindow(
    window_rect,
    manager=manager,
    window_display_title="Settings",
)

And remove the part in the event handler responding to the UI_WINDOW_CLOSE event.

bennett-nguyen commented 1 year ago

Does this also apply to other UI components of the API? Also, I think you should highlight this notice in the docs so it doesn't cause any confusion.