Esempio n. 1
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
Esempio n. 2
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
Esempio n. 3
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