Prechádzať zdrojové kódy

tools: use constants

Daniele Bartolini 4 rokov pred
rodič
commit
3d50e2662a

+ 18 - 18
tools/level_editor/level.vala

@@ -218,7 +218,7 @@ public class Level
 	public void on_unit_spawned(Guid id, string? name, Vector3 pos, Quaternion rot, Vector3 scl)
 	{
 		_db.add_restore_point((int)ActionType.SPAWN_UNIT, new Guid[] { id });
-		_db.create(id, "unit");
+		_db.create(id, OBJECT_TYPE_UNIT);
 		_db.set_property_string(id, "editor.name", "unit_%04u".printf(_num_units++));
 
 		if (name != null)
@@ -229,12 +229,12 @@ public class Level
 
 		Unit unit = new Unit(_db, id);
 		Guid component_id;
-		if (unit.has_component(out component_id, "transform"))
+		if (unit.has_component(out component_id, OBJECT_TYPE_TRANSFORM))
 		{
 			unit.set_component_property_vector3   (component_id, "data.position", pos);
 			unit.set_component_property_quaternion(component_id, "data.rotation", rot);
 			unit.set_component_property_vector3   (component_id, "data.scale", scl);
-			unit.set_component_property_string    (component_id, "type", "transform");
+			unit.set_component_property_string    (component_id, "type", OBJECT_TYPE_TRANSFORM);
 		}
 		else
 		{
@@ -248,7 +248,7 @@ public class Level
 	public void on_sound_spawned(Guid id, string name, Vector3 pos, Quaternion rot, Vector3 scl, double range, double volume, bool loop)
 	{
 		_db.add_restore_point((int)ActionType.SPAWN_SOUND, new Guid[] { id });
-		_db.create(id, "sound_source");
+		_db.create(id, OBJECT_TYPE_SOUND_SOURCE);
 		_db.set_property_string    (id, "editor.name", "sound_%04u".printf(_num_sounds++));
 		_db.set_property_vector3   (id, "position", pos);
 		_db.set_property_quaternion(id, "rotation", rot);
@@ -274,7 +274,7 @@ public class Level
 			{
 				Unit unit = new Unit(_db, id);
 				Guid component_id;
-				if (unit.has_component(out component_id, "transform"))
+				if (unit.has_component(out component_id, OBJECT_TYPE_TRANSFORM))
 				{
 					unit.set_component_property_vector3   (component_id, "data.position", pos);
 					unit.set_component_property_quaternion(component_id, "data.rotation", rot);
@@ -333,7 +333,7 @@ public class Level
 		unit.set_component_property_double (component_id, "data.intensity",  intensity);
 		unit.set_component_property_double (component_id, "data.spot_angle", spot_angle);
 		unit.set_component_property_vector3(component_id, "data.color",      color);
-		unit.set_component_property_string (component_id, "type", "light");
+		unit.set_component_property_string (component_id, "type", OBJECT_TYPE_LIGHT);
 
 		_client.send_script(LevelEditorApi.set_light(unit_id, type, range, intensity, spot_angle, color));
 		_client.send(DeviceApi.frame());
@@ -348,7 +348,7 @@ public class Level
 		unit.set_component_property_string(component_id, "data.geometry_name", geometry);
 		unit.set_component_property_string(component_id, "data.material", material);
 		unit.set_component_property_bool  (component_id, "data.visible", visible);
-		unit.set_component_property_string(component_id, "type", "mesh_renderer");
+		unit.set_component_property_string(component_id, "type", OBJECT_TYPE_MESH_RENDERER);
 
 		_client.send_script(LevelEditorApi.set_mesh(unit_id, material, visible));
 		_client.send(DeviceApi.frame());
@@ -364,7 +364,7 @@ public class Level
 		unit.set_component_property_string(component_id, "data.material", material);
 		unit.set_component_property_string(component_id, "data.sprite_resource", sprite_resource);
 		unit.set_component_property_bool  (component_id, "data.visible", visible);
-		unit.set_component_property_string(component_id, "type", "sprite_renderer");
+		unit.set_component_property_string(component_id, "type", OBJECT_TYPE_SPRITE_RENDERER);
 
 		_client.send_script(LevelEditorApi.set_sprite(unit_id, sprite_resource, material, layer, depth, visible));
 		_client.send(DeviceApi.frame());
@@ -379,7 +379,7 @@ public class Level
 		unit.set_component_property_double(component_id, "data.fov", fov);
 		unit.set_component_property_double(component_id, "data.near_range", near_range);
 		unit.set_component_property_double(component_id, "data.far_range", far_range);
-		unit.set_component_property_string(component_id, "type", "camera");
+		unit.set_component_property_string(component_id, "type", OBJECT_TYPE_CAMERA);
 
 		_client.send_script(LevelEditorApi.set_camera(unit_id, projection, fov, near_range, far_range));
 		_client.send(DeviceApi.frame());
@@ -393,7 +393,7 @@ public class Level
 		unit.set_component_property_string(component_id, "data.shape", shape);
 		unit.set_component_property_string(component_id, "data.scene", scene);
 		unit.set_component_property_string(component_id, "data.name", name);
-		unit.set_component_property_string(component_id, "type", "collider");
+		unit.set_component_property_string(component_id, "type", OBJECT_TYPE_COLLIDER);
 
 		// No synchronization.
 	}
@@ -413,7 +413,7 @@ public class Level
 		unit.set_component_property_bool  (component_id, "data.lock_translation_x", (bool)unit.get_component_property_bool(component_id, "data.lock_translation_x"));
 		unit.set_component_property_bool  (component_id, "data.lock_translation_y", (bool)unit.get_component_property_bool(component_id, "data.lock_translation_y"));
 		unit.set_component_property_bool  (component_id, "data.lock_translation_z", (bool)unit.get_component_property_bool(component_id, "data.lock_translation_z"));
-		unit.set_component_property_string(component_id, "type", "actor");
+		unit.set_component_property_string(component_id, "type", OBJECT_TYPE_ACTOR);
 
 		// No synchronization.
 	}
@@ -424,7 +424,7 @@ public class Level
 
 		Unit unit = new Unit(_db, unit_id);
 		unit.set_component_property_string(component_id, "data.script_resource", script_resource);
-		unit.set_component_property_string(component_id, "type", "script");
+		unit.set_component_property_string(component_id, "type", OBJECT_TYPE_SCRIPT);
 
 		// No synchronization.
 	}
@@ -435,7 +435,7 @@ public class Level
 
 		Unit unit = new Unit(_db, unit_id);
 		unit.set_component_property_string(component_id, "data.state_machine_resource", state_machine_resource);
-		unit.set_component_property_string(component_id, "type", "animation_state_machine");
+		unit.set_component_property_string(component_id, "type", OBJECT_TYPE_ANIMATION_STATE_MACHINE);
 
 		// No synchronization.
 	}
@@ -697,7 +697,7 @@ public class Level
 
 						Unit unit = new Unit(_db, unit_id);
 						Guid component_id;
-						if (unit.has_component(out component_id, "transform"))
+						if (unit.has_component(out component_id, OBJECT_TYPE_TRANSFORM))
 						{
 							positions[i] = unit.get_component_property_vector3   (component_id, "data.position");
 							rotations[i] = unit.get_component_property_quaternion(component_id, "data.rotation");
@@ -749,7 +749,7 @@ public class Level
 
 				Unit unit = new Unit(_db, unit_id);
 				Guid component_id;
-				unit.has_component(out component_id, "light");
+				unit.has_component(out component_id, OBJECT_TYPE_LIGHT);
 
 				_client.send_script(LevelEditorApi.set_light(unit_id
 					, unit.get_component_property_string (component_id, "data.type")
@@ -770,7 +770,7 @@ public class Level
 
 				Unit unit = new Unit(_db, unit_id);
 				Guid component_id;
-				unit.has_component(out component_id, "mesh_renderer");
+				unit.has_component(out component_id, OBJECT_TYPE_MESH_RENDERER);
 
 				_client.send_script(LevelEditorApi.set_mesh(unit_id
 					, unit.get_component_property_string(component_id, "data.material")
@@ -788,7 +788,7 @@ public class Level
 
 				Unit unit = new Unit(_db, unit_id);
 				Guid component_id;
-				unit.has_component(out component_id, "sprite_renderer");
+				unit.has_component(out component_id, OBJECT_TYPE_SPRITE_RENDERER);
 
 				_client.send_script(LevelEditorApi.set_sprite(unit_id
 					, unit.get_component_property_string(component_id, "data.sprite_resource")
@@ -809,7 +809,7 @@ public class Level
 
 				Unit unit = new Unit(_db, unit_id);
 				Guid component_id;
-				unit.has_component(out component_id, "camera");
+				unit.has_component(out component_id, OBJECT_TYPE_CAMERA);
 
 				_client.send_script(LevelEditorApi.set_camera(unit_id
 					, unit.get_component_property_string(component_id, "data.projection")

+ 1 - 1
tools/level_editor/project.vala

@@ -446,7 +446,7 @@ public class Project
 		string name = type == "" ? path : path.substring(0, path.last_index_of("."));
 
 		Guid id = Guid.new_guid();
-		_files.create(id, "file");
+		_files.create(id, OBJECT_TYPE_FILE);
 		_files.set_property_string(id, "path", path);
 		_files.set_property_string(id, "type", type);
 		_files.set_property_string(id, "name", name);

+ 2 - 2
tools/level_editor/properties_view.vala

@@ -815,9 +815,9 @@ public class PropertiesView : Gtk.Bin
 		if (!_db.has_object(id))
 			return;
 
-		if (_db.object_type(id) == "unit")
+		if (_db.object_type(id) == OBJECT_TYPE_UNIT)
 			show_unit(id);
-		else if (_db.object_type(id) == "sound_source")
+		else if (_db.object_type(id) == OBJECT_TYPE_SOUND_SOURCE)
 			show_sound_source(id);
 		else
 			_stack.set_visible_child(_unknown_object_type);

+ 13 - 13
tools/resource/mesh_resource.vala

@@ -22,10 +22,10 @@ public class MeshResource
 		// Create transform
 		{
 			Guid component_id;
-			if (!unit.has_component(out component_id, "transform"))
+			if (!unit.has_component(out component_id, OBJECT_TYPE_TRANSFORM))
 			{
 				component_id = Guid.new_guid();
-				db.create(component_id, "transform");
+				db.create(component_id, OBJECT_TYPE_TRANSFORM);
 				db.add_to_set(unit_id, "components", component_id);
 			}
 
@@ -37,10 +37,10 @@ public class MeshResource
 		// Create mesh_renderer
 		{
 			Guid component_id;
-			if (!unit.has_component(out component_id, "mesh_renderer"))
+			if (!unit.has_component(out component_id, OBJECT_TYPE_MESH_RENDERER))
 			{
 				component_id = Guid.new_guid();
-				db.create(component_id, "mesh_renderer");
+				db.create(component_id, OBJECT_TYPE_MESH_RENDERER);
 				db.add_to_set(unit_id, "components", component_id);
 			}
 
@@ -53,10 +53,10 @@ public class MeshResource
 		// Create collider
 		{
 			Guid component_id;
-			if (!unit.has_component(out component_id, "collider"))
+			if (!unit.has_component(out component_id, OBJECT_TYPE_COLLIDER))
 			{
 				component_id = Guid.new_guid();
-				db.create(component_id, "collider");
+				db.create(component_id, OBJECT_TYPE_COLLIDER);
 				db.add_to_set(unit_id, "components", component_id);
 			}
 
@@ -68,10 +68,10 @@ public class MeshResource
 		// Create actor
 		{
 			Guid component_id;
-			if (!unit.has_component(out component_id, "actor"))
+			if (!unit.has_component(out component_id, OBJECT_TYPE_ACTOR))
 			{
 				component_id = Guid.new_guid();
-				db.create(component_id, "actor");
+				db.create(component_id, OBJECT_TYPE_ACTOR);
 				db.add_to_set(unit_id, "components", component_id);
 			}
 
@@ -90,7 +90,7 @@ public class MeshResource
 			foreach (var child in children.entries)
 			{
 				Guid new_unit_id = Guid.new_guid();
-				db.create(new_unit_id, "unit");
+				db.create(new_unit_id, OBJECT_TYPE_UNIT);
 				create_components(db, unit_id, new_unit_id, material_name, resource_name, child.key,(Hashtable)child.value);
 			}
 		}
@@ -163,7 +163,7 @@ public class MeshResource
 			{
 				db = new Database();
 				unit_id = Guid.new_guid();
-				db.create(unit_id, "unit");
+				db.create(unit_id, OBJECT_TYPE_UNIT);
 			}
 
 			Hashtable mesh = SJSON.load_from_path(filename_i);
@@ -177,10 +177,10 @@ public class MeshResource
 				Unit unit = new Unit(db, unit_id);
 
 				Guid component_id;
-				if (!unit.has_component(out component_id, "transform"))
+				if (!unit.has_component(out component_id, OBJECT_TYPE_TRANSFORM))
 				{
 					component_id = Guid.new_guid();
-					db.create(component_id, "transform");
+					db.create(component_id, OBJECT_TYPE_TRANSFORM);
 					db.add_to_set(unit_id, "components", component_id);
 				}
 
@@ -197,7 +197,7 @@ public class MeshResource
 					// If the mesh contains multiple root objects, create a new unit for each
 					// one of those, otherwise put the components inside the base unit.
 					new_unit_id = Guid.new_guid();
-					db.create(new_unit_id, "unit");
+					db.create(new_unit_id, OBJECT_TYPE_UNIT);
 				}
 				create_components(db, unit_id, new_unit_id, material_name, resource_name, entry.key, (Hashtable)entry.value);
 			}

+ 11 - 11
tools/resource/sprite_resource.vala

@@ -168,7 +168,7 @@ public class SpriteResource
 			{
 				db = new Database();
 				unit_id = Guid.new_guid();
-				db.create(unit_id, "unit");
+				db.create(unit_id, OBJECT_TYPE_UNIT);
 			}
 
 			Unit unit = new Unit(db, unit_id);
@@ -176,10 +176,10 @@ public class SpriteResource
 			// Create transform
 			{
 				Guid component_id;
-				if (!unit.has_component(out component_id, "transform"))
+				if (!unit.has_component(out component_id, OBJECT_TYPE_TRANSFORM))
 				{
 					component_id = Guid.new_guid();
-					db.create(component_id, "transform");
+					db.create(component_id, OBJECT_TYPE_TRANSFORM);
 					db.add_to_set(unit_id, "components", component_id);
 				}
 
@@ -191,10 +191,10 @@ public class SpriteResource
 			// Create sprite_renderer
 			{
 				Guid component_id;
-				if (!unit.has_component(out component_id, "sprite_renderer"))
+				if (!unit.has_component(out component_id, OBJECT_TYPE_SPRITE_RENDERER))
 				{
 					component_id = Guid.new_guid();
-					db.create(component_id, "sprite_renderer");
+					db.create(component_id, OBJECT_TYPE_SPRITE_RENDERER);
 					db.add_to_set(unit_id, "components", component_id);
 				}
 
@@ -213,10 +213,10 @@ public class SpriteResource
 					Quaternion rotation = QUATERNION_IDENTITY;
 
 					Guid component_id;
-					if (!unit.has_component(out component_id, "collider"))
+					if (!unit.has_component(out component_id, OBJECT_TYPE_COLLIDER))
 					{
 						component_id = Guid.new_guid();
-						db.create(component_id, "collider");
+						db.create(component_id, OBJECT_TYPE_COLLIDER);
 						db.add_to_set(unit_id, "components", component_id);
 					}
 
@@ -261,10 +261,10 @@ public class SpriteResource
 				// Create actor
 				{
 					Guid component_id;
-					if (!unit.has_component(out component_id, "actor"))
+					if (!unit.has_component(out component_id, OBJECT_TYPE_ACTOR))
 					{
 						component_id = Guid.new_guid();
-						db.create(component_id, "actor");
+						db.create(component_id, OBJECT_TYPE_ACTOR);
 						db.add_to_set(unit_id, "components", component_id);
 					}
 
@@ -284,12 +284,12 @@ public class SpriteResource
 			{
 				// Destroy collider and actor if any
 				Guid component_id;
-				if (unit.has_component(out component_id, "collider"))
+				if (unit.has_component(out component_id, OBJECT_TYPE_COLLIDER))
 				{
 					db.remove_from_set(unit_id, "components", component_id);
 					db.destroy(component_id);
 				}
-				if (unit.has_component(out component_id, "actor"))
+				if (unit.has_component(out component_id, OBJECT_TYPE_ACTOR))
 				{
 					db.remove_from_set(unit_id, "components", component_id);
 					db.destroy(component_id);

+ 23 - 0
tools/resource/types.vala

@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2012-2021 Daniele Bartolini et al.
+ * License: https://github.com/dbartolini/crown/blob/master/LICENSE
+ */
+
+namespace Crown
+{
+	const string OBJECT_TYPE_ACTOR                   = "actor";
+	const string OBJECT_TYPE_ANIMATION_STATE_MACHINE = "animation_state_machine";
+	const string OBJECT_TYPE_CAMERA                  = "camera";
+	const string OBJECT_TYPE_COLLIDER                = "collider";
+	const string OBJECT_TYPE_FILE                    = "file";
+	const string OBJECT_TYPE_LEVEL                   = "level";
+	const string OBJECT_TYPE_LIGHT                   = "light";
+	const string OBJECT_TYPE_MESH_RENDERER           = "mesh_renderer";
+	const string OBJECT_TYPE_SCRIPT                  = "script";
+	const string OBJECT_TYPE_SOUND_SOURCE            = "sound_source";
+	const string OBJECT_TYPE_SPRITE_RENDERER         = "sprite_renderer";
+	const string OBJECT_TYPE_STATE_MACHINE           = "state_machine";
+	const string OBJECT_TYPE_TRANSFORM               = "transform";
+	const string OBJECT_TYPE_UNIT                    = "unit";
+
+}