Skip to content

Commit a5668d2

Browse files
authored
(graphics) Add a new option "keep_br_space" (#9)
1 parent 505a484 commit a5668d2

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

agrf/graphics/spritesheet.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ def squash(self, ratio):
8484

8585

8686
class CustomCropMixin:
87-
def __init__(self, *args, fixed_crop=False, crop_amount=(0, 0), **kw):
87+
def __init__(self, *args, fixed_crop=False, crop_amount=(0, 0), keep_br_space=False, **kw):
8888
super().__init__(*args, **kw)
8989
self.fixed_crop = fixed_crop
9090
self.crop_amount = crop_amount
91+
self.keep_br_space = keep_br_space
9192
if fixed_crop:
9293
assert crop_amount[0] <= self.h
9394
assert crop_amount[1] <= self.w
@@ -98,23 +99,27 @@ def _do_crop(self, context, w, h, rgb, alpha, mask):
9899
crop_x, crop_y = self.crop_amount
99100

100101
timer = context.start_timer()
101-
if alpha is not None:
102-
cols_bitset = alpha.any(0)
103-
rows_bitset = alpha.any(1)
104-
elif rgb is not None:
105-
cols_bitset = rgb.any((0, 2))
106-
rows_bitset = rgb.any((1, 2))
107-
elif mask is not None:
108-
cols_bitset = mask.any(0)
109-
rows_bitset = mask.any(1)
110-
else:
111-
raise context.failure(self, "All data layers are None")
112-
113-
cols_used = np.arange(w)[cols_bitset]
114-
rows_used = np.arange(h)[rows_bitset]
115102

116-
w = max(cols_used, default=0) - crop_x + 1
117-
h = max(rows_used, default=0) - crop_y + 1
103+
if self.keep_br_space:
104+
w = rgb.shape[1] - crop_x
105+
h = rgb.shape[0] - crop_y
106+
else:
107+
if alpha is not None:
108+
cols_bitset = alpha.any(0)
109+
rows_bitset = alpha.any(1)
110+
elif rgb is not None:
111+
cols_bitset = rgb.any((0, 2))
112+
rows_bitset = rgb.any((1, 2))
113+
elif mask is not None:
114+
cols_bitset = mask.any(0)
115+
rows_bitset = mask.any(1)
116+
else:
117+
raise context.failure(self, "All data layers are None")
118+
cols_used = np.arange(w)[cols_bitset]
119+
rows_used = np.arange(h)[rows_bitset]
120+
121+
w = max(cols_used, default=0) - crop_x + 1
122+
h = max(rows_used, default=0) - crop_y + 1
118123

119124
if w <= 0 or h <= 0:
120125
w = h = 1
@@ -163,6 +168,7 @@ def spritesheet_template(
163168
shift=0,
164169
mode="vehicle",
165170
manual_crop=None,
171+
keep_br_space=False,
166172
childsprite=False,
167173
relative_childsprite=False,
168174
nomask=False,
@@ -229,7 +235,13 @@ def get_rels(direction, scale):
229235
def with_optional_mask(sprite, mask):
230236
if mask is None:
231237
return sprite
232-
return CustomCropWithMask(sprite, mask, fixed_crop=sprite.fixed_crop, crop_amount=sprite.crop_amount)
238+
return CustomCropWithMask(
239+
sprite,
240+
mask,
241+
fixed_crop=sprite.fixed_crop,
242+
crop_amount=sprite.crop_amount,
243+
keep_br_space=sprite.keep_br_space,
244+
)
233245

234246
return [
235247
LazyAlternativeSprites(
@@ -256,6 +268,7 @@ def with_optional_mask(sprite, mask):
256268
),
257269
bpp=bpp,
258270
zoom=SCALE_TO_ZOOM[scale],
271+
keep_br_space=keep_br_space,
259272
**(
260273
{}
261274
if manual_crop is None

agrf/graphics/voxel.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ def spritesheet(self, xdiff=0, ydiff=0, zdiff=0, shift=0, xspan=16, yspan=16):
254254
scales=self.config["agrf_scales"],
255255
shift=shift,
256256
manual_crop=self.config.get("agrf_manual_crop", None),
257+
keep_br_space=self.config.get("agrf_manual_crop_keep_br_space", False),
257258
childsprite=self.config.get("agrf_childsprite", False),
258259
relative_childsprite=self.config.get("agrf_relative_childsprite", False),
259260
nomask=self.config.get("agrf_no_mask", False),

0 commit comments

Comments
 (0)