Skip to content

Commit

Permalink
Fixed loading external images. Added confirmation dialog for texture …
Browse files Browse the repository at this point in the history
…editing.
  • Loading branch information
rainlizard committed May 20, 2024
1 parent f32b206 commit 4fc48f6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Autoload/Graphics.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ func load_extra_images_from_harddrive():
var custom_images_dir = Settings.unearthdata.plus_file("custom-object-images")
var image_paths = Utils.get_filetype_in_directory(custom_images_dir, "png")
for image_path in image_paths:
var texture = load(image_path)
if texture is Texture:
var texture = Utils.load_external_texture(image_path)
if texture is ImageTexture:
var image_name = image_path.get_file().get_basename().to_upper()
sprite_id[image_name] = texture
else:
Expand Down
7 changes: 7 additions & 0 deletions Autoload/Utils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func string_has_letters(string):
return true
return false

func load_external_texture(path):
var img = Image.new()
img.load(path)
var texture = ImageTexture.new()
texture.create_from_image(img)
return texture

func get_filetype_in_directory(directory_path: String, file_extension: String) -> Array:
var files = []
var directory = Directory.new()
Expand Down
32 changes: 30 additions & 2 deletions Scenes/TextureEditingWindow.gd
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func _on_TextureEditingHelpButton_pressed():
var helptxt = ""
helptxt += "After you load a tileset, a bunch of .PNG files will be saved to your hard drive. Edit these files in your favourite image editor.\n"
helptxt += "Unearth will actively reload the textures in real-time as you edit and save those .PNGs. So any edits you make will be shown in real-time in Unearth. This applies to the 3D view too, so press Spacebar while in the 3D view to stop the camera from moving.\n"
helptxt += "Be aware that 'Load tileset' may overwrite your .PNGs, so be sure to press 'Save tileset' when you're done otherwise you could lose data.\n"
oMessage.big("Help",helptxt)


Expand Down Expand Up @@ -213,6 +212,9 @@ func _on_ExportTmapaDatDialog_file_selected(path):



var user_cancelled = false


func _on_ChooseTmapaFileDialog_file_selected(path):
var sourceImg = oTextureCache.convert_tmapa_to_image(path)
if sourceImg == null: return
Expand All @@ -232,7 +234,6 @@ func _on_ChooseTmapaFileDialog_file_selected(path):
flContent = flContent.replace("textures_pack_number", "textures_pack_" + numberString)
filelistFile.close()


var lineArray = Array(flContent.split('\n', false))
lineArray.pop_front() # For the array remove the first line: textures_pack_number 8 68 32 32

Expand Down Expand Up @@ -279,14 +280,33 @@ func _on_ChooseTmapaFileDialog_file_selected(path):

# Save PNGs (separate loop so we're iterating once on each file, rather than every single filelist line)

var checkForExistingFolderOnce = 0

var dir = Directory.new()
for localPath in imageDictionary:

var savePath = outputDir.plus_file(localPath)
var packFolder = savePath.get_base_dir()


if dir.dir_exists(packFolder) == false:
dir.make_dir_recursive(packFolder)
else:
if checkForExistingFolderOnce == 0:
var confirmDialog = ConfirmationDialog.new()
confirmDialog.dialog_text = "The folder of .PNGs already exists, they will be overwritten: \n" + packFolder + "\n\n If overwriting the files here causes you data loss then Cancel and go backup the folder."
confirmDialog.window_title = "Confirm File Replacement"
confirmDialog.popup_exclusive = true
confirmDialog.connect("confirmed", self, "_on_confirmation_confirmed")
confirmDialog.connect("popup_hide", self, "_on_confirmation_hide")
add_child(confirmDialog)
confirmDialog.popup_centered()
yield(confirmDialog, "visibility_changed")
yield(get_tree(),'idle_frame')
if user_cancelled == true:
return

checkForExistingFolderOnce = 1

var createNewImage = imageDictionary[localPath]
createNewImage.save_png(savePath)
Expand All @@ -299,6 +319,14 @@ func _on_ChooseTmapaFileDialog_file_selected(path):
save_new_filelist_txt_file(flContent, numberString, outputDir) # This goes after the "make_dir_recursive" commands
print('Exported Filelist in: ' + str(OS.get_ticks_msec() - CODETIME_START) + 'ms')

func _on_confirmation_confirmed():
user_cancelled = false

func _on_confirmation_hide():
if not user_cancelled:
user_cancelled = true


func save_new_filelist_txt_file(flContent, numberString, outputDir):
var file = File.new()
var path = outputDir.plus_file("filelist_tmapa"+numberString+".txt")
Expand Down

0 comments on commit 4fc48f6

Please sign in to comment.