示例#1
0
    def __init__(self,
                 widget_id='',
                 onchange=None,
                 onreturn=None,
                 args=None,
                 kwargs=None):
        """
        :param widget_id: Widget identifier
        :type widget_id: basestring
        :param onchange: Callback when changing the selector
        :type onchange: function, NoneType
        :param onreturn: Callback when pressing return button
        :type onreturn: function, NoneType
        :param args: Optional arguments for callbacks
        :param kwargs: Optional keyword-arguments for callbacks
        """

        # Store id, if None or empty create new ID based on UUID
        if widget_id is None or len(widget_id) == 0:
            widget_id = uuid4()
        self._id = str(widget_id)
        self._surface = None  # Rendering surface
        self._render_string_cache = 0
        self._render_string_cache_surface = None
        self._rect = _pygame.Rect(0, 0, 0, 0)
        self._alignment = _locals.PYGAME_ALIGN_CENTER
        self._fps = 0

        self._on_change = onchange
        self._on_return = onreturn
        self._args = args or []
        self._kwargs = kwargs or {}

        # Modified in set_font() method
        self._font = _cfg.MENU_FONT_SIZE_TITLE
        self._font_size = _cfg.MENU_FONT_SIZE
        self._font_color = _cfg.MENU_FONT_COLOR
        self._font_selected_color = _cfg.MENU_SELECTEDCOLOR
        self._font_antialias = True

        # Text shadow
        self._shadow = _cfg.MENU_OPTION_SHADOW
        self._shadow_color = _cfg.MENU_SHADOW_COLOR
        self._shadow_offset = _cfg.MENU_SHADOW_OFFSET
        self._shadow_position = _cfg.MENU_SHADOW_POSITION
        self._create_shadow_tuple()

        # Public attributs
        self.joystick_enabled = True
        self.mouse_enabled = True
        self.selected = False
        self.sound = _Sound()
示例#2
0
    def set_sound(self, sound, recursive=False):
        """
        Set sound engine to a menu.

        :param sound: Sound object
        :type sound: pygameMenu.sound.Sound, NoneType
        :param recursive: Set the sound engine to all submenus
        :type recursive: bool
        :return: None
        """
        assert isinstance(sound, (type(self._sounds), type(None)))
        if sound is None:
            sound = _Sound()
        self._sounds = sound
        for widget in self._option:
            widget.set_sound(sound)
        if recursive:
            for menu in self._submenus:
                menu.set_sound(sound, recursive=True)
示例#3
0
    def __init__(self,
                 surface,
                 window_width,
                 window_height,
                 font,
                 title,
                 back_box=True,
                 bgfun=None,
                 color_selected=_cfg.MENU_SELECTEDCOLOR,
                 dopause=True,
                 draw_region_x=_cfg.MENU_DRAW_X,
                 draw_region_y=_cfg.MENU_DRAW_Y,
                 draw_select=_cfg.MENU_SELECTED_DRAW,
                 enabled=True,
                 font_color=_cfg.MENU_FONT_COLOR,
                 font_size=_cfg.MENU_FONT_SIZE,
                 font_size_title=_cfg.MENU_FONT_SIZE_TITLE,
                 font_title=None,
                 fps=0,
                 joystick_enabled=True,
                 menu_alpha=_cfg.MENU_ALPHA,
                 menu_color=_cfg.MENU_BGCOLOR,
                 menu_color_title=_cfg.MENU_TITLE_BG_COLOR,
                 menu_height=_cfg.MENU_HEIGHT,
                 menu_width=_cfg.MENU_WIDTH,
                 mouse_enabled=True,
                 mouse_visible=True,
                 onclose=None,
                 option_margin=_cfg.MENU_OPTION_MARGIN,
                 option_shadow=_cfg.MENU_OPTION_SHADOW,
                 option_shadow_offset=_cfg.MENU_SHADOW_OFFSET,
                 option_shadow_position=_cfg.MENU_SHADOW_POSITION,
                 rect_width=_cfg.MENU_SELECTED_WIDTH,
                 title_offsetx=0,
                 title_offsety=0,
                 widget_alignment=_locals.ALIGN_CENTER):
        """
        Menu constructor.

        :param surface: Pygame surface
        :type surface: pygame.surface.SurfaceType
        :param window_width: Window width size (px)
        :type window_width: int
        :param window_height: Window height size (px)
        :type window_height: int
        :param font: Font file path
        :type font: basestring
        :param title: Title of the menu (main title)
        :type title: basestring
        :param back_box: Draw a back-box button on header
        :type back_box: bool
        :param bgfun: Background drawing function (only if menu pause app)
        :type bgfun: function
        :param color_selected: Color of selected item
        :type color_selected: tuple
        :param dopause: Pause game
        :type dopause: bool
        :param draw_region_x: Drawing position of element inside menu (x-axis)
        :type draw_region_x: int
        :param draw_region_y: Drawing position of element inside menu (y-axis)
        :type draw_region_y: int
        :param draw_select: Draw a rectangle around selected item (bool)
        :type draw_select: bool
        :param enabled: Menu is enabled by default or not
        :type enabled: bool
        :param fps: FPS of the menu
        :type fps: int, float
        :param font_color: Color of font
        :type font_color: tuple
        :param font_size: Font size
        :type font_size: int
        :param font_size_title: Font size of the title
        :type font_size_title: int
        :param font_title: Alternative font of the title (file path)
        :type font_title: basestring
        :param joystick_enabled: Enable/disable joystick on menu
        :type joystick_enabled: bool
        :param menu_alpha: Alpha of background (0=transparent, 100=opaque)
        :type menu_alpha: int
        :param menu_color: Menu color
        :type menu_color: tuple
        :param menu_color_title: Background color of title
        :type menu_color_title: tuple
        :param menu_height: Height of menu (px)
        :type menu_height: int
        :param menu_width: Width of menu (px)
        :type menu_width: int
        :param mouse_enabled: Enable/disable mouse click on menu
        :type mouse_enabled: bool
        :param mouse_visible: Set mouse visible on menu
        :type mouse_visible: bool
        :param onclose: Function applied when closing the menu
        :type onclose: function, NoneType
        :param option_margin: Margin of each element in menu (px)
        :type option_margin: int
        :param option_shadow: Indicate if a shadow is drawn on each option
        :type option_shadow: bool
        :param option_shadow_offset: Offset of shadow
        :type option_shadow_offset: int
        :param option_shadow_position: Position of shadow
        :type option_shadow_position: basestring
        :param rect_width: Border with of rectangle around selected item
        :type rect_width: int
        :param title_offsetx: Offset x-position of title (px)
        :type title_offsetx: int
        :param title_offsety: Offset y-position of title (px)
        :type title_offsety: int
        :param widget_alignment: Default widget alignment
        :type widget_alignment: basestring
        """
        assert isinstance(window_width, int)
        assert isinstance(window_height, int)
        assert isinstance(font, str)
        assert isinstance(title, str)

        assert isinstance(back_box, bool)
        assert isinstance(color_selected, tuple)
        assert isinstance(dopause, bool)
        assert isinstance(draw_region_x, int)
        assert isinstance(draw_region_y, int)
        assert isinstance(draw_select, bool)
        assert isinstance(enabled, bool)
        assert isinstance(font_color, tuple)
        assert isinstance(font_size, int)
        assert isinstance(font_size_title, int)
        assert isinstance(font_title, (str, type(None)))
        assert isinstance(joystick_enabled, bool)
        assert isinstance(menu_alpha, int)
        assert isinstance(menu_color, tuple)
        assert isinstance(menu_color_title, tuple)
        assert isinstance(menu_height, int)
        assert isinstance(menu_width, int)
        assert isinstance(mouse_enabled, bool)
        assert isinstance(mouse_visible, bool)
        assert isinstance(option_margin, int)
        assert isinstance(option_shadow, bool)
        assert isinstance(option_shadow_offset, int)
        assert isinstance(option_shadow_position, str)
        assert isinstance(rect_width, int)

        # Other asserts
        if dopause:
            assert callable(bgfun), \
                'Bgfun must be a function (or None if menu does not pause ' \
                'execution of the application)'
        else:
            assert isinstance(bgfun, type(None)), \
                'Bgfun must be None if menu does not pause execution of the application'
        assert dopause and bgfun is not None or not dopause and bgfun is None, \
            'If pause main execution is enabled then bgfun (Background ' \
            'function drawing) must be defined (not None)'
        assert draw_region_y >= 0 and draw_region_x >= 0, \
            'Drawing regions must be greater or equal than zero'
        assert font_size > 0 and font_size_title > 0, \
            'Font sizes must be greater than zero'
        assert menu_width > 0 and menu_height > 0, \
            'Menu size must be greater than zero'
        assert 0 <= menu_alpha <= 100, \
            'menu_alpha must be between 0 and 100 (both values included)'
        assert option_margin >= 0, \
            'Option margin must be greater or equal than zero'
        assert rect_width >= 0, 'rect_width must be greater or equal than zero'
        assert window_height > 0 and window_width > 0, \
            'Window size must be greater than zero'

        # Store configuration
        self._bgfun = bgfun
        self._bgcolor = (menu_color[0], menu_color[1], menu_color[2],
                         int(255 * (1 - (100 - menu_alpha) / 100.0)))

        self._drawselrect = draw_select
        self._font_color = font_color
        self._fsize = font_size
        self._height = menu_height
        self._opt_dy = option_margin
        self._option_shadow = option_shadow
        self._option_shadow_offset = option_shadow_offset
        self._option_shadow_position = option_shadow_position
        self._rect_width = rect_width
        self._sel_color = color_selected
        self._surface = surface
        self._width = menu_width

        # Inner variables
        self._actual = self  # Actual menu
        self._clock = _pygame.time.Clock()  # Inner clock
        self._closelocked = False  # Lock close until next mainloop
        self._dopause = dopause  # Pause or not
        self._enabled = enabled  # Menu is enabled or not
        self._fps = 0
        self._frame = 0
        self._index = 0  # Selected index
        self._onclose = onclose  # Function that calls after closing menu
        self._size = 0  # Menu total elements
        self._sounds = _Sound()

        # Menu widgets
        self._option = []  # type: list[_widgets.WidgetType]

        # Previous menu
        self._prev = None  # type: list[Menu]

        # Top level menu
        self._top = None  # type: Menu

        # List of all linked menus
        self._submenus = []  # type: list[Menu]

        # Load fonts
        self._font = _fonts.get_font(font, self._fsize)

        # Position of menu
        self._posx = (window_width - self._width) / 2
        self._posy = (window_height - self._height) / 2
        self._bgrect = [(self._posx, self._posy),
                        (self._posx + self._width, self._posy),
                        (self._posx + self._width, self._posy + self._height),
                        (self._posx, self._posy + self._height)]
        self._draw_regionx = draw_region_x
        self._draw_regiony = draw_region_y

        # Option position
        self._opt_posx = int(self._width *
                             (self._draw_regionx / 100.0)) + self._posx
        self._opt_posy = int(self._height *
                             (self._draw_regiony / 100.0)) + self._posy
        self._widget_align = widget_alignment

        # Init joystick
        self._joystick = joystick_enabled
        if self._joystick and not _pygame.joystick.get_init():
            _pygame.joystick.init()
            for i in range(_pygame.joystick.get_count()):
                _pygame.joystick.Joystick(i).init()

        # Init mouse
        self._mouse = mouse_enabled and mouse_visible
        self._mouse_visible = mouse_visible

        # Create menu bar
        self._menubar = _widgets.MenuBar(title, self._width, back_box, None,
                                         self._back)
        self._menubar.set_title(title, title_offsetx, title_offsety)
        font_title = _fonts.get_font(font_title or font, font_size_title)
        bg_color_title = (menu_color_title[0], menu_color_title[1],
                          menu_color_title[2],
                          int(255 * (1 - (100 - menu_alpha) / 100.0)))
        self._menubar.set_font(font_title, font_size_title, bg_color_title,
                               self._font_color)
        self._menubar.set_shadow(enabled=self._option_shadow,
                                 color=_cfg.MENU_SHADOW_COLOR,
                                 position=self._option_shadow_position,
                                 offset=self._option_shadow_offset)
        self._menubar.set_controls(self._joystick, self._mouse)

        # Selected option
        self._selected_inflate_x = 16
        self._selected_inflate_y = 6

        # FPS of the menu
        self.set_fps(fps)