From d6258fbbb6d8f73658a7748a52549998de64a753 Mon Sep 17 00:00:00 2001 From: Benjamin Porter Date: Sun, 23 Sep 2018 09:41:06 +1000 Subject: [PATCH] fix: composite parts don't save invalid references --- scripts/convert_v1_file.py | 149 +------------------------------------ src/projectmodel.cpp | 14 +++- 2 files changed, 13 insertions(+), 150 deletions(-) diff --git a/scripts/convert_v1_file.py b/scripts/convert_v1_file.py index 5f09792..2a8917b 100644 --- a/scripts/convert_v1_file.py +++ b/scripts/convert_v1_file.py @@ -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 @@ -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): diff --git a/src/projectmodel.cpp b/src/projectmodel.cpp index 6b3e0f3..cc1d4ee 100644 --- a/src/projectmodel.cpp +++ b/src/projectmodel.cpp @@ -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); @@ -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){