فهرست منبع

tools: add ActionType.SET_{CAMERA,COLLIDER,ACTOR}

Daniele Bartolini 6 سال پیش
والد
کامیت
0c0207cd15

+ 7 - 0
samples/core/editors/level_editor/level_editor.lua

@@ -381,6 +381,13 @@ function UnitBox:set_sprite(material, layer, depth, visible)
 	RenderWorld.sprite_set_visible(LevelEditor._rw, self._unit_id, visible)
 end
 
+function UnitBox:set_camera(projection, fov, near_range, far_range)
+	World.camera_set_projection_type(LevelEditor._world, self._unit_id, projection)
+	World.camera_set_fov(LevelEditor._world, self._unit_id, fov)
+	World.camera_set_near_clip_distance(LevelEditor._world, self._unit_id, near_range)
+	World.camera_set_far_clip_distance(LevelEditor._world, self._unit_id, far_range)
+end
+
 SoundObject = class(SoundObject)
 
 function SoundObject:init(world, id, name, range, volume, loop)

+ 10 - 0
tools/api/engine_api.vala

@@ -302,6 +302,16 @@ namespace Crown
 				);
 		}
 
+		public string set_camera(Guid id, string projection, double fov, double near_range, double far_range)
+		{
+			return @"LevelEditor._objects[\"%s\"]:set_camera(\"%s\", %.17g, %.17g, %.17g)".printf(id.to_string()
+				, projection
+				, fov
+				, near_range
+				, far_range
+				);
+		}
+
 		public string set_placeable(string type, string name)
 		{
 			return "LevelEditor:set_placeable(\"%s\", \"%s\")".printf(type, name);

+ 3 - 0
tools/level_editor/action_type.vala

@@ -17,6 +17,9 @@ namespace Crown
 		SET_LIGHT,
 		SET_MESH,
 		SET_SPRITE,
+		SET_CAMERA,
+		SET_COLLIDER,
+		SET_ACTOR,
 		SET_SOUND
 	}
 }

+ 76 - 0
tools/level_editor/level.vala

@@ -328,6 +328,48 @@ namespace Crown
 			_client.send_script(LevelEditorApi.set_sprite(unit_id, material, layer, depth, visible));
 		}
 
+		public void set_camera(Guid unit_id, Guid component_id, string projection, double fov, double near_range, double far_range)
+		{
+			_db.add_restore_point((int)ActionType.SET_CAMERA, new Guid[] { unit_id });
+
+			Unit unit = new Unit(_db, unit_id, _prefabs);
+			unit.set_component_property_string(component_id, "data.projection", projection);
+			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");
+
+			_client.send_script(LevelEditorApi.set_camera(unit_id, projection, fov, near_range, far_range));
+		}
+
+		public void set_collider(Guid unit_id, Guid component_id, string shape, string scene, string name, string material)
+		{
+			_db.add_restore_point((int)ActionType.SET_COLLIDER, new Guid[] { unit_id });
+
+			Unit unit = new Unit(_db, unit_id, _prefabs);
+			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, "data.material", material);
+			unit.set_component_property_string(component_id, "type", "collider");
+
+			// No synchronization
+		}
+
+		public void set_actor(Guid unit_id, Guid component_id, string class, string collision_filter, string material, double mass)
+		{
+			_db.add_restore_point((int)ActionType.SET_ACTOR, new Guid[] { unit_id });
+
+			Unit unit = new Unit(_db, unit_id, _prefabs);
+			unit.set_component_property_string(component_id, "data.class", class);
+			unit.set_component_property_string(component_id, "data.collision_filter", collision_filter);
+			unit.set_component_property_string(component_id, "data.material", material);
+			unit.set_component_property_double(component_id, "data.mass", mass);
+			unit.set_component_property_string(component_id, "type", "actor");
+
+			// No synchronization
+		}
+
 		public void set_sound(Guid sound_id, string name, double range, double volume, bool loop)
 		{
 			_db.add_restore_point((int)ActionType.SET_SOUND, new Guid[] { sound_id });
@@ -707,6 +749,39 @@ namespace Crown
 				}
 				break;
 
+			case (int)ActionType.SET_CAMERA:
+				{
+					Guid unit_id = data[0];
+					Guid component_id = GUID_ZERO;
+
+					Unit unit = new Unit(_db, unit_id, _prefabs);
+					unit.has_component("camera", ref component_id);
+
+					_client.send_script(LevelEditorApi.set_camera(unit_id
+						, unit.get_component_property_string(component_id, "data.projection")
+						, unit.get_component_property_double(component_id, "data.fov")
+						, unit.get_component_property_double(component_id, "data.near_range")
+						, unit.get_component_property_double(component_id, "data.far_range")
+						));
+					// FIXME: Hack to force update the properties view
+					selection_changed(_selection);
+				}
+				break;
+
+			case (int)ActionType.SET_COLLIDER:
+				{
+					// FIXME: Hack to force update the properties view
+					selection_changed(_selection);
+				}
+				break;
+
+			case (int)ActionType.SET_ACTOR:
+				{
+					// FIXME: Hack to force update the properties view
+					selection_changed(_selection);
+				}
+				break;
+
 			case (int)ActionType.SET_SOUND:
 				{
 					Guid sound_id = data[0];
@@ -720,6 +795,7 @@ namespace Crown
 				break;
 
 			default:
+				stdout.printf("Unknown undo/redo action: %d\n", id);
 				assert(false);
 				break;
 			}

+ 25 - 26
tools/level_editor/properties_view.vala

@@ -315,26 +315,23 @@ namespace Crown
 
 		private void on_value_changed()
 		{
-			Unit unit = new Unit(_level._db, _id, _level._prefabs);
-			unit.set_component_property_string(_component_id, "data.projection", _projection.value);
-			unit.set_component_property_double(_component_id, "data.fov",        _fov.value*(Math.PI/180.0));
-			unit.set_component_property_double(_component_id, "data.near_range", _near_range.value);
-			unit.set_component_property_double(_component_id, "data.far_range",  _far_range.value);
-			unit.set_component_property_string(_component_id, "type",            "camera");
+			_level.set_camera(_id
+				, _component_id
+				, _projection.value
+				, _fov.value*(Math.PI/180.0)
+				, _near_range.value
+				, _far_range.value
+				);
 		}
 
 		public override void update()
 		{
 			Unit unit = new Unit(_level._db, _id, _level._prefabs);
-			string type       = unit.get_component_property_string(_component_id, "data.projection");
-			double fov        = unit.get_component_property_double(_component_id, "data.fov");
-			double near_range = unit.get_component_property_double(_component_id, "data.near_range");
-			double far_range  = unit.get_component_property_double(_component_id, "data.far_range");
 
-			_projection.value = type;
-			_fov.value        = fov*(180.0/Math.PI);
-			_near_range.value = near_range;
-			_far_range.value  = far_range;
+			_projection.value = unit.get_component_property_string(_component_id, "data.projection");
+			_fov.value        = unit.get_component_property_double(_component_id, "data.fov") * (180.0/Math.PI);
+			_near_range.value = unit.get_component_property_double(_component_id, "data.near_range");
+			_far_range.value  = unit.get_component_property_double(_component_id, "data.far_range");
 		}
 	}
 
@@ -372,12 +369,13 @@ namespace Crown
 
 		private void on_value_changed()
 		{
-			Unit unit = new Unit(_level._db, _id, _level._prefabs);
-			unit.set_component_property_string(_component_id, "data.shape",    _shape.text);
-			unit.set_component_property_string(_component_id, "data.scene",    _scene.value);
-			unit.set_component_property_string(_component_id, "data.name",     _name.text);
-			unit.set_component_property_string(_component_id, "data.material", _material.text);
-			unit.set_component_property_string(_component_id, "type",          "collider");
+			_level.set_collider(_id
+				, _component_id
+				, _shape.text
+				, _scene.value
+				, _name.text
+				, _material.text
+				);
 		}
 
 		public override void update()
@@ -424,12 +422,13 @@ namespace Crown
 
 		private void on_value_changed()
 		{
-			Unit unit = new Unit(_level._db, _id, _level._prefabs);
-			unit.set_component_property_string(_component_id, "data.class",            _class.text);
-			unit.set_component_property_string(_component_id, "data.collision_filter", _collision_filter.text);
-			unit.set_component_property_string(_component_id, "data.material",         _material.text);
-			unit.set_component_property_double(_component_id, "data.mass",             _mass.value);
-			unit.set_component_property_string(_component_id, "type",                  "actor");
+			_level.set_actor(_id
+				, _component_id
+				, _class.text
+				, _collision_filter.text
+				, _material.text
+				, _mass.value
+				);
 		}
 
 		public override void update()