Skip to content

Commit

Permalink
tools: import delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
dbartolini committed Jan 2, 2025
1 parent 4eef4f2 commit d8f35f6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
28 changes: 17 additions & 11 deletions tools/level_editor/level_editor.vala
Original file line number Diff line number Diff line change
Expand Up @@ -790,11 +790,11 @@ public class LevelEditorApplication : Gtk.Application

_project = new Project();
_project.set_toolchain_dir(_toolchain_dir.get_path());
_project.register_importer("Sprite", { "png" }, SpriteResource.import, 0.0);
_project.register_importer("Mesh", { "mesh" }, MeshResource.import, 1.0);
_project.register_importer("Sound", { "wav" }, SoundResource.import, 2.0);
_project.register_importer("Texture", { "png", "tga", "dds", "ktx", "pvr" }, TextureResource.import, 2.0);
_project.register_importer("Font", { "ttf", "otf" }, FontResource.import, 3.0);
_project.register_importer("Sprite", { "png" }, SpriteResource.import, on_import_result, 0.0);
_project.register_importer("Mesh", { "mesh" }, MeshResource.import, on_import_result, 1.0);
_project.register_importer("Sound", { "wav" }, SoundResource.import, on_import_result, 2.0);
_project.register_importer("Texture", { "png", "tga", "dds", "ktx", "pvr" }, TextureResource.import, on_import_result, 2.0);
_project.register_importer("Font", { "ttf", "otf" }, FontResource.import, on_import_result, 3.0);
_project.project_reset.connect(on_project_reset);
_project.project_loaded.connect(on_project_loaded);

Expand Down Expand Up @@ -2377,15 +2377,12 @@ public class LevelEditorApplication : Gtk.Application
save_as(null);
}

private void on_import(GLib.SimpleAction action, GLib.Variant? param)
private void on_import_result(ImportResult result)
{
string? destination_dir = param == null ? null : param.get_string();

ImportResult ec = _project.import(destination_dir, this.active_window);
if (ec == ImportResult.ERROR) {
if (result == ImportResult.ERROR) {
loge("Failed to import resource(s)");
return;
} else if (ec == ImportResult.SUCCESS) {
} else if (result == ImportResult.SUCCESS) {
_data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
_data_compiler.compile.end(res);
});
Expand All @@ -2395,6 +2392,15 @@ public class LevelEditorApplication : Gtk.Application
_level.selection_changed(_level._selection);
}

private void on_import(GLib.SimpleAction action, GLib.Variant? param)
{
string? destination_dir = param == null ? null : param.get_string();

ImportResult ec = _project.import(destination_dir, on_import_result, this.active_window);
if (ec != ImportResult.CALLBACK)
on_import_result(ec);
}

private void on_preferences(GLib.SimpleAction action, GLib.Variant? param)
{
_preferences_dialog.set_transient_for(this.active_window);
Expand Down
20 changes: 13 additions & 7 deletions tools/level_editor/project.vala
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ public enum ImportResult
{
SUCCESS, ///< Data imported successfully.
ERROR, ///< Error during import or elsewhere.
CANCEL ///< User cancelled the import.
CANCEL, ///< User cancelled the import.
CALLBACK ///< The actual importing will happen in the provided callback.
}

public delegate void Import(ImportResult result);

public class Project
{
public const string LEVEL_EDITOR_TEST_NAME = "_level_editor_test";

public delegate ImportResult ImporterDelegate(Project project, string destination_dir, SList<string> filenames);
public delegate ImportResult ImporterDelegate(Project project, string destination_dir, SList<string> filenames, Import import_result);

[Compact]
public struct ImporterData
Expand All @@ -25,13 +28,15 @@ public class Project
public Gee.ArrayList<string> extensions;
public double order;
public Gtk.FileFilter _filter;
public unowned Import import_result;

ImporterData()
{
delegate = null;
extensions = new Gee.ArrayList<string>();
order = 0.0;
_filter = new Gtk.FileFilter();
import_result = null;
}

public bool can_import_extension(string extension)
Expand Down Expand Up @@ -518,7 +523,7 @@ public class Project
return Path.build_filename(prefix, resource_path);
}

public static ImportResult import_all_extensions(Project project, string destination_dir, SList<string> filenames)
public static ImportResult import_all_extensions(Project project, string destination_dir, SList<string> filenames, Import import_result)
{
Gee.ArrayList<string> paths = new Gee.ArrayList<string>();
foreach (var item in filenames)
Expand Down Expand Up @@ -559,7 +564,7 @@ public class Project
foreach (var item in importables)
importables_list.append(item);

result = importer.delegate(project, destination_dir, importables_list);
result = importer.delegate(project, destination_dir, importables_list, import_result);
}

return result;
Expand Down Expand Up @@ -598,12 +603,13 @@ public class Project
// Registers an @a importer for importing source data with the given @a
// extensions. @a order is used to establish precedence when distinct importers
// support similar extensions; lower values have higher precedence.
public void register_importer(string name, string[] extensions, ImporterDelegate importer, double order)
public void register_importer(string name, string[] extensions, ImporterDelegate importer, Import import_result, double order)
{
ImporterData data = ImporterData();
data.delegate = importer;
data.extensions.add_all_array(extensions);
data.order = order;
data.import_result = import_result;

register_importer_internal(name, ref data);
}
Expand All @@ -630,7 +636,7 @@ public class Project
return find_importer_for_extension(type) != null;
}

public ImportResult import(string? destination_dir, Gtk.Window? parent_window = null)
public ImportResult import(string? destination_dir, Import import_result, Gtk.Window? parent_window = null)
{
Gtk.FileChooserDialog src = new Gtk.FileChooserDialog("Import..."
, parent_window
Expand Down Expand Up @@ -691,7 +697,7 @@ public class Project
if (importer == null)
importer = _all_extensions_importer_data.delegate;

return importer(this, out_dir, filenames);
return importer(this, out_dir, filenames, import_result);
}

public void delete_tree(GLib.File file) throws Error
Expand Down

0 comments on commit d8f35f6

Please sign in to comment.