Skip to content

Commit

Permalink
Finished py3 conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ppizarror committed Jan 16, 2021
1 parent f11f8b0 commit f49bc51
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 75 deletions.
8 changes: 7 additions & 1 deletion pygame_menu/__pyinstaller/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@
"""

import os
from typing import List


def get_hook_dirs():
def get_hook_dirs() -> List[str]:
"""
Return hook dirs to PyInstaller.
:return: Hook dir list
"""
return [os.path.dirname(__file__)]
12 changes: 7 additions & 5 deletions pygame_menu/custom_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@

# noinspection PyUnresolvedReferences
from typing import Union, Dict, List, Tuple, Any, Callable, Sequence, Mapping, \
TYPE_CHECKING
TYPE_CHECKING, Optional

# Common types
ArgsType = Union[Sequence[Any], None]
ArgsType = Optional[Sequence[Any]]
CallbackType = Optional[Callable]
ColorType = Union[Tuple[int, int, int], Tuple[int, int, int, int]]
KwargsType = Union[Mapping[Any, Any], None]
KwargsType = Optional[Mapping[Any, Any]]
NumberType = Union[int, float]

# Vectors
Expand All @@ -52,14 +53,15 @@
VectorIntType = Union[Tuple[int, ...], List[int]]

# Tuples
TupleIntType = Tuple[int, ...]
Tuple2NumberType = Tuple[NumberType, NumberType]
Tuple2IntType = Tuple[int, int]
Tuple4Tuple2IntType = Tuple[Tuple2IntType, Tuple2IntType, Tuple2IntType, Tuple2IntType]

# Menu constructor types
MenuColumnMaxWidthType = Union[int, float, VectorType, None]
MenuColumnMaxWidthType = Optional[Union[int, float, VectorType]]
MenuColumnMinWidthType = Union[int, float, VectorType]
MenuRowsType = Union[int, VectorIntType, None]
MenuRowsType = Optional[Union[int, VectorIntType]]

# Other
PaddingType = Union[NumberType, List[NumberType],
Expand Down
4 changes: 2 additions & 2 deletions pygame_menu/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import os.path as path
import pygame.font as _font
from typing import Union
from typing import Union, Optional

# Available fonts path
__fonts_path__ = path.join(path.dirname(path.abspath(__file__)), 'resources', 'fonts', '{0}')
Expand Down Expand Up @@ -67,7 +67,7 @@ def get_font(name: Union[str, '_font.Font'], size: int) -> '_font.Font':
"""
assert isinstance(size, int)

font: Union['_font.Font', None]
font: Optional['_font.Font']
if isinstance(name, _font.Font):
font = name
return font
Expand Down
66 changes: 30 additions & 36 deletions pygame_menu/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

import pygame
import pygame.gfxdraw as gfxdraw

import pygame_menu.baseimage as _baseimage
import pygame_menu.controls as _controls
import pygame_menu.events as _events
Expand All @@ -52,10 +51,7 @@

from pygame_menu.custom_types import Callable, Any, Dict, NumberType, VectorType, Vector2NumberType, \
Union, Tuple, List, Vector2IntType, Vector2BoolType, Tuple4Tuple2IntType, \
MenuColumnMaxWidthType, MenuColumnMinWidthType, MenuRowsType

MenuCallableType = Union['_events.MenuAction', Callable[[], Any], None]
OnselectCallableType = Union[Callable[[bool, '_widgets.core.Widget', 'Menu'], Any], None]
MenuColumnMaxWidthType, MenuColumnMinWidthType, MenuRowsType, CallbackType, Optional


class Menu(object):
Expand Down Expand Up @@ -85,13 +81,13 @@ class Menu(object):
:param touchscreen_motion_selection: Select widgets using touchscreen motion. If ``True`` menu draws a ``focus`` on the selected widget
"""
_attributes: Dict[str, Any]
_background_function: Union[Callable[['Menu'], Any], Callable[[], Any], None]
_background_function: Optional[Union[Callable[['Menu'], Any], Callable[[], Any]]]
_center_content: bool
_clock: 'pygame.time.Clock'
_column_max_width: VectorType
_column_min_width: VectorType
_column_pos_x: List[NumberType]
_column_widths: Union[List[NumberType], None]
_column_widths: Optional[List[NumberType]]
_columns: int
_current: 'Menu'
_enabled: bool
Expand All @@ -111,10 +107,10 @@ class Menu(object):
_mouse_motion_selection: bool
_mouse_visible: bool
_mouse_visible_default: bool
_onclose: MenuCallableType
_onclose: Optional[Union['_events.MenuAction', Callable[[], Any]]]
_overflow: Tuple[bool, bool]
_position: Tuple[int, int]
_prev: Union[List[Union['Menu', List['Menu']]], None]
_prev: Optional[List[Union['Menu', List['Menu']]]]
_scroll: 'ScrollArea'
_scrollarea_margin: List[int]
_sounds: 'Sound'
Expand All @@ -130,7 +126,7 @@ class Menu(object):
_widget_min_position: Tuple[int, int]
_widget_offset: List[int]
_widgets: List['_widgets.core.Widget']
_widgets_surface: Union['pygame.Surface', None]
_widgets_surface: Optional['pygame.Surface']
_width: int
_window_size: Tuple[int, int]

Expand All @@ -149,10 +145,10 @@ def __init__(self,
mouse_enabled: bool = True,
mouse_motion_selection: bool = False,
mouse_visible: bool = True,
onclose: MenuCallableType = None,
onclose: Optional[Union['_events.MenuAction', Callable[[], Any]]] = None,
overflow: Vector2BoolType = (True, True),
rows: MenuRowsType = None,
screen_dimension: Union[Vector2IntType, None] = None,
screen_dimension: Optional[Vector2IntType] = None,
theme: '_themes.Theme' = _themes.THEME_DEFAULT.copy(),
touchscreen: bool = False,
touchscreen_motion_selection: bool = False
Expand Down Expand Up @@ -489,7 +485,7 @@ def __init__(self,
self._scroll.set_menu(self)
self._overflow = tuple(overflow)

def set_onclose(self, onclose: MenuCallableType) -> None:
def set_onclose(self, onclose: Optional[Union['_events.MenuAction', Callable[[], Any]]]) -> None:
"""
Set ``onclose`` callback.
Expand Down Expand Up @@ -524,7 +520,7 @@ def _check_kwargs(kwargs: Dict) -> None:

def add_button(self,
title: Any,
action: Union['Menu', '_events.MenuAction', Callable, int, None],
action: Optional[Union['Menu', '_events.MenuAction', Callable, int]],
*args,
**kwargs
) -> '_widgets.Button':
Expand Down Expand Up @@ -685,9 +681,9 @@ def add_color_input(self,
hex_format: str = 'none',
input_separator: str = ',',
input_underline: str = '_',
onchange: Union[Callable, None] = None,
onreturn: Union[Callable, None] = None,
onselect: OnselectCallableType = None,
onchange: CallbackType = None,
onreturn: CallbackType = None,
onselect: Optional[Callable[[bool, '_widgets.core.Widget', 'Menu'], Any]] = None,
previsualization_width: int = 3,
**kwargs
) -> '_widgets.ColorInput':
Expand Down Expand Up @@ -782,7 +778,7 @@ def add_image(self,
image_path: Union[str, 'Path', '_baseimage.BaseImage'],
angle: NumberType = 0,
image_id: str = '',
onselect: OnselectCallableType = None,
onselect: Optional[Callable[[bool, '_widgets.core.Widget', 'Menu'], Any]] = None,
scale: Vector2NumberType = (1, 1),
scale_smooth: bool = False,
selectable: bool = False,
Expand Down Expand Up @@ -858,7 +854,7 @@ def add_label(self,
title: Any,
label_id: str = '',
max_char: int = 0,
onselect: OnselectCallableType = None,
onselect: Optional[Callable[[bool, '_widgets.core.Widget', 'Menu'], Any]] = None,
selectable: bool = False,
**kwargs
) -> Union['_widgets.Label', List['_widgets.Label']]:
Expand Down Expand Up @@ -960,9 +956,9 @@ def add_selector(self,
title: Any,
items: Union[List[Tuple[str, Any]], List[str]],
default: int = 0,
onchange: Union[Callable, None] = None,
onreturn: Union[Callable, None] = None,
onselect: OnselectCallableType = None,
onchange: CallbackType = None,
onreturn: CallbackType = None,
onselect: Optional[Callable[[bool, '_widgets.core.Widget', 'Menu'], Any]] = None,
selector_id: str = '',
**kwargs
) -> '_widgets.Selector':
Expand Down Expand Up @@ -1062,13 +1058,13 @@ def add_text_input(self,
input_underline_len: int = 0,
maxchar: int = 0,
maxwidth: int = 0,
onchange: Union[Callable, None] = None,
onreturn: Union[Callable, None] = None,
onselect: OnselectCallableType = None,
onchange: CallbackType = None,
onreturn: CallbackType = None,
onselect: Optional[Callable[[bool, '_widgets.core.Widget', 'Menu'], Any]] = None,
password: bool = False,
tab_size: int = 4,
textinput_id: str = '',
valid_chars: Union[List[str], None] = None,
valid_chars: Optional[List[str]] = None,
**kwargs
) -> '_widgets.TextInput':
"""
Expand Down Expand Up @@ -2094,8 +2090,8 @@ def draw(self, surface: 'pygame.Surface', clear_surface: bool = False) -> None:

self._current._stats.draw += 1

def _draw_focus_widget(self, surface: 'pygame.Surface', widget: Union['_widgets.core.Widget', None]
) -> Union[Dict[int, Tuple4Tuple2IntType], None]:
def _draw_focus_widget(self, surface: 'pygame.Surface', widget: Optional['_widgets.core.Widget']
) -> Optional[Dict[int, Tuple4Tuple2IntType]]:
"""
Draw the focus background from a given widget. Widget must be selectable,
active, selected. Not all widgets requests the active status, then focus may not
Expand Down Expand Up @@ -2283,7 +2279,7 @@ def update(self, events: List['pygame.event.Event']) -> bool:
# Update mouse
pygame.mouse.set_visible(self._current._mouse_visible)

selected_widget: Union['_widgets.core.Widget', None] = None
selected_widget: Optional['_widgets.core.Widget'] = None
if len(self._current._widgets) >= 1:
index = self._current._index % len(self._current._widgets)
selected_widget = self._current._widgets[index]
Expand Down Expand Up @@ -2474,7 +2470,7 @@ def update(self, events: List['pygame.event.Event']) -> bool:

def mainloop(self,
surface: 'pygame.Surface',
bgfun: Union[Callable[['Menu'], Any], Callable[[], Any], None] = None,
bgfun: Optional[Union[Callable[['Menu'], Any], Callable[[], Any]]] = None,
disable_loop: bool = False,
fps_limit: int = 30
) -> None:
Expand Down Expand Up @@ -2591,11 +2587,10 @@ def get_rect(self) -> 'pygame.Rect':
Return the Menu rect.
:return: Rect
:rtype: :py:class:`pygame.Rect`
"""
return pygame.Rect(int(self._position[0]), int(self._position[1]), int(self._width), int(self._height))

def set_sound(self, sound: Union['Sound', None], recursive: bool = False) -> None:
def set_sound(self, sound: Optional['Sound'], recursive: bool = False) -> None:
"""
Add a sound engine to the Menu. If ``recursive=True``, the sound is
applied to all submenus.
Expand Down Expand Up @@ -2635,7 +2630,7 @@ def get_title(self) -> str:
"""
return self._menubar.get_title()

def set_title(self, title: Any, offset: Union[Vector2NumberType, None] = None) -> None:
def set_title(self, title: Any, offset: Optional[Vector2NumberType] = None) -> None:
"""
Set the title of the Menu.
Expand Down Expand Up @@ -2837,7 +2832,6 @@ def get_window_size(self) -> Tuple[int, int]:
to :py:meth:`pygame_menu.Menu.get_current` object.
:return: Window size in px
:rtype: tuple
"""
return self._window_size

Expand Down Expand Up @@ -2895,7 +2889,7 @@ def get_scrollarea(self) -> 'ScrollArea':
"""
return self._scroll

def get_widget(self, widget_id: str, recursive: bool = False) -> Union['_widgets.core.Widget', None]:
def get_widget(self, widget_id: str, recursive: bool = False) -> Optional['_widgets.core.Widget']:
"""
Return a widget by a given ID from the Menu.
Expand Down Expand Up @@ -3032,7 +3026,7 @@ def get_index(self) -> int:
"""
return self._index

def get_selected_widget(self) -> Union['_widgets.core.Widget', None]:
def get_selected_widget(self) -> Optional['_widgets.core.Widget']:
"""
Return the selected widget on the Menu.
Expand Down
17 changes: 7 additions & 10 deletions pygame_menu/scrollarea.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from pygame_menu.widgets import ScrollBar

from pygame_menu.custom_types import ColorType, Union, NumberType, Tuple, List, \
TYPE_CHECKING, Tuple2NumberType
TYPE_CHECKING, Tuple2NumberType, Optional

if TYPE_CHECKING:
from pygame_menu.widgets.core.widget import Widget
Expand All @@ -54,7 +54,6 @@ class ScrollArea(object):
:param area_width: Width of scrollable area (px)
:param area_height: Height of scrollable area (px)
:param area_color: Background color, it can be a color or an image
:type area_color: tuple, list, :py:class:`pygame_menu.baseimage.BaseImage`, None
:param extend_x: Px to extend the surface in yxaxis (px) from left
:param extend_y: Px to extend the surface in y axis (px) from top
:param scrollbar_color: Scrollbars color
Expand All @@ -73,16 +72,16 @@ class ScrollArea(object):
_scrollbars: List['ScrollBar']
_scrollbar_positions: Tuple[str, ...]
_scrollbar_thick: NumberType
_bg_surface: Union['pygame.Surface', None]
_bg_surface: Optional['pygame.Surface']
_extend_x: int
_extend_y: int
_view_rect: 'pygame.Rect'
_menu: Union['Menu', None]
_menu: Optional['Menu']

def __init__(self,
area_width: int,
area_height: int,
area_color: Union[ColorType, '_baseimage.BaseImage', None] = None,
area_color: Optional[Union[ColorType, '_baseimage.BaseImage']] = None,
extend_x: int = 0,
extend_y: int = 0,
scrollbar_color: ColorType = (235, 235, 235),
Expand All @@ -94,7 +93,7 @@ def __init__(self,
shadow_color: ColorType = (0, 0, 0),
shadow_offset: NumberType = 2,
shadow_position: str = _locals.POSITION_SOUTHEAST,
world: Union['pygame.Surface', None] = None
world: Optional['pygame.Surface'] = None
) -> None:
assert isinstance(area_width, int)
assert isinstance(area_height, int)
Expand Down Expand Up @@ -272,7 +271,6 @@ def get_rect(self) -> 'pygame.Rect':
Return the Rect object.
:return: Pygame.Rect object
:rtype: :py:class:`pygame.Rect`
"""
return self._rect.copy()

Expand Down Expand Up @@ -304,7 +302,6 @@ def get_view_rect(self) -> 'pygame.Rect':
or may not be displayed.
:return: View rect object
:rtype: :py:class:`pygame.Rect`
"""
rect = pygame.Rect(self._rect)

Expand Down Expand Up @@ -441,7 +438,7 @@ def scroll_to_rect(self, rect: 'pygame.Rect', margin: NumberType = 10) -> None:
value = max(sbar.get_minimum(), value)
sbar.set_value(value)

def set_position(self, posx: int, posy: int):
def set_position(self, posx: int, posy: int) -> None:
"""
Set the position.
Expand Down Expand Up @@ -547,7 +544,7 @@ def set_menu(self, menu: 'Menu') -> None:
for sbar in self._scrollbars:
sbar.set_menu(menu)

def get_menu(self) -> Union['Menu', None]:
def get_menu(self) -> Optional['Menu']:
"""
Return the Menu reference (if exists).
Expand Down
Loading

0 comments on commit f49bc51

Please sign in to comment.