Bläddra i källkod

tools: add a way to create new materials

Daniele Bartolini 10 månader sedan
förälder
incheckning
d520306201

+ 1 - 0
docs/changelog.rst

@@ -8,6 +8,7 @@ Changelog
 
 * Added the ability to import skeletons and animation clips from FBX files.
 * Added the ability to skip creating units when importing FBX files.
+* Added the ability to create new materials from the Project Browser.
 * Fixed a crash when editing many objects simultaneusly.
 * Fixed a crash when trying to load levels with many objects.
 * Fixed an issue that prevented undo/redo operations to be executed in bulk.

+ 17 - 0
tools/level_editor/level_editor.vala

@@ -564,6 +564,7 @@ public class LevelEditorApplication : Gtk.Application
 		{ "create-script",        on_create_script,        "(ssb)", null },
 		{ "create-unit",          on_create_unit,          "(ss)",  null },
 		{ "create-state-machine", on_create_state_machine, "(sss)", null },
+		{ "create-material",      on_create_material,      "(ss)",  null },
 		{ "open-containing",      on_open_containing,      "s",     null },
 		{ "texture-settings",     on_texture_settings,     "s",     null },
 		{ "reveal-resource",      on_reveal,               "(ss)",  null }
@@ -3062,6 +3063,22 @@ public class LevelEditorApplication : Gtk.Application
 			});
 	}
 
+	private void on_create_material(GLib.SimpleAction action, GLib.Variant? param)
+	{
+		string dir_name = (string)param.get_child_value(0);
+		string material_name = (string)param.get_child_value(1);
+
+		int ec = _project.create_material(dir_name, material_name);
+		if (ec < 0) {
+			loge("Failed to create material %s".printf(material_name));
+			return;
+		}
+
+		_data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
+				_data_compiler.compile.end(res);
+			});
+	}
+
 	private void on_open_containing(GLib.SimpleAction action, GLib.Variant? param)
 	{
 		string path = param.get_string();

+ 10 - 0
tools/level_editor/project.vala

@@ -390,6 +390,16 @@ public class Project
 		return 0;
 	}
 
+	public int create_material(string directory, string name)
+	{
+		string resource_name = Path.build_filename(directory, name);
+
+		Database db = new Database(this, null);
+		Guid material_id = Guid.new_guid();
+		MaterialResource material_resource = MaterialResource.mesh(db, material_id);
+		return material_resource.save(this, resource_name);
+	}
+
 	// Returns the absolute path to the source directory.
 	public string source_dir()
 	{

+ 32 - 0
tools/level_editor/project_browser.vala

@@ -142,6 +142,38 @@ private Gtk.Menu? project_entry_menu_create(string type, string name)
 			});
 		menu.add(mi);
 
+		mi = new Gtk.MenuItem.with_label("New Material...");
+		mi.activate.connect(() => {
+				Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Material Name"
+					, ((Gtk.Application)GLib.Application.get_default()).active_window
+					, DialogFlags.MODAL
+					, "Cancel"
+					, ResponseType.CANCEL
+					, "Ok"
+					, ResponseType.OK
+					, null
+					);
+
+				EntryText sb = new EntryText();
+				sb.activate.connect(() => { dg.response(ResponseType.OK); });
+				dg.get_content_area().add(sb);
+				dg.skip_taskbar_hint = true;
+				dg.show_all();
+
+				if (dg.run() == (int)ResponseType.OK) {
+					if (sb.text.strip() == "") {
+						dg.destroy();
+						return;
+					}
+
+					var tuple = new GLib.Variant.tuple({(string)name, sb.text});
+					GLib.Application.get_default().activate_action("create-material", tuple);
+				}
+
+				dg.destroy();
+			});
+		menu.add(mi);
+
 		mi = new Gtk.SeparatorMenuItem();
 		menu.add(mi);