Explorar o código

tools: add ability to rename a sprite before it is imported

Daniele Bartolini %!s(int64=2) %!d(string=hai) anos
pai
achega
27e02810fc

+ 1 - 0
docs/changelog.rst

@@ -10,6 +10,7 @@ Changelog
 
 **Tools**
 
+* Added the ability to rename a sprite in the Sprite Importer.
 * Fixed initial 'sensitivity' state in some widgets.
 * Fixed erratic messages when importing assets and improved import procedure robustness.
 

+ 9 - 2
tools/level_editor/sprite_import_dialog.vala

@@ -92,6 +92,7 @@ public class SpriteImportDialog : Gtk.Dialog
 	public Gtk.ScrolledWindow _scrolled_window;
 	public Gtk.DrawingArea _preview;
 
+	public EntryResourceBasename _unit_name;
 	public Gtk.Label resolution;
 	public EntryVector2 cells;
 	public Gtk.CheckButton cell_wh_auto;
@@ -121,14 +122,14 @@ public class SpriteImportDialog : Gtk.Dialog
 	public Gtk.CheckButton lock_rotation_y;
 
 	// Widgets
-	public SpriteImportDialog(string png)
+	public SpriteImportDialog(string image_path, string unit_name)
 	{
 		this.border_width = 4;
 		this.title = "Import Sprite...";
 		this.set_icon_name(CROWN_ICON_NAME);
 
 		try {
-			_pixbuf = new Gdk.Pixbuf.from_file(png);
+			_pixbuf = new Gdk.Pixbuf.from_file(image_path);
 		} catch (GLib.Error e) {
 			loge(e.message);
 		}
@@ -292,6 +293,8 @@ public class SpriteImportDialog : Gtk.Dialog
 				return Gdk.EVENT_STOP;
 			});
 
+		_unit_name = new EntryResourceBasename(unit_name);
+
 		resolution = new Gtk.Label(_pixbuf.width.to_string() + " × " + _pixbuf.height.to_string());
 		resolution.halign = Gtk.Align.START;
 
@@ -423,6 +426,10 @@ public class SpriteImportDialog : Gtk.Dialog
 		sprite_set.border_width = 6;
 
 		PropertyGrid cv;
+		cv = new PropertyGrid();
+		cv.add_row("Name", _unit_name);
+		sprite_set.add_property_grid(cv, "Unit");
+
 		cv = new PropertyGrid();
 		cv.add_row("Resolution", resolution);
 		cv.add_row("Cells", cells);

+ 23 - 3
tools/resource/sprite_resource.vala

@@ -11,17 +11,31 @@ public class SpriteResource
 	{
 		Hashtable importer_settings = null;
 		string importer_settings_path = null;
+		string image_path;
+		string image_type;
+		string image_name;
 		{
 			GLib.File file_src = File.new_for_path(filenames.nth_data(0));
-			GLib.File file_dst = File.new_for_path(Path.build_filename(destination_dir, file_src.get_basename()));
+			image_path = file_src.get_path();
+			image_type = image_path.substring(image_path.last_index_of_char('.') + 1
+				, image_path.length - image_path.last_index_of_char('.') - 1
+				);
 
+			GLib.File file_dst = File.new_for_path(Path.build_filename(destination_dir, file_src.get_basename()));
 			string resource_filename = project._source_dir.get_relative_path(file_dst);
 			string resource_path     = resource_filename.substring(0, resource_filename.last_index_of_char('.'));
 
 			importer_settings_path = Path.build_filename(project.source_dir(), resource_path) + ".importer_settings";
+
+			int last_slash = resource_path.last_index_of_char('/');
+			if (last_slash == -1)
+				image_name = resource_path;
+			else
+				image_name = resource_path.substring(last_slash + 1, resource_path.length - last_slash - 1);
 		}
 
-		SpriteImportDialog dlg = new SpriteImportDialog(filenames.nth_data(0));
+		SpriteImportDialog dlg = new SpriteImportDialog(image_path, image_name);
+		dlg._unit_name.sensitive = filenames.length() == 1;
 		dlg.show_all();
 
 		if (File.new_for_path(importer_settings_path).query_exists()) {
@@ -69,13 +83,19 @@ public class SpriteResource
 		string actor_class             = (string)dlg.actor_class.value;
 		bool lock_rotation_y           = dlg.lock_rotation_y.active;
 		double mass                    = (double)dlg.mass.value;
+		image_name                     = dlg._unit_name.value;
 
 		dlg.destroy();
 
 		foreach (unowned string filename_i in filenames) {
 			GLib.File file_src = File.new_for_path(filename_i);
-			GLib.File file_dst = File.new_for_path(Path.build_filename(destination_dir, file_src.get_basename()));
+			string resource_basename;
+			if (filenames.length() == 1)
+				resource_basename = image_name + "." + image_type;
+			else
+				resource_basename = file_src.get_basename();
 
+			GLib.File file_dst = File.new_for_path(Path.build_filename(destination_dir, resource_basename));
 			string resource_filename = project._source_dir.get_relative_path(file_dst);
 			string resource_path     = resource_filename.substring(0, resource_filename.last_index_of_char('.'));
 			string resource_name     = ResourceId.resource_name(resource_path);

+ 81 - 0
tools/widgets/entry_resource_basename.vala

@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2012-2023 Daniele Bartolini et al.
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+using Gtk;
+
+namespace Crown
+{
+public class EntryResourceBasename : EntryText
+{
+	public string _basename;
+	public bool _stop_emit;
+
+	public string value
+	{
+		get
+		{
+			return this.text;
+		}
+		set
+		{
+			_stop_emit = true;
+			set_value_safe(value);
+			_stop_emit = false;
+		}
+	}
+
+	// Signals
+	public signal void value_changed();
+
+	public EntryResourceBasename(string basename)
+	{
+		this.activate.connect(on_activate);
+
+		_stop_emit = true;
+		_basename = "unset";
+		set_value_safe(basename);
+		_stop_emit = false;
+	}
+
+	private void on_activate()
+	{
+		this.select_region(0, 0);
+		this.set_position(-1);
+		this.set_value_safe(this.text);
+	}
+
+	private void set_value_safe(string val)
+	{
+		if (val.length == 0
+			|| val.has_prefix(" ")
+			|| val.has_suffix(" ")
+			|| val.index_of_char('.') != -1
+			|| val.index_of_char('<') != -1
+			|| val.index_of_char('>') != -1
+			|| val.index_of_char(':') != -1
+			|| val.index_of_char('"') != -1
+			|| val.index_of_char('/') != -1
+			|| val.index_of_char('\\') != -1
+			|| val.index_of_char('|') != -1
+			|| val.index_of_char('?') != -1
+			|| val.index_of_char('*') != -1
+			|| val.is_ascii() == false
+			) {
+			this.text = _basename;
+			return;
+		}
+
+		this.text = val;
+
+		// Notify value changed.
+		if (_basename != val) {
+			_basename = val;
+			if (!_stop_emit)
+				value_changed();
+		}
+	}
+}
+
+} /* namespace Crown */