refactor of the save method & replaced the path strings with a maybe(string) instead of a string+bool

This commit is contained in:
Stefan Stefanov 2024-04-23 12:39:32 +03:00
parent d68a2f99b5
commit 0faa687d05
4 changed files with 82 additions and 43 deletions

2
.vscode/tasks.json vendored
View file

@ -17,7 +17,7 @@
}, },
"group": { "group": {
"kind": "build", "kind": "build",
"isDefault": true "isDefault": false
}, },
}, },
{ {

View file

@ -45,7 +45,7 @@ update :: proc() {
rl.SetMouseScale(1 / scaling, 1 / scaling) rl.SetMouseScale(1 / scaling, 1 / scaling)
if g_mem.should_open_file_dialog { if g_mem.should_open_file_dialog {
open_file_dialog_and_store_output_paths() open_file_dialog()
} }
} }
@ -80,10 +80,8 @@ draw_screen_target :: proc() {
atlas_entries: [dynamic]AtlasEntry atlas_entries: [dynamic]AtlasEntry
delete(atlas_entries) delete(atlas_entries)
if g_mem.input_path_set { if files, ok := g_mem.source_files_to_pack.([]string); ok {
unmarshall_aseprite_dir(g_mem.output_folder_path, &atlas_entries) unmarshall_aseprite_files(files, &atlas_entries)
} else if g_mem.input_files_set {
unmarshall_aseprite_files(g_mem.source_files_to_pack, &atlas_entries)
} else { } else {
fmt.println("No source folder or files set! Can't pack the void!!!") fmt.println("No source folder or files set! Can't pack the void!!!")
g_mem.should_render_atlas = false g_mem.should_render_atlas = false
@ -102,7 +100,7 @@ draw_screen_target :: proc() {
// OpenGL's Y buffer is flipped // OpenGL's Y buffer is flipped
rl.ImageFlipVertical(&atlas) rl.ImageFlipVertical(&atlas)
// rl.UnloadTexture(atlas_render_target.texture) // rl.UnloadTexture(atlas_render_target.texture)
fmt.println("Packed everything!") fmt.println("Packed everything!")
atlas_render_target.texture = rl.LoadTextureFromImage(atlas) atlas_render_target.texture = rl.LoadTextureFromImage(atlas)
g_mem.should_render_atlas = false g_mem.should_render_atlas = false
@ -410,9 +408,12 @@ draw_atlas_settings_and_preview :: proc() {
} }
} }
open_file_dialog_and_store_output_paths :: proc() { open_file_dialog :: proc() {
if g_mem.source_location_type == .SourceFiles { switch g_mem.source_location_type {
files := cstring( case .SourceFiles:
// `open_file_dialog` returns a single cstring with one or more paths, divided by a separator ('|'),
// https://github.com/native-toolkit/libtinyfiledialogs/blob/master/tinyfiledialogs.c#L2706
file_paths_conc := cstring(
diag.open_file_dialog( diag.open_file_dialog(
"Select source files", "Select source files",
cstring(&g_mem.file_dialog_text_buffer[0]), cstring(&g_mem.file_dialog_text_buffer[0]),
@ -422,37 +423,69 @@ open_file_dialog_and_store_output_paths :: proc() {
1, 1,
), ),
) )
if len(file_paths_conc) > 0 {
// todo(stefan): Currently we're not doing any checks if the filepaths are valid at all,
// this should be fine because it's returned by the OS' file picker but who knows...
source_files_to_pack := strings.clone_from_cstring(file_paths_conc, context.allocator)
g_mem.source_files_to_pack = strings.split(source_files_to_pack, "|")
source_files_to_pack := strings.clone_from_cstring(files, context.allocator) fmt.println(g_mem.source_files_to_pack)
// File dialog returns an array of path(s), separated by a '|' } else {
g_mem.source_files_to_pack = strings.split(source_files_to_pack, "|") fmt.println("No files were selected!")
g_mem.input_files_set = (len(source_files_to_pack) > 0) }
fmt.println(g_mem.source_files_to_pack) case .SourceFolder:
}
if g_mem.source_location_type == .SourceFolder {
file := cstring( file := cstring(
diag.select_folder_dialog( diag.select_folder_dialog(
"Select source folder", "Select source folder",
cstring(&g_mem.file_dialog_text_buffer[0]), cstring(&g_mem.file_dialog_text_buffer[0]),
), ),
) )
g_mem.source_location_to_pack = strings.clone_from_cstring(file) if len(file) > 0 {
g_mem.input_path_set = (len(file) > 0) g_mem.source_location_to_pack = strings.clone_from_cstring(file)
fmt.println(g_mem.source_location_to_pack) fmt.println(g_mem.source_location_to_pack)
} } else {
if g_mem.source_location_type == .OutputFolder { fmt.println("Got an empty path from the file dialog!")
}
case .OutputFolder:
file := cstring( file := cstring(
diag.select_folder_dialog( diag.select_folder_dialog(
"Select source folder", "Select source folder",
cstring(&g_mem.file_dialog_text_buffer[0]), cstring(&g_mem.file_dialog_text_buffer[0]),
), ),
) )
g_mem.output_folder_path = strings.clone_from_cstring(file) if len(file) > 0 {
g_mem.output_folder_path = strings.clone_from_cstring(file)
fmt.println(g_mem.output_folder_path)
} else {
fmt.println("Got an empty path from the file dialog!")
}
g_mem.output_path_set = (len(file) > 0) case .SaveFileAs:
fmt.println(g_mem.output_folder_path) file_path: cstring
patterns: []cstring = {"*.png"}
if default_path, ok := g_mem.output_folder_path.(string); ok {
default_path_filename := strings.concatenate({default_path, atlas_path})
default_path_to_save: cstring = strings.clone_to_cstring(default_path_filename)
file_path = cstring(
diag.save_file_dialog(
"Save as...",
default_path_to_save,
1,
&patterns[0],
"Atlas",
),
)
} else {
file_path = cstring(diag.save_file_dialog("Save as...", "", 1, &patterns[0], "Atlas"))
}
if file_path != nil {
save_output()
}
} }
g_mem.should_open_file_dialog = false g_mem.should_open_file_dialog = false
} }

View file

@ -25,7 +25,7 @@ FileDialogType :: enum {
SourceFiles, SourceFiles,
SourceFolder, SourceFolder,
OutputFolder, OutputFolder,
Exit, SaveFileAs,
} }
PackerSettings :: struct { PackerSettings :: struct {
@ -46,14 +46,11 @@ GameMemory :: struct {
window_info: WindowInformation, window_info: WindowInformation,
monitor_info: MonitorInformation, monitor_info: MonitorInformation,
// Where the output files will be written (atlas.png, json output, etc) // Where the output files will be written (atlas.png, json output, etc)
output_path_set: bool, output_folder_path: Maybe(string),
output_folder_path: string,
// If files were chosen as input - their paths // If files were chosen as input - their paths
input_path_set: bool, source_location_to_pack: Maybe(string),
source_location_to_pack: string,
// If a folder was chosen as input - the path // If a folder was chosen as input - the path
input_files_set: bool, source_files_to_pack: Maybe([]string),
source_files_to_pack: []string,
// What type of file dialog to open // What type of file dialog to open
source_location_type: FileDialogType, source_location_type: FileDialogType,
// Packer settings // Packer settings

View file

@ -1,17 +1,26 @@
package game package game
import "core:strings" import "core:strings"
import "core:fmt"
import rl "vendor:raylib" import rl "vendor:raylib"
save_output :: proc() { when ODIN_OS == .Windows {
image := rl.LoadImageFromTexture(g_mem.atlas_render_texture_target.texture) atlas_path :: "\\atlas.png"
rl.ImageFlipVertical(&image) } else {
when ODIN_OS == .Windows { atlas_path :: "/atlas.png"
atlas_path :: "\\atlas.png" }
} else {
atlas_path :: "/atlas.png" save_output :: proc() {
} if output_path, ok := g_mem.output_folder_path.(string); ok {
output_path := strings.concatenate({g_mem.output_folder_path, atlas_path}) if len(output_path) == 0 {
cstring_output_path := strings.clone_to_cstring(output_path) fmt.println("Output path is empty!")
rl.ExportImage(image, cstring_output_path) return
}
image := rl.LoadImageFromTexture(g_mem.atlas_render_texture_target.texture)
rl.ImageFlipVertical(&image)
output_path := strings.concatenate({output_path, atlas_path})
cstring_output_path := strings.clone_to_cstring(output_path)
rl.ExportImage(image, cstring_output_path)
}
} }