File indexing completed on 2024-06-23 04:27:59
0001 # 0002 # SPDX-License-Identifier: GPL-3.0-or-later 0003 # 0004 0005 import json 0006 from pathlib import Path 0007 0008 0009 class COAToolsFormat: 0010 def __init__(self, cfg, statusBar): 0011 self.cfg = cfg 0012 self.statusBar = statusBar 0013 self.reset() 0014 0015 def reset(self): 0016 self.nodes = [] 0017 0018 def showError(self, msg): 0019 msg, timeout = (self.cfg["error"]["msg"].format(msg), self.cfg["error"]["timeout"]) 0020 self.statusBar.showMessage(msg, timeout) 0021 0022 def collect(self, node): 0023 print("COAToolsFormat collecting %s" % (node.name)) 0024 self.nodes.append(node) 0025 0026 def remap(self, oldValue, oldMin, oldMax, newMin, newMax): 0027 if oldMin == newMin and oldMax == newMax: 0028 return oldValue 0029 return (((oldValue - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin 0030 0031 def save(self, output_dir=""): 0032 """ 0033 Parses layers configured to export to COA Tools and builds the JSON data 0034 COA Tools need to import the files 0035 """ 0036 # For each top-level node (Group Layer) 0037 export_dir = output_dir 0038 for wn in self.nodes: 0039 children = wn.children 0040 path = wn.path 0041 0042 if path != "": 0043 export_dir = path 0044 0045 print("COAToolsFormat exporting %d items from %s" % (len(children), wn.name)) 0046 try: 0047 if len(children) <= 0: 0048 raise ValueError(wn.name, "has no children to export") 0049 0050 coa_data = {"name": wn.name, "nodes": []} 0051 print("COAToolsFormat exporting %s to %s" % (wn.name, export_dir)) 0052 for idx, child in enumerate(children): 0053 sheet_meta = dict() 0054 if child.coa != "": 0055 fn, sheet_meta = child.saveCOASpriteSheet(export_dir) 0056 else: 0057 fn = child.saveCOA(export_dir) 0058 path = Path(fn) 0059 node = child.node 0060 coords = node.bounds().getCoords() 0061 relative_coords = coords 0062 0063 parent_node = node.parentNode() 0064 parent_coords = parent_node.bounds().getCoords() 0065 relative_coords = [coords[0] - parent_coords[0], coords[1] - parent_coords[1]] 0066 0067 p_width = parent_coords[2] - parent_coords[0] 0068 p_height = parent_coords[3] - parent_coords[1] 0069 0070 tiles_x, tiles_y = 1, 1 0071 if len(sheet_meta) > 0: 0072 tiles_x, tiles_y = sheet_meta["tiles_x"], sheet_meta["tiles_y"] 0073 0074 coa_entry = { 0075 "children": [], 0076 "frame_index": 0, 0077 "name": child.name, 0078 "node_path": child.name, 0079 "offset": [-p_width / 2, p_height / 2], 0080 "opacity": self.remap(node.opacity(), 0, 255, 0, 1), 0081 "pivot_offset": [0.0, 0.0], 0082 "position": relative_coords, 0083 "resource_path": str(path.relative_to(Path(export_dir))), 0084 "rotation": 0.0, 0085 "scale": [1.0, 1.0], 0086 "tiles_x": tiles_x, 0087 "tiles_y": tiles_y, 0088 "type": "SPRITE", 0089 "z": idx - len(children) + 1, 0090 } 0091 coa_data["nodes"].append(coa_entry) 0092 json_data = json.dumps(coa_data, sort_keys=True, indent=4, separators=(",", ": ")) 0093 Path(export_dir, wn.name + ".json").write_text(json_data) 0094 0095 except ValueError as e: 0096 self.showError(e)