ppizarror / pygame-menu

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

open submenu in if statement? #318

Closed ojaap closed 3 years ago

ojaap commented 3 years ago

Hello! I hope i don't bother you with this question, but I think i understand something fundamentally wrong.

In a submenu a got a textfield, a start button, and a return button. After pressing start it should check if there is already a folder with this name. If yes, it should open a submenu to ask, if you want to override. But it was hard to start this menu without a button or something else, and my solution that works best till now isn't realy working, there are big problems with the return button, but else it got even worse. In the new_menu it will jump back to the override_menu (obviously) and not the main_menu. There must be a better way to open a submenu in such a situation, but i don't find it.

Here my example code

import pygame
import pygame_menu
import os

pygame.init()
surface = None
name = 'Test'
size = None

def start():
    global name
    global surface
    global size

    surface = pygame.display.set_mode((0, 0), pygame.FULLSCREEN | pygame.DOUBLEBUF)
    size = pygame.display.get_window_size()

    def renew_name(value):
        global name
        name = value

    def start_generator():
        global name
        if not os.path.isdir('./' + name):
            generator.generator()
        else:
            override_menu.mainloop(surface)
            pass

    override_menu = pygame_menu.Menu(
        title="override?",
        height=size[1] / 100 * 30,
        width=size[0] / 100 * 20,
        theme=pygame_menu.themes.THEME_DARK
    )
    new_menu = pygame_menu.Menu(
        title="start new world",
        height=size[1] / 100 * 30,
        width=size[0] / 100 * 20,
        theme=pygame_menu.themes.THEME_DARK
    )
    main_menu = pygame_menu.Menu(
        title="Welcome",
        height=size[1] / 100 * 30,
        width=size[0] / 100 * 20,
        theme=pygame_menu.themes.THEME_DARK
    )

    override_menu.add_button('Override',)
    override_menu.add.button('Return', new_menu)

    new_menu.add.text_input('Name:', onchange=renew_name, textinput_id='start_name')
    new_menu.add.button('start', start_generator)
    new_menu.add.button('return', pygame_menu.events.BACK)

    main_menu.add.button('New', new_menu)

    while True:

        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                exit()

        if main_menu.is_enabled():
            main_menu.mainloop(surface)

        pygame.display.flip()

if __name__ == '__main__':
    start()

Could you please help? What is the elegant way in such a situation? Generally, this is a nice lib, thanks for the good work :)

ppizarror commented 3 years ago

Hi, what if you solve this issue by using https://pygame-menu.readthedocs.io/en/4.0.2/_source/add_widgets.html#add-a-menu-link?

Here's an example that opens the override menu if "epic" is within the name; I got to remove some things as pygame v2 is not supported yet on m1 macs.

import pygame
import pygame_menu
import os

pygame.init()
surface = None
name = 'Test'
size = None

def start():
    global name
    global surface
    global size
    global override_link

    surface = pygame.display.set_mode((0,0), pygame.DOUBLEBUF)
    # size = pygame.display.get_window_size()
    size = (1920, 1080)

    def renew_name(value):
        global name
        name = value

    def start_generator():
        global name
        if 'epic' in name:
            override_link.open()

    override_menu = pygame_menu.Menu(
        title="override?",
        height=size[1] / 100 * 30,
        width=size[0] / 100 * 20,
        theme=pygame_menu.themes.THEME_DARK
    )
    new_menu = pygame_menu.Menu(
        title="start new world",
        height=size[1] / 100 * 30,
        width=size[0] / 100 * 20,
        theme=pygame_menu.themes.THEME_DARK
    )
    main_menu = pygame_menu.Menu(
        title="Welcome",
        height=size[1] / 100 * 30,
        width=size[0] / 100 * 20,
        theme=pygame_menu.themes.THEME_DARK
    )

    override_link = new_menu.add.menu_link(override_menu)

    override_menu.add_button('Override',)
    override_menu.add.button('Return', pygame_menu.events.BACK)

    new_menu.add.text_input('Name:', onchange=renew_name, textinput_id='start_name')
    new_menu.add.button('start', start_generator)
    new_menu.add.button('return', pygame_menu.events.BACK)

    main_menu.add.button('New', new_menu)

    while True:

        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                exit()

        if main_menu.is_enabled():
            main_menu.mainloop(surface)

        pygame.display.flip()

if __name__ == '__main__':
    start()

Menu links can open any menu through the .open() method. This modifies the state of the menu without adding additional buttons. In this example, the generator calls the link to open the given menu, and that overridden menu adds a button to go back to the previous executed menu state.

If the documentation is not clear enough or more examples are needed, please feel free to create an example to be added in the library docs :)

Let me know if this approach solves your problem.

ojaap commented 3 years ago

This is exactly what i have thought of! Thank you very much and have a nice day!