Skip to content

Commit 1a691ea

Browse files
feat(rendering): rendering shape regions with original size
1 parent 863fa0d commit 1a691ea

File tree

4 files changed

+34
-33
lines changed

4 files changed

+34
-33
lines changed

system/lib/console.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ def ask_integer(message: str):
2121

2222
@staticmethod
2323
def question(message):
24-
answer = None
25-
while not answer in 'ny':
24+
while True:
2625
answer = input(f'[????] {message} [Y/n] ').lower()
26+
if answer in 'ny':
27+
break
2728

2829
return 'ny'.index(answer)
2930

system/lib/features/cut_sprites.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ def cut_sprites(swf: SupercellSWF, export_folder: str):
5050
for region_index in range(regions_count):
5151
region = shape.regions[region_index]
5252

53-
region.apply_matrix(None)
54-
rendered_region = region.render()
53+
rendered_region = region.render(use_original_size=True)
5554
rendered_region.save(f'{export_folder}/shape_{shape.id}_{region_index}.png')
5655

5756
for shape_index in range(shapes_count):

system/lib/features/place_sprites.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from PIL import Image, ImageDraw
44

55
from system.lib import Console
6+
from system.lib.helper import get_sides, get_size
67
from system.lib.images import get_format_by_pixel_type
78
from system.lib.xcod import parse_info, FileInfo
89
from system.localization import locale
@@ -33,27 +34,23 @@ def place_sprites(xcod_path: str, folder: str, overwrite: bool = False) -> (list
3334
for region_index in range(regions_count):
3435
texture_id, points_count = xcod.read_uchar(), xcod.read_uchar()
3536
texture_width, texture_height = sheets[texture_id].width, sheets[texture_id].height
36-
polygon = [(xcod.read_ushort(), xcod.read_ushort()) for _ in range(points_count)]
37+
points = [(xcod.read_ushort(), xcod.read_ushort()) for _ in range(points_count)]
3738
mirroring, rotation = xcod.read_uchar() == 1, xcod.read_char() * 90
3839

3940
filename = f'shape_{shape_id}_{region_index}.png'
4041
if filename not in files_to_overwrite:
4142
continue
4243

43-
tmp_region = Image.open(
44-
f'{folder}{"/overwrite" if overwrite else ""}/{filename}'
45-
).convert('RGBA').rotate(-rotation, expand=True)
46-
4744
img_mask = Image.new('L', (texture_width, texture_height), 0)
4845
color = 255
49-
ImageDraw.Draw(img_mask).polygon(polygon, fill=color)
46+
ImageDraw.Draw(img_mask).polygon(points, fill=color)
5047
bbox = img_mask.getbbox()
5148

5249
if not bbox:
53-
min_x = min(i[0] for i in polygon)
54-
min_y = min(i[1] for i in polygon)
55-
max_x = max(i[0] for i in polygon)
56-
max_y = max(i[1] for i in polygon)
50+
min_x = min(i[0] for i in points)
51+
min_y = min(i[1] for i in points)
52+
max_x = max(i[0] for i in points)
53+
max_y = max(i[1] for i in points)
5754

5855
if max_y - min_y != 0:
5956
for _y in range(max_y - min_y):
@@ -64,25 +61,26 @@ def place_sprites(xcod_path: str, folder: str, overwrite: bool = False) -> (list
6461
img_mask.putpixel((min_x + _x - 1, max_y - 1), color)
6562
else:
6663
img_mask.putpixel((max_x - 1, max_y - 1), color)
67-
bbox = img_mask.getbbox()
6864

69-
left, top, right, bottom = bbox
70-
if right - left - 1:
71-
right -= 1
72-
if bottom - top - 1:
73-
bottom -= 1
65+
left, top, right, bottom = get_sides(points)
66+
if left == right:
67+
right += 1
68+
if top == bottom:
69+
bottom += 1
7470

75-
bbox = left, top, right, bottom
71+
width, height = get_size(left, top, right, bottom)
7672

77-
width = right - left
78-
height = bottom - top
79-
region_size = width, height
80-
tmp_region = tmp_region.resize(region_size, Image.ANTIALIAS)
73+
bbox = left, top, right, bottom
8174

75+
tmp_region = Image.open(
76+
f'{folder}{"/overwrite" if overwrite else ""}/{filename}'
77+
).convert('RGBA')
8278
if mirroring:
8379
tmp_region = tmp_region.transpose(Image.FLIP_LEFT_RIGHT)
80+
tmp_region = tmp_region.rotate(rotation, expand=True)
81+
tmp_region = tmp_region.resize((width, height), Image.ANTIALIAS)
8482

85-
sheets[texture_id].paste(Image.new('RGBA', region_size), (left, top), img_mask.crop(bbox))
83+
sheets[texture_id].paste(Image.new('RGBA', (width, height)), (left, top), img_mask.crop(bbox))
8684
sheets[texture_id].paste(tmp_region, (left, top), tmp_region)
8785
print()
8886

system/lib/objects/shape.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from math import ceil, degrees, atan2
2-
from typing import List, Tuple
2+
from typing import List, Tuple, Optional
33

44
from PIL import Image, ImageDraw
55

@@ -125,8 +125,8 @@ def load(self, swf, tag: int):
125125

126126
self._uv_points[i].position = (u_rounded, v_rounded)
127127

128-
def render(self):
129-
self._transformed_points = self._xy_points
128+
def render(self, use_original_size: bool = False) -> Image.Image:
129+
self.apply_matrix(None)
130130

131131
left, top, right, bottom = self.get_sides()
132132
width, height = get_size(left, top, right, bottom)
@@ -150,8 +150,9 @@ def render(self):
150150
rendered_region = rendered_region.rotate(-self.rotation, expand=True)
151151
if self.is_mirrored:
152152
rendered_region = rendered_region.transpose(Image.FLIP_LEFT_RIGHT)
153-
rendered_region = rendered_region.resize((width, height), Image.ANTIALIAS)
154-
return rendered_region
153+
if use_original_size:
154+
return rendered_region
155+
return rendered_region.resize((width, height), Image.ANTIALIAS)
155156

156157
def get_image(self) -> Image:
157158
left, top, right, bottom = get_sides(self._uv_points)
@@ -205,7 +206,7 @@ def get_position(self) -> Tuple[float, float]:
205206
def get_sides(self) -> Tuple[float, float, float, float]:
206207
return get_sides(self._transformed_points)
207208

208-
def apply_matrix(self, matrix: Matrix2x3 = None) -> None:
209+
def apply_matrix(self, matrix: Optional[Matrix2x3] = None) -> None:
209210
"""Applies affine matrix to shape (xy) points. If matrix is none, copies the points.
210211
211212
:param matrix: Affine matrix
@@ -220,7 +221,9 @@ def apply_matrix(self, matrix: Matrix2x3 = None) -> None:
220221
matrix.apply_y(point.x, point.y)
221222
))
222223

223-
def calculate_rotation(self, round_to_nearest: bool = False, custom_points: List[Point] = None) -> (int, bool):
224+
def calculate_rotation(self,
225+
round_to_nearest: bool = False,
226+
custom_points: List[Point] = None) -> (int, bool):
224227
"""Calculates rotation and if region is mirrored.
225228
226229
:param round_to_nearest: should round to a multiple of 90

0 commit comments

Comments
 (0)