Jelajahi Sumber

tools: use object_type() to determine the object types!

Daniele Bartolini 4 tahun lalu
induk
melakukan
2e95eb4ae0
2 mengubah file dengan 64 tambahan dan 64 penghapusan
  1. 10 20
      tools/level_editor/level.vala
  2. 54 44
      tools/level_editor/properties_view.vala

+ 10 - 20
tools/level_editor/level.vala

@@ -122,9 +122,9 @@ public class Level
 
 		foreach (Guid id in ids)
 		{
-			if (is_unit(id))
+			if (_db.object_type(id) == "unit")
 				units += id;
-			else if (is_sound(id))
+			else if (_db.object_type(id) == "sound_source")
 				sounds += id;
 		}
 
@@ -202,11 +202,11 @@ public class Level
 		{
 			_db.duplicate(ids[i], new_ids[i]);
 
-			if (is_unit(ids[i]))
+			if (_db.object_type(ids[i]) == "unit")
 			{
 				_db.add_to_set(_id, "units", new_ids[i]);
 			}
-			else if (is_sound(ids[i]))
+			else if (_db.object_type(ids[i]) == "sound_source")
 			{
 				_db.add_to_set(_id, "sounds", new_ids[i]);
 			}
@@ -270,7 +270,7 @@ public class Level
 			Quaternion rot = rotations[i];
 			Vector3 scl = scales[i];
 
-			if (is_unit(id))
+			if (_db.object_type(id) == "unit")
 			{
 				Unit unit = new Unit(_db, id);
 				Guid component_id;
@@ -287,7 +287,7 @@ public class Level
 					_db.set_property_vector3   (id, "scale", scl);
 				}
 			}
-			else if (is_sound(id))
+			else if (_db.object_type(id) == "sound_source")
 			{
 				_db.set_property_vector3   (id, "position", pos);
 				_db.set_property_quaternion(id, "rotation", rot);
@@ -490,11 +490,11 @@ public class Level
 		StringBuilder sb = new StringBuilder();
 		for (int i = 0; i < ids.length; ++i)
 		{
-			if (is_unit(ids[i]))
+			if (_db.object_type(ids[i]) == "unit")
 			{
 				generate_spawn_unit_commands(new Guid[] { ids[i] }, sb);
 			}
-			else if (is_sound(ids[i]))
+			else if (_db.object_type(ids[i]) == "sound_source")
 			{
 				generate_spawn_sound_commands(new Guid[] { ids[i] }, sb);
 			}
@@ -691,7 +691,7 @@ public class Level
 
 				for (int i = 0; i < ids.length; ++i)
 				{
-					if (is_unit(ids[i]))
+					if (_db.object_type(ids[i]) == "unit")
 					{
 						Guid unit_id = ids[i];
 
@@ -710,7 +710,7 @@ public class Level
 							scales[i]    = _db.get_property_vector3   (unit_id, "scale");
 						}
 					}
-					else if (is_sound(ids[i]))
+					else if (_db.object_type(ids[i]) == "sound_source")
 					{
 						Guid sound_id = ids[i];
 						positions[i] = _db.get_property_vector3   (sound_id, "position");
@@ -850,16 +850,6 @@ public class Level
 		}
 	}
 
-	public bool is_unit(Guid id)
-	{
-		return _db.get_property_set(_id, "units", new HashSet<Guid?>()).contains(id);
-	}
-
-	public bool is_sound(Guid id)
-	{
-		return _db.get_property_set(_id, "sounds", new HashSet<Guid?>()).contains(id);
-	}
-
 	private void fix_objects_types()
 	{
 		HashSet<Guid?> units = _db.get_property_set(_id, "units", new HashSet<Guid?>());

+ 54 - 44
tools/level_editor/properties_view.vala

@@ -687,12 +687,14 @@ public class PropertiesView : Gtk.Bin
 
 	// Data
 	private Level _level;
+	private Database _db;
 	private HashMap<string, Gtk.Expander> _expanders;
 	private HashMap<string, PropertyGrid> _objects;
 	private ArrayList<ComponentEntry?> _entries;
 
 	// Widgets
 	private Gtk.Label _nothing_to_show;
+	private Gtk.Label _unknown_object_type;
 	private Gtk.Viewport _viewport;
 	private Gtk.ScrolledWindow _scrolled_window;
 	private PropertyGridSet _object_view;
@@ -703,6 +705,7 @@ public class PropertiesView : Gtk.Bin
 		// Data
 		_level = level;
 		_level.selection_changed.connect(on_selection_changed);
+		_db = level._db;
 
 		_expanders = new HashMap<string, Gtk.Expander>();
 		_objects = new HashMap<string, PropertyGrid>();
@@ -729,6 +732,7 @@ public class PropertiesView : Gtk.Bin
 		register_object_type("Sound",     "sound_properties", 1, new SoundView(_level, store));
 
 		_nothing_to_show = new Gtk.Label("Nothing to show");
+		_unknown_object_type = new Gtk.Label("Unknown object type");
 
 		_viewport = new Gtk.Viewport(null, null);
 		_viewport.add(_object_view);
@@ -752,65 +756,71 @@ public class PropertiesView : Gtk.Bin
 		_entries.add({ object_type, position });
 	}
 
-	private void on_selection_changed(Gee.ArrayList<Guid?> selection)
+	public void show_unit(Guid id)
 	{
-		if (selection.size != 1)
-		{
-			_stack.set_visible_child(_nothing_to_show);
-			return;
-		}
-
-		Guid id = selection[selection.size - 1];
+		_stack.set_visible_child(_scrolled_window);
 
-		if (_level.is_unit(id))
+		foreach (var entry in _entries)
 		{
-			_stack.set_visible_child(_scrolled_window);
+			Gtk.Expander expander = _expanders[entry.type];
 
-			foreach (var entry in _entries)
+			Unit unit = new Unit(_db, id);
+			Guid component_id;
+			if (unit.has_component(out component_id, entry.type) || entry.type == "name")
 			{
-				Gtk.Expander expander = _expanders[entry.type];
-
-				Unit unit = new Unit(_level._db, id);
-				Guid component_id;
-				if (unit.has_component(out component_id, entry.type) || entry.type == "name")
-				{
-					PropertyGrid cv = _objects[entry.type];
-					cv._id = id;
-					cv._component_id = component_id;
-					cv.update();
-					expander.show_all();
-				}
-				else
-				{
-					expander.hide();
-				}
+				PropertyGrid cv = _objects[entry.type];
+				cv._id = id;
+				cv._component_id = component_id;
+				cv.update();
+				expander.show_all();
+			}
+			else
+			{
+				expander.hide();
 			}
 		}
-		else if (_level.is_sound(id))
+	}
+
+	public void show_sound_source(Guid id)
+	{
+		_stack.set_visible_child(_scrolled_window);
+
+		foreach (var entry in _entries)
 		{
-			_stack.set_visible_child(_scrolled_window);
+			Gtk.Expander expander = _expanders[entry.type];
 
-			foreach (var entry in _entries)
+			if (entry.type == "sound_transform" || entry.type == "sound_properties")
 			{
-				Gtk.Expander expander = _expanders[entry.type];
-
-				if (entry.type == "sound_transform" || entry.type == "sound_properties")
-				{
-					PropertyGrid cv = _objects[entry.type];
-					cv._id = id;
-					cv.update();
-					expander.show_all();
-				}
-				else
-				{
-					expander.hide();
-				}
+				PropertyGrid cv = _objects[entry.type];
+				cv._id = id;
+				cv.update();
+				expander.show_all();
+			}
+			else
+			{
+				expander.hide();
 			}
 		}
-		else
+	}
+
+	public void on_selection_changed(Gee.ArrayList<Guid?> selection)
+	{
+		if (selection.size != 1)
 		{
 			_stack.set_visible_child(_nothing_to_show);
+			return;
 		}
+
+		Guid id = selection[selection.size - 1];
+		if (!_db.has_object(id))
+			return;
+
+		if (_db.object_type(id) == "unit")
+			show_unit(id);
+		else if (_db.object_type(id) == "sound_source")
+			show_sound_source(id);
+		else
+			_stack.set_visible_child(_unknown_object_type);
 	}
 }