Skip to content

Commit 8a6f609

Browse files
committed
Reimplement export and replace main functions
1 parent e3c4ba3 commit 8a6f609

File tree

4 files changed

+235
-239
lines changed

4 files changed

+235
-239
lines changed

src/infoDisplay.py

Lines changed: 21 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -58,54 +58,32 @@ def saveAs(self):
5858
if file:
5959
extension = Path(file.name).suffix
6060
value = self.selected.get()
61-
mainApp = self.master.master
6261
if value != "":
6362
selected = value
64-
atlasType = self.lastActualOption
65-
66-
# Define variables by atlas type
67-
if atlasType == "Items":
68-
element = self.globalVars.items[selected]
69-
atlas = self.globalVars.itemsAtlas
70-
elif atlasType == "Blocks":
71-
element = self.globalVars.blocks[selected]
72-
atlas = self.globalVars.blocksAtlas
63+
64+
element = self.globalVars.items[selected]
65+
atlas = self.globalVars.atlasHandler
7366
position = element["uv"]
7467

7568
# Copy region and export
76-
export = atlas.atlas.copy(position[0], position[1], position[0] + element["tileSize"], position[1] + element["tileSize"])
69+
export = atlas.atlas.copy(position[0], position[1], position[2], position[3])
7770
if extension == ".png":
7871
export.save(file)
7972
elif extension == ".3dst":
8073
export3dst = Texture3dst().fromImage(export)
8174
export3dst.export(file.name)
82-
file.close()
75+
file.close()
8376

8477
def changeTextureCallback(self):
8578
self.changeTextureFunc(self.selected.get())
8679

8780
def changeTextureFunc(self, value):
88-
mainFrame = self.master
8981
outputDir = self.globalVars.outputFolder
9082
allowResize = self.globalVars.allowResize
9183

92-
atlasType = self.lastActualOption
93-
items = self.globalVars.items
94-
blocks = self.globalVars.blocks
95-
itemsAtlas = self.globalVars.itemsAtlas
96-
blocksAtlas = self.globalVars.blocksAtlas
97-
addedItems = self.globalVars.addedItems
98-
addedBlocks = self.globalVars.addedBlocks
99-
100-
# Define variables by atlas type
101-
if atlasType == "Items":
102-
added = addedItems
103-
atlas: atlasTexture3dst = itemsAtlas
104-
element = items[value]
105-
elif atlasType == "Blocks":
106-
added = addedBlocks
107-
atlas: atlasTexture3dst = blocksAtlas
108-
element = blocks[value]
84+
added = self.globalVars.addedItems
85+
atlas: atlasTexture3dst = self.globalVars.atlasHandler
86+
element = self.globalVars.items[value]
10987
position = element["uv"]
11088

11189
exts = Image.registered_extensions()
@@ -116,49 +94,37 @@ def changeTextureFunc(self, value):
11694
if idx < len(supported_extensions)-1:
11795
exts_str += " "
11896
filePath = customtkinter.filedialog.askopenfilename(filetypes=[("Image files", ".jpeg .jpg .gif .png .webp .tiff .tif .bmp .psd .ico"), ("All image files", exts_str)])
119-
if filePath != '':
97+
if filePath != '' and isinstance(filePath, str):
12098
if not canOpenImage(filePath):
12199
messagebox.showerror("Failed to open image", "Unable to open the selected image")
122100
else:
123101
textureToReplace = Image.open(filePath)
124-
isSized = isImageSize(textureToReplace, element["tileSize"], element["tileSize"])
102+
isSized = isImageSize(textureToReplace, position[2] - position[0], position[3] - position[1])
125103
if not isSized and not allowResize:
126-
messagebox.showerror("Invalid texture", f"Texture selected is not {element['tileSize']}x{element['tileSize']}")
104+
messagebox.showerror("Invalid texture", f"Texture selected is not {position[2] - position[0]}x{position[3] - position[1]}")
127105
else:
128106
if not isSized:
129-
textureToReplace = textureToReplace.resize((element['tileSize'], element['tileSize']), Image.Resampling.LANCZOS)
107+
textureToReplace = textureToReplace.resize((position[2] - position[0], position[3] - position[1]), Image.Resampling.LANCZOS)
130108
atlas.addElement(position, textureToReplace)
131-
if atlasType == "Items":
132-
duplicated = checkForMatch(value, added.getItems())
133-
134-
if not os.path.exists(f"{outputDir}/textures/items"):
135-
os.makedirs(f"{outputDir}/textures/items")
136-
137-
newTexture = Texture3dst().fromImage(textureToReplace)
138-
newTexture.export(f"{outputDir}/textures/items/{value}.3dst")
139-
140-
if duplicated == -1:
141-
added.addItem(value)
142-
elif atlasType == "Blocks":
143-
duplicated = checkForMatch(value, added.getItems())
109+
duplicated = checkForMatch(value, added.getItems())
144110

145-
if not os.path.exists(f"{outputDir}/textures/blocks"):
146-
os.makedirs(f"{outputDir}/textures/blocks")
111+
if not os.path.exists(f"{outputDir}/textures"):
112+
os.makedirs(f"{outputDir}/textures")
147113

148-
newTexture = Texture3dst().fromImage(textureToReplace)
149-
newTexture.export(f"{outputDir}/textures/blocks/{value}.3dst")
114+
newTexture = Texture3dst().fromImage(textureToReplace)
115+
newTexture.export(f"{outputDir}/textures/{value}.3dst")
150116

151-
if duplicated == -1:
152-
added.addItem(value)
117+
if duplicated == -1:
118+
added.addItem(value)
153119

154120
# Updates portview
155-
self.showItemInfo(value, self.lastActualOption)
121+
self.showItemInfo(value)
156122

157123
self.globalVars.saved = False
158124
self.globalVars.updateList()
159125
self.buttonReplace.configure(state="normal")
160126

161-
def showItemInfo(self, value = None, cat_opt = None):
127+
def showItemInfo(self, value = None):
162128
if value == None:
163129
item = self.globalVars.treeElementSelected
164130
if not "values" in item:

0 commit comments

Comments
 (0)