diff --git a/pygame_gui/core/drawable_shapes/ellipse_drawable_shape.py b/pygame_gui/core/drawable_shapes/ellipse_drawable_shape.py index 0107ec93..9aaa3041 100644 --- a/pygame_gui/core/drawable_shapes/ellipse_drawable_shape.py +++ b/pygame_gui/core/drawable_shapes/ellipse_drawable_shape.py @@ -154,107 +154,111 @@ def redraw_state(self, state_str: str, add_text: bool = True): :param state_str: The ID string of the state to rebuild. """ - border_colour_state_str = state_str + '_border' - bg_colour_state_str = state_str + '_bg' - text_colour_state_str = state_str + '_text' - text_shadow_colour_state_str = state_str + '_text_shadow' - image_state_str = state_str + '_image' - - found_shape = None - shape_id = None - if 'filled_bar' not in self.theming and 'filled_bar_width_percentage' not in self.theming: - shape_id = self.shape_cache.build_cache_id('ellipse', - self.containing_rect.size, - self.shadow_width, - self.border_width, - self.theming[border_colour_state_str], - self.theming[bg_colour_state_str]) - - found_shape = self.shape_cache.find_surface_in_cache(shape_id) - if found_shape is not None: - self.states[state_str].surface = found_shape.copy() + if self.containing_rect.width <= 0 or self.containing_rect.height <= 0: + self.states[state_str].surface = self.ui_manager.get_universal_empty_surface() else: - self.states[state_str].surface = self.base_surface.copy() - - # Try one AA call method - aa_amount = 4 - self.border_rect = pygame.Rect((self.shadow_width * aa_amount, - self.shadow_width * aa_amount), - (self.click_area_shape.width * aa_amount, - self.click_area_shape.height * aa_amount)) - - self.background_rect = pygame.Rect(((self.border_width + - self.shadow_width) * aa_amount, - (self.border_width + - self.shadow_width) * aa_amount), - (self.border_rect.width - - (2 * self.border_width * aa_amount), - self.border_rect.height - - (2 * self.border_width * aa_amount))) - - bab_surface = pygame.surface.Surface((self.containing_rect.width * aa_amount, - self.containing_rect.height * aa_amount), - flags=pygame.SRCALPHA, - depth=32) - bab_surface.fill(pygame.Color('#00000000')) - if self.border_width > 0: - if isinstance(self.theming[border_colour_state_str], ColourGradient): + + border_colour_state_str = state_str + '_border' + bg_colour_state_str = state_str + '_bg' + text_colour_state_str = state_str + '_text' + text_shadow_colour_state_str = state_str + '_text_shadow' + image_state_str = state_str + '_image' + + found_shape = None + shape_id = None + if 'filled_bar' not in self.theming and 'filled_bar_width_percentage' not in self.theming: + shape_id = self.shape_cache.build_cache_id('ellipse', + self.containing_rect.size, + self.shadow_width, + self.border_width, + self.theming[border_colour_state_str], + self.theming[bg_colour_state_str]) + + found_shape = self.shape_cache.find_surface_in_cache(shape_id) + if found_shape is not None: + self.states[state_str].surface = found_shape.copy() + else: + self.states[state_str].surface = self.base_surface.copy() + + # Try one AA call method + aa_amount = 4 + self.border_rect = pygame.Rect((self.shadow_width * aa_amount, + self.shadow_width * aa_amount), + (self.click_area_shape.width * aa_amount, + self.click_area_shape.height * aa_amount)) + + self.background_rect = pygame.Rect(((self.border_width + + self.shadow_width) * aa_amount, + (self.border_width + + self.shadow_width) * aa_amount), + (self.border_rect.width - + (2 * self.border_width * aa_amount), + self.border_rect.height - + (2 * self.border_width * aa_amount))) + + bab_surface = pygame.surface.Surface((self.containing_rect.width * aa_amount, + self.containing_rect.height * aa_amount), + flags=pygame.SRCALPHA, + depth=32) + bab_surface.fill(pygame.Color('#00000000')) + if self.border_width > 0: + if isinstance(self.theming[border_colour_state_str], ColourGradient): + shape_surface = self.clear_and_create_shape_surface(bab_surface, + self.border_rect, + 0, aa_amount=aa_amount, + clear=False) + self.theming[border_colour_state_str].apply_gradient_to_surface(shape_surface) + else: + shape_surface = self.clear_and_create_shape_surface(bab_surface, + self.border_rect, + 0, aa_amount=aa_amount, + clear=False) + apply_colour_to_surface(self.theming[border_colour_state_str], + shape_surface) + basic_blit(bab_surface, shape_surface, self.border_rect) + if isinstance(self.theming[bg_colour_state_str], ColourGradient): shape_surface = self.clear_and_create_shape_surface(bab_surface, - self.border_rect, - 0, aa_amount=aa_amount, - clear=False) - self.theming[border_colour_state_str].apply_gradient_to_surface(shape_surface) + self.background_rect, 1, + aa_amount=aa_amount) + self.theming[bg_colour_state_str].apply_gradient_to_surface(shape_surface) else: shape_surface = self.clear_and_create_shape_surface(bab_surface, - self.border_rect, - 0, aa_amount=aa_amount, - clear=False) - apply_colour_to_surface(self.theming[border_colour_state_str], - shape_surface) - basic_blit(bab_surface, shape_surface, self.border_rect) - if isinstance(self.theming[bg_colour_state_str], ColourGradient): - shape_surface = self.clear_and_create_shape_surface(bab_surface, - self.background_rect, 1, - aa_amount=aa_amount) - self.theming[bg_colour_state_str].apply_gradient_to_surface(shape_surface) - else: - shape_surface = self.clear_and_create_shape_surface(bab_surface, - self.background_rect, 1, - aa_amount=aa_amount) - apply_colour_to_surface(self.theming[bg_colour_state_str], shape_surface) - - basic_blit(bab_surface, shape_surface, self.background_rect) - # apply AA to background - bab_surface = pygame.transform.smoothscale(bab_surface, self.containing_rect.size) - - # cut a hole in shadow, then blit background into it - sub_surface = pygame.surface.Surface( - ((self.containing_rect.width - (2 * self.shadow_width)) * aa_amount, - (self.containing_rect.height - (2 * self.shadow_width)) * aa_amount), - flags=pygame.SRCALPHA, depth=32) - sub_surface.fill(pygame.Color('#00000000')) - pygame.draw.ellipse(sub_surface, pygame.Color("#FFFFFFFF"), sub_surface.get_rect()) - small_sub = pygame.transform.smoothscale(sub_surface, - (self.containing_rect.width - - (2 * self.shadow_width), - self.containing_rect.height - - (2 * self.shadow_width))) - self.states[state_str].surface.blit(small_sub, pygame.Rect((self.shadow_width, - self.shadow_width), - sub_surface.get_size()), - special_flags=pygame.BLEND_RGBA_SUB) - basic_blit(self.states[state_str].surface, bab_surface, (0, 0)) - - if (shape_id is not None and - self.states[state_str].surface.get_width() <= 1024 and - self.states[state_str].surface.get_height() <= 1024): - self.shape_cache.add_surface_to_cache(self.states[state_str].surface.copy(), - shape_id) - - self.finalise_images_and_text(image_state_str, state_str, - text_colour_state_str, - text_shadow_colour_state_str, - add_text) + self.background_rect, 1, + aa_amount=aa_amount) + apply_colour_to_surface(self.theming[bg_colour_state_str], shape_surface) + + basic_blit(bab_surface, shape_surface, self.background_rect) + # apply AA to background + bab_surface = pygame.transform.smoothscale(bab_surface, self.containing_rect.size) + + # cut a hole in shadow, then blit background into it + sub_surface = pygame.surface.Surface( + ((self.containing_rect.width - (2 * self.shadow_width)) * aa_amount, + (self.containing_rect.height - (2 * self.shadow_width)) * aa_amount), + flags=pygame.SRCALPHA, depth=32) + sub_surface.fill(pygame.Color('#00000000')) + pygame.draw.ellipse(sub_surface, pygame.Color("#FFFFFFFF"), sub_surface.get_rect()) + small_sub = pygame.transform.smoothscale(sub_surface, + (self.containing_rect.width - + (2 * self.shadow_width), + self.containing_rect.height - + (2 * self.shadow_width))) + self.states[state_str].surface.blit(small_sub, pygame.Rect((self.shadow_width, + self.shadow_width), + sub_surface.get_size()), + special_flags=pygame.BLEND_RGBA_SUB) + basic_blit(self.states[state_str].surface, bab_surface, (0, 0)) + + if (shape_id is not None and + self.states[state_str].surface.get_width() <= 1024 and + self.states[state_str].surface.get_height() <= 1024): + self.shape_cache.add_surface_to_cache(self.states[state_str].surface.copy(), + shape_id) + + self.finalise_images_and_text(image_state_str, state_str, + text_colour_state_str, + text_shadow_colour_state_str, + add_text) self.states[state_str].has_fresh_surface = True self.states[state_str].generated = True diff --git a/pygame_gui/core/drawable_shapes/rect_drawable_shape.py b/pygame_gui/core/drawable_shapes/rect_drawable_shape.py index 3b51e94b..d8aaa39d 100644 --- a/pygame_gui/core/drawable_shapes/rect_drawable_shape.py +++ b/pygame_gui/core/drawable_shapes/rect_drawable_shape.py @@ -155,93 +155,96 @@ def redraw_state(self, state_str: str, add_text: bool = True): :param state_str: The ID string of the state to rebuild. """ - border_colour_state_str = state_str + '_border' - bg_colour_state_str = state_str + '_bg' - text_colour_state_str = state_str + '_text' - text_shadow_colour_state_str = state_str + '_text_shadow' - image_state_str = state_str + '_image' - - found_shape = None - shape_id = None - if 'filled_bar' not in self.theming and 'filled_bar_width_percentage' not in self.theming: - shape_id = self.shape_cache.build_cache_id('rectangle', self.containing_rect.size, - self.shadow_width, - self.border_width, - self.theming[border_colour_state_str], - self.theming[bg_colour_state_str]) - - found_shape = self.shape_cache.find_surface_in_cache(shape_id) - if found_shape is not None: - self.states[state_str].surface = found_shape.copy() + if self.containing_rect.width <= 0 or self.containing_rect.height <= 0: + self.states[state_str].surface = self.ui_manager.get_universal_empty_surface() else: - self.states[state_str].surface = self.base_surface.copy() - - if self.border_width > 0: - - if isinstance(self.theming[border_colour_state_str], ColourGradient): - border_shape_surface = pygame.surface.Surface(self.border_rect.size, - flags=pygame.SRCALPHA, depth=32) - border_shape_surface.fill(pygame.Color('#FFFFFFFF')) - self.states[state_str].surface.blit(border_shape_surface, - self.border_rect, - special_flags=pygame.BLEND_RGBA_SUB) - self.theming[border_colour_state_str].apply_gradient_to_surface( - border_shape_surface) - basic_blit(self.states[state_str].surface, - border_shape_surface, self.border_rect) - else: - self.states[state_str].surface.fill(self.theming[border_colour_state_str], - self.border_rect) - - if isinstance(self.theming[bg_colour_state_str], ColourGradient): - background_shape_surface = pygame.surface.Surface(self.background_rect.size, - flags=pygame.SRCALPHA, depth=32) - background_shape_surface.fill(pygame.Color('#FFFFFFFF')) - self.states[state_str].surface.blit(background_shape_surface, - self.background_rect, - special_flags=pygame.BLEND_RGBA_SUB) - self.theming[bg_colour_state_str].apply_gradient_to_surface( - background_shape_surface) - basic_blit(self.states[state_str].surface, - background_shape_surface, self.background_rect) + border_colour_state_str = state_str + '_border' + bg_colour_state_str = state_str + '_bg' + text_colour_state_str = state_str + '_text' + text_shadow_colour_state_str = state_str + '_text_shadow' + image_state_str = state_str + '_image' + + found_shape = None + shape_id = None + if 'filled_bar' not in self.theming and 'filled_bar_width_percentage' not in self.theming: + shape_id = self.shape_cache.build_cache_id('rectangle', self.containing_rect.size, + self.shadow_width, + self.border_width, + self.theming[border_colour_state_str], + self.theming[bg_colour_state_str]) + + found_shape = self.shape_cache.find_surface_in_cache(shape_id) + if found_shape is not None: + self.states[state_str].surface = found_shape.copy() else: - self.states[state_str].surface.fill(self.theming[bg_colour_state_str], - self.background_rect) - - if 'filled_bar' in self.theming and 'filled_bar_width_percentage' in self.theming: - bar_rect = pygame.Rect(self.background_rect.topleft, - (int(self.theming['filled_bar_width_percentage'] * - self.background_rect.width), - self.background_rect.height)) - if isinstance(self.theming['filled_bar'], ColourGradient): - bar_shape_surface = pygame.surface.Surface(bar_rect.size, - flags=pygame.SRCALPHA, - depth=32) - bar_shape_surface.fill(pygame.Color('#FFFFFFFF')) - self.states[state_str].surface.blit(bar_shape_surface, bar_rect, + self.states[state_str].surface = self.base_surface.copy() + + if self.border_width > 0: + + if isinstance(self.theming[border_colour_state_str], ColourGradient): + border_shape_surface = pygame.surface.Surface(self.border_rect.size, + flags=pygame.SRCALPHA, depth=32) + border_shape_surface.fill(pygame.Color('#FFFFFFFF')) + self.states[state_str].surface.blit(border_shape_surface, + self.border_rect, + special_flags=pygame.BLEND_RGBA_SUB) + self.theming[border_colour_state_str].apply_gradient_to_surface( + border_shape_surface) + basic_blit(self.states[state_str].surface, + border_shape_surface, self.border_rect) + else: + self.states[state_str].surface.fill(self.theming[border_colour_state_str], + self.border_rect) + + if isinstance(self.theming[bg_colour_state_str], ColourGradient): + background_shape_surface = pygame.surface.Surface(self.background_rect.size, + flags=pygame.SRCALPHA, depth=32) + background_shape_surface.fill(pygame.Color('#FFFFFFFF')) + self.states[state_str].surface.blit(background_shape_surface, + self.background_rect, special_flags=pygame.BLEND_RGBA_SUB) - self.theming['filled_bar'].apply_gradient_to_surface(bar_shape_surface) + self.theming[bg_colour_state_str].apply_gradient_to_surface( + background_shape_surface) basic_blit(self.states[state_str].surface, - bar_shape_surface, bar_rect) + background_shape_surface, self.background_rect) else: - self.states[state_str].surface.fill(self.theming['filled_bar'], bar_rect) - - if self.states[state_str].cached_background_id is not None: - self.shape_cache.remove_user_from_cache_item( - self.states[state_str].cached_background_id) - if (not self.has_been_resized - and ((self.containing_rect.width * self.containing_rect.height) < 40000) - and (shape_id is not None - and self.states[state_str].surface.get_width() <= 1024 - and self.states[state_str].surface.get_height() <= 1024)): - self.shape_cache.add_surface_to_cache(self.states[state_str].surface.copy(), - shape_id) - self.states[state_str].cached_background_id = shape_id - - self.finalise_images_and_text(image_state_str, state_str, - text_colour_state_str, - text_shadow_colour_state_str, - add_text) + self.states[state_str].surface.fill(self.theming[bg_colour_state_str], + self.background_rect) + + if 'filled_bar' in self.theming and 'filled_bar_width_percentage' in self.theming: + bar_rect = pygame.Rect(self.background_rect.topleft, + (int(self.theming['filled_bar_width_percentage'] * + self.background_rect.width), + self.background_rect.height)) + if isinstance(self.theming['filled_bar'], ColourGradient): + bar_shape_surface = pygame.surface.Surface(bar_rect.size, + flags=pygame.SRCALPHA, + depth=32) + bar_shape_surface.fill(pygame.Color('#FFFFFFFF')) + self.states[state_str].surface.blit(bar_shape_surface, bar_rect, + special_flags=pygame.BLEND_RGBA_SUB) + self.theming['filled_bar'].apply_gradient_to_surface(bar_shape_surface) + basic_blit(self.states[state_str].surface, + bar_shape_surface, bar_rect) + else: + self.states[state_str].surface.fill(self.theming['filled_bar'], bar_rect) + + if self.states[state_str].cached_background_id is not None: + self.shape_cache.remove_user_from_cache_item( + self.states[state_str].cached_background_id) + if (not self.has_been_resized + and ((self.containing_rect.width * self.containing_rect.height) < 40000) + and (shape_id is not None + and self.states[state_str].surface.get_width() <= 1024 + and self.states[state_str].surface.get_height() <= 1024)): + self.shape_cache.add_surface_to_cache(self.states[state_str].surface.copy(), + shape_id) + self.states[state_str].cached_background_id = shape_id + + self.finalise_images_and_text(image_state_str, state_str, + text_colour_state_str, + text_shadow_colour_state_str, + add_text) self.states[state_str].has_fresh_surface = True self.states[state_str].generated = True diff --git a/pygame_gui/core/drawable_shapes/rounded_rect_drawable_shape.py b/pygame_gui/core/drawable_shapes/rounded_rect_drawable_shape.py index 8085b4aa..13e07e4c 100644 --- a/pygame_gui/core/drawable_shapes/rounded_rect_drawable_shape.py +++ b/pygame_gui/core/drawable_shapes/rounded_rect_drawable_shape.py @@ -256,35 +256,39 @@ def set_dimensions(self, dimensions: Union[pygame.math.Vector2, self.click_area_shape.width = int(dimensions[0]) - (2 * self.shadow_width) self.click_area_shape.height = int(dimensions[1]) - (2 * self.shadow_width) - if self.shadow_width > 0: - quick_surf = self.ui_manager.get_shadow(self.containing_rect.size, - self.shadow_width, - 'rectangle', - corner_radius=self.shadow_width + 2) - else: - quick_surf = pygame.surface.Surface(self.containing_rect.size, - flags=pygame.SRCALPHA, - depth=32) - quick_surf.fill(pygame.Color('#00000000')) - if isinstance(self.theming['normal_bg'], ColourGradient): - grad_surf = pygame.surface.Surface(self.click_area_shape.size, - flags=pygame.SRCALPHA, - depth=32) - grad_surf.fill(pygame.Color('#FFFFFFFF')) - self.theming['normal_bg'].apply_gradient_to_surface(grad_surf) - - basic_blit(quick_surf, grad_surf, - pygame.Rect((self.shadow_width, - self.shadow_width), - self.click_area_shape.size)) + if dimensions[0] <= 0 or dimensions[1] <= 0: + self.states['normal'].surface = self.ui_manager.get_universal_empty_surface() else: - quick_surf.fill(self.theming['normal_bg'], pygame.Rect((self.shadow_width, - self.shadow_width), - self.click_area_shape.size)) - - self.states['normal'].surface = quick_surf - self.finalise_images_and_text('normal_image', 'normal', - 'normal_text', 'normal_text_shadow', True) + if self.shadow_width > 0: + quick_surf = self.ui_manager.get_shadow(self.containing_rect.size, + self.shadow_width, + 'rectangle', + corner_radius=self.shadow_width + 2) + else: + quick_surf = pygame.surface.Surface(self.containing_rect.size, + flags=pygame.SRCALPHA, + depth=32) + quick_surf.fill(pygame.Color('#00000000')) + if isinstance(self.theming['normal_bg'], ColourGradient): + grad_surf = pygame.surface.Surface(self.click_area_shape.size, + flags=pygame.SRCALPHA, + depth=32) + grad_surf.fill(pygame.Color('#FFFFFFFF')) + self.theming['normal_bg'].apply_gradient_to_surface(grad_surf) + + basic_blit(quick_surf, grad_surf, + pygame.Rect((self.shadow_width, + self.shadow_width), + self.click_area_shape.size)) + else: + quick_surf.fill(self.theming['normal_bg'], pygame.Rect((self.shadow_width, + self.shadow_width), + self.click_area_shape.size)) + + self.states['normal'].surface = quick_surf + self.finalise_images_and_text('normal_image', 'normal', + 'normal_text', + 'normal_text_shadow', True) self.states['normal'].has_fresh_surface = True self.has_been_resized = True @@ -301,104 +305,107 @@ def redraw_state(self, state_str: str, add_text: bool = True): :param state_str: The ID string of the state to rebuild. """ - text_colour_state_str = state_str + '_text' - text_shadow_colour_state_str = state_str + '_text_shadow' - bg_col = self.theming[state_str + '_bg'] - border_col = self.theming[state_str + '_border'] - - found_shape = None - shape_id = None - if 'filled_bar' not in self.theming and 'filled_bar_width_percentage' not in self.theming: - shape_id = self.shape_cache.build_cache_id('rounded_rectangle', - self.containing_rect.size, - self.shadow_width, - self.border_width, - border_col, - bg_col, - self.corner_radius) - - found_shape = self.shape_cache.find_surface_in_cache(shape_id) - if found_shape is not None: - self.states[state_str].surface = found_shape.copy() + if self.containing_rect.width <= 0 or self.containing_rect.height <= 0: + self.states[state_str].surface = self.ui_manager.get_universal_empty_surface() else: - # border_corner_radius = self.corner_radius - if self.base_surface is not None: - self.states[state_str].surface = self.base_surface.copy() - - # Try one AA call method - aa_amount = 4 - self.border_rect = pygame.Rect((self.shadow_width * aa_amount, - self.shadow_width * aa_amount), - (self.click_area_shape.width * aa_amount, - self.click_area_shape.height * aa_amount)) - - self.background_rect = pygame.Rect(((self.border_width + - self.shadow_width) * aa_amount, - (self.border_width + - self.shadow_width) * aa_amount), - (self.border_rect.width - - (2 * self.border_width * aa_amount), - self.border_rect.height - - (2 * self.border_width * aa_amount))) - - dimension_scale = min(self.background_rect.width / max(self.border_rect.width, 1), - self.background_rect.height / max(self.border_rect.height, 1)) - bg_corner_radius = int(self.corner_radius * dimension_scale) - - bab_surface = pygame.surface.Surface((self.containing_rect.width * aa_amount, - self.containing_rect.height * aa_amount), - flags=pygame.SRCALPHA, depth=32) - bab_surface.fill(pygame.Color('#00000000')) - if self.border_width > 0: + text_colour_state_str = state_str + '_text' + text_shadow_colour_state_str = state_str + '_text_shadow' + bg_col = self.theming[state_str + '_bg'] + border_col = self.theming[state_str + '_border'] + + found_shape = None + shape_id = None + if 'filled_bar' not in self.theming and 'filled_bar_width_percentage' not in self.theming: + shape_id = self.shape_cache.build_cache_id('rounded_rectangle', + self.containing_rect.size, + self.shadow_width, + self.border_width, + border_col, + bg_col, + self.corner_radius) + + found_shape = self.shape_cache.find_surface_in_cache(shape_id) + if found_shape is not None: + self.states[state_str].surface = found_shape.copy() + else: + # border_corner_radius = self.corner_radius + if self.base_surface is not None: + self.states[state_str].surface = self.base_surface.copy() + + # Try one AA call method + aa_amount = 4 + self.border_rect = pygame.Rect((self.shadow_width * aa_amount, + self.shadow_width * aa_amount), + (self.click_area_shape.width * aa_amount, + self.click_area_shape.height * aa_amount)) + + self.background_rect = pygame.Rect(((self.border_width + + self.shadow_width) * aa_amount, + (self.border_width + + self.shadow_width) * aa_amount), + (self.border_rect.width - + (2 * self.border_width * aa_amount), + self.border_rect.height - + (2 * self.border_width * aa_amount))) + + dimension_scale = min(self.background_rect.width / max(self.border_rect.width, 1), + self.background_rect.height / max(self.border_rect.height, 1)) + bg_corner_radius = int(self.corner_radius * dimension_scale) + + bab_surface = pygame.surface.Surface((self.containing_rect.width * aa_amount, + self.containing_rect.height * aa_amount), + flags=pygame.SRCALPHA, depth=32) + bab_surface.fill(pygame.Color('#00000000')) + if self.border_width > 0: + shape_surface = self.clear_and_create_shape_surface(bab_surface, + self.border_rect, + 0, + self.corner_radius, + aa_amount=aa_amount, + clear=False) + if isinstance(border_col, ColourGradient): + border_col.apply_gradient_to_surface(shape_surface) + else: + apply_colour_to_surface(border_col, shape_surface) + + basic_blit(bab_surface, shape_surface, self.border_rect) + shape_surface = self.clear_and_create_shape_surface(bab_surface, - self.border_rect, + self.background_rect, 0, - self.corner_radius, - aa_amount=aa_amount, - clear=False) - if isinstance(border_col, ColourGradient): - border_col.apply_gradient_to_surface(shape_surface) - else: - apply_colour_to_surface(border_col, shape_surface) + bg_corner_radius, + aa_amount=aa_amount) - basic_blit(bab_surface, shape_surface, self.border_rect) - - shape_surface = self.clear_and_create_shape_surface(bab_surface, - self.background_rect, - 0, - bg_corner_radius, - aa_amount=aa_amount) - - if 'filled_bar' in self.theming and 'filled_bar_width_percentage' in self.theming: - self._redraw_filled_bar(bg_col, shape_surface) - else: - if isinstance(bg_col, ColourGradient): - bg_col.apply_gradient_to_surface(shape_surface) + if 'filled_bar' in self.theming and 'filled_bar_width_percentage' in self.theming: + self._redraw_filled_bar(bg_col, shape_surface) else: - apply_colour_to_surface(bg_col, shape_surface) - - basic_blit(bab_surface, shape_surface, self.background_rect) - - # apply AA to background - bab_surface = pygame.transform.smoothscale(bab_surface, self.containing_rect.size) - - basic_blit(self.states[state_str].surface, bab_surface, (0, 0)) - - if self.states[state_str].cached_background_id is not None: - cached_id = self.states[state_str].cached_background_id - self.shape_cache.remove_user_from_cache_item(cached_id) - if (not self.has_been_resized - and ((self.containing_rect.width * self.containing_rect.height) < 40000) - and (shape_id is not None - and self.states[state_str].surface.get_width() <= 1024 - and self.states[state_str].surface.get_height() <= 1024)): - self.shape_cache.add_surface_to_cache(self.states[state_str].surface.copy(), - shape_id) - self.states[state_str].cached_background_id = shape_id - - self.finalise_images_and_text(state_str + '_image', state_str, - text_colour_state_str, - text_shadow_colour_state_str, add_text) + if isinstance(bg_col, ColourGradient): + bg_col.apply_gradient_to_surface(shape_surface) + else: + apply_colour_to_surface(bg_col, shape_surface) + + basic_blit(bab_surface, shape_surface, self.background_rect) + + # apply AA to background + bab_surface = pygame.transform.smoothscale(bab_surface, self.containing_rect.size) + + basic_blit(self.states[state_str].surface, bab_surface, (0, 0)) + + if self.states[state_str].cached_background_id is not None: + cached_id = self.states[state_str].cached_background_id + self.shape_cache.remove_user_from_cache_item(cached_id) + if (not self.has_been_resized + and ((self.containing_rect.width * self.containing_rect.height) < 40000) + and (shape_id is not None + and self.states[state_str].surface.get_width() <= 1024 + and self.states[state_str].surface.get_height() <= 1024)): + self.shape_cache.add_surface_to_cache(self.states[state_str].surface.copy(), + shape_id) + self.states[state_str].cached_background_id = shape_id + + self.finalise_images_and_text(state_str + '_image', state_str, + text_colour_state_str, + text_shadow_colour_state_str, add_text) self.states[state_str].has_fresh_surface = True self.states[state_str].generated = True diff --git a/pygame_gui/core/ui_element.py b/pygame_gui/core/ui_element.py index 92061a17..5f243828 100644 --- a/pygame_gui/core/ui_element.py +++ b/pygame_gui/core/ui_element.py @@ -1180,6 +1180,12 @@ def _set_image_clip(self, rect: Union[pygame.Rect, None]): else: self._image_clip = None + def _get_pre_clipped_image_size(self) -> Coordinate: + if self._pre_clipped_image is not None: + return self._pre_clipped_image.get_size() + else: + return 0, 0 + def get_image_clipping_rect(self) -> Union[pygame.Rect, None]: """ Obtain the current image clipping rect. diff --git a/pygame_gui/elements/ui_button.py b/pygame_gui/elements/ui_button.py index 7279f100..1383a3c7 100644 --- a/pygame_gui/elements/ui_button.py +++ b/pygame_gui/elements/ui_button.py @@ -775,7 +775,10 @@ def rebuild(self): def _calc_dynamic_size(self): if self.dynamic_width or self.dynamic_height: - self._set_dimensions(self.image.get_size()) + if self.image.get_size() == (0, 0): + self._set_dimensions(self._get_pre_clipped_image_size()) + else: + self._set_dimensions(self.image.get_size()) # if we have anchored the left side of our button to the right of its container then # changing the width is going to mess up the horiz position as well. diff --git a/pygame_gui/elements/ui_label.py b/pygame_gui/elements/ui_label.py index 1d53c9c5..00938fcc 100644 --- a/pygame_gui/elements/ui_label.py +++ b/pygame_gui/elements/ui_label.py @@ -163,7 +163,10 @@ def rebuild(self): def _calc_dynamic_size(self): if self.dynamic_width or self.dynamic_height: - self._set_dimensions(self.image.get_size()) + if self.image.get_size() == (0, 0): + self._set_dimensions(self._get_pre_clipped_image_size()) + else: + self._set_dimensions(self.image.get_size()) # if we have anchored the left side of our button to the right of its container then # changing the width is going to mess up the horiz position as well.