def validate(self) -> 'Theme': """ Validate the values of the theme. If there's a invalid parameter throws an ``AssertionError``. This function also converts all lists to tuples. This is done because lists are mutable. :return: Self reference """ if self._disable_validation: return self # Boolean asserts assert isinstance(self.scrollbar_shadow, bool) assert isinstance(self.title_bar_modify_scrollarea, bool) assert isinstance(self.title_close_button, bool) assert isinstance(self.title_font_antialias, bool) assert isinstance(self.title_font_shadow, bool) assert isinstance(self.widget_font_antialias, bool) assert isinstance(self.widget_font_background_color_from_menu, bool) assert isinstance(self.widget_font_shadow, bool) # Value type checks assert_alignment(self.widget_alignment) assert_cursor(self.scrollbar_cursor) assert_cursor(self.title_close_button_cursor) assert_cursor(self.widget_cursor) assert_font(self.title_font) assert_font(self.widget_font) assert_position(self.scrollbar_shadow_position) assert_position(self.title_font_shadow_position) assert_position(self.widget_font_shadow_position) assert_position_vector(self.widget_border_position) assert _check_menubar_style(self.title_bar_style) assert get_scrollbars_from_position( self.scrollarea_position) is not None # Check selection effect if None if self.widget_selection_effect is None: self.widget_selection_effect = NoneSelection() assert isinstance(self.cursor_switch_ms, NumberInstance) assert isinstance(self.fps, NumberInstance) assert isinstance(self.scrollbar_shadow_offset, int) assert isinstance(self.scrollbar_slider_pad, NumberInstance) assert isinstance(self.scrollbar_thick, int) assert isinstance(self.title, bool) assert isinstance(self.title_fixed, bool) assert isinstance(self.title_floating, bool) assert isinstance(self.title_font_shadow_offset, int) assert isinstance(self.title_font_size, int) assert isinstance(self.title_updates_pygame_display, bool) assert isinstance(self.widget_background_inflate_to_selection, bool) assert isinstance(self.widget_border_width, int) assert isinstance(self.widget_box_border_width, int) assert isinstance(self.widget_font_shadow_offset, int) assert isinstance(self.widget_font_size, int) assert isinstance(self.widget_padding, PaddingInstance) assert isinstance(self.widget_selection_effect, Selection) assert isinstance(self.widget_tab_size, int) # Format colors, this converts all color lists to tuples automatically, # if image, return the same object self.background_color = self._format_color_opacity( self.background_color) self.cursor_color = self._format_color_opacity(self.cursor_color) self.cursor_selection_color = self._format_color_opacity( self.cursor_selection_color) self.focus_background_color = self._format_color_opacity( self.focus_background_color) self.readonly_color = self._format_color_opacity(self.readonly_color) self.readonly_selected_color = self._format_color_opacity( self.readonly_selected_color) self.scrollbar_color = self._format_color_opacity(self.scrollbar_color) self.scrollbar_shadow_color = self._format_color_opacity( self.scrollbar_shadow_color) self.scrollbar_slider_color = self._format_color_opacity( self.scrollbar_slider_color) self.scrollbar_slider_hover_color = self._format_color_opacity( self.scrollbar_slider_hover_color) self.selection_color = self._format_color_opacity(self.selection_color) self.surface_clear_color = self._format_color_opacity( self.surface_clear_color) self.title_background_color = self._format_color_opacity( self.title_background_color) self.title_close_button_background_color = self._format_color_opacity( self.title_close_button_background_color) self.title_font_color = self._format_color_opacity( self.title_font_color) self.title_font_shadow_color = self._format_color_opacity( self.title_font_shadow_color) self.widget_background_color = self._format_color_opacity( self.widget_background_color, none=True) self.widget_border_color = self._format_color_opacity( self.widget_border_color) self.widget_box_arrow_color = self._format_color_opacity( self.widget_box_arrow_color) self.widget_box_background_color = self._format_color_opacity( self.widget_box_background_color) self.widget_box_border_color = self._format_color_opacity( self.widget_box_border_color) self.widget_font_background_color = self._format_color_opacity( self.widget_font_background_color, none=True) self.widget_font_color = self._format_color_opacity( self.widget_font_color) self.widget_font_shadow_color = self._format_color_opacity( self.widget_font_shadow_color) self.widget_url_color = self._format_color_opacity( self.widget_url_color) # List to tuple self.scrollarea_outer_margin = self._vec_to_tuple( self.scrollarea_outer_margin, 2, NumberInstance) self.title_offset = self._vec_to_tuple(self.title_offset, 2, NumberInstance) self.widget_background_inflate = self._vec_to_tuple( self.widget_background_inflate, 2, int) self.widget_border_inflate = self._vec_to_tuple( self.widget_border_inflate, 2, int) self.widget_box_arrow_margin = self._vec_to_tuple( self.widget_box_arrow_margin, 3, int) self.widget_box_inflate = self._vec_to_tuple(self.widget_box_inflate, 2, int) self.widget_box_margin = self._vec_to_tuple(self.widget_box_margin, 2, NumberInstance) self.widget_margin = self._vec_to_tuple(self.widget_margin, 2, NumberInstance) if isinstance(self.widget_padding, VectorInstance): self.widget_padding = self._vec_to_tuple(self.widget_padding) assert 2 <= len(self.widget_padding) <= 4, \ 'widget padding tuple length must be 2, 3 or 4' for p in self.widget_padding: assert isinstance(p, NumberInstance), \ 'each padding element must be numeric (integer or float)' assert p >= 0, \ 'all padding elements must be equal or greater than zero' else: assert self.widget_padding >= 0, 'padding cannot be a negative number' self.widget_offset = self._vec_to_tuple(self.widget_offset, 2, NumberInstance) # Check sizes assert self.scrollarea_outer_margin[0] >= 0 and self.scrollarea_outer_margin[1] >= 0, \ 'scroll area outer margin must be equal or greater than zero on both axis' assert self.widget_offset[0] >= 0 and self.widget_offset[1] >= 0, \ 'widget offset must be equal or greater than zero' assert self.widget_background_inflate[0] >= 0 and self.widget_background_inflate[1] >= 0, \ 'widget background inflate must be equal or greater than zero on both axis' assert self.widget_border_inflate[0] >= 0 and self.widget_border_inflate[1] >= 0, \ 'widget border inflate must be equal or greater than zero on both axis' assert self.widget_box_inflate[0] >= 0 and self.widget_box_inflate[1] >= 0, \ 'widget box inflate inflate must be equal or greater than zero on both axis' assert self.cursor_switch_ms > 0, 'cursor switch ms must be greater than zero' assert self.fps >= 0, 'fps must be equal or greater than zero' assert self.scrollbar_shadow_offset > 0, 'scrollbar shadow offset must be greater than zero' assert self.scrollbar_slider_pad >= 0, 'slider pad must be equal or greater than zero' assert self.scrollbar_thick > 0, 'scrollbar thickness must be greater than zero' assert self.title_font_size > 0, 'title font size must be greater than zero' assert self.widget_border_width >= 0, 'widget border width must be equal or greater than zero' assert self.widget_box_border_width >= 0, 'widget border box width must be equal or greater than zero' assert self.widget_font_shadow_offset > 0, 'widget font shadow offset must be greater than zero' assert self.widget_font_size > 0, 'widget font size must be greater than zero' assert self.widget_tab_size >= 0, 'widget tab size must be equal or greater than zero' # Color asserts assert self.focus_background_color[3] != 0, \ 'focus background color cannot be fully transparent, suggested opacity between 1 and 255' return self
def _get(params: Dict[str, Any], key: str, allowed_types: Optional[Union[Type, str, List[Type], Tuple[Type, ...]]] = None, default: Any = None) -> Any: """ Return a value from a dictionary. Custom types (str) - alignment – pygame-menu alignment (locals) - callable – Is callable type, same as ``"function"`` - color – Check color - color_image – Color or :py:class:`pygame_menu.baseimage.BaseImage` - color_image_none – Color, :py:class:`pygame_menu.baseimage.BaseImage`, or None - color_none – Color or None - cursor – Cursor object (pygame) - font – Font type - image – Value must be ``BaseImage`` - none – None only - position – pygame-menu position (locals) - position_vector – pygame-menu position (str or vector) - tuple2 – Only valid numeric tuples ``(x, y)`` or ``[x, y]`` - tuple2int – Only valid integer tuples ``(x, y)`` or ``[x, y]`` - tuple3 – Only valid numeric tuples ``(x, y, z)`` or ``[x, y, z]`` - tuple3int – Only valid integer tuples ``(x, y, z)`` or ``[x, y, z]`` - type – Type-class (bool, str, etc...) :param params: Parameters dictionary :param key: Key to look for :param allowed_types: List of allowed types :param default: Default value to return :return: The value associated to the key """ value = params.pop(key, default) if allowed_types is not None: other_types = [] # Contain other types to check from if not isinstance(allowed_types, VectorInstance): allowed_types = (allowed_types, ) for val_type in allowed_types: if val_type == 'alignment': assert_alignment(value) elif val_type == callable or val_type == 'function' or val_type == 'callable': assert is_callable(value), \ 'value must be callable type' elif val_type == 'color': value = assert_color(value) elif val_type == 'color_image': if not isinstance(value, BaseImage): value = assert_color(value) elif val_type == 'color_image_none': if not (value is None or isinstance(value, BaseImage)): value = assert_color(value) elif val_type == 'color_none': if value is not None: value = assert_color(value) elif val_type == 'cursor': assert_cursor(value) elif val_type == 'font': assert_font(value) elif val_type == 'image': assert isinstance(value, BaseImage), \ 'value must be BaseImage type' elif val_type == 'none': assert value is None elif val_type == 'position': assert_position(value) elif val_type == 'position_vector': assert_position_vector(value) elif val_type == 'type': assert isinstance(value, type), \ 'value is not type-class' elif val_type == 'tuple2': assert_vector(value, 2) elif val_type == 'tuple2int': assert_vector(value, 2, int) elif val_type == 'tuple3': assert_vector(value, 3) elif val_type == 'tuple3int': assert_vector(value, 3, int) else: # Unknown type assert isinstance(val_type, type), \ 'allowed type "{0}" is not a type-class'.format(val_type) other_types.append(val_type) # Check other types if len(other_types) > 0: others = tuple(other_types) assert isinstance(value, others), \ 'Theme.{} type shall be in {} types (got {})'.format(key, others, type(value)) return value
def _filter_widget_attributes(self, kwargs: Dict) -> Dict[str, Any]: attributes = {} # align align = kwargs.pop('align', self._theme.widget_alignment) assert isinstance(align, str) attributes['align'] = align # background_color background_is_color = False background_color = kwargs.pop('background_color', self._theme.widget_background_color) if background_color is not None: if isinstance(background_color, pygame_menu.BaseImage): pass else: background_color = assert_color(background_color) background_is_color = True attributes['background_color'] = background_color # background_inflate background_inflate = kwargs.pop('background_inflate', self._theme.widget_background_inflate) if background_inflate == 0: background_inflate = (0, 0) assert_vector(background_inflate, 2, int) assert background_inflate[0] >= 0 and background_inflate[1] >= 0, \ 'both background inflate components must be equal or greater than zero' attributes['background_inflate'] = background_inflate # border_color border_color = kwargs.pop('border_color', self._theme.widget_border_color) if border_color is not None: border_color = assert_color(border_color) attributes['border_color'] = border_color # border_inflate border_inflate = kwargs.pop('border_inflate', self._theme.widget_border_inflate) if border_inflate == 0: border_inflate = (0, 0) assert_vector(border_inflate, 2, int) assert isinstance(border_inflate[0], int) and border_inflate[0] >= 0 assert isinstance(border_inflate[1], int) and border_inflate[1] >= 0 attributes['border_inflate'] = border_inflate # border_position border_position = kwargs.pop('border_position', self._theme.widget_border_position) assert_position_vector(border_position) attributes['border_position'] = border_position # border_width border_width = kwargs.pop('border_width', self._theme.widget_border_width) assert isinstance(border_width, int) and border_width >= 0 attributes['border_width'] = border_width # cursor cursor = kwargs.pop('cursor', self._theme.widget_cursor) assert_cursor(cursor) attributes['cursor'] = cursor # floating status float_ = kwargs.pop('float', False) assert isinstance(float_, bool) attributes['float'] = float_ float_origin_position = kwargs.pop('float_origin_position', False) assert isinstance(float_origin_position, bool) attributes['float_origin_position'] = float_origin_position # font_antialias attributes['font_antialias'] = self._theme.widget_font_antialias # font_background_color font_background_color = kwargs.pop( 'font_background_color', self._theme.widget_font_background_color) if font_background_color is None and \ self._theme.widget_font_background_color_from_menu and \ not background_is_color: if not isinstance(self._theme.background_color, pygame_menu.BaseImage): font_background_color = assert_color( self._theme.background_color) attributes['font_background_color'] = font_background_color # font_color font_color = kwargs.pop('font_color', self._theme.widget_font_color) attributes['font_color'] = assert_color(font_color) # font_name font_name = kwargs.pop('font_name', self._theme.widget_font) assert_font(font_name) attributes['font_name'] = font_name # font_shadow font_shadow = kwargs.pop('font_shadow', self._theme.widget_font_shadow) assert isinstance(font_shadow, bool) attributes['font_shadow'] = font_shadow # font_shadow_color font_shadow_color = kwargs.pop('font_shadow_color', self._theme.widget_font_shadow_color) attributes['font_shadow_color'] = assert_color(font_shadow_color) # font_shadow_offset font_shadow_offset = kwargs.pop('font_shadow_offset', self._theme.widget_font_shadow_offset) assert isinstance(font_shadow_offset, int) attributes['font_shadow_offset'] = font_shadow_offset # font_shadow_position font_shadow_position = kwargs.pop( 'font_shadow_position', self._theme.widget_font_shadow_position) assert isinstance(font_shadow_position, str) attributes['font_shadow_position'] = font_shadow_position # font_size font_size = kwargs.pop('font_size', self._theme.widget_font_size) assert isinstance(font_size, int) assert font_size > 0, 'font size must be greater than zero' attributes['font_size'] = font_size # margin margin = kwargs.pop('margin', self._theme.widget_margin) if margin == 0: margin = (0, 0) assert_vector(margin, 2) attributes['margin'] = margin # padding padding = kwargs.pop('padding', self._theme.widget_padding) assert isinstance(padding, PaddingInstance) attributes['padding'] = padding # readonly_color readonly_color = kwargs.pop('readonly_color', self._theme.readonly_color) attributes['readonly_color'] = assert_color(readonly_color) # readonly_selected_color readonly_selected_color = kwargs.pop( 'readonly_selected_color', self._theme.readonly_selected_color) attributes['readonly_selected_color'] = assert_color( readonly_selected_color) # selection_color selection_color = kwargs.pop('selection_color', self._theme.selection_color) attributes['selection_color'] = assert_color(selection_color) # selection_effect selection_effect = kwargs.pop('selection_effect', self._theme.widget_selection_effect) if selection_effect is None: selection_effect = pygame_menu.widgets.NoneSelection() else: selection_effect = selection_effect.copy() assert isinstance(selection_effect, pygame_menu.widgets.core.Selection) selection_effect.set_color(attributes['selection_color']) attributes['selection_effect'] = selection_effect # shadow attributes['shadow_aa'] = kwargs.pop('shadow_aa', self._theme.widget_shadow_aa) attributes['shadow_color'] = kwargs.pop( 'shadow_color', self._theme.widget_shadow_color) attributes['shadow_radius'] = kwargs.pop( 'shadow_radius', self._theme.widget_shadow_radius) attributes['shadow_type'] = kwargs.pop('shadow_type', self._theme.widget_shadow_type) attributes['shadow_width'] = kwargs.pop( 'shadow_width', self._theme.widget_shadow_width) # tab_size attributes['tab_size'] = kwargs.pop('tab_size', self._theme.widget_tab_size) return attributes