Save and copy editor flags with scene. Replaced EditorComponent with DontSafe-flag

This commit is contained in:
Simon Lübeß
2024-03-10 13:11:53 +01:00
parent 72bd152999
commit fc475eb468
5 changed files with 40 additions and 55 deletions
+1
View File
@@ -5,4 +5,5 @@ enum EditorFlags
Default = 0,
HideInHierarchy = 1,
HideInScene = 2,
DontSave = 4
}
@@ -36,16 +36,6 @@ namespace GlitchyEngine.World
}
}
/// If an entity has the EditorComponent it won't be displayed in the scene hierarchy.
struct EditorComponent
{
bool b = false;
public this()
{
}
}
[Component("Sprite Renderer")]
struct SpriteRendererComponent
{
+21 -4
View File
@@ -143,7 +143,6 @@ namespace GlitchyEngine.World
// Copy components
CopyComponents<MeshRendererComponent>(this, target);
CopyComponents<MeshComponent>(this, target);
CopyComponents<EditorComponent>(this, target);
CopyComponents<SpriteRendererComponent>(this, target);
CopyComponents<CircleRendererComponent>(this, target);
CopyComponents<CameraComponent>(this, target);
@@ -154,6 +153,8 @@ namespace GlitchyEngine.World
CopyComponents<CircleCollider2DComponent>(this, target);
CopyComponents<PolygonCollider2DComponent>(this, target);
CopyComponents<EditorFlagsComponent>(this, target);
if (copyScripts)
{
for (let (sourceHandle, sourceScript) in _ecsWorld.Enumerate<ScriptComponent>())
@@ -193,15 +194,30 @@ namespace GlitchyEngine.World
Entity sourceEntity = .(sourceHandle, source);
Entity targetEntity = target.GetEntityByID(sourceEntity.UUID);
if (targetEntity.TryGetComponent<TComponent>(let targetComponent))
{
*targetComponent = *sourceComponent;
}
else
{
targetEntity.AddComponent<TComponent>(*sourceComponent);
}
}
}
private static void CopyComponent<TComponent>(Entity source, Entity target) where TComponent : struct, new
{
if (source.TryGetComponent<TComponent>(let component))
if (source.TryGetComponent<TComponent>(let sourceComponent))
{
target.AddComponent<TComponent>(*component);
if (target.TryGetComponent<TComponent>(let targetComponent))
{
*targetComponent = *sourceComponent;
}
else
{
target.AddComponent<TComponent>(*sourceComponent);
}
}
}
@@ -901,7 +917,6 @@ namespace GlitchyEngine.World
CopyComponent<MeshRendererComponent>(original, copy);
CopyComponent<MeshComponent>(original, copy);
CopyComponent<EditorComponent>(original, copy);
CopyComponent<SpriteRendererComponent>(original, copy);
CopyComponent<CircleRendererComponent>(original, copy);
CopyComponent<CameraComponent>(original, copy);
@@ -912,6 +927,8 @@ namespace GlitchyEngine.World
CopyComponent<CircleCollider2DComponent>(original, copy);
CopyComponent<PolygonCollider2DComponent>(original, copy);
CopyComponent<EditorFlagsComponent>(original, copy);
// Copy ScriptComponent... needs extra handling for the script instances
if (original.TryGetComponent<ScriptComponent>(let sourceScript))
{
+12 -39
View File
@@ -10,6 +10,7 @@ using GlitchyEngine.Renderer;
using GlitchyEngine.Content;
using GlitchyEngine.Scripting;
using GlitchyEngine.Serialization;
using GlitchyEngine.World.Components;
namespace GlitchyEngine.World;
@@ -72,8 +73,7 @@ class SceneSerializer
{
Entity entity = .(e, _scene);
// TODO: also serialize object with EditorComponent (e.g. to save the location of the editor camera)
if (entity.HasComponent<EditorComponent>())
if (entity.EditorFlags.HasFlag(.DontSave))
continue;
SerializeEntity(writer, entity);
@@ -113,8 +113,6 @@ class SceneSerializer
{
Serialize.Value(writer, "Id", entity.UUID);
SerializeComponent<EditorComponent>(writer, entity, "EditorComponent", scope (component) => {});
SerializeComponent<NameComponent>(writer, entity, "NameComponent", scope (component) =>
{
Serialize.Value(writer, "Name", component.Name);
@@ -245,41 +243,11 @@ class SceneSerializer
_objectsNotWritten.Remove(entity.UUID);
}
});
//if (component.HasScript)
// TODO: Thats not a good check, I think. At least we know the script class is valid
//if (ScriptEngine.GetScriptClass(component.ScriptClassName) != null)
//{
//Serialize.Value(writer, "Fields", );
// TODO: Serialize Script Instance!
/*let fields = ScriptEngine.GetScriptFieldMap(entity);
writer.Identifier("Fields");
using (writer.ArrayBlock())
SerializeComponent<EditorFlagsComponent>(writer, entity, "Editor", scope (component) =>
{
for (var (fieldName, fieldInstance) in fields)
{
if (fieldInstance.Type == .None)
continue;
switch (fieldInstance.Type)
{
case .Enum, .Class, .Struct:
// TODO: implement
default:
writer.Identifier(fieldName);
String str = scope .();
fieldInstance.Type.ToString(str);
writer.Type(str);
Serialize.Value(writer, ValueView(fieldInstance.Type.GetBeefType(), &fieldInstance.[Friend]_data), gBonEnv);
}
}
}*/
//}
Serialize.Value(writer, "Flags", component.Flags);
});
}
@@ -450,8 +418,6 @@ class SceneSerializer
switch(identifier)
{
case "EditorComponent":
Try!(DeserializeComponent<EditorComponent>(reader, entity, scope (component) => { return .Ok; }));
case "NameComponent":
Try!(DeserializeComponent<NameComponent>(reader, entity, scope (component) =>
{
@@ -698,6 +664,13 @@ class SceneSerializer
SerializedObject.BonDeserialize(reader, _serializedObjects, gBonEnv);
}
return .Ok;
}));
case "Editor":
Try!(DeserializeComponent<EditorFlagsComponent>(reader, entity, scope (component) =>
{
Try!(Deserialize.Value(reader, "Flags", out component.Flags));
return .Ok;
}));
default:
+4
View File
@@ -17,4 +17,8 @@ public enum EditorFlags : byte
/// The entity is hidden in the scene.
/// </summary>
HideInScene = 2,
/// <summary>
/// The entity will not be saved.
/// </summary>
DontSave = 4
}