Skip to content

Commit 5593a36

Browse files
committed
Enable columns or rows for both directions
1 parent 04efd24 commit 5593a36

File tree

2 files changed

+53
-54
lines changed

2 files changed

+53
-54
lines changed

spritesheetExporter/spritesheet_exporter.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ class SpritesheetExporter:
2525
unique_frames: bool
2626

2727
horizontal: bool
28-
size: int
28+
columns: int
29+
rows: int
30+
2931
start: int
3032
end: int
3133

@@ -261,11 +263,11 @@ def _process_frames(self, src: Document, dest: Document):
261263

262264
# Layers are ordered by when they were added, so using `i` is fine
263265
if self.horizontal:
264-
x_pos = (i % self.size) * width
265-
y_pos = (i // self.size) * height
266+
x_pos = (i % self.columns) * width
267+
y_pos = (i // self.columns) * height
266268
else:
267-
x_pos = (i // self.size) * width
268-
y_pos = (i % self.size) * height
269+
x_pos = (i // self.rows) * width
270+
y_pos = (i % self.rows) * height
269271

270272
layer.move(x_pos, y_pos)
271273

@@ -338,29 +340,29 @@ def export(self, debug=False):
338340
self._copy_frames(doc, sheet)
339341
num_frames = len(sheet.topLevelNodes())
340342

341-
if self.size == DEFAULT_SPACE:
342-
# Pack the sprites as densely as possible with a square fit
343-
self.size = ceil(sqrt(num_frames))
344-
columns, rows = self.size, self.size
345-
else:
343+
if self.columns != DEFAULT_SPACE:
346344
# Remove empty sprite cells
347-
self.size = min(self.size, num_frames)
348-
349-
columns = self.size
350-
rows = ceil(num_frames / columns)
351-
if not self.horizontal:
352-
columns, rows = rows, columns
345+
self.columns = min(self.columns, num_frames)
346+
self.rows = ceil(num_frames / self.columns)
347+
elif self.rows != DEFAULT_SPACE:
348+
self.rows = min(self.rows, num_frames)
349+
self.columns = ceil(num_frames / self.rows)
350+
else:
351+
# Pack the sprites as densely as possible with a square fit
352+
size = ceil(sqrt(num_frames))
353+
self.columns = size
354+
self.rows = size
353355

354-
sheet.setWidth(columns * width)
355-
sheet.setHeight(rows * height)
356+
sheet.setWidth(self.columns * width)
357+
sheet.setHeight(self.rows * height)
356358

357359
if debug:
358360
print(
359361
f"New document name: {sheet.name()}",
360362
f"New document size: {sheet.width()}x{sheet.height()}",
361363
f"Number of frames: {num_frames}",
362-
f"Columns: {columns}",
363-
f"Rows: {rows}",
364+
f"Columns: {self.columns}",
365+
f"Rows: {self.rows}",
364366
sep="\n",
365367
)
366368

spritesheetExporter/ui.py

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
QFormLayout,
1111
QGridLayout,
1212
QGroupBox,
13+
QButtonGroup,
1314
QPushButton,
1415
QFileDialog,
1516
QSpinBox,
@@ -144,8 +145,9 @@ class SpritePlacement(QFormLayout):
144145
h_dir = QRadioButton("Horizontal")
145146
v_dir = QRadioButton("Vertical")
146147

147-
columns = QSpinBox(value=DEFAULT_SPACE, minimum=DEFAULT_SPACE)
148-
rows = QSpinBox(value=DEFAULT_SPACE, minimum=DEFAULT_SPACE)
148+
columns = QRadioButton("Columns")
149+
rows = QRadioButton("Rows")
150+
size = QSpinBox(value=DEFAULT_SPACE, minimum=DEFAULT_SPACE)
149151

150152
def __init__(self):
151153
super().__init__()
@@ -156,47 +158,42 @@ def __init__(self):
156158
self.h_dir.setToolTip("Order the sprites horizontally")
157159
self.v_dir.setToolTip("Order the sprites vertically")
158160

159-
self.columns.setSpecialValueText("Auto")
160-
self.columns.setToolTip("Number of columns in the spritesheet")
161+
self.size.setSpecialValueText("Auto")
162+
self.size.setToolTip("Number of columns or rows in the spritesheet")
161163

162-
self.rows.setEnabled(False)
163-
self.rows.setSpecialValueText("Auto")
164-
self.rows.setToolTip("Number of rows in the spritesheet")
164+
self.columns.setChecked(True)
165165

166-
self.h_dir.toggled.connect(self.toggle_horizontal)
167-
self.v_dir.toggled.connect(self.toggle_vertical)
166+
dirs = QVBoxLayout()
167+
dirs.addWidget(self.h_dir)
168+
dirs.addWidget(self.v_dir)
169+
dirs_buttons = QButtonGroup()
170+
dirs_buttons.addButton(self.h_dir)
171+
dirs_buttons.addButton(self.v_dir)
168172

169-
col_layout = QFormLayout()
170-
col_layout.setHorizontalSpacing(4)
171-
col_layout.addRow("Columns:", self.columns)
173+
sizes = QHBoxLayout()
172174

173-
row_layout = QFormLayout()
174-
row_layout.setHorizontalSpacing(4)
175-
row_layout.addRow("Rows:", self.rows)
175+
size_buttons_box = QVBoxLayout()
176+
size_buttons_box.addWidget(self.columns)
177+
size_buttons_box.addWidget(self.rows)
178+
size_buttons = QButtonGroup(size_buttons_box)
179+
size_buttons.addButton(self.columns)
180+
size_buttons.addButton(self.rows)
176181

177-
field = QGridLayout()
178-
field.addWidget(self.h_dir, 0, 0)
179-
field.addWidget(self.v_dir, 0, 1)
180-
field.addLayout(col_layout, 1, 0)
181-
field.addLayout(row_layout, 1, 1)
182+
sizes.addLayout(size_buttons_box)
183+
sizes.addWidget(self.size)
182184

183-
self.addRow("Sprite placement:", field)
184-
185-
def toggle_horizontal(self, checked: bool):
186-
self.columns.setEnabled(checked)
187-
self.rows.setEnabled(not checked)
188-
189-
def toggle_vertical(self, checked: bool):
190-
self.columns.setEnabled(not checked)
191-
self.rows.setEnabled(checked)
185+
self.addRow("Sprite placement:", dirs)
186+
self.addRow("Spritesheet size:", sizes)
192187

193188
def apply_settings(self, exporter: SpritesheetExporter):
194-
if self.h_dir.isChecked():
195-
exporter.horizontal = True
196-
exporter.size = self.columns.value()
189+
exporter.horizontal = self.h_dir.isChecked()
190+
191+
if self.columns.isChecked():
192+
exporter.columns = self.size.value()
193+
exporter.rows = DEFAULT_SPACE
197194
else:
198-
exporter.horizontal = False
199-
exporter.size = self.rows.value()
195+
exporter.columns = DEFAULT_SPACE
196+
exporter.rows = self.size.value()
200197

201198

202199
class SpinBoxes(QFormLayout):

0 commit comments

Comments
 (0)