Skip to content

Commit 83e2ce6

Browse files
little addition of type hinting, some new names
1 parent dfb113a commit 83e2ce6

File tree

10 files changed

+95
-92
lines changed

10 files changed

+95
-92
lines changed

system/lib/console.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
1-
import sys
2-
3-
41
class Console:
52
@staticmethod
6-
def progress_bar(message, current, total, start=0, end=100):
7-
sys.stdout.write(f"\r[{((current + 1) * end + start) // total + start}%] {message}")
3+
def progress_bar(message, current, total):
4+
percentage = (current + 1) * 100 // total
5+
print('\r', end='')
6+
print(f'[{percentage}%] {message}', end='')
7+
if current + 1 == total:
8+
print()
89

910
@staticmethod
10-
def percent(current, total):
11+
def percent(current: int, total: int) -> int:
1112
return (current + 1) * 100 // total
1213

1314
@staticmethod
14-
def ask_integer(message):
15-
try:
16-
return int(input(f'[????] {message}: '))
17-
except ValueError:
18-
return Console.ask_integer(message)
15+
def ask_integer(message: str):
16+
while True:
17+
try:
18+
return int(input(f'[????] {message}: '))
19+
except ValueError:
20+
pass
1921

2022
@staticmethod
2123
def question(message):
22-
answer = input(f'[????] {message} [Y/n] ').lower()
23-
if answer in 'ny':
24-
return 'ny'.index(answer)
25-
else:
26-
return Console.question(message)
24+
answer = None
25+
while not answer in 'ny':
26+
answer = input(f'[????] {message} [Y/n] ').lower()
27+
28+
return 'ny'.index(answer)
2729

2830

2931
if __name__ == '__main__':
3032
console = Console()
31-
console.ask_integer('Please, type any integer: ')
33+
console.ask_integer('Please, type any integer')
34+
35+
for i in range(1000):
36+
console.progress_bar('Test progress bar', i, 1000)

system/lib/features/cut_sprites.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from system.localization import locale
66

77

8-
def cut_sprites(swf: SupercellSWF, folder_export):
9-
os.makedirs(f'{folder_export}/overwrite', exist_ok=True)
10-
os.makedirs(f'{folder_export}/shapes', exist_ok=True)
8+
def cut_sprites(swf: SupercellSWF, export_folder: str):
9+
os.makedirs(f'{export_folder}/overwrite', exist_ok=True)
10+
os.makedirs(f'{export_folder}/shapes', exist_ok=True)
1111

1212
shapes_count = len(swf.shapes)
1313
swf.xcod_writer.write_uint16(shapes_count)
@@ -22,7 +22,7 @@ def cut_sprites(swf: SupercellSWF, folder_export):
2222
shape = swf.shapes[shape_index]
2323

2424
rendered_shape = shape.render(swf)
25-
rendered_shape.save(f'{folder_export}/shapes/{shape.id}.png')
25+
rendered_shape.save(f'{export_folder}/shapes/{shape.id}.png')
2626

2727
regions_count = len(shape.regions)
2828
swf.xcod_writer.write_uint16(shape.id)
@@ -40,5 +40,4 @@ def cut_sprites(swf: SupercellSWF, folder_export):
4040
swf.xcod_writer.write_ubyte(region.rotation // 90)
4141

4242
rendered_region = region.render(swf)
43-
rendered_region.save(f'{folder_export}/shape_{shape.id}_{region_index}.png')
44-
print()
43+
rendered_region.save(f'{export_folder}/shape_{shape.id}_{region_index}.png')

system/lib/features/place_sprites.py

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

55
from system.lib import Console
6-
from system.lib.images import pixel_type2str
6+
from system.lib.images import get_format_by_pixel_type
77
from system.lib.xcod import parse_info, FileInfo
88
from system.localization import locale
99

@@ -21,7 +21,7 @@ def place_sprites(xcod_path: str, folder: str, overwrite: bool = False) -> (list
2121
sheets.append(
2222
Image.open(f'{folder}/textures/{texture_files[i]}')
2323
if overwrite else
24-
Image.new(pixel_type2str(sheet_info.pixel_type), sheet_info.size)
24+
Image.new(get_format_by_pixel_type(sheet_info.pixel_type), sheet_info.size)
2525
)
2626

2727
shapes_count = xcod.read_uint16()

system/lib/features/sc/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ def compile_sc(_dir, file_info: FileInfo, sheets: list = None, output_folder: st
5656

5757
if file_type in (27, 28):
5858
split_image(img)
59-
print()
6059

6160
save_texture(sc, img, pixel_type)
62-
print()
6361

6462
sc.write(bytes(5))
6563
print()

system/lib/images.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
from system.lib.console import Console
88
from system.localization import locale
99

10+
CHUNK_SIZE = 32
1011

11-
def load_image_from_buffer(img):
12+
13+
def load_image_from_buffer(img: Image) -> None:
1214
width, height = img.size
1315
img_loaded = img.load()
1416

@@ -20,26 +22,25 @@ def load_image_from_buffer(img):
2022
img_loaded[x, y] = tuple(pixel_buffer.read(channels_count))
2123

2224

23-
def join_image(img):
25+
def join_image(img: Image) -> None:
2426
with open('pixel_buffer', 'rb') as pixel_buffer:
2527
channels_count = int.from_bytes(pixel_buffer.read(1), 'little')
2628

2729
width, height = img.size
2830
loaded_img = img.load()
29-
chunk_size = 32
3031

31-
x_chunks_count = width // chunk_size
32-
y_chunks_count = height // chunk_size
32+
x_chunks_count = width // CHUNK_SIZE
33+
y_chunks_count = height // CHUNK_SIZE
3334

3435
for y_chunk in range(y_chunks_count + 1):
3536
for x_chunk in range(x_chunks_count + 1):
36-
for y in range(chunk_size):
37-
pixel_y = y_chunk * chunk_size + y
37+
for y in range(CHUNK_SIZE):
38+
pixel_y = y_chunk * CHUNK_SIZE + y
3839
if pixel_y >= height:
3940
break
4041

41-
for x in range(chunk_size):
42-
pixel_x = x_chunk * chunk_size + x
42+
for x in range(CHUNK_SIZE):
43+
pixel_x = x_chunk * CHUNK_SIZE + x
4344
if pixel_x >= width:
4445
break
4546

@@ -55,22 +56,21 @@ def add_pixel(pixel: tuple):
5556
width, height = img.size
5657
loaded_image = img.load()
5758
loaded_clone = img.copy().load()
58-
chunk_size = 32
5959

60-
x_chunks_count = width // chunk_size
61-
y_chunks_count = height // chunk_size
60+
x_chunks_count = width // CHUNK_SIZE
61+
y_chunks_count = height // CHUNK_SIZE
6262

6363
pixel_index = 0
6464

6565
for y_chunk in range(y_chunks_count + 1):
6666
for x_chunk in range(x_chunks_count + 1):
67-
for y in range(chunk_size):
68-
pixel_y = (y_chunk * chunk_size) + y
67+
for y in range(CHUNK_SIZE):
68+
pixel_y = (y_chunk * CHUNK_SIZE) + y
6969
if pixel_y >= height:
7070
break
7171

72-
for x in range(chunk_size):
73-
pixel_x = (x_chunk * chunk_size) + x
72+
for x in range(CHUNK_SIZE):
73+
pixel_x = (x_chunk * CHUNK_SIZE) + x
7474
if pixel_x >= width:
7575
break
7676

@@ -90,7 +90,7 @@ def get_pixel_size(_type):
9090
raise Exception(locale.unk_type % _type)
9191

9292

93-
def pixel_type2str(_type):
93+
def get_format_by_pixel_type(_type):
9494
if _type in (0, 1, 2, 3):
9595
return 'RGBA'
9696
elif _type == 4:

system/lib/menu.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
import shutil
2+
import textwrap
3+
import typing
24

35
import colorama
46

57
from system.lib.config import config
68
from system.localization import locale
79

810

9-
def print_feature(name: str, description: str = None, console_width: int = -1):
10-
print(name, end='')
11+
def print_feature(feature_id: int, name: str, description: str = None, console_width: int = -1):
12+
text = f' {feature_id} {name}'
1113
if description:
12-
print(' ' * (console_width // 2 - len(name)) + ': ' + description, end='')
13-
print()
14+
text += ' ' * (console_width // 2 - len(text)) + ': ' + description
1415

16+
print(textwrap.fill(text, console_width))
1517

16-
def print_category(text):
17-
return print(colorama.Back.GREEN + colorama.Fore.BLACK + text + ' ' * (10 - len(text)) + colorama.Style.RESET_ALL)
1818

19-
20-
def print_line(console_width):
21-
print((console_width - 1) * '-')
19+
def print_category(text: str, background_width: int = 10):
20+
print(colorama.Back.GREEN + colorama.Fore.BLACK + text + ' ' * (background_width - len(text)) + colorama.Style.RESET_ALL)
2221

2322

2423
class Menu:
2524
class Item:
26-
def __init__(self, name, description=None, handler=None):
27-
self.name = name
28-
self.description = description
29-
self.handler = handler
25+
def __init__(self, name: str, description: str = None, handler: typing.Callable = None):
26+
self.name: str = name
27+
self.description: str = description
28+
self.handler: typing.Callable = handler
3029

3130
class Category:
3231
def __init__(self, _id: int, name: str):
@@ -57,14 +56,14 @@ def choice(self):
5756
colorama.Style.RESET_ALL
5857
).center(console_width + 12))
5958
print('github.com/Vorono4ka/XCoder'.center(console_width - 1))
60-
print_line(console_width)
59+
Menu._print_divider_line(console_width)
6160

6261
for category in self.categories:
6362
print_category(category.name)
6463
for item_index in range(len(category.items)):
6564
item = category.items[item_index]
66-
print_feature(f' {category.id * 10 + item_index + 1} {item.name}', item.description, console_width)
67-
print_line(console_width)
65+
print_feature(category.id * 10 + item_index + 1, item.name, item.description, console_width)
66+
Menu._print_divider_line(console_width)
6867

6968
choice = input(locale.choice)
7069
try:
@@ -73,7 +72,7 @@ def choice(self):
7372
return None
7473
except ValueError:
7574
return None
76-
print_line(console_width)
75+
Menu._print_divider_line(console_width)
7776

7877
category_id = choice // 10
7978
item_index = choice % 10
@@ -85,5 +84,9 @@ def choice(self):
8584
break
8685
return None
8786

87+
@staticmethod
88+
def _print_divider_line(console_width: int):
89+
print((console_width - 1) * '-')
90+
8891

8992
menu = Menu()

system/lib/objects/point.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Point:
2-
def __init__(self, x=0, y=0):
3-
self.x = x
4-
self.y = y
2+
def __init__(self, x: float = 0, y: float = 0):
3+
self.x: float = x
4+
self.y: float = y
55

66
def __eq__(self, other):
77
return self.x == other.x and self.y == other.y
@@ -24,10 +24,10 @@ def __repr__(self):
2424
return str(self.position)
2525

2626
@property
27-
def position(self):
27+
def position(self) -> (float, float):
2828
return self.x, self.y
2929

3030
@position.setter
31-
def position(self, value):
31+
def position(self, value: (float, float)):
3232
self.x = value[0]
3333
self.y = value[1]

system/lib/objects/shape.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,19 @@ def load(self, swf, tag: int):
9999

100100
multiplier = 0.5 if swf.use_lowres_texture else 1
101101

102-
for z in range(self.points_count):
103-
self.shape_points[z].x = swf.reader.read_int32() / 20 * multiplier
104-
self.shape_points[z].y = swf.reader.read_int32() / 20 * multiplier
105-
for z in range(self.points_count):
106-
w, h = [swf.reader.read_uint16() * swf.textures[self.texture_id].width / 0xffff * multiplier,
107-
swf.reader.read_uint16() * swf.textures[self.texture_id].height / 0xffff * multiplier]
108-
x, y = map(ceil, (w, h))
109-
if int(w) == x:
110-
x += 1
111-
if int(h) == y:
112-
y += 1
113-
114-
self.sheet_points[z].position = (x, y)
102+
for i in range(self.points_count):
103+
self.shape_points[i].x = swf.reader.read_int32() / 20
104+
self.shape_points[i].y = swf.reader.read_int32() / 20
105+
for i in range(self.points_count):
106+
u, v = (swf.reader.read_uint16() * swf.textures[self.texture_id].width / 0xffff * multiplier,
107+
swf.reader.read_uint16() * swf.textures[self.texture_id].height / 0xffff * multiplier)
108+
u_rounded, v_rounded = map(ceil, (u, v))
109+
if int(u) == u_rounded:
110+
u_rounded += 1
111+
if int(v) == v_rounded:
112+
v_rounded += 1
113+
114+
self.sheet_points[i].position = (u_rounded, v_rounded)
115115

116116
def render(self, swf):
117117
sheet_left = min(point.x for point in self.sheet_points)

system/lib/objects/texture.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from PIL import Image
44

5-
from system.lib.images import pixel_type2str, load_texture, join_image, load_image_from_buffer
5+
from system.lib.images import get_format_by_pixel_type, load_texture, join_image, load_image_from_buffer
66

77

88
class SWFTexture:
@@ -19,11 +19,11 @@ def load(self, swf, tag: int, has_texture: bool):
1919
self.width, self.height = (swf.reader.read_uint16(), swf.reader.read_uint16())
2020

2121
if has_texture:
22-
img = Image.new(pixel_type2str(self.pixel_type), (self.width, self.height))
22+
img = Image.new(get_format_by_pixel_type(self.pixel_type), (self.width, self.height))
2323

2424
load_texture(swf.reader, self.pixel_type, img)
2525

26-
if tag in (27, 28):
26+
if tag in (27, 28, 29):
2727
join_image(img)
2828
print()
2929
else:

0 commit comments

Comments
 (0)