Exemplo n.º 1
0
    def __init__(self,
                 image_path,
                 drawing_mode=IMAGE_MODE_FILL,
                 drawing_offset=(0, 0)):
        assert isinstance(image_path, str)
        assert isinstance(drawing_mode, int)
        assert_vector2(drawing_offset)

        _, file_extension = path.splitext(image_path)
        file_extension = file_extension.lower()

        valid_formats = [
            '.jpg', '.png', '.gif', '.bmp', '.pcx', '.tga', '.tif', '.lbm',
            '.pbm', '.pgm', '.ppm', '.xpm'
        ]
        assert file_extension in valid_formats, \
            'file extension {0} not valid, please use: {1}'.format(file_extension, ','.join(valid_formats))
        assert path.isfile(image_path), 'file {0} does not exist or could not be found, please ' \
                                        'check if the path of the image is valid'.format(image_path)

        self._filepath = image_path
        self._filename = path.splitext(path.basename(image_path))[0]
        self._extension = file_extension

        # Drawing mode
        self._drawing_mode = drawing_mode
        self._drawing_offset = (drawing_offset[0], drawing_offset[1])

        # Load the image and store as a surface
        self._surface = pygame.image.load(image_path)  # type: pygame.Surface
        self._original_surface = self._surface.copy()
Exemplo n.º 2
0
    def __init__(self,
                 image_path,
                 drawing_mode=IMAGE_MODE_FILL,
                 drawing_offset=(0, 0),
                 load_from_file=True):
        assert isinstance(image_path, str)
        assert isinstance(drawing_mode, int)
        assert isinstance(load_from_file, bool)
        assert_vector2(drawing_offset)

        _, file_extension = path.splitext(image_path)
        file_extension = file_extension.lower()

        assert file_extension in _VALID_IMAGE_FORMATS, \
            'file extension {0} not valid, please use: {1}'.format(file_extension, ','.join(_VALID_IMAGE_FORMATS))
        assert path.isfile(image_path), 'file {0} does not exist or could not be found, please ' \
                                        'check if the path of the image is valid'.format(image_path)

        self._filepath = image_path
        self._filename = path.splitext(path.basename(image_path))[0]
        self._extension = file_extension

        # Drawing mode
        self._drawing_mode = drawing_mode
        self._drawing_offset = (drawing_offset[0], drawing_offset[1])

        # Load the image and store as a surface
        if load_from_file:
            self._surface = pygame.image.load(
                image_path)  # type: pygame.Surface
            self._original_surface = self._surface.copy()
Exemplo n.º 3
0
    def set_drawing_offset(self, drawing_offset):
        """
        Set the image drawing offset.

        :param drawing_offset: Drawing offset tuple *(x, y)*
        :type drawing_offset: tuple, list
        :return: None
        """
        assert_vector2(drawing_offset)
        self._drawing_offset = (drawing_offset[0], drawing_offset[1])
Exemplo n.º 4
0
    def _get(params, key, allowed_types=None, default=None):
        """
        Return a value from a dictionary.

        :param params: parameters dictionary
        :type params: dict
        :param key: key to look for
        :type key: str
        :param allowed_types: list of allowed types
        :type allowed_types: any
        :param default: default value to return
        :type default: any
        :return: The value associated to the key
        :rtype: any
        """
        if key not in params:
            return default

        value = params.pop(key)
        if allowed_types:
            if not isinstance(allowed_types, (tuple, list)):
                allowed_types = (allowed_types, )
            for valtype in allowed_types:
                if valtype == 'color':
                    _utils.assert_color(value)
                elif valtype == 'color_none':
                    if value is None:
                        return value
                    _utils.assert_color(value)
                elif valtype == 'color_image':
                    if isinstance(value, BaseImage):
                        return value
                    _utils.assert_color(value)
                elif valtype == 'color_image_none':
                    if value is None:
                        return value
                    elif isinstance(value, BaseImage):
                        return value
                    _utils.assert_color(value)
                elif valtype == 'position':
                    _utils.assert_position(value)
                elif valtype == 'alignment':
                    _utils.assert_alignment(value)
                elif valtype == 'tuple2':
                    _utils.assert_vector2(value)

            all_types = ('color', 'color_none', 'color_image',
                         'color_image_none', 'position', 'alignment', 'tuple2')
            others = tuple(t for t in allowed_types if t not in all_types)
            if others:
                msg = 'Theme.{} type shall be in {} types (got {})'.format(
                    key, others, type(value))
                assert isinstance(value, others), msg
        return value
Exemplo n.º 5
0
 def __init__(self, margin_left, margin_right, margin_top, margin_bottom,
              arrow_size=(10, 15), arrow_vertical_offset=0, blink_ms=0):
     super(ArrowSelection, self).__init__(
         margin_left=margin_left,
         margin_right=margin_right,
         margin_top=margin_top,
         margin_bottom=margin_bottom
     )
     assert_vector2(arrow_size)
     assert isinstance(arrow_vertical_offset, (int, float))
     assert isinstance(blink_ms, int)
     assert arrow_size[0] > 0 and arrow_size[1] > 0, 'arrow size must be greater than zero'
     assert blink_ms >= 0, 'blinking milliseconds must be greater than or equal to zero'
     self._arrow_vertical_offset = arrow_vertical_offset
     self._arrow_size = (arrow_size[0], arrow_size[1])  # type: tuple
     self._blink_ms = blink_ms
     self._blink_time = 0
     self._blink_status = True
     self._last_widget = None
Exemplo n.º 6
0
    def set_background_color(self, color, inflate=(0, 0)):
        """
        Set widget background color.

        :param color: Widget background color
        :type color: tuple, list, :py:class:`pygame_menu.baseimage.BaseImage`, None
        :param inflate: Inflate background in x,y
        :type inflate: tuple, list
        :return: None
        """
        if color is not None:
            if isinstance(color, _baseimage.BaseImage):
                assert color.get_drawing_mode() == _baseimage.IMAGE_MODE_FILL, \
                    'currently widget only support IMAGE_MODE_FILL drawing mode'
            else:
                assert_color(color)
        assert_vector2(inflate)
        assert inflate[0] >= 0 and inflate[1] >= 0, \
            'widget background inflate must be equal or greater than zero in both axis'
        self._background_color = color
        self._background_inflate = inflate
Exemplo n.º 7
0
    def set_background_color(self, color, inflate=(0, 0)):
        """
        Set widget background color.

        :param color: Widget background color
        :type color: tuple, list, :py:class:`pygame_menu.baseimage.BaseImage`, None
        :param inflate: Inflate background in *(x,y)*. If ``None``, the widget value is not updated
        :type inflate: tuple, list, None
        :return: None
        """
        if color is not None:
            if isinstance(color, _baseimage.BaseImage):
                assert color.get_drawing_mode() == _baseimage.IMAGE_MODE_FILL, \
                    'currently widget only supports IMAGE_MODE_FILL drawing mode'
            else:
                assert_color(color)
        if inflate is None:
            inflate = self._background_inflate
        assert_vector2(inflate)
        assert inflate[0] >= 0 and inflate[1] >= 0, \
            'widget background inflate must be equal or greater than zero in both axis'
        self._background_color = color
        self._background_inflate = tuple(inflate)
        self._last_render_hash = 0  # Force widget render