Skip to content
This repository has been archived by the owner on Jan 28, 2023. It is now read-only.

Commit

Permalink
fix: composite parts don't save invalid references
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenbom committed Sep 22, 2018
1 parent 305a674 commit d6258fb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 150 deletions.
149 changes: 2 additions & 147 deletions scripts/convert_v1_file.py
Expand Up @@ -95,6 +95,8 @@ def create_dirs(dir_list):
assert part_ref in parts_by_name, "%s not in %s"%(str(part_ref), str(parts_by_name.keys()))
part_ref = parts_by_name[part_ref]["id"]
part["part"] = part_ref
else:
del part["part"]
comp_list.append(comp)

data_obj["comps"] = comp_list
Expand Down Expand Up @@ -128,153 +130,6 @@ def flatten(node):
root, name), arcname=os.path.join(
relative_dir, name))

# tarinfo = tarfile.TarInfo.frombuf(json.dumps(data_obj))
# tarinfo.name = "data.json"
# file.add(tarinfo)

# Copy all images to a temporary dir
# and pack them into a spritesheet

# for ti in file.getmembers():
# if ti.name.startswith("_"):
# continue
# if ti.isfile() and ti.name.endswith(".png"):
# file.extract(ti.name, tmp_dir)

# Process parts
# parts = data_obj["parts"]
# assert isinstance(parts, dict), "parts not a dictionary"



'''
'''

# First: replace name key with unique id key, and add "name" as a field


#for key, val in parts.iteritems():

'''
# Replace all the image references in parts
# with the spritesheet info
for pname, part in parts.items():
# print(pname)
props = {}
has_remapped_rects = False
hit_rect_mode = None
# handle properties
if "properties" in part.keys():
# convert string to a json object, to a python object
# print(mode)
mode = part["properties"].strip()
if len(mode) > 0:
try:
props = json.loads(mode)
except ValueError as ve:
print("ERROR: " + str(ve))
print("json: \"%s\"" % (mode))
print("in part: ", pname, part)
raise(ve)
part["properties"] = props
if "hit_rect_mode" in props:
hit_rect_mode = props["hit_rect_mode"]
assert hit_rect_mode in part.keys(), "hit_rect_mode \"" + hit_rect_mode + \
"\" not found in " + pname
else:
part["properties"] = {}
for mname, mode in part.items():
if mname == "properties":
pass
else:
# print(mname)
for i in range(mode["numFrames"]):
frame = mode["frames"][i]
image_name = frame["image"]
mapped_data = image_map[image_name]
frame["image"] = new_spritesheet_name
frame["dx"] = mapped_data["spriteSourceSize"]["x"]
frame["dy"] = mapped_data["spriteSourceSize"]["y"]
frame.update(mapped_data["frame"])
# Special case of an empty frame
# Texture packer gives it a width,height=1,1
# But we'll set it to 0,0 so Moonman engine can ignore
# it
if frame["dx"] == 0 and frame["dy"] == 0 and frame["w"] == 1 and frame["h"] == 1:
# print("Detected empty frame in %s"%pname)
frame["w"] = 0
frame["h"] = 0
if not has_remapped_rects and (
(hit_rect_mode is None) or (
hit_rect_mode == mname)):
has_remapped_rects = True
if "properties" in part.keys():
props["hit_rect_mode"] = mname
part["properties"].update(props)
# Transform rectangles - relative to anchor
for prop_name, prop in props.items():
if prop_name.endswith("_rect"):
rect = prop
ax, ay = frame["ax"], frame["ay"]
rect[0] = rect[0] - ax
rect[1] = rect[1] - ay
part["properties"][prop_name] = {
"x": rect[0], "y": rect[1], "w": rect[2], "h": rect[3]}
# Write the modified json file back out (for now)
data_obj["parts"] = parts
'''
'''
# Process comps
for comp_name, comp in data_obj["comps"].items():
if comp_name.startswith("_"):
continue
# for each comp we just need to
# patch up the part refs
comp["rootIndex"] = comp["root"]
del comp["root"]
for part in comp["parts"]:
if len(part["part"]) == 0:
part["part"] = ""
else:
new_ref = "(sprite/" + part["part"] + ",spritemap)"
part["part"] = new_ref
# handle properties
has_valid_properties = False
if "properties" in comp:
# convert string to a json object, to a python object
# print(mode)
mode = comp["properties"].strip()
if len(mode) > 0:
try:
props = json.loads(mode)
comp["properties"] = props
for prop_name, prop in props.items():
if prop_name.endswith("_rect"):
rect = prop
comp["properties"][prop_name] = {
"x": rect[0], "y": rect[1], "w": rect[2], "h": rect[3]}
has_valid_properties = True
except ValueError as e:
print(e)
print("composition: ", comp_name)
if not has_valid_properties:
del comp["properties"]
filename = os.path.join(package_dir, "sprite", comp_name + ".comp")
dir = os.path.dirname(filename)
if not os.path.isdir(dir):
os.makedirs(dir)
json.dump(comp, open(filename, "w"), indent=1)
'''
print("Success!")

if (len(sys.argv) != 3):
Expand Down
14 changes: 11 additions & 3 deletions src/projectmodel.cpp
Expand Up @@ -570,7 +570,10 @@ void ProjectModel::compositeToJson(const QString& name, const Composite& comp, Q
childObject.insert("parentPivot", child.parentPivot);
childObject.insert("z", child.z);
childObject.insert("index", index++);
childObject.insert("part", child.part.id);
if (!child.part.isNull()) {
Q_ASSERT(child.part.type == AssetType::Part);
childObject.insert("part", child.part.id);
}
QJsonArray children;
for(int ci: child.children){
children.append(ci);
Expand Down Expand Up @@ -606,8 +609,13 @@ void ProjectModel::jsonToComposite(const QJsonObject& obj, Composite* comp){
child.parent = childObject.value("parent").toInt();
child.parentPivot = childObject.value("parentPivot").toInt();
child.z = childObject.value("z").toInt();
child.part.id = childObject.value("part").toInt();
child.part.type = AssetType::Part;
if (childObject.contains("part")) {
child.part.id = childObject.value("part").toInt();
child.part.type = AssetType::Part;
}
else {
child.part.type = AssetType::None;
}
child.index = index++;
QJsonArray childrenOfChild = childObject.value("children").toArray();
for(const auto& ci: childrenOfChild){
Expand Down

0 comments on commit d6258fb

Please sign in to comment.