Skip to content

Commit

Permalink
Had to remove no-merge option for now + some bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
UncertainProd committed Jul 15, 2022
1 parent da51146 commit afd23f0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 59 deletions.
116 changes: 60 additions & 56 deletions src/engine/xmlpngengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def make_png_xml(frames, save_dir, character_name="Result", progressupdatefn=Non
must_use_prefix = settings.get('must_use_prefix', 0) != 0 # use the custom prefix even if frame is from existing spritesheet
padding_pixels = settings.get('frame_padding', 0)
packing_algorithm = settings.get('packing_algo', 0) # 0 = Growing Packer, 1 = Ordered Packer
no_merge = settings.get('no_merge', 0) != 0 # no merging lookalike frames
# no_merge = settings.get('no_merge', 0) != 0 # no merging lookalike frames

# print(len(imghashes))
# print(len(frames))
Expand Down Expand Up @@ -54,23 +54,25 @@ def make_png_xml(frames, save_dir, character_name="Result", progressupdatefn=Non
frame_dict_arr = []
current_img_hashes = set([x.data.img_hash for x in frames])

if no_merge:
for f in frames:
# Doesn't quite work yet, still a WIP
# if no_merge:
# for f in frames:
# frame_dict_arr.append({
# "id": f.data.img_hash,
# "w": imghashes.get(f.data.img_hash).width + 2*padding_pixels,
# "h": imghashes.get(f.data.img_hash).height + 2*padding_pixels,
# "frame": f # this comes in handy later on
# })
# else:
# pass
# add the padding to width and height, then actually padding the images (kind of a hack but it works TODO: work out a better way to do this)
for imhash, img in imghashes.items():
if imhash in current_img_hashes:
frame_dict_arr.append({
"id": f.data.img_hash,
"w": imghashes.get(f.data.img_hash).width + 2*padding_pixels,
"h": imghashes.get(f.data.img_hash).height + 2*padding_pixels,
"frame": f # this comes in handy later on
"id": imhash,
"w": img.width + 2*padding_pixels,
"h": img.height + 2*padding_pixels
})
else:
# add the padding to width and height, then actually padding the images (kind of a hack but it works TODO: work out a better way to do this)
for imhash, img in imghashes.items():
if imhash in current_img_hashes:
frame_dict_arr.append({
"id": imhash,
"w": img.width + 2*padding_pixels,
"h": img.height + 2*padding_pixels
})

if packing_algorithm == 1:
packer = OrderedPacker()
Expand All @@ -94,47 +96,49 @@ def make_png_xml(frames, save_dir, character_name="Result", progressupdatefn=Non
prgs += 1
progressupdatefn(prgs, "Adding images to spritesheet...")

if no_merge:
for framedict in frame_dict_arr:
frame = framedict['frame']
subtexture_element = ET.Element("SubTexture")
subtexture_element.tail = linesep
w, h = imghashes.get(frame.data.img_hash).size
subtexture_element.attrib = {
"name" : frame.data.xml_pose_name,
"x": str(framedict['fit']['x']),
"y": str(framedict['fit']['y']),
"width": str(w + 2*padding_pixels),
"height": str(h + 2*padding_pixels),
"frameX": str(frame.data.framex),
"frameY": str(frame.data.framey),
"frameWidth": str(frame.data.framew),
"frameHeight": str(frame.data.frameh),
}
root.append(subtexture_element)
prgs += 1
progressupdatefn(prgs, f"Saving {frame.data.xml_pose_name} to XML...")
else:
# Doesn't quite work yet, still a WIP
# if no_merge:
# for framedict in frame_dict_arr:
# frame = framedict['frame']
# subtexture_element = ET.Element("SubTexture")
# subtexture_element.tail = linesep
# w, h = imghashes.get(frame.data.img_hash).size
# subtexture_element.attrib = {
# "name" : frame.data.xml_pose_name,
# "x": str(framedict['fit']['x']),
# "y": str(framedict['fit']['y']),
# "width": str(w + 2*padding_pixels),
# "height": str(h + 2*padding_pixels),
# "frameX": str(frame.data.framex),
# "frameY": str(frame.data.framey),
# "frameWidth": str(frame.data.framew),
# "frameHeight": str(frame.data.frameh),
# }
# root.append(subtexture_element)
# prgs += 1
# progressupdatefn(prgs, f"Saving {frame.data.xml_pose_name} to XML...")
# else:
# pass
# convert frame_dict_arr into a dict[image_hash -> position in spritesheet]:
imghash_dict = { rect['id']: (rect['fit']['x'], rect['fit']['y']) for rect in frame_dict_arr }
for frame in frames:
subtexture_element = ET.Element("SubTexture")
subtexture_element.tail = linesep
w, h = imghashes.get(frame.data.img_hash).size
subtexture_element.attrib = {
"name" : frame.data.xml_pose_name,
"x": str(imghash_dict[frame.data.img_hash][0]),
"y": str(imghash_dict[frame.data.img_hash][1]),
"width": str(w + 2*padding_pixels),
"height": str(h + 2*padding_pixels),
"frameX": str(frame.data.framex),
"frameY": str(frame.data.framey),
"frameWidth": str(frame.data.framew),
"frameHeight": str(frame.data.frameh),
}
root.append(subtexture_element)
prgs += 1
progressupdatefn(prgs, f"Saving {frame.data.xml_pose_name} to XML...")
imghash_dict = { rect['id']: (rect['fit']['x'], rect['fit']['y']) for rect in frame_dict_arr }
for frame in frames:
subtexture_element = ET.Element("SubTexture")
subtexture_element.tail = linesep
w, h = imghashes.get(frame.data.img_hash).size
subtexture_element.attrib = {
"name" : frame.data.xml_pose_name,
"x": str(imghash_dict[frame.data.img_hash][0]),
"y": str(imghash_dict[frame.data.img_hash][1]),
"width": str(w + 2*padding_pixels),
"height": str(h + 2*padding_pixels),
"frameX": str(frame.data.framex),
"frameY": str(frame.data.framey),
"frameWidth": str(frame.data.framew),
"frameHeight": str(frame.data.frameh),
}
root.append(subtexture_element)
prgs += 1
progressupdatefn(prgs, f"Saving {frame.data.xml_pose_name} to XML...")
# im.close()
print("Saving XML...")
xmltree = ET.ElementTree(root)
Expand Down
9 changes: 6 additions & 3 deletions src/settingswindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def __init__(self, *args, **kwargs):
self.ui.custom_prefix_radiobtn.toggled.connect(lambda is_toggled: self.ui.custom_prefix_text.setEnabled(is_toggled))
self.ui.save_settings_btn.clicked.connect(lambda: self.saveSettings()) # make sure event related parameters don't get accidentally sent to self.saveSettings
self.ui.settings_cancel_btn.clicked.connect(self.restoreToNormal)

# hide the no_merge checkbox for now as it is a WIP
self.ui.no_merge_checkbox.setVisible(False)

def _get_prefix_type(self):
if self.ui.custom_prefix_radiobtn.isChecked():
Expand All @@ -45,7 +48,7 @@ def restoreToNormal(self):
self.ui.insist_prefix_checkbox.setCheckState(self.must_use_prefix)
self.ui.frame_padding_spinbox.setValue(self.frame_padding)
self.ui.packingalgo_combobox.setCurrentIndex(self.packing_algo)
self.ui.no_merge_checkbox.setCheckState(self.no_merge)
# self.ui.no_merge_checkbox.setCheckState(self.no_merge)
self.close()

def saveSettings(self, shouldclose=True):
Expand All @@ -56,15 +59,15 @@ def saveSettings(self, shouldclose=True):
self.must_use_prefix = self.ui.insist_prefix_checkbox.checkState()
self.frame_padding = self.ui.frame_padding_spinbox.value()
self.packing_algo = self.ui.packingalgo_combobox.currentIndex()
self.no_merge = self.ui.no_merge_checkbox.checkState()
# self.no_merge = self.ui.no_merge_checkbox.checkState()
# saving to global settings obj
g_settings['isclip'] = self.isclip
g_settings['prefix_type'] = self.prefix_type
g_settings['custom_prefix'] = self.custom_prefix
g_settings['must_use_prefix'] = self.must_use_prefix
g_settings['frame_padding'] = self.frame_padding
g_settings['packing_algo'] = self.packing_algo
g_settings['no_merge'] = self.no_merge
# g_settings['no_merge'] = self.no_merge
if shouldclose:
self.close()

Expand Down

0 comments on commit afd23f0

Please sign in to comment.