def __init__(
            self,
            image_path: Union[str, 'BaseImage', 'Path', 'BytesIO'],
            angle: NumberType = 0,
            image_id: str = '',
            onselect: CallbackType = None,
            scale: Tuple2NumberType = (1, 1),
            scale_smooth: bool = True
    ) -> None:
        assert isinstance(image_path, (str, Path, BaseImage, BytesIO))
        assert isinstance(image_id, str)
        assert isinstance(angle, NumberInstance)
        assert isinstance(scale_smooth, bool)
        assert_vector(scale, 2)

        super(Image, self).__init__(
            onselect=onselect,
            widget_id=image_id
        )

        if isinstance(image_path, BaseImage):
            self._image = image_path
        else:
            self._image = BaseImage(image_path)
            self._image.rotate(angle)
            self._image.scale(scale[0], scale[1], smooth=scale_smooth)
Exemple #2
0
class Image(Widget):
    """
    Image widget.

    :param image_path: Path of the image
    :type image_path: str
    :param image_id: Image ID
    :type image_id: str
    :param angle: Angle of the image in degrees (clockwise)
    :type angle: int, float
    :param scale: Scale of the image (x,y), float or int
    :type scale: tuple, list
    :param scale_smooth: Scale is smoothed
    :type scale_smooth: bool
    """
    def __init__(self,
                 image_path,
                 image_id='',
                 angle=0,
                 scale=(1, 1),
                 scale_smooth=True):
        assert isinstance(image_path, str)
        assert isinstance(image_id, str)
        assert isinstance(angle, (int, float))
        assert isinstance(scale, (tuple, list))
        assert isinstance(scale_smooth, bool)
        super(Image, self).__init__(widget_id=image_id)

        self._image = BaseImage(image_path)
        self._image.rotate(angle)
        self._image.scale(scale[0], scale[1], smooth=scale_smooth)

        self.is_selectable = False

    def _apply_font(self):
        pass

    # noinspection PyMissingOrEmptyDocstring
    def draw(self, surface):
        self._render()
        surface.blit(self._surface, self._rect.topleft)

    def _render(self):
        if self._surface is not None:
            return
        self._surface = self._image.get_surface()
        self._rect.width, self._rect.height = self._surface.get_size()

    # noinspection PyMissingOrEmptyDocstring
    def update(self, events):
        return False
Exemple #3
0
    def add_button(
        menu: Menu,
        id_to_action: Dict[str, Any],
        image: Image,
        scaling: float,
        text_width: int,
        action_type,
        identificator,
        padding,
    ):
        buffered = BytesIO()
        image.save(buffered, format="PNG")
        buffered.seek(0)
        image = BaseImage(buffered).scale(scaling, scaling)

        button = menu.add.button(
            " " * text_width,
            lambda *args: args,
            action_type,
            identificator,
            padding=padding,
        )

        decorator = button.get_decorator()
        decorator.add_baseimage(0, 0, image, centered=True)
        id_to_action[button.get_id()] = (action_type, identificator)
Exemple #4
0
    def __init__(self,
                 image_path,
                 image_id='',
                 angle=0,
                 scale=(1, 1),
                 scale_smooth=True):
        assert isinstance(image_path, str)
        assert isinstance(image_id, str)
        assert isinstance(angle, (int, float))
        assert isinstance(scale, (tuple, list))
        assert isinstance(scale_smooth, bool)
        super(Image, self).__init__(widget_id=image_id)

        self._image = BaseImage(image_path)
        self._image.rotate(angle)
        self._image.scale(scale[0], scale[1], smooth=scale_smooth)
        self.selection_effect_enabled = False
Exemple #5
0
    def __init__(self,
                 type1: str,
                 type2: str = "",
                 scale=(1.0, 1.0),
                 align=pygame_menu.locals.ALIGN_LEFT):
        super(PokemonTypeWidget, self).__init__()
        self.set_alignment(align)
        self.type1 = type1.strip()
        self.type2 = type2.strip()

        self._image1 = BaseImage('./images/gui/' + self.type1 + '.png')
        self._image1.scale(scale[0], scale[1], smooth=True)
        if self.type2 == None or self.type2 == "":
            self._image2 = None
        else:
            self._image2 = BaseImage('./images/gui/' + self.type2 + '.png')
            self._image2.scale(scale[0], scale[1], smooth=True)
def create_example_window(
        title: str,
        window_size: Tuple[int, int],
        pygame_menu_icon: bool = True,
        init_pygame: bool = True,
        center_window: bool = True,
        **kwargs
) -> 'pygame.Surface':
    """
    Set pygame window.

    :param title: Window title
    :param window_size: Window size
    :param pygame_menu_icon: Use pygame menu icon
    :param init_pygame: Init pygame
    :param center_window: Center the window
    :param kwargs: Optional keyword arguments received by display set_mode
    :return: Pygame surface from created display
    """
    assert len(title) > 0, 'title cannot be empty'
    assert len(window_size) == 2, 'window size shape must be (width, height)'
    assert isinstance(window_size[0], int), 'width must be an integer'
    assert isinstance(window_size[1], int), 'height must be an integer'

    from pygame_menu.baseimage import IMAGE_EXAMPLE_PYGAME_MENU, BaseImage
    import os

    if init_pygame:
        pygame.init()
    if center_window:
        os.environ['SDL_VIDEO_CENTERED'] = '1'

    # Create pygame screen and objects
    if sys.platform == 'darwin':
        kwargs = {}

    try:
        surface = pygame.display.set_mode(window_size, **kwargs)
    except TypeError:
        surface = pygame.display.set_mode(window_size)
    pygame.display.set_caption(title)

    if pygame_menu_icon:
        # noinspection PyBroadException
        try:
            if _PYGAME_ICON[0] is not None:
                pygame.display.set_icon(_PYGAME_ICON[0])
            else:
                icon = BaseImage(IMAGE_EXAMPLE_PYGAME_MENU).get_surface(new=False)
                pygame.display.set_icon(icon)
                _PYGAME_ICON[0] = icon
        except BaseException:  # lgtm [py/catch-base-exception]
            pass

    return surface
Exemple #7
0
class PokemonTypeWidget(Widget):
    def __init__(self,
                 type1: str,
                 type2: str = "",
                 scale=(1.0, 1.0),
                 align=pygame_menu.locals.ALIGN_LEFT):
        super(PokemonTypeWidget, self).__init__()
        self.set_alignment(align)
        self.type1 = type1.strip()
        self.type2 = type2.strip()

        self._image1 = BaseImage('./images/gui/' + self.type1 + '.png')
        self._image1.scale(scale[0], scale[1], smooth=True)
        if self.type2 == None or self.type2 == "":
            self._image2 = None
        else:
            self._image2 = BaseImage('./images/gui/' + self.type2 + '.png')
            self._image2.scale(scale[0], scale[1], smooth=True)

    def _draw(self, surface: pygame.Surface) -> None:
        surface.blit(self._surface, self._rect.topleft)

    def draw(self, surface):
        self._draw(surface)

    def _render(self):
        if self._surface is not None:
            return True
        surface1 = self._image1.get_surface()
        if self._image2 == None:
            self._rect.width, self._rect.height = surface1.get_size()
            self._surface = surface1
        else:
            surface2 = self._image2.get_surface()
            surface = pygame.Surface(
                (surface1.get_width() + surface2.get_width() + 15,
                 surface1.get_height()), pygame.SRCALPHA)
            surface.blit(surface1, (5, 0))
            surface.blit(surface2, (surface1.get_width() + 10, 0))
            self._surface = surface
            self._rect.width, self._rect.height = surface.get_size()

        #if not self._render_hash_changed(self._visible):
        #    return True
        #self.force_menu_surface_update()

    def _apply_font(self):
        pass

    def update(self, events):
        pass
Exemple #8
0
class Image(Widget):
    """
    Image widget.

    :param image_path: Path of the image or BaseImage object. If BaseImage object is provided drawing mode is not considered. It can be a string or :py:class:`pathlib.Path` on ``Python 3+``
    :type image_path: str, :py:class:`pathlib.Path`, BaseImage
    :param image_id: Image ID
    :type image_id: str
    :param angle: Angle of the image in degrees (clockwise)
    :type angle: int, float
    :param scale: Scale of the image *(x,y)*
    :type scale: tuple, list
    :param scale_smooth: Scale is smoothed
    :type scale_smooth: bool
    """
    def __init__(self,
                 image_path,
                 image_id='',
                 angle=0,
                 scale=(1, 1),
                 scale_smooth=True):
        assert isinstance(image_id, str)
        assert isinstance(angle, (int, float))
        assert isinstance(scale, (tuple, list))
        assert isinstance(scale_smooth, bool)
        super(Image, self).__init__(widget_id=image_id)

        if isinstance(image_path, BaseImage):
            self._image = image_path
        else:
            self._image = BaseImage(image_path)
            self._image.rotate(angle)
            self._image.scale(scale[0], scale[1], smooth=scale_smooth)

        self.selection_effect_enabled = False

    def get_image(self):
        """
        Gets the BaseImage object from widget.

        :return: Widget image
        :rtype: BaseImage
        """
        return self._image

    def set_image(self, image):
        """
        Set the BaseImage object from widget.

        :param image: BaseImage object
        :type image: BaseImage
        :return: None
        """
        self._image = image
        self._surface = None
        self._render()

    def _apply_font(self):
        pass

    def rotate(self, angle):
        self._image.rotate(angle)
        self._surface = None

    def flip(self, x, y):
        if x or y:
            self._image.flip(x, y)
            self._surface = None

    def scale(self, width, height, smooth=False):
        self._image.scale(width, height, smooth)
        self._surface = None

    def resize(self, width, height, smooth=False):
        self._image.resize(width, height, smooth)
        self._surface = None

    # noinspection PyMissingOrEmptyDocstring
    def draw(self, surface):
        self._render()
        surface.blit(self._surface, self._rect.topleft)

    def _render(self):
        if self._surface is not None:
            return True
        self._surface = self._image.get_surface()
        self._rect.width, self._rect.height = self._surface.get_size()
        if not self._render_hash_changed(self.visible):
            return True
        self._menu_surface_needs_update = True  # Force menu update

    # noinspection PyMissingOrEmptyDocstring
    def update(self, events):
        return False
Exemple #9
0
class Image(Widget):
    """
    Image widget.

    .. note::

        Image accepts all transformations.

    :param image_path: Path of the image, BytesIO object, or :py:class:`pygame_menu.baseimage.BaseImage` object. If :py:class:`pygame_menu.baseimage.BaseImage` object is provided drawing mode is not considered
    :param image_id: Image ID
    :param angle: Angle of the image in degrees (clockwise)
    :param onselect: Function when selecting the widget
    :param scale: Scale of the image on x-axis and y-axis (x, y) in px
    :param scale_smooth: Scale is smoothed
    """
    _image: 'BaseImage'

    def __init__(self,
                 image_path: Union[str, 'BaseImage', 'Path', 'BytesIO'],
                 angle: NumberType = 0,
                 image_id: str = '',
                 onselect: CallbackType = None,
                 scale: Tuple2NumberType = (1, 1),
                 scale_smooth: bool = True) -> None:
        assert isinstance(image_path, (str, Path, BaseImage, BytesIO))
        assert isinstance(image_id, str)
        assert isinstance(angle, NumberInstance)
        assert isinstance(scale_smooth, bool)
        assert_vector(scale, 2)

        super(Image, self).__init__(onselect=onselect, widget_id=image_id)

        if isinstance(image_path, BaseImage):
            self._image = image_path
        else:
            self._image = BaseImage(image_path)
            self._image.rotate(angle)
            self._image.scale(scale[0], scale[1], smooth=scale_smooth)

    def set_title(self, title: str) -> 'Image':
        return self

    def get_image(self) -> 'BaseImage':
        """
        Gets the :py:class:`pygame_menu.baseimage.BaseImage` object from widget.

        :return: Widget image
        """
        return self._image

    def get_angle(self) -> NumberType:
        """
        Return the image angle.

        :return: Angle in degrees
        """
        return self._image.get_angle()

    def set_image(self, image: 'BaseImage') -> None:
        """
        Set the :py:class:`pygame_menu.baseimage.BaseImage` object from widget.

        :param image: Image object
        :return: None
        """
        self._image = image
        self._surface = None
        self._render()

    def _apply_font(self) -> None:
        pass

    def _update_surface(self) -> 'Image':
        """
        Updates surface and renders.

        :return: Self reference
        """
        self._surface = None
        self._render()
        return self

    def scale(self,
              width: NumberType,
              height: NumberType,
              smooth: bool = False) -> 'Image':
        self._image.scale(width, height, smooth)
        return self._update_surface()

    def resize(self,
               width: NumberType,
               height: NumberType,
               smooth: bool = False) -> 'Image':
        self._image.resize(width, height, smooth)
        self._surface = None
        return self._update_surface()

    def set_max_width(self,
                      width: Optional[NumberType],
                      scale_height: NumberType = False,
                      smooth: bool = True) -> 'Image':
        if width is not None and self._image.get_width() > width:
            sx = width / self._image.get_width()
            height = self._image.get_height()
            if scale_height:
                height *= sx
            self._image.resize(width, height, smooth)
            return self._update_surface()
        return self

    def set_max_height(self,
                       height: Optional[NumberType],
                       scale_width: NumberType = False,
                       smooth: bool = True) -> 'Image':
        if height is not None and self._image.get_height() > height:
            sy = height / self._image.get_height()
            width = self._image.get_width()
            if scale_width:
                width *= sy
            self._image.resize(width, height, smooth)
            return self._update_surface()
        return self

    def rotate(self, angle: NumberType) -> 'Image':
        self._image.rotate(angle)
        return self._update_surface()

    def flip(self, x: bool, y: bool) -> 'Image':
        assert isinstance(x, bool)
        assert isinstance(y, bool)
        self._flip = (x, y)
        if x or y:
            self._image.flip(x, y)
            return self._update_surface()
        return self

    def _draw(self, surface: 'pygame.Surface') -> None:
        surface.blit(self._surface, self._rect.topleft)

    def _render(self) -> Optional[bool]:
        if self._surface is not None:
            return True
        self._surface = self._image.get_surface(new=False)
        self._rect.width, self._rect.height = self._surface.get_size()
        if not self._render_hash_changed(self._visible):
            return True
        self.force_menu_surface_update()

    def update(self, events: EventVectorType) -> bool:
        self.apply_update_callbacks(events)
        for event in events:
            if self._check_mouseover(event):
                break
        return False
Exemple #10
0
class Image(Widget):
    """
    Image widget.

    .. note::

        This class redefines all widget transformations.

    :param image_path: Path of the image, BytesIO object, or :py:class:`pygame_menu.baseimage.BaseImage` object. If :py:class:`pygame_menu.baseimage.BaseImage` object is provided drawing mode is not considered
    :param image_id: Image ID
    :param angle: Angle of the image in degrees (clockwise)
    :param onselect: Function when selecting the widget
    :param scale: Scale of the image *(x, y)*
    :param scale_smooth: Scale is smoothed
    """
    _image: 'BaseImage'

    def __init__(self,
                 image_path: Union[str, 'BaseImage', 'Path', 'BytesIO'],
                 image_id: str = '',
                 angle: NumberType = 0,
                 onselect: CallbackType = None,
                 scale: Tuple2NumberType = (1, 1),
                 scale_smooth: bool = True) -> None:
        assert isinstance(image_path, (str, Path, BaseImage, BytesIO))
        assert isinstance(image_id, str)
        assert isinstance(angle, (int, float))
        assert isinstance(scale_smooth, bool)
        assert_vector(scale, 2)

        super(Image, self).__init__(onselect=onselect, widget_id=image_id)

        if isinstance(image_path, BaseImage):
            self._image = image_path
        else:
            self._image = BaseImage(image_path)
            self._image.rotate(angle)
            self._image.scale(scale[0], scale[1], smooth=scale_smooth)

    def set_title(self, title: str) -> 'Widget':
        return self

    def get_image(self) -> 'BaseImage':
        """
        Gets the :py:class:`pygame_menu.baseimage.BaseImage` object from widget.

        :return: Widget image
        """
        return self._image

    def set_image(self, image: 'BaseImage') -> None:
        """
        Set the :py:class:`pygame_menu.baseimage.BaseImage` object from widget.

        :param image: Image object
        :return: None
        """
        self._image = image
        self._surface = None
        self._render()

    def _apply_font(self) -> None:
        pass

    def rotate(self, angle: NumberType) -> 'Widget':
        self._image.rotate(angle)
        self._surface = None
        return self

    def flip(self, x: bool, y: bool) -> 'Widget':
        if x or y:
            self._image.flip(x, y)
            self._surface = None
        return self

    def scale(self,
              width: NumberType,
              height: NumberType,
              smooth: bool = False) -> 'Widget':
        self._image.scale(width, height, smooth)
        self._surface = None
        return self

    def resize(self,
               width: NumberType,
               height: NumberType,
               smooth: bool = False) -> 'Widget':
        self._image.resize(width, height, smooth)
        self._surface = None
        return self

    def _draw(self, surface: 'pygame.Surface') -> None:
        surface.blit(self._surface, self._rect.topleft)

    def _render(self) -> Optional[bool]:
        if self._surface is not None:
            return True
        self._surface = self._image.get_surface(new=False)
        self._rect.width, self._rect.height = self._surface.get_size()
        if not self._render_hash_changed(self._visible):
            return True
        self.force_menu_surface_update()

    def update(
        self, events: Union[List['pygame.event.Event'],
                            Tuple['pygame.event.Event']]
    ) -> bool:
        return False