From 65690ca90ced48ff215c587a6f4cbb47be1c13c1 Mon Sep 17 00:00:00 2001 From: LoafyLemon Date: Wed, 16 Oct 2024 20:59:14 +0100 Subject: [PATCH] Improve icon generation and whitespace detection --- game/scripts/doll/clothes.rpy | 26 +++++++++++++------------- game/scripts/doll/clothes_dynamic.rpy | 21 +++++++++++++-------- game/scripts/utility/image_crop.rpy | 9 +++++++++ 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/game/scripts/doll/clothes.rpy b/game/scripts/doll/clothes.rpy index 70143969..411fd244 100644 --- a/game/scripts/doll/clothes.rpy +++ b/game/scripts/doll/clothes.rpy @@ -182,27 +182,27 @@ init python: matrix = SaturationMatrix(0.0) sprites = [i for i in self.build_image(hash, matrix=matrix) if not i[0] == "mask"] - try: - bounds = self.get_layers(hash).get("outline", [sprites[0][1]])[0] - except IndexError: - print(f"Missing textures:\n{self.__repr__()}") - return Text(f"TexErr\n{{color=#00ffff}}{{size=-6}}ID:{self.id}{{/size}}{{/color}}", color="#ff0000") + icon_sprite = Fixed(*[i[1] for i in sprites], fit_first=True) + sprites.extend(self.char.body.build_image(self.char.body._hash, matrix=matrix)) sprites.sort(key=itemgetter(2)) wmax, hmax = self.sizes - wmin = hmin = 256 + wmin = hmin = 128 + padding = 32 - x, y, w, h = crop_whitespace(bounds) - xoffset, yoffset = w/2, h/2 + x, y, w, h = crop_whitespace_surface(icon_sprite) + xoffset, yoffset = w//2, h//2 + w = h = max(w, h) - w = h = max(w, h, wmin, hmin) + if w == 0 or h == 0: + return Null() - w = max(wmin, w + w/2) - h = max(hmin, h + h/2) + w = max(wmin, w + padding) + h = max(hmin, h + padding) - x = clamp( (x - w/2) + xoffset, 0, wmax) - y = clamp( (y - h/2) + yoffset, 0, hmax) + x = clamp( (x - w//2) + xoffset, 0, wmax) + y = clamp( (y - h//2) + yoffset, 0, hmax) # Forbid exceeding the image height. if y+h > hmax: diff --git a/game/scripts/doll/clothes_dynamic.rpy b/game/scripts/doll/clothes_dynamic.rpy index 6ecd49d3..5fa38c32 100644 --- a/game/scripts/doll/clothes_dynamic.rpy +++ b/game/scripts/doll/clothes_dynamic.rpy @@ -170,6 +170,8 @@ init python: matrix = SaturationMatrix(0.0) sprites = [i for i in self.build_image(hash, matrix=matrix) if not i[0] == "mask"] + icon_sprite = Fixed(*[i[1] for i in sprites], fit_first=True) + if not tracking_object is None: sprites.extend([i for i in tracking_object.build_image(tracking_object._hash, matrix=matrix) if not i[0] == "mask"]) @@ -178,18 +180,21 @@ init python: bounds = self.get_layers(hash).get("outline", [sprites[0][1]])[0] wmax, hmax = self.sizes - wmin = hmin = 96 + wmin = hmin = 128 + padding = 32 - x, y, w, h = crop_whitespace(bounds) - xoffset, yoffset = w/2, h/2 + x, y, w, h = crop_whitespace_surface(icon_sprite) + xoffset, yoffset = w//2, h//2 + w = h = max(w, h) - w = h = max(w, h, wmin, hmin) + if w == 0 or h == 0: + return Null() - w = max(wmin, w + w/2) - h = max(hmin, h + h/2) + w = max(wmin, w + padding) + h = max(hmin, h + padding) - x = clamp( (x - w/2) + xoffset, 0, wmax) - y = clamp( (y - h/2) + yoffset, 0, hmax) + x = clamp( (x - w//2) + xoffset, 0, wmax) + y = clamp( (y - h//2) + yoffset, 0, hmax) # Forbid exceeding the image height. if y+h > hmax: diff --git a/game/scripts/utility/image_crop.rpy b/game/scripts/utility/image_crop.rpy index 7a6f4e48..46c2c6e3 100644 --- a/game/scripts/utility/image_crop.rpy +++ b/game/scripts/utility/image_crop.rpy @@ -31,6 +31,15 @@ init python: whitespace_dict[path] = tuple(map(int, box)) return box + def crop_whitespace_surface(displayable): + surf = renpy.render_to_surface(displayable, width=1010, height=1200) + size = surf.get_size() + box = tuple(surf.get_bounding_rect()) + if size[0] != 1010: + ratio = size[0]/1010 + box = tuple(v//ratio for v in box) + return box + def crop_image_zoom(path, xsize, ysize, grayscale=False): x, y, w, h = crop_whitespace(path) matrix = SaturationMatrix(0)*BrightnessMatrix(-0.25)*OpacityMatrix(0.5) if grayscale else None